2026-09-18 12:48:02 +00:00
|
|
|
using AIStudio.Tools.Databases.VectorStore;
|
|
|
|
|
|
2026-02-03 13:32:17 +00:00
|
|
|
namespace AIStudio.Tools.Services;
|
|
|
|
|
|
|
|
|
|
public sealed partial class RustService
|
|
|
|
|
{
|
2026-09-18 12:48:02 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// The issue code the Rust runtime sends when a vector store is there, but cannot be opened.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Mirrors ISSUE_CODE_STORE_UNREADABLE in runtime/src/qdrant_edge_database.rs. Reading the code
|
|
|
|
|
/// rather than the message is what keeps a reworded message on the Rust side harmless here.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
private const string ISSUE_CODE_STORE_UNREADABLE = "store-unreadable";
|
|
|
|
|
|
2026-06-02 15:22:59 +00:00
|
|
|
public async Task<TDatabaseInfo> GetDatabaseInfo<TDatabaseInfo>(
|
|
|
|
|
string databaseName,
|
|
|
|
|
string infoPath,
|
|
|
|
|
Func<string, TDatabaseInfo> unavailableFactory,
|
|
|
|
|
CancellationToken cancellationToken = default)
|
2026-02-03 13:32:17 +00:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-05-19 06:24:22 +00:00
|
|
|
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
|
|
|
|
cts.CancelAfter(TimeSpan.FromSeconds(45));
|
2026-06-02 15:22:59 +00:00
|
|
|
|
|
|
|
|
var databaseInfo = await this.http.GetFromJsonAsync<TDatabaseInfo>(infoPath, this.jsonRustSerializerOptions, cts.Token);
|
|
|
|
|
return databaseInfo ?? unavailableFactory("The database information response was empty.");
|
2026-05-19 06:24:22 +00:00
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
if(this.logger is not null)
|
2026-06-02 15:22:59 +00:00
|
|
|
this.logger.LogWarning("Fetching {DatabaseName} info from Rust service was cancelled by caller.", databaseName);
|
2026-05-19 06:24:22 +00:00
|
|
|
else
|
2026-06-02 15:22:59 +00:00
|
|
|
Console.WriteLine($"Fetching {databaseName} info from Rust service was cancelled by caller.");
|
|
|
|
|
|
|
|
|
|
return unavailableFactory("Operation cancelled by caller.");
|
2026-02-03 13:32:17 +00:00
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
if(this.logger is not null)
|
2026-06-02 15:22:59 +00:00
|
|
|
this.logger.LogError(e, "Error while fetching {DatabaseName} info from Rust service.", databaseName);
|
2026-02-03 13:32:17 +00:00
|
|
|
else
|
2026-06-02 15:22:59 +00:00
|
|
|
Console.WriteLine($"Error while fetching {databaseName} info from Rust service: '{e}'.");
|
|
|
|
|
|
|
|
|
|
return unavailableFactory(e.Message);
|
2026-02-03 13:32:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-06-02 15:22:59 +00:00
|
|
|
|
|
|
|
|
public async Task ExecuteDatabaseOperation<TRequest>(string databaseName, string path, TRequest request, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
|
|
|
|
cts.CancelAfter(TimeSpan.FromMinutes(5));
|
|
|
|
|
|
|
|
|
|
using var response = await this.http.PostAsJsonAsync(path, request, this.jsonRustSerializerOptions, cts.Token);
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var operation = await response.Content.ReadFromJsonAsync<DatabaseOperationResponse>(this.jsonRustSerializerOptions, cts.Token);
|
|
|
|
|
if (operation is not { Success: true })
|
2026-09-18 12:48:02 +00:00
|
|
|
throw CreateDatabaseException(operation?.Issue, operation?.IssueCode, $"The {databaseName} operation failed.");
|
2026-06-02 15:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
2026-09-09 16:43:37 +00:00
|
|
|
public async Task<TResult?> ExecuteDatabaseQuery<TRequest, TResult>(string databaseName, string path, TRequest request, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
|
|
|
|
cts.CancelAfter(TimeSpan.FromMinutes(5));
|
|
|
|
|
|
|
|
|
|
using var response = await this.http.PostAsJsonAsync(path, request, this.jsonRustSerializerOptions, cts.Token);
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var operation = await response.Content.ReadFromJsonAsync<DatabaseQueryResponse<TResult>>(this.jsonRustSerializerOptions, cts.Token);
|
|
|
|
|
if (operation is not { Success: true })
|
2026-09-18 12:48:02 +00:00
|
|
|
throw CreateDatabaseException(operation?.Issue, operation?.IssueCode, $"The {databaseName} query failed.");
|
2026-09-09 16:43:37 +00:00
|
|
|
|
|
|
|
|
return operation.Data;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-18 12:48:02 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Turns a failed database response into the exception which fits its issue code.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Almost every failure says all it has to say in its message. A store which cannot be opened is
|
|
|
|
|
/// the exception: the only way out of it is a rebuild which costs the user money and time, so it
|
|
|
|
|
/// gets a type of its own and reaches the places which can offer that rebuild instead of
|
|
|
|
|
/// starting it unasked.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
private static Exception CreateDatabaseException(string? issue, string? issueCode, string fallbackMessage)
|
|
|
|
|
{
|
|
|
|
|
var message = string.IsNullOrWhiteSpace(issue) ? fallbackMessage : issue;
|
|
|
|
|
return issueCode switch
|
|
|
|
|
{
|
|
|
|
|
ISSUE_CODE_STORE_UNREADABLE => new VectorStoreUnreadableException(message),
|
|
|
|
|
_ => new InvalidOperationException(message),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private sealed record DatabaseOperationResponse(bool Success, string Issue, string IssueCode);
|
2026-09-09 16:43:37 +00:00
|
|
|
|
2026-09-18 12:48:02 +00:00
|
|
|
private sealed record DatabaseQueryResponse<TResult>(bool Success, string Issue, string IssueCode, TResult? Data);
|
2026-06-02 15:22:59 +00:00
|
|
|
}
|