Repository navigation
fix(repo): Keep the parent entries when Tree::add splits a subtree - #351
Open
TomPlanche wants to merge 1 commit into
Open
TomPlanche wants to merge 1 commit into
TomPlanche wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #343.
Problem
mst::Tree::addcan remove keys that are already in the tree. After that,Tree::getreturnsNonefor those keys, andTree::keysdoes not list them. Onmain, the reproduction from the issue loses a key in 72 of 300 key sequences.Cause
When
addputs a key in a layer above a subtree,split_subtreesplits that subtree into a left part and a right part.split_subtreefirst splits the lowest node on the path of the key. Then it walks back up the path and splits each parent.A parent kept its entries on one side only if the child also had entries on that side. If all the entries of the child were on one side of the key, the old code dropped the entries of the parent on the other side.
The smallest case has 3 keys, with D < F < G:
[D, subtree(G)].split_subtreesplits the root around F.[G]goes completely to the right of F, so the left part of this node isNone.The diagram shows the tree before the insertion of F. Then it shows the result on
mainand the result with the fix.flowchart LR subgraph s1["Before"] direction TB a1["Root, layer 1<br/>key: D"] a2["Layer 0<br/>key: G"] a1 -->|"keys after D"| a2 end subgraph s2["On main: D is lost"] direction TB b1["Root, layer 2<br/>key: F"] b2["Layer 1, not written<br/>key: D"] b3["Layer 1<br/>no key"] b4["Layer 0<br/>key: G"] b1 -.->|"keys before F: no link"| b2 b1 -->|"keys after F"| b3 b3 --> b4 end subgraph s3["With the fix: D stays"] direction TB c1["Root, layer 2<br/>key: F"] c2["Layer 1<br/>key: D"] c3["Layer 1<br/>no key"] c4["Layer 0<br/>key: G"] c1 -->|"keys before F"| c2 c1 -->|"keys after F"| c3 c3 --> c4 end s1 ~~~ s2 ~~~ s3 classDef added fill:#dafbe1,stroke:#1a7f37,color:#1f2328 classDef lost fill:#ffebe9,stroke:#cf222e,color:#cf222e,stroke-dasharray:5 5 class b1,c1 added class b2 lost linkStyle 1 stroke:#cf222eThe mirror case loses a key on the right side. Add E (layer 0) and I (layer 1), then add F (layer 2), with E < F < I. The node
[E]goes completely to the left of F. The old code then made no right part for the root, and I was lost.flowchart LR subgraph s1["Before"] direction TB a1["Root, layer 1<br/>key: I"] a2["Layer 0<br/>key: E"] a1 -->|"keys before I"| a2 end subgraph s2["On main: I is lost"] direction TB b1["Root, layer 2<br/>key: F"] b2["Layer 1<br/>no key"] b3["Layer 1, not written<br/>key: I"] b4["Layer 0<br/>key: E"] b1 -->|"keys before F"| b2 b1 -.->|"keys after F: no link"| b3 b2 --> b4 end subgraph s3["With the fix: I stays"] direction TB c1["Root, layer 2<br/>key: F"] c2["Layer 1<br/>no key"] c3["Layer 1<br/>key: I"] c4["Layer 0<br/>key: E"] c1 -->|"keys before F"| c2 c1 -->|"keys after F"| c3 c2 --> c4 end s1 ~~~ s2 ~~~ s3 classDef added fill:#dafbe1,stroke:#1a7f37,color:#1f2328 classDef lost fill:#ffebe9,stroke:#cf222e,color:#cf222e,stroke-dasharray:5 5 class b1,c1 added class b3 lost linkStyle 2 stroke:#cf222eKeys that arrive in sorted order, such as TIDs, do not cause this bug. The new key is always the largest key, so each parent has no entries on the right side. This is why the existing tests did not find the bug. Keys that arrive in a random order, such as CIDs, cause the bug after a small number of insertions.
Fix
Each side of a parent now always keeps the entries of that parent.
split_subtreeadds the link to the child part only if that part exists. A side becomesNoneonly if it has no entries at all.The change is in the loop of
split_subtreethat walks back up the path. The public API does not change.Tests
mst_split_keeps_left_entriesandmst_split_keeps_right_entriesare the two cases with 3 keys, one for each side. Each test also makes sure that a different insertion order gives the same root CID.mst_add_unordered_keysadds 20 CID-shaped keys for each of 50 salts. These are the same keys as in the reproduction from the issue. After eachadd, the test reads all the earlier keys. Then the test compares the root CID with the root CID of a tree made from the sorted keys.main, the 3 new tests fail.mst_add_unordered_keysfails onsalt-2after 18 insertions, which is the case from the issue.cargo test -p atrium-repo: 23 unit tests and 1 doc test pass.cargo clippy -p atrium-repo --all-targets --no-deps -- -D warningsandcargo fmt-no-gen --checkare clean.