Skip to content

Commit

Permalink
Merge pull request #85 from Encamina/@rliberoff/add_delete_chat_history
Browse files Browse the repository at this point in the history
Fixed `DeleteAsync<TEntityId>` method in `CosmosRepository<T>`
  • Loading branch information
rliberoff authored Mar 8, 2024
2 parents 1f16fe4 + 1ae2677 commit b0515bb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ Previous classification is not required if changes are simple or all belong to t
### Major Changes

- In interface type `IChatHistoryProvider` added new method `DeleteChatMessagesHistoryAsync` to delete a user's chat history messages. This method is implemented in `ChatHistoryProvider`.
- Added new interface `Encamina.Enmarcha.AI.Abstractions.ISemanticTextSplitter` and its implementations `Encamina.Enmarcha.AI.SemanticTextSplitter` to split a text into meaningful chunks based on embeddings.
- Added a new utility class for mathematical operations `Encamina.Enmarcha.Core.MathUtils`.
- Fixed `DeleteAsync<TEntityId>` method in `CosmosRepository<T>`. This method was always throwing exceptions because the partition key value was always `null`. It is fixed by considering the `Id` to delete the whole partition. If a specific item in the partition should be removed, then use the `DeleteAsync` on-generic method.
- Updated dependencies:
- Updated `Bogus` from `35.4.0` to `35.4.1`.
- Updated `Azure.Core` from `1.37.0` to `1.38.0`.
Expand All @@ -50,8 +53,6 @@ Previous classification is not required if changes are simple or all belong to t
- Updated `xunit.analyzers` from `1.10.0` to `1.11.0`.
- Updated `xunit.extensibility.core` from `2.6.6` to `2.7.0`.
- Updated `xunit.runner.visualstudio` from `2.5.6` to `2.5.7`.
- Added new interface `Encamina.Enmarcha.AI.Abstractions.ISemanticTextSplitter` and its implementations `Encamina.Enmarcha.AI.SemanticTextSplitter` to split a text into meaningful chunks based on embeddings.
- Added a new utility class for mathematical operations `Encamina.Enmarcha.Core.MathUtils`.

### Minor Changes

Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<PropertyGroup>
<VersionPrefix>8.1.5</VersionPrefix>
<VersionSuffix>preview-05</VersionSuffix>
<VersionSuffix>preview-06</VersionSuffix>
</PropertyGroup>

<!--
Expand Down
33 changes: 31 additions & 2 deletions src/Encamina.Enmarcha.Data.Cosmos/CosmosRepository{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net;

using Encamina.Enmarcha.Core.Extensions;
using Encamina.Enmarcha.Entities.Abstractions;

using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Linq;
Expand Down Expand Up @@ -317,7 +318,26 @@ public async Task<IQueryable<T>> GetAllAsync([NotNull] Func<IQueryable<T>, IQuer
public async Task AddBatchAsync(IEnumerable<T> entities, CancellationToken cancellationToken) => await AddOrUpdateBulkAsync(entities, cancellationToken);

/// <inheritdoc/>
public async Task DeleteAsync<TEntityId>(TEntityId id, CancellationToken cancellationToken) => await DeleteAsync(id is string idString ? idString : id.ToString(), null, cancellationToken);
/// <remarks>
/// From the point of view of Cosmos DB, the <paramref name="id"/> is the partition key value.
/// </remarks>
public async Task DeleteAsync<TEntityId>(TEntityId id, CancellationToken cancellationToken)
{
var partitionKey = GeneratePartition(id is string idString ? idString : id.ToString());

var resultSet = container.GetItemQueryIterator<CosmosDbItem>(new QueryDefinition(@"SELECT c.id FROM c"), requestOptions: new QueryRequestOptions()
{
PartitionKey = partitionKey,
});

while (resultSet.HasMoreResults)
{
foreach (var item in await resultSet.ReadNextAsync(cancellationToken))
{
await container.DeleteItemAsync<CosmosDbItem>(item.Id, partitionKey, cancellationToken: cancellationToken);
}
}
}

private static async Task<(IQueryable<TEntity> Results, string ContinuationToken)> BuildResult<TEntity>(FeedIterator<TEntity> feedIterator, CancellationToken cancellationToken)
{
Expand All @@ -342,6 +362,15 @@ public async Task<IQueryable<T>> GetAllAsync([NotNull] Func<IQueryable<T>, IQuer

private static PartitionKey GeneratePartition(string partitionKey)
{
return string.IsNullOrWhiteSpace(partitionKey) ? default : new PartitionKey(partitionKey);
return string.IsNullOrWhiteSpace(partitionKey)
? throw new ArgumentNullException(nameof(partitionKey))
: new PartitionKey(partitionKey);
}

/// <summary>
/// Auxiliary type to retrieve items from Cosmos DB with just the unique identifier.
/// </summary>
private sealed class CosmosDbItem : IdentifiableBase<string>
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public ChatHistoryProvider(Func<string, int> tokensLengthFunction, IAsyncReposit
}

/// <inheritdoc/>
public async Task DeleteChatMessagesHistoryAsync(string userId, CancellationToken cancellationToken)
public Task DeleteChatMessagesHistoryAsync(string userId, CancellationToken cancellationToken)
{
await chatMessagesHistoryRepository.DeleteAsync(userId, cancellationToken);
return chatMessagesHistoryRepository.DeleteAsync(userId, cancellationToken);
}

/// <inheritdoc/>
Expand Down

0 comments on commit b0515bb

Please sign in to comment.