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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
pub(crate) mod engines;
pub mod utils;
use engines::Engine;
use once_cell::sync::Lazy;
use parking_lot::Mutex;
use ruc::*;
use serde::{Deserialize, Serialize};
use std::{
env, fs,
mem::size_of,
path::{Path, PathBuf},
sync::atomic::{AtomicBool, Ordering},
};
use threadpool::ThreadPool;
pub const NULL: &[u8] = &[];
pub type RawBytes = Vec<u8>;
pub type RawKey = RawBytes;
pub type RawValue = RawBytes;
pub type Pre = u64;
pub const PREFIX_SIZE: usize = size_of::<Pre>();
pub type PreBytes = [u8; PREFIX_SIZE];
pub type BranchID = [u8; size_of::<u64>()];
pub type VersionID = [u8; size_of::<u64>()];
pub const VER_ID_MAX: VersionID = VersionIDBase::MAX.to_be_bytes();
pub type BranchIDBase = u64;
pub type VersionIDBase = u64;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct BranchName<'a>(pub &'a [u8]);
#[derive(Clone, Debug, Eq, PartialEq, Default, Serialize, Deserialize)]
pub struct BranchNameOwned(pub Vec<u8>);
#[derive(Clone, Copy, Debug, Eq, PartialEq, Default, Serialize, Deserialize)]
pub struct ParentBranchName<'a>(pub &'a [u8]);
#[derive(Clone, Debug, Eq, PartialEq, Default, Serialize, Deserialize)]
pub struct ParentBranchNameOwned(pub Vec<u8>);
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct VersionName<'a>(pub &'a [u8]);
#[derive(Clone, Debug, Eq, PartialEq, Default, Serialize, Deserialize)]
pub struct VersionNameOwned(pub Vec<u8>);
pub const KB: u64 = 1 << 10;
pub const MB: u64 = 1 << 20;
pub const GB: u64 = 1 << 30;
const RESERVED_ID_CNT: Pre = 4096_0000;
pub const BIGGEST_RESERVED_ID: Pre = RESERVED_ID_CNT - 1;
pub const NULL_ID: BranchID = (BIGGEST_RESERVED_ID as BranchIDBase).to_be_bytes();
pub const INITIAL_BRANCH_ID: BranchIDBase = 0;
pub const INITIAL_BRANCH_NAME: BranchName<'static> = BranchName(b"master");
pub const RESERVED_VERSION_NUM_DEFAULT: usize = 10;
const BASE_DIR_VAR: &str = "VSDB_BASE_DIR";
static VSDB_BASE_DIR: Lazy<Mutex<PathBuf>> = Lazy::new(|| Mutex::new(gen_data_dir()));
static VSDB_CUSTOM_DIR: Lazy<PathBuf> = Lazy::new(|| {
let mut d = VSDB_BASE_DIR.lock().clone();
d.push("__CUSTOM__");
pnk!(fs::create_dir_all(&d));
env::set_var("VSDB_CUSTOM_DIR", d.as_os_str());
d
});
#[cfg(any(
feature = "rocks_engine",
all(feature = "rocks_engine", feature = "sled_engine"),
all(not(feature = "rocks_engine"), not(feature = "sled_engine")),
))]
pub static VSDB: Lazy<VsDB<engines::RocksDB>> = Lazy::new(|| pnk!(VsDB::new()));
#[cfg(all(feature = "sled_engine", not(feature = "rocks_engine")))]
pub static VSDB: Lazy<VsDB<engines::Sled>> = Lazy::new(|| pnk!(VsDB::new()));
pub static TRASH_CLEANER: Lazy<Mutex<ThreadPool>> =
Lazy::new(|| Mutex::new(ThreadPool::new(1)));
#[macro_export(crate)]
macro_rules! parse_int {
($bytes: expr, $ty: ty) => {{
let array: [u8; std::mem::size_of::<$ty>()] = $bytes[..].try_into().unwrap();
<$ty>::from_be_bytes(array)
}};
}
#[macro_export(crate)]
macro_rules! parse_prefix {
($bytes: expr) => {
$crate::parse_int!($bytes, $crate::common::Pre)
};
}
pub struct VsDB<T: Engine> {
db: T,
}
impl<T: Engine> VsDB<T> {
#[inline(always)]
fn new() -> Result<Self> {
Ok(Self {
db: T::new().c(d!())?,
})
}
#[inline(always)]
pub fn alloc_br_id(&self) -> BranchIDBase {
self.db.alloc_br_id()
}
#[inline(always)]
pub fn alloc_ver_id(&self) -> VersionIDBase {
self.db.alloc_ver_id()
}
#[inline(always)]
fn flush(&self) {
self.db.flush()
}
}
#[inline(always)]
fn gen_data_dir() -> PathBuf {
let d = env::var(BASE_DIR_VAR)
.or_else(|_| env::var("HOME").map(|h| format!("{}/.vsdb", h)))
.unwrap_or_else(|_| "/tmp/.vsdb".to_owned());
pnk!(fs::create_dir_all(&d));
PathBuf::from(d)
}
#[inline(always)]
pub fn vsdb_get_custom_dir() -> &'static Path {
VSDB_CUSTOM_DIR.as_path()
}
#[inline(always)]
pub fn vsdb_get_base_dir() -> PathBuf {
VSDB_BASE_DIR.lock().clone()
}
#[inline(always)]
pub fn vsdb_set_base_dir(dir: impl AsRef<Path>) -> Result<()> {
static HAS_INITED: AtomicBool = AtomicBool::new(false);
if HAS_INITED.swap(true, Ordering::Relaxed) {
Err(eg!("VSDB has been initialized !!"))
} else {
env::set_var(BASE_DIR_VAR, dir.as_ref().as_os_str());
*VSDB_BASE_DIR.lock() = dir.as_ref().to_path_buf();
Ok(())
}
}
#[inline(always)]
pub fn vsdb_flush() {
VSDB.flush();
}
macro_rules! impl_from_for_name {
($target: tt) => {
impl<'a> From<&'a [u8]> for $target<'a> {
fn from(t: &'a [u8]) -> Self {
$target(t)
}
}
impl<'a> From<&'a Vec<u8>> for $target<'a> {
fn from(t: &'a Vec<u8>) -> Self {
$target(t.as_slice())
}
}
impl<'a> From<&'a str> for $target<'a> {
fn from(t: &'a str) -> Self {
$target(t.as_bytes())
}
}
impl<'a> From<&'a String> for $target<'a> {
fn from(t: &'a String) -> Self {
$target(t.as_bytes())
}
}
};
($target: tt, $($t: tt),+) => {
impl_from_for_name!($target);
impl_from_for_name!($($t), +);
};
}
impl_from_for_name!(BranchName, ParentBranchName, VersionName);
impl Default for BranchName<'static> {
fn default() -> Self {
INITIAL_BRANCH_NAME
}
}
impl BranchNameOwned {
#[inline(always)]
pub fn as_deref(&self) -> BranchName {
BranchName(&self.0)
}
}
impl<'a> From<&'a BranchNameOwned> for BranchName<'a> {
fn from(b: &'a BranchNameOwned) -> Self {
b.as_deref()
}
}
impl From<BranchName<'_>> for BranchNameOwned {
fn from(b: BranchName) -> Self {
BranchNameOwned(b.0.to_vec())
}
}
impl ParentBranchNameOwned {
#[inline(always)]
pub fn as_deref(&self) -> ParentBranchName {
ParentBranchName(&self.0)
}
}
impl<'a> From<&'a ParentBranchNameOwned> for ParentBranchName<'a> {
fn from(b: &'a ParentBranchNameOwned) -> Self {
b.as_deref()
}
}
impl From<ParentBranchName<'_>> for ParentBranchNameOwned {
fn from(b: ParentBranchName) -> Self {
ParentBranchNameOwned(b.0.to_vec())
}
}
impl VersionNameOwned {
#[inline(always)]
pub fn as_deref(&self) -> VersionName {
VersionName(&self.0)
}
}
impl<'a> From<&'a VersionNameOwned> for VersionName<'a> {
fn from(b: &'a VersionNameOwned) -> Self {
b.as_deref()
}
}
impl From<VersionName<'_>> for VersionNameOwned {
fn from(b: VersionName) -> Self {
VersionNameOwned(b.0.to_vec())
}
}