|
| 1 | +//<auto-generated/> |
| 2 | +#nullable enable |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.ComponentModel; |
| 6 | +using System.Linq; |
| 7 | +using System.Linq.Expressions; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Azure.Data.Tables; |
| 11 | +using Mono.Linq.Expressions; |
| 12 | + |
| 13 | +namespace Devlooped; |
| 14 | + |
| 15 | +/// <inheritdoc /> |
| 16 | +public partial class MemoryPartition<T> : ITablePartition<T>, IDocumentPartition<T> where T : class |
| 17 | +{ |
| 18 | + readonly MemoryRepository<T> repository; |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Initializes the repository with the given storage account and optional table name. |
| 22 | + /// </summary> |
| 23 | + /// <param name="storageAccount">The <see cref="CloudStorageAccount"/> to use to connect to the table.</param> |
| 24 | + public MemoryPartition() |
| 25 | + : this(TablePartition.GetDefaultTableName<T>(), |
| 26 | + TablePartition.GetDefaultPartitionKey<T>(), |
| 27 | + RowKeyAttribute.CreateAccessor<T>()) |
| 28 | + { } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Initializes the repository with the given storage account and optional table name. |
| 32 | + /// </summary> |
| 33 | + /// <param name="tableName">The table that backs this table partition.</param> |
| 34 | + public MemoryPartition(string tableName) |
| 35 | + : this(tableName ?? TablePartition.GetDefaultTableName<T>(), |
| 36 | + TablePartition.GetDefaultPartitionKey<T>(), |
| 37 | + RowKeyAttribute.CreateAccessor<T>()) |
| 38 | + { } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Initializes the repository with the given storage account and optional table name. |
| 42 | + /// </summary> |
| 43 | + /// <param name="tableName">The table that backs this table partition.</param> |
| 44 | + /// <param name="partitionKey">The fixed partition key that backs this table partition.</param> |
| 45 | + public MemoryPartition(string tableName, string partitionKey) |
| 46 | + : this(tableName ?? TablePartition.GetDefaultTableName<T>(), |
| 47 | + partitionKey, |
| 48 | + RowKeyAttribute.CreateAccessor<T>()) |
| 49 | + { } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Initializes the repository with the given storage account and optional table name. |
| 53 | + /// </summary> |
| 54 | + /// <param name="tableName">The table that backs this table partition.</param> |
| 55 | + /// <param name="partitionKey">The fixed partition key that backs this table partition.</param> |
| 56 | + /// <param name="rowKey">A function to determine the row key for an entity of type <typeparamref name="T"/> within the partition.</param> |
| 57 | + public MemoryPartition(string tableName, string partitionKey, Expression<Func<T, string>> rowKey) |
| 58 | + { |
| 59 | + partitionKey ??= TablePartition.GetDefaultPartitionKey<T>(); |
| 60 | + PartitionKey = partitionKey; |
| 61 | + |
| 62 | + repository = new MemoryRepository<T>(tableName, _ => partitionKey, |
| 63 | + rowKey ?? RowKeyAttribute.CreateAccessor<T>()); |
| 64 | + } |
| 65 | + |
| 66 | + /// <inheritdoc /> |
| 67 | + public string TableName => repository.TableName; |
| 68 | + |
| 69 | + /// <inheritdoc /> |
| 70 | + public string PartitionKey { get; } |
| 71 | + |
| 72 | + /// <inheritdoc /> |
| 73 | + public IQueryable<T> CreateQuery() => repository.CreateQuery(PartitionKey); |
| 74 | + |
| 75 | + /// <inheritdoc /> |
| 76 | + public Task<bool> DeleteAsync(T entity, CancellationToken cancellation = default) |
| 77 | + { |
| 78 | + if (entity is TableEntity te && !PartitionKey.Equals(te.PartitionKey, StringComparison.Ordinal)) |
| 79 | + throw new ArgumentException("Entity does not belong to the partition."); |
| 80 | + |
| 81 | + return repository.DeleteAsync(entity, cancellation); |
| 82 | + } |
| 83 | + |
| 84 | + /// <inheritdoc /> |
| 85 | + public Task<bool> DeleteAsync(string rowKey, CancellationToken cancellation = default) |
| 86 | + => repository.DeleteAsync(PartitionKey, rowKey, cancellation); |
| 87 | + |
| 88 | + /// <inheritdoc /> |
| 89 | + public IAsyncEnumerable<T> EnumerateAsync(CancellationToken cancellation = default) |
| 90 | + => repository.EnumerateAsync(PartitionKey, cancellation); |
| 91 | + |
| 92 | + /// <inheritdoc /> |
| 93 | + public IAsyncEnumerable<T> EnumerateAsync(Expression<Func<IDocumentEntity, bool>> predicate, CancellationToken cancellation = default) |
| 94 | + => repository.EnumerateAsync(predicate.AndAlso(x => x.PartitionKey == PartitionKey), cancellation); |
| 95 | + |
| 96 | + /// <inheritdoc /> |
| 97 | + public Task<T?> GetAsync(string rowKey, CancellationToken cancellation = default) |
| 98 | + => repository.GetAsync(PartitionKey, rowKey, cancellation); |
| 99 | + |
| 100 | + /// <inheritdoc /> |
| 101 | + public Task<T> PutAsync(T entity, CancellationToken cancellation = default) |
| 102 | + { |
| 103 | + if (entity is TableEntity te && !PartitionKey.Equals(te.PartitionKey, StringComparison.Ordinal)) |
| 104 | + throw new ArgumentException("Entity does not belong to the partition."); |
| 105 | + |
| 106 | + return repository.PutAsync(entity, cancellation); |
| 107 | + } |
| 108 | +} |
0 commit comments