Skip to content

Commit

Permalink
actual implementation of detecting the possibility of write down
Browse files Browse the repository at this point in the history
  • Loading branch information
Scooletz committed Jun 28, 2024
1 parent 8d8cc0f commit d540970
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/Paprika/Store/DataPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public Page Set(in NibblePath key, in ReadOnlySpan<byte> data, IBatchContext bat
return page;
}

Page child;

// Special case: if the key can be flushed down and a child if it was written in this batch, go to it directly
if (TryWriteDownToAlreadyWrittenInThisBatch(key, data, batch))
{
return page;
}

// No place in map, try flush to leafs first
TryFlushDownToExistingChildren(map, batch);

Expand All @@ -74,7 +82,6 @@ public Page Set(in NibblePath key, in ReadOnlySpan<byte> data, IBatchContext bat

// Try get the child page
ref var address = ref Data.Buckets[nibble];
Page child;

if (address.IsNull)
{
Expand All @@ -98,6 +105,26 @@ public Page Set(in NibblePath key, in ReadOnlySpan<byte> data, IBatchContext bat
return Set(key, data, batch);
}

private bool TryWriteDownToAlreadyWrittenInThisBatch(in NibblePath key, ReadOnlySpan<byte> data, IBatchContext batch)
{
if (key.IsEmpty)
return false;

var addr = Data.Buckets[key.FirstNibble];
if (addr.IsNull)
return false;

var child = batch.GetAt(addr);
if (batch.WasWritten(child) == false || child.Header.PageType != PageType.Standard)
return false;

// The key has a single nibble, the child page was written in this batch and is a standard page. Let's proceed!
Debug.Assert(child.Header.BatchId == batch.BatchId);
var result = new DataPage(child).Set(key, data, batch);
Debug.Assert(result.Equals(child));
return true;
}

private void TryFlushDownToExistingChildren(in SlottedArray map, IBatchContext batch)
{
var anyChildren = false;
Expand Down

0 comments on commit d540970

Please sign in to comment.