adana_db/
lib.rs

1mod batch;
2mod database;
3mod file_db;
4mod file_lock;
5mod in_memory;
6mod tree;
7
8pub use batch::*;
9pub use database::*;
10pub use file_db::*;
11pub use file_lock::*;
12pub use in_memory::*;
13use serde::Serialize;
14use std::collections::BTreeMap;
15use std::fmt::Debug;
16use std::hash::Hash;
17pub use tree::Tree;
18
19pub trait Key:
20    Hash + Eq + Send + Clone + Serialize + Debug + Sync + Ord
21{
22}
23pub trait Value: Serialize + Send + Clone + Debug + Sync {}
24
25impl<T: Hash + Eq + Send + Clone + Serialize + Debug + Sync + Ord> Key for T {}
26impl<T: Serialize + Send + Clone + Debug + Sync> Value for T {}
27pub trait Op<K: Key, V: Value> {
28    fn read(&self, k: impl Into<K>, r: impl Fn(&V) -> Option<V>) -> Option<V>;
29
30    fn get_value(&self, k: impl Into<K>) -> Option<V> {
31        self.read(k, |v| Some(v.clone()))
32    }
33    fn list_all(&self) -> BTreeMap<K, V>;
34
35    fn read_no_op(
36        &self,
37        k: impl Into<K>,
38        r: impl Fn(&V) -> Option<V>,
39    ) -> Option<V> {
40        self.read(k.into(), |v| {
41            r(v);
42            None
43        })
44    }
45    fn keys(&self) -> Vec<K>;
46    fn insert(&mut self, k: impl Into<K>, v: impl Into<V>) -> Option<V>;
47    fn remove(&mut self, k: impl Into<K>) -> Option<V>;
48    fn clear(&mut self);
49    fn contains(&self, k: &K) -> Option<bool>;
50    fn len(&self) -> Option<usize>;
51    fn is_empty(&self) -> bool {
52        self.len().filter(|&s| s > 0).is_none()
53    }
54}
55pub trait DbOp<K: Key, V: Value>: Op<K, V> {
56    fn get_current_tree(&self) -> Option<String>;
57
58    fn flush(&self) -> anyhow::Result<&'static str> {
59        Ok("sync not implemented")
60    }
61
62    fn open_tree(&mut self, tree_name: &str) -> Option<bool>;
63
64    fn tree_names(&self) -> Vec<String>;
65
66    fn drop_tree(&mut self, tree_name: &str) -> bool;
67
68    fn clear_tree(&mut self, tree_name: &str) -> bool;
69
70    fn merge_trees(
71        &mut self,
72        tree_name_source: &str,
73        tree_name_dest: &str,
74    ) -> Option<()>;
75
76    fn merge_current_tree_with(&mut self, tree_name_source: &str)
77    -> Option<()>;
78
79    fn apply_batch(&mut self, batch: Batch<K, V>) -> Option<()>;
80
81    fn apply_tree(
82        &mut self,
83        tree_name: &str,
84        consumer: &mut impl FnMut(&mut Tree<K, V>) -> Option<V>,
85    ) -> Option<V>;
86
87    fn open_tree_and_apply(
88        &mut self,
89        tree_name: &str,
90        consumer: &mut impl FnMut(&mut Tree<K, V>) -> Option<V>,
91    ) -> Option<V> {
92        self.open_tree(tree_name);
93        self.apply_tree(tree_name, consumer)
94    }
95}