fc_db/kv/
parity_db_adapter.rs1use sp_database::{error::DatabaseError, Change, ColumnId, Database, Transaction};
21
22fn handle_err<T>(result: parity_db::Result<T>) -> T {
23 match result {
24 Ok(r) => r,
25 Err(e) => {
26 panic!("Critical database error: {e:?}");
27 }
28 }
29}
30
31pub struct DbAdapter(pub parity_db::Db);
32
33impl<H: Clone + AsRef<[u8]>> Database<H> for DbAdapter {
34 fn commit(&self, transaction: Transaction<H>) -> Result<(), DatabaseError> {
35 handle_err(
36 self.0
37 .commit(transaction.0.into_iter().map(|change| match change {
38 Change::Set(col, key, value) => (col as u8, key, Some(value)),
39 Change::Remove(col, key) => (col as u8, key, None),
40 _ => unimplemented!(),
41 })),
42 );
43
44 Ok(())
45 }
46
47 fn get(&self, col: ColumnId, key: &[u8]) -> Option<Vec<u8>> {
48 handle_err(self.0.get(col as u8, key))
49 }
50
51 fn contains(&self, col: ColumnId, key: &[u8]) -> bool {
52 handle_err(self.0.get_size(col as u8, key)).is_some()
53 }
54
55 fn value_size(&self, col: ColumnId, key: &[u8]) -> Option<usize> {
56 handle_err(self.0.get_size(col as u8, key)).map(|s| s as usize)
57 }
58
59 fn supports_ref_counting(&self) -> bool {
60 true
61 }
62
63 fn sanitize_key(&self, key: &mut Vec<u8>) {
64 let _prefix = key.drain(0..key.len() - super::DB_HASH_LEN);
65 }
66}