fc_db/kv/
parity_db_adapter.rs

1// This file is part of Frontier.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19// Substrate
20use 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}