Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 使用HashSet保存已经存在CollectionName #36

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/DashVector.SemanticKernel/DashVectorMemroyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using DashVector.Models;
using Microsoft.SemanticKernel.Memory;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
Expand All @@ -25,6 +26,11 @@ public class DashVectorMemoryStore : IMemoryStore
[nameof(MemoryRecordMetadata.AdditionalMetadata)] = FieldType.STRING,
};

/// <summary>
/// List of collections that already exist
/// </summary>
static HashSet<string> CollectionNames = [];

static MemoryRecordMetadata ToMetaData(Dictionary<string, FieldValue> fields)
{
return new MemoryRecordMetadata(
Expand Down Expand Up @@ -72,25 +78,33 @@ await client.CreateCollectionAsync(new Models.Requests.CreateCollectionRequest()
ExtraParams = options.ExtraParams,
FieldsSchema = FieldsSchema,
}, cancellationToken).ConfigureAwait(false);
CollectionNames.Add(collectionName);
}

/// <inheritdoc/>
public async Task DeleteCollectionAsync(string collectionName, CancellationToken cancellationToken = default)
{
await client.DeleteCollectionAsync(collectionName, cancellationToken).ConfigureAwait(false);
CollectionNames.Remove(collectionName);
}

/// <inheritdoc/>
public async Task<bool> DoesCollectionExistAsync(string collectionName, CancellationToken cancellationToken = default)
{
if (CollectionNames.Contains(collectionName))
{
return true;
}
var isExist = false;
try
{
var result = await client.DescribeCollectionAsync(collectionName, cancellationToken).ConfigureAwait(false);

return result.OutPut?.Status == CollectionStatus.SERVING;
isExist = result.OutPut?.Status == CollectionStatus.SERVING;
if (isExist) CollectionNames.Add(collectionName);
}
catch { }
return false;
return isExist;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -139,6 +153,7 @@ public async IAsyncEnumerable<string> GetCollectionsAsync([EnumeratorCancellatio
{
foreach (var collection in collections.OutPut)
{
CollectionNames.Add(collection);
yield return collection;
}
}
Expand Down