Commit 5c5a99f9 authored by Éloïs's avatar Éloïs

[feat] kv_typed: define trait TransactionalBackend

parent eaae10e6
......@@ -25,6 +25,22 @@ pub mod sled;
use crate::*;
pub trait TransactionalBackend<DbReader: From<Vec<Self::TxCol>>, DbWriter: From<Vec<Self::TxCol>>>:
Backend
{
type Err: Error + Send + Sync + 'static;
type TxCol: BackendCol;
fn read<A: Debug, D, F: Fn(&DbReader) -> TransactionResult<D, A, Self::Err>>(
&self,
f: F,
) -> TransactionResult<D, A, Self::Err>;
fn write<A: Debug, F: Fn(&DbWriter) -> TransactionResult<(), A, Self::Err>>(
&self,
f: F,
) -> TransactionResult<(), A, Self::Err>;
}
pub trait Backend: 'static + Clone + Sized {
const NAME: &'static str;
type Col: BackendCol;
......
......@@ -15,10 +15,12 @@
//! Sled backend for KV Typed,
use crate::*;
mod transactional;
pub use sled::Config;
use crate::*;
#[derive(Clone, Debug)]
pub struct Sled {
db: sled::Db,
......
// Copyright (C) 2020 Éloïs SANCHEZ.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Sled backend for KV Typed,
/*use crate::*;
use sled::transaction::{ConflictableTransactionError, Transactional, TransactionalTree};
enum AbortType<A> {
User(A),
Kv(KvError),
}
#[allow(missing_copy_implementations)]
pub struct SledTxCol(&'static TransactionalTree);
impl Debug for SledTxCol {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SledTxCol")
.field("0", &"TransactionalTree")
.finish()
}
}
impl<DbReader: From<Vec<SledTxCol>>, DbWriter> TransactionalBackend<DbReader, DbWriter> for Sled {
type Err = sled::Error;
type TxCol = SledTxCol;
fn read<A: Debug, D, F: Fn(&DbReader) -> TransactionResult<D, A, Self::Err>>(
&self,
f: F,
) -> TransactionResult<D, A, Self::Err> {
match self
.trees
.transaction::<_, D>(|tx_trees: &Vec<TransactionalTree>| {
let reader = DbReader::from(
tx_trees
.iter()
.map(|tx_tree| SledTxCol(unsafe { to_static_ref(tx_tree) }))
.collect(),
);
f(&reader).map_err(|e| match e {
TransactionError::Abort(a) => {
ConflictableTransactionError::Abort(AbortType::User(a))
}
TransactionError::BackendErr(e) => ConflictableTransactionError::Storage(e),
TransactionError::KvError(e) => {
ConflictableTransactionError::Abort(AbortType::Kv(e))
}
})
}) {
Ok(t) => Ok(t),
Err(e) => match e {
sled::transaction::TransactionError::Abort(a) => match a {
AbortType::User(a) => Err(TransactionError::Abort(a)),
AbortType::Kv(e) => Err(TransactionError::KvError(e)),
},
sled::transaction::TransactionError::Storage(e) => {
Err(TransactionError::BackendErr(e))
}
},
}
}
fn write<A: Debug, F: Fn(&DbWriter) -> TransactionResult<(), A, Self::Err>>(
&self,
_f: F,
) -> TransactionResult<(), A, Self::Err> {
todo!()
}
}*/
......@@ -55,3 +55,11 @@ impl From<sled::Error> for KvError {
KvError::BackendError(Box::new(e).into())
}
}
pub type TransactionResult<D, A, BE> = Result<D, TransactionError<A, BE>>;
#[derive(Debug)]
pub enum TransactionError<A: Debug, BE: Error + Send + Sync + 'static> {
Abort(A),
BackendErr(BE),
KvError(KvError),
}
......@@ -56,13 +56,15 @@ pub mod prelude {
pub use crate::backend::mock::{MockBackend, MockBackendCol, MockBackendIter};
#[cfg(feature = "sled_backend")]
pub use crate::backend::sled::{Config as SledConf, Sled};
pub use crate::backend::{Backend, BackendCol};
pub use crate::backend::{Backend, BackendCol, TransactionalBackend};
pub use crate::batch::Batch;
#[cfg(feature = "mock")]
pub use crate::collection_ro::MockColRo;
pub use crate::collection_ro::{ColRo, DbCollectionRo};
pub use crate::collection_rw::{ColRw, DbCollectionRw};
pub use crate::error::{DynErr, KvError, KvResult, StringErr};
pub use crate::error::{
DynErr, KvError, KvResult, StringErr, TransactionError, TransactionResult,
};
pub use crate::event::{EventTrait, Events};
#[cfg(feature = "explorer")]
pub use crate::explorer::{ExplorableKey, ExplorableValue};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment