Skip to content

Commit 6a2b178

Browse files
committed
Set NextOpIndex after loading BTree to remove skipped regions on op index.
1 parent bbf27b3 commit 6a2b178

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

src/ZoneTree.UnitTests/OpIndexTests.cs

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,77 @@ void CreateData()
3131
maintainer.WaitForBackgroundThreads();
3232
}
3333

34-
void ReloadData()
34+
void ReloadData(int expectedIndex, bool drop = false)
3535
{
3636
using var zoneTree = new ZoneTreeFactory<int, int>()
3737
.SetDataDirectory(dataPath)
3838
.SetMutableSegmentMaxItemCount(100)
3939
.Open();
4040

41-
var opIndex = zoneTree.Upsert(recordCount + 1, recordCount + 1);
42-
Assert.That(opIndex, Is.EqualTo(recordCount + 1));
43-
zoneTree.Maintenance.Drop();
41+
var opIndex = zoneTree.Upsert(expectedIndex, expectedIndex);
42+
Assert.That(opIndex, Is.EqualTo(expectedIndex));
43+
if (drop)
44+
zoneTree.Maintenance.Drop();
4445
}
46+
4547
CreateData();
46-
ReloadData();
48+
ReloadData(recordCount + 1);
49+
ReloadData(recordCount + 2);
50+
ReloadData(recordCount + 3, true);
51+
52+
Assert.IsTrue(
53+
opIndexes.Order().ToArray()
54+
.SequenceEqual(Enumerable.Range(1, recordCount).Select(x => (long)x)));
55+
}
56+
57+
[Test]
58+
public void TestOpIndex2()
59+
{
60+
var dataPath = "data/TestOpIndex2";
61+
if (Directory.Exists(dataPath))
62+
Directory.Delete(dataPath, true);
63+
var recordCount = 10_000;
64+
var opIndexes = new ConcurrentBag<long>();
65+
void CreateData()
66+
{
67+
using var zoneTree = new ZoneTreeFactory<int, int>()
68+
.SetDataDirectory(dataPath)
69+
.SetMutableSegmentMaxItemCount(100)
70+
.OpenOrCreate();
71+
72+
using var maintainer = zoneTree.CreateMaintainer();
73+
Parallel.For(0, recordCount, (i) =>
74+
{
75+
var opIndex = zoneTree.Upsert(i, i);
76+
opIndexes.Add(opIndex);
77+
});
78+
maintainer.EvictToDisk();
79+
maintainer.WaitForBackgroundThreads();
80+
}
81+
82+
void ReloadData(int expectedIndex, bool drop = false)
83+
{
84+
using var zoneTree = new ZoneTreeFactory<int, int>()
85+
.SetDataDirectory(dataPath)
86+
.SetMutableSegmentMaxItemCount(100)
87+
.Open();
88+
89+
var opIndex = zoneTree.Upsert(expectedIndex, expectedIndex);
90+
Assert.That(opIndex, Is.EqualTo(expectedIndex));
91+
92+
using var maintainer = zoneTree.CreateMaintainer();
93+
maintainer.EvictToDisk();
94+
maintainer.WaitForBackgroundThreads();
95+
96+
if (drop)
97+
zoneTree.Maintenance.Drop();
98+
}
99+
100+
CreateData();
101+
ReloadData(recordCount + 1);
102+
ReloadData(recordCount + 2);
103+
ReloadData(recordCount + 3, true);
104+
47105
Assert.IsTrue(
48106
opIndexes.Order().ToArray()
49107
.SequenceEqual(Enumerable.Range(1, recordCount).Select(x => (long)x)));

src/ZoneTree/Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<Authors>Ahmed Yasin Koculu</Authors>
66
<PackageId>ZoneTree</PackageId>
77
<Title>ZoneTree</Title>
8-
<ProductVersion>1.8.1.0</ProductVersion>
9-
<Version>1.8.1.0</Version>
8+
<ProductVersion>1.8.2.0</ProductVersion>
9+
<Version>1.8.2.0</Version>
1010
<Authors>Ahmed Yasin Koculu</Authors>
1111
<AssemblyTitle>ZoneTree</AssemblyTitle>
1212
<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>

src/ZoneTree/Segments/InMemory/MutableSegment.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ public MutableSegment(
8282
null,
8383
Options.BTreeNodeSize,
8484
Options.BTreeLeafSize);
85-
BTree.SetNextOpIndex(nextOpIndex);
8685

8786
MarkValueDeleted = options.MarkValueDeleted;
8887
MutableSegmentMaxItemCount = options.MutableSegmentMaxItemCount;
@@ -96,6 +95,8 @@ public MutableSegment(
9695
{
9796
LoadLogEntries(keys, values);
9897
}
98+
// set op index after loading entries.
99+
BTree.SetNextOpIndex(nextOpIndex);
99100
}
100101

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

0 commit comments

Comments
 (0)