From b1b927bb5c09626d48d3cecdba412a3baead0289 Mon Sep 17 00:00:00 2001 From: Shubham Sonthalia Date: Wed, 18 Sep 2024 21:52:05 +0530 Subject: [PATCH 1/4] Initial Changes for confirmation --- src/Paprika/Store/BatchContextBase.cs | 5 ++++- src/Paprika/Store/DataPage.cs | 5 +++++ src/Paprika/Store/IBatchContext.cs | 2 ++ src/Paprika/Store/LeafOverflowPage.cs | 5 +++++ src/Paprika/Store/PagedDb.cs | 2 +- src/Paprika/Store/StateRootPage.cs | 6 ++++++ src/Paprika/Store/StorageFanOut.cs | 6 ++++++ 7 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Paprika/Store/BatchContextBase.cs b/src/Paprika/Store/BatchContextBase.cs index 28968a21..8aeed93a 100644 --- a/src/Paprika/Store/BatchContextBase.cs +++ b/src/Paprika/Store/BatchContextBase.cs @@ -39,7 +39,7 @@ public Page GetWritableCopy(Page page) public bool WasWritten(DbAddress addr) => GetAt(addr).Header.BatchId == BatchId; - public abstract void RegisterForFutureReuse(Page page, bool possibleImmediateReuse = false); + public abstract void RegisterForFutureReuse(Page page, bool possibleImmediateReuse = false); public virtual void NoticeAbandonedPageReused(Page page) { } @@ -71,4 +71,7 @@ public Page TryGetPageAlloc(ref DbAddress addr, PageType pageType) } public BatchStats? Stats { get; } = new(); + + public unsafe Page NullPage => new Page((byte*)0); + } diff --git a/src/Paprika/Store/DataPage.cs b/src/Paprika/Store/DataPage.cs index e73128eb..5ee0f6b0 100644 --- a/src/Paprika/Store/DataPage.cs +++ b/src/Paprika/Store/DataPage.cs @@ -74,6 +74,11 @@ public Page DeleteByPrefix(in NibblePath prefix, IBatchContext batch) buckets[prefix.Nibble0] = batch.GetAddress(child); } } + else + { + batch.RegisterForFutureReuse(page); + return batch.NullPage; + } } else { diff --git a/src/Paprika/Store/IBatchContext.cs b/src/Paprika/Store/IBatchContext.cs index 0ad72127..e13d05d5 100644 --- a/src/Paprika/Store/IBatchContext.cs +++ b/src/Paprika/Store/IBatchContext.cs @@ -72,6 +72,8 @@ Page EnsureWritableCopy(ref DbAddress addr) Page TryGetPageAlloc(ref DbAddress addr, PageType pageType); BatchStats? Stats { get; } + + public unsafe Page NullPage => new Page((byte*)0); } public class BatchStats : IBatchStats diff --git a/src/Paprika/Store/LeafOverflowPage.cs b/src/Paprika/Store/LeafOverflowPage.cs index 5ec5ae1a..a9c5d0e8 100644 --- a/src/Paprika/Store/LeafOverflowPage.cs +++ b/src/Paprika/Store/LeafOverflowPage.cs @@ -47,6 +47,11 @@ public Page DeleteByPrefix(in NibblePath prefix, IBatchContext batch) var writable = batch.GetWritableCopy(page); return new LeafOverflowPage(writable).DeleteByPrefix(prefix, batch); } + if (prefix.IsEmpty) + { + batch.RegisterForFutureReuse(page); + return batch.NullPage; + } Map.DeleteByPrefix(prefix); diff --git a/src/Paprika/Store/PagedDb.cs b/src/Paprika/Store/PagedDb.cs index aa17306e..7f17ae57 100644 --- a/src/Paprika/Store/PagedDb.cs +++ b/src/Paprika/Store/PagedDb.cs @@ -520,7 +520,7 @@ class Batch : BatchContextBase, IBatch private readonly uint _reusePagesOlderThanBatchId; private bool _verify = false; private bool _disposed; - + private readonly Context _ctx; /// diff --git a/src/Paprika/Store/StateRootPage.cs b/src/Paprika/Store/StateRootPage.cs index 86681d90..236b24d8 100644 --- a/src/Paprika/Store/StateRootPage.cs +++ b/src/Paprika/Store/StateRootPage.cs @@ -75,6 +75,12 @@ public Page DeleteByPrefix(in NibblePath prefix, IBatchContext batch) return new StateRootPage(writable).DeleteByPrefix(prefix, batch); } + if (prefix.IsEmpty) + { + batch.RegisterForFutureReuse(page); + return batch.NullPage; + } + Debug.Assert(prefix.Length > ConsumedNibbles, "Removing prefix at the root? Something went wrong."); var index = GetIndex(prefix); diff --git a/src/Paprika/Store/StorageFanOut.cs b/src/Paprika/Store/StorageFanOut.cs index aed117f7..2f23adc5 100644 --- a/src/Paprika/Store/StorageFanOut.cs +++ b/src/Paprika/Store/StorageFanOut.cs @@ -188,6 +188,12 @@ public Page DeleteByPrefix(in NibblePath prefix, IBatchContext batch) var writable = batch.GetWritableCopy(page); return new Level1Page(writable).DeleteByPrefix(prefix, batch); } + if (prefix.IsEmpty) + { + batch.RegisterForFutureReuse(page); + return batch.NullPage; + } + var index = GetIndex(prefix, Type.Storage, out var sliced); From f6a9393f90713b695dc85fa70bc3d63ef37c3cff Mon Sep 17 00:00:00 2001 From: Shubham Sonthalia Date: Wed, 18 Sep 2024 21:57:08 +0530 Subject: [PATCH 2/4] Correction --- src/Paprika/Store/BatchContextBase.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Paprika/Store/BatchContextBase.cs b/src/Paprika/Store/BatchContextBase.cs index 8aeed93a..317164e9 100644 --- a/src/Paprika/Store/BatchContextBase.cs +++ b/src/Paprika/Store/BatchContextBase.cs @@ -71,7 +71,4 @@ public Page TryGetPageAlloc(ref DbAddress addr, PageType pageType) } public BatchStats? Stats { get; } = new(); - - public unsafe Page NullPage => new Page((byte*)0); - } From ea349a6591a002a85c321581804adb7a890f89e9 Mon Sep 17 00:00:00 2001 From: Shubham Sonthalia Date: Wed, 18 Sep 2024 21:58:27 +0530 Subject: [PATCH 3/4] Removing extra spaces and lines --- src/Paprika/Store/BatchContextBase.cs | 2 +- src/Paprika/Store/PagedDb.cs | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Paprika/Store/BatchContextBase.cs b/src/Paprika/Store/BatchContextBase.cs index 317164e9..28968a21 100644 --- a/src/Paprika/Store/BatchContextBase.cs +++ b/src/Paprika/Store/BatchContextBase.cs @@ -39,7 +39,7 @@ public Page GetWritableCopy(Page page) public bool WasWritten(DbAddress addr) => GetAt(addr).Header.BatchId == BatchId; - public abstract void RegisterForFutureReuse(Page page, bool possibleImmediateReuse = false); + public abstract void RegisterForFutureReuse(Page page, bool possibleImmediateReuse = false); public virtual void NoticeAbandonedPageReused(Page page) { } diff --git a/src/Paprika/Store/PagedDb.cs b/src/Paprika/Store/PagedDb.cs index 7f17ae57..b1aee86a 100644 --- a/src/Paprika/Store/PagedDb.cs +++ b/src/Paprika/Store/PagedDb.cs @@ -519,8 +519,7 @@ class Batch : BatchContextBase, IBatch private readonly RootPage _root; private readonly uint _reusePagesOlderThanBatchId; private bool _verify = false; - private bool _disposed; - + private bool _disposed; private readonly Context _ctx; /// From 376d94173bcc948c561995403657396847d3dc26 Mon Sep 17 00:00:00 2001 From: Shubham Sonthalia Date: Wed, 18 Sep 2024 21:59:33 +0530 Subject: [PATCH 4/4] correction #2 --- src/Paprika/Store/PagedDb.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Paprika/Store/PagedDb.cs b/src/Paprika/Store/PagedDb.cs index b1aee86a..e79195c8 100644 --- a/src/Paprika/Store/PagedDb.cs +++ b/src/Paprika/Store/PagedDb.cs @@ -520,6 +520,7 @@ class Batch : BatchContextBase, IBatch private readonly uint _reusePagesOlderThanBatchId; private bool _verify = false; private bool _disposed; + private readonly Context _ctx; ///