Skip to content

Commit

Permalink
.Net: Change VolatileMemoryStore.CreateCollectionAsync to not throw…
Browse files Browse the repository at this point in the history
… when collection already exists (microsoft#2300)

### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->
today the `VolatileMemoryStore` will throw when creating a collection if
it already exists. other stores (e.g. Postgres, DuckDB) follow a
different pattern and return gracefully. throwing an error can create a
problem for parallel writes to the memory store, which is being added in
ChatCopilot [here](microsoft/chat-copilot#83).

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist


<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄
  • Loading branch information
dehoward authored and SOE-YoungS committed Oct 31, 2023
1 parent e112732 commit dbad0c7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,13 @@ public async Task ItCanCreateAndGetCollectionAsync()
}

[Fact]
public async Task ItCannotCreateDuplicateCollectionAsync()
public async Task ItHandlesExceptionsWhenCreatingCollectionAsync()
{
// Arrange
string collection = "test_collection" + this._collectionNum;
this._collectionNum++;

// Act
await this._db.CreateCollectionAsync(collection);
string? collection = null;

// Assert
await Assert.ThrowsAsync<MemoryException>(async () => await this._db.CreateCollectionAsync(collection));
await Assert.ThrowsAsync<ArgumentNullException>(async () => await this._db.CreateCollectionAsync(collection!));
}

[Fact]
Expand Down
6 changes: 2 additions & 4 deletions dotnet/src/SemanticKernel/Memory/VolatileMemoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ public class VolatileMemoryStore : IMemoryStore
/// <inheritdoc/>
public Task CreateCollectionAsync(string collectionName, CancellationToken cancellationToken = default)
{
if (!this._store.TryAdd(collectionName, new ConcurrentDictionary<string, MemoryRecord>()))
{
return Task.FromException(new MemoryException(MemoryException.ErrorCodes.FailedToCreateCollection, $"Could not create collection {collectionName}"));
}
Verify.NotNullOrWhiteSpace(collectionName);

this._store.TryAdd(collectionName, new ConcurrentDictionary<string, MemoryRecord>());
return Task.CompletedTask;
}

Expand Down

0 comments on commit dbad0c7

Please sign in to comment.