Skip to content

Commit

Permalink
Set NextOpIndex after loading BTree to remove skipped regions on op i…
Browse files Browse the repository at this point in the history
…ndex.
  • Loading branch information
koculu committed Sep 7, 2024
1 parent bbf27b3 commit 6a2b178
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
68 changes: 63 additions & 5 deletions src/ZoneTree.UnitTests/OpIndexTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,77 @@ void CreateData()
maintainer.WaitForBackgroundThreads();
}

void ReloadData()
void ReloadData(int expectedIndex, bool drop = false)
{
using var zoneTree = new ZoneTreeFactory<int, int>()
.SetDataDirectory(dataPath)
.SetMutableSegmentMaxItemCount(100)
.Open();

var opIndex = zoneTree.Upsert(recordCount + 1, recordCount + 1);
Assert.That(opIndex, Is.EqualTo(recordCount + 1));
zoneTree.Maintenance.Drop();
var opIndex = zoneTree.Upsert(expectedIndex, expectedIndex);
Assert.That(opIndex, Is.EqualTo(expectedIndex));
if (drop)
zoneTree.Maintenance.Drop();
}

CreateData();
ReloadData();
ReloadData(recordCount + 1);
ReloadData(recordCount + 2);
ReloadData(recordCount + 3, true);

Assert.IsTrue(
opIndexes.Order().ToArray()
.SequenceEqual(Enumerable.Range(1, recordCount).Select(x => (long)x)));
}

[Test]
public void TestOpIndex2()
{
var dataPath = "data/TestOpIndex2";
if (Directory.Exists(dataPath))
Directory.Delete(dataPath, true);
var recordCount = 10_000;
var opIndexes = new ConcurrentBag<long>();
void CreateData()
{
using var zoneTree = new ZoneTreeFactory<int, int>()
.SetDataDirectory(dataPath)
.SetMutableSegmentMaxItemCount(100)
.OpenOrCreate();

using var maintainer = zoneTree.CreateMaintainer();
Parallel.For(0, recordCount, (i) =>
{
var opIndex = zoneTree.Upsert(i, i);
opIndexes.Add(opIndex);
});
maintainer.EvictToDisk();
maintainer.WaitForBackgroundThreads();
}

void ReloadData(int expectedIndex, bool drop = false)
{
using var zoneTree = new ZoneTreeFactory<int, int>()
.SetDataDirectory(dataPath)
.SetMutableSegmentMaxItemCount(100)
.Open();

var opIndex = zoneTree.Upsert(expectedIndex, expectedIndex);
Assert.That(opIndex, Is.EqualTo(expectedIndex));

using var maintainer = zoneTree.CreateMaintainer();
maintainer.EvictToDisk();
maintainer.WaitForBackgroundThreads();

if (drop)
zoneTree.Maintenance.Drop();
}

CreateData();
ReloadData(recordCount + 1);
ReloadData(recordCount + 2);
ReloadData(recordCount + 3, true);

Assert.IsTrue(
opIndexes.Order().ToArray()
.SequenceEqual(Enumerable.Range(1, recordCount).Select(x => (long)x)));
Expand Down
4 changes: 2 additions & 2 deletions src/ZoneTree/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<Authors>Ahmed Yasin Koculu</Authors>
<PackageId>ZoneTree</PackageId>
<Title>ZoneTree</Title>
<ProductVersion>1.8.1.0</ProductVersion>
<Version>1.8.1.0</Version>
<ProductVersion>1.8.2.0</ProductVersion>
<Version>1.8.2.0</Version>
<Authors>Ahmed Yasin Koculu</Authors>
<AssemblyTitle>ZoneTree</AssemblyTitle>
<Description>ZoneTree is a persistent, high-performance, transactional, ACID-compliant ordered key-value database for NET. It can operate in memory or on local/cloud storage.</Description>
Expand Down
3 changes: 2 additions & 1 deletion src/ZoneTree/Segments/InMemory/MutableSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ public MutableSegment(
null,
Options.BTreeNodeSize,
Options.BTreeLeafSize);
BTree.SetNextOpIndex(nextOpIndex);

MarkValueDeleted = options.MarkValueDeleted;
MutableSegmentMaxItemCount = options.MutableSegmentMaxItemCount;
Expand All @@ -96,6 +95,8 @@ public MutableSegment(
{
LoadLogEntries(keys, values);
}
// set op index after loading entries.
BTree.SetNextOpIndex(nextOpIndex);
}

void LoadLogEntries(IReadOnlyList<TKey> keys, IReadOnlyList<TValue> values)
Expand Down

0 comments on commit 6a2b178

Please sign in to comment.