| | | 1 | | using Amazon.DynamoDBv2.DataModel; |
| | | 2 | | using Amazon.DynamoDBv2.DocumentModel; |
| | | 3 | | using Amazon.DynamoDBv2.Model; |
| | | 4 | | using ArturRios.Data.DynamoDb.Interfaces; |
| | | 5 | | using ArturRios.Output; |
| | | 6 | | |
| | | 7 | | namespace ArturRios.Data.DynamoDb.Repositories; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// DynamoDB implementation of <see cref="IAsyncDynamoRepository{T}" /> over the AWS object-persistence |
| | | 11 | | /// model (<see cref="IDynamoDBContext" />). Failures are returned as <see cref="DataOutput{T}" /> / |
| | | 12 | | /// <see cref="ProcessOutput" />; a <see cref="ConditionalCheckFailedException" /> (from |
| | | 13 | | /// <c>[DynamoDBVersion]</c> optimistic locking) becomes a concurrency error. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <typeparam name="T">The item type.</typeparam> |
| | | 16 | | /// <param name="context">The DynamoDB object-persistence context.</param> |
| | 9 | 17 | | public class DynamoRepository<T>(IDynamoDBContext context) : IAsyncDynamoRepository<T> where T : class |
| | | 18 | | { |
| | | 19 | | /// <summary>Message prefix returned when an operation fails.</summary> |
| | | 20 | | protected const string OperationFailedMessage = "A data-access error occurred:"; |
| | | 21 | | |
| | | 22 | | /// <summary>Message returned on an optimistic-concurrency conflict.</summary> |
| | | 23 | | protected const string ConcurrencyMessage = "Concurrency conflict: the item was modified by another process."; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// The DynamoDB batch-write API rejects types with a <c>[DynamoDBVersion]</c> property unless |
| | | 27 | | /// version checking is explicitly skipped (batch writes have no per-item conditional-check |
| | | 28 | | /// support). Optimistic concurrency remains enforced on the single-item <see cref="SaveAsync" />/ |
| | | 29 | | /// <see cref="DeleteAsync" /> paths. |
| | | 30 | | /// </summary> |
| | 1 | 31 | | private static readonly BatchWriteConfig BatchSkipVersionCheckConfig = new() { SkipVersionCheck = true }; |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | | 34 | | public Task<DataOutput<T>> SaveAsync(T item, CancellationToken ct = default) => |
| | 13 | 35 | | GuardedAsync(async () => |
| | 13 | 36 | | { |
| | 13 | 37 | | await context.SaveAsync(item, ct); |
| | 13 | 38 | | |
| | 11 | 39 | | return item; |
| | 24 | 40 | | }); |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | | 43 | | public Task<DataOutput<T?>> LoadAsync(object hashKey, CancellationToken ct = default) => |
| | 2 | 44 | | GuardedAsync<T?>(async () => await context.LoadAsync<T>(hashKey, ct)); |
| | | 45 | | |
| | | 46 | | /// <inheritdoc /> |
| | | 47 | | public Task<DataOutput<T?>> LoadAsync(object hashKey, object rangeKey, CancellationToken ct = default) => |
| | 6 | 48 | | GuardedAsync<T?>(async () => await context.LoadAsync<T>(hashKey, rangeKey, ct)); |
| | | 49 | | |
| | | 50 | | /// <inheritdoc /> |
| | | 51 | | public Task<ProcessOutput> DeleteAsync(T item, CancellationToken ct = default) => |
| | 4 | 52 | | GuardedProcessAsync(async () => await context.DeleteAsync(item, ct)); |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | | 55 | | public Task<DataOutput<IEnumerable<T>>> QueryAsync(object hashKey, CancellationToken ct = default) => |
| | 2 | 56 | | GuardedAsync<IEnumerable<T>>(async () => await context.QueryAsync<T>(hashKey).GetRemainingAsync(ct)); |
| | | 57 | | |
| | | 58 | | /// <inheritdoc /> |
| | | 59 | | public Task<DataOutput<IEnumerable<T>>> QueryAsync(object hashKey, QueryOperator op, |
| | | 60 | | IEnumerable<object> sortKeyValues, CancellationToken ct = default) => |
| | 1 | 61 | | GuardedAsync<IEnumerable<T>>(async () => |
| | 2 | 62 | | await context.QueryAsync<T>(hashKey, op, sortKeyValues).GetRemainingAsync(ct)); |
| | | 63 | | |
| | | 64 | | /// <inheritdoc /> |
| | | 65 | | public Task<DataOutput<IEnumerable<T>>> ScanAsync(IEnumerable<ScanCondition> conditions, |
| | | 66 | | CancellationToken ct = default) => |
| | 2 | 67 | | GuardedAsync<IEnumerable<T>>(async () => await context.ScanAsync<T>(conditions).GetRemainingAsync(ct)); |
| | | 68 | | |
| | | 69 | | /// <inheritdoc /> |
| | | 70 | | public Task<DataOutput<IEnumerable<T>>> SaveManyAsync(IEnumerable<T> items, CancellationToken ct = default) => |
| | 1 | 71 | | GuardedAsync<IEnumerable<T>>(async () => |
| | 1 | 72 | | { |
| | 1 | 73 | | var list = items.ToList(); |
| | 1 | 74 | | var batch = context.CreateBatchWrite<T>(BatchSkipVersionCheckConfig); |
| | 1 | 75 | | batch.AddPutItems(list); |
| | 1 | 76 | | await batch.ExecuteAsync(ct); |
| | 1 | 77 | | return list; |
| | 2 | 78 | | }); |
| | | 79 | | |
| | | 80 | | /// <inheritdoc /> |
| | | 81 | | public Task<ProcessOutput> DeleteManyAsync(IEnumerable<T> items, CancellationToken ct = default) => |
| | 1 | 82 | | GuardedProcessAsync(async () => |
| | 1 | 83 | | { |
| | 1 | 84 | | var batch = context.CreateBatchWrite<T>(BatchSkipVersionCheckConfig); |
| | 1 | 85 | | batch.AddDeleteItems(items.ToList()); |
| | 1 | 86 | | await batch.ExecuteAsync(ct); |
| | 2 | 87 | | }); |
| | | 88 | | |
| | | 89 | | /// <inheritdoc /> |
| | | 90 | | public Task<DataOutput<IEnumerable<T>>> |
| | | 91 | | LoadManyAsync(IEnumerable<object> hashKeys, CancellationToken ct = default) => |
| | 2 | 92 | | GuardedAsync<IEnumerable<T>>(async () => |
| | 2 | 93 | | { |
| | 2 | 94 | | var batch = context.CreateBatchGet<T>(); |
| | 14 | 95 | | foreach (var key in hashKeys) |
| | 4 | 96 | | { |
| | 4 | 97 | | batch.AddKey(key); |
| | 4 | 98 | | } |
| | 2 | 99 | | |
| | 2 | 100 | | await batch.ExecuteAsync(ct); |
| | 2 | 101 | | |
| | 2 | 102 | | return batch.Results; |
| | 4 | 103 | | }); |
| | | 104 | | |
| | | 105 | | /// <summary>Runs an operation returning data, converting failures to envelope errors.</summary> |
| | | 106 | | protected static async Task<DataOutput<TResult>> GuardedAsync<TResult>(Func<Task<TResult>> operation) |
| | 23 | 107 | | { |
| | | 108 | | try |
| | 23 | 109 | | { |
| | 23 | 110 | | return DataOutput<TResult>.New.WithData(await operation()); |
| | | 111 | | } |
| | 0 | 112 | | catch (OperationCanceledException) |
| | 0 | 113 | | { |
| | 0 | 114 | | throw; |
| | | 115 | | } |
| | 2 | 116 | | catch (Exception ex) |
| | 2 | 117 | | { |
| | 2 | 118 | | return Fail<TResult>(ex); |
| | | 119 | | } |
| | 23 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary>Runs an operation with no payload, converting failures to envelope errors.</summary> |
| | | 123 | | protected static async Task<ProcessOutput> GuardedProcessAsync(Func<Task> operation) |
| | 3 | 124 | | { |
| | | 125 | | try |
| | 3 | 126 | | { |
| | 3 | 127 | | await operation(); |
| | 3 | 128 | | return ProcessOutput.New; |
| | | 129 | | } |
| | 0 | 130 | | catch (OperationCanceledException) |
| | 0 | 131 | | { |
| | 0 | 132 | | throw; |
| | | 133 | | } |
| | 0 | 134 | | catch (Exception ex) |
| | 0 | 135 | | { |
| | 0 | 136 | | return ex is ConditionalCheckFailedException |
| | 0 | 137 | | ? ProcessOutput.New.WithError(ConcurrencyMessage) |
| | 0 | 138 | | : ProcessOutput.New.WithError($"{OperationFailedMessage} {ex.GetBaseException().Message}"); |
| | | 139 | | } |
| | 3 | 140 | | } |
| | | 141 | | |
| | | 142 | | /// <summary>Maps an exception to a data-output error envelope.</summary> |
| | 2 | 143 | | protected static DataOutput<TResult> Fail<TResult>(Exception ex) => ex switch |
| | 2 | 144 | | { |
| | 1 | 145 | | ConditionalCheckFailedException => DataOutput<TResult>.New.WithError(ConcurrencyMessage), |
| | 1 | 146 | | _ => DataOutput<TResult>.New.WithError($"{OperationFailedMessage} {ex.GetBaseException().Message}") |
| | 2 | 147 | | }; |
| | | 148 | | } |