From fc971b6cf3dd7a5488739bbcf4146935b884afa6 Mon Sep 17 00:00:00 2001 From: Ahmed Yasin Koculu Date: Sun, 1 Sep 2024 15:24:41 +0200 Subject: [PATCH] Add compaction methods to the RecordTable. --- .../SearchEngineApp.cs | 2 + .../Directory.Build.props | 4 +- .../Index/IndexOfTokenRecordPreviousToken.cs | 2 +- .../Storage/RecordTable.cs | 41 ++++++++++++++++++- 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/ZoneTree.FullTextSearch.Playground/SearchEngineApp.cs b/src/ZoneTree.FullTextSearch.Playground/SearchEngineApp.cs index 55ea531..3dac1f1 100644 --- a/src/ZoneTree.FullTextSearch.Playground/SearchEngineApp.cs +++ b/src/ZoneTree.FullTextSearch.Playground/SearchEngineApp.cs @@ -242,8 +242,10 @@ record = Interlocked.Increment(ref nextRecord); sw.Restart(); SearchEngine.Index.EvictToDisk(); + RecordTable.EvictToDisk(); Console.WriteLine("Waiting for background threads..."); SearchEngine.Index.WaitForBackgroundThreads(); + RecordTable.WaitForBackgroundThreads(); Console.WriteLine("Merging completed in: " + sw.ElapsedMilliseconds + " ms"); if (cancellationTokenSource.IsCancellationRequested && isInteractive) { diff --git a/src/ZoneTree.FullTextSearch/Directory.Build.props b/src/ZoneTree.FullTextSearch/Directory.Build.props index 7d9926a..fdc54ac 100644 --- a/src/ZoneTree.FullTextSearch/Directory.Build.props +++ b/src/ZoneTree.FullTextSearch/Directory.Build.props @@ -5,8 +5,8 @@ Ahmed Yasin Koculu ZoneTree.FullTextSearch ZoneTree.FullTextSearch - 1.0.2.0 - 1.0.2.0 + 1.0.3.0 + 1.0.3.0 Ahmed Yasin Koculu ZoneTree.FullTextSearch ZoneTree.FullTextSearch is an open-source library that extends ZoneTree to provide efficient full-text search capabilities. It offers a fast, embedded search engine suitable for applications that require high performance and do not rely on external databases. diff --git a/src/ZoneTree.FullTextSearch/Index/IndexOfTokenRecordPreviousToken.cs b/src/ZoneTree.FullTextSearch/Index/IndexOfTokenRecordPreviousToken.cs index 3fefaa1..c19505e 100644 --- a/src/ZoneTree.FullTextSearch/Index/IndexOfTokenRecordPreviousToken.cs +++ b/src/ZoneTree.FullTextSearch/Index/IndexOfTokenRecordPreviousToken.cs @@ -169,7 +169,7 @@ public void ThrowIfIndexIsDropped() public bool IsIndexDropped { get => isDropped; } /// - /// Evicts inactive data from memory to disk in both primary and secondary zone trees. + /// Evicts data from memory to disk in both primary and secondary zone trees. /// public void EvictToDisk() { diff --git a/src/ZoneTree.FullTextSearch/Storage/RecordTable.cs b/src/ZoneTree.FullTextSearch/Storage/RecordTable.cs index 4543305..be5b739 100644 --- a/src/ZoneTree.FullTextSearch/Storage/RecordTable.cs +++ b/src/ZoneTree.FullTextSearch/Storage/RecordTable.cs @@ -34,7 +34,7 @@ public sealed class RecordTable : IDisposable where TRecord : u /// public bool IsDropped { get => isDropped; } - bool isDropped = false; + bool isDropped; /// /// Initializes a new instance of the RecordTable class, setting up the two ZoneTrees and their maintainers. @@ -105,6 +105,15 @@ public bool TryGetValue(TRecord record, out TValue value) return ZoneTree1.TryGet(record, out value); } + /// + /// Throws an exception if the index has been dropped, preventing further operations on a dropped index. + /// + void ThrowIfIndexIsDropped() + { + if (isDropped) + throw new Exception($"{nameof(RecordTable)} is dropped."); + } + /// /// Tries to retrieve a record associated with a given value. /// @@ -116,6 +125,36 @@ public bool TryGetRecord(TValue value, out TRecord record) return ZoneTree2.TryGet(value, out record); } + /// + /// Evicts data from memory to disk in both primary and secondary zone trees. + /// + public void EvictToDisk() + { + ThrowIfIndexIsDropped(); + Maintainer1.EvictToDisk(); + Maintainer2.EvictToDisk(); + } + + /// + /// Attempts to cancel any background threads associated with maintenance tasks for both zone trees. + /// + public void TryCancelBackgroundThreads() + { + ThrowIfIndexIsDropped(); + Maintainer1.TryCancelBackgroundThreads(); + Maintainer2.TryCancelBackgroundThreads(); + } + + /// + /// Waits for all background threads associated with maintenance tasks to complete for both zone trees. + /// + public void WaitForBackgroundThreads() + { + ThrowIfIndexIsDropped(); + Maintainer1.WaitForBackgroundThreads(); + Maintainer2.WaitForBackgroundThreads(); + } + /// /// Drops the record table. ///