-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds PartiQL support to low-level API.
- Loading branch information
Showing
18 changed files
with
759 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/EfficientDynamoDb/DynamoDbContext/DynamoDbLowLevelPartiQLContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using EfficientDynamoDb.Internal; | ||
using EfficientDynamoDb.Internal.Operations.BatchExecuteStatement; | ||
using EfficientDynamoDb.Internal.Operations.ExecuteStatement; | ||
using EfficientDynamoDb.Internal.Operations.ExecuteTransaction; | ||
using EfficientDynamoDb.Internal.Operations.Query; | ||
using EfficientDynamoDb.Internal.Operations.TransactGetItems; | ||
using EfficientDynamoDb.Operations.BatchExecuteStatement; | ||
using EfficientDynamoDb.Operations.ExecuteStatement; | ||
using EfficientDynamoDb.Operations.ExecuteTransaction; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace EfficientDynamoDb | ||
{ | ||
internal class DynamoDbLowLevelPartiQLContext : IDynamoDbLowLevelPartiQLContext | ||
{ | ||
internal DynamoDbContextConfig Config { get; } | ||
internal HttpApi Api { get; } | ||
|
||
internal DynamoDbLowLevelPartiQLContext(DynamoDbContextConfig config, HttpApi api) | ||
{ | ||
Api = api; | ||
Config = config; | ||
} | ||
|
||
public async Task<ExecuteStatementResponse> ExecuteStatementAsync(ExecuteStatementRequest request, CancellationToken cancellationToken = default) | ||
{ | ||
var httpContent = new ExecuteStatementRequestHttpContent(request); | ||
using var response = await Api.SendAsync(Config, httpContent, cancellationToken).ConfigureAwait(false); | ||
var result = await DynamoDbLowLevelContext.ReadDocumentAsync(response, QueryParsingOptions.Instance, cancellationToken).ConfigureAwait(false); | ||
return ExecuteStatementResponseParser.Parse(result!); | ||
} | ||
|
||
public async Task<BatchExecuteStatementResponse> BatchExecuteStatementAsync(BatchExecuteStatementRequest request, CancellationToken cancellationToken = default) | ||
{ | ||
var httpContent = new BatchExecuteStatementRequestHttpContent(request); | ||
using var response = await Api.SendAsync(Config, httpContent, cancellationToken).ConfigureAwait(false); | ||
var result = await DynamoDbLowLevelContext.ReadDocumentAsync(response, TransactGetItemsParsingOptions.Instance, cancellationToken).ConfigureAwait(false); | ||
return BatchExecuteStatementResponseParser.Parse(result!); | ||
} | ||
|
||
public async Task<ExecuteTransactionResponse> ExecuteTransactionAsync(ExecuteTransactionRequest request, CancellationToken cancellationToken = default) | ||
{ | ||
var httpContent = new ExecuteTransactionRequestHttpContent(request); | ||
using var response = await Api.SendAsync(Config, httpContent, cancellationToken).ConfigureAwait(false); | ||
var result = await DynamoDbLowLevelContext.ReadDocumentAsync(response, TransactGetItemsParsingOptions.Instance, cancellationToken).ConfigureAwait(false); | ||
return ExecuteTransactionResponseParser.Parse(result!); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/EfficientDynamoDb/DynamoDbContext/IDynamoDbLowLevelPartiQLContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using EfficientDynamoDb.Operations.BatchExecuteStatement; | ||
using EfficientDynamoDb.Operations.ExecuteStatement; | ||
using EfficientDynamoDb.Operations.ExecuteTransaction; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace EfficientDynamoDb | ||
{ | ||
public interface IDynamoDbLowLevelPartiQLContext | ||
{ | ||
Task<ExecuteStatementResponse> ExecuteStatementAsync(ExecuteStatementRequest request, CancellationToken cancellationToken = default); | ||
|
||
Task<BatchExecuteStatementResponse> BatchExecuteStatementAsync(BatchExecuteStatementRequest request, CancellationToken cancellationToken = default); | ||
|
||
Task<ExecuteTransactionResponse> ExecuteTransactionAsync(ExecuteTransactionRequest request, CancellationToken cancellationToken = default); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...moDb/Internal/Operations/BatchExecuteStatement/BatchExecuteStatementRequestHttpContent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using EfficientDynamoDb.Converters; | ||
using EfficientDynamoDb.Internal.Extensions; | ||
using EfficientDynamoDb.Internal.Operations.Shared; | ||
using EfficientDynamoDb.Operations.BatchExecuteStatement; | ||
using EfficientDynamoDb.Operations.Shared; | ||
using System.Threading.Tasks; | ||
|
||
namespace EfficientDynamoDb.Internal.Operations.BatchExecuteStatement | ||
{ | ||
internal class BatchExecuteStatementRequestHttpContent : DynamoDbHttpContent | ||
{ | ||
private readonly BatchExecuteStatementRequest _request; | ||
|
||
public BatchExecuteStatementRequestHttpContent(BatchExecuteStatementRequest request) : base("DynamoDB_20120810.BatchExecuteStatement") | ||
{ | ||
_request = request; | ||
} | ||
|
||
protected override ValueTask WriteDataAsync(DdbWriter writer) | ||
{ | ||
var json = writer.JsonWriter; | ||
json.WriteStartObject(); | ||
|
||
json.WritePropertyName("Statements"); | ||
json.WriteStartArray(); | ||
foreach (var statementRequest in _request.Statements) | ||
{ | ||
json.WriteStartObject(); | ||
|
||
json.WriteString("Statement", statementRequest.Statement); | ||
|
||
if (statementRequest.Parameters.Count > 0) | ||
{ | ||
json.WritePropertyName("Parameters"); | ||
json.WriteStartArray(); | ||
foreach (var parameter in statementRequest.Parameters) | ||
{ | ||
parameter.Write(json); | ||
} | ||
json.WriteEndArray(); | ||
} | ||
|
||
if (statementRequest.ConsistentRead) | ||
json.WriteBoolean("ConsistentRead", true); | ||
|
||
if (statementRequest.ReturnValuesOnConditionCheckFailure != ReturnValuesOnConditionCheckFailure.None) | ||
json.WriteReturnValuesOnConditionCheckFailure(statementRequest.ReturnValuesOnConditionCheckFailure); | ||
|
||
json.WriteEndObject(); | ||
} | ||
json.WriteEndArray(); | ||
|
||
if (_request.ReturnConsumedCapacity != ReturnConsumedCapacity.None) | ||
json.WriteReturnConsumedCapacity(_request.ReturnConsumedCapacity); | ||
|
||
json.WriteEndObject(); | ||
|
||
return default; | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...DynamoDb/Internal/Operations/BatchExecuteStatement/BatchExecuteStatementResponseParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using EfficientDynamoDb.DocumentModel; | ||
using EfficientDynamoDb.Internal.Operations.Shared; | ||
using EfficientDynamoDb.Operations.BatchExecuteStatement; | ||
using System; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace EfficientDynamoDb.Internal.Operations.BatchExecuteStatement | ||
{ | ||
internal static class BatchExecuteStatementResponseParser | ||
{ | ||
public static BatchExecuteStatementResponse Parse(Document response) | ||
=> new BatchExecuteStatementResponse { Responses = ParseResponses(response), ConsumedCapacity = CapacityParser.ParseFullConsumedCapacities(response) }; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static BatchStatementResponse[] ParseResponses(Document response) | ||
{ | ||
if (!response.TryGetValue("Responses", out var responsesAttr)) | ||
return Array.Empty<BatchStatementResponse>(); | ||
|
||
var attributesList = responsesAttr.AsListAttribute().Items; | ||
var responses = new BatchStatementResponse[attributesList.Count]; | ||
for (var i = 0; i < attributesList.Count; i++) | ||
responses[i] = ParseBatchStatementResponse(attributesList[i].AsDocument()); | ||
|
||
return responses; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static BatchStatementResponse ParseBatchStatementResponse(Document document) | ||
{ | ||
var response = new BatchStatementResponse(); | ||
|
||
if (document.TryGetValue("TableName", out var tableNameAttr)) | ||
{ | ||
response.TableName = tableNameAttr.AsString(); | ||
} | ||
|
||
if (document.TryGetValue("Error", out var errorAttr)) | ||
{ | ||
response.Error = ParseBatchStatementError(errorAttr.AsDocument()); | ||
} | ||
|
||
if (document.TryGetValue("Item", out var itemAttr)) | ||
{ | ||
response.Item = itemAttr.AsDocument(); | ||
} | ||
|
||
return response; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static BatchStatementError? ParseBatchStatementError(Document document) | ||
{ | ||
var error = new BatchStatementError(); | ||
|
||
if (document.TryGetValue("Code", out var codeAttr)) | ||
{ | ||
error.Code = codeAttr.AsString(); | ||
} | ||
|
||
if (document.TryGetValue("Message", out var messageAttr)) | ||
{ | ||
error.Message = messageAttr.AsString(); | ||
} | ||
|
||
if (document.TryGetValue("Item", out var itemAttr)) | ||
{ | ||
error.Item = itemAttr.AsDocument(); | ||
} | ||
|
||
return error; | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...icientDynamoDb/Internal/Operations/ExecuteStatement/ExecuteStatementRequestHttpContent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using EfficientDynamoDb.Converters; | ||
using EfficientDynamoDb.Internal.Extensions; | ||
using EfficientDynamoDb.Internal.Operations.Shared; | ||
using EfficientDynamoDb.Operations.ExecuteStatement; | ||
using EfficientDynamoDb.Operations.Shared; | ||
using System.Threading.Tasks; | ||
|
||
namespace EfficientDynamoDb.Internal.Operations.ExecuteStatement | ||
{ | ||
internal class ExecuteStatementRequestHttpContent : DynamoDbHttpContent | ||
{ | ||
private readonly ExecuteStatementRequest _request; | ||
|
||
public ExecuteStatementRequestHttpContent(ExecuteStatementRequest executeStatementRequest) : base("DynamoDB_20120810.ExecuteStatement") | ||
{ | ||
_request = executeStatementRequest; | ||
} | ||
|
||
protected override ValueTask WriteDataAsync(DdbWriter writer) | ||
{ | ||
var json = writer.JsonWriter; | ||
json.WriteStartObject(); | ||
|
||
json.WritePropertyName("Statement"); | ||
json.WriteStringValue(_request.Statement); | ||
|
||
if (_request.Parameters.Count > 0) | ||
{ | ||
json.WritePropertyName("Parameters"); | ||
json.WriteStartArray(); | ||
foreach (var parameter in _request.Parameters) | ||
{ | ||
parameter.Write(json); | ||
} | ||
json.WriteEndArray(); | ||
} | ||
|
||
if (_request.ConsistentRead) | ||
json.WriteBoolean("ConsistentRead", true); | ||
|
||
if (_request.Limit.HasValue) | ||
json.WriteNumber("Limit", _request.Limit.Value); | ||
|
||
if (_request.NextToken != null) | ||
json.WriteString("NextToken", _request.NextToken); | ||
|
||
if (_request.ReturnConsumedCapacity != ReturnConsumedCapacity.None) | ||
json.WriteReturnConsumedCapacity(_request.ReturnConsumedCapacity); | ||
|
||
if (_request.ReturnItemCollectionMetrics != ReturnItemCollectionMetrics.None) | ||
json.WriteReturnItemCollectionMetrics(_request.ReturnItemCollectionMetrics); | ||
|
||
if (_request.ReturnValuesOnConditionCheckFailure != ReturnValuesOnConditionCheckFailure.None) | ||
json.WriteReturnValuesOnConditionCheckFailure(_request.ReturnValuesOnConditionCheckFailure); | ||
|
||
json.WriteEndObject(); | ||
return default; | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/EfficientDynamoDb/Internal/Operations/ExecuteStatement/ExecuteStatementResponseParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using EfficientDynamoDb.DocumentModel; | ||
using EfficientDynamoDb.Internal.Operations.Shared; | ||
using EfficientDynamoDb.Operations.ExecuteStatement; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace EfficientDynamoDb.Internal.Operations.ExecuteStatement | ||
{ | ||
internal static class ExecuteStatementResponseParser | ||
{ | ||
public static ExecuteStatementResponse Parse(Document response) => | ||
new ExecuteStatementResponse | ||
{ | ||
Items = response.TryGetValue("Items", out var items) ? items._documentListValue.Items : Array.Empty<Document>(), | ||
LastEvaluatedKey = ParseLastEvaluatedKey(response), | ||
NextToken = response.TryGetValue("NextToken", out var nextToken) ? nextToken.AsString() : string.Empty, | ||
ConsumedCapacity = CapacityParser.ParseFullConsumedCapacity(response), | ||
}; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal static IReadOnlyDictionary<string, AttributeValue>? ParseLastEvaluatedKey(Document response) | ||
{ | ||
if (!response.TryGetValue("LastEvaluatedKey", out var attribute)) | ||
return null; | ||
|
||
var document = attribute.AsDocument(); | ||
return document.Count > 0 ? document : null; | ||
} | ||
} | ||
} |
Oops, something went wrong.