Skip to content

Commit

Permalink
Add metadata impl for chaindb mock
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Jul 6, 2022
1 parent e6d4c87 commit 1ee920b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dan_layer/core/src/storage/chain/metadata_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::fmt::{Display, Formatter};

use crate::storage::metadata_backend_adapter::AsKeyBytes;

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChainDbMetadataKey {
CheckpointNumber,
}
Expand Down
2 changes: 1 addition & 1 deletion dan_layer/core/src/storage/metadata_backend_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// TODO: probably want to use something like bors or consensus encoding
use tari_utilities::message_format::MessageFormat;

use crate::storage::StorageError;
use crate::storage::AtomicDb;

pub trait MetadataBackendAdapter<K: AsKeyBytes>: AtomicDb + Send + Sync + Clone {
fn get_metadata<T: MessageFormat>(
Expand Down
27 changes: 22 additions & 5 deletions dan_layer/core/src/storage/mocks/chain_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,26 +208,43 @@ impl MetadataBackendAdapter<ChainDbMetadataKey> for MockChainDbBackupAdapter {
fn get_metadata<T: MessageFormat>(
&self,
key: &ChainDbMetadataKey,
transaction: &Self::DbTransaction,
_transaction: &Self::DbTransaction,
) -> Result<Option<T>, Self::Error> {
let lock = self.db.read()?;
Ok(lock.metadata.rows().find(|(k, _)| k == key))
let v = lock
.metadata
.rows()
.find(|(k, _)| k == key)
.map(|(_, v)| v)
.map(|v| T::from_binary(v).unwrap());
Ok(v)
}

fn set_metadata<T: MessageFormat>(
&self,
key: ChainDbMetadataKey,
value: T,
transaction: &Self::DbTransaction,
_transaction: &Self::DbTransaction,
) -> Result<(), Self::Error> {
let mut lock = self.db.write()?;
Ok(lock.metadata.rows().find(|(k, _)| k == key))
let value = value.to_binary().unwrap();
let id = lock.metadata.records().find(|(_, (k, _))| *k == key).map(|(id, _)| id);
match id {
Some(id) => {
lock.metadata.update(id, (key, value));
},
None => {
lock.metadata.insert((key, value));
},
}

Ok(())
}

fn metadata_key_exists(
&self,
_key: &ChainDbMetadataKey,
transaction: &Self::DbTransaction,
_transaction: &Self::DbTransaction,
) -> Result<bool, Self::Error> {
todo!()
}
Expand Down

0 comments on commit 1ee920b

Please sign in to comment.