-
Notifications
You must be signed in to change notification settings - Fork 4
/
external.rs
392 lines (373 loc) · 12.3 KB
/
external.rs
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
use bifrost::utils::async_locks::{Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard};
use bifrost::utils::fut_exec::wait;
use client::AsyncClient;
use core::borrow::BorrowMut;
use dovahkiin::types::custom_types::id::Id;
use dovahkiin::types::custom_types::map::Map;
use dovahkiin::types::type_id_of;
use dovahkiin::types::value::ToValue;
use futures::Future;
use index::btree::*;
use index::EntryKey;
use index::Slice;
use itertools::Itertools;
use owning_ref::{OwningHandle, OwningRef, RcRef};
use ram::cell::Cell;
use ram::schema::{Field, Schema};
use ram::types::*;
use std::cell::Ref;
use std::cell::RefCell;
use std::cell::RefMut;
use std::cell::UnsafeCell;
use std::collections::BTreeSet;
use std::collections::HashMap;
use std::marker::PhantomData;
use std::mem;
use std::ops::Deref;
use std::ops::DerefMut;
use std::rc::Rc;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use utils::lru_cache::LRUCache;
const PAGE_SCHEMA: &'static str = "NEB_BTREE_PAGE";
const KEYS_FIELD: &'static str = "keys";
const NEXT_FIELD: &'static str = "next";
const PREV_FIELD: &'static str = "prev";
lazy_static! {
static ref KEYS_KEY_HASH: u64 = key_hash(KEYS_FIELD);
static ref NEXT_PAGE_KEY_HASH: u64 = key_hash(NEXT_FIELD);
static ref PREV_PAGE_KEY_HASH: u64 = key_hash(PREV_FIELD);
static ref PAGE_SCHEMA_ID: u32 = key_hash(PAGE_SCHEMA) as u32;
}
pub struct ExtNode<KS, PS>
where
KS: Slice<EntryKey> + Debug + 'static,
PS: Slice<NodeCellRef> + 'static,
{
pub id: Id,
pub keys: KS,
pub next: NodeCellRef,
pub prev: NodeCellRef,
pub len: usize,
pub dirty: bool,
pub right_bound: EntryKey,
mark: PhantomData<PS>,
}
pub struct ExtNodeSplit<KS, PS>
where
KS: Slice<EntryKey> + Debug + 'static,
PS: Slice<NodeCellRef> + 'static,
{
pub node_2: ExtNode<KS, PS>,
pub keys_1_len: usize,
}
impl<KS, PS> ExtNode<KS, PS>
where
KS: Slice<EntryKey> + Debug + 'static,
PS: Slice<NodeCellRef> + 'static,
{
pub fn new(id: Id, right_bound: EntryKey) -> ExtNode<KS, PS> {
ExtNode {
id,
keys: KS::init(),
next: Node::<KS, PS>::none_ref(),
prev: Node::<KS, PS>::none_ref(),
len: 0,
dirty: false,
right_bound,
mark: PhantomData,
}
}
pub fn to_cell(&self) -> Cell {
let mut value = Value::Map(Map::new());
let prev_id = read_node(&self.prev, |node: &NodeReadHandler<KS, PS>| {
node.extnode().id
});
let next_id = read_node(&self.next, |node: &NodeReadHandler<KS, PS>| {
node.extnode().id
});
value[*NEXT_PAGE_KEY_HASH] = Value::Id(prev_id);
value[*PREV_PAGE_KEY_HASH] = Value::Id(next_id);
value[*KEYS_KEY_HASH] = self.keys.as_slice_immute()[..self.len]
.iter()
.map(|key| SmallBytes::from_vec(key.as_slice().to_vec()))
.collect_vec()
.value();
Cell::new_with_id(*PAGE_SCHEMA_ID, &self.id, value)
}
pub fn search(&self, key: &EntryKey) -> usize {
self.keys.as_slice_immute()[..self.len]
.binary_search(key)
.unwrap_or_else(|i| i)
}
pub fn remove_at(&mut self, pos: usize) {
let mut cached_len = &mut self.len;
debug!(
"Removing from external pos {}, len {}, key {:?}",
pos,
cached_len,
self.keys.as_slice()[pos]
);
self.keys.remove_at(pos, cached_len);
}
pub fn split_insert(
&mut self,
key: EntryKey,
pos: usize,
self_ref: &NodeCellRef,
self_next: &mut NodeWriteGuard<KS, PS>,
tree: &BPlusTree<KS, PS>,
) -> (NodeCellRef, EntryKey) {
// cached.dump();
let pivot = self.len / 2;
let new_page_id = tree.new_page_id();
let mut keys_1 = &mut self.keys;
let mut keys_2 = keys_1.split_at_pivot(pivot, self.len);
let mut keys_1_len = pivot;
let mut keys_2_len = self.len - pivot;
// modify next node point previous to new node
insert_into_split(
key,
keys_1,
&mut keys_2,
&mut keys_1_len,
&mut keys_2_len,
pos,
);
let pivot_key = keys_2.as_slice()[0].clone();
let extnode_2: ExtNode<KS, PS> = ExtNode {
id: new_page_id,
keys: keys_2,
next: self.next.clone(),
prev: self_ref.clone(),
len: keys_2_len,
dirty: true,
right_bound: mem::replace(&mut self.right_bound, pivot_key.clone()),
mark: PhantomData,
};
debug_assert!(pivot_key > smallvec!(0));
debug_assert!(
&pivot_key > &keys_1.as_slice()[keys_1_len - 1],
"{:?} / {:?} @ {}",
pivot_key,
&keys_1.as_slice()[keys_1_len - 1],
pos
);
debug_assert!(read_unchecked::<KS, PS>(&extnode_2.prev).is_ext());
self.len = keys_1_len;
debug!(
"Split to left len {}, right len {}, right prev id: {:?}",
self.len,
extnode_2.len,
read_unchecked::<KS, PS>(&extnode_2.prev).ext_id()
);
let node_2 = NodeCellRef::new(Node::with_external(extnode_2));
{
if !self_next.is_none() {
self_next.extnode_mut().prev = node_2.clone();
}
}
self.next = node_2.clone();
(node_2, pivot_key)
}
pub fn insert(
&mut self,
key: &EntryKey,
tree: &BPlusTree<KS, PS>,
self_ref: &NodeCellRef,
parent: &NodeCellRef,
) -> Option<NodeSplit<KS, PS>> {
let key = key.clone();
let pos = self.search(&key);
debug_assert!(self.len <= KS::slice_len());
debug_assert!(pos <= self.len);
if self.len == KS::slice_len() {
// need to split
debug!("insert to external with split, key {:?}, pos {}", key, pos);
let mut self_next: NodeWriteGuard<KS, PS> = write_node(&self.next);
let mut parent_latch: NodeWriteGuard<KS, PS> = write_node(parent);
let (node_2, pivot_key) = self.split_insert(key, pos, self_ref, &mut self_next, tree);
Some(NodeSplit {
new_right_node: node_2,
pivot: pivot_key,
parent_latch,
left_node_latch: NodeWriteGuard::default(),
})
} else {
debug!("insert to external without split at {}, key {:?}", pos, key);
self.keys.insert_at(key, pos, &mut self.len);
None
}
}
pub fn merge_with(&mut self, right: &mut Self) {
debug!(
"Merge external node, left len {}:{:?}, right len {}:{:?}",
self.len, self.keys, right.len, right.keys
);
let self_len = self.len;
let new_len = self.len + right.len;
debug_assert!(new_len <= KS::slice_len());
for i in self.len..new_len {
mem::swap(
&mut self.keys.as_slice()[i],
&mut right.keys.as_slice()[i - self_len],
);
}
self.len = new_len;
}
pub fn remove_contains(&mut self, set: &mut BTreeSet<EntryKey>) {
let remaining_keys = {
let remaining = self.keys.as_slice()[..self.len]
.iter()
.filter(|&k| !set.remove(k))
.collect_vec();
if remaining.len() == self.len {
return;
}
remaining.into_iter().cloned().collect_vec()
};
let mut self_key_slice = self.keys.as_slice();
self.len = remaining_keys.len();
for (i, k_ref) in remaining_keys.into_iter().enumerate() {
self_key_slice[i] = k_ref;
}
}
pub fn merge_sort(&mut self, right: &[&EntryKey]) {
debug!("Merge sort have right nodes {:?}", right);
let self_len_before_merge = self.len;
debug_assert!(self_len_before_merge + right.len() <= KS::slice_len());
let mut pos = 0;
let mut left_pos = 0;
let mut right_pos = 0;
let mut left_keys = mem::replace(&mut self.keys, KS::init());
let mut left = left_keys.as_slice();
while left_pos < self.len && right_pos < right.len() {
let left_key = &left[left_pos];
let right_key = right[right_pos];
if left_key < right_key {
self.keys.as_slice()[pos] = left_key.clone();
left_pos += 1;
} else {
self.keys.as_slice()[pos] = right_key.clone();
right_pos += 1;
}
pos += 1;
}
for key in &left[left_pos..self.len] {
self.keys.as_slice()[pos] = key.clone();
pos += 1;
left_pos += 1;
}
for key in &right[right_pos..] {
self.keys.as_slice()[pos] = (*key).clone();
pos += 1;
right_pos += 1;
}
debug!(
"Merge sorted have keys {:?}",
&self.keys.as_slice_immute()[..pos]
);
self.len = pos;
self.dirty = true;
debug_assert_eq!(self_len_before_merge, left_pos);
debug_assert_eq!(right.len(), right_pos);
debug_assert_eq!(self.len, self_len_before_merge + right.len());
debug!(
"Merge sorted page have keys: {:?}",
&self.keys.as_slice_immute()[..self.len]
)
}
pub fn dump(&self) {
debug!("Dumping {:?}, keys {}", self.id, self.len);
for i in 0..KS::slice_len() {
debug!("{}\t- {:?}", i, self.keys.as_slice_immute()[i]);
}
}
pub fn is_dirty(&self) -> bool {
unimplemented!()
}
}
pub fn page_schema() -> Schema {
Schema {
id: *PAGE_SCHEMA_ID,
name: String::from(PAGE_SCHEMA),
key_field: None,
str_key_field: None,
is_dynamic: false,
fields: Field::new(
"*",
0,
false,
false,
Some(vec![
Field::new(NEXT_FIELD, type_id_of(Type::Id), false, false, None),
Field::new(PREV_FIELD, type_id_of(Type::Id), false, false, None),
Field::new(KEYS_FIELD, type_id_of(Type::SmallBytes), false, true, None),
]),
),
}
}
pub struct NodeCache<KS, PS>
where
KS: Slice<EntryKey> + Debug + 'static,
PS: Slice<NodeCellRef> + 'static,
{
nodes: RefCell<HashMap<Id, NodeCellRef>>,
storage: Arc<AsyncClient>,
marker: PhantomData<(KS, PS)>,
}
impl<KS, PS> NodeCache<KS, PS>
where
KS: Slice<EntryKey> + Debug + 'static,
PS: Slice<NodeCellRef> + 'static,
{
pub fn new(neb_client: &Arc<AsyncClient>) -> Self {
NodeCache {
nodes: RefCell::new(HashMap::new()),
storage: neb_client.clone(),
marker: PhantomData,
}
}
pub fn get(&self, id: &Id) -> NodeCellRef {
if id.is_unit_id() {
return NodeCellRef::new(Node::<KS, PS>::with_none());
}
let mut nodes = self.nodes.borrow_mut();
nodes
.entry(*id)
.or_insert_with(|| {
let cell = self.storage.read_cell(*id).wait().unwrap().unwrap();
NodeCellRef::new(Node::with_external(self.extnode_from_cell(cell)))
})
.clone()
}
fn extnode_from_cell(&self, cell: Cell) -> ExtNode<KS, PS> {
let cell_id = cell.id();
let cell_version = cell.header.version;
let next = cell.data[*NEXT_PAGE_KEY_HASH].Id().unwrap();
let prev = cell.data[*PREV_PAGE_KEY_HASH].Id().unwrap();
let keys = &cell.data[*KEYS_KEY_HASH];
let keys_len = keys.len().unwrap();
let keys_array = if let Value::PrimArray(PrimitiveArray::SmallBytes(ref array)) = keys {
array
} else {
panic!()
};
let mut key_slice = KS::init();
let mut key_count = 0;
for (i, key_val) in keys_array.iter().enumerate() {
key_slice.as_slice()[i] = EntryKey::from(key_val.as_slice());
key_count += 1;
}
ExtNode {
id: cell_id,
keys: key_slice,
next: self.get(next),
prev: self.get(prev),
len: key_count,
dirty: false,
right_bound: max_entry_key(), // TODO: assign a real one on reconstructing
mark: PhantomData,
}
}
}