Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean up vertex delete #2644

Merged
merged 1 commit into from
Sep 20, 2024
Merged
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
34 changes: 17 additions & 17 deletions nimbus/db/aristo/aristo_delete.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ import
# Private heplers
# ------------------------------------------------------------------------------

proc branchStillNeeded(vtx: VertexRef): Result[int,void] =
proc branchStillNeeded(vtx: VertexRef, removed: int): Result[int,void] =
## Returns the nibble if there is only one reference left.
var nibble = -1
for n in 0 .. 15:
if n == removed:
continue

if vtx.bVid[n].isValid:
if 0 <= nibble:
return ok(-1)
Expand Down Expand Up @@ -61,31 +64,23 @@ proc deleteImpl(
# leaves to update
return ok(nil)

# Get current `Branch` vertex `br`
let br = block:
var wp = hike.legs[^2].wp
wp.vtx = wp.vtx.dup # make sure that layers are not impliciteley modified
wp
if br.vtx.vType != Branch:
if hike.legs[^2].wp.vtx.vType != Branch:
return err(DelBranchExpexted)

# Unlink child vertex from structural table
br.vtx.bVid[hike.legs[^2].nibble] = VertexID(0)
db.layersPutVtx((hike.root, br.vid), br.vtx)
# Get current `Branch` vertex `br`
let
br = hike.legs[^2].wp
nbl = br.vtx.branchStillNeeded(hike.legs[^2].nibble).valueOr:
return err(DelBranchWithoutRefs)

# Clear all Merkle hash keys up to the root key
for n in 0 .. hike.legs.len - 2:
let vid = hike.legs[n].wp.vid
db.layersResKey((hike.root, vid))

let nbl = block:
let rc = br.vtx.branchStillNeeded()
if rc.isErr:
return err(DelBranchWithoutRefs)
rc.value

if 0 <= nbl:
# Branch has only one entry - convert it to a leaf or join with parent
# Branch has only one entry - move that entry to where the branch was and
# update its path

# Get child vertex (there must be one after a `Branch` node)
let
Expand Down Expand Up @@ -118,6 +113,11 @@ proc deleteImpl(
else:
ok(nil)
else:
# Clear the removed leaf from the branch (that still contains other children)
let brDup = br.vtx.dup
brDup.bVid[hike.legs[^2].nibble] = VertexID(0)
db.layersPutVtx((hike.root, br.vid), brDup)

ok(nil)

# ------------------------------------------------------------------------------
Expand Down
Loading