Skip to content

Commit

Permalink
Always return a typed structure from lmdb store (#3312)
Browse files Browse the repository at this point in the history
We have a method get which returns a Vec, it's used in cases when we can't implement FromLmdbBytes or Readable on a desired type, eg when both traits and type are external for the current crate. This approach requires an extra allocation of a Vec, this PR introduces a method which accepts desirialzer as closure, which allows to desrialize in place without an intermidiate heap allocation.

It's still possible to get a Vec if needed, just pass a Vec constructor to get_with.
  • Loading branch information
hashmap authored May 5, 2020
1 parent c82199b commit 2397407
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
10 changes: 5 additions & 5 deletions chain/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,11 @@ impl<'a> Batch<'a> {
}

fn get_legacy_input_bitmap(&self, bh: &Hash) -> Result<Bitmap, Error> {
if let Ok(Some(bytes)) = self.db.get(&to_key(BLOCK_INPUT_BITMAP_PREFIX, bh)) {
Ok(Bitmap::deserialize(&bytes))
} else {
Err(Error::NotFoundErr("legacy block input bitmap".to_string()))
}
option_to_not_found(
self.db
.get_with(&to_key(BLOCK_INPUT_BITMAP_PREFIX, bh), Bitmap::deserialize),
|| "legacy block input bitmap".to_string(),
)
}

/// Get the "spent index" from the db for the specified block.
Expand Down
16 changes: 10 additions & 6 deletions store/src/lmdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,15 @@ impl Store {
}

/// Gets a value from the db, provided its key
pub fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Error> {
pub fn get_with<T, F>(&self, key: &[u8], f: F) -> Result<Option<T>, Error>
where
F: Fn(&[u8]) -> T,
{
let db = self.db.read();
let txn = lmdb::ReadTransaction::new(self.env.clone())?;
let access = txn.access();
let res = access.get(&db.as_ref().unwrap(), key);
res.map(|res: &[u8]| res.to_vec())
.to_opt()
.map_err(From::from)
res.map(f).to_opt().map_err(From::from)
}

/// Gets a `Readable` value from the db, provided its key. Encapsulates
Expand Down Expand Up @@ -345,8 +346,11 @@ impl<'a> Batch<'a> {
}

/// gets a value from the db, provided its key
pub fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Error> {
self.store.get(key)
pub fn get_with<T, F>(&self, key: &[u8], f: F) -> Result<Option<T>, Error>
where
F: Fn(&[u8]) -> T,
{
self.store.get_with(key, f)
}

/// Whether the provided key exists
Expand Down

0 comments on commit 2397407

Please sign in to comment.