Skip to content

Commit

Permalink
Implement reparenting (depends on 701) (#706)
Browse files Browse the repository at this point in the history
Signed-off-by: Ron Kuris <[email protected]>
  • Loading branch information
rkuris authored Aug 22, 2024
1 parent f4e341c commit f139784
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 47 deletions.
8 changes: 4 additions & 4 deletions firewood/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ where
}
}
let nodestore = merkle.into_inner();
let immutable: Arc<NodeStore<ImmutableProposal, FileBacked>> = Arc::new(nodestore.into());
let immutable: Arc<NodeStore<Arc<ImmutableProposal>, FileBacked>> =
Arc::new(nodestore.into());
self.manager
.write()
.expect("poisoned lock")
Expand Down Expand Up @@ -312,13 +313,13 @@ impl Db {

#[derive(Debug)]
pub struct Proposal<'p> {
nodestore: Arc<NodeStore<ImmutableProposal, FileBacked>>,
nodestore: Arc<NodeStore<Arc<ImmutableProposal>, FileBacked>>,
db: &'p Db,
}

#[async_trait]
impl<'a> api::DbView for Proposal<'a> {
type Stream<'b> = MerkleKeyValueStream<'b, NodeStore<ImmutableProposal, FileBacked>> where Self: 'b;
type Stream<'b> = MerkleKeyValueStream<'b, NodeStore<Arc<ImmutableProposal>, FileBacked>> where Self: 'b;

async fn root_hash(&self) -> Result<Option<api::HashKey>, api::Error> {
todo!()
Expand Down Expand Up @@ -363,7 +364,6 @@ impl<'a> api::Proposal for Proposal<'a> {
todo!()
}

// When committing a proposal, refuse to commit if there are any cloned proposals.
async fn commit(self: Arc<Self>) -> Result<(), api::Error> {
match Arc::into_inner(self) {
Some(proposal) => {
Expand Down
24 changes: 12 additions & 12 deletions firewood/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct RevisionManagerConfig {
}

type CommittedRevision = Arc<NodeStore<Committed, FileBacked>>;
type ProposedRevision = Arc<NodeStore<ImmutableProposal, FileBacked>>;
type ProposedRevision = Arc<NodeStore<Arc<ImmutableProposal>, FileBacked>>;

#[derive(Debug)]
pub(crate) struct RevisionManager {
Expand Down Expand Up @@ -108,7 +108,7 @@ impl RevisionManager {
let current_revision = self.current_revision();
if !proposal
.kind
.parent_is(&current_revision.kind.as_nodestore_parent())
.parent_hash_is(current_revision.kind.root_hash())
{
return Err(RevisionManagerError::NotLatest);
}
Expand All @@ -133,16 +133,16 @@ impl RevisionManager {
proposal.flush_header()?;

// 7. Proposal Cleanup
self.proposals.retain(|p| {
// TODO: reparent proposals; this needs a lock on the parent element of immutable proposals
// if p
// .kind
// .parent_is(&proposal.kind.as_nodestore_parent())
// {
// p.kind.reparent_to(&committed.kind.as_nodestore_parent());
// }
!Arc::ptr_eq(&proposal, p)
});
// first remove the committing proposal from the list of outstanding proposals
self.proposals.retain(|p| !Arc::ptr_eq(&proposal, p));

// then reparent any proposals that have this proposal as a parent
for p in self.proposals.iter() {
proposal.commit_reparent(p);
}

// 8. Revision reaping
// TODO

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions firewood/src/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ impl<T: HashedNodeReader> Merkle<T> {
}

impl<S: ReadableStorage> From<Merkle<NodeStore<MutableProposal, S>>>
for Merkle<NodeStore<ImmutableProposal, S>>
for Merkle<NodeStore<Arc<ImmutableProposal>, S>>
{
fn from(m: Merkle<NodeStore<MutableProposal, S>>) -> Self {
Merkle {
Expand All @@ -395,7 +395,7 @@ impl<S: ReadableStorage> From<Merkle<NodeStore<MutableProposal, S>>>
}

impl<S: ReadableStorage> Merkle<NodeStore<MutableProposal, S>> {
pub fn hash(self) -> Merkle<NodeStore<ImmutableProposal, S>> {
pub fn hash(self) -> Merkle<NodeStore<Arc<ImmutableProposal>, S>> {
self.into()
}

Expand Down
1 change: 1 addition & 0 deletions storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ serde = { version = "1.0.199", features = ["derive"] }
smallvec = { version = "1.13.2", features = ["serde", "write", "union"] }
sha2 = "0.10.8"
integer-encoding = "4.0.0"
arc-swap = "1.7.1"
lru = "0.12.4"

[dev-dependencies]
Expand Down
Loading

0 comments on commit f139784

Please sign in to comment.