@@ -16,7 +16,8 @@
Loading
16 16
use crate::index::{
17 17
    config::{IndexType, IndexTypeId, Indexes, ValueMode, INDEX_DATA_PREFIX, INDEX_META_PREFIX},
18 18
    keeper::{IndexKeeper, IndexRawIter},
19 -
    tree::{Index, PageIter, PageIterBack, Value},
19 +
    nodes::{PageIter, PageIterBack, Value},
20 +
    tree::Index,
20 21
};
21 22
use crate::transaction::{
22 23
    PreparedState, SyncMode, Transaction, TxRead,

@@ -1,7 +1,8 @@
Loading
1 1
use crate::index::{
2 2
    config::{ByteVec, IndexOrd, IndexType, Indexes, ValueMode, WrapperType},
3 +
    nodes::{compare, Node, NodeRef, PageIter, PageIterBack, Value},
3 4
    serialization::{deserialize, serialize},
4 -
    tree::{compare, IndexApply, KeyChanges, Node, NodeRef, PageIter, PageIterBack, Value, ValueChange as TreeValue},
5 +
    tree::{IndexApply, KeyChanges, ValueChange as TreeValue},
5 6
};
6 7
use crate::{
7 8
    error::{PRes, PersyError},

@@ -52,7 +52,7 @@
Loading
52 52
    error::{PRes, PersyError},
53 53
    id::{IndexId, PersyId, SegmentId, ToIndexId, ToSegmentId},
54 54
    index::config::{ByteVec, IndexType, IndexTypeId, ValueMode},
55 -
    index::tree::Value,
55 +
    index::nodes::Value,
56 56
    persy::{IndexInfo, RecoverStatus},
57 57
};
58 58
use crate::{

@@ -1,7 +1,7 @@
Loading
1 1
use crate::index::{
2 2
    config::{IndexType, ValueMode},
3 3
    keeper::IndexTransactionKeeper,
4 -
    tree::Value,
4 +
    nodes::Value,
5 5
};
6 6
use crate::{
7 7
    address::Address,

@@ -0,0 +1,797 @@
Loading
1 +
use crate::{
2 +
    error::PersyError,
3 +
    index::{
4 +
        config::{IndexType, ValueMode},
5 +
        tree::PosRef,
6 +
    },
7 +
    persy::RecRef,
8 +
    PRes,
9 +
};
10 +
use std::{
11 +
    cmp::Ordering,
12 +
    iter::{Peekable, Rev},
13 +
    ops::Bound,
14 +
    vec::IntoIter,
15 +
};
16 +
17 +
pub type NodeRef = RecRef;
18 +
#[derive(Clone)]
19 +
pub enum Node<K, V> {
20 +
    NODE(Nodes<K>),
21 +
    LEAF(Leaf<K, V>),
22 +
}
23 +
24 +
impl<K: IndexType, V: IndexType> Node<K, V> {
25 +
    pub fn merge_right(&mut self, k: K, node: &mut Node<K, V>) {
26 +
        match self {
27 +
            Node::NODE(n) => match node {
28 +
                Node::NODE(n1) => {
29 +
                    n.merge_right(k, n1);
30 +
                }
31 +
                Node::LEAF(_) => {
32 +
                    panic!("impossible merge a leaf to node");
33 +
                }
34 +
            },
35 +
            Node::LEAF(l) => match node {
36 +
                Node::NODE(_) => {
37 +
                    panic!("impossible merge a node to leaf");
38 +
                }
39 +
                Node::LEAF(l1) => {
40 +
                    l.merge_right(l1);
41 +
                }
42 +
            },
43 +
        }
44 +
    }
45 +
    pub fn len(&self) -> usize {
46 +
        match self {
47 +
            Node::NODE(n) => n.len(),
48 +
            Node::LEAF(l) => l.len(),
49 +
        }
50 +
    }
51 +
    pub fn split(&mut self, top_limit: usize) -> Vec<(K, Node<K, V>)> {
52 +
        match self {
53 +
            Node::NODE(n) => n.split(top_limit).into_iter().map(|x| (x.0, Node::NODE(x.1))).collect(),
54 +
            Node::LEAF(l) => l.split(top_limit).into_iter().map(|x| (x.0, Node::LEAF(x.1))).collect(),
55 +
        }
56 +
    }
57 +
    pub fn get_prev(&self) -> &Option<K> {
58 +
        match self {
59 +
            Node::NODE(n) => &n.prev,
60 +
            Node::LEAF(l) => &l.prev,
61 +
        }
62 +
    }
63 +
    pub fn get_next(&self) -> &Option<K> {
64 +
        match self {
65 +
            Node::NODE(n) => &n.next,
66 +
            Node::LEAF(l) => &l.next,
67 +
        }
68 +
    }
69 +
    pub fn check_range(&self, k: &K) -> bool {
70 +
        match self {
71 +
            Node::NODE(n) => n.check_range(k),
72 +
            Node::LEAF(l) => l.check_range(k),
73 +
        }
74 +
    }
75 +
}
76 +
77 +
pub(crate) fn compare<T: IndexType>(first: &T, second: &T) -> Ordering {
78 +
    first.cmp(second)
79 +
}
80 +
81 +
#[derive(Clone)]
82 +
pub struct Nodes<K> {
83 +
    pub keys: Vec<K>,
84 +
    pub pointers: Vec<NodeRef>,
85 +
    pub prev: Option<K>,
86 +
    pub next: Option<K>,
87 +
}
88 +
89 +
impl<K: IndexType> Nodes<K> {
90 +
    pub fn new_from_split(left: NodeRef, values: &[(K, NodeRef)]) -> Nodes<K> {
91 +
        let keys = values.iter().map(|z| z.0.clone()).collect();
92 +
        let mut pointers: Vec<NodeRef> = values.iter().map(|z| z.1.clone()).collect();
93 +
        pointers.insert(0, left);
94 +
        Nodes {
95 +
            keys,
96 +
            pointers,
97 +
            prev: None,
98 +
            next: None,
99 +
        }
100 +
    }
101 +
102 +
    pub fn add(&mut self, pos: usize, k: &K, node_ref: NodeRef) {
103 +
        self.keys.insert(pos, k.clone());
104 +
        self.pointers.insert(pos + 1, node_ref);
105 +
    }
106 +
107 +
    pub fn find(&self, k: &K) -> PosRef<K> {
108 +
        match self.keys.binary_search_by(|x| compare(x, k)) {
109 +
            Ok(index) => {
110 +
                let sibling = Some(self.pointers[index].clone());
111 +
                PosRef::new(k, index + 1, self.pointers[index + 1].clone(), sibling)
112 +
            }
113 +
            Err(index) => {
114 +
                let sibling = if index > 0 {
115 +
                    Some(self.pointers[index - 1].clone())
116 +
                } else if self.pointers.len() > index + 1 {
117 +
                    Some(self.pointers[index + 1].clone())
118 +
                } else {
119 +
                    None
120 +
                };
121 +
                PosRef::new(k, index, self.pointers[index].clone(), sibling)
122 +
            }
123 +
        }
124 +
    }
125 +
    pub fn find_write(&self, k: &K) -> Option<PosRef<K>> {
126 +
        let pos = self.find(k);
127 +
        if pos.pos == 0 {
128 +
            if let Some(pk) = &self.prev {
129 +
                if compare(k, pk) == Ordering::Less {
130 +
                    return None;
131 +
                }
132 +
            }
133 +
        } else if pos.pos == self.pointers.len() {
134 +
            if let Some(nk) = &self.next {
135 +
                if compare(k, nk) != Ordering::Less {
136 +
                    return None;
137 +
                }
138 +
            }
139 +
        }
140 +
141 +
        Some(pos)
142 +
    }
143 +
144 +
    pub fn get_key(&self, pos: usize) -> K {
145 +
        self.keys[pos].clone()
146 +
    }
147 +
148 +
    pub fn get(&self, pos: usize) -> NodeRef {
149 +
        self.pointers[pos].clone()
150 +
    }
151 +
152 +
    pub fn insert_after(&mut self, pos: usize, values: &mut Vec<(K, NodeRef)>) {
153 +
        values.reverse();
154 +
        for val in values.iter() {
155 +
            self.add(pos, &val.0, val.1.clone());
156 +
        }
157 +
    }
158 +
159 +
    pub fn remove(&mut self, pos: usize) -> Option<NodeRef> {
160 +
        if pos < self.pointers.len() {
161 +
            self.keys.remove(pos - 1);
162 +
            Some(self.pointers.remove(pos))
163 +
        } else {
164 +
            None
165 +
        }
166 +
    }
167 +
168 +
    pub fn len(&self) -> usize {
169 +
        self.pointers.len()
170 +
    }
171 +
172 +
    pub fn split(&mut self, max: usize) -> Vec<(K, Nodes<K>)> {
173 +
        let mut split_result: Vec<(K, Nodes<K>)> = Vec::new();
174 +
        let size = self.keys.len();
175 +
        let n_split = size / max;
176 +
        let split_offset = size / (n_split + 1) + 1;
177 +
        let mut others = self.keys.split_off(split_offset - 1);
178 +
        let mut other_pointers = self.pointers.split_off(split_offset);
179 +
180 +
        let pre_next = self.next.clone();
181 +
        while others.len() > max {
182 +
            let new = others.split_off(split_offset);
183 +
            let new_pointers = other_pointers.split_off(split_offset);
184 +
            let key = others.remove(0);
185 +
            if let Some((_, ref mut x)) = split_result.last_mut() {
186 +
                x.next = Some(key.clone());
187 +
            } else {
188 +
                self.next = Some(key.clone());
189 +
            }
190 +
            let leaf = Nodes {
191 +
                keys: others,
192 +
                pointers: other_pointers,
193 +
                prev: Some(key.clone()),
194 +
                next: None,
195 +
            };
196 +
            split_result.push((key, leaf));
197 +
            others = new;
198 +
            other_pointers = new_pointers;
199 +
        }
200 +
201 +
        let key = others.remove(0);
202 +
        if let Some((_, ref mut x)) = split_result.last_mut() {
203 +
            x.next = Some(key.clone());
204 +
        } else {
205 +
            self.next = Some(key.clone());
206 +
        }
207 +
        let leaf = Nodes {
208 +
            keys: others,
209 +
            pointers: other_pointers,
210 +
            prev: Some(key.clone()),
211 +
            next: pre_next,
212 +
        };
213 +
        split_result.push((key, leaf));
214 +
        split_result
215 +
    }
216 +
217 +
    #[allow(dead_code)]
218 +
    pub fn merge_left(&mut self, owner: K, nodes: &mut Nodes<K>) {
219 +
        let mut keys = std::mem::replace(&mut nodes.keys, Vec::new());
220 +
        let mut pointers = std::mem::replace(&mut nodes.pointers, Vec::new());
221 +
        keys.push(owner);
222 +
        keys.append(&mut self.keys);
223 +
        pointers.append(&mut self.pointers);
224 +
        self.keys = keys;
225 +
        self.pointers = pointers;
226 +
    }
227 +
228 +
    pub fn merge_right(&mut self, owner: K, nodes: &mut Nodes<K>) {
229 +
        self.keys.push(owner);
230 +
        self.keys.append(&mut nodes.keys);
231 +
        self.pointers.append(&mut nodes.pointers);
232 +
        self.next = nodes.next.clone();
233 +
    }
234 +
235 +
    fn check_range(&self, k: &K) -> bool {
236 +
        if let Some(x) = &self.prev {
237 +
            if compare(x, k) == Ordering::Greater {
238 +
                return false;
239 +
            }
240 +
        }
241 +
        if let Some(x) = &self.next {
242 +
            if compare(x, k) == Ordering::Less {
243 +
                return false;
244 +
            }
245 +
        }
246 +
        true
247 +
    }
248 +
}
249 +
250 +
/// The associated value to the index key
251 +
#[derive(Clone, PartialEq, Debug)]
252 +
pub enum Value<V> {
253 +
    /// A cluster of values
254 +
    CLUSTER(Vec<V>),
255 +
    /// A single value entry
256 +
    SINGLE(V),
257 +
}
258 +
259 +
impl<V> IntoIterator for Value<V> {
260 +
    type Item = V;
261 +
    type IntoIter = IntoIter<V>;
262 +
263 +
    fn into_iter(self) -> IntoIter<V> {
264 +
        match self {
265 +
            Value::SINGLE(v) => vec![v].into_iter(),
266 +
            Value::CLUSTER(v) => v.into_iter(),
267 +
        }
268 +
    }
269 +
}
270 +
271 +
pub struct PageIter<K: IndexType, V: IndexType> {
272 +
    pub iter: Peekable<IntoIter<LeafEntry<K, V>>>,
273 +
}
274 +
275 +
pub struct PageIterBack<K: IndexType, V: IndexType> {
276 +
    pub iter: Peekable<Rev<IntoIter<LeafEntry<K, V>>>>,
277 +
}
278 +
279 +
#[derive(Clone)]
280 +
pub struct Leaf<K, V> {
281 +
    pub entries: Vec<LeafEntry<K, V>>,
282 +
    pub prev: Option<K>,
283 +
    pub next: Option<K>,
284 +
}
285 +
286 +
#[derive(Clone)]
287 +
pub struct LeafEntry<K, V> {
288 +
    pub key: K,
289 +
    pub value: Value<V>,
290 +
}
291 +
292 +
impl<K: IndexType, V: IndexType> Leaf<K, V> {
293 +
    pub fn new() -> Leaf<K, V> {
294 +
        Leaf {
295 +
            entries: Vec::new(),
296 +
            prev: None,
297 +
            next: None,
298 +
        }
299 +
    }
300 +
301 +
    pub fn add(&mut self, pos: usize, k: &K, v: &V, _value_mode: ValueMode) {
302 +
        self.entries.insert(
303 +
            pos,
304 +
            LeafEntry {
305 +
                key: k.clone(),
306 +
                value: Value::SINGLE(v.clone()),
307 +
            },
308 +
        );
309 +
    }
310 +
311 +
    pub fn find<'a>(&'a self, k: &K) -> Result<(K, Value<V>), usize> {
312 +
        self.entries
313 +
            .binary_search_by(|n| compare(&n.key, k))
314 +
            .map(|index| (self.entries[index].key.clone(), self.entries[index].value.clone()))
315 +
    }
316 +
317 +
    pub fn iter_from(&self, bound: Bound<&K>) -> IntoIter<LeafEntry<K, V>> {
318 +
        let index = match bound {
319 +
            Bound::Included(k) => match self.entries.binary_search_by(|n| compare(&n.key, k)) {
320 +
                Ok(index) => index,
321 +
                Err(index) => index,
322 +
            },
323 +
            Bound::Excluded(k) => match self.entries.binary_search_by(|n| compare(&n.key, k)) {
324 +
                Ok(index) => index + 1,
325 +
                Err(index) => index,
326 +
            },
327 +
            Bound::Unbounded => 0,
328 +
        };
329 +
        self.entries[index..].to_vec().into_iter()
330 +
    }
331 +
332 +
    pub fn back_iter_from(&self, bound: Bound<&K>) -> Rev<IntoIter<LeafEntry<K, V>>> {
333 +
        let index = match bound {
334 +
            Bound::Included(k) => match self.entries.binary_search_by(|n| compare(&n.key, k)) {
335 +
                Ok(index) => index + 1,
336 +
                Err(index) => index,
337 +
            },
338 +
            Bound::Excluded(k) => match self.entries.binary_search_by(|n| compare(&n.key, k)) {
339 +
                Ok(index) => index,
340 +
                Err(index) => index,
341 +
            },
342 +
            Bound::Unbounded => self.len(),
343 +
        };
344 +
        self.entries[..index].to_vec().into_iter().rev()
345 +
    }
346 +
347 +
    pub fn insert_or_update(&mut self, k: &K, v: &V, value_mode: ValueMode, index_name: &str) -> PRes<()> {
348 +
        match self.entries.binary_search_by(|n| compare(&n.key, k)) {
349 +
            Ok(index) => {
350 +
                let entry = &mut self.entries[index];
351 +
                match value_mode {
352 +
                    ValueMode::REPLACE => {
353 +
                        entry.value = Value::SINGLE(v.clone());
354 +
                    }
355 +
                    ValueMode::EXCLUSIVE => match entry.value {
356 +
                        Value::SINGLE(ref ev) => {
357 +
                            if compare(ev, v) != Ordering::Equal {
358 +
                                return Err(PersyError::IndexDuplicateKey(index_name.to_string(), format!("{}", k)));
359 +
                            }
360 +
                        }
361 +
                        _ => unreachable!("Exclusive leafs never have cluster values"),
362 +
                    },
363 +
                    ValueMode::CLUSTER => {
364 +
                        let mut new_value = None;
365 +
                        match entry.value {
366 +
                            Value::SINGLE(ref ev) => {
367 +
                                if compare(ev, v) != Ordering::Equal {
368 +
                                    new_value = Some(Value::CLUSTER(vec![ev.clone(), v.clone()]));
369 +
                                }
370 +
                            }
371 +
                            Value::CLUSTER(ref mut cl) => {
372 +
                                if let Err(index) = cl.binary_search_by(|x| compare(x, v)) {
373 +
                                    cl.insert(index, v.clone());
374 +
                                }
375 +
                            }
376 +
                        }
377 +
                        if let Some(v) = new_value {
378 +
                            entry.value = v;
379 +
                        }
380 +
                    }
381 +
                }
382 +
            }
383 +
            Err(index) => self.add(index, k, v, value_mode),
384 +
        }
385 +
        Ok(())
386 +
    }
387 +
388 +
    pub fn remove(&mut self, k: &K, v: &Option<V>) -> bool {
389 +
        match self.entries.binary_search_by(|n| compare(&n.key, k)) {
390 +
            Ok(index) => {
391 +
                if let Some(rv) = v {
392 +
                    let mut removed = false;
393 +
                    let remove_entry = {
394 +
                        let mut new_value = None;
395 +
                        let entry = &mut self.entries[index];
396 +
                        let remove_entry = match &mut entry.value {
397 +
                            Value::SINGLE(val) => {
398 +
                                if compare(val, rv) == Ordering::Equal {
399 +
                                    removed = true;
400 +
                                    true
401 +
                                } else {
402 +
                                    false
403 +
                                }
404 +
                            }
405 +
                            Value::CLUSTER(ref mut cl) => {
406 +
                                if let Ok(index) = cl.binary_search_by(|x| compare(x, rv)) {
407 +
                                    removed = true;
408 +
                                    cl.remove(index);
409 +
                                }
410 +
                                if cl.len() == 1 {
411 +
                                    new_value = Some(Value::SINGLE(cl.pop().unwrap()));
412 +
                                    false
413 +
                                } else {
414 +
                                    cl.is_empty()
415 +
                                }
416 +
                            }
417 +
                        };
418 +
                        if let Some(new) = new_value {
419 +
                            entry.value = new;
420 +
                        }
421 +
                        remove_entry
422 +
                    };
423 +
                    if remove_entry {
424 +
                        self.entries.remove(index);
425 +
                    }
426 +
                    removed
427 +
                } else {
428 +
                    self.entries.remove(index);
429 +
                    true
430 +
                }
431 +
            }
432 +
            Err(_) => false,
433 +
        }
434 +
    }
435 +
436 +
    pub fn len(&self) -> usize {
437 +
        self.entries.len()
438 +
    }
439 +
440 +
    pub fn split(&mut self, max: usize) -> Vec<(K, Leaf<K, V>)> {
441 +
        let mut split_result: Vec<(K, Leaf<K, V>)> = Vec::new();
442 +
        let size = self.entries.len();
443 +
        let n_split = size / max;
444 +
        let split_offset = size / (n_split + 1) + 1;
445 +
        let mut others = self.entries.split_off(split_offset);
446 +
        let pre_next = self.next.clone();
447 +
        while others.len() > max {
448 +
            let new = others.split_off(split_offset);
449 +
            let key = others[0].key.clone();
450 +
            if let Some((_, ref mut x)) = split_result.last_mut() {
451 +
                x.next = Some(key.clone());
452 +
            } else {
453 +
                self.next = Some(key.clone());
454 +
            }
455 +
            let leaf = Leaf {
456 +
                entries: others,
457 +
                prev: Some(key.clone()),
458 +
                next: None,
459 +
            };
460 +
            split_result.push((key, leaf));
461 +
            others = new;
462 +
        }
463 +
464 +
        let key = others[0].key.clone();
465 +
        if let Some((_, ref mut x)) = split_result.last_mut() {
466 +
            x.next = Some(key.clone());
467 +
        } else {
468 +
            self.next = Some(key.clone());
469 +
        }
470 +
        let leaf = Leaf {
471 +
            entries: others,
472 +
            prev: Some(key.clone()),
473 +
            next: pre_next,
474 +
        };
475 +
        split_result.push((key, leaf));
476 +
        split_result
477 +
    }
478 +
479 +
    #[allow(dead_code)]
480 +
    pub fn merge_left(&mut self, leaf: &mut Leaf<K, V>) {
481 +
        let mut entries = std::mem::replace(&mut leaf.entries, Vec::new());
482 +
        entries.append(&mut self.entries);
483 +
        self.entries = entries;
484 +
    }
485 +
486 +
    pub fn merge_right(&mut self, leaf: &mut Leaf<K, V>) {
487 +
        self.entries.append(&mut leaf.entries);
488 +
        self.next = leaf.next.clone();
489 +
    }
490 +
    fn check_range(&self, k: &K) -> bool {
491 +
        if let Some(x) = &self.prev {
492 +
            if compare(x, k) == Ordering::Greater {
493 +
                return false;
494 +
            }
495 +
        }
496 +
        if let Some(x) = &self.next {
497 +
            if compare(x, k) == Ordering::Less {
498 +
                return false;
499 +
            }
500 +
        }
501 +
        true
502 +
    }
503 +
}
504 +
505 +
#[cfg(test)]
506 +
mod tests {
507 +
    use super::{Leaf, NodeRef, Nodes, PosRef, Value, ValueMode};
508 +
    use crate::id::RecRef;
509 +
    use rand::random;
510 +
511 +
    fn random_pointer() -> NodeRef {
512 +
        RecRef::new(random::<u64>(), random::<u32>())
513 +
    }
514 +
515 +
    #[test]
516 +
    fn single_node_add_test() {
517 +
        let val1 = random_pointer();
518 +
        let val2 = random_pointer();
519 +
        let val3 = random_pointer();
520 +
        let val4 = random_pointer();
521 +
        let val5 = random_pointer();
522 +
        let val6 = random_pointer();
523 +
        let mut node = Nodes::new_from_split(val1, &[(0, val2)]);
524 +
        let pos = node.find(&2).pos;
525 +
        node.add(pos, &2, val3.clone());
526 +
        let pos = node.find(&5).pos;
527 +
        node.add(pos, &5, val4.clone());
528 +
        let pos = node.find(&6).pos;
529 +
        node.add(pos, &6, val5);
530 +
        let pos = node.find(&4).pos;
531 +
        node.add(pos, &4, val6.clone());
532 +
533 +
        let found = node.find(&4);
534 +
        assert_eq!(found.pos, 3);
535 +
        //If i search for 4 i get the one on the left of 4 so the value of 2 that is val3
536 +
        assert_eq!(found.node_ref, val6);
537 +
538 +
        let found = node.find(&5);
539 +
        assert_eq!(found.pos, 4);
540 +
        //If i search for 5 i get the one on the left of 5 so the value of 4 that is val6
541 +
        assert_eq!(found.node_ref, val4);
542 +
543 +
        let found = node.find(&3);
544 +
        //If i search for a value that do not exist i get the position of the value at is right
545 +
        //that is value 4 position 2
546 +
        assert_eq!(found.pos, 2);
547 +
        //If i search for 3 i get the value at the left of 4 that is val3
548 +
        assert_eq!(found.node_ref, val3);
549 +
    }
550 +
551 +
    #[test]
552 +
    fn single_leaf_insert_test() {
553 +
        let mut leaf = Leaf::new();
554 +
        for n in 0..50 {
555 +
            leaf.insert_or_update(&n, &n, ValueMode::REPLACE, "aa")
556 +
                .expect("insert is ok");
557 +
        }
558 +
        let res = leaf.find(&10);
559 +
        assert_eq!(Ok((10, Value::SINGLE(10))), res);
560 +
561 +
        let res = leaf.find(&60);
562 +
        assert_eq!(Err(50), res);
563 +
    }
564 +
565 +
    #[test]
566 +
    fn single_leaf_cluster_insert_test() {
567 +
        let mut leaf = Leaf::new();
568 +
        leaf.insert_or_update(&10, &1, ValueMode::CLUSTER, "aa")
569 +
            .expect("insert is ok");
570 +
        leaf.insert_or_update(&10, &2, ValueMode::CLUSTER, "aa")
571 +
            .expect("insert is ok");
572 +
        let res = leaf.find(&10);
573 +
        assert_eq!(Ok((10, Value::CLUSTER(vec![1, 2]))), res);
574 +
    }
575 +
576 +
    #[test]
577 +
    fn leaf_cluster_remove_test() {
578 +
        let mut leaf = Leaf::new();
579 +
        leaf.insert_or_update(&10, &1, ValueMode::CLUSTER, "aa")
580 +
            .expect("insert is ok");
581 +
        leaf.insert_or_update(&10, &2, ValueMode::CLUSTER, "aa")
582 +
            .expect("insert is ok");
583 +
        assert!(leaf.remove(&10, &Some(2)));
584 +
        let res = leaf.find(&10);
585 +
        assert_eq!(Ok((10, Value::SINGLE(1))), res);
586 +
    }
587 +
588 +
    #[test]
589 +
    fn leaf_cluster_remove_not_exist_value_test() {
590 +
        let mut leaf = Leaf::new();
591 +
        leaf.insert_or_update(&10, &1, ValueMode::CLUSTER, "aa")
592 +
            .expect("insert is ok");
593 +
        leaf.insert_or_update(&10, &2, ValueMode::CLUSTER, "aa")
594 +
            .expect("insert is ok");
595 +
        assert!(!leaf.remove(&10, &Some(10)));
596 +
        let res = leaf.find(&10);
597 +
        assert_eq!(Ok((10, Value::CLUSTER(vec![1, 2]))), res);
598 +
    }
599 +
600 +
    #[test]
601 +
    fn leaf_single_delete_not_exist_value_test() {
602 +
        let mut leaf = Leaf::new();
603 +
        leaf.insert_or_update(&10, &1, ValueMode::EXCLUSIVE, "aa")
604 +
            .expect("insert is ok");
605 +
        assert!(!leaf.remove(&10, &Some(10)));
606 +
        let res = leaf.find(&10);
607 +
        assert_eq!(Ok((10, Value::SINGLE(1))), res);
608 +
    }
609 +
610 +
    #[test]
611 +
    fn leaf_duplicate_key_test() {
612 +
        let mut leaf = Leaf::new();
613 +
        leaf.insert_or_update(&10, &1, ValueMode::EXCLUSIVE, "aa")
614 +
            .expect("insert is ok");
615 +
        let res = leaf.insert_or_update(&10, &2, ValueMode::EXCLUSIVE, "aa");
616 +
        assert!(res.is_err());
617 +
    }
618 +
619 +
    #[test]
620 +
    fn test_leaf_split() {
621 +
        let mut leaf = Leaf::new();
622 +
623 +
        for n in 0..103 {
624 +
            leaf.insert_or_update(&n, &n, ValueMode::REPLACE, "aa")
625 +
                .expect("insert is ok");
626 +
        }
627 +
628 +
        let res = leaf.split(21);
629 +
        assert_eq!(leaf.len(), 21);
630 +
        assert_eq!(res[0].1.len(), 21);
631 +
        assert_eq!(res[1].1.len(), 21);
632 +
        assert_eq!(res[2].1.len(), 21);
633 +
        assert_eq!(res[3].1.len(), 19);
634 +
    }
635 +
636 +
    #[test]
637 +
    fn test_node_split() {
638 +
        let mut node = Nodes::new_from_split(random_pointer(), &[(0, random_pointer())]);
639 +
        for n in 1..103 {
640 +
            let pos = node.find(&n).pos;
641 +
            node.add(pos, &n, random_pointer());
642 +
        }
643 +
644 +
        let res = node.split(21);
645 +
        assert_eq!(node.len(), 21);
646 +
        assert_eq!(node.pointers.len(), 21);
647 +
        assert_eq!(node.keys.len(), 20);
648 +
        assert_eq!(res[0].1.len(), 21);
649 +
        assert_eq!(res[0].1.pointers.len(), 21);
650 +
        assert_eq!(res[0].1.keys.len(), 20);
651 +
        assert_eq!(res[1].1.len(), 21);
652 +
        assert_eq!(res[1].1.pointers.len(), 21);
653 +
        assert_eq!(res[1].1.keys.len(), 20);
654 +
        assert_eq!(res[2].1.len(), 21);
655 +
        assert_eq!(res[2].1.pointers.len(), 21);
656 +
        assert_eq!(res[2].1.keys.len(), 20);
657 +
        assert_eq!(res[3].1.len(), 20);
658 +
        assert_eq!(res[3].1.pointers.len(), 20);
659 +
        assert_eq!(res[3].1.keys.len(), 19);
660 +
    }
661 +
662 +
    #[test]
663 +
    fn test_remove_from_leaf() {
664 +
        let mut leaf = Leaf::new();
665 +
        for n in 0..50 {
666 +
            leaf.insert_or_update(&n, &n, ValueMode::REPLACE, "aa")
667 +
                .expect("insert is ok");
668 +
        }
669 +
        assert!(leaf.remove(&10, &Some(10)));
670 +
        assert!(!leaf.remove(&100, &Some(100)));
671 +
        assert_eq!(leaf.len(), 49);
672 +
        let res = leaf.find(&10);
673 +
        assert_eq!(Err(10), res);
674 +
    }
675 +
676 +
    #[test]
677 +
    fn test_remove_from_node() {
678 +
        //TODO: check why the remove of 10 make to point to 9
679 +
        let mut node = Nodes::new_from_split(random_pointer(), &[(0, random_pointer())]);
680 +
        let mut keep_pre = None;
681 +
        let mut keep = None;
682 +
        for n in 1..50 {
683 +
            let pos = node.find(&n).pos;
684 +
            let point = random_pointer();
685 +
            if n == 8 {
686 +
                keep_pre = Some(point.clone());
687 +
            }
688 +
            if n == 9 {
689 +
                keep = Some(point.clone());
690 +
            }
691 +
            node.add(pos, &n, point);
692 +
        }
693 +
        let pos = node.find(&10).pos;
694 +
        node.remove(pos);
695 +
        assert_eq!(node.len(), 50);
696 +
        let res = node.find(&10);
697 +
        assert_eq!(PosRef::new(&10, 10, keep.unwrap(), keep_pre), res);
698 +
    }
699 +
700 +
    #[test]
701 +
    fn test_merge_leaf() {
702 +
        let mut leaf = Leaf::new();
703 +
        let mut leaf2 = Leaf::new();
704 +
        for n in 0..20 {
705 +
            leaf.insert_or_update(&n, &n, ValueMode::REPLACE, "aa")
706 +
                .expect("insert is ok");
707 +
        }
708 +
709 +
        for n in 20..40 {
710 +
            leaf2
711 +
                .insert_or_update(&n, &n, ValueMode::REPLACE, "aa")
712 +
                .expect("insert is ok");
713 +
        }
714 +
        leaf.merge_right(&mut leaf2);
715 +
        assert_eq!(leaf.len(), 40);
716 +
        assert_eq!(leaf2.len(), 0);
717 +
        let res = leaf.find(&35);
718 +
        assert_eq!(res, Ok((35, Value::SINGLE(35))));
719 +
720 +
        let mut leaf = Leaf::new();
721 +
        let mut leaf2 = Leaf::new();
722 +
        for n in 20..40 {
723 +
            leaf.insert_or_update(&n, &n, ValueMode::REPLACE, "aa")
724 +
                .expect("insert is ok");
725 +
        }
726 +
727 +
        for n in 0..20 {
728 +
            leaf2
729 +
                .insert_or_update(&n, &n, ValueMode::REPLACE, "aa")
730 +
                .expect("insert is ok");
731 +
        }
732 +
        leaf.merge_left(&mut leaf2);
733 +
        assert_eq!(leaf.len(), 40);
734 +
        assert_eq!(leaf2.len(), 0);
735 +
        let res = leaf.find(&35);
736 +
        assert_eq!(res, Ok((35, Value::SINGLE(35))));
737 +
    }
738 +
739 +
    #[test]
740 +
    fn test_merge_nodes() {
741 +
        let mut node = Nodes::new_from_split(random_pointer(), &[(0, random_pointer())]);
742 +
        for n in 1..20 {
743 +
            let pos = node.find(&n).pos;
744 +
            let point = random_pointer();
745 +
            node.add(pos, &n, point);
746 +
        }
747 +
748 +
        let mut node2 = Nodes::new_from_split(random_pointer(), &[(21, random_pointer())]);
749 +
        let mut keep_pre = None;
750 +
        let mut keep = None;
751 +
        for n in 22..40 {
752 +
            let pos = node2.find(&n).pos;
753 +
            let point = random_pointer();
754 +
            if n == 25 {
755 +
                keep_pre = Some(point.clone());
756 +
            }
757 +
            if n == 26 {
758 +
                keep = Some(point.clone());
759 +
            }
760 +
            node2.add(pos, &n, point);
761 +
        }
762 +
763 +
        node.merge_right(20, &mut node2);
764 +
        assert_eq!(node.len(), 41);
765 +
        assert_eq!(node2.len(), 0);
766 +
        let res = node.find(&26);
767 +
        assert_eq!(PosRef::new(&26, 27, keep.unwrap(), keep_pre), res);
768 +
769 +
        let mut node = Nodes::new_from_split(random_pointer(), &[(21, random_pointer())]);
770 +
        let mut keep_pre = None;
771 +
        let mut keep = None;
772 +
        for n in 22..40 {
773 +
            let pos = node.find(&n).pos;
774 +
            let point = random_pointer();
775 +
            if n == 25 {
776 +
                keep_pre = Some(point.clone());
777 +
            }
778 +
            if n == 26 {
779 +
                keep = Some(point.clone());
780 +
            }
781 +
            node.add(pos, &n, point);
782 +
        }
783 +
784 +
        let mut node2 = Nodes::new_from_split(random_pointer(), &[(0, random_pointer())]);
785 +
        for n in 1..20 {
786 +
            let pos = node2.find(&n).pos;
787 +
            let point = random_pointer();
788 +
            node2.add(pos, &n, point);
789 +
        }
790 +
791 +
        node.merge_left(20, &mut node2);
792 +
        assert_eq!(node.len(), 41);
793 +
        assert_eq!(node2.len(), 0);
794 +
        let res = node.find(&26);
795 +
        assert_eq!(PosRef::new(&26, 27, keep.unwrap(), keep_pre), res);
796 +
    }
797 +
}

@@ -4,7 +4,7 @@
Loading
4 4
    index::{
5 5
        config::{ByteVec, IndexType, IndexTypeId},
6 6
        keeper::Container,
7 -
        tree::{Leaf, LeafEntry, Node, NodeRef, Nodes, Value},
7 +
        nodes::{Leaf, LeafEntry, Node, NodeRef, Nodes, Value},
8 8
    },
9 9
};
10 10
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
@@ -353,7 +353,7 @@
Loading
353 353
    use crate::id::{PersyId, RecRef};
354 354
    use crate::index::{
355 355
        config::{ByteVec, IndexType, ValueMode},
356 -
        tree::{compare, Leaf, Node, NodeRef, Nodes, Value},
356 +
        nodes::{compare, Leaf, Node, NodeRef, Nodes, Value},
357 357
    };
358 358
    use rand::random;
359 359
    use std::{cmp::Ordering, fmt::Debug};

@@ -1,506 +1,12 @@
Loading
1 1
use crate::{
2 -
    error::{PRes, PersyError},
3 -
    id::RecRef,
2 +
    error::PRes,
4 3
    index::{
5 4
        config::{IndexType, ValueMode},
6 5
        keeper::{IndexKeeper, IndexModify},
6 +
        nodes::{Leaf, Node, NodeRef, Nodes, PageIter, PageIterBack, Value},
7 7
    },
8 8
};
9 -
pub type NodeRef = RecRef;
10 -
use std::{
11 -
    cmp::Ordering,
12 -
    iter::{Peekable, Rev},
13 -
    ops::Bound,
14 -
    rc::Rc,
15 -
    vec::IntoIter,
16 -
};
17 -
18 -
#[derive(Clone)]
19 -
pub enum Node<K, V> {
20 -
    NODE(Nodes<K>),
21 -
    LEAF(Leaf<K, V>),
22 -
}
23 -
24 -
impl<K: IndexType, V: IndexType> Node<K, V> {
25 -
    pub fn merge_right(&mut self, k: K, node: &mut Node<K, V>) {
26 -
        match self {
27 -
            Node::NODE(n) => match node {
28 -
                Node::NODE(n1) => {
29 -
                    n.merge_right(k, n1);
30 -
                }
31 -
                Node::LEAF(_) => {
32 -
                    panic!("impossible merge a leaf to node");
33 -
                }
34 -
            },
35 -
            Node::LEAF(l) => match node {
36 -
                Node::NODE(_) => {
37 -
                    panic!("impossible merge a node to leaf");
38 -
                }
39 -
                Node::LEAF(l1) => {
40 -
                    l.merge_right(l1);
41 -
                }
42 -
            },
43 -
        }
44 -
    }
45 -
    pub fn len(&self) -> usize {
46 -
        match self {
47 -
            Node::NODE(n) => n.len(),
48 -
            Node::LEAF(l) => l.len(),
49 -
        }
50 -
    }
51 -
    pub fn split(&mut self, top_limit: usize) -> Vec<(K, Node<K, V>)> {
52 -
        match self {
53 -
            Node::NODE(n) => n.split(top_limit).into_iter().map(|x| (x.0, Node::NODE(x.1))).collect(),
54 -
            Node::LEAF(l) => l.split(top_limit).into_iter().map(|x| (x.0, Node::LEAF(x.1))).collect(),
55 -
        }
56 -
    }
57 -
    pub fn get_prev(&self) -> &Option<K> {
58 -
        match self {
59 -
            Node::NODE(n) => &n.prev,
60 -
            Node::LEAF(l) => &l.prev,
61 -
        }
62 -
    }
63 -
    pub fn get_next(&self) -> &Option<K> {
64 -
        match self {
65 -
            Node::NODE(n) => &n.next,
66 -
            Node::LEAF(l) => &l.next,
67 -
        }
68 -
    }
69 -
    pub fn check_range(&self, k: &K) -> bool {
70 -
        match self {
71 -
            Node::NODE(n) => n.check_range(k),
72 -
            Node::LEAF(l) => l.check_range(k),
73 -
        }
74 -
    }
75 -
}
76 -
77 -
pub(crate) fn compare<T: IndexType>(first: &T, second: &T) -> Ordering {
78 -
    first.cmp(second)
79 -
}
80 -
81 -
#[derive(Clone)]
82 -
pub struct Nodes<K> {
83 -
    pub keys: Vec<K>,
84 -
    pub pointers: Vec<NodeRef>,
85 -
    pub prev: Option<K>,
86 -
    pub next: Option<K>,
87 -
}
88 -
89 -
impl<K: IndexType> Nodes<K> {
90 -
    pub fn new_from_split(left: NodeRef, values: &[(K, NodeRef)]) -> Nodes<K> {
91 -
        let keys = values.iter().map(|z| z.0.clone()).collect();
92 -
        let mut pointers: Vec<NodeRef> = values.iter().map(|z| z.1.clone()).collect();
93 -
        pointers.insert(0, left);
94 -
        Nodes {
95 -
            keys,
96 -
            pointers,
97 -
            prev: None,
98 -
            next: None,
99 -
        }
100 -
    }
101 -
102 -
    pub fn add(&mut self, pos: usize, k: &K, node_ref: NodeRef) {
103 -
        self.keys.insert(pos, k.clone());
104 -
        self.pointers.insert(pos + 1, node_ref);
105 -
    }
106 -
107 -
    pub fn find(&self, k: &K) -> PosRef<K> {
108 -
        match self.keys.binary_search_by(|x| compare(x, k)) {
109 -
            Ok(index) => {
110 -
                let sibling = Some(self.pointers[index].clone());
111 -
                PosRef::new(k, index + 1, self.pointers[index + 1].clone(), sibling)
112 -
            }
113 -
            Err(index) => {
114 -
                let sibling = if index > 0 {
115 -
                    Some(self.pointers[index - 1].clone())
116 -
                } else if self.pointers.len() > index + 1 {
117 -
                    Some(self.pointers[index + 1].clone())
118 -
                } else {
119 -
                    None
120 -
                };
121 -
                PosRef::new(k, index, self.pointers[index].clone(), sibling)
122 -
            }
123 -
        }
124 -
    }
125 -
    pub fn find_write(&self, k: &K) -> Option<PosRef<K>> {
126 -
        let pos = self.find(k);
127 -
        if pos.pos == 0 {
128 -
            if let Some(pk) = &self.prev {
129 -
                if compare(k, pk) == Ordering::Less {
130 -
                    return None;
131 -
                }
132 -
            }
133 -
        } else if pos.pos == self.pointers.len() {
134 -
            if let Some(nk) = &self.next {
135 -
                if compare(k, nk) != Ordering::Less {
136 -
                    return None;
137 -
                }
138 -
            }
139 -
        }
140 -
141 -
        Some(pos)
142 -
    }
143 -
144 -
    pub fn get_key(&self, pos: usize) -> K {
145 -
        self.keys[pos].clone()
146 -
    }
147 -
148 -
    pub fn get(&self, pos: usize) -> NodeRef {
149 -
        self.pointers[pos].clone()
150 -
    }
151 -
152 -
    pub fn insert_after(&mut self, pos: usize, values: &mut Vec<(K, NodeRef)>) {
153 -
        values.reverse();
154 -
        for val in values.iter() {
155 -
            self.add(pos, &val.0, val.1.clone());
156 -
        }
157 -
    }
158 -
159 -
    pub fn remove(&mut self, pos: usize) -> Option<NodeRef> {
160 -
        if pos < self.pointers.len() {
161 -
            self.keys.remove(pos - 1);
162 -
            Some(self.pointers.remove(pos))
163 -
        } else {
164 -
            None
165 -
        }
166 -
    }
167 -
168 -
    pub fn len(&self) -> usize {
169 -
        self.pointers.len()
170 -
    }
171 -
172 -
    pub fn split(&mut self, max: usize) -> Vec<(K, Nodes<K>)> {
173 -
        let mut split_result: Vec<(K, Nodes<K>)> = Vec::new();
174 -
        let size = self.keys.len();
175 -
        let n_split = size / max;
176 -
        let split_offset = size / (n_split + 1) + 1;
177 -
        let mut others = self.keys.split_off(split_offset - 1);
178 -
        let mut other_pointers = self.pointers.split_off(split_offset);
179 -
180 -
        let pre_next = self.next.clone();
181 -
        while others.len() > max {
182 -
            let new = others.split_off(split_offset);
183 -
            let new_pointers = other_pointers.split_off(split_offset);
184 -
            let key = others.remove(0);
185 -
            if let Some((_, ref mut x)) = split_result.last_mut() {
186 -
                x.next = Some(key.clone());
187 -
            } else {
188 -
                self.next = Some(key.clone());
189 -
            }
190 -
            let leaf = Nodes {
191 -
                keys: others,
192 -
                pointers: other_pointers,
193 -
                prev: Some(key.clone()),
194 -
                next: None,
195 -
            };
196 -
            split_result.push((key, leaf));
197 -
            others = new;
198 -
            other_pointers = new_pointers;
199 -
        }
200 -
201 -
        let key = others.remove(0);
202 -
        if let Some((_, ref mut x)) = split_result.last_mut() {
203 -
            x.next = Some(key.clone());
204 -
        } else {
205 -
            self.next = Some(key.clone());
206 -
        }
207 -
        let leaf = Nodes {
208 -
            keys: others,
209 -
            pointers: other_pointers,
210 -
            prev: Some(key.clone()),
211 -
            next: pre_next,
212 -
        };
213 -
        split_result.push((key, leaf));
214 -
        split_result
215 -
    }
216 -
217 -
    #[allow(dead_code)]
218 -
    pub fn merge_left(&mut self, owner: K, nodes: &mut Nodes<K>) {
219 -
        let mut keys = std::mem::replace(&mut nodes.keys, Vec::new());
220 -
        let mut pointers = std::mem::replace(&mut nodes.pointers, Vec::new());
221 -
        keys.push(owner);
222 -
        keys.append(&mut self.keys);
223 -
        pointers.append(&mut self.pointers);
224 -
        self.keys = keys;
225 -
        self.pointers = pointers;
226 -
    }
227 -
228 -
    pub fn merge_right(&mut self, owner: K, nodes: &mut Nodes<K>) {
229 -
        self.keys.push(owner);
230 -
        self.keys.append(&mut nodes.keys);
231 -
        self.pointers.append(&mut nodes.pointers);
232 -
        self.next = nodes.next.clone();
233 -
    }
234 -
235 -
    fn check_range(&self, k: &K) -> bool {
236 -
        if let Some(x) = &self.prev {
237 -
            if compare(x, k) == Ordering::Greater {
238 -
                return false;
239 -
            }
240 -
        }
241 -
        if let Some(x) = &self.next {
242 -
            if compare(x, k) == Ordering::Less {
243 -
                return false;
244 -
            }
245 -
        }
246 -
        true
247 -
    }
248 -
}
249 -
250 -
/// The associated value to the index key
251 -
#[derive(Clone, PartialEq, Debug)]
252 -
pub enum Value<V> {
253 -
    /// A cluster of values
254 -
    CLUSTER(Vec<V>),
255 -
    /// A single value entry
256 -
    SINGLE(V),
257 -
}
258 -
259 -
impl<V> IntoIterator for Value<V> {
260 -
    type Item = V;
261 -
    type IntoIter = IntoIter<V>;
262 -
263 -
    fn into_iter(self) -> IntoIter<V> {
264 -
        match self {
265 -
            Value::SINGLE(v) => vec![v].into_iter(),
266 -
            Value::CLUSTER(v) => v.into_iter(),
267 -
        }
268 -
    }
269 -
}
270 -
271 -
pub struct PageIter<K: IndexType, V: IndexType> {
272 -
    pub iter: Peekable<IntoIter<LeafEntry<K, V>>>,
273 -
}
274 -
275 -
pub struct PageIterBack<K: IndexType, V: IndexType> {
276 -
    pub iter: Peekable<Rev<IntoIter<LeafEntry<K, V>>>>,
277 -
}
278 -
279 -
#[derive(Clone)]
280 -
pub struct Leaf<K, V> {
281 -
    pub entries: Vec<LeafEntry<K, V>>,
282 -
    pub prev: Option<K>,
283 -
    pub next: Option<K>,
284 -
}
285 -
286 -
#[derive(Clone)]
287 -
pub struct LeafEntry<K, V> {
288 -
    pub key: K,
289 -
    pub value: Value<V>,
290 -
}
291 -
292 -
impl<K: IndexType, V: IndexType> Leaf<K, V> {
293 -
    pub fn new() -> Leaf<K, V> {
294 -
        Leaf {
295 -
            entries: Vec::new(),
296 -
            prev: None,
297 -
            next: None,
298 -
        }
299 -
    }
300 -
301 -
    pub fn add(&mut self, pos: usize, k: &K, v: &V, _value_mode: ValueMode) {
302 -
        self.entries.insert(
303 -
            pos,
304 -
            LeafEntry {
305 -
                key: k.clone(),
306 -
                value: Value::SINGLE(v.clone()),
307 -
            },
308 -
        );
309 -
    }
310 -
311 -
    pub fn find<'a>(&'a self, k: &K) -> Result<(K, Value<V>), usize> {
312 -
        self.entries
313 -
            .binary_search_by(|n| compare(&n.key, k))
314 -
            .map(|index| (self.entries[index].key.clone(), self.entries[index].value.clone()))
315 -
    }
316 -
317 -
    pub fn iter_from(&self, bound: Bound<&K>) -> IntoIter<LeafEntry<K, V>> {
318 -
        let index = match bound {
319 -
            Bound::Included(k) => match self.entries.binary_search_by(|n| compare(&n.key, k)) {
320 -
                Ok(index) => index,
321 -
                Err(index) => index,
322 -
            },
323 -
            Bound::Excluded(k) => match self.entries.binary_search_by(|n| compare(&n.key, k)) {
324 -
                Ok(index) => index + 1,
325 -
                Err(index) => index,
326 -
            },
327 -
            Bound::Unbounded => 0,
328 -
        };
329 -
        self.entries[index..].to_vec().into_iter()
330 -
    }
331 -
332 -
    pub fn back_iter_from(&self, bound: Bound<&K>) -> Rev<IntoIter<LeafEntry<K, V>>> {
333 -
        let index = match bound {
334 -
            Bound::Included(k) => match self.entries.binary_search_by(|n| compare(&n.key, k)) {
335 -
                Ok(index) => index + 1,
336 -
                Err(index) => index,
337 -
            },
338 -
            Bound::Excluded(k) => match self.entries.binary_search_by(|n| compare(&n.key, k)) {
339 -
                Ok(index) => index,
340 -
                Err(index) => index,
341 -
            },
342 -
            Bound::Unbounded => self.len(),
343 -
        };
344 -
        self.entries[..index].to_vec().into_iter().rev()
345 -
    }
346 -
347 -
    pub fn insert_or_update(&mut self, k: &K, v: &V, value_mode: ValueMode, index_name: &str) -> PRes<()> {
348 -
        match self.entries.binary_search_by(|n| compare(&n.key, k)) {
349 -
            Ok(index) => {
350 -
                let entry = &mut self.entries[index];
351 -
                match value_mode {
352 -
                    ValueMode::REPLACE => {
353 -
                        entry.value = Value::SINGLE(v.clone());
354 -
                    }
355 -
                    ValueMode::EXCLUSIVE => match entry.value {
356 -
                        Value::SINGLE(ref ev) => {
357 -
                            if compare(ev, v) != Ordering::Equal {
358 -
                                return Err(PersyError::IndexDuplicateKey(index_name.to_string(), format!("{}", k)));
359 -
                            }
360 -
                        }
361 -
                        _ => unreachable!("Exclusive leafs never have cluster values"),
362 -
                    },
363 -
                    ValueMode::CLUSTER => {
364 -
                        let mut new_value = None;
365 -
                        match entry.value {
366 -
                            Value::SINGLE(ref ev) => {
367 -
                                if compare(ev, v) != Ordering::Equal {
368 -
                                    new_value = Some(Value::CLUSTER(vec![ev.clone(), v.clone()]));
369 -
                                }
370 -
                            }
371 -
                            Value::CLUSTER(ref mut cl) => {
372 -
                                if let Err(index) = cl.binary_search_by(|x| compare(x, v)) {
373 -
                                    cl.insert(index, v.clone());
374 -
                                }
375 -
                            }
376 -
                        }
377 -
                        if let Some(v) = new_value {
378 -
                            entry.value = v;
379 -
                        }
380 -
                    }
381 -
                }
382 -
            }
383 -
            Err(index) => self.add(index, k, v, value_mode),
384 -
        }
385 -
        Ok(())
386 -
    }
387 -
388 -
    pub fn remove(&mut self, k: &K, v: &Option<V>) -> bool {
389 -
        match self.entries.binary_search_by(|n| compare(&n.key, k)) {
390 -
            Ok(index) => {
391 -
                if let Some(rv) = v {
392 -
                    let mut removed = false;
393 -
                    let remove_entry = {
394 -
                        let mut new_value = None;
395 -
                        let entry = &mut self.entries[index];
396 -
                        let remove_entry = match &mut entry.value {
397 -
                            Value::SINGLE(val) => {
398 -
                                if compare(val, rv) == Ordering::Equal {
399 -
                                    removed = true;
400 -
                                    true
401 -
                                } else {
402 -
                                    false
403 -
                                }
404 -
                            }
405 -
                            Value::CLUSTER(ref mut cl) => {
406 -
                                if let Ok(index) = cl.binary_search_by(|x| compare(x, rv)) {
407 -
                                    removed = true;
408 -
                                    cl.remove(index);
409 -
                                }
410 -
                                if cl.len() == 1 {
411 -
                                    new_value = Some(Value::SINGLE(cl.pop().unwrap()));
412 -
                                    false
413 -
                                } else {
414 -
                                    cl.is_empty()
415 -
                                }
416 -
                            }
417 -
                        };
418 -
                        if let Some(new) = new_value {
419 -
                            entry.value = new;
420 -
                        }
421 -
                        remove_entry
422 -
                    };
423 -
                    if remove_entry {
424 -
                        self.entries.remove(index);
425 -
                    }
426 -
                    removed
427 -
                } else {
428 -
                    self.entries.remove(index);
429 -
                    true
430 -
                }
431 -
            }
432 -
            Err(_) => false,
433 -
        }
434 -
    }
435 -
436 -
    pub fn len(&self) -> usize {
437 -
        self.entries.len()
438 -
    }
439 -
440 -
    pub fn split(&mut self, max: usize) -> Vec<(K, Leaf<K, V>)> {
441 -
        let mut split_result: Vec<(K, Leaf<K, V>)> = Vec::new();
442 -
        let size = self.entries.len();
443 -
        let n_split = size / max;
444 -
        let split_offset = size / (n_split + 1) + 1;
445 -
        let mut others = self.entries.split_off(split_offset);
446 -
        let pre_next = self.next.clone();
447 -
        while others.len() > max {
448 -
            let new = others.split_off(split_offset);
449 -
            let key = others[0].key.clone();
450 -
            if let Some((_, ref mut x)) = split_result.last_mut() {
451 -
                x.next = Some(key.clone());
452 -
            } else {
453 -
                self.next = Some(key.clone());
454 -
            }
455 -
            let leaf = Leaf {
456 -
                entries: others,
457 -
                prev: Some(key.clone()),
458 -
                next: None,
459 -
            };
460 -
            split_result.push((key, leaf));
461 -
            others = new;
462 -
        }
463 -
464 -
        let key = others[0].key.clone();
465 -
        if let Some((_, ref mut x)) = split_result.last_mut() {
466 -
            x.next = Some(key.clone());
467 -
        } else {
468 -
            self.next = Some(key.clone());
469 -
        }
470 -
        let leaf = Leaf {
471 -
            entries: others,
472 -
            prev: Some(key.clone()),
473 -
            next: pre_next,
474 -
        };
475 -
        split_result.push((key, leaf));
476 -
        split_result
477 -
    }
478 -
479 -
    #[allow(dead_code)]
480 -
    pub fn merge_left(&mut self, leaf: &mut Leaf<K, V>) {
481 -
        let mut entries = std::mem::replace(&mut leaf.entries, Vec::new());
482 -
        entries.append(&mut self.entries);
483 -
        self.entries = entries;
484 -
    }
485 -
486 -
    pub fn merge_right(&mut self, leaf: &mut Leaf<K, V>) {
487 -
        self.entries.append(&mut leaf.entries);
488 -
        self.next = leaf.next.clone();
489 -
    }
490 -
    fn check_range(&self, k: &K) -> bool {
491 -
        if let Some(x) = &self.prev {
492 -
            if compare(x, k) == Ordering::Greater {
493 -
                return false;
494 -
            }
495 -
        }
496 -
        if let Some(x) = &self.next {
497 -
            if compare(x, k) == Ordering::Less {
498 -
                return false;
499 -
            }
500 -
        }
501 -
        true
502 -
    }
503 -
}
9 +
use std::{cmp::Ordering, ops::Bound, rc::Rc};
504 10
505 11
pub enum ValueChange<V> {
506 12
    ADD(V),
@@ -571,7 +77,7 @@
Loading
571 77
}
572 78
573 79
impl<K: IndexType> PosRef<K> {
574 -
    fn new(k: &K, pos: usize, node_ref: NodeRef, sibling: Option<NodeRef>) -> PosRef<K> {
80 +
    pub(crate) fn new(k: &K, pos: usize, node_ref: NodeRef, sibling: Option<NodeRef>) -> PosRef<K> {
575 81
        PosRef {
576 82
            k: k.clone(),
577 83
            pos,
@@ -1102,8 +608,8 @@
Loading
1102 608
#[cfg(test)]
1103 609
mod tests {
1104 610
    use super::{
1105 -
        Index, IndexApply, IndexKeeper, IndexModify, IndexType, KeyChanges, Leaf, Node, NodeRef, Nodes, PRes, PosRef,
1106 -
        Value, ValueChange, ValueMode,
611 +
        Index, IndexApply, IndexKeeper, IndexModify, IndexType, KeyChanges, Leaf, Node, NodeRef, Nodes, PRes, Value,
612 +
        ValueChange, ValueMode,
1107 613
    };
1108 614
    use crate::{error::PersyError, id::RecRef};
1109 615
    use rand::random;
@@ -1191,289 +697,6 @@
Loading
1191 697
        RecRef::new(random::<u64>(), random::<u32>())
1192 698
    }
1193 699
1194 -
    #[test]
1195 -
    fn single_node_add_test() {
1196 -
        let val1 = random_pointer();
1197 -
        let val2 = random_pointer();
1198 -
        let val3 = random_pointer();
1199 -
        let val4 = random_pointer();
1200 -
        let val5 = random_pointer();
1201 -
        let val6 = random_pointer();
1202 -
        let mut node = Nodes::new_from_split(val1, &[(0, val2)]);
1203 -
        let pos = node.find(&2).pos;
1204 -
        node.add(pos, &2, val3.clone());
1205 -
        let pos = node.find(&5).pos;
1206 -
        node.add(pos, &5, val4.clone());
1207 -
        let pos = node.find(&6).pos;
1208 -
        node.add(pos, &6, val5);
1209 -
        let pos = node.find(&4).pos;
1210 -
        node.add(pos, &4, val6.clone());
1211 -
1212 -
        let found = node.find(&4);
1213 -
        assert_eq!(found.pos, 3);
1214 -
        //If i search for 4 i get the one on the left of 4 so the value of 2 that is val3
1215 -
        assert_eq!(found.node_ref, val6);
1216 -
1217 -
        let found = node.find(&5);
1218 -
        assert_eq!(found.pos, 4);
1219 -
        //If i search for 5 i get the one on the left of 5 so the value of 4 that is val6
1220 -
        assert_eq!(found.node_ref, val4);
1221 -
1222 -
        let found = node.find(&3);
1223 -
        //If i search for a value that do not exist i get the position of the value at is right
1224 -
        //that is value 4 position 2
1225 -
        assert_eq!(found.pos, 2);
1226 -
        //If i search for 3 i get the value at the left of 4 that is val3
1227 -
        assert_eq!(found.node_ref, val3);
1228 -
    }
1229 -
1230 -
    #[test]
1231 -
    fn single_leaf_insert_test() {
1232 -
        let mut leaf = Leaf::new();
1233 -
        for n in 0..50 {
1234 -
            leaf.insert_or_update(&n, &n, ValueMode::REPLACE, "aa")
1235 -
                .expect("insert is ok");
1236 -
        }
1237 -
        let res = leaf.find(&10);
1238 -
        assert_eq!(Ok((10, Value::SINGLE(10))), res);
1239 -
1240 -
        let res = leaf.find(&60);
1241 -
        assert_eq!(Err(50), res);
1242 -
    }
1243 -
1244 -
    #[test]
1245 -
    fn single_leaf_cluster_insert_test() {
1246 -
        let mut leaf = Leaf::new();
1247 -
        leaf.insert_or_update(&10, &1, ValueMode::CLUSTER, "aa")
1248 -
            .expect("insert is ok");
1249 -
        leaf.insert_or_update(&10, &2, ValueMode::CLUSTER, "aa")
1250 -
            .expect("insert is ok");
1251 -
        let res = leaf.find(&10);
1252 -
        assert_eq!(Ok((10, Value::CLUSTER(vec![1, 2]))), res);
1253 -
    }
1254 -
1255 -
    #[test]
1256 -
    fn leaf_cluster_remove_test() {
1257 -
        let mut leaf = Leaf::new();
1258 -
        leaf.insert_or_update(&10, &1, ValueMode::CLUSTER, "aa")
1259 -
            .expect("insert is ok");
1260 -
        leaf.insert_or_update(&10, &2, ValueMode::CLUSTER, "aa")
1261 -
            .expect("insert is ok");
1262 -
        assert!(leaf.remove(&10, &Some(2)));
1263 -
        let res = leaf.find(&10);
1264 -
        assert_eq!(Ok((10, Value::SINGLE(1))), res);
1265 -
    }
1266 -
1267 -
    #[test]
1268 -
    fn leaf_cluster_remove_not_exist_value_test() {
1269 -
        let mut leaf = Leaf::new();
1270 -
        leaf.insert_or_update(&10, &1, ValueMode::CLUSTER, "aa")
1271 -
            .expect("insert is ok");
1272 -
        leaf.insert_or_update(&10, &2, ValueMode::CLUSTER, "aa")
1273 -
            .expect("insert is ok");
1274 -
        assert!(!leaf.remove(&10, &Some(10)));
1275 -
        let res = leaf.find(&10);
1276 -
        assert_eq!(Ok((10, Value::CLUSTER(vec![1, 2]))), res);
1277 -
    }
1278 -
1279 -
    #[test]
1280 -
    fn leaf_single_delete_not_exist_value_test() {
1281 -
        let mut leaf = Leaf::new();
1282 -
        leaf.insert_or_update(&10, &1, ValueMode::EXCLUSIVE, "aa")
1283 -
            .expect("insert is ok");
1284 -
        assert!(!leaf.remove(&10, &Some(10)));
1285 -
        let res = leaf.find(&10);
1286 -
        assert_eq!(Ok((10, Value::SINGLE(1))), res);
1287 -
    }
1288 -
1289 -
    #[test]
1290 -
    fn leaf_duplicate_key_test() {
1291 -
        let mut leaf = Leaf::new();
1292 -
        leaf.insert_or_update(&10, &1, ValueMode::EXCLUSIVE, "aa")
1293 -
            .expect("insert is ok");
1294 -
        let res = leaf.insert_or_update(&10, &2, ValueMode::EXCLUSIVE, "aa");
1295 -
        assert!(res.is_err());
1296 -
    }
1297 -
1298 -
    #[test]
1299 -
    fn test_leaf_split() {
1300 -
        let mut leaf = Leaf::new();
1301 -
1302 -
        for n in 0..103 {
1303 -
            leaf.insert_or_update(&n, &n, ValueMode::REPLACE, "aa")
1304 -
                .expect("insert is ok");
1305 -
        }
1306 -
1307 -
        let res = leaf.split(21);
1308 -
        assert_eq!(leaf.len(), 21);
1309 -
        assert_eq!(res[0].1.len(), 21);
1310 -
        assert_eq!(res[1].1.len(), 21);
1311 -
        assert_eq!(res[2].1.len(), 21);
1312 -
        assert_eq!(res[3].1.len(), 19);
1313 -
    }
1314 -
1315 -
    #[test]
1316 -
    fn test_node_split() {
1317 -
        let mut node = Nodes::new_from_split(random_pointer(), &[(0, random_pointer())]);
1318 -
        for n in 1..103 {
1319 -
            let pos = node.find(&n).pos;
1320 -
            node.add(pos, &n, random_pointer());
1321 -
        }
1322 -
1323 -
        let res = node.split(21);
1324 -
        assert_eq!(node.len(), 21);
1325 -
        assert_eq!(node.pointers.len(), 21);
1326 -
        assert_eq!(node.keys.len(), 20);
1327 -
        assert_eq!(res[0].1.len(), 21);
1328 -
        assert_eq!(res[0].1.pointers.len(), 21);
1329 -
        assert_eq!(res[0].1.keys.len(), 20);
1330 -
        assert_eq!(res[1].1.len(), 21);
1331 -
        assert_eq!(res[1].1.pointers.len(), 21);
1332 -
        assert_eq!(res[1].1.keys.len(), 20);
1333 -
        assert_eq!(res[2].1.len(), 21);
1334 -
        assert_eq!(res[2].1.pointers.len(), 21);
1335 -
        assert_eq!(res[2].1.keys.len(), 20);
1336 -
        assert_eq!(res[3].1.len(), 20);
1337 -
        assert_eq!(res[3].1.pointers.len(), 20);
1338 -
        assert_eq!(res[3].1.keys.len(), 19);
1339 -
    }
1340 -
1341 -
    #[test]
1342 -
    fn test_remove_from_leaf() {
1343 -
        let mut leaf = Leaf::new();
1344 -
        for n in 0..50 {
1345 -
            leaf.insert_or_update(&n, &n, ValueMode::REPLACE, "aa")
1346 -
                .expect("insert is ok");
1347 -
        }
1348 -
        assert!(leaf.remove(&10, &Some(10)));
1349 -
        assert!(!leaf.remove(&100, &Some(100)));
1350 -
        assert_eq!(leaf.len(), 49);
1351 -
        let res = leaf.find(&10);
1352 -
        assert_eq!(Err(10), res);
1353 -
    }
1354 -
1355 -
    #[test]
1356 -
    fn test_remove_from_node() {
1357 -
        //TODO: check why the remove of 10 make to point to 9
1358 -
        let mut node = Nodes::new_from_split(random_pointer(), &[(0, random_pointer())]);
1359 -
        let mut keep_pre = None;
1360 -
        let mut keep = None;
1361 -
        for n in 1..50 {
1362 -
            let pos = node.find(&n).pos;
1363 -
            let point = random_pointer();
1364 -
            if n == 8 {
1365 -
                keep_pre = Some(point.clone());
1366 -
            }
1367 -
            if n == 9 {
1368 -
                keep = Some(point.clone());
1369 -
            }
1370 -
            node.add(pos, &n, point);
1371 -
        }
1372 -
        let pos = node.find(&10).pos;
1373 -
        node.remove(pos);
1374 -
        assert_eq!(node.len(), 50);
1375 -
        let res = node.find(&10);
1376 -
        assert_eq!(PosRef::new(&10, 10, keep.unwrap(), keep_pre), res);
1377 -
    }
1378 -
1379 -
    #[test]
1380 -
    fn test_merge_leaf() {
1381 -
        let mut leaf = Leaf::new();
1382 -
        let mut leaf2 = Leaf::new();
1383 -
        for n in 0..20 {
1384 -
            leaf.insert_or_update(&n, &n, ValueMode::REPLACE, "aa")
1385 -
                .expect("insert is ok");
1386 -
        }
1387 -
1388 -
        for n in 20..40 {
1389 -
            leaf2
1390 -
                .insert_or_update(&n, &n, ValueMode::REPLACE, "aa")
1391 -
                .expect("insert is ok");
1392 -
        }
1393 -
        leaf.merge_right(&mut leaf2);
1394 -
        assert_eq!(leaf.len(), 40);
1395 -
        assert_eq!(leaf2.len(), 0);
1396 -
        let res = leaf.find(&35);
1397 -
        assert_eq!(res, Ok((35, Value::SINGLE(35))));
1398 -
1399 -
        let mut leaf = Leaf::new();
1400 -
        let mut leaf2 = Leaf::new();
1401 -
        for n in 20..40 {
1402 -
            leaf.insert_or_update(&n, &n, ValueMode::REPLACE, "aa")
1403 -
                .expect("insert is ok");
1404 -
        }
1405 -
1406 -
        for n in 0..20 {
1407 -
            leaf2
1408 -
                .insert_or_update(&n, &n, ValueMode::REPLACE, "aa")
1409 -
                .expect("insert is ok");
1410 -
        }
1411 -
        leaf.merge_left(&mut leaf2);
1412 -
        assert_eq!(leaf.len(), 40);
1413 -
        assert_eq!(leaf2.len(), 0);
1414 -
        let res = leaf.find(&35);
1415 -
        assert_eq!(res, Ok((35, Value::SINGLE(35))));
1416 -
    }
1417 -
1418 -
    #[test]
1419 -
    fn test_merge_nodes() {
1420 -
        let mut node = Nodes::new_from_split(random_pointer(), &[(0, random_pointer())]);
1421 -
        for n in 1..20 {
1422 -
            let pos = node.find(&n).pos;
1423 -
            let point = random_pointer();
1424 -
            node.add(pos, &n, point);
1425 -
        }
1426 -
1427 -
        let mut node2 = Nodes::new_from_split(random_pointer(), &[(21, random_pointer())]);
1428 -
        let mut keep_pre = None;
1429 -
        let mut keep = None;
1430 -
        for n in 22..40 {
1431 -
            let pos = node2.find(&n).pos;
1432 -
            let point = random_pointer();
1433 -
            if n == 25 {
1434 -
                keep_pre = Some(point.clone());
1435 -
            }
1436 -
            if n == 26 {
1437 -
                keep = Some(point.clone());
1438 -
            }
1439 -
            node2.add(pos, &n, point);
1440 -
        }
1441 -
1442 -
        node.merge_right(20, &mut node2);
1443 -
        assert_eq!(node.len(), 41);
1444 -
        assert_eq!(node2.len(), 0);
1445 -
        let res = node.find(&26);
1446 -
        assert_eq!(PosRef::new(&26, 27, keep.unwrap(), keep_pre), res);
1447 -
1448 -
        let mut node = Nodes::new_from_split(random_pointer(), &[(21, random_pointer())]);
1449 -
        let mut keep_pre = None;
1450 -
        let mut keep = None;
1451 -
        for n in 22..40 {
1452 -
            let pos = node.find(&n).pos;
1453 -
            let point = random_pointer();
1454 -
            if n == 25 {
1455 -
                keep_pre = Some(point.clone());
1456 -
            }
1457 -
            if n == 26 {
1458 -
                keep = Some(point.clone());
1459 -
            }
1460 -
            node.add(pos, &n, point);
1461 -
        }
1462 -
1463 -
        let mut node2 = Nodes::new_from_split(random_pointer(), &[(0, random_pointer())]);
1464 -
        for n in 1..20 {
1465 -
            let pos = node2.find(&n).pos;
1466 -
            let point = random_pointer();
1467 -
            node2.add(pos, &n, point);
1468 -
        }
1469 -
1470 -
        node.merge_left(20, &mut node2);
1471 -
        assert_eq!(node.len(), 41);
1472 -
        assert_eq!(node2.len(), 0);
1473 -
        let res = node.find(&26);
1474 -
        assert_eq!(PosRef::new(&26, 27, keep.unwrap(), keep_pre), res);
1475 -
    }
1476 -
1477 700
    struct MockIndexKeeper<K: Clone + Ord, V: Clone> {
1478 701
        store: HashMap<NodeRef, Node<K, V>>,
1479 702
        root: Option<NodeRef>,
Files Coverage
src 92.96%
tests 98.56%
Project Totals (28 files) 94.00%
Notifications are pending CI completion. Waiting for GitLab's status webhook to queue notifications. Push notifications now.
1
coverage:
2
  status:
3
    project:
4
      default:
5
        target: 80%
6
        threshold: 1.0%
7
    patch: off
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading