Skip to content

Commit 3a930c2

Browse files
authored
#906 -Added methods in status API supports for saving and reading binary 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]>
1 parent ccf2bfd commit 3a930c2

File tree

12 files changed

+1042
-350
lines changed

12 files changed

+1042
-350
lines changed

examples/Client/StateManagement/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ------------------------------------------------------------------------
1+
// ------------------------------------------------------------------------
22
// Copyright 2021 The Dapr Authors
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -24,7 +24,8 @@ class Program
2424
new StateStoreExample(),
2525
new StateStoreTransactionsExample(),
2626
new StateStoreETagsExample(),
27-
new BulkStateExample()
27+
new BulkStateExample(),
28+
new StateStoreBinaryExample()
2829
};
2930

3031
static async Task<int> Main(string[] args)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Dapr.Client;
5+
using System.Threading.Tasks;
6+
using System.Threading;
7+
using Google.Protobuf;
8+
9+
namespace Samples.Client
10+
{
11+
public class StateStoreBinaryExample : Example
12+
{
13+
14+
private static readonly string stateKeyName = "binarydata";
15+
private static readonly string storeName = "statestore";
16+
17+
public override string DisplayName => "Using the State Store with binary data";
18+
19+
public override async Task RunAsync(CancellationToken cancellationToken)
20+
{
21+
using var client = new DaprClientBuilder().Build();
22+
23+
var state = "Test Binary Data";
24+
// convert variable in to byte array
25+
var stateBytes = Encoding.UTF8.GetBytes(state);
26+
await client.SaveByteStateAsync(storeName, stateKeyName, stateBytes.AsMemory(), cancellationToken: cancellationToken);
27+
Console.WriteLine("Saved State!");
28+
29+
var responseBytes = await client.GetByteStateAsync(storeName, stateKeyName, cancellationToken: cancellationToken);
30+
var savedState = Encoding.UTF8.GetString(ByteString.CopyFrom(responseBytes.Span).ToByteArray());
31+
32+
if (savedState == null)
33+
{
34+
Console.WriteLine("State not found in store");
35+
}
36+
else
37+
{
38+
Console.WriteLine($"Got State: {savedState}");
39+
}
40+
41+
await client.DeleteStateAsync(storeName, stateKeyName, cancellationToken: cancellationToken);
42+
Console.WriteLine("Deleted State!");
43+
}
44+
45+
46+
}
47+
}

src/Dapr.Client/DaprClient.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,80 @@ public abstract Task SaveStateAsync<TValue>(
850850
IReadOnlyDictionary<string, string> metadata = default,
851851
CancellationToken cancellationToken = default);
852852

853+
854+
/// <summary>
855+
/// Saves the provided <paramref name="binaryValue" /> associated with the provided <paramref name="key" /> to the Dapr state
856+
/// store
857+
/// </summary>
858+
/// <param name="storeName">The name of the state store.</param>
859+
/// <param name="key">The state key.</param>
860+
/// <param name="binaryValue">The binary data that will be stored in the state store.</param>
861+
/// <param name="stateOptions">Options for performing save state operation.</param>
862+
/// <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>
863+
/// <param name="cancellationToken">A <see cref="CancellationToken" /> that can be used to cancel the operation.</param>
864+
/// <returns>A <see cref="Task" /> that will complete when the operation has completed.</returns>
865+
public abstract Task SaveByteStateAsync(
866+
string storeName,
867+
string key,
868+
ReadOnlyMemory<byte> binaryValue,
869+
StateOptions stateOptions = default,
870+
IReadOnlyDictionary<string, string> metadata = default,
871+
CancellationToken cancellationToken = default);
872+
873+
/// <summary>
874+
///Saves the provided <paramref name="binaryValue" /> associated with the provided <paramref name="key" /> using the
875+
/// <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.
876+
/// </summary>
877+
/// <param name="storeName">The name of the state store.</param>
878+
/// <param name="key">The state key.</param>
879+
/// <param name="binaryValue">The binary data that will be stored in the state store.</param>
880+
/// <param name="etag">An ETag.</param>
881+
/// <param name="stateOptions">Options for performing save state operation.</param>
882+
/// <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>
883+
/// <param name="cancellationToken">A <see cref="CancellationToken" /> that can be used to cancel the operation.</param>
884+
/// <returns>A <see cref="Task" /> that will complete when the operation has completed.</returns>
885+
public abstract Task<bool> TrySaveByteStateAsync(
886+
string storeName,
887+
string key,
888+
ReadOnlyMemory<byte> binaryValue,
889+
string etag,
890+
StateOptions stateOptions = default,
891+
IReadOnlyDictionary<string, string> metadata = default,
892+
CancellationToken cancellationToken = default);
893+
894+
895+
/// <summary>
896+
/// Gets the current binary value associated with the <paramref name="key" /> from the Dapr state store.
897+
/// </summary>
898+
/// <param name="storeName">The name of state store to read from.</param>
899+
/// <param name="key">The state key.</param>
900+
/// <param name="consistencyMode">The consistency mode <see cref="ConsistencyMode" />.</param>
901+
/// <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>
902+
/// <param name="cancellationToken">A <see cref="CancellationToken" /> that can be used to cancel the operation.</param>
903+
/// <returns>A <see cref="Task{T}" /> that will return the value when the operation has completed.</returns>
904+
public abstract Task<ReadOnlyMemory<byte>> GetByteStateAsync(
905+
string storeName,
906+
string key,
907+
ConsistencyMode? consistencyMode = default,
908+
IReadOnlyDictionary<string, string> metadata = default,
909+
CancellationToken cancellationToken = default);
910+
911+
/// <summary>
912+
/// Gets the current binary value associated with the <paramref name="key" /> from the Dapr state store and an ETag.
913+
/// </summary>
914+
/// <param name="storeName">The name of the state store.</param>
915+
/// <param name="key">The state key.</param>
916+
/// <param name="consistencyMode">The consistency mode <see cref="ConsistencyMode" />.</param>
917+
/// <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>
918+
/// <param name="cancellationToken">A <see cref="CancellationToken" /> that can be used to cancel the operation.</param>
919+
/// <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>
920+
public abstract Task<(ReadOnlyMemory<byte>, string etag)> GetByteStateAndETagAsync(
921+
string storeName,
922+
string key,
923+
ConsistencyMode? consistencyMode = default,
924+
IReadOnlyDictionary<string, string> metadata = default,
925+
CancellationToken cancellationToken = default);
926+
853927
/// <summary>
854928
/// Tries to save the state <paramref name="value" /> associated with the provided <paramref name="key" /> using the
855929
/// <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.

0 commit comments

Comments
 (0)