Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/DryDB/BTree/InternalNodeReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ public void GetAt(int index, out ReadOnlySpan<byte> key, out PageNumber childPag
childPageNumber = Unsafe.ReadUnaligned<PageNumber>(ref ptr);
}

public bool TrySearch(
public bool TrySearch<TComparer>(
ReadOnlySpan<byte> key,
IKeyEncoding keyEncoding,
TComparer comparer,
ulong keyDigest,
bool hasKeyDigest,
out PageNumber childPageNumber)
where TComparer : struct, IKeyComparer
{
#if NETSTANDARD
ref var pageReference = ref MemoryMarshal.GetReference(page);
Expand All @@ -78,11 +79,11 @@ public bool TrySearch(
ref Unsafe.Add(ref pageReference, DigestBase + mid * sizeof(ulong)));
cmp = digest != keyDigest
? (digest < keyDigest ? -1 : 1)
: CompareFull(ref pageReference, mid, key, keyEncoding);
: CompareFull(ref pageReference, mid, key, comparer);
}
else
{
cmp = CompareFull(ref pageReference, mid, key, keyEncoding);
cmp = CompareFull(ref pageReference, mid, key, comparer);
}

if (cmp <= 0) // upper bounds
Expand All @@ -105,13 +106,14 @@ ref Unsafe.Add(
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
int CompareFull(ref byte pageReference, int index, ReadOnlySpan<byte> key, IKeyEncoding keyEncoding)
int CompareFull<TComparer>(ref byte pageReference, int index, ReadOnlySpan<byte> key, TComparer comparer)
where TComparer : struct, IKeyComparer
{
var meta = GetMeta(index);
var entryKey = MemoryMarshal.CreateReadOnlySpan(
ref Unsafe.Add(ref pageReference, meta.PageOffset),
meta.KeyLength);
return KeyCompare.Compare(keyEncoding, entryKey, key);
return comparer.Compare(entryKey, key);
}

// for debug purpose
Expand Down
25 changes: 14 additions & 11 deletions src/DryDB/BTree/LeafNodeReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,15 @@ public void GetAt(int index, out int pageOffset, out ushort keyLength, out ushor
valueLength = meta.ValueLength;
}

public bool TryFindValue(
public bool TryFindValue<TComparer>(
scoped ReadOnlySpan<byte> key,
IKeyEncoding keyEncoding,
TComparer comparer,
ulong keyDigest,
bool hasKeyDigest,
out int index,
out int valueOffset,
out ushort valueLength)
where TComparer : struct, IKeyComparer
{
#if NETSTANDARD
ref var pageReference = ref MemoryMarshal.GetReference(page);
Expand All @@ -99,11 +100,11 @@ public bool TryFindValue(
ref Unsafe.Add(ref pageReference, DigestBase + midIndex * sizeof(ulong)));
compared = digest != keyDigest
? (digest < keyDigest ? -1 : 1)
: CompareFull(ref pageReference, midIndex, key, keyEncoding);
: CompareFull(ref pageReference, midIndex, key, comparer);
}
else
{
compared = CompareFull(ref pageReference, midIndex, key, keyEncoding);
compared = CompareFull(ref pageReference, midIndex, key, comparer);
}

if (compared == 0)
Expand All @@ -130,13 +131,14 @@ public bool TryFindValue(
return false;
}

public bool TrySearch(
public bool TrySearch<TComparer>(
ReadOnlySpan<byte> key,
SearchOperator op,
IKeyEncoding keyEncoding,
TComparer comparer,
ulong keyDigest,
bool hasKeyDigest,
out int index)
where TComparer : struct, IKeyComparer
{
#if NETSTANDARD
ref var pageReference = ref MemoryMarshal.GetReference(page);
Expand All @@ -158,11 +160,11 @@ public bool TrySearch(
ref Unsafe.Add(ref pageReference, DigestBase + midIndex * sizeof(ulong)));
compared = digest != keyDigest
? (digest < keyDigest ? -1 : 1)
: CompareFull(ref pageReference, midIndex, key, keyEncoding);
: CompareFull(ref pageReference, midIndex, key, comparer);
}
else
{
compared = CompareFull(ref pageReference, midIndex, key, keyEncoding);
compared = CompareFull(ref pageReference, midIndex, key, comparer);
}

switch (op)
Expand Down Expand Up @@ -221,7 +223,7 @@ public bool TrySearch(
case SearchOperator.Equal:
if (min < max)
{
if (CompareFull(ref pageReference, min, key, keyEncoding) == 0)
if (CompareFull(ref pageReference, min, key, comparer) == 0)
{
index = min;
return true;
Expand All @@ -247,13 +249,14 @@ public bool TrySearch(
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
int CompareFull(ref byte pageReference, int index, ReadOnlySpan<byte> key, IKeyEncoding keyEncoding)
int CompareFull<TComparer>(ref byte pageReference, int index, ReadOnlySpan<byte> key, TComparer comparer)
where TComparer : struct, IKeyComparer
{
var meta = GetMeta(index);
var entryKey = MemoryMarshal.CreateReadOnlySpan(
ref Unsafe.Add(ref pageReference, meta.PageOffset),
meta.KeyLength);
return KeyCompare.Compare(keyEncoding, entryKey, key);
return comparer.Compare(entryKey, key);
}

// for debug purpose
Expand Down
Loading
Loading