Added DeleteBulkState operation to dotnet sdk (#637)

Co-authored-by: Ryan Nowak <nowakra@gmail.com>
This commit is contained in:
Jigar 2021-05-01 04:05:44 +05:30 committed by GitHub
parent b1658657a5
commit 53a8f96c10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 138 additions and 4 deletions

View File

@ -571,6 +571,15 @@ namespace Dapr.Client
/// <returns>A <see cref="Task{IReadOnlyList}" /> that will return the list of values when the operation has completed.</returns>
public abstract Task<IReadOnlyList<BulkStateItem>> GetBulkStateAsync(string storeName, IReadOnlyList<string> keys, int? parallelism, IReadOnlyDictionary<string, string> metadata = default, CancellationToken cancellationToken = default);
/// <summary>
/// Deletes a list of <paramref name="items" /> from the Dapr state store.
/// </summary>
/// <param name="storeName">The name of state store to delete from.</param>
/// <param name="items">The list of items to delete</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 DeleteBulkStateAsync(string storeName, IReadOnlyList<BulkDeleteStateItem> items, CancellationToken cancellationToken = default);
/// <summary>
/// Gets the current value associated with the <paramref name="key" /> from the Dapr state store and an ETag.
/// </summary>

View File

@ -527,7 +527,7 @@ namespace Dapr.Client
var options = CreateCallOptions(headers: null, cancellationToken);
Autogenerated.GetBulkStateResponse response;
try
{
response = await client.GetBulkStateAsync(envelope, options);
@ -578,7 +578,7 @@ namespace Dapr.Client
var options = CreateCallOptions(headers: null, cancellationToken);
Autogenerated.GetStateResponse response;
try
{
response = await client.GetStateAsync(envelope, options);
@ -598,6 +598,53 @@ namespace Dapr.Client
}
}
/// <inheritdoc />
public override async Task DeleteBulkStateAsync(string storeName, IReadOnlyList<BulkDeleteStateItem> items, CancellationToken cancellationToken = default)
{
var envelope = new Autogenerated.DeleteBulkStateRequest()
{
StoreName = storeName,
};
foreach (var item in items)
{
var stateItem = new Autogenerated.StateItem()
{
Key = item.Key,
};
if (item.ETag != null)
{
stateItem.Etag = new Autogenerated.Etag() { Value = item.ETag };
}
if (item.Metadata != null)
{
foreach (var kvp in item.Metadata)
{
stateItem.Metadata.Add(kvp.Key, kvp.Value);
}
}
if (item.StateOptions != null)
{
stateItem.Options = ToAutoGeneratedStateOptions(item.StateOptions);
}
envelope.States.Add(stateItem);
}
try
{
await this.Client.DeleteBulkStateAsync(envelope, cancellationToken: cancellationToken);
}
catch (RpcException ex)
{
throw new DaprException("State operation failed: the Dapr endpoint indicated a failure. See InnerException for details.", ex);
}
}
/// <inheritdoc/>
public override async Task<(TValue value, string etag)> GetStateAndETagAsync<TValue>(
string storeName,
@ -630,7 +677,7 @@ namespace Dapr.Client
var options = CreateCallOptions(headers: null, cancellationToken);
Autogenerated.GetStateResponse response;
try
{
response = await client.GetStateAsync(envelope, options);
@ -705,6 +752,7 @@ namespace Dapr.Client
StoreName = storeName,
};
var stateItem = new Autogenerated.StateItem()
{
Key = key,
@ -1042,7 +1090,7 @@ namespace Dapr.Client
options.Headers.Add(this.apiTokenHeader.Value.Key, this.apiTokenHeader.Value.Value);
}
return options;
return options;
}
/// <summary>

View File

@ -0,0 +1,50 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// ------------------------------------------------------------
using System.Collections.Generic;
namespace Dapr.Client
{
/// <summary>
/// Represents a state object used for bulk delete state operation
/// </summary>
public readonly struct BulkDeleteStateItem
{
/// <summary>
/// Initializes a new instance of the <see cref="BulkStateItem"/> class.
/// </summary>
/// <param name="key">The state key.</param>
/// <param name="etag">The ETag.</param>
/// <param name="stateOptions">The stateOptions.</param>
/// <param name="metadata">The metadata.</param>
public BulkDeleteStateItem(string key, string etag, StateOptions stateOptions = default, IReadOnlyDictionary<string, string> metadata = default)
{
this.Key = key;
this.ETag = etag;
StateOptions = stateOptions;
Metadata = metadata;
}
/// <summary>
/// Gets the state key.
/// </summary>
public string Key { get; }
/// <summary>
/// Get the ETag.
/// </summary>
public string ETag { get; }
/// <summary>
/// Gets the StateOptions.
/// </summary>
public StateOptions StateOptions { get; }
/// <summary>
/// Gets the Metadata.
/// </summary>
public IReadOnlyDictionary<string, string> Metadata { get; }
}
}

View File

@ -964,6 +964,33 @@ namespace Dapr.Client.Test
envelope.Options.Consistency.Should().Be(expectedConsistency);
}
[Fact]
public async Task DeleteBulkStateAsync_ValidateRequest()
{
await using var client = TestClient.CreateForDaprClient();
var key = "test";
var etag = "etag";
var metadata = new Dictionary<string, string>
{
{ "partitionKey", "mypartition" }
};
var deleteBulkStateItem = new BulkDeleteStateItem(key, etag, null, metadata);
var request = await client.CaptureGrpcRequestAsync(async daprClient =>
{
await daprClient.DeleteBulkStateAsync("testStore", new List<BulkDeleteStateItem>() { deleteBulkStateItem });
});
request.Dismiss();
// Create Response & Validate
var envelope = await request.GetRequestEnvelopeAsync<Autogenerated.DeleteBulkStateRequest>();
envelope.StoreName.Should().Be("testStore");
envelope.States.Count.Should().Be(1);
envelope.States[0].Key.Should().Be(key);
envelope.States[0].Metadata.Should().ContainKey("partitionKey");
}
private Autogenerated.GetStateResponse MakeGetStateResponse<T>(T state, string etag = null)
{
var data = TypeConverters.ToJsonByteString(state, new JsonSerializerOptions(JsonSerializerDefaults.Web));