Skip to content

Commit

Permalink
Unify mutable segment name across the codebase to avoid confusion.
Browse files Browse the repository at this point in the history
  • Loading branch information
koculu committed Aug 28, 2022
1 parent bd095b3 commit 4e97c37
Show file tree
Hide file tree
Showing 23 changed files with 133 additions and 118 deletions.
4 changes: 2 additions & 2 deletions src/Playground/Benchmark/ZoneTree1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static void Insert(WriteAheadLogMode mode, int count)

if (mode == WriteAheadLogMode.None)
{
zoneTree.Maintenance.MoveSegmentZeroForward();
zoneTree.Maintenance.MoveMutableSegmentForward();
zoneTree.Maintenance.StartMergeOperation()?.Join();
}
basicMaintainer.CompleteRunningTasks();
Expand Down Expand Up @@ -181,7 +181,7 @@ public static void InsertSingleAndMerge(WriteAheadLogMode mode, int count, int k
zoneTree.Upsert(key, key + key);
++key;
}
zoneTree.Maintenance.MoveSegmentZeroForward();
zoneTree.Maintenance.MoveMutableSegmentForward();
zoneTree.Maintenance.StartMergeOperation()?.Join();

basicMaintainer.CompleteRunningTasks();
Expand Down
2 changes: 1 addition & 1 deletion src/Playground/Benchmark/ZoneTree2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static void Insert(WriteAheadLogMode mode, int count)
stopWatch.Restart();
if (mode == WriteAheadLogMode.None)
{
zoneTree.Maintenance.MoveSegmentZeroForward();
zoneTree.Maintenance.MoveMutableSegmentForward();
zoneTree.Maintenance.StartMergeOperation()?.Join();
}
basicMaintainer.CompleteRunningTasks();
Expand Down
2 changes: 1 addition & 1 deletion src/Playground/Benchmark/ZoneTree3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static void Insert(WriteAheadLogMode mode, int count)

if (mode == WriteAheadLogMode.None)
{
zoneTree.Maintenance.ZoneTree.Maintenance.MoveSegmentZeroForward();
zoneTree.Maintenance.ZoneTree.Maintenance.MoveMutableSegmentForward();
zoneTree.Maintenance.ZoneTree.Maintenance.StartMergeOperation()?.Join();
}

Expand Down
4 changes: 2 additions & 2 deletions src/ZoneTree.UnitTests/AtomicUpdateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void IntIntAtomicIncrement(WriteAheadLogMode walMode)
{
data.Upsert(i, i + i);
}
data.Maintenance.MoveSegmentZeroForward();
data.Maintenance.MoveMutableSegmentForward();
data.Maintenance.StartMergeOperation().Join();
var random = new Random();
var off = -1;
Expand Down Expand Up @@ -69,7 +69,7 @@ public void IntIntAtomicIncrement(WriteAheadLogMode walMode)
});
}

data.Maintenance.MoveSegmentZeroForward();
data.Maintenance.MoveMutableSegmentForward();
data.Maintenance.StartMergeOperation().Join();
data.TryGet(counterKey, out var finalValue);
Assert.That(finalValue, Is.EqualTo(off));
Expand Down
2 changes: 1 addition & 1 deletion src/ZoneTree.UnitTests/BottomSegmentMergeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void IntIntBottomMerge()
zoneTree.Upsert(i, i + i);
if (i % p == p - 1)
{
m.MoveSegmentZeroForward();
m.MoveMutableSegmentForward();
m.StartMergeOperation().Join();
for (var j = 0; j < p - 10; ++j)
{
Expand Down
8 changes: 4 additions & 4 deletions src/ZoneTree.UnitTests/FixedSizeKeyAndValueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void IntIntTreeTest()
{
data.Upsert(i, i + i);
}
data.Maintenance.MoveSegmentZeroForward();
data.Maintenance.MoveMutableSegmentForward();
data.Maintenance.StartMergeOperation().Join();
for (var i = 0; i < 2000; ++i)
{
Expand Down Expand Up @@ -51,7 +51,7 @@ public void IntStringTreeTest()
{
data.Upsert(i, (i + i).ToString());
}
data.Maintenance.MoveSegmentZeroForward();
data.Maintenance.MoveMutableSegmentForward();
data.Maintenance.StartMergeOperation().Join();
for (var i = 0; i < n; ++i)
{
Expand Down Expand Up @@ -83,7 +83,7 @@ public void StringIntTreeTest(bool useSparseArray)
{
data.Upsert(i.ToString(), i + i);
}
data.Maintenance.MoveSegmentZeroForward();
data.Maintenance.MoveMutableSegmentForward();
data.Maintenance.StartMergeOperation().Join();
if (useSparseArray)
data.Maintenance.DiskSegment.InitSparseArray(200);
Expand Down Expand Up @@ -118,7 +118,7 @@ public void StringStringTreeTest(bool useSparseArray)
{
data.Upsert(i.ToString(), (i + i).ToString());
}
data.Maintenance.MoveSegmentZeroForward();
data.Maintenance.MoveMutableSegmentForward();
data.Maintenance.StartMergeOperation().Join();
if (useSparseArray)
data.Maintenance.DiskSegment.InitSparseArray(200);
Expand Down
6 changes: 3 additions & 3 deletions src/ZoneTree.UnitTests/IteratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void IntIntIterator()

Assert.That(reverseIterator.Next(), Is.False);

zoneTree.Maintenance.MoveSegmentZeroForward();
zoneTree.Maintenance.MoveMutableSegmentForward();
zoneTree.Maintenance.StartMergeOperation().Join();

Assert.That(zoneTree.Maintenance.DiskSegment.Length, Is.EqualTo(b - 4));
Expand Down Expand Up @@ -157,7 +157,7 @@ public void IntIntIteratorSeek()
Assert.That(reverseIterator.CurrentKey, Is.EqualTo(b - 2));
Assert.That(zoneTree.Count(), Is.EqualTo(b - 4));

zoneTree.Maintenance.MoveSegmentZeroForward();
zoneTree.Maintenance.MoveMutableSegmentForward();
zoneTree.Maintenance.StartMergeOperation().Join();

Assert.That(zoneTree.Maintenance.DiskSegment.Length, Is.EqualTo(b - 4));
Expand Down Expand Up @@ -211,7 +211,7 @@ public void IntIntIteratorReflectNewInserts()
* This is not a bug. Callers can always double check
* with TryGetKey() if they want to read most recent values
* for every key they read from iteration.
* Auto refresh property was made for SegmentZeroMoveForward
* Auto refresh property was made for MutableSegmentMoveForward
* event. A manual refresh also works but it is expensive to call
* for every key.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/ZoneTree.UnitTests/StringTreeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void NullStringKeyTest()
++j;
}
var maintenance = zoneTree.Maintenance;
maintenance.MoveSegmentZeroForward();
maintenance.MoveMutableSegmentForward();
maintenance.StartMergeOperation()?.Join();

using var iterator2 = zoneTree.CreateIterator();
Expand Down
22 changes: 11 additions & 11 deletions src/ZoneTree/Core/ZoneTree.Iterators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Tenray.ZoneTree.Core;
public sealed partial class ZoneTree<TKey, TValue> : IZoneTree<TKey, TValue>, IZoneTreeMaintenance<TKey, TValue>
{
public SegmentCollection CollectSegments(
bool includeSegmentZero,
bool includeMutableSegment,
bool includeDiskSegment,
bool includeBottomSegments)
{
Expand All @@ -15,8 +15,8 @@ public SegmentCollection CollectSegments(
{
var roSegments = ReadOnlySegmentQueue.ToArray();
var seekableIterators = new List<ISeekableIterator<TKey, TValue>>();
if (includeSegmentZero)
seekableIterators.Add(SegmentZero.GetSeekableIterator());
if (includeMutableSegment)
seekableIterators.Add(MutableSegment.GetSeekableIterator());

var readOnlySegmentsArray = roSegments.Select(x => x.GetSeekableIterator()).ToArray();
seekableIterators.AddRange(readOnlySegmentsArray.Reverse());
Expand Down Expand Up @@ -63,7 +63,7 @@ public class SegmentCollection
public IZoneTreeIterator<TKey, TValue> CreateIterator(
IteratorType iteratorType, bool includeDeletedRecords)
{
var includeSegmentZero = iteratorType is not IteratorType.Snapshot and
var includeMutableSegment = iteratorType is not IteratorType.Snapshot and
not IteratorType.ReadOnlyRegion;

var iterator = new ZoneTreeIterator<TKey, TValue>(
Expand All @@ -73,13 +73,13 @@ public IZoneTreeIterator<TKey, TValue> CreateIterator(
autoRefresh: iteratorType == IteratorType.AutoRefresh,
isReverseIterator: false,
includeDeletedRecords,
includeSegmentZero: includeSegmentZero,
includeMutableSegment: includeMutableSegment,
includeDiskSegment: true,
includeBottomSegments: true);

if (iteratorType == IteratorType.Snapshot)
{
MoveSegmentZeroForward();
MoveMutableSegmentForward();
iterator.Refresh();
iterator.WaitUntilReadOnlySegmentsBecomeFullyFrozen();
}
Expand All @@ -94,7 +94,7 @@ public IZoneTreeIterator<TKey, TValue> CreateIterator(
public IZoneTreeIterator<TKey, TValue> CreateReverseIterator(
IteratorType iteratorType, bool includeDeletedRecords)
{
var includeSegmentZero = iteratorType is not IteratorType.Snapshot and
var includeMutableSegment = iteratorType is not IteratorType.Snapshot and
not IteratorType.ReadOnlyRegion;

var iterator = new ZoneTreeIterator<TKey, TValue>(
Expand All @@ -104,13 +104,13 @@ public IZoneTreeIterator<TKey, TValue> CreateReverseIterator(
autoRefresh: iteratorType == IteratorType.AutoRefresh,
isReverseIterator: true,
includeDeletedRecords,
includeSegmentZero: includeSegmentZero,
includeMutableSegment: includeMutableSegment,
includeDiskSegment: true,
includeBottomSegments: true);

if (iteratorType == IteratorType.Snapshot)
{
MoveSegmentZeroForward();
MoveMutableSegmentForward();
iterator.Refresh();
iterator.WaitUntilReadOnlySegmentsBecomeFullyFrozen();
}
Expand All @@ -136,7 +136,7 @@ public IZoneTreeIterator<TKey, TValue> CreateReadOnlySegmentsIterator(bool autoR
autoRefresh: autoRefresh,
isReverseIterator: false,
includeDeletedRecords,
includeSegmentZero: false,
includeMutableSegment: false,
includeDiskSegment: false,
includeBottomSegments: false);
return iterator;
Expand All @@ -158,7 +158,7 @@ public IZoneTreeIterator<TKey, TValue>
autoRefresh: autoRefresh,
isReverseIterator: false,
includeDeletedRecords,
includeSegmentZero: true,
includeMutableSegment: true,
includeDiskSegment: false,
includeBottomSegments: false);
return iterator;
Expand Down
26 changes: 13 additions & 13 deletions src/ZoneTree/Core/ZoneTree.Merge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,39 @@ public sealed partial class ZoneTree<TKey, TValue> : IZoneTree<TKey, TValue>, IZ
/// The movement only occurs
/// if the current segment zero is the given segment zero.
/// </summary>
/// <param name="segmentZero">The segment zero to move forward.</param>
void MoveSegmentZeroForward(IMutableSegment<TKey, TValue> segmentZero)
/// <param name="mutableSegment">The segment zero to move forward.</param>
void MoveMutableSegmentForward(IMutableSegment<TKey, TValue> mutableSegment)
{
lock (AtomicUpdateLock)
{
// move segment zero only if
// the given segment zero is the current segment zero (not already moved)
// and it is not frozen.
if (segmentZero.IsFrozen || segmentZero != SegmentZero)
if (mutableSegment.IsFrozen || mutableSegment != MutableSegment)
return;

//Don't move empty segment zero.
var c = segmentZero.Length;
var c = mutableSegment.Length;
if (c == 0)
return;

segmentZero.Freeze();
ReadOnlySegmentQueue.Enqueue(segmentZero);
MetaWal.EnqueueReadOnlySegment(segmentZero.SegmentId);
mutableSegment.Freeze();
ReadOnlySegmentQueue.Enqueue(mutableSegment);
MetaWal.EnqueueReadOnlySegment(mutableSegment.SegmentId);

SegmentZero = new MutableSegment<TKey, TValue>(
MutableSegment = new MutableSegment<TKey, TValue>(
Options, IncrementalIdProvider.NextId(),
segmentZero.OpIndexProvider);
MetaWal.NewSegmentZero(SegmentZero.SegmentId);
mutableSegment.OpIndexProvider);
MetaWal.NewMutableSegment(MutableSegment.SegmentId);
}
OnSegmentZeroMovedForward?.Invoke(this);
OnMutableSegmentMovedForward?.Invoke(this);
}

public void MoveSegmentZeroForward()
public void MoveMutableSegmentForward()
{
lock (AtomicUpdateLock)
{
MoveSegmentZeroForward(SegmentZero);
MoveMutableSegmentForward(MutableSegment);
}
}

Expand Down
38 changes: 19 additions & 19 deletions src/ZoneTree/Core/ZoneTree.ReadWrite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public sealed partial class ZoneTree<TKey, TValue> : IZoneTree<TKey, TValue>, IZ
public bool ContainsKey(in TKey key)
{
TValue value;
if (SegmentZero.ContainsKey(key))
if (MutableSegment.ContainsKey(key))
{
if (SegmentZero.TryGet(key, out value))
if (MutableSegment.TryGet(key, out value))
return !IsValueDeleted(value);
}

Expand Down Expand Up @@ -85,7 +85,7 @@ bool TryGetFromReadonlySegments(in TKey key, out TValue value)

public bool TryGet(in TKey key, out TValue value)
{
if (SegmentZero.TryGet(key, out value))
if (MutableSegment.TryGet(key, out value))
{
return !IsValueDeleted(value);
}
Expand All @@ -97,7 +97,7 @@ public bool TryGetAndUpdate(in TKey key, out TValue value, ValueUpdaterDelegate<
if (IsReadOnly)
throw new ZoneTreeIsReadOnlyException();

if (SegmentZero.TryGet(key, out value))
if (MutableSegment.TryGet(key, out value))
{
if (IsValueDeleted(value))
return false;
Expand All @@ -122,7 +122,7 @@ public bool TryAtomicGetAndUpdate(in TKey key, out TValue value, ValueUpdaterDel

lock (AtomicUpdateLock)
{
if (SegmentZero.TryGet(key, out value))
if (MutableSegment.TryGet(key, out value))
{
if (IsValueDeleted(value))
return false;
Expand Down Expand Up @@ -175,39 +175,39 @@ public bool TryAtomicAddOrUpdate(in TKey key, in TValue valueToAdd, ValueUpdater
if (IsReadOnly)
throw new ZoneTreeIsReadOnlyException();
AddOrUpdateResult status;
IMutableSegment<TKey, TValue> segmentZero;
IMutableSegment<TKey, TValue> mutableSegment;
while (true)
{
lock (AtomicUpdateLock)
{
segmentZero = SegmentZero;
if (segmentZero.IsFrozen)
mutableSegment = MutableSegment;
if (mutableSegment.IsFrozen)
{
status = AddOrUpdateResult.RETRY_SEGMENT_IS_FULL;
}
else if (segmentZero.TryGet(in key, out var existing))
else if (mutableSegment.TryGet(in key, out var existing))
{
if (!valueUpdater(ref existing))
return false;
status = segmentZero.Upsert(key, existing);
status = mutableSegment.Upsert(key, existing);
}
else if (TryGetFromReadonlySegments(in key, out existing))
{
if (!valueUpdater(ref existing))
return false;
status = segmentZero.Upsert(key, existing);
status = mutableSegment.Upsert(key, existing);
}
else
{
status = segmentZero.Upsert(key, valueToAdd);
status = mutableSegment.Upsert(key, valueToAdd);
}
}
switch (status)
{
case AddOrUpdateResult.RETRY_SEGMENT_IS_FROZEN:
continue;
case AddOrUpdateResult.RETRY_SEGMENT_IS_FULL:
MoveSegmentZeroForward(segmentZero);
MoveMutableSegmentForward(mutableSegment);
continue;
default:
return status == AddOrUpdateResult.ADDED;
Expand All @@ -231,14 +231,14 @@ public void Upsert(in TKey key, in TValue value)
throw new ZoneTreeIsReadOnlyException();
while (true)
{
var segmentZero = SegmentZero;
var status = segmentZero.Upsert(key, value);
var mutableSegment = MutableSegment;
var status = mutableSegment.Upsert(key, value);
switch (status)
{
case AddOrUpdateResult.RETRY_SEGMENT_IS_FROZEN:
continue;
case AddOrUpdateResult.RETRY_SEGMENT_IS_FULL:
MoveSegmentZeroForward(segmentZero);
MoveMutableSegmentForward(mutableSegment);
continue;
default:
return;
Expand All @@ -262,15 +262,15 @@ public void ForceDelete(in TKey key)
throw new ZoneTreeIsReadOnlyException();
while (true)
{
var segmentZero = SegmentZero;
var status = segmentZero.Delete(key);
var mutableSegment = MutableSegment;
var status = mutableSegment.Delete(key);
switch (status)
{
case AddOrUpdateResult.RETRY_SEGMENT_IS_FROZEN:
ForceDelete(key);
continue;
case AddOrUpdateResult.RETRY_SEGMENT_IS_FULL:
MoveSegmentZeroForward(segmentZero);
MoveMutableSegmentForward(mutableSegment);
ForceDelete(key);
continue;
default: return;
Expand Down
Loading

0 comments on commit 4e97c37

Please sign in to comment.