mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-30 01:27:56 +00:00
4a0d6d9490
* Transaction indexing * Tests and fixes * Fixed a comment * Style * Build * Style * Apply suggestions from code review Co-authored-by: cheme <emericchevalier.pro@gmail.com> * Code review suggestions * Add missing impl * Apply suggestions from code review Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * impl JoinInput * Don't store empty slices * JoinInput operates on slices Co-authored-by: cheme <emericchevalier.pro@gmail.com> Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
76 lines
2.5 KiB
Rust
76 lines
2.5 KiB
Rust
// This file is part of Substrate.
|
|
|
|
// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd.
|
|
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
/// A `Database` adapter for parity-db.
|
|
|
|
use sp_database::{Database, Change, ColumnId, Transaction, error::DatabaseError};
|
|
use crate::utils::{DatabaseType, NUM_COLUMNS};
|
|
use crate::columns;
|
|
|
|
struct DbAdapter(parity_db::Db);
|
|
|
|
fn handle_err<T>(result: parity_db::Result<T>) -> T {
|
|
match result {
|
|
Ok(r) => r,
|
|
Err(e) => {
|
|
panic!("Critical database error: {:?}", e);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Wrap parity-db database into a trait object that implements `sp_database::Database`
|
|
pub fn open<H: Clone + AsRef<[u8]>>(path: &std::path::Path, db_type: DatabaseType)
|
|
-> parity_db::Result<std::sync::Arc<dyn Database<H>>>
|
|
{
|
|
let mut config = parity_db::Options::with_columns(path, NUM_COLUMNS as u8);
|
|
config.sync = true; // Flush each commit
|
|
if db_type == DatabaseType::Full {
|
|
let mut state_col = &mut config.columns[columns::STATE as usize];
|
|
state_col.ref_counted = true;
|
|
state_col.preimage = true;
|
|
state_col.uniform = true;
|
|
}
|
|
let db = parity_db::Db::open(&config)?;
|
|
Ok(std::sync::Arc::new(DbAdapter(db)))
|
|
}
|
|
|
|
impl<H: Clone + AsRef<[u8]>> Database<H> for DbAdapter {
|
|
fn commit(&self, transaction: Transaction<H>) -> Result<(), DatabaseError> {
|
|
handle_err(self.0.commit(transaction.0.into_iter().map(|change|
|
|
match change {
|
|
Change::Set(col, key, value) => (col as u8, key, Some(value)),
|
|
Change::Remove(col, key) => (col as u8, key, None),
|
|
_ => unimplemented!(),
|
|
}))
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn get(&self, col: ColumnId, key: &[u8]) -> Option<Vec<u8>> {
|
|
handle_err(self.0.get(col as u8, key))
|
|
}
|
|
|
|
fn contains(&self, col: ColumnId, key: &[u8]) -> bool {
|
|
handle_err(self.0.get_size(col as u8, key)).is_some()
|
|
}
|
|
|
|
fn value_size(&self, col: ColumnId, key: &[u8]) -> Option<usize> {
|
|
handle_err(self.0.get_size(col as u8, key)).map(|s| s as usize)
|
|
}
|
|
}
|