namespace AIStudio.Tools.Services.Indexing; /// /// Counts the file system changes which the watchers report, so the log gets a summary instead of one line per change. /// /// /// A build, a checkout, or a sync tool changes thousands of files within seconds. With one log line each, such a /// burst buried the log, and the log events piled up on their way to the runtime until it closed the connection /// of the .NET server. Therefore, the first change of a data source opens a window and is logged as it comes; /// every further change is only counted until the window closes, and its summary takes one line. /// internal sealed class WatcherChangeTally { private readonly Lock tallyLock = new(); private readonly Dictionary openWindows = new(StringComparer.OrdinalIgnoreCase); /// /// Counts a change of a data source. /// /// The id of the data source. /// The path of the change. /// Whether the change concerns the RAG index. /// True when the change opened a window: the caller logs it as it comes and closes the window later. public bool Count(string dataSourceId, string path, bool isRelevant) { lock (this.tallyLock) { if (this.openWindows.TryGetValue(dataSourceId, out var counts)) { this.openWindows[dataSourceId] = counts.Add(path, isRelevant); return false; } this.openWindows[dataSourceId] = WatcherChangeCounts.NONE; return true; } } /// /// Closes the window of a data source, so its next change opens a new one. /// /// The id of the data source. /// The changes counted after the one which opened the window. public WatcherChangeCounts Close(string dataSourceId) { lock (this.tallyLock) return this.openWindows.Remove(dataSourceId, out var counts) ? counts : WatcherChangeCounts.NONE; } }