pub trait DbValueType {
type Type: ?Sized;
}
pub trait KeyValueDbTypes {
type ValueType: DbValueType;
}
pub trait KeyValueDbTraitRead: KeyValueDbTypes {
fn get(&self, key: &[u8]) -> Result<Option<Self::ValueType>>;
fn get_with_number_key(&self, key: i64) -> Result<Option<Self::ValueType>> {
self.get(key.to_string().as_bytes())
}
}
pub trait KeyValueDbTraitMultiReader: KeyValueDbTraitRead {}
pub trait KeyValueDbTraitOwnedRead: KeyValueDbTypes {
fn get_mut(&mut self, key: &[u8]) -> Result<Option<Self::ValueType>>;
fn get_mut_with_number_key(
&mut self, key: i64,
) -> Result<Option<Self::ValueType>> {
trace!(
"KeyValue:get: key={:?} {:?}",
key,
key.to_string().as_bytes()
);
self.get_mut(key.to_string().as_bytes())
}
}
enable_impl_transmute_for_element_satisfy! {
generic Item;
trait 'static + FallibleIterator<Item = Item, Error = Error>;
}
pub struct KvdbIterIterator<Item, KeyType: ?Sized, T: ?Sized> {
__i_m: std::marker::PhantomData<Item>,
__k_m: std::marker::PhantomData<KeyType>,
__t_m: std::marker::PhantomData<T>,
}
pub trait KeyValueDbIterableTrait<Item, KeyType: ?Sized, Tag: ?Sized>
where KvdbIterIterator<Item, KeyType, Tag>:
WrappedTrait<dyn FallibleIterator<Item = Item, Error = Error>>
{
fn iter_range(
&mut self, lower_bound_incl: &KeyType,
upper_bound_excl: Option<&KeyType>,
) -> Result<
Wrap<
KvdbIterIterator<Item, KeyType, Tag>,
dyn FallibleIterator<Item = Item, Error = Error>,
>,
>;
fn iter_range_excl(
&mut self, lower_bound_excl: &KeyType, upper_bound_excl: &KeyType,
) -> Result<
Wrap<
KvdbIterIterator<Item, KeyType, Tag>,
dyn FallibleIterator<Item = Item, Error = Error>,
>,
>;
}
pub trait KeyValueDbTraitSingleWriter: KeyValueDbTraitOwnedRead {
fn delete(&mut self, key: &[u8])
-> Result<Option<Option<Self::ValueType>>>;
fn delete_with_number_key(
&mut self, key: i64,
) -> Result<Option<Option<Self::ValueType>>> {
self.delete(key.to_string().as_bytes())
}
fn put(
&mut self, key: &[u8], value: &<Self::ValueType as DbValueType>::Type,
) -> Result<Option<Option<Self::ValueType>>>;
fn put_with_number_key(
&mut self, key: i64, value: &<Self::ValueType as DbValueType>::Type,
) -> Result<Option<Option<Self::ValueType>>> {
trace!(
"KeyValue:put: key={:?} {:?}",
key,
key.to_string().as_bytes()
);
self.put(key.to_string().as_bytes(), value)
}
}
pub trait KeyValueDbTraitSingleWriterMultiReader:
KeyValueDbTraitMultiReader + KeyValueDbTraitSingleWriter
{
}
pub trait KeyValueDbTrait:
KeyValueDbTraitMultiReader + Send + Sync + MallocSizeOf
{
fn delete(&self, key: &[u8]) -> Result<Option<Option<Self::ValueType>>>;
fn delete_with_number_key(
&self, key: i64,
) -> Result<Option<Option<Self::ValueType>>> {
self.delete(key.to_string().as_bytes())
}
fn put(
&self, key: &[u8], value: &<Self::ValueType as DbValueType>::Type,
) -> Result<Option<Option<Self::ValueType>>>;
fn put_with_number_key(
&self, key: i64, value: &<Self::ValueType as DbValueType>::Type,
) -> Result<Option<Option<Self::ValueType>>> {
self.put(key.to_string().as_bytes(), value)
}
}
#[allow(drop_bounds)]
pub trait KeyValueDbTransactionTrait:
KeyValueDbTraitSingleWriter + Drop
{
fn commit(&mut self, db: &dyn Any) -> Result<()>;
fn revert(&mut self) -> Result<()>;
fn restart(&mut self, immediate_write: bool, no_revert: bool)
-> Result<()>;
}
pub trait KeyValueDbAsAnyTrait: KeyValueDbTypes {
fn as_any(&self) -> &dyn Any;
}
impl<T: KeyValueDbTypes + Any> KeyValueDbAsAnyTrait for T {
fn as_any(&self) -> &dyn Any { self }
}
pub trait KeyValueDbTraitTransactional: KeyValueDbAsAnyTrait {
type TransactionType: KeyValueDbTransactionTrait<
ValueType = Self::ValueType,
>;
fn start_transaction(
&self, immediate_write: bool,
) -> Result<Self::TransactionType>;
}
pub trait KeyValueDbTraitTransactionalDyn: KeyValueDbAsAnyTrait {
fn start_transaction_dyn(
&self, immediate_write: bool,
) -> Result<Box<dyn KeyValueDbTransactionTrait<ValueType = Self::ValueType>>>;
}
impl<T: KeyValueDbTraitTransactional> KeyValueDbTraitTransactionalDyn for T
where T::TransactionType: 'static
{
fn start_transaction_dyn(
&self, immediate_write: bool,
) -> Result<Box<dyn KeyValueDbTransactionTrait<ValueType = Self::ValueType>>>
{
Ok(Box::new(self.start_transaction(immediate_write)?))
}
}
pub trait KeyValueDbToOwnedReadTrait: KeyValueDbTypes {
fn to_owned_read(
&self,
) -> Result<
Box<dyn '_ + KeyValueDbTraitOwnedRead<ValueType = Self::ValueType>>,
>;
}
impl<T: 'static + KeyValueDbTraitMultiReader> KeyValueDbToOwnedReadTrait for T
where for<'a> &'a T: KeyValueDbTraitOwnedRead<ValueType = Self::ValueType>
{
fn to_owned_read(
&self,
) -> Result<
Box<dyn '_ + KeyValueDbTraitOwnedRead<ValueType = Self::ValueType>>,
> {
Ok(Box::new(self))
}
}
macro_rules! mark_kvdb_multi_reader {
($type:ty) => {
impl KeyValueDbTraitMultiReader for $type {}
impl OwnedReadImplFamily for &$type {
type FamilyRepresentative = dyn KeyValueDbTraitMultiReader<
ValueType = <$type as KeyValueDbTypes>::ValueType,
>;
}
};
}
impl DbValueType for () {
type Type = ();
}
impl DbValueType for Box<[u8]> {
type Type = [u8];
}
impl DbValueType for i64 {
type Type = i64;
}
impl<
T: OwnedReadImplFamily
+ OwnedReadImplByFamily<
<T as OwnedReadImplFamily>::FamilyRepresentative,
>,
> KeyValueDbTraitOwnedRead for T
{
fn get_mut(&mut self, key: &[u8]) -> Result<Option<Self::ValueType>> {
self.get_mut_impl(key)
}
fn get_mut_with_number_key(
&mut self, key: i64,
) -> Result<Option<Self::ValueType>> {
trace!(
"KeyValue:get_impl: key={:?} {:?}",
key,
key.to_string().as_bytes()
);
self.get_mut_with_number_key_impl(key)
}
}
pub trait OwnedReadImplFamily {
type FamilyRepresentative: ?Sized;
}
pub trait OwnedReadImplByFamily<FamilyRepresentative: ?Sized>:
KeyValueDbTypes
{
fn get_mut_impl(&mut self, key: &[u8]) -> Result<Option<Self::ValueType>>;
fn get_mut_with_number_key_impl(
&mut self, key: i64,
) -> Result<Option<Self::ValueType>>;
}
impl<
T: SingleWriterImplFamily
+ SingleWriterImplByFamily<
<T as SingleWriterImplFamily>::FamilyRepresentative,
> + KeyValueDbTraitOwnedRead,
> KeyValueDbTraitSingleWriter for T
{
fn delete(
&mut self, key: &[u8],
) -> Result<Option<Option<Self::ValueType>>> {
self.delete_impl(key)
}
fn delete_with_number_key(
&mut self, key: i64,
) -> Result<Option<Option<Self::ValueType>>> {
self.delete_with_number_key_impl(key)
}
fn put(
&mut self, key: &[u8], value: &<Self::ValueType as DbValueType>::Type,
) -> Result<Option<Option<Self::ValueType>>> {
self.put_impl(key, value)
}
fn put_with_number_key(
&mut self, key: i64, value: &<Self::ValueType as DbValueType>::Type,
) -> Result<Option<Option<Self::ValueType>>> {
self.put_with_number_key_impl(key, value)
}
}
pub trait SingleWriterImplFamily {
type FamilyRepresentative: ?Sized;
}
pub trait SingleWriterImplByFamily<FamilyRepresentative: ?Sized>:
KeyValueDbTypes
{
fn delete_impl(
&mut self, key: &[u8],
) -> Result<Option<Option<Self::ValueType>>>;
fn delete_with_number_key_impl(
&mut self, key: i64,
) -> Result<Option<Option<Self::ValueType>>>;
fn put_impl(
&mut self, key: &[u8], value: &<Self::ValueType as DbValueType>::Type,
) -> Result<Option<Option<Self::ValueType>>>;
fn put_with_number_key_impl(
&mut self, key: i64, value: &<Self::ValueType as DbValueType>::Type,
) -> Result<Option<Option<Self::ValueType>>>;
}
impl<
T: ReadImplFamily
+ ReadImplByFamily<<T as ReadImplFamily>::FamilyRepresentative>,
> KeyValueDbTraitRead for T
{
fn get(&self, key: &[u8]) -> Result<Option<Self::ValueType>> {
self.get_impl(key)
}
fn get_with_number_key(&self, key: i64) -> Result<Option<Self::ValueType>> {
self.get_with_number_key_impl(key)
}
}
pub trait ReadImplFamily {
type FamilyRepresentative: ?Sized;
}
pub trait ReadImplByFamily<FamilyRepresentative: ?Sized>:
KeyValueDbTypes
{
fn get_impl(&self, key: &[u8]) -> Result<Option<Self::ValueType>>;
fn get_with_number_key_impl(
&self, key: i64,
) -> Result<Option<Self::ValueType>>;
}
impl<
T: DbImplFamily
+ DbImplByFamily<<T as DbImplFamily>::FamilyRepresentative>
+ KeyValueDbTraitMultiReader
+ Send
+ Sync
+ MallocSizeOf,
> KeyValueDbTrait for T
{
fn delete(&self, key: &[u8]) -> Result<Option<Option<Self::ValueType>>> {
self.delete_impl(key)
}
fn delete_with_number_key(
&self, key: i64,
) -> Result<Option<Option<Self::ValueType>>> {
self.delete_with_number_key_impl(key)
}
fn put(
&self, key: &[u8], value: &<Self::ValueType as DbValueType>::Type,
) -> Result<Option<Option<Self::ValueType>>> {
self.put_impl(key, value)
}
fn put_with_number_key(
&self, key: i64, value: &<Self::ValueType as DbValueType>::Type,
) -> Result<Option<Option<Self::ValueType>>> {
self.put_with_number_key_impl(key, value)
}
}
pub trait DbImplFamily {
type FamilyRepresentative: ?Sized;
}
pub trait DbImplByFamily<FamilyRepresentative: ?Sized>:
KeyValueDbTypes
{
fn delete_impl(
&self, key: &[u8],
) -> Result<Option<Option<Self::ValueType>>>;
fn delete_with_number_key_impl(
&self, key: i64,
) -> Result<Option<Option<Self::ValueType>>>;
fn put_impl(
&self, key: &[u8], value: &<Self::ValueType as DbValueType>::Type,
) -> Result<Option<Option<Self::ValueType>>>;
fn put_with_number_key_impl(
&self, key: i64, value: &<Self::ValueType as DbValueType>::Type,
) -> Result<Option<Option<Self::ValueType>>>;
}
impl<ValueType: DbValueType> OwnedReadImplFamily
for dyn KeyValueDbTraitMultiReader<ValueType = ValueType>
{
type FamilyRepresentative =
dyn KeyValueDbTraitMultiReader<ValueType = ValueType>;
}
impl<
T: KeyValueDbTraitMultiReader<ValueType = ValueType>,
ValueType: DbValueType,
> KeyValueDbTypes for &T
{
type ValueType = T::ValueType;
}
impl<
T: KeyValueDbTraitMultiReader<ValueType = ValueType>,
ValueType: DbValueType,
>
OwnedReadImplByFamily<dyn KeyValueDbTraitMultiReader<ValueType = ValueType>>
for &T
{
fn get_mut_impl(&mut self, key: &[u8]) -> Result<Option<Self::ValueType>> {
self.get(key)
}
fn get_mut_with_number_key_impl(
&mut self, key: i64,
) -> Result<Option<Self::ValueType>> {
self.get_with_number_key(key)
}
}
use crate::{
impls::errors::*,
utils::{
tuple::ElementSatisfy,
wrap::{Wrap, WrappedTrait},
},
};
use fallible_iterator::FallibleIterator;
use malloc_size_of::MallocSizeOf;
use std::any::Any;