Skip to content

Commit

Permalink
#906 -Added methods in status API supports for saving and reading bin…
Browse files Browse the repository at this point in the history
…ary data (#1116)

* Added methods in status API supports for direct storage and reading of byte arrays #906

Signed-off-by: Divya Perumal <[email protected]>
Signed-off-by: Divya Perumal <[email protected]>
  • Loading branch information
divzi-p authored Dec 11, 2024
1 parent ccf2bfd commit 3a930c2
Show file tree
Hide file tree
Showing 12 changed files with 1,042 additions and 350 deletions.
5 changes: 3 additions & 2 deletions examples/Client/StateManagement/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// Copyright 2021 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -24,7 +24,8 @@ class Program
new StateStoreExample(),
new StateStoreTransactionsExample(),
new StateStoreETagsExample(),
new BulkStateExample()
new BulkStateExample(),
new StateStoreBinaryExample()
};

static async Task<int> Main(string[] args)
Expand Down
47 changes: 47 additions & 0 deletions examples/Client/StateManagement/StateStoreBinaryExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Text;
using Dapr.Client;
using System.Threading.Tasks;
using System.Threading;
using Google.Protobuf;

namespace Samples.Client
{
public class StateStoreBinaryExample : Example
{

private static readonly string stateKeyName = "binarydata";
private static readonly string storeName = "statestore";

public override string DisplayName => "Using the State Store with binary data";

public override async Task RunAsync(CancellationToken cancellationToken)
{
using var client = new DaprClientBuilder().Build();

var state = "Test Binary Data";
// convert variable in to byte array
var stateBytes = Encoding.UTF8.GetBytes(state);
await client.SaveByteStateAsync(storeName, stateKeyName, stateBytes.AsMemory(), cancellationToken: cancellationToken);
Console.WriteLine("Saved State!");

var responseBytes = await client.GetByteStateAsync(storeName, stateKeyName, cancellationToken: cancellationToken);
var savedState = Encoding.UTF8.GetString(ByteString.CopyFrom(responseBytes.Span).ToByteArray());

if (savedState == null)
{
Console.WriteLine("State not found in store");
}
else
{
Console.WriteLine($"Got State: {savedState}");
}

await client.DeleteStateAsync(storeName, stateKeyName, cancellationToken: cancellationToken);
Console.WriteLine("Deleted State!");
}


}
}
74 changes: 74 additions & 0 deletions src/Dapr.Client/DaprClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,80 @@ public abstract Task SaveStateAsync<TValue>(
IReadOnlyDictionary<string, string> metadata = default,
CancellationToken cancellationToken = default);


/// <summary>
/// Saves the provided <paramref name="binaryValue" /> associated with the provided <paramref name="key" /> to the Dapr state
/// store
/// </summary>
/// <param name="storeName">The name of the state store.</param>
/// <param name="key">The state key.</param>
/// <param name="binaryValue">The binary data that will be stored in the state store.</param>
/// <param name="stateOptions">Options for performing save state operation.</param>
/// <param name="metadata">A collection of metadata key-value pairs that will be provided to the state store. The valid metadata keys and values are determined by the type of state store used.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> that can be used to cancel the operation.</param>
/// <returns>A <see cref="Task" /> that will complete when the operation has completed.</returns>
public abstract Task SaveByteStateAsync(
string storeName,
string key,
ReadOnlyMemory<byte> binaryValue,
StateOptions stateOptions = default,
IReadOnlyDictionary<string, string> metadata = default,
CancellationToken cancellationToken = default);

/// <summary>
///Saves the provided <paramref name="binaryValue" /> associated with the provided <paramref name="key" /> using the
/// <paramref name="etag"/> to the Dapr state. State store implementation will allow the update only if the attached ETag matches with the latest ETag in the state store.
/// </summary>
/// <param name="storeName">The name of the state store.</param>
/// <param name="key">The state key.</param>
/// <param name="binaryValue">The binary data that will be stored in the state store.</param>
/// <param name="etag">An ETag.</param>
/// <param name="stateOptions">Options for performing save state operation.</param>
/// <param name="metadata">A collection of metadata key-value pairs that will be provided to the state store. The valid metadata keys and values are determined by the type of state store used.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> that can be used to cancel the operation.</param>
/// <returns>A <see cref="Task" /> that will complete when the operation has completed.</returns>
public abstract Task<bool> TrySaveByteStateAsync(
string storeName,
string key,
ReadOnlyMemory<byte> binaryValue,
string etag,
StateOptions stateOptions = default,
IReadOnlyDictionary<string, string> metadata = default,
CancellationToken cancellationToken = default);


/// <summary>
/// Gets the current binary value associated with the <paramref name="key" /> from the Dapr state store.
/// </summary>
/// <param name="storeName">The name of state store to read from.</param>
/// <param name="key">The state key.</param>
/// <param name="consistencyMode">The consistency mode <see cref="ConsistencyMode" />.</param>
/// <param name="metadata">A collection of metadata key-value pairs that will be provided to the state store. The valid metadata keys and values are determined by the type of state store used.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> that can be used to cancel the operation.</param>
/// <returns>A <see cref="Task{T}" /> that will return the value when the operation has completed.</returns>
public abstract Task<ReadOnlyMemory<byte>> GetByteStateAsync(
string storeName,
string key,
ConsistencyMode? consistencyMode = default,
IReadOnlyDictionary<string, string> metadata = default,
CancellationToken cancellationToken = default);

/// <summary>
/// Gets the current binary value associated with the <paramref name="key" /> from the Dapr state store and an ETag.
/// </summary>
/// <param name="storeName">The name of the state store.</param>
/// <param name="key">The state key.</param>
/// <param name="consistencyMode">The consistency mode <see cref="ConsistencyMode" />.</param>
/// <param name="metadata">A collection of metadata key-value pairs that will be provided to the state store. The valid metadata keys and values are determined by the type of state store used.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> that can be used to cancel the operation.</param>
/// <returns>A <see cref="Task{T}" /> that will return the value when the operation has completed. This wraps the read value and an ETag.</returns>
public abstract Task<(ReadOnlyMemory<byte>, string etag)> GetByteStateAndETagAsync(
string storeName,
string key,
ConsistencyMode? consistencyMode = default,
IReadOnlyDictionary<string, string> metadata = default,
CancellationToken cancellationToken = default);

/// <summary>
/// Tries to save the state <paramref name="value" /> associated with the provided <paramref name="key" /> using the
/// <paramref name="etag"/> to the Dapr state. State store implementation will allow the update only if the attached ETag matches with the latest ETag in the state store.
Expand Down
Loading

0 comments on commit 3a930c2

Please sign in to comment.