| 1 | using Microsoft.Data.Sqlite; |
| 2 | |
| 3 | namespace HybridCodebaseIndex.Core; |
| 4 | |
| 5 | internal static partial class SqliteFtsIndex |
| 6 | { |
| 7 | internal static Task<IndexStatus> GetStatusAsync(string workspaceRoot, string dbPath, CancellationToken cancellationToken) |
| 8 | => Task.Run(() => GetStatus(workspaceRoot, dbPath), cancellationToken); |
| 9 | |
| 10 | private static IndexStatus GetStatus(string workspaceRoot, string dbPath) |
| 11 | { |
| 12 | workspaceRoot = Path.GetFullPath(workspaceRoot.TrimEnd(Path.DirectorySeparatorChar)); |
| 13 | var exists = File.Exists(dbPath); |
| 14 | if (!exists) |
| 15 | { |
| 16 | IndexSettings.TryLoadFromIndexDirectoryWithDiagnostics( |
| 17 | Path.GetDirectoryName(dbPath), |
| 18 | out _, |
| 19 | out var src, |
| 20 | out var err); |
| 21 | return new IndexStatus(FormatVersion, dbPath, false, 0, false, null, workspaceRoot, null, null, src, err, null, null, null); |
| 22 | } |
| 23 | |
| 24 | using var conn = new SqliteConnection($"Data Source={dbPath};Mode=ReadOnly"); |
| 25 | conn.Open(); |
| 26 | |
| 27 | var indexedAt = ReadMeta(conn, "indexed_at"); |
| 28 | var reindexState = ReadMeta(conn, "reindex_state"); |
| 29 | if (string.IsNullOrWhiteSpace(reindexState)) |
| 30 | reindexState = null; |
| 31 | var reindexStartedAt = ReadMeta(conn, "reindex_started_at"); |
| 32 | if (string.IsNullOrWhiteSpace(reindexStartedAt)) |
| 33 | reindexStartedAt = null; |
| 34 | var lastErr = ReadMeta(conn, "reindex_error"); |
| 35 | if (string.IsNullOrWhiteSpace(lastErr)) |
| 36 | lastErr = null; |
| 37 | var lastErrAt = ReadMeta(conn, "reindex_error_at"); |
| 38 | if (string.IsNullOrWhiteSpace(lastErrAt)) |
| 39 | lastErrAt = null; |
| 40 | |
| 41 | IndexSettings.TryLoadFromIndexDirectoryWithDiagnostics( |
| 42 | Path.GetDirectoryName(dbPath), |
| 43 | out var settings, |
| 44 | out var settingsSource, |
| 45 | out var settingsParseError); |
| 46 | |
| 47 | using var countCmd = conn.CreateCommand(); |
| 48 | countCmd.CommandText = "SELECT count(*) FROM chunks;"; |
| 49 | var docCount = Convert.ToInt32(countCmd.ExecuteScalar() ?? 0); |
| 50 | var mayBeStale = string.Equals(reindexState, "running", StringComparison.OrdinalIgnoreCase); |
| 51 | |
| 52 | var eff = new EffectiveSettings( |
| 53 | settings.IncludeCsInFts, |
| 54 | settings.ExtraIncludeRoots, |
| 55 | settings.ExcludeRoots, |
| 56 | settings.GetEffectiveExtensions(), |
| 57 | settings.ExcludePathSegments, |
| 58 | settings.IgnoreFiles, |
| 59 | settings.GetEffectiveMaxIndexedFileBytes(), |
| 60 | settings.GetEffectiveChunkLines(), |
| 61 | settings.GetEffectiveChunkOverlapLines(), |
| 62 | settings.GetEffectiveBinaryProbeBytes()); |
| 63 | return new IndexStatus( |
| 64 | FormatVersion, |
| 65 | dbPath, |
| 66 | true, |
| 67 | docCount, |
| 68 | mayBeStale, |
| 69 | indexedAt, |
| 70 | workspaceRoot, |
| 71 | lastErr, |
| 72 | lastErrAt, |
| 73 | settingsSource, |
| 74 | settingsParseError, |
| 75 | eff, |
| 76 | reindexState, |
| 77 | reindexStartedAt); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | |