Auxiliary data storage in client backend (#849)

* Auxiliary data storage in client backend

* Runtime error handling
This commit is contained in:
Arkadiy Paronyan
2018-10-01 01:10:45 +02:00
committed by Gav Wood
parent 2e728005c9
commit 04cf0072ba
5 changed files with 60 additions and 2 deletions
+29
View File
@@ -122,6 +122,7 @@ mod columns {
pub const BODY: Option<u32> = Some(5);
pub const JUSTIFICATION: Option<u32> = Some(6);
pub const CHANGES_TRIE: Option<u32> = Some(7);
pub const AUX: Option<u32> = Some(8);
}
struct PendingBlock<Block: BlockT> {
@@ -739,6 +740,24 @@ impl<Block> client::backend::Backend<Block, Blake2Hasher> for Backend<Block> whe
_ => Err(client::error::ErrorKind::UnknownBlock(format!("{:?}", block)).into()),
}
}
fn insert_aux<'a, 'b: 'a, 'c: 'a, I: IntoIterator<Item=&'a (&'c [u8], &'c [u8])>, D: IntoIterator<Item=&'a &'b [u8]>>
(&self, insert: I, delete: D) -> Result<(), client::error::Error>
{
let mut transaction = DBTransaction::new();
for (k, v) in insert {
transaction.put(columns::AUX, k, v);
}
for k in delete {
transaction.delete(columns::AUX, k);
}
self.storage.db.write(transaction).map_err(db_err)?;
Ok(())
}
fn get_aux(&self, key: &[u8]) -> Result<Option<Vec<u8>>, client::error::Error> {
Ok(self.storage.db.get(columns::AUX, key).map(|r| r.map(|v| v.to_vec())).map_err(db_err)?)
}
}
impl<Block> client::backend::LocalBackend<Block, Blake2Hasher> for Backend<Block>
@@ -1144,4 +1163,14 @@ mod tests {
let backend: Arc<Backend<test_client::runtime::Block>> = Arc::new(Backend::new_test(20, 20));
test_client::trait_tests::test_blockchain_query_by_number_gets_canonical(backend);
}
#[test]
fn test_aux() {
let backend: Backend<test_client::runtime::Block> = Backend::new_test(0, 0);
assert!(backend.get_aux(b"test").unwrap().is_none());
backend.insert_aux(&[(&b"test"[..], &b"hello"[..])], &[]).unwrap();
assert_eq!(b"hello", &backend.get_aux(b"test").unwrap().unwrap()[..]);
backend.insert_aux(&[], &[&b"test"[..]]).unwrap();
assert!(backend.get_aux(b"test").unwrap().is_none());
}
}
+1 -1
View File
@@ -32,7 +32,7 @@ use DatabaseSettings;
/// Number of columns in the db. Must be the same for both full && light dbs.
/// Otherwise RocksDb will fail to open database && check its type.
pub const NUM_COLUMNS: u32 = 8;
pub const NUM_COLUMNS: u32 = 9;
/// Meta column. The set of keys in the column is shared by full && light storages.
pub const COLUMN_META: Option<u32> = Some(0);
+4
View File
@@ -119,6 +119,10 @@ where
/// Attempts to revert the chain by `n` blocks. Returns the number of blocks that were
/// successfully reverted.
fn revert(&self, n: NumberFor<Block>) -> error::Result<NumberFor<Block>>;
/// Insert auxiliary data into key-value store.
fn insert_aux<'a, 'b: 'a, 'c: 'a, I: IntoIterator<Item=&'a(&'c [u8], &'c [u8])>, D: IntoIterator<Item=&'a &'b [u8]>>(&self, insert: I, delete: D) -> error::Result<()>;
/// Query auxiliary data from key-value store.
fn get_aux(&self, key: &[u8]) -> error::Result<Option<Vec<u8>>>;
}
/// Mark for all Backend implementations, that are making use of state data, stored locally.
+17
View File
@@ -417,6 +417,7 @@ where
states: RwLock<HashMap<Block::Hash, InMemory<H>>>,
changes_trie_storage: InMemoryChangesTrieStorage<H>,
blockchain: Blockchain<Block>,
aux: RwLock<HashMap<Vec<u8>, Vec<u8>>>,
}
impl<Block, H> Backend<Block, H>
@@ -432,6 +433,7 @@ where
states: RwLock::new(HashMap::new()),
changes_trie_storage: InMemoryChangesTrieStorage::new(),
blockchain: Blockchain::new(),
aux: RwLock::new(HashMap::new()),
}
}
}
@@ -514,6 +516,21 @@ where
fn revert(&self, _n: NumberFor<Block>) -> error::Result<NumberFor<Block>> {
Ok(As::sa(0))
}
fn insert_aux<'a, 'b: 'a, 'c: 'a, I: IntoIterator<Item=&'a (&'c [u8], &'c [u8])>, D: IntoIterator<Item=&'a &'b [u8]>>(&self, insert: I, delete: D) -> error::Result<()> {
let mut aux = self.aux.write();
for (k, v) in insert {
aux.insert(k.to_vec(), v.to_vec());
}
for k in delete {
aux.remove(*k);
}
Ok(())
}
fn get_aux(&self, key: &[u8]) -> error::Result<Option<Vec<u8>>> {
Ok(self.aux.read().get(key).cloned())
}
}
impl<Block, H> backend::LocalBackend<Block, H> for Backend<Block, H>
+9 -1
View File
@@ -126,7 +126,15 @@ impl<S, F, Block, H> ClientBackend<Block, H> for Backend<S, F> where
}
fn revert(&self, _n: NumberFor<Block>) -> ClientResult<NumberFor<Block>> {
unimplemented!()
Err(ClientErrorKind::NotAvailableOnLightClient.into())
}
fn insert_aux<'a, 'b: 'a, 'c: 'a, I: IntoIterator<Item=&'a (&'c [u8], &'c [u8])>, D: IntoIterator<Item=&'a &'b [u8]>>(&self, _insert: I, _delete: D) -> ClientResult<()> {
Err(ClientErrorKind::NotAvailableOnLightClient.into())
}
fn get_aux(&self, _key: &[u8]) -> ClientResult<Option<Vec<u8>>> {
Err(ClientErrorKind::NotAvailableOnLightClient.into())
}
}