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
use std::collections::HashMap;
use std::path::Path;
use std::sync::{Arc, RwLock};
use std::sync::atomic::AtomicU64;
use crate::{Db, watch};
use super::Result;
/// Builder for the [`Db`](super::Db) instance.
pub struct Builder {
cache_size_bytes: Option<usize>,
}
impl Builder {
/// Similar to [redb::Builder::new()](https://docs.rs/redb/latest/redb/struct.Builder.html#method.new).
pub fn new() -> Self {
Self {
cache_size_bytes: None,
}
}
fn new_rdb_builder(&self) -> redb::Builder {
let mut redb_builder = redb::Builder::new();
if let Some(cache_size_bytes) = self.cache_size_bytes {
redb_builder.set_cache_size(cache_size_bytes);
}
redb_builder
}
fn new_redb(redb_database: redb::Database) -> Db {
Db {
instance: redb_database,
table_definitions: HashMap::new(),
watchers: Arc::new(RwLock::new(watch::Watchers::new())),
watchers_counter_id: AtomicU64::new(0),
}
}
/// Similar to [redb::Builder::set_cache_size()](https://docs.rs/redb/latest/redb/struct.Builder.html#method.set_cache_size).
pub fn set_cache_size(&mut self, bytes: usize) -> &mut Self {
self.cache_size_bytes = Some(bytes);
self
}
/// Creates a new `Db` instance using the given path.
///
/// Similar to [redb::Builder.create(...)](https://docs.rs/redb/latest/redb/struct.Builder.html#method.create)
pub fn create(&self,path: impl AsRef<Path>) -> Result<Db> {
let db = self.new_rdb_builder().create(path)?;
Ok(Self::new_redb(db))
}
/// Creates a new `Db` instance using [Builder::create] in order to create it in a temporary
/// directory with the given path.
///
/// Example: `builder::create_tmp('project/my_db')` will create the db to `/tmp/project/my_db`.
pub fn create_tmp(&self, path: impl AsRef<Path>) -> Result<Db> {
let tmp_dir = std::env::temp_dir();
let tmp_dir = tmp_dir.join(path);
self.create(tmp_dir.as_path())
}
/// Similar to [redb::Builder::open(...)](https://docs.rs/redb/latest/redb/struct.Builder.html#method.open)
pub fn open(&self, path: impl AsRef<Path>) -> Result<Db> {
let db = self.new_rdb_builder().open(path)?;
Ok(Self::new_redb(db))
}
/// Similar to [Builder::open] in order to open a database in a temporary directory with the
/// given path.
pub fn open_tmp(&self, path: impl AsRef<Path>) -> Result<Db> {
let tmp_dir = std::env::temp_dir();
let tmp_dir = tmp_dir.join(path);
self.open(tmp_dir.as_path())
}
}