mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 11:07:56 +00:00
@@ -0,0 +1,35 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2020 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
/// The error type for database operations.
|
||||
#[derive(Debug)]
|
||||
pub struct DatabaseError(pub Box<dyn std::error::Error + Send>);
|
||||
|
||||
impl std::fmt::Display for DatabaseError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for DatabaseError {
|
||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// A specialized `Result` type for database operations.
|
||||
pub type Result<T> = std::result::Result<T, DatabaseError>;
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
use ::kvdb::{DBTransaction, KeyValueDB};
|
||||
|
||||
use crate::{Database, Change, Transaction, ColumnId};
|
||||
use crate::{Database, Change, ColumnId, Transaction, error};
|
||||
|
||||
struct DbAdapter<D: KeyValueDB + 'static>(D);
|
||||
|
||||
@@ -38,7 +38,7 @@ pub fn as_database<D: KeyValueDB + 'static, H: Clone>(db: D) -> std::sync::Arc<d
|
||||
}
|
||||
|
||||
impl<D: KeyValueDB, H: Clone> Database<H> for DbAdapter<D> {
|
||||
fn commit(&self, transaction: Transaction<H>) {
|
||||
fn commit(&self, transaction: Transaction<H>) -> error::Result<()> {
|
||||
let mut tx = DBTransaction::new();
|
||||
for change in transaction.0.into_iter() {
|
||||
match change {
|
||||
@@ -47,7 +47,7 @@ impl<D: KeyValueDB, H: Clone> Database<H> for DbAdapter<D> {
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
}
|
||||
handle_err(self.0.write(tx));
|
||||
self.0.write(tx).map_err(|e| error::DatabaseError(Box::new(e)))
|
||||
}
|
||||
|
||||
fn get(&self, col: ColumnId, key: &[u8]) -> Option<Vec<u8>> {
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
//! The main database trait, allowing Substrate to store data persistently.
|
||||
|
||||
pub mod error;
|
||||
mod mem;
|
||||
mod kvdb;
|
||||
|
||||
@@ -82,20 +83,22 @@ impl<H> Transaction<H> {
|
||||
pub trait Database<H: Clone>: Send + Sync {
|
||||
/// Commit the `transaction` to the database atomically. Any further calls to `get` or `lookup`
|
||||
/// will reflect the new state.
|
||||
fn commit(&self, transaction: Transaction<H>) {
|
||||
fn commit(&self, transaction: Transaction<H>) -> error::Result<()> {
|
||||
for change in transaction.0.into_iter() {
|
||||
match change {
|
||||
Change::Set(col, key, value) => self.set(col, &key, &value),
|
||||
Change::Remove(col, key) => self.remove(col, &key),
|
||||
Change::Store(hash, preimage) => self.store(&hash, &preimage),
|
||||
Change::Release(hash) => self.release(&hash),
|
||||
}
|
||||
}?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Commit the `transaction` to the database atomically. Any further calls to `get` or `lookup`
|
||||
/// will reflect the new state.
|
||||
fn commit_ref<'a>(&self, transaction: &mut dyn Iterator<Item=ChangeRef<'a, H>>) {
|
||||
fn commit_ref<'a>(&self, transaction: &mut dyn Iterator<Item=ChangeRef<'a, H>>) -> error::Result<()> {
|
||||
let mut tx = Transaction::new();
|
||||
for change in transaction {
|
||||
match change {
|
||||
@@ -105,13 +108,13 @@ pub trait Database<H: Clone>: Send + Sync {
|
||||
ChangeRef::Release(hash) => tx.release(hash),
|
||||
}
|
||||
}
|
||||
self.commit(tx);
|
||||
self.commit(tx)
|
||||
}
|
||||
|
||||
/// Retrieve the value previously stored against `key` or `None` if
|
||||
/// `key` is not currently in the database.
|
||||
fn get(&self, col: ColumnId, key: &[u8]) -> Option<Vec<u8>>;
|
||||
|
||||
|
||||
/// Call `f` with the value previously stored against `key`.
|
||||
///
|
||||
/// This may be faster than `get` since it doesn't allocate.
|
||||
@@ -119,24 +122,24 @@ pub trait Database<H: Clone>: Send + Sync {
|
||||
fn with_get(&self, col: ColumnId, key: &[u8], f: &mut dyn FnMut(&[u8])) {
|
||||
self.get(col, key).map(|v| f(&v));
|
||||
}
|
||||
|
||||
|
||||
/// Set the value of `key` in `col` to `value`, replacing anything that is there currently.
|
||||
fn set(&self, col: ColumnId, key: &[u8], value: &[u8]) {
|
||||
fn set(&self, col: ColumnId, key: &[u8], value: &[u8]) -> error::Result<()> {
|
||||
let mut t = Transaction::new();
|
||||
t.set(col, key, value);
|
||||
self.commit(t);
|
||||
self.commit(t)
|
||||
}
|
||||
/// Remove the value of `key` in `col`.
|
||||
fn remove(&self, col: ColumnId, key: &[u8]) {
|
||||
fn remove(&self, col: ColumnId, key: &[u8]) -> error::Result<()> {
|
||||
let mut t = Transaction::new();
|
||||
t.remove(col, key);
|
||||
self.commit(t);
|
||||
self.commit(t)
|
||||
}
|
||||
|
||||
/// Retrieve the first preimage previously `store`d for `hash` or `None` if no preimage is
|
||||
/// currently stored.
|
||||
fn lookup(&self, hash: &H) -> Option<Vec<u8>>;
|
||||
|
||||
|
||||
/// Call `f` with the preimage stored for `hash` and return the result, or `None` if no preimage
|
||||
/// is currently stored.
|
||||
///
|
||||
@@ -145,23 +148,23 @@ pub trait Database<H: Clone>: Send + Sync {
|
||||
fn with_lookup(&self, hash: &H, f: &mut dyn FnMut(&[u8])) {
|
||||
self.lookup(hash).map(|v| f(&v));
|
||||
}
|
||||
|
||||
|
||||
/// Store the `preimage` of `hash` into the database, so that it may be looked up later with
|
||||
/// `Database::lookup`. This may be called multiple times, but `Database::lookup` but subsequent
|
||||
/// calls will ignore `preimage` and simply increase the number of references on `hash`.
|
||||
fn store(&self, hash: &H, preimage: &[u8]) {
|
||||
fn store(&self, hash: &H, preimage: &[u8]) -> error::Result<()> {
|
||||
let mut t = Transaction::new();
|
||||
t.store(hash.clone(), preimage);
|
||||
self.commit(t);
|
||||
self.commit(t)
|
||||
}
|
||||
|
||||
|
||||
/// Release the preimage of `hash` from the database. An equal number of these to the number of
|
||||
/// corresponding `store`s must have been given before it is legal for `Database::lookup` to
|
||||
/// be unable to provide the preimage.
|
||||
fn release(&self, hash: &H) {
|
||||
fn release(&self, hash: &H) -> error::Result<()> {
|
||||
let mut t = Transaction::new();
|
||||
t.release(hash.clone());
|
||||
self.commit(t);
|
||||
self.commit(t)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
//! In-memory implementation of `Database`
|
||||
|
||||
use std::collections::HashMap;
|
||||
use crate::{Database, Transaction, ColumnId, Change};
|
||||
use crate::{Database, Change, ColumnId, Transaction, error};
|
||||
use parking_lot::RwLock;
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -29,7 +29,7 @@ pub struct MemDb<H: Clone + Send + Sync + Eq + PartialEq + Default + std::hash::
|
||||
impl<H> Database<H> for MemDb<H>
|
||||
where H: Clone + Send + Sync + Eq + PartialEq + Default + std::hash::Hash
|
||||
{
|
||||
fn commit(&self, transaction: Transaction<H>) {
|
||||
fn commit(&self, transaction: Transaction<H>) -> error::Result<()> {
|
||||
let mut s = self.0.write();
|
||||
for change in transaction.0.into_iter() {
|
||||
match change {
|
||||
@@ -39,6 +39,8 @@ impl<H> Database<H> for MemDb<H>
|
||||
Change::Release(hash) => { s.1.remove(&hash); },
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get(&self, col: ColumnId, key: &[u8]) -> Option<Vec<u8>> {
|
||||
|
||||
Reference in New Issue
Block a user