Skip to content

Commit

Permalink
Finished rebasing
Browse files Browse the repository at this point in the history
  • Loading branch information
batconjurer authored and brentstone committed Mar 8, 2024
1 parent 0f701a0 commit c215db7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 180 deletions.
120 changes: 0 additions & 120 deletions crates/apps/src/lib/node/ledger/storage/rocksdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1590,126 +1590,6 @@ impl DB for RocksDB {

Ok(())
}

Check warning on line 1592 in crates/apps/src/lib/node/ledger/storage/rocksdb.rs

View check run for this annotation

Codecov / codecov/patch

crates/apps/src/lib/node/ledger/storage/rocksdb.rs#L1583-L1592

Added lines #L1583 - L1592 were not covered by tests

#[inline]
fn overwrite_entry(
&self,
batch: &mut Self::WriteBatch,
height: Option<BlockHeight>,
key: &Key,
new_value: impl AsRef<[u8]>,
) -> Result<()> {
let last_height: BlockHeight = {
let state_cf = self.get_column_family(STATE_CF)?;

decode(
self.0
.get_cf(state_cf, "height")
.map_err(|e| Error::DBError(e.to_string()))?
.ok_or_else(|| {
Error::DBError("No block height found".to_string())
})?,
)
.map_err(|e| {
Error::DBError(format!("Unable to decode block height: {e}"))
})?
};
let desired_height = height.unwrap_or(last_height);

if desired_height != last_height {
todo!(
"Overwriting values at heights different than the last \
committed height hast yet to be implemented"
);
}
// NB: the following code only updates values
// written to at the last committed height

let val = new_value.as_ref();

// update subspace value
let subspace_cf = self.get_column_family(SUBSPACE_CF)?;
let subspace_key = key.to_string();

batch.0.put_cf(subspace_cf, subspace_key, val);

// update value stored in diffs
let diffs_cf = self.get_column_family(DIFFS_CF)?;
let diffs_key = Key::from(last_height.to_db_key())
.with_segment("new".to_owned())
.join(key)
.to_string();

batch.0.put_cf(diffs_cf, diffs_key, val);

Ok(())
}
}

/// A struct that can visit a set of updates,
/// registering them all in the batch
pub struct RocksDBUpdateVisitor<'db> {
db: &'db RocksDB,
batch: RocksDBWriteBatch,
}

impl<'db> RocksDBUpdateVisitor<'db> {
pub fn new(db: &'db RocksDB) -> Self {
Self {
db,
batch: Default::default(),
}
}

pub fn take_batch(self) -> RocksDBWriteBatch {
self.batch
}
}

impl<'db> DBUpdateVisitor for RocksDBUpdateVisitor<'db> {
fn read(&self, key: &Key) -> Option<Vec<u8>> {
self.db
.read_subspace_val(key)
.expect("Failed to read from storage")
}

fn write(&mut self, key: &Key, value: impl AsRef<[u8]>) {
self.db
.overwrite_entry(&mut self.batch, None, key, value)
.expect("Failed to overwrite a key in storage")
}

fn delete(&mut self, key: &Key) {
let last_height: BlockHeight = {
let state_cf = self.db.get_column_family(STATE_CF).unwrap();

decode(
self.db
.0
.get_cf(state_cf, "height")
.map_err(|e| Error::DBError(e.to_string()))
.unwrap()
.ok_or_else(|| {
Error::DBError("No block height found".to_string())
})
.unwrap(),
)
.map_err(|e| {
Error::DBError(format!("Unable to decode block height: {e}"))
})
.unwrap()
};
self.db
.batch_delete_subspace_val(&mut self.batch, last_height, key, true)
.expect("Failed to delete key from storage");
}

fn get_pattern(&self, pattern: Regex) -> Vec<(String, Vec<u8>)> {
self.db
.iter_pattern(None, pattern)
.map(|(k, v, _)| (k, v))
.collect()
}
}

/// A struct that can visit a set of updates,
Expand Down
2 changes: 1 addition & 1 deletion crates/migrations/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ version.workspace = true
[dependencies]
data-encoding.workspace = true
lazy_static.workspace = true
linkme = {workspace = true, optional = true}
linkme.workspace = true
serde.workspace = true
Loading

0 comments on commit c215db7

Please sign in to comment.