FileBackend for Merkle{Reader,Writer}#498
FileBackend for Merkle{Reader,Writer}#498malcolmgreaves wants to merge 1 commit intomg/merkle_interfacefrom
Conversation
Implements the current custom file format based Merkle tree node storage and retrieval logic for the `MerkleReader` and `MerkleWriter` traits as the new `FileBackend` type. Also refactors `MerkleNodeDB::to_node`'s logic into a new function on the enum `EMerkleTreeNode::from_type_and_bytes`.
📝 WalkthroughSummary by CodeRabbit
WalkthroughThis PR introduces a new file-based Merkle node storage backend ( Changes
Sequence DiagramssequenceDiagram
participant Client
participant FileBackend
participant FileWriteSession
participant FileNodeSession
participant MerkleNodeDB
Client->>FileBackend: begin()
FileBackend->>FileWriteSession: new()
FileWriteSession-->>Client: FileWriteSession
Client->>FileWriteSession: node_session(node_id)
FileWriteSession->>MerkleNodeDB: open_read_write()
MerkleNodeDB-->>FileNodeSession: session
FileNodeSession-->>Client: FileNodeSession
Client->>FileNodeSession: add_child()
FileNodeSession->>MerkleNodeDB: (delegates)
Client->>FileNodeSession: finish()
FileNodeSession->>MerkleNodeDB: close()
note over FileNodeSession: finish marked, Drop ensures<br/>cleanup if not explicit
Client->>FileWriteSession: finish()
FileWriteSession-->>Client: Ok(())
sequenceDiagram
participant Client
participant FileBackend
participant MerkleNodeDB
Client->>FileBackend: exists(hash)
FileBackend->>MerkleNodeDB: hash_exists()
MerkleNodeDB-->>FileBackend: bool or false
FileBackend-->>Client: Ok(bool)
Client->>FileBackend: get_node(hash)
FileBackend->>MerkleNodeDB: get_node()
MerkleNodeDB-->>FileBackend: MerkleNodeRecord or None
FileBackend-->>Client: Ok(Some/None)
Client->>FileBackend: get_children(hash)
FileBackend->>MerkleNodeDB: get_children()
MerkleNodeDB-->>FileBackend: Vec<(hash, node)> or empty
FileBackend-->>Client: Ok(Vec)
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Review rate limit: 4/5 reviews remaining, refill in 12 minutes. Comment |
|
STACKED PR: Do not merge until #472 has been merged. |
|
@coderabbitai please re-review this PR |
|
🧠 Learnings used✅ Actions performedFull review triggered. |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
crates/lib/src/core/db/merkle_node/file_backend.rs (1)
56-67: 💤 Low valueDocstring doesn't mention error behavior for missing hashes.
Unlike
get_nodewhich explicitly handles missing hashes by returningOk(None),get_childrenwill propagate an error fromopen_read_onlyif the hash doesn't exist. The docstring should clarify this to match the actual behavior.📝 Suggested docstring update
/// Retrieves the children of the node with the given `hash` from the store. - /// An empty vec means that either the node is a not a directory or virtual node or it is one - /// but has no files. + /// An empty vec means that either the node is not a directory or virtual node, or it is one + /// but has no files. Returns an error if the node does not exist; callers should use + /// [`exists`] first if they need to distinguish missing nodes from other errors. /// /// Alias for [`MerkleNodeDB::open_read_only`] & a `.map()` call..🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@crates/lib/src/core/db/merkle_node/file_backend.rs` around lines 56 - 67, The docstring for get_children is missing the error behavior when the provided hash is absent; update the comment for fn get_children to explicitly state that, unlike get_node which returns Ok(None) for missing hashes, get_children calls MerkleNodeDB::open_read_only and will propagate an Err (MerkleDbError) if the hash does not exist or cannot be opened, and document that an empty Vec still indicates either a non-directory/virtual node or a directory with no files.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@crates/lib/src/core/db/merkle_node/file_backend.rs`:
- Around line 56-67: The docstring for get_children is missing the error
behavior when the provided hash is absent; update the comment for fn
get_children to explicitly state that, unlike get_node which returns Ok(None)
for missing hashes, get_children calls MerkleNodeDB::open_read_only and will
propagate an Err (MerkleDbError) if the hash does not exist or cannot be opened,
and document that an empty Vec still indicates either a non-directory/virtual
node or a directory with no files.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 869f49f4-0b86-4fa7-9424-5551a5ce0742
📒 Files selected for processing (4)
crates/lib/src/core/db/merkle_node.rscrates/lib/src/core/db/merkle_node/file_backend.rscrates/lib/src/core/db/merkle_node/merkle_node_db.rscrates/lib/src/model/merkle_tree/node.rs
Implements the current custom file format based Merkle tree node storage
and retrieval logic for the
MerkleReaderandMerkleWritertraits asthe new
FileBackendtype.Also refactors
MerkleNodeDB::to_node's logic into a new function on theenum
EMerkleTreeNode::from_type_and_bytes.