using Microsoft.Extensions.Options; using SurrealDb.Net; using TarninaHub.Application.Abstractions; using TarninaHub.Application.Dtos; using TarninaHub.Infrastructure.Settings; namespace TarninaHub.Infrastructure.Persistence; public sealed class StorageService : IStorageService { private const string ProbeTable = "storage_probe"; private readonly ISurrealDbClient _client; private readonly SurrealDbSettings _settings; private readonly string _dataDirectory; public StorageService(ISurrealDbClient client, IOptions settings) { _client = client; _settings = settings.Value; _dataDirectory = Path.Combine(AppContext.BaseDirectory, _settings.DataDirectory); } public async Task InitializeAsync(CancellationToken cancellationToken) { await _client.RawQuery( $"DEFINE TABLE IF NOT EXISTS {ProbeTable} SCHEMALESS;", cancellationToken: cancellationToken); } public StorageInfo GetInfo() { var isRemote = !string.IsNullOrWhiteSpace(_settings.RemoteUrl); return new StorageInfo( isRemote ? "remote" : "rocksdb", isRemote ? _settings.RemoteUrl! : _dataDirectory, _settings.Namespace, _settings.Database); } public async Task GetProbeCountAsync(CancellationToken cancellationToken) { var probes = await _client.Select(ProbeTable, cancellationToken); return probes.Count(); } public Task AddProbeAsync(CancellationToken cancellationToken) { var probe = new StorageProbe { CreatedAt = DateTimeOffset.UtcNow, }; return _client.Create(ProbeTable, probe, cancellationToken); } }