// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see .
//! Database trait for polkadot db.
pub use kvdb::{DBKeyValue, DBTransaction, DBValue, KeyValueDB};
/// Database trait with ordered key capacity.
pub trait Database: KeyValueDB {
/// Check if column allows content iteration
/// and removal by prefix.
fn is_indexed_column(&self, col: u32) -> bool;
}
/// Implementation for database supporting `KeyValueDB` already.
pub mod kvdb_impl {
use super::{DBKeyValue, DBTransaction, DBValue, Database, KeyValueDB};
use kvdb::{DBOp, IoStats, IoStatsKind};
use std::{collections::BTreeSet, io::Result};
/// Adapter implementing subsystem database
/// for `KeyValueDB`.
#[derive(Clone)]
pub struct DbAdapter {
db: D,
indexed_columns: BTreeSet,
}
impl DbAdapter {
/// Instantiate new subsystem database, with
/// the columns that allow ordered iteration.
pub fn new(db: D, indexed_columns: &[u32]) -> Self {
DbAdapter { db, indexed_columns: indexed_columns.iter().cloned().collect() }
}
fn ensure_is_indexed(&self, col: u32) {
debug_assert!(
self.is_indexed_column(col),
"Invalid configuration of database, column {} is not ordered.",
col
);
}
fn ensure_ops_indexing(&self, transaction: &DBTransaction) {
debug_assert!({
let mut pass = true;
for op in &transaction.ops {
if let DBOp::DeletePrefix { col, .. } = op {
if !self.is_indexed_column(*col) {
pass = false;
break
}
}
}
pass
})
}
}
impl Database for DbAdapter {
fn is_indexed_column(&self, col: u32) -> bool {
self.indexed_columns.contains(&col)
}
}
impl KeyValueDB for DbAdapter {
fn transaction(&self) -> DBTransaction {
self.db.transaction()
}
fn get(&self, col: u32, key: &[u8]) -> Result