btree_ondisk/
lib.rs

1//! Key/Value map implementation.
2//!
3//! Starting from a flat map of signle direct node which for small k/v set.
4//!
5//! Converted to btree based map when key exceed direct node's capacity.
6//!
7//! Individual btree node in the map can be load back from backend storage by [`BlockLoader`].
8use std::fmt;
9use std::io::Result;
10
11pub mod ondisk;
12pub mod node;
13pub mod btree;
14#[allow(dead_code)]
15#[cfg(not(feature = "sync-api"))]
16pub mod node_v1;
17#[allow(dead_code)]
18#[cfg(not(feature = "sync-api"))]
19pub mod btree_v1;
20#[allow(dead_code)]
21#[cfg(not(feature = "sync-api"))]
22pub mod direct_v1;
23mod direct;
24pub mod bmap;
25mod utils;
26mod loader;
27mod cache;
28pub use crate::loader::null::NullBlockLoader;
29pub use crate::loader::memory::MemoryBlockLoader;
30pub use crate::cache::null::NullNodeCache;
31
32#[maybe_async::maybe_async(AFIT)]
33#[allow(async_fn_in_trait)]
34pub trait VMap<K, V>
35    where
36        K: Copy + Default + fmt::Display + PartialOrd + Eq + std::hash::Hash,
37        V: Copy + Default + fmt::Display,
38{
39    async fn lookup(&self, key: &K, level: usize) -> Result<V>;
40    async fn lookup_contig(&self, key: &K, maxblocks: usize) -> Result<(V, usize)>;
41    async fn insert(&self, key: K, val: V) -> Result<()>;
42    async fn insert_or_update(&self, key: K, val: V) -> Result<Option<V>>;
43    async fn delete(&self, key: &K) -> Result<()>;
44    async fn seek_key(&self, start: &K) -> Result<K>;
45    async fn last_key(&self) -> Result<K>;
46}
47
48pub const VALID_EXTERNAL_ASSIGN_MASK: u64 = 0xFFFF_0000_0000_0000;
49pub const DEFAULT_CACHE_UNLIMITED: usize = usize::MAX; // unlimited by default
50
51pub trait NodeValue {
52    fn is_invalid(&self) -> bool;
53    fn invalid_value() -> Self;
54    fn is_valid_extern_assign(&self) -> bool;
55}
56
57pub trait BlockLoader<V> {
58    // return: potentially more meta blocks in vec
59    #[cfg(feature = "mt")]
60    fn read(&self, v: V, buf: &mut [u8], user_data: u32) -> impl std::future::Future<Output = Result<Vec<(V, Vec<u8>)>>> + Send;
61    #[cfg(not(feature = "mt"))]
62    fn read(&self, v: V, buf: &mut [u8], user_data: u32) -> impl std::future::Future<Output = Result<Vec<(V, Vec<u8>)>>>;
63    fn from_new_path(self, new_path: &str) -> Self;
64}
65
66impl NodeValue for u64 {
67    fn is_invalid(&self) -> bool {
68        self == &u64::MIN 
69    }
70
71    fn invalid_value() -> u64 {
72       u64::MIN
73    }
74
75    fn is_valid_extern_assign(&self) -> bool {
76        (self & VALID_EXTERNAL_ASSIGN_MASK) != 0
77    }
78}
79
80impl<V: Send> BlockLoader<V> for u64 {
81    async fn read(&self, v: V, buf: &mut [u8], user_data: u32) -> Result<Vec<(V, Vec<u8>)>> {
82        let _ = v;
83        let _ = buf;
84        let _ = user_data;
85        Ok(Vec::new())
86    }
87
88    fn from_new_path(self, new_path: &str) -> Self {
89        let _ = new_path;
90        self.clone()
91    }
92}
93
94pub use crate::cache::NodeTieredCacheStats;
95
96pub trait NodeCache<P> {
97    fn push(&self, p: &P, data: &[u8]);
98    #[cfg(feature = "mt")]
99    fn load(&self, p: P, data: &mut [u8]) -> impl std::future::Future<Output = Result<bool>> + Send;
100    #[cfg(not(feature = "mt"))]
101    fn load(&self, p: P, data: &mut [u8]) -> impl std::future::Future<Output = Result<bool>>;
102    fn invalid(&self, p: &P);
103    fn evict(&self);
104    fn get_stats(&self) -> cache::NodeTieredCacheStats;
105    fn shutdown(&self);
106}