use super::{IterItem, Key, Result, StorageList, StorageMap, TimestampMillis};
use async_trait::async_trait;
use serde::de::DeserializeOwned;
#[async_trait]
pub trait AsyncIterator {
type Item;
async fn next(&mut self) -> Option<Self::Item>;
}
pub trait SplitSubslice {
fn split_subslice(&self, subslice: &[u8]) -> Option<(&[u8], &[u8])>;
}
impl SplitSubslice for [u8] {
fn split_subslice(&self, subslice: &[u8]) -> Option<(&[u8], &[u8])> {
self.windows(subslice.len())
.position(|window| window == subslice)
.map(|index| self.split_at(index + subslice.len()))
}
}
#[async_trait]
#[allow(clippy::len_without_is_empty)]
pub trait IStorageDB: Send + Sync {
type MapType: Map;
type ListType: List;
async fn map<N: AsRef<[u8]> + Sync + Send>(
&self,
name: N,
expire: Option<TimestampMillis>,
) -> Result<Self::MapType>;
async fn map_remove<K>(&self, name: K) -> Result<()>
where
K: AsRef<[u8]> + Sync + Send;
async fn map_contains_key<K: AsRef<[u8]> + Sync + Send>(&self, key: K) -> Result<bool>;
async fn list<V: AsRef<[u8]> + Sync + Send>(
&self,
name: V,
expire: Option<TimestampMillis>,
) -> Result<Self::ListType>;
async fn list_remove<K>(&self, name: K) -> Result<()>
where
K: AsRef<[u8]> + Sync + Send;
async fn list_contains_key<K: AsRef<[u8]> + Sync + Send>(&self, key: K) -> Result<bool>;
async fn insert<K, V>(&self, key: K, val: &V) -> Result<()>
where
K: AsRef<[u8]> + Sync + Send,
V: serde::ser::Serialize + Sync + Send;
async fn get<K, V>(&self, key: K) -> Result<Option<V>>
where
K: AsRef<[u8]> + Sync + Send,
V: DeserializeOwned + Sync + Send;
async fn remove<K>(&self, key: K) -> Result<()>
where
K: AsRef<[u8]> + Sync + Send;
async fn batch_insert<V>(&self, key_vals: Vec<(Key, V)>) -> Result<()>
where
V: serde::ser::Serialize + Sync + Send;
async fn batch_remove(&self, keys: Vec<Key>) -> Result<()>;
async fn counter_incr<K>(&self, key: K, increment: isize) -> Result<()>
where
K: AsRef<[u8]> + Sync + Send;
async fn counter_decr<K>(&self, key: K, increment: isize) -> Result<()>
where
K: AsRef<[u8]> + Sync + Send;
async fn counter_get<K>(&self, key: K) -> Result<Option<isize>>
where
K: AsRef<[u8]> + Sync + Send;
async fn counter_set<K>(&self, key: K, val: isize) -> Result<()>
where
K: AsRef<[u8]> + Sync + Send;
async fn contains_key<K: AsRef<[u8]> + Sync + Send>(&self, key: K) -> Result<bool>;
#[cfg(feature = "len")]
async fn len(&self) -> Result<usize>;
async fn db_size(&self) -> Result<usize>;
#[cfg(feature = "ttl")]
async fn expire_at<K>(&self, key: K, at: TimestampMillis) -> Result<bool>
where
K: AsRef<[u8]> + Sync + Send;
#[cfg(feature = "ttl")]
async fn expire<K>(&self, key: K, dur: TimestampMillis) -> Result<bool>
where
K: AsRef<[u8]> + Sync + Send;
#[cfg(feature = "ttl")]
async fn ttl<K>(&self, key: K) -> Result<Option<TimestampMillis>>
where
K: AsRef<[u8]> + Sync + Send;
async fn map_iter<'a>(
&'a mut self,
) -> Result<Box<dyn AsyncIterator<Item = Result<StorageMap>> + Send + 'a>>;
async fn list_iter<'a>(
&'a mut self,
) -> Result<Box<dyn AsyncIterator<Item = Result<StorageList>> + Send + 'a>>;
async fn scan<'a, P>(
&'a mut self,
pattern: P,
) -> Result<Box<dyn AsyncIterator<Item = Result<Key>> + Send + 'a>>
where
P: AsRef<[u8]> + Send + Sync;
async fn info(&self) -> Result<serde_json::Value>;
}
#[async_trait]
pub trait Map: Sync + Send {
fn name(&self) -> &[u8];
async fn insert<K, V>(&self, key: K, val: &V) -> Result<()>
where
K: AsRef<[u8]> + Sync + Send,
V: serde::ser::Serialize + Sync + Send + ?Sized;
async fn get<K, V>(&self, key: K) -> Result<Option<V>>
where
K: AsRef<[u8]> + Sync + Send,
V: DeserializeOwned + Sync + Send;
async fn remove<K>(&self, key: K) -> Result<()>
where
K: AsRef<[u8]> + Sync + Send;
async fn contains_key<K: AsRef<[u8]> + Sync + Send>(&self, key: K) -> Result<bool>;
#[cfg(feature = "map_len")]
async fn len(&self) -> Result<usize>;
async fn is_empty(&self) -> Result<bool>;
async fn clear(&self) -> Result<()>;
async fn remove_and_fetch<K, V>(&self, key: K) -> Result<Option<V>>
where
K: AsRef<[u8]> + Sync + Send,
V: DeserializeOwned + Sync + Send;
async fn remove_with_prefix<K>(&self, prefix: K) -> Result<()>
where
K: AsRef<[u8]> + Sync + Send;
async fn batch_insert<V>(&self, key_vals: Vec<(Key, V)>) -> Result<()>
where
V: serde::ser::Serialize + Sync + Send;
async fn batch_remove(&self, keys: Vec<Key>) -> Result<()>;
async fn iter<'a, V>(
&'a mut self,
) -> Result<Box<dyn AsyncIterator<Item = IterItem<V>> + Send + 'a>>
where
V: DeserializeOwned + Sync + Send + 'a + 'static;
async fn key_iter<'a>(
&'a mut self,
) -> Result<Box<dyn AsyncIterator<Item = Result<Key>> + Send + 'a>>;
async fn prefix_iter<'a, P, V>(
&'a mut self,
prefix: P,
) -> Result<Box<dyn AsyncIterator<Item = IterItem<V>> + Send + 'a>>
where
P: AsRef<[u8]> + Send + Sync,
V: DeserializeOwned + Sync + Send + 'a + 'static;
#[cfg(feature = "ttl")]
async fn expire_at(&self, at: TimestampMillis) -> Result<bool>;
#[cfg(feature = "ttl")]
async fn expire(&self, dur: TimestampMillis) -> Result<bool>;
#[cfg(feature = "ttl")]
async fn ttl(&self) -> Result<Option<TimestampMillis>>;
}
#[async_trait]
pub trait List: Sync + Send {
fn name(&self) -> &[u8];
async fn push<V>(&self, val: &V) -> Result<()>
where
V: serde::ser::Serialize + Sync + Send;
async fn pushs<V>(&self, vals: Vec<V>) -> Result<()>
where
V: serde::ser::Serialize + Sync + Send;
async fn push_limit<V>(
&self,
val: &V,
limit: usize,
pop_front_if_limited: bool,
) -> Result<Option<V>>
where
V: serde::ser::Serialize + Sync + Send,
V: DeserializeOwned;
async fn pop<V>(&self) -> Result<Option<V>>
where
V: DeserializeOwned + Sync + Send;
async fn all<V>(&self) -> Result<Vec<V>>
where
V: DeserializeOwned + Sync + Send;
async fn get_index<V>(&self, idx: usize) -> Result<Option<V>>
where
V: DeserializeOwned + Sync + Send;
async fn len(&self) -> Result<usize>;
async fn is_empty(&self) -> Result<bool>;
async fn clear(&self) -> Result<()>;
async fn iter<'a, V>(
&'a mut self,
) -> Result<Box<dyn AsyncIterator<Item = Result<V>> + Send + 'a>>
where
V: DeserializeOwned + Sync + Send + 'a + 'static;
#[cfg(feature = "ttl")]
async fn expire_at(&self, at: TimestampMillis) -> Result<bool>;
#[cfg(feature = "ttl")]
async fn expire(&self, dur: TimestampMillis) -> Result<bool>;
#[cfg(feature = "ttl")]
async fn ttl(&self) -> Result<Option<TimestampMillis>>;
}