1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#![ feature(map_first_last) ]
#![ feature(trait_alias) ]
#![ feature(async_stream) ]
#![ feature(write_all_vectored) ]
#![ feature(array_methods) ]
#![ feature(get_mut_unchecked) ]
use std::marker::PhantomData;
use std::path::{Path, PathBuf};
use bincode::Options;
pub mod sync_iter;
#[ cfg(feature="sync") ]
pub use sync_iter as iterate;
#[ cfg(not(feature="sync")) ]
pub mod async_iter;
#[ cfg(not(feature="sync")) ]
pub use async_iter as iterate;
mod values;
use values::Value;
mod sorted_table;
use sorted_table::Key;
mod tasks;
mod memtable;
mod logic;
use logic::DbLogic;
mod level;
mod wal;
mod manifest;
mod cond_var;
mod entry;
mod data_blocks;
mod index_blocks;
#[ cfg(not(feature="sync")) ]
mod async_api;
#[ cfg(not(feature="sync")) ]
pub use async_api::Database;
#[ cfg(feature="sync") ]
mod sync_api;
#[ cfg(feature="sync") ]
pub use sync_api::Database;
pub trait KV_Trait = Send+serde::Serialize+serde::de::DeserializeOwned+'static+Unpin+Clone;
pub struct WriteBatch<K: KV_Trait, V: KV_Trait> {
_marker: PhantomData<fn(K,V)>,
writes: Vec<(Key, Value)>
}
#[ derive(Clone, Debug) ]
pub enum WriteError {
Unknown
}
impl<K: KV_Trait, V: KV_Trait> WriteBatch<K, V> {
pub fn new() -> Self {
Self{
writes: Vec::new(),
_marker: PhantomData
}
}
pub fn put(&mut self, key: &K, value: &V) {
let enc = get_encoder();
self.writes.push(
(enc.serialize(key).unwrap(), enc.serialize(value).unwrap()));
}
}
impl<K: KV_Trait, V: KV_Trait> Default for WriteBatch<K, V> {
fn default() -> Self {
Self::new()
}
}
pub struct WriteOptions {
pub sync: bool
}
impl WriteOptions {
pub const fn new() -> Self {
Self{ sync: true }
}
}
impl Default for WriteOptions {
fn default() -> Self {
Self::new()
}
}
pub enum StartMode {
CreateOrOpen,
Open,
CreateOrOverride
}
pub struct Params {
pub db_path: PathBuf,
pub max_memtable_size: usize,
pub num_levels: usize,
pub max_open_files: usize,
pub max_key_block_size: usize,
pub block_restart_interval: usize,
}
impl Default for Params {
fn default() -> Self {
Self {
db_path: Path::new("./storage.lsm").to_path_buf(),
max_memtable_size: 64*1024,
num_levels: 5,
max_open_files: 1000,
max_key_block_size: 1024,
block_restart_interval: 16,
}
}
}
#[inline]
fn get_encoder() ->
bincode::config::WithOtherEndian<bincode::DefaultOptions, bincode::config::BigEndian> {
bincode::options().with_big_endian()
}