1
0
Fork 0
mirror of https://gitlab.com/famedly/conduit.git synced 2025-06-27 16:35:59 +00:00
conduit/src/database/abstraction.rs

80 lines
2 KiB
Rust
Raw Normal View History

2021-06-08 18:10:00 +02:00
use super::Config;
2021-07-14 07:07:08 +00:00
use crate::Result;
2024-06-22 21:08:17 +07:00
use async_trait::async_trait;
use std::{future::Future, pin::Pin, sync::Arc};
#[cfg(feature = "sled")]
2021-07-14 07:07:08 +00:00
pub mod sled;
2021-07-14 07:07:08 +00:00
#[cfg(feature = "sqlite")]
pub mod sqlite;
2021-06-08 18:10:00 +02:00
2021-07-29 20:17:47 +02:00
#[cfg(feature = "heed")]
pub mod heed;
2021-10-16 15:19:25 +02:00
#[cfg(feature = "rocksdb")]
pub mod rocksdb;
#[cfg(feature = "persy")]
pub mod persy;
#[cfg(any(
feature = "sqlite",
feature = "rocksdb",
feature = "heed",
feature = "persy"
))]
pub mod watchers;
2024-06-22 21:08:17 +07:00
#[async_trait]
pub trait KeyValueDatabaseEngine: Send + Sync {
2024-06-22 21:08:17 +07:00
async fn open(config: &Config) -> Result<Self>
where
Self: Sized;
2024-06-22 21:08:17 +07:00
async fn open_tree(&self, name: &'static str) -> Result<Arc<dyn KvTree>>;
async fn flush(&self) -> Result<()>;
async fn cleanup(&self) -> Result<()> {
Ok(())
}
2024-06-22 21:08:17 +07:00
async fn memory_usage(&self) -> Result<String> {
2022-01-18 21:05:40 +01:00
Ok("Current database engine does not support memory usage reporting.".to_owned())
}
2021-06-08 18:10:00 +02:00
}
2024-06-22 21:22:43 +07:00
#[async_trait]
pub trait KvTree: Send + Sync {
2021-06-08 18:10:00 +02:00
fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>>;
fn insert(&self, key: &[u8], value: &[u8]) -> Result<()>;
fn insert_batch(&self, iter: &mut dyn Iterator<Item = (Vec<u8>, Vec<u8>)>) -> Result<()>;
2021-06-08 18:10:00 +02:00
fn remove(&self, key: &[u8]) -> Result<()>;
fn iter<'a>(&'a self) -> Box<dyn Send + Iterator<Item = (Vec<u8>, Vec<u8>)> + 'a>;
2021-06-08 18:10:00 +02:00
fn iter_from<'a>(
&'a self,
from: &[u8],
backwards: bool,
) -> Box<dyn Send + Iterator<Item = (Vec<u8>, Vec<u8>)> + 'a>;
2021-06-08 18:10:00 +02:00
2024-06-22 21:22:43 +07:00
async fn increment(&self, key: &[u8]) -> Result<Vec<u8>>;
fn increment_batch(&self, iter: &mut dyn Iterator<Item = Vec<u8>>) -> Result<()>;
2021-06-08 18:10:00 +02:00
fn scan_prefix<'a>(
&'a self,
prefix: Vec<u8>,
) -> Box<dyn Send + Iterator<Item = (Vec<u8>, Vec<u8>)> + 'a>;
2021-06-08 18:10:00 +02:00
fn watch_prefix<'a>(&'a self, prefix: &[u8]) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>;
2024-06-22 21:22:43 +07:00
async fn clear(&self) -> Result<()> {
2021-06-08 18:10:00 +02:00
for (key, _) in self.iter() {
self.remove(&key)?;
}
Ok(())
}
}