Skip to content

Commit

Permalink
Relocate DataHeaderDevice to the corresponding disk segments.
Browse files Browse the repository at this point in the history
  • Loading branch information
koculu committed May 27, 2023
1 parent 5d1b505 commit d32418a
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 24 deletions.
16 changes: 16 additions & 0 deletions src/ZoneTree/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@
Scope = "namespaceanddescendants",
Target = "Tenray.ZoneTree.Comparers")]

[assembly: SuppressMessage(
"\tUsage",
"CA2213: Disposable fields should be disposed",
Justification = "False warning: Device is disposed on ReleaseResources method.",
Scope = "namespaceanddescendants",
Target = "Tenray.ZoneTree.Segments.DiskSegmentVariations")]

[assembly: SuppressMessage(
"\tUsage",
"CA2213: Disposable fields should be disposed",
Justification = "False warning: Device is disposed on ReleaseResources method.",
Scope = "member",
Target = "Tenray.ZoneTree.Segments.Disk.DiskSegment.DataDevice")]

#pragma warning disable CA2213 // False warning: Device is disposed on ReleaseResources.

[assembly: SuppressMessage(
"Naming",
"CA1711: Identifiers should not have incorrect suffix",
Expand Down
25 changes: 7 additions & 18 deletions src/ZoneTree/Segments/Disk/DiskSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public abstract class DiskSegment<TKey, TValue> : IDiskSegment<TKey, TValue>

protected readonly ISerializer<TValue> ValueSerializer;

protected IRandomAccessDevice DataHeaderDevice;

protected IRandomAccessDevice DataDevice;

protected int KeySize;
Expand Down Expand Up @@ -48,9 +46,7 @@ public abstract class DiskSegment<TKey, TValue> : IDiskSegment<TKey, TValue>

public bool IsIterativeIndexReader => false;

public int ReadBufferCount =>
(DataDevice?.ReadBufferCount ?? 0) +
(DataHeaderDevice?.ReadBufferCount ?? 0);
public abstract int ReadBufferCount { get; }

public Action<IDiskSegment<TKey, TValue>, Exception> DropFailureReporter { get; set; }

Expand All @@ -66,11 +62,9 @@ public abstract class DiskSegment<TKey, TValue> : IDiskSegment<TKey, TValue>

protected unsafe DiskSegment(long segmentId,
ZoneTreeOptions<TKey, TValue> options,
IRandomAccessDevice dataHeaderDevice,
IRandomAccessDevice dataDevice)
{
SegmentId = segmentId;
DataHeaderDevice = dataHeaderDevice;
DataDevice = dataDevice;

Comparer = options.Comparer;
Expand Down Expand Up @@ -351,12 +345,13 @@ public void Drop()
// No active reads remaining.
// Safe to drop.

DataHeaderDevice?.Delete();
DataDevice.Delete();
DeleteDevices();
IsDropped = true;
}
}

protected abstract void DeleteDevices();

public IIndexedReader<TKey, TValue> GetIndexedReader()
{
return this;
Expand Down Expand Up @@ -465,18 +460,12 @@ public long GetFirstGreaterOrEqualPosition(in TKey key)
return GetFirstGreaterOrEqualPosition(in key, lower, upper);
}

public void ReleaseResources()
public virtual void ReleaseResources()
{
DataHeaderDevice?.Dispose();
DataDevice.Dispose();
DataDevice?.Dispose();
}

public int ReleaseReadBuffers(long ticks)
{
var a = DataHeaderDevice?.ReleaseReadBuffers(ticks) ?? 0;
var b = DataDevice?.ReleaseReadBuffers(ticks) ?? 0;
return a + b;
}
public abstract int ReleaseReadBuffers(long ticks);

public void Drop(HashSet<long> excludedPartIds)
{
Expand Down
2 changes: 2 additions & 0 deletions src/ZoneTree/Segments/Disk/DiskSegmentConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
public static class DiskSegmentConstants
{
public const string DataHeaderCategory = ".head";

public const string DataCategory = ".data";

public const string MultiPartDiskSegmentCategory = ".multi";
}
2 changes: 1 addition & 1 deletion src/ZoneTree/Segments/Disk/DiskSegmentFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static class DiskSegmentFactory
var hasFixedSizeKey = !RuntimeHelpers.IsReferenceOrContainsReferences<TKey>();
var hasFixedSizeValue = !RuntimeHelpers.IsReferenceOrContainsReferences<TValue>();
if (hasFixedSizeKey && hasFixedSizeValue)
return new FixedSizeKeyAndValueDiskSegment<TKey, TValue>(segmentId, options, dataHeaderDevice, dataDevice);
return new FixedSizeKeyAndValueDiskSegment<TKey, TValue>(segmentId, options, dataDevice);
if (hasFixedSizeKey)
return new FixedSizeKeyDiskSegment<TKey, TValue>(segmentId, options, dataHeaderDevice, dataDevice);
if (hasFixedSizeValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ namespace Tenray.ZoneTree.Segments.DiskSegmentVariations;

public sealed class FixedSizeKeyAndValueDiskSegment<TKey, TValue> : DiskSegment<TKey, TValue>
{
public override int ReadBufferCount =>
(DataDevice?.ReadBufferCount ?? 0);

public unsafe FixedSizeKeyAndValueDiskSegment(
long segmentId,
ZoneTreeOptions<TKey, TValue> options) : base(segmentId, options)
Expand All @@ -31,8 +34,7 @@ public sealed class FixedSizeKeyAndValueDiskSegment<TKey, TValue> : DiskSegment<

public unsafe FixedSizeKeyAndValueDiskSegment(long segmentId,
ZoneTreeOptions<TKey, TValue> options,
IRandomAccessDevice dataHeaderDevice,
IRandomAccessDevice dataDevice) : base(segmentId, options, dataHeaderDevice, dataDevice)
IRandomAccessDevice dataDevice) : base(segmentId, options, dataDevice)
{
EnsureKeyAndValueTypesAreSupported();
InitKeyAndValueSizeAndDataLength();
Expand Down Expand Up @@ -92,4 +94,19 @@ protected override unsafe TValue ReadValue(long index)
Interlocked.Decrement(ref ReadCount);
}
}

protected override void DeleteDevices()
{
DataDevice?.Delete();
}

public override void ReleaseResources()
{
DataDevice?.Dispose();
}

public override int ReleaseReadBuffers(long ticks)
{
return DataDevice?.ReleaseReadBuffers(ticks) ?? 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ namespace Tenray.ZoneTree.Segments.DiskSegmentVariations;

public sealed class FixedSizeKeyDiskSegment<TKey, TValue> : DiskSegment<TKey, TValue>
{
readonly IRandomAccessDevice DataHeaderDevice;

public override int ReadBufferCount =>
(DataDevice?.ReadBufferCount ?? 0) + (DataHeaderDevice?.ReadBufferCount ?? 0);

public unsafe FixedSizeKeyDiskSegment(
long segmentId,
ZoneTreeOptions<TKey, TValue> options) : base(segmentId, options)
Expand Down Expand Up @@ -46,8 +51,9 @@ public sealed class FixedSizeKeyDiskSegment<TKey, TValue> : DiskSegment<TKey, TV
public unsafe FixedSizeKeyDiskSegment(long segmentId,
ZoneTreeOptions<TKey, TValue> options,
IRandomAccessDevice dataHeaderDevice,
IRandomAccessDevice dataDevice) : base(segmentId, options, dataHeaderDevice, dataDevice)
IRandomAccessDevice dataDevice) : base(segmentId, options, dataDevice)
{
DataHeaderDevice = dataHeaderDevice;
EnsureKeyAndValueTypesAreSupported();
InitKeySizeAndDataLength();
}
Expand Down Expand Up @@ -109,4 +115,23 @@ protected override unsafe TValue ReadValue(long index)
Interlocked.Decrement(ref ReadCount);
}
}

protected override void DeleteDevices()
{
DataHeaderDevice?.Delete();
DataDevice?.Delete();
}

public override void ReleaseResources()
{
DataHeaderDevice?.Dispose();
DataDevice?.Dispose();
}

public override int ReleaseReadBuffers(long ticks)
{
var a = DataHeaderDevice?.ReleaseReadBuffers(ticks) ?? 0;
var b = DataDevice?.ReleaseReadBuffers(ticks) ?? 0;
return a + b;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ namespace Tenray.ZoneTree.Segments.DiskSegmentVariations;

public sealed class FixedSizeValueDiskSegment<TKey, TValue> : DiskSegment<TKey, TValue>
{
readonly IRandomAccessDevice DataHeaderDevice;

public override int ReadBufferCount =>
(DataDevice?.ReadBufferCount ?? 0) + (DataHeaderDevice?.ReadBufferCount ?? 0);

public unsafe FixedSizeValueDiskSegment(
long segmentId,
ZoneTreeOptions<TKey, TValue> options) : base(segmentId, options)
Expand Down Expand Up @@ -44,8 +49,9 @@ public sealed class FixedSizeValueDiskSegment<TKey, TValue> : DiskSegment<TKey,
public unsafe FixedSizeValueDiskSegment(long segmentId,
ZoneTreeOptions<TKey, TValue> options,
IRandomAccessDevice dataHeaderDevice,
IRandomAccessDevice dataDevice) : base(segmentId, options, dataHeaderDevice, dataDevice)
IRandomAccessDevice dataDevice) : base(segmentId, options, dataDevice)
{
DataHeaderDevice = dataHeaderDevice;
EnsureKeyAndValueTypesAreSupported();
InitKeySizeAndDataLength();
}
Expand Down Expand Up @@ -107,4 +113,23 @@ protected override unsafe TValue ReadValue(long index)
Interlocked.Decrement(ref ReadCount);
}
}

protected override void DeleteDevices()
{
DataHeaderDevice?.Delete();
DataDevice?.Delete();
}

public override void ReleaseResources()
{
DataHeaderDevice?.Dispose();
DataDevice?.Dispose();
}

public override int ReleaseReadBuffers(long ticks)
{
var a = DataHeaderDevice?.ReleaseReadBuffers(ticks) ?? 0;
var b = DataDevice?.ReleaseReadBuffers(ticks) ?? 0;
return a + b;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ namespace Tenray.ZoneTree.Segments.DiskSegmentVariations;

public sealed class VariableSizeDiskSegment<TKey, TValue> : DiskSegment<TKey, TValue>
{
readonly IRandomAccessDevice DataHeaderDevice;

public override int ReadBufferCount =>
(DataDevice?.ReadBufferCount ?? 0) + (DataHeaderDevice?.ReadBufferCount ?? 0);

public unsafe VariableSizeDiskSegment(
long segmentId,
ZoneTreeOptions<TKey, TValue> options) : base(segmentId, options)
Expand Down Expand Up @@ -44,8 +49,9 @@ public sealed class VariableSizeDiskSegment<TKey, TValue> : DiskSegment<TKey, TV
public unsafe VariableSizeDiskSegment(long segmentId,
ZoneTreeOptions<TKey, TValue> options,
IRandomAccessDevice dataHeaderDevice,
IRandomAccessDevice dataDevice) : base(segmentId, options, dataHeaderDevice, dataDevice)
IRandomAccessDevice dataDevice) : base(segmentId, options, dataDevice)
{
DataHeaderDevice = dataHeaderDevice;
EnsureKeyAndValueTypesAreSupported();
InitDataLength();
}
Expand Down Expand Up @@ -104,4 +110,23 @@ protected override unsafe TValue ReadValue(long index)
Interlocked.Decrement(ref ReadCount);
}
}

protected override void DeleteDevices()
{
DataHeaderDevice?.Delete();
DataDevice?.Delete();
}

public override void ReleaseResources()
{
DataHeaderDevice?.Dispose();
DataDevice?.Dispose();
}

public override int ReleaseReadBuffers(long ticks)
{
var a = DataHeaderDevice?.ReleaseReadBuffers(ticks) ?? 0;
var b = DataDevice?.ReleaseReadBuffers(ticks) ?? 0;
return a + b;
}
}

0 comments on commit d32418a

Please sign in to comment.