Skip to content

Commit

Permalink
Colleect garbage from write ahead log of mutable segment.
Browse files Browse the repository at this point in the history
  • Loading branch information
koculu committed Jun 17, 2023
1 parent d10e0cc commit ea5e49e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/ZoneTree.UnitTests/FixedSizeKeyAndValueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public void IntStringGarbageCollectionTest()
}

// reload tree and check the length
for (var i = 0; i < 3; ++i)
{
using var data = new ZoneTreeFactory<int, string>()
.Configure(options => options.EnableSingleSegmentGarbageCollection = true)
Expand Down
23 changes: 20 additions & 3 deletions src/ZoneTree/Core/ZoneTreeLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Tenray.ZoneTree.Segments.InMemory;
using Tenray.ZoneTree.Segments.MultiPart;
using Tenray.ZoneTree.Segments.NullDisk;
using Tenray.ZoneTree.WAL;

namespace Tenray.ZoneTree.Core;

Expand Down Expand Up @@ -145,15 +146,17 @@ void ValidateSegmentOrder()
}
}

void LoadMutableSegment(long maximumOpIndex,
IWriteAheadLog<TKey, TValue> LoadMutableSegment(long maximumOpIndex,
bool collectGarbage)
{
var loader = new MutableSegmentLoader<TKey, TValue>(Options);
MutableSegment = loader
.LoadMutableSegment(
ZoneTreeMeta.MutableSegment,
maximumOpIndex,
collectGarbage);
collectGarbage,
out var wal);
return wal;
}

long LoadReadOnlySegments()
Expand Down Expand Up @@ -237,7 +240,21 @@ void SetMaximumId()
SetMaximumId();
var maximumOpIndex = LoadReadOnlySegments();
bool collectGarbage = Options.EnableSingleSegmentGarbageCollection && !ZoneTreeMeta.HasDiskSegment && ReadOnlySegments.Count == 0;
LoadMutableSegment(maximumOpIndex, collectGarbage);
var mutableSegmentWal = LoadMutableSegment(maximumOpIndex, collectGarbage);
if (collectGarbage)
{
var len = MutableSegment.Length;
var keys = new TKey[len];
var values = new TValue[len];
var iterator = MutableSegment.GetSeekableIterator();
var i = 0;
while (iterator.Next())
{
keys[i] = iterator.CurrentKey;
values[i++] = iterator.CurrentValue;
}
mutableSegmentWal.ReplaceWriteAheadLog(keys, values, true);
}
LoadDiskSegment();
LoadBottomSegments();
var zoneTree = new ZoneTree<TKey, TValue>(Options, ZoneTreeMeta,
Expand Down
6 changes: 0 additions & 6 deletions src/ZoneTree/Options/ZoneTreeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,6 @@ public void Validate()
/// </summary>
public DeleteValueConfigurationValidation DeleteValueConfigurationValidation { get; set; }

/// <summary>
/// If ZoneTree contains single segment (which is the mutable segment),
/// there is the opportunity to hard delete the soft deleted values.
/// If enabled, tree performs a garbage collection on load if applicable.
/// </summary>
///
/// <summary>
/// If the ZoneTree contains only a single segment (which is the mutable segment),
/// there is an opportunity to perform a hard delete of the soft deleted values.
Expand Down
6 changes: 4 additions & 2 deletions src/ZoneTree/Segments/InMemory/MutableSegmentLoader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Tenray.ZoneTree.Exceptions;
using Tenray.ZoneTree.Core;
using Tenray.ZoneTree.Options;
using Tenray.ZoneTree.WAL;

namespace Tenray.ZoneTree.Segments.InMemory;

Expand All @@ -17,9 +18,10 @@ public sealed class MutableSegmentLoader<TKey, TValue>
public IMutableSegment<TKey, TValue> LoadMutableSegment(
long segmentId,
long maximumOpIndex,
bool collectGarbage)
bool collectGarbage,
out IWriteAheadLog<TKey, TValue> wal)
{
var wal = Options.WriteAheadLogProvider
wal = Options.WriteAheadLogProvider
.GetOrCreateWAL(
segmentId,
ZoneTree<TKey, TValue>.SegmentWalCategory,
Expand Down

0 comments on commit ea5e49e

Please sign in to comment.