Skip to content

Commit

Permalink
Merge pull request #37 from koculu/validate-delete-value-configuration
Browse files Browse the repository at this point in the history
Validate delete value configuration.
  • Loading branch information
koculu committed Jun 16, 2023
2 parents f2d6006 + ecee237 commit 04f5caa
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 24 deletions.
8 changes: 5 additions & 3 deletions src/Playground/Benchmark/OldTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static void Insert(WriteAheadLogMode mode, int count)
{
var recCount = count / 1000000.0 + "M";
Console.WriteLine("\r\n--------------------------");
new StatsCollector().LogWithColor($"\r\n{mode} Insert <int,int> {recCount}\r\n", ConsoleColor.Cyan);
new StatsCollector().LogWithColor($"\r\n{mode} Insert <int,int> {recCount}\r\n", ConsoleColor.Cyan);
string dataPath = GetDataPath(mode, count);
if (TestConfig.RecreateDatabases && Directory.Exists(dataPath))
Directory.Delete(dataPath, true);
Expand Down Expand Up @@ -51,7 +51,7 @@ public static void Insert(WriteAheadLogMode mode, int count)

new StatsCollector().LogWithColor(
"Completed in:",
stopWatch.ElapsedMilliseconds,
stopWatch.ElapsedMilliseconds,
ConsoleColor.Green);
stopWatch.Restart();

Expand Down Expand Up @@ -179,7 +179,8 @@ public static void InsertSingleAndMerge(WriteAheadLogMode mode, int count, int k
stopWatch.ElapsedMilliseconds,
ConsoleColor.DarkYellow);

for (var i = 0; i < amount; ++i) {
for (var i = 0; i < amount; ++i)
{
zoneTree.Upsert(key, key + key);
++key;
}
Expand Down Expand Up @@ -271,6 +272,7 @@ public static void MultipleIterate(WriteAheadLogMode mode, int count, int iterat
private static IZoneTree<int, int> OpenOrCreateZoneTree(WriteAheadLogMode mode, string dataPath)
{
return new ZoneTreeFactory<int, int>()
.DisableDeleteValueConfigurationValidation(false)
.SetMutableSegmentMaxItemCount(TestConfig.MutableSegmentMaxItemCount)
.SetDiskSegmentCompression(TestConfig.EnableDiskSegmentCompression)
.SetDiskSegmentCompressionBlockSize(TestConfig.DiskCompressionBlockSize)
Expand Down
3 changes: 2 additions & 1 deletion src/Playground/Benchmark/ZoneTreeTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected string GetLabel(string label)
protected ZoneTreeFactory<TKey, TValue> GetFactory()
{
return new ZoneTreeFactory<TKey, TValue>()
.DisableDeleteValueConfigurationValidation(false)
.SetMutableSegmentMaxItemCount(TestConfig.MutableSegmentMaxItemCount)
.SetDiskSegmentMaxItemCount(TestConfig.DiskSegmentMaxItemCount)
.SetDiskSegmentCompression(TestConfig.EnableDiskSegmentCompression)
Expand Down Expand Up @@ -87,7 +88,7 @@ public void AddDatabaseFileUsage(IStatsCollector stats)
long totalBytes = 0;
foreach (var file in files)
{
var finfo = new FileInfo(file);
var finfo = new FileInfo(file);
totalBytes += finfo.Length;
}
stats.AddAdditionalStats("Disk Usage", totalBytes.Bytes().Humanize());
Expand Down
14 changes: 10 additions & 4 deletions src/ZoneTree.UnitTests/AtomicUpdateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public void IntIntAtomicIncrement(WriteAheadLogMode walMode)
Directory.Delete(dataPath, true);
var counterKey = -3999;
using var data = new ZoneTreeFactory<int, int>()
.DisableDeleteValueConfigurationValidation(false)
.SetComparer(new Int32ComparerDescending())
.SetMutableSegmentMaxItemCount(500)
.SetDataDirectory(dataPath)
Expand All @@ -42,7 +43,7 @@ public void IntIntAtomicIncrement(WriteAheadLogMode walMode)
len = random.Next(1501);
for (var i = 0; i < len; ++i)
{
data.TryAtomicAddOrUpdate(counterKey, 0,
data.TryAtomicAddOrUpdate(counterKey, 0,
bool (ref int y) =>
{
++y;
Expand Down Expand Up @@ -86,6 +87,7 @@ public void IntIntAtomicIncrementForBTree(WriteAheadLogMode walMode)
Directory.Delete(dataPath, true);

using var data = new ZoneTreeFactory<int, int>()
.DisableDeleteValueConfigurationValidation(false)
.SetComparer(new Int32ComparerDescending())
.SetDataDirectory(dataPath)
.SetWriteAheadLogDirectory(dataPath)
Expand All @@ -109,8 +111,9 @@ public void IntIntAtomicIncrementForBTree(WriteAheadLogMode walMode)
len = random.Next(1501);
for (var i = 0; i < len; ++i)
{
data.TryAtomicAddOrUpdate(3999, 0,
bool (ref int y) => {
data.TryAtomicAddOrUpdate(3999, 0,
bool (ref int y) =>
{
++y;
return true;
});
Expand Down Expand Up @@ -150,6 +153,7 @@ public void IntIntMutableSegmentOnlyAtomicIncrement(WriteAheadLogMode walMode)
Directory.Delete(dataPath, true);
var counterKey = -3999;
using var data = new ZoneTreeFactory<int, int>()
.DisableDeleteValueConfigurationValidation(false)
.SetComparer(new Int32ComparerDescending())
.SetDataDirectory(dataPath)
.SetWriteAheadLogDirectory(dataPath)
Expand All @@ -174,7 +178,8 @@ public void IntIntMutableSegmentOnlyAtomicIncrement(WriteAheadLogMode walMode)
for (var i = 0; i < len; ++i)
{
data.TryAtomicAddOrUpdate(counterKey, 0,
bool (ref int y) => {
bool (ref int y) =>
{
++y;
return true;
});
Expand Down Expand Up @@ -213,6 +218,7 @@ public void IntIntMutableSegmentSeveralUpserts(WriteAheadLogMode walMode)
if (Directory.Exists(dataPath))
Directory.Delete(dataPath, true);
using var data = new ZoneTreeFactory<int, int>()
.DisableDeleteValueConfigurationValidation(false)
.SetComparer(new Int32ComparerDescending())
.SetDataDirectory(dataPath)
.SetWriteAheadLogDirectory(dataPath)
Expand Down
2 changes: 2 additions & 0 deletions src/ZoneTree.UnitTests/BottomSegmentMergeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public void IntIntBottomMerge()
Directory.Delete(dataPath, true);

var zoneTree = new ZoneTreeFactory<int, int>()
.DisableDeleteValueConfigurationValidation(false)
.SetDiskSegmentMaxItemCount(10)
.SetDataDirectory(dataPath)
.ConfigureDiskSegmentOptions(
Expand Down Expand Up @@ -56,6 +57,7 @@ public void IntIntBottomMerge()
zoneTree.Dispose();

zoneTree = new ZoneTreeFactory<int, int>()
.DisableDeleteValueConfigurationValidation(false)
.SetDiskSegmentMaxItemCount(10)
.SetDataDirectory(dataPath)
.OpenOrCreate();
Expand Down
4 changes: 3 additions & 1 deletion src/ZoneTree.UnitTests/ExceptionlessTransactionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public void TransactionWithNoThrowAPI(int compactionThreshold)
Directory.Delete(dataPath, true);

using var zoneTree = new ZoneTreeFactory<int, int>()
.DisableDeleteValueConfigurationValidation(false)
.SetDataDirectory(dataPath)
.SetWriteAheadLogDirectory(dataPath)
.OpenOrCreateTransactional();
Expand Down Expand Up @@ -63,6 +64,7 @@ public async Task TransactionWithFluentAPI(int compactionThreshold)
Directory.Delete(dataPath, true);

using var zoneTree = new ZoneTreeFactory<int, int>()
.DisableDeleteValueConfigurationValidation(false)
.SetDataDirectory(dataPath)
.SetWriteAheadLogDirectory(dataPath)
.ConfigureWriteAheadLogOptions(x =>
Expand Down Expand Up @@ -103,7 +105,7 @@ await Parallel.ForEachAsync(Enumerable.Range(0, 1000), async (x, cancel) =>
if (transaction.TotalPendingTransactionsRetried > 0)
Console.WriteLine("pending:" + transaction.TotalPendingTransactionsRetried);
});

zoneTree.Maintenance.DestroyTree();
}
}
43 changes: 42 additions & 1 deletion src/ZoneTree.UnitTests/FixedSizeKeyAndValueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public void IntIntTreeTest()
if (Directory.Exists(dataPath))
Directory.Delete(dataPath, true);
using var data = new ZoneTreeFactory<int, int>()
.DisableDeleteValueConfigurationValidation(false)
.SetMutableSegmentMaxItemCount(5)
.SetDataDirectory(dataPath)
.SetWriteAheadLogDirectory(dataPath)
Expand Down Expand Up @@ -64,6 +65,45 @@ public void IntStringTreeTest()
data.Maintenance.DestroyTree();
}

[Test]
public void IntStringDeleteTest()
{
var dataPath = "data/IntStringDeleteTest";
if (Directory.Exists(dataPath))
Directory.Delete(dataPath, true);

using var data = new ZoneTreeFactory<int, string>()
.SetDataDirectory(dataPath)
.OpenOrCreate();
data.TryAtomicAdd(1, "1");
data.TryAtomicAdd(2, "2");
data.TryAtomicAdd(3, "3");
data.TryDelete(2);
Assert.That(data.ContainsKey(1), Is.True);
Assert.That(data.ContainsKey(2), Is.False);
Assert.That(data.ContainsKey(3), Is.True);
}

[Test]
public void IntNullableIntDeleteTest()
{
var dataPath = "data/IntStringDeleteTest";
if (Directory.Exists(dataPath))
Directory.Delete(dataPath, true);

using var data = new ZoneTreeFactory<int, int?>()
.SetDataDirectory(dataPath)
.SetValueSerializer(new NullableInt32Serializer())
.OpenOrCreate();
data.TryAtomicAdd(1, 1);
data.TryAtomicAdd(2, 2);
data.TryAtomicAdd(3, 3);
data.TryDelete(2);
Assert.That(data.ContainsKey(1), Is.True);
Assert.That(data.ContainsKey(2), Is.False);
Assert.That(data.ContainsKey(3), Is.True);
}

[TestCase(true)]
[TestCase(false)]
public void StringIntTreeTest(bool useSparseArray)
Expand All @@ -73,6 +113,7 @@ public void StringIntTreeTest(bool useSparseArray)
Directory.Delete(dataPath, true);

using var data = new ZoneTreeFactory<string, int>()
.DisableDeleteValueConfigurationValidation(false)
.SetMutableSegmentMaxItemCount(5)
.SetDataDirectory(dataPath)
.SetWriteAheadLogDirectory(dataPath)
Expand Down Expand Up @@ -139,7 +180,7 @@ static void ReloadIntIntTreeTestHelper(string dataPath, bool destroy)
.SetMutableSegmentMaxItemCount(5)
.SetDataDirectory(dataPath)
.SetWriteAheadLogDirectory(dataPath)
.ConfigureWriteAheadLogOptions(x =>
.ConfigureWriteAheadLogOptions(x =>
x.WriteAheadLogMode = WriteAheadLogMode.Sync)
.SetIsValueDeletedDelegate((in int x) => x == -1)
.SetMarkValueDeletedDelegate((ref int x) => x = -1)
Expand Down
8 changes: 8 additions & 0 deletions src/ZoneTree.UnitTests/IteratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ public void IntIntIteratorParallelInserts()
var iteratorCount = 1000;

using var zoneTree = new ZoneTreeFactory<int, int>()
.DisableDeleteValueConfigurationValidation(false)
.SetMutableSegmentMaxItemCount(insertCount * 2)
.SetDataDirectory(dataPath)
.SetWriteAheadLogDirectory(dataPath)
Expand Down Expand Up @@ -293,6 +294,7 @@ public void IntIntReverseIteratorParallelInserts(bool reverse)
var iteratorCount = 1000;

using var zoneTree = new ZoneTreeFactory<int, int>()
.DisableDeleteValueConfigurationValidation(false)
.SetMutableSegmentMaxItemCount(insertCount * 2)
.SetDataDirectory(dataPath)
.SetWriteAheadLogDirectory(dataPath)
Expand Down Expand Up @@ -355,6 +357,7 @@ public void IntIntSnapshotIteratorParallelInserts()
var iteratorCount = 1000;

using var zoneTree = new ZoneTreeFactory<int, int>()
.DisableDeleteValueConfigurationValidation(false)
.SetMutableSegmentMaxItemCount(insertCount * 2)
.SetDataDirectory(dataPath)
.SetWriteAheadLogDirectory(dataPath)
Expand Down Expand Up @@ -397,6 +400,7 @@ public void ReversePrefixSearch()
Directory.Delete(dataPath, true);

using var zoneTree = new ZoneTreeFactory<string, int>()
.DisableDeleteValueConfigurationValidation(false)
.SetDataDirectory(dataPath)
.SetWriteAheadLogDirectory(dataPath)
.OpenOrCreate();
Expand Down Expand Up @@ -438,6 +442,7 @@ public void PrefixSearch()
Directory.Delete(dataPath, true);

using var zoneTree = new ZoneTreeFactory<string, int>()
.DisableDeleteValueConfigurationValidation(false)
.SetDataDirectory(dataPath)
.SetWriteAheadLogDirectory(dataPath)
.OpenOrCreate();
Expand Down Expand Up @@ -486,6 +491,7 @@ public void SeekIteratorsAfterMerge(
Directory.Delete(dataPath, true);

using var zoneTree = new ZoneTreeFactory<string, int>()
.DisableDeleteValueConfigurationValidation(false)
.SetDataDirectory(dataPath)
.SetWriteAheadLogDirectory(dataPath)
.SetComparer(new StringCurrentCultureComparerAscending())
Expand Down Expand Up @@ -560,6 +566,7 @@ public void SeekIteratorsAfterMergeReload(
Directory.Delete(dataPath, true);

var zoneTree = new ZoneTreeFactory<string, int>()
.DisableDeleteValueConfigurationValidation(false)
.SetDataDirectory(dataPath)
.SetWriteAheadLogDirectory(dataPath)
.SetComparer(new StringCurrentCultureComparerAscending())
Expand All @@ -582,6 +589,7 @@ public void SeekIteratorsAfterMergeReload(

zoneTree.Dispose();
zoneTree = new ZoneTreeFactory<string, int>()
.DisableDeleteValueConfigurationValidation(false)
.SetDataDirectory(dataPath)
.SetWriteAheadLogDirectory(dataPath)
.SetComparer(new StringCurrentCultureComparerAscending())
Expand Down
7 changes: 5 additions & 2 deletions src/ZoneTree.UnitTests/OptimisticTransactionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public void FirstTransaction(int compactionThreshold)
Directory.Delete(dataPath, true);

using var zoneTree = new ZoneTreeFactory<int, int>()
.DisableDeleteValueConfigurationValidation(false)
.SetDataDirectory(dataPath)
.SetWriteAheadLogDirectory(dataPath)
.OpenOrCreateTransactional();
Expand Down Expand Up @@ -64,6 +65,7 @@ public void SeveralParallelTransactions(WriteAheadLogMode walMode)

int n = 10000;
using var zoneTree = new ZoneTreeFactory<int, int>()
.DisableDeleteValueConfigurationValidation(false)
.SetDataDirectory(dataPath)
.SetWriteAheadLogDirectory(dataPath)
.ConfigureWriteAheadLogOptions(x => x.WriteAheadLogMode = walMode)
Expand Down Expand Up @@ -91,6 +93,7 @@ public void SeveralParallelUpserts(WriteAheadLogMode walMode)

int n = 10000;
using var zoneTree = new ZoneTreeFactory<int, int>()
.DisableDeleteValueConfigurationValidation(false)
.SetDataDirectory(dataPath)
.SetWriteAheadLogDirectory(dataPath)
.ConfigureWriteAheadLogOptions(x => x.WriteAheadLogMode = walMode)
Expand Down Expand Up @@ -165,7 +168,7 @@ public void TransactionLogCompactionTest(int compactionThreshold)
var dataPath = "data/TransactionLogCompactionTest" + compactionThreshold;
if (Directory.Exists(dataPath))
Directory.Delete(dataPath, true);

using var zoneTree = new ZoneTreeFactory<int, int>()
.SetDataDirectory(dataPath)
.SetWriteAheadLogDirectory(dataPath)
Expand Down Expand Up @@ -269,7 +272,7 @@ public void TransactionIsolationTest(int compactionThreshold)
Assert.IsTrue(zoneTree.TryGetNoThrow(tx1, 5, out var v5).IsAborted);
Assert.IsTrue(zoneTree.TryGetNoThrow(tx2, 5, out v5).Succeeded);
Assert.That(v5, Is.EqualTo(12));

// tx2 depends on tx1 bcs of read of key 9. tx1 aborted and so tx2.
Assert.That(zoneTree.PrepareAndCommitNoThrow(tx2).IsAborted, Is.True);

Expand Down
3 changes: 2 additions & 1 deletion src/ZoneTree.UnitTests/StringTreeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void NullStringKeyTest()
{
"Zbc", "DDD", "Abc", "Cbc", "Dbc", null
};
for(var i = 0; i < keys.Length; i++)
for (var i = 0; i < keys.Length; i++)
{
zoneTree.Upsert(keys[i], values[i]);
}
Expand Down Expand Up @@ -65,6 +65,7 @@ public void TestSingleCharacter()
for (var i = 0; i < 2; ++i)
{
using var db = new ZoneTreeFactory<string, int>()
.DisableDeleteValueConfigurationValidation(false)
.SetDataDirectory(dataPath)
.OpenOrCreate();
db.Upsert("0", 123);
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.6.3.0</ProductVersion>
<Version>1.6.3.0</Version>
<ProductVersion>1.6.4.0</ProductVersion>
<Version>1.6.4.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
23 changes: 23 additions & 0 deletions src/ZoneTree/Options/DeleteValueConfigurationValidation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Tenray.ZoneTree.Options;

/// <summary>
/// Defines the validation behavior
/// of not providing the delete value delegates.
/// </summary>
public enum DeleteValueConfigurationValidation
{
/// <summary>
/// Throws an error if the value deletion delegates are not configured.
/// </summary>
Required,

/// <summary>
/// Logs a warning if the value deletion delegates are not configured.
/// </summary>
Warning,

/// <summary>
/// Allows creating ZoneTree without delete record support.
/// </summary>
NotRequired,
}
Loading

0 comments on commit 04f5caa

Please sign in to comment.