add transaction stat

without.crypto
Niko 2 years ago
parent 1771f400d6
commit 7453bb29cd
  1. 20
      src/backend/impl_lmdb/transaction.rs
  2. 13
      src/backend/impl_safe/transaction.rs
  3. 6
      src/backend/traits.rs
  4. 15
      src/readwrite.rs

@ -10,7 +10,7 @@
use lmdb::Transaction;
use super::{DatabaseImpl, ErrorImpl, RoCursorImpl, WriteFlagsImpl};
use super::{DatabaseImpl, ErrorImpl, RoCursorImpl, StatImpl, WriteFlagsImpl};
use crate::backend::traits::{
BackendRoCursorTransaction, BackendRoTransaction, BackendRwCursorTransaction,
BackendRwTransaction,
@ -22,6 +22,7 @@ pub struct RoTransactionImpl<'t>(pub(crate) lmdb::RoTransaction<'t>);
impl<'t> BackendRoTransaction for RoTransactionImpl<'t> {
type Database = DatabaseImpl;
type Error = ErrorImpl;
type Stat = StatImpl;
fn get(&self, db: &Self::Database, key: &[u8]) -> Result<&[u8], Self::Error> {
self.0.get(db.0, &key).map_err(ErrorImpl::LmdbError)
@ -30,6 +31,14 @@ impl<'t> BackendRoTransaction for RoTransactionImpl<'t> {
fn abort(self) {
self.0.abort()
}
fn stat(&self, db: &Self::Database) -> Result<Self::Stat, Self::Error> {
//Err(ErrorImpl::LmdbError(lmdb::Error::Invalid))
self.0
.stat(db.0)
.map(StatImpl)
.map_err(ErrorImpl::LmdbError)
}
}
impl<'t> BackendRoCursorTransaction<'t> for RoTransactionImpl<'t> {
@ -50,11 +59,20 @@ impl<'t> BackendRwTransaction for RwTransactionImpl<'t> {
type Database = DatabaseImpl;
type Error = ErrorImpl;
type Flags = WriteFlagsImpl;
type Stat = StatImpl;
fn get(&self, db: &Self::Database, key: &[u8]) -> Result<&[u8], Self::Error> {
self.0.get(db.0, &key).map_err(ErrorImpl::LmdbError)
}
fn stat(&self, db: &Self::Database) -> Result<Self::Stat, Self::Error> {
//Err(ErrorImpl::LmdbError(lmdb::Error::Invalid))
self.0
.stat(db.0)
.map(StatImpl)
.map_err(ErrorImpl::LmdbError)
}
fn put(
&mut self,
db: &Self::Database,

@ -12,7 +12,8 @@
use std::{collections::HashMap, sync::Arc};
use super::{
snapshot::Snapshot, DatabaseImpl, EnvironmentImpl, ErrorImpl, RoCursorImpl, WriteFlagsImpl,
snapshot::Snapshot, DatabaseImpl, EnvironmentImpl, ErrorImpl, RoCursorImpl, StatImpl,
WriteFlagsImpl,
};
use crate::backend::traits::{
BackendRoCursorTransaction, BackendRoTransaction, BackendRwCursorTransaction,
@ -48,6 +49,7 @@ impl<'t> RoTransactionImpl<'t> {
impl<'t> BackendRoTransaction for RoTransactionImpl<'t> {
type Database = DatabaseImpl;
type Error = ErrorImpl;
type Stat = StatImpl;
fn get(&self, db: &Self::Database, key: &[u8]) -> Result<&[u8], Self::Error> {
let snapshot = self.snapshots.get(db).ok_or(ErrorImpl::DbIsForeignError)?;
@ -57,6 +59,10 @@ impl<'t> BackendRoTransaction for RoTransactionImpl<'t> {
fn abort(self) {
// noop
}
fn stat(&self, db: &Self::Database) -> Result<Self::Stat, Self::Error> {
Err(ErrorImpl::DbNotFoundError)
}
}
impl<'t> BackendRoCursorTransaction<'t> for RoTransactionImpl<'t> {
@ -98,12 +104,17 @@ impl<'t> BackendRwTransaction for RwTransactionImpl<'t> {
type Database = DatabaseImpl;
type Error = ErrorImpl;
type Flags = WriteFlagsImpl;
type Stat = StatImpl;
fn get(&self, db: &Self::Database, key: &[u8]) -> Result<&[u8], Self::Error> {
let snapshot = self.snapshots.get(db).ok_or(ErrorImpl::DbIsForeignError)?;
snapshot.get(key).ok_or(ErrorImpl::KeyValuePairNotFound)
}
fn stat(&self, db: &Self::Database) -> Result<Self::Stat, Self::Error> {
Err(ErrorImpl::DbNotFoundError)
}
#[cfg(not(feature = "db-dup-sort"))]
fn put(
&mut self,

@ -134,16 +134,20 @@ pub trait BackendEnvironment<'e>: Debug {
pub trait BackendRoTransaction: Debug {
type Error: BackendError;
type Database: BackendDatabase;
type Stat: BackendStat;
fn get(&self, db: &Self::Database, key: &[u8]) -> Result<&[u8], Self::Error>;
fn abort(self);
fn stat(&self, db: &Self::Database) -> Result<Self::Stat, Self::Error>;
}
pub trait BackendRwTransaction: Debug {
type Error: BackendError;
type Database: BackendDatabase;
type Flags: BackendWriteFlags;
type Stat: BackendStat;
fn get(&self, db: &Self::Database, key: &[u8]) -> Result<&[u8], Self::Error>;
@ -171,6 +175,8 @@ pub trait BackendRwTransaction: Debug {
fn commit(self) -> Result<(), Self::Error>;
fn abort(self);
fn stat(&self, db: &Self::Database) -> Result<Self::Stat, Self::Error>;
}
pub trait BackendRoCursorTransaction<'t>: BackendRoTransaction {

@ -11,7 +11,7 @@
use crate::{
backend::{
BackendDatabase, BackendRoCursor, BackendRoCursorTransaction, BackendRoTransaction,
BackendRwCursorTransaction, BackendRwTransaction,
BackendRwCursorTransaction, BackendRwTransaction, BackendStat,
},
error::StoreError,
helpers::read_transform,
@ -24,12 +24,15 @@ pub struct Writer<T>(T);
pub trait Readable<'r> {
type Database: BackendDatabase;
type RoCursor: BackendRoCursor<'r>;
type Stat: BackendStat;
fn get<K>(&'r self, db: &Self::Database, k: &K) -> Result<Option<Value<'r>>, StoreError>
where
K: AsRef<[u8]>;
fn open_ro_cursor(&'r self, db: &Self::Database) -> Result<Self::RoCursor, StoreError>;
fn stat(&'r self, db: &Self::Database) -> Result<Self::Stat, StoreError>;
}
impl<'r, T> Readable<'r> for Reader<T>
@ -38,6 +41,7 @@ where
{
type Database = T::Database;
type RoCursor = T::RoCursor;
type Stat = T::Stat;
fn get<K>(&'r self, db: &T::Database, k: &K) -> Result<Option<Value<'r>>, StoreError>
where
@ -53,6 +57,10 @@ where
fn open_ro_cursor(&'r self, db: &T::Database) -> Result<T::RoCursor, StoreError> {
self.0.open_ro_cursor(db).map_err(|e| e.into())
}
fn stat(&'r self, db: &Self::Database) -> Result<Self::Stat, StoreError> {
self.0.stat(db).map_err(|e| e.into())
}
}
impl<T> Reader<T> {
@ -76,6 +84,7 @@ where
{
type Database = T::Database;
type RoCursor = T::RoCursor;
type Stat = T::Stat;
fn get<K>(&'r self, db: &T::Database, k: &K) -> Result<Option<Value<'r>>, StoreError>
where
@ -91,6 +100,10 @@ where
fn open_ro_cursor(&'r self, db: &T::Database) -> Result<T::RoCursor, StoreError> {
self.0.open_ro_cursor(db).map_err(|e| e.into())
}
fn stat(&'r self, db: &Self::Database) -> Result<Self::Stat, StoreError> {
self.0.stat(db).map_err(|e| e.into())
}
}
impl<T> Writer<T> {

Loading…
Cancel
Save