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
use std::{
ops::Deref,
sync::{atomic::AtomicUsize, Arc, RwLock},
};
use pagecache::FastMap8;
use super::*;
#[derive(Clone)]
pub struct Db {
context: Arc<Context>,
default: Arc<Tree>,
tenants: Arc<RwLock<FastMap8<Vec<u8>, Arc<Tree>>>>,
}
unsafe impl Send for Db {}
unsafe impl Sync for Db {}
impl Deref for Db {
type Target = Tree;
fn deref(&self) -> &Tree {
&self.default
}
}
impl Db {
pub fn start_default<P: AsRef<std::path::Path>>(path: P) -> Result<Db> {
let config = ConfigBuilder::new().path(path).build();
Self::start(config)
}
pub fn start(config: Config) -> Result<Db> {
let _measure = Measure::new(&M.tree_start);
let context = Arc::new(Context::start(config)?);
let tx = context.pagecache.begin()?;
let default = Arc::new(meta::open_tree(
context.clone(),
DEFAULT_TREE_ID.to_vec(),
&tx,
)?);
let ret = Db {
context: context.clone(),
default,
tenants: Arc::new(RwLock::new(FastMap8::default())),
};
let mut tenants = ret.tenants.write().unwrap();
for (id, root) in context.pagecache.meta(&tx)?.tenants().into_iter() {
let tree = Tree {
tree_id: id.clone(),
subscriptions: Arc::new(Subscriptions::default()),
context: context.clone(),
root: Arc::new(AtomicUsize::new(root)),
};
tenants.insert(id, Arc::new(tree));
}
drop(tenants);
#[cfg(feature = "event_log")]
context.event_log.meta_after_restart(
context
.pagecache
.meta(&tx)
.expect("should be able to get meta under test")
.clone(),
);
Ok(ret)
}
pub fn open_tree(&self, name: Vec<u8>) -> Result<Arc<Tree>> {
let tenants = self.tenants.read().unwrap();
if let Some(tree) = tenants.get(&name) {
return Ok(tree.clone());
}
drop(tenants);
let tx = self.context.pagecache.begin()?;
let mut tenants = self.tenants.write().unwrap();
let tree =
Arc::new(meta::open_tree(self.context.clone(), name.clone(), &tx)?);
tenants.insert(name, tree.clone());
drop(tenants);
Ok(tree)
}
pub fn drop_tree(&self, name: &[u8]) -> Result<bool> {
if name == DEFAULT_TREE_ID {
return Err(Error::Unsupported(
"cannot remove the core structures".into(),
));
}
trace!("dropping tree {:?}", name,);
let mut tenants = self.tenants.write().unwrap();
let tree = if let Some(tree) = tenants.remove(&*name) {
tree
} else {
return Ok(false);
};
let tx = self.context.pagecache.begin()?;
let mut root_id =
Some(self.context.pagecache.meta_pid_for_name(&name, &tx)?);
let leftmost_chain: Vec<PageId> = tree
.path_for_key(b"", &tx)?
.into_iter()
.map(|(id, _frag, _tp)| id)
.collect();
loop {
let res = self.context.pagecache.cas_root_in_meta(
name.to_vec(),
root_id,
None,
&tx,
)?;
if let Err(actual_root) = res {
root_id = actual_root;
} else {
break;
}
}
tree.root
.store(usize::max_value(), std::sync::atomic::Ordering::SeqCst);
drop(tenants);
tree.gc_pages(leftmost_chain)?;
tx.flush();
Ok(true)
}
pub fn was_recovered(&self) -> bool {
self.context.was_recovered()
}
pub fn generate_id(&self) -> Result<u64> {
self.context.generate_id()
}
}