-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support merge strategy when updating entities
In table table (not entity or document, which cannot do merge since the entity is stored as a whole), we can perform data merging by submitting only non-null properties for saving, so that when using Merge, we can fetch all columns and avoid overwriting existing values if no new values are provided. According to the docs on the REST API operations on entities (see https://docs.microsoft.com/en-us/rest/api/storageservices/Merge-Entity?redirectedfrom=MSDN#remarks), null values are already skipped when doing this, so we just need to replicate that ourselves too by skipping those properties too when persisting. NOTE: when using Merge, every PutAsync operation will incur an additional REST request for the retrieval of the merged data. Fixes #46
- Loading branch information
Showing
8 changed files
with
162 additions
and
16 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
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
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
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
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,35 @@ | ||
//<auto-generated/> | ||
#nullable enable | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Threading; | ||
|
||
namespace Devlooped | ||
{ | ||
/// <summary> | ||
/// Various usability overloads. | ||
/// </summary> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
static partial class TableStorageExtensions | ||
{ | ||
/// <summary> | ||
/// Queries the repository for items that match the given <paramref name="predicate"/>. | ||
/// </summary> | ||
/// <remarks> | ||
/// Shortcut for <c>CreateQuery().Where(predicate)</c> and returning as <see cref="IAsyncEnumerable{T}"/> | ||
/// for use with <c>await foreach</c> directly. | ||
/// </remarks> | ||
/// <example> | ||
/// var books = TableRepository.Create<Book>(); | ||
/// await foreach (var book in books.QueryAsync(x => x.IsPublished)) | ||
/// { | ||
/// Console.WriteLine(book.ISBN); | ||
/// } | ||
/// </example> | ||
public static IAsyncEnumerable<T> EnumerateAsync<T>(this ITableRepository<T> repository, Expression<Func<T, bool>> predicate, CancellationToken cancellation = default) where T : class | ||
=> (IAsyncEnumerable<T>)repository.CreateQuery().Where(predicate); | ||
} | ||
} |
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,40 @@ | ||
//<auto-generated/> | ||
#nullable enable | ||
using Microsoft.Azure.Cosmos.Table; | ||
|
||
namespace Devlooped | ||
{ | ||
/// <summary> | ||
/// Provides the strategy used when saving entities (using `PutAsync`) that | ||
/// already exist in the table. Either <see cref="Merge"/> or <see cref="Replace"/>. | ||
/// When not provided, <see cref="Replace"/> is used by default. | ||
/// </summary> | ||
abstract partial class UpdateStrategy | ||
{ | ||
/// <summary> | ||
/// When storing an entity that already exists in the table, merge with the | ||
/// existing data, using <see cref="TableOperation.InsertOrMerge(ITableEntity)"/>. | ||
/// </summary> | ||
public static UpdateStrategy Merge { get; } = new MergeStrategy(); | ||
|
||
/// <summary> | ||
/// When storing an entity that already exists in the table, replace the | ||
/// existing data, using <see cref="TableOperation.InsertOrReplace(ITableEntity)"/>. | ||
/// </summary> | ||
public static UpdateStrategy Replace { get; } = new ReplaceStrategy(); | ||
|
||
protected internal abstract TableOperation CreateOperation(ITableEntity entity); | ||
|
||
class MergeStrategy : UpdateStrategy | ||
{ | ||
protected internal override TableOperation CreateOperation(ITableEntity entity) | ||
=> TableOperation.InsertOrMerge(entity); | ||
} | ||
|
||
class ReplaceStrategy : UpdateStrategy | ||
{ | ||
protected internal override TableOperation CreateOperation(ITableEntity entity) | ||
=> TableOperation.InsertOrReplace(entity); | ||
} | ||
} | ||
} |
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
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