1
0
Fork 0
mirror of https://forgejo.ellis.link/continuwuation/continuwuity.git synced 2025-07-27 18:28:31 +00:00
continuwuity/src/database/abstraction.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

93 lines
2.3 KiB
Rust
Raw Normal View History

use std::{error::Error, future::Future, pin::Pin, sync::Arc};
2021-06-08 18:10:00 +02:00
use super::Config;
2021-07-14 07:07:08 +00:00
use crate::Result;
#[cfg(feature = "sqlite")]
pub mod sqlite;
2021-06-08 18:10:00 +02:00
2021-10-16 15:19:25 +02:00
#[cfg(feature = "rocksdb")]
pub(crate) mod rocksdb;
2021-10-16 15:19:25 +02:00
#[cfg(any(feature = "sqlite", feature = "rocksdb"))]
pub(crate) mod watchers;
pub(crate) trait KeyValueDatabaseEngine: Send + Sync {
fn open(config: &Config) -> Result<Self>
where
Self: Sized;
fn open_tree(&self, name: &'static str) -> Result<Arc<dyn KvTree>>;
fn flush(&self) -> Result<()>;
fn sync(&self) -> Result<()> { Ok(()) }
fn cleanup(&self) -> Result<()> { Ok(()) }
fn memory_usage(&self) -> Result<String> {
Ok("Current database engine does not support memory usage reporting.".to_owned())
}
#[allow(dead_code)]
fn clear_caches(&self) {}
fn backup(&self) -> Result<(), Box<dyn Error>> {
unimplemented!()
}
fn backup_list(&self) -> Result<String> {
Ok(String::new())
}
2021-06-08 18:10:00 +02:00
}
pub(crate) trait KvTree: Send + Sync {
fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>>;
2021-06-08 18:10:00 +02:00
#[allow(dead_code)]
#[cfg(feature = "rocksdb")]
fn multi_get(
&self, _iter: Vec<(&Arc<rust_rocksdb::BoundColumnFamily<'_>>, Vec<u8>)>,
) -> Vec<std::result::Result<Option<Vec<u8>>, rust_rocksdb::Error>> {
unimplemented!()
}
fn insert(&self, key: &[u8], value: &[u8]) -> Result<()>;
fn insert_batch(&self, iter: &mut dyn Iterator<Item = (Vec<u8>, Vec<u8>)>) -> Result<()> {
for (key, value) in iter {
self.insert(&key, &value)?;
}
Ok(())
}
2021-06-08 18:10:00 +02:00
fn remove(&self, key: &[u8]) -> Result<()>;
fn remove_batch(&self, iter: &mut dyn Iterator<Item = Vec<u8>>) -> Result<()> {
for key in iter {
self.remove(&key)?;
}
2021-06-08 18:10:00 +02:00
Ok(())
}
fn iter<'a>(&'a self) -> Box<dyn 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 Iterator<Item = (Vec<u8>, Vec<u8>)> + 'a>;
2021-06-08 18:10:00 +02:00
fn increment(&self, key: &[u8]) -> Result<Vec<u8>>;
fn increment_batch(&self, iter: &mut dyn Iterator<Item = Vec<u8>>) -> Result<()> {
for key in iter {
self.increment(&key)?;
}
Ok(())
}
2021-06-08 18:10:00 +02:00
fn scan_prefix<'a>(&'a self, prefix: Vec<u8>) -> Box<dyn 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>>;
2021-06-08 18:10:00 +02:00
fn clear(&self) -> Result<()> {
for (key, _) in self.iter() {
self.remove(&key)?;
}
2021-06-08 18:10:00 +02:00
Ok(())
}
2021-06-08 18:10:00 +02:00
}