@@ -0,0 +1,516 @@
Loading
1 +
use super::{
2 +
    Index, IndexApply, IndexKeeper, IndexModify, IndexType, KeyChanges, Leaf, Node, NodeRef, Nodes, PRes, Value,
3 +
    ValueChange, ValueMode,
4 +
};
5 +
use crate::{error::PersyError, id::RecRef};
6 +
use rand::random;
7 +
use std::{collections::HashMap, ops::Bound, rc::Rc};
8 +
9 +
impl<V> std::fmt::Display for Value<V>
10 +
where
11 +
    V: std::fmt::Display,
12 +
{
13 +
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
14 +
        match self {
15 +
            Value::CLUSTER(x) => {
16 +
                write!(f, "{} values: [", x.len())?;
17 +
                for v in x {
18 +
                    write!(f, " {}, ", v)?;
19 +
                }
20 +
                write!(f, "]")?;
21 +
            }
22 +
            Value::SINGLE(v) => {
23 +
                write!(f, "value: {}", v)?;
24 +
            }
25 +
        }
26 +
        Ok(())
27 +
    }
28 +
}
29 +
30 +
fn print_nodes<K: IndexType, V: IndexType>(
31 +
    tree: &mut dyn IndexKeeper<K, V>,
32 +
    node: &Nodes<K>,
33 +
    depth: usize,
34 +
) -> PRes<()> {
35 +
    let padding = String::from_utf8(vec![b' '; depth]).unwrap();
36 +
    for i in 0..node.len() {
37 +
        if i == 0 {
38 +
            println!("{} __ ", padding);
39 +
        } else {
40 +
            println!("{} {}  ", padding, node.keys[i - 1]);
41 +
        }
42 +
        print_node(tree, &node.pointers[i], depth + 1)?;
43 +
    }
44 +
    Ok(())
45 +
}
46 +
47 +
fn print_leaf<K: IndexType, V: IndexType>(
48 +
    _tree: &mut dyn IndexKeeper<K, V>,
49 +
    leaf: &Leaf<K, V>,
50 +
    depth: usize,
51 +
) -> PRes<()> {
52 +
    let padding = String::from_utf8(vec![b' '; depth]).unwrap();
53 +
    println!("{} ", padding);
54 +
    for i in 0..leaf.len() {
55 +
        println!("{} {} {} ", padding, leaf.entries[i].key, leaf.entries[i].value);
56 +
    }
57 +
    println!("{} ", padding);
58 +
    Ok(())
59 +
}
60 +
61 +
fn print_node<K: IndexType, V: IndexType>(tree: &mut dyn IndexKeeper<K, V>, node: &NodeRef, depth: usize) -> PRes<()> {
62 +
    match tree.load(node)? {
63 +
        Node::NODE(n) => {
64 +
            print_nodes(tree, &n, depth)?;
65 +
        }
66 +
        Node::LEAF(l) => {
67 +
            print_leaf(tree, &l, depth)?;
68 +
        }
69 +
    }
70 +
    Ok(())
71 +
}
72 +
73 +
fn print_tree<K: IndexType, V: IndexType>(tree: &mut dyn IndexKeeper<K, V>) -> PRes<()> {
74 +
    let root = tree.get_root()?;
75 +
    if let Some(r) = root {
76 +
        print_node(tree, &r, 0)?;
77 +
    } else {
78 +
        println!(" Empty Root");
79 +
    }
80 +
81 +
    Ok(())
82 +
}
83 +
fn random_pointer() -> NodeRef {
84 +
    RecRef::new(random::<u64>(), random::<u32>())
85 +
}
86 +
87 +
struct MockIndexKeeper<K: Clone + Ord, V: Clone> {
88 +
    store: HashMap<NodeRef, Node<K, V>>,
89 +
    root: Option<NodeRef>,
90 +
    v: ValueMode,
91 +
    name: String,
92 +
}
93 +
94 +
impl<K: Clone + Ord, V: Clone> MockIndexKeeper<K, V> {
95 +
    fn new() -> MockIndexKeeper<K, V> {
96 +
        MockIndexKeeper {
97 +
            store: HashMap::new(),
98 +
            root: None,
99 +
            v: ValueMode::REPLACE,
100 +
            name: "test_index".to_string(),
101 +
        }
102 +
    }
103 +
104 +
    fn new_mode(v: ValueMode) -> MockIndexKeeper<K, V> {
105 +
        MockIndexKeeper {
106 +
            store: HashMap::new(),
107 +
            root: None,
108 +
            v,
109 +
            name: "test_index".to_string(),
110 +
        }
111 +
    }
112 +
}
113 +
114 +
impl<K: Clone + Ord, V: Clone> IndexKeeper<K, V> for MockIndexKeeper<K, V> {
115 +
    fn load(&self, node: &NodeRef) -> PRes<Node<K, V>> {
116 +
        Ok(self.store.get(&node).unwrap().clone())
117 +
    }
118 +
    fn get_root(&self) -> PRes<Option<NodeRef>> {
119 +
        Ok(self.root.clone())
120 +
    }
121 +
122 +
    fn value_mode(&self) -> ValueMode {
123 +
        self.v.clone()
124 +
    }
125 +
126 +
    fn index_name(&self) -> &String {
127 +
        &self.name
128 +
    }
129 +
}
130 +
131 +
impl<K: Clone + Ord, V: Clone> IndexModify<K, V> for MockIndexKeeper<K, V> {
132 +
    fn load_modify(&self, node: &NodeRef) -> PRes<Option<(Rc<Node<K, V>>, u16)>> {
133 +
        Ok(Some((Rc::new(self.store.get(&node).unwrap().clone()), 0)))
134 +
    }
135 +
136 +
    fn lock(&mut self, _node: &NodeRef, _version: u16) -> PRes<bool> {
137 +
        Ok(true)
138 +
    }
139 +
140 +
    fn unlock(&mut self, _node: &NodeRef) -> PRes<bool> {
141 +
        Ok(true)
142 +
    }
143 +
    fn unlock_config(&mut self) -> PRes<bool> {
144 +
        Ok(true)
145 +
    }
146 +
147 +
    fn lock_config(&mut self) -> PRes<bool> {
148 +
        Ok(true)
149 +
    }
150 +
151 +
    fn owned(&mut self, _node_ref: &NodeRef, mut node: Rc<Node<K, V>>) -> Node<K, V> {
152 +
        Rc::make_mut(&mut node);
153 +
        Rc::try_unwrap(node).ok().unwrap()
154 +
    }
155 +
    fn insert(&mut self, node: Node<K, V>) -> PRes<NodeRef> {
156 +
        let node_ref = random_pointer();
157 +
        self.store.insert(node_ref.clone(), node.clone());
158 +
        Ok(node_ref)
159 +
    }
160 +
161 +
    fn update(&mut self, node_ref: &NodeRef, node: Node<K, V>, _version: u16) -> PRes<()> {
162 +
        self.store.insert(node_ref.clone(), node);
163 +
        Ok(())
164 +
    }
165 +
166 +
    fn get_root_refresh(&mut self) -> PRes<Option<NodeRef>> {
167 +
        Ok(self.root.clone())
168 +
    }
169 +
170 +
    fn set_root(&mut self, root: Option<NodeRef>) -> PRes<()> {
171 +
        Ok(self.root = root)
172 +
    }
173 +
174 +
    fn delete(&mut self, node: &NodeRef, _version: u16) -> PRes<()> {
175 +
        self.store.remove(&node);
176 +
        Ok(())
177 +
    }
178 +
    fn bottom_limit(&self) -> usize {
179 +
        10
180 +
    }
181 +
    fn top_limit(&self) -> usize {
182 +
        30
183 +
    }
184 +
}
185 +
186 +
#[test]
187 +
fn test_single_add() {
188 +
    let mut keeper = MockIndexKeeper::<u8, u8>::new();
189 +
    let mut changes = Vec::new();
190 +
    changes.push(KeyChanges::single_add(1, 1));
191 +
    changes.push(KeyChanges::single_add(2, 2));
192 +
    changes.push(KeyChanges::single_add(3, 4));
193 +
    keeper.apply(&changes).unwrap();
194 +
    assert_eq!(keeper.get(&2).ok(), Some(Some(Value::SINGLE(2))));
195 +
}
196 +
197 +
#[test]
198 +
fn test_many_add() {
199 +
    let mut keeper = MockIndexKeeper::<u8, u8>::new();
200 +
    let mut changes = Vec::new();
201 +
    for i in 0..200 {
202 +
        changes.push(KeyChanges::single_add(i, i));
203 +
    }
204 +
    keeper.apply(&changes).unwrap();
205 +
    assert_eq!(keeper.get(&2).ok(), Some(Some(Value::SINGLE(2))));
206 +
    assert_eq!(keeper.get(&100).ok(), Some(Some(Value::SINGLE(100))));
207 +
    assert_eq!(keeper.get(&201).ok(), Some(None));
208 +
}
209 +
210 +
#[test]
211 +
fn test_many_add_multiple_times() {
212 +
    let mut keeper = MockIndexKeeper::<u8, u8>::new();
213 +
    let mut changes = Vec::new();
214 +
    for n in 0..8 {
215 +
        for i in 0..20 {
216 +
            changes.push(KeyChanges::single_add(i, i));
217 +
        }
218 +
        keeper.apply(&changes).unwrap();
219 +
        assert_eq!(keeper.get(&(n + 2)).ok(), Some(Some(Value::SINGLE(n + 2))));
220 +
    }
221 +
}
222 +
223 +
#[test]
224 +
fn test_single_add_remove() {
225 +
    let mut keeper = MockIndexKeeper::<u8, u8>::new();
226 +
    let mut changes = Vec::new();
227 +
    changes.push(KeyChanges::single_add(1, 1));
228 +
    changes.push(KeyChanges::single_add(2, 2));
229 +
    changes.push(KeyChanges::single_add(3, 4));
230 +
    keeper.apply(&changes).unwrap();
231 +
    assert_eq!(keeper.get(&2).ok(), Some(Some(Value::SINGLE(2))));
232 +
233 +
    let mut changes = Vec::new();
234 +
    changes.push(KeyChanges::single_delete(1, Some(1)));
235 +
    changes.push(KeyChanges::single_delete(2, Some(2)));
236 +
    changes.push(KeyChanges::single_delete(3, Some(4)));
237 +
    keeper.apply(&changes).unwrap();
238 +
    assert_eq!(keeper.get(&2).ok(), Some(None));
239 +
}
240 +
241 +
#[test]
242 +
fn test_aggregate_add_remove() {
243 +
    let mut keeper = MockIndexKeeper::<u8, u8>::new();
244 +
    let mut changes = Vec::new();
245 +
    changes.push(KeyChanges::new(
246 +
        2,
247 +
        vec![ValueChange::ADD(1), ValueChange::REMOVE(Some(1)), ValueChange::ADD(2)],
248 +
    ));
249 +
    keeper.apply(&changes).unwrap();
250 +
    assert_eq!(keeper.get(&2).ok(), Some(Some(Value::SINGLE(2))));
251 +
252 +
    let mut changes = Vec::new();
253 +
    changes.push(KeyChanges::new(
254 +
        2,
255 +
        vec![
256 +
            ValueChange::REMOVE(Some(2)),
257 +
            ValueChange::ADD(1),
258 +
            ValueChange::REMOVE(Some(1)),
259 +
        ],
260 +
    ));
261 +
    keeper.apply(&changes).unwrap();
262 +
    assert_eq!(keeper.get(&2).ok(), Some(None));
263 +
}
264 +
265 +
#[test]
266 +
fn test_group_replace_remove() {
267 +
    let mut keeper = MockIndexKeeper::<u8, u8>::new();
268 +
    let mut changes = Vec::new();
269 +
    changes.push(KeyChanges::new(2, vec![ValueChange::ADD(1), ValueChange::ADD(2)]));
270 +
    keeper.apply(&changes).unwrap();
271 +
    assert_eq!(keeper.get(&2).ok(), Some(Some(Value::SINGLE(2))));
272 +
273 +
    let mut changes = Vec::new();
274 +
    changes.push(KeyChanges::new(2, vec![ValueChange::REMOVE(Some(2))]));
275 +
    keeper.apply(&changes).unwrap();
276 +
    assert_eq!(keeper.get(&2).ok(), Some(None));
277 +
}
278 +
279 +
#[test]
280 +
fn test_group_values_remove() {
281 +
    let mut keeper = MockIndexKeeper::<u8, u8>::new_mode(ValueMode::CLUSTER);
282 +
    let mut changes = Vec::new();
283 +
    changes.push(KeyChanges::new(
284 +
        2,
285 +
        vec![ValueChange::ADD(1), ValueChange::ADD(2), ValueChange::ADD(3)],
286 +
    ));
287 +
    keeper.apply(&changes).unwrap();
288 +
    assert_eq!(keeper.get(&2).ok(), Some(Some(Value::CLUSTER(vec![1, 2, 3]))));
289 +
290 +
    let mut changes = Vec::new();
291 +
    changes.push(KeyChanges::new(
292 +
        2,
293 +
        vec![ValueChange::REMOVE(Some(1)), ValueChange::REMOVE(Some(2))],
294 +
    ));
295 +
    keeper.apply(&changes).unwrap();
296 +
    assert_eq!(keeper.get(&2).ok(), Some(Some(Value::SINGLE(3))));
297 +
}
298 +
299 +
#[test]
300 +
fn test_group_values_remove_no_order() {
301 +
    let mut keeper = MockIndexKeeper::<u8, u8>::new_mode(ValueMode::CLUSTER);
302 +
    let mut changes = Vec::new();
303 +
    changes.push(KeyChanges::new(
304 +
        2,
305 +
        vec![ValueChange::ADD(3), ValueChange::ADD(1), ValueChange::ADD(2)],
306 +
    ));
307 +
    keeper.apply(&changes).unwrap();
308 +
    assert_eq!(keeper.get(&2).ok(), Some(Some(Value::CLUSTER(vec![3, 1, 2]))));
309 +
310 +
    let mut changes = Vec::new();
311 +
    changes.push(KeyChanges::new(
312 +
        2,
313 +
        vec![ValueChange::REMOVE(Some(1)), ValueChange::REMOVE(Some(2))],
314 +
    ));
315 +
    keeper.apply(&changes).unwrap();
316 +
    assert_eq!(keeper.get(&2).ok(), Some(Some(Value::SINGLE(3))));
317 +
}
318 +
319 +
#[test]
320 +
fn test_add_same_value_twice() {
321 +
    let mut keeper = MockIndexKeeper::<u8, u8>::new_mode(ValueMode::CLUSTER);
322 +
    let mut changes = Vec::new();
323 +
    changes.push(KeyChanges::new(
324 +
        2,
325 +
        vec![ValueChange::ADD(1), ValueChange::ADD(2), ValueChange::ADD(1)],
326 +
    ));
327 +
328 +
    changes.push(KeyChanges::new(1, vec![ValueChange::ADD(1), ValueChange::ADD(1)]));
329 +
    keeper.apply(&changes).unwrap();
330 +
    assert_eq!(keeper.get(&2).ok(), Some(Some(Value::CLUSTER(vec![1, 2]))));
331 +
    assert_eq!(keeper.get(&1).ok(), Some(Some(Value::SINGLE(1))));
332 +
}
333 +
334 +
#[test]
335 +
fn test_add_to_esclusive() {
336 +
    let mut keeper = MockIndexKeeper::<u8, u8>::new_mode(ValueMode::EXCLUSIVE);
337 +
    let mut changes = Vec::new();
338 +
    changes.push(KeyChanges::new(2, vec![ValueChange::ADD(1)]));
339 +
    keeper.apply(&changes).unwrap();
340 +
    assert_eq!(keeper.get(&2).ok(), Some(Some(Value::SINGLE(1))));
341 +
    let mut changes = Vec::new();
342 +
    changes.push(KeyChanges::new(2, vec![ValueChange::ADD(1)]));
343 +
344 +
    keeper.apply(&changes).unwrap();
345 +
    assert_eq!(keeper.get(&2).ok(), Some(Some(Value::SINGLE(1))));
346 +
    let mut changes = Vec::new();
347 +
    changes.push(KeyChanges::new(2, vec![ValueChange::ADD(2)]));
348 +
    assert!(match keeper.apply(&changes) {
349 +
        Err(PersyError::IndexDuplicateKey(ref idxname, ref keyname)) if (idxname == "test_index" && keyname == "2") => {
350 +
            true
351 +
        }
352 +
        _ => false,
353 +
    });
354 +
}
355 +
356 +
#[test]
357 +
fn test_group_key_remove() {
358 +
    let mut keeper = MockIndexKeeper::<u8, u8>::new_mode(ValueMode::CLUSTER);
359 +
    let mut changes = Vec::new();
360 +
    changes.push(KeyChanges::new(2, vec![ValueChange::ADD(1), ValueChange::ADD(2)]));
361 +
    keeper.apply(&changes).unwrap();
362 +
    assert_eq!(keeper.get(&2).ok(), Some(Some(Value::CLUSTER(vec![1, 2]))));
363 +
364 +
    let mut changes = Vec::new();
365 +
    changes.push(KeyChanges::new(2, vec![ValueChange::REMOVE(None)]));
366 +
    keeper.apply(&changes).unwrap();
367 +
    assert_eq!(keeper.get(&2).ok(), Some(None));
368 +
}
369 +
370 +
#[test]
371 +
fn test_many_add_remove() {
372 +
    let mut keeper = MockIndexKeeper::<u8, u8>::new();
373 +
    let mut changes = Vec::new();
374 +
    for i in 0..200 {
375 +
        changes.push(KeyChanges::single_add(i, i));
376 +
    }
377 +
    changes.sort_by_key(|k| k.k);
378 +
    keeper.apply(&changes).unwrap();
379 +
    print_tree(&mut keeper).unwrap();
380 +
    assert_eq!(keeper.get(&2).ok(), Some(Some(Value::SINGLE(2))));
381 +
    assert_eq!(keeper.get(&100).ok(), Some(Some(Value::SINGLE(100))));
382 +
    assert_eq!(keeper.get(&201).ok(), Some(None));
383 +
    let mut changes = Vec::new();
384 +
    for i in 0..200 {
385 +
        changes.push(KeyChanges::single_delete(i, Some(i)));
386 +
    }
387 +
    changes.sort_by_key(|k| k.k);
388 +
    keeper.apply(&changes).unwrap();
389 +
    print_tree(&mut keeper).unwrap();
390 +
    assert_eq!(keeper.get_root().ok(), Some(None));
391 +
    assert_eq!(keeper.get(&2).ok(), Some(None));
392 +
    assert_eq!(keeper.get(&100).ok(), Some(None));
393 +
}
394 +
395 +
#[test]
396 +
fn test_many_add_remove_multiple_times() {
397 +
    let mut keeper = MockIndexKeeper::<u8, u8>::new();
398 +
    let mut changes = Vec::new();
399 +
    let mut rchanges = Vec::new();
400 +
    for n in 0..8 {
401 +
        for i in 0..20 {
402 +
            changes.push(KeyChanges::single_add(i, i));
403 +
            rchanges.push(KeyChanges::single_delete(i, Some(i)));
404 +
        }
405 +
        changes.sort_by_key(|k| k.k);
406 +
        keeper.apply(&changes).unwrap();
407 +
        assert_eq!(keeper.get(&(n + 2)).ok(), Some(Some(Value::SINGLE(n + 2))));
408 +
        rchanges.sort_by_key(|k| k.k);
409 +
        keeper.apply(&rchanges).unwrap();
410 +
        assert_eq!(keeper.get(&(n + 2)).ok(), Some(None));
411 +
    }
412 +
}
413 +
414 +
#[test]
415 +
fn test_iter_from() {
416 +
    let mut keeper = MockIndexKeeper::<u8, u8>::new();
417 +
    let mut changes = Vec::new();
418 +
    for i in 0..50 {
419 +
        changes.push(KeyChanges::single_add(i, i));
420 +
    }
421 +
    keeper.apply(&changes).unwrap();
422 +
    print_tree(&mut keeper).unwrap();
423 +
    let mut iter = keeper.iter_from(Bound::Included(&5)).unwrap();
424 +
    let next = iter.iter.next();
425 +
426 +
    assert_eq!(5, next.unwrap().key);
427 +
    let next = iter.iter.next();
428 +
    assert_eq!(6, next.unwrap().key);
429 +
    let mut last_val = None;
430 +
    for v in iter.iter {
431 +
        last_val = Some(v);
432 +
    }
433 +
    let mut next_page = keeper
434 +
        .iter_from(Bound::Excluded(&last_val.clone().unwrap().key))
435 +
        .unwrap();
436 +
    let next = next_page.iter.next();
437 +
    assert_eq!(last_val.unwrap().key + 1, next.unwrap().key);
438 +
}
439 +
440 +
#[test]
441 +
fn test_iter() {
442 +
    let mut keeper = MockIndexKeeper::<u8, u8>::new();
443 +
    let mut changes = Vec::new();
444 +
    for i in 0..50 {
445 +
        changes.push(KeyChanges::single_add(i, i));
446 +
    }
447 +
    keeper.apply(&changes).unwrap();
448 +
    print_tree(&mut keeper).unwrap();
449 +
    let mut iter = keeper.iter_from(Bound::Unbounded).unwrap();
450 +
    let next = iter.iter.next();
451 +
452 +
    assert_eq!(0, next.unwrap().key);
453 +
    let mut last_val = None;
454 +
    for v in iter.iter {
455 +
        last_val = Some(v);
456 +
    }
457 +
    let mut next_page = keeper
458 +
        .iter_from(Bound::Excluded(&last_val.clone().unwrap().key))
459 +
        .unwrap();
460 +
    let next = next_page.iter.next();
461 +
    assert_eq!(last_val.unwrap().key + 1, next.unwrap().key);
462 +
    let mut last_val = None;
463 +
    for v in next_page.iter {
464 +
        last_val = Some(v);
465 +
    }
466 +
    assert_eq!(last_val.unwrap().key, 49);
467 +
    let mut next_page = keeper.iter_from(Bound::Excluded(&49)).unwrap();
468 +
    assert!(next_page.iter.next().is_none());
469 +
}
470 +
471 +
#[test]
472 +
fn test_a_lot_add_remove_multiple_times() {
473 +
    let mut keeper = MockIndexKeeper::<u32, u32>::new();
474 +
    let mut changes = Vec::new();
475 +
    let mut remove = Vec::new();
476 +
    for n in 1..30 {
477 +
        for i in 1..200 {
478 +
            changes.push(KeyChanges::single_add(i * n, i * n));
479 +
            if i % 2 == 0 {
480 +
                remove.push(KeyChanges::single_delete(i * n, Some(i * n)));
481 +
            }
482 +
        }
483 +
        changes.sort_by_key(|k| k.k);
484 +
        keeper.apply(&changes).unwrap();
485 +
        assert_eq!(keeper.get(&(2 * n)).ok(), Some(Some(Value::SINGLE(2 * n))));
486 +
        assert_eq!(keeper.get(&(100 * n)).ok(), Some(Some(Value::SINGLE(100 * n))));
487 +
        assert_eq!(keeper.get(&20001).ok(), Some(None));
488 +
        remove.sort_by_key(|k| k.k);
489 +
        keeper.apply(&remove).unwrap();
490 +
        assert_eq!(keeper.get(&(2 * n)).ok(), Some(None));
491 +
        assert_eq!(keeper.get(&(100 * n)).ok(), Some(None));
492 +
    }
493 +
}
494 +
495 +
#[test]
496 +
fn test_big_tree() {
497 +
    let mut keeper = MockIndexKeeper::<u32, u32>::new();
498 +
    for n in 1..20 {
499 +
        let mut changes = Vec::new();
500 +
        let mut remove = Vec::new();
501 +
        for i in 1..301 {
502 +
            changes.push(KeyChanges::single_add(i * n, i * n));
503 +
            if i % 2 == 0 {
504 +
                remove.push(KeyChanges::single_delete(i * n, Some(i * n)));
505 +
            }
506 +
        }
507 +
        keeper.apply(&changes).unwrap();
508 +
        assert_eq!(keeper.get(&(2 * n)).ok(), Some(Some(Value::SINGLE(2 * n))));
509 +
        assert_eq!(keeper.get(&(100 * n)).ok(), Some(Some(Value::SINGLE(100 * n))));
510 +
        assert_eq!(keeper.get(&20001).ok(), Some(None));
511 +
512 +
        keeper.apply(&remove).unwrap();
513 +
        assert_eq!(keeper.get(&(2 * n)).ok(), Some(None));
514 +
        assert_eq!(keeper.get(&(100 * n)).ok(), Some(None));
515 +
    }
516 +
}

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

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

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

@@ -4,7 +4,7 @@
Loading
4 4
    index::{
5 5
        config::{ByteVec, IndexType, IndexTypeId},
6 6
        keeper::Container,
7 -
        nodes::{Leaf, LeafEntry, Node, NodeRef, Nodes, Value},
7 +
        tree::nodes::{Leaf, LeafEntry, Node, NodeRef, Nodes, Value},
8 8
    },
9 9
    io::{
10 10
        InfallibleRead, InfallibleReadFormat, InfallibleReadVarInt, InfallibleWrite, InfallibleWriteFormat,
@@ -381,7 +381,7 @@
Loading
381 381
    use crate::id::{PersyId, RecRef};
382 382
    use crate::index::{
383 383
        config::{ByteVec, IndexType, ValueMode},
384 -
        nodes::{compare, Leaf, Node, NodeRef, Nodes, Value},
384 +
        tree::nodes::{compare, Leaf, Node, NodeRef, Nodes, Value},
385 385
    };
386 386
    use rand::random;
387 387
    use std::{cmp::Ordering, fmt::Debug};

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

@@ -1,9 +1,10 @@
Loading
1 +
pub mod nodes;
1 2
use crate::{
2 3
    error::PRes,
3 4
    index::{
4 5
        config::{IndexType, ValueMode},
5 6
        keeper::{IndexKeeper, IndexModify},
6 -
        nodes::{Leaf, Node, NodeRef, Nodes, PageIter, PageIterBack, Value},
7 +
        tree::nodes::{Leaf, Node, NodeRef, Nodes, PageIter, PageIterBack, Value},
7 8
    },
8 9
};
9 10
use std::{cmp::Ordering, ops::Bound, rc::Rc};
@@ -606,527 +607,4 @@
Loading
606 607
}
607 608
608 609
#[cfg(test)]
609 -
mod tests {
610 -
    use super::{
611 -
        Index, IndexApply, IndexKeeper, IndexModify, IndexType, KeyChanges, Leaf, Node, NodeRef, Nodes, PRes, Value,
612 -
        ValueChange, ValueMode,
613 -
    };
614 -
    use crate::{error::PersyError, id::RecRef};
615 -
    use rand::random;
616 -
    use std::{collections::HashMap, ops::Bound, rc::Rc};
617 -
618 -
    impl<V> std::fmt::Display for Value<V>
619 -
    where
620 -
        V: std::fmt::Display,
621 -
    {
622 -
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
623 -
            match self {
624 -
                Value::CLUSTER(x) => {
625 -
                    write!(f, "{} values: [", x.len())?;
626 -
                    for v in x {
627 -
                        write!(f, " {}, ", v)?;
628 -
                    }
629 -
                    write!(f, "]")?;
630 -
                }
631 -
                Value::SINGLE(v) => {
632 -
                    write!(f, "value: {}", v)?;
633 -
                }
634 -
            }
635 -
            Ok(())
636 -
        }
637 -
    }
638 -
639 -
    fn print_nodes<K: IndexType, V: IndexType>(
640 -
        tree: &mut dyn IndexKeeper<K, V>,
641 -
        node: &Nodes<K>,
642 -
        depth: usize,
643 -
    ) -> PRes<()> {
644 -
        let padding = String::from_utf8(vec![b' '; depth]).unwrap();
645 -
        for i in 0..node.len() {
646 -
            if i == 0 {
647 -
                println!("{} __ ", padding);
648 -
            } else {
649 -
                println!("{} {}  ", padding, node.keys[i - 1]);
650 -
            }
651 -
            print_node(tree, &node.pointers[i], depth + 1)?;
652 -
        }
653 -
        Ok(())
654 -
    }
655 -
656 -
    fn print_leaf<K: IndexType, V: IndexType>(
657 -
        _tree: &mut dyn IndexKeeper<K, V>,
658 -
        leaf: &Leaf<K, V>,
659 -
        depth: usize,
660 -
    ) -> PRes<()> {
661 -
        let padding = String::from_utf8(vec![b' '; depth]).unwrap();
662 -
        println!("{} ", padding);
663 -
        for i in 0..leaf.len() {
664 -
            println!("{} {} {} ", padding, leaf.entries[i].key, leaf.entries[i].value);
665 -
        }
666 -
        println!("{} ", padding);
667 -
        Ok(())
668 -
    }
669 -
670 -
    fn print_node<K: IndexType, V: IndexType>(
671 -
        tree: &mut dyn IndexKeeper<K, V>,
672 -
        node: &NodeRef,
673 -
        depth: usize,
674 -
    ) -> PRes<()> {
675 -
        match tree.load(node)? {
676 -
            Node::NODE(n) => {
677 -
                print_nodes(tree, &n, depth)?;
678 -
            }
679 -
            Node::LEAF(l) => {
680 -
                print_leaf(tree, &l, depth)?;
681 -
            }
682 -
        }
683 -
        Ok(())
684 -
    }
685 -
686 -
    fn print_tree<K: IndexType, V: IndexType>(tree: &mut dyn IndexKeeper<K, V>) -> PRes<()> {
687 -
        let root = tree.get_root()?;
688 -
        if let Some(r) = root {
689 -
            print_node(tree, &r, 0)?;
690 -
        } else {
691 -
            println!(" Empty Root");
692 -
        }
693 -
694 -
        Ok(())
695 -
    }
696 -
    fn random_pointer() -> NodeRef {
697 -
        RecRef::new(random::<u64>(), random::<u32>())
698 -
    }
699 -
700 -
    struct MockIndexKeeper<K: Clone + Ord, V: Clone> {
701 -
        store: HashMap<NodeRef, Node<K, V>>,
702 -
        root: Option<NodeRef>,
703 -
        v: ValueMode,
704 -
        name: String,
705 -
    }
706 -
707 -
    impl<K: Clone + Ord, V: Clone> MockIndexKeeper<K, V> {
708 -
        fn new() -> MockIndexKeeper<K, V> {
709 -
            MockIndexKeeper {
710 -
                store: HashMap::new(),
711 -
                root: None,
712 -
                v: ValueMode::REPLACE,
713 -
                name: "test_index".to_string(),
714 -
            }
715 -
        }
716 -
717 -
        fn new_mode(v: ValueMode) -> MockIndexKeeper<K, V> {
718 -
            MockIndexKeeper {
719 -
                store: HashMap::new(),
720 -
                root: None,
721 -
                v,
722 -
                name: "test_index".to_string(),
723 -
            }
724 -
        }
725 -
    }
726 -
727 -
    impl<K: Clone + Ord, V: Clone> IndexKeeper<K, V> for MockIndexKeeper<K, V> {
728 -
        fn load(&self, node: &NodeRef) -> PRes<Node<K, V>> {
729 -
            Ok(self.store.get(&node).unwrap().clone())
730 -
        }
731 -
        fn get_root(&self) -> PRes<Option<NodeRef>> {
732 -
            Ok(self.root.clone())
733 -
        }
734 -
735 -
        fn value_mode(&self) -> ValueMode {
736 -
            self.v.clone()
737 -
        }
738 -
739 -
        fn index_name(&self) -> &String {
740 -
            &self.name
741 -
        }
742 -
    }
743 -
744 -
    impl<K: Clone + Ord, V: Clone> IndexModify<K, V> for MockIndexKeeper<K, V> {
745 -
        fn load_modify(&self, node: &NodeRef) -> PRes<Option<(Rc<Node<K, V>>, u16)>> {
746 -
            Ok(Some((Rc::new(self.store.get(&node).unwrap().clone()), 0)))
747 -
        }
748 -
749 -
        fn lock(&mut self, _node: &NodeRef, _version: u16) -> PRes<bool> {
750 -
            Ok(true)
751 -
        }
752 -
753 -
        fn unlock(&mut self, _node: &NodeRef) -> PRes<bool> {
754 -
            Ok(true)
755 -
        }
756 -
        fn unlock_config(&mut self) -> PRes<bool> {
757 -
            Ok(true)
758 -
        }
759 -
760 -
        fn lock_config(&mut self) -> PRes<bool> {
761 -
            Ok(true)
762 -
        }
763 -
764 -
        fn owned(&mut self, _node_ref: &NodeRef, mut node: Rc<Node<K, V>>) -> Node<K, V> {
765 -
            Rc::make_mut(&mut node);
766 -
            Rc::try_unwrap(node).ok().unwrap()
767 -
        }
768 -
        fn insert(&mut self, node: Node<K, V>) -> PRes<NodeRef> {
769 -
            let node_ref = random_pointer();
770 -
            self.store.insert(node_ref.clone(), node.clone());
771 -
            Ok(node_ref)
772 -
        }
773 -
774 -
        fn update(&mut self, node_ref: &NodeRef, node: Node<K, V>, _version: u16) -> PRes<()> {
775 -
            self.store.insert(node_ref.clone(), node);
776 -
            Ok(())
777 -
        }
778 -
779 -
        fn get_root_refresh(&mut self) -> PRes<Option<NodeRef>> {
780 -
            Ok(self.root.clone())
781 -
        }
782 -
783 -
        fn set_root(&mut self, root: Option<NodeRef>) -> PRes<()> {
784 -
            Ok(self.root = root)
785 -
        }
786 -
787 -
        fn delete(&mut self, node: &NodeRef, _version: u16) -> PRes<()> {
788 -
            self.store.remove(&node);
789 -
            Ok(())
790 -
        }
791 -
        fn bottom_limit(&self) -> usize {
792 -
            10
793 -
        }
794 -
        fn top_limit(&self) -> usize {
795 -
            30
796 -
        }
797 -
    }
798 -
799 -
    #[test]
800 -
    fn test_single_add() {
801 -
        let mut keeper = MockIndexKeeper::<u8, u8>::new();
802 -
        let mut changes = Vec::new();
803 -
        changes.push(KeyChanges::single_add(1, 1));
804 -
        changes.push(KeyChanges::single_add(2, 2));
805 -
        changes.push(KeyChanges::single_add(3, 4));
806 -
        keeper.apply(&changes).unwrap();
807 -
        assert_eq!(keeper.get(&2).ok(), Some(Some(Value::SINGLE(2))));
808 -
    }
809 -
810 -
    #[test]
811 -
    fn test_many_add() {
812 -
        let mut keeper = MockIndexKeeper::<u8, u8>::new();
813 -
        let mut changes = Vec::new();
814 -
        for i in 0..200 {
815 -
            changes.push(KeyChanges::single_add(i, i));
816 -
        }
817 -
        keeper.apply(&changes).unwrap();
818 -
        assert_eq!(keeper.get(&2).ok(), Some(Some(Value::SINGLE(2))));
819 -
        assert_eq!(keeper.get(&100).ok(), Some(Some(Value::SINGLE(100))));
820 -
        assert_eq!(keeper.get(&201).ok(), Some(None));
821 -
    }
822 -
823 -
    #[test]
824 -
    fn test_many_add_multiple_times() {
825 -
        let mut keeper = MockIndexKeeper::<u8, u8>::new();
826 -
        let mut changes = Vec::new();
827 -
        for n in 0..8 {
828 -
            for i in 0..20 {
829 -
                changes.push(KeyChanges::single_add(i, i));
830 -
            }
831 -
            keeper.apply(&changes).unwrap();
832 -
            assert_eq!(keeper.get(&(n + 2)).ok(), Some(Some(Value::SINGLE(n + 2))));
833 -
        }
834 -
    }
835 -
836 -
    #[test]
837 -
    fn test_single_add_remove() {
838 -
        let mut keeper = MockIndexKeeper::<u8, u8>::new();
839 -
        let mut changes = Vec::new();
840 -
        changes.push(KeyChanges::single_add(1, 1));
841 -
        changes.push(KeyChanges::single_add(2, 2));
842 -
        changes.push(KeyChanges::single_add(3, 4));
843 -
        keeper.apply(&changes).unwrap();
844 -
        assert_eq!(keeper.get(&2).ok(), Some(Some(Value::SINGLE(2))));
845 -
846 -
        let mut changes = Vec::new();
847 -
        changes.push(KeyChanges::single_delete(1, Some(1)));
848 -
        changes.push(KeyChanges::single_delete(2, Some(2)));
849 -
        changes.push(KeyChanges::single_delete(3, Some(4)));
850 -
        keeper.apply(&changes).unwrap();
851 -
        assert_eq!(keeper.get(&2).ok(), Some(None));
852 -
    }
853 -
854 -
    #[test]
855 -
    fn test_aggregate_add_remove() {
856 -
        let mut keeper = MockIndexKeeper::<u8, u8>::new();
857 -
        let mut changes = Vec::new();
858 -
        changes.push(KeyChanges::new(
859 -
            2,
860 -
            vec![ValueChange::ADD(1), ValueChange::REMOVE(Some(1)), ValueChange::ADD(2)],
861 -
        ));
862 -
        keeper.apply(&changes).unwrap();
863 -
        assert_eq!(keeper.get(&2).ok(), Some(Some(Value::SINGLE(2))));
864 -
865 -
        let mut changes = Vec::new();
866 -
        changes.push(KeyChanges::new(
867 -
            2,
868 -
            vec![
869 -
                ValueChange::REMOVE(Some(2)),
870 -
                ValueChange::ADD(1),
871 -
                ValueChange::REMOVE(Some(1)),
872 -
            ],
873 -
        ));
874 -
        keeper.apply(&changes).unwrap();
875 -
        assert_eq!(keeper.get(&2).ok(), Some(None));
876 -
    }
877 -
878 -
    #[test]
879 -
    fn test_group_replace_remove() {
880 -
        let mut keeper = MockIndexKeeper::<u8, u8>::new();
881 -
        let mut changes = Vec::new();
882 -
        changes.push(KeyChanges::new(2, vec![ValueChange::ADD(1), ValueChange::ADD(2)]));
883 -
        keeper.apply(&changes).unwrap();
884 -
        assert_eq!(keeper.get(&2).ok(), Some(Some(Value::SINGLE(2))));
885 -
886 -
        let mut changes = Vec::new();
887 -
        changes.push(KeyChanges::new(2, vec![ValueChange::REMOVE(Some(2))]));
888 -
        keeper.apply(&changes).unwrap();
889 -
        assert_eq!(keeper.get(&2).ok(), Some(None));
890 -
    }
891 -
892 -
    #[test]
893 -
    fn test_group_values_remove() {
894 -
        let mut keeper = MockIndexKeeper::<u8, u8>::new_mode(ValueMode::CLUSTER);
895 -
        let mut changes = Vec::new();
896 -
        changes.push(KeyChanges::new(
897 -
            2,
898 -
            vec![ValueChange::ADD(1), ValueChange::ADD(2), ValueChange::ADD(3)],
899 -
        ));
900 -
        keeper.apply(&changes).unwrap();
901 -
        assert_eq!(keeper.get(&2).ok(), Some(Some(Value::CLUSTER(vec![1, 2, 3]))));
902 -
903 -
        let mut changes = Vec::new();
904 -
        changes.push(KeyChanges::new(
905 -
            2,
906 -
            vec![ValueChange::REMOVE(Some(1)), ValueChange::REMOVE(Some(2))],
907 -
        ));
908 -
        keeper.apply(&changes).unwrap();
909 -
        assert_eq!(keeper.get(&2).ok(), Some(Some(Value::SINGLE(3))));
910 -
    }
911 -
912 -
    #[test]
913 -
    fn test_group_values_remove_no_order() {
914 -
        let mut keeper = MockIndexKeeper::<u8, u8>::new_mode(ValueMode::CLUSTER);
915 -
        let mut changes = Vec::new();
916 -
        changes.push(KeyChanges::new(
917 -
            2,
918 -
            vec![ValueChange::ADD(3), ValueChange::ADD(1), ValueChange::ADD(2)],
919 -
        ));
920 -
        keeper.apply(&changes).unwrap();
921 -
        assert_eq!(keeper.get(&2).ok(), Some(Some(Value::CLUSTER(vec![3, 1, 2]))));
922 -
923 -
        let mut changes = Vec::new();
924 -
        changes.push(KeyChanges::new(
925 -
            2,
926 -
            vec![ValueChange::REMOVE(Some(1)), ValueChange::REMOVE(Some(2))],
927 -
        ));
928 -
        keeper.apply(&changes).unwrap();
929 -
        assert_eq!(keeper.get(&2).ok(), Some(Some(Value::SINGLE(3))));
930 -
    }
931 -
932 -
    #[test]
933 -
    fn test_add_same_value_twice() {
934 -
        let mut keeper = MockIndexKeeper::<u8, u8>::new_mode(ValueMode::CLUSTER);
935 -
        let mut changes = Vec::new();
936 -
        changes.push(KeyChanges::new(
937 -
            2,
938 -
            vec![ValueChange::ADD(1), ValueChange::ADD(2), ValueChange::ADD(1)],
939 -
        ));
940 -
941 -
        changes.push(KeyChanges::new(1, vec![ValueChange::ADD(1), ValueChange::ADD(1)]));
942 -
        keeper.apply(&changes).unwrap();
943 -
        assert_eq!(keeper.get(&2).ok(), Some(Some(Value::CLUSTER(vec![1, 2]))));
944 -
        assert_eq!(keeper.get(&1).ok(), Some(Some(Value::SINGLE(1))));
945 -
    }
946 -
947 -
    #[test]
948 -
    fn test_add_to_esclusive() {
949 -
        let mut keeper = MockIndexKeeper::<u8, u8>::new_mode(ValueMode::EXCLUSIVE);
950 -
        let mut changes = Vec::new();
951 -
        changes.push(KeyChanges::new(2, vec![ValueChange::ADD(1)]));
952 -
        keeper.apply(&changes).unwrap();
953 -
        assert_eq!(keeper.get(&2).ok(), Some(Some(Value::SINGLE(1))));
954 -
        let mut changes = Vec::new();
955 -
        changes.push(KeyChanges::new(2, vec![ValueChange::ADD(1)]));
956 -
957 -
        keeper.apply(&changes).unwrap();
958 -
        assert_eq!(keeper.get(&2).ok(), Some(Some(Value::SINGLE(1))));
959 -
        let mut changes = Vec::new();
960 -
        changes.push(KeyChanges::new(2, vec![ValueChange::ADD(2)]));
961 -
        assert!(match keeper.apply(&changes) {
962 -
            Err(PersyError::IndexDuplicateKey(ref idxname, ref keyname))
963 -
                if (idxname == "test_index" && keyname == "2") =>
964 -
            {
965 -
                true
966 -
            }
967 -
            _ => false,
968 -
        });
969 -
    }
970 -
971 -
    #[test]
972 -
    fn test_group_key_remove() {
973 -
        let mut keeper = MockIndexKeeper::<u8, u8>::new_mode(ValueMode::CLUSTER);
974 -
        let mut changes = Vec::new();
975 -
        changes.push(KeyChanges::new(2, vec![ValueChange::ADD(1), ValueChange::ADD(2)]));
976 -
        keeper.apply(&changes).unwrap();
977 -
        assert_eq!(keeper.get(&2).ok(), Some(Some(Value::CLUSTER(vec![1, 2]))));
978 -
979 -
        let mut changes = Vec::new();
980 -
        changes.push(KeyChanges::new(2, vec![ValueChange::REMOVE(None)]));
981 -
        keeper.apply(&changes).unwrap();
982 -
        assert_eq!(keeper.get(&2).ok(), Some(None));
983 -
    }
984 -
985 -
    #[test]
986 -
    fn test_many_add_remove() {
987 -
        let mut keeper = MockIndexKeeper::<u8, u8>::new();
988 -
        let mut changes = Vec::new();
989 -
        for i in 0..200 {
990 -
            changes.push(KeyChanges::single_add(i, i));
991 -
        }
992 -
        changes.sort_by_key(|k| k.k);
993 -
        keeper.apply(&changes).unwrap();
994 -
        print_tree(&mut keeper).unwrap();
995 -
        assert_eq!(keeper.get(&2).ok(), Some(Some(Value::SINGLE(2))));
996 -
        assert_eq!(keeper.get(&100).ok(), Some(Some(Value::SINGLE(100))));
997 -
        assert_eq!(keeper.get(&201).ok(), Some(None));
998 -
        let mut changes = Vec::new();
999 -
        for i in 0..200 {
1000 -
            changes.push(KeyChanges::single_delete(i, Some(i)));
1001 -
        }
1002 -
        changes.sort_by_key(|k| k.k);
1003 -
        keeper.apply(&changes).unwrap();
1004 -
        print_tree(&mut keeper).unwrap();
1005 -
        assert_eq!(keeper.get_root().ok(), Some(None));
1006 -
        assert_eq!(keeper.get(&2).ok(), Some(None));
1007 -
        assert_eq!(keeper.get(&100).ok(), Some(None));
1008 -
    }
1009 -
1010 -
    #[test]
1011 -
    fn test_many_add_remove_multiple_times() {
1012 -
        let mut keeper = MockIndexKeeper::<u8, u8>::new();
1013 -
        let mut changes = Vec::new();
1014 -
        let mut rchanges = Vec::new();
1015 -
        for n in 0..8 {
1016 -
            for i in 0..20 {
1017 -
                changes.push(KeyChanges::single_add(i, i));
1018 -
                rchanges.push(KeyChanges::single_delete(i, Some(i)));
1019 -
            }
1020 -
            changes.sort_by_key(|k| k.k);
1021 -
            keeper.apply(&changes).unwrap();
1022 -
            assert_eq!(keeper.get(&(n + 2)).ok(), Some(Some(Value::SINGLE(n + 2))));
1023 -
            rchanges.sort_by_key(|k| k.k);
1024 -
            keeper.apply(&rchanges).unwrap();
1025 -
            assert_eq!(keeper.get(&(n + 2)).ok(), Some(None));
1026 -
        }
1027 -
    }
1028 -
1029 -
    #[test]
1030 -
    fn test_iter_from() {
1031 -
        let mut keeper = MockIndexKeeper::<u8, u8>::new();
1032 -
        let mut changes = Vec::new();
1033 -
        for i in 0..50 {
1034 -
            changes.push(KeyChanges::single_add(i, i));
1035 -
        }
1036 -
        keeper.apply(&changes).unwrap();
1037 -
        print_tree(&mut keeper).unwrap();
1038 -
        let mut iter = keeper.iter_from(Bound::Included(&5)).unwrap();
1039 -
        let next = iter.iter.next();
1040 -
1041 -
        assert_eq!(5, next.unwrap().key);
1042 -
        let next = iter.iter.next();
1043 -
        assert_eq!(6, next.unwrap().key);
1044 -
        let mut last_val = None;
1045 -
        for v in iter.iter {
1046 -
            last_val = Some(v);
1047 -
        }
1048 -
        let mut next_page = keeper
1049 -
            .iter_from(Bound::Excluded(&last_val.clone().unwrap().key))
1050 -
            .unwrap();
1051 -
        let next = next_page.iter.next();
1052 -
        assert_eq!(last_val.unwrap().key + 1, next.unwrap().key);
1053 -
    }
1054 -
1055 -
    #[test]
1056 -
    fn test_iter() {
1057 -
        let mut keeper = MockIndexKeeper::<u8, u8>::new();
1058 -
        let mut changes = Vec::new();
1059 -
        for i in 0..50 {
1060 -
            changes.push(KeyChanges::single_add(i, i));
1061 -
        }
1062 -
        keeper.apply(&changes).unwrap();
1063 -
        print_tree(&mut keeper).unwrap();
1064 -
        let mut iter = keeper.iter_from(Bound::Unbounded).unwrap();
1065 -
        let next = iter.iter.next();
1066 -
1067 -
        assert_eq!(0, next.unwrap().key);
1068 -
        let mut last_val = None;
1069 -
        for v in iter.iter {
1070 -
            last_val = Some(v);
1071 -
        }
1072 -
        let mut next_page = keeper
1073 -
            .iter_from(Bound::Excluded(&last_val.clone().unwrap().key))
1074 -
            .unwrap();
1075 -
        let next = next_page.iter.next();
1076 -
        assert_eq!(last_val.unwrap().key + 1, next.unwrap().key);
1077 -
        let mut last_val = None;
1078 -
        for v in next_page.iter {
1079 -
            last_val = Some(v);
1080 -
        }
1081 -
        assert_eq!(last_val.unwrap().key, 49);
1082 -
        let mut next_page = keeper.iter_from(Bound::Excluded(&49)).unwrap();
1083 -
        assert!(next_page.iter.next().is_none());
1084 -
    }
1085 -
1086 -
    #[test]
1087 -
    fn test_a_lot_add_remove_multiple_times() {
1088 -
        let mut keeper = MockIndexKeeper::<u32, u32>::new();
1089 -
        let mut changes = Vec::new();
1090 -
        let mut remove = Vec::new();
1091 -
        for n in 1..30 {
1092 -
            for i in 1..200 {
1093 -
                changes.push(KeyChanges::single_add(i * n, i * n));
1094 -
                if i % 2 == 0 {
1095 -
                    remove.push(KeyChanges::single_delete(i * n, Some(i * n)));
1096 -
                }
1097 -
            }
1098 -
            changes.sort_by_key(|k| k.k);
1099 -
            keeper.apply(&changes).unwrap();
1100 -
            assert_eq!(keeper.get(&(2 * n)).ok(), Some(Some(Value::SINGLE(2 * n))));
1101 -
            assert_eq!(keeper.get(&(100 * n)).ok(), Some(Some(Value::SINGLE(100 * n))));
1102 -
            assert_eq!(keeper.get(&20001).ok(), Some(None));
1103 -
            remove.sort_by_key(|k| k.k);
1104 -
            keeper.apply(&remove).unwrap();
1105 -
            assert_eq!(keeper.get(&(2 * n)).ok(), Some(None));
1106 -
            assert_eq!(keeper.get(&(100 * n)).ok(), Some(None));
1107 -
        }
1108 -
    }
1109 -
1110 -
    #[test]
1111 -
    fn test_big_tree() {
1112 -
        let mut keeper = MockIndexKeeper::<u32, u32>::new();
1113 -
        for n in 1..20 {
1114 -
            let mut changes = Vec::new();
1115 -
            let mut remove = Vec::new();
1116 -
            for i in 1..301 {
1117 -
                changes.push(KeyChanges::single_add(i * n, i * n));
1118 -
                if i % 2 == 0 {
1119 -
                    remove.push(KeyChanges::single_delete(i * n, Some(i * n)));
1120 -
                }
1121 -
            }
1122 -
            keeper.apply(&changes).unwrap();
1123 -
            assert_eq!(keeper.get(&(2 * n)).ok(), Some(Some(Value::SINGLE(2 * n))));
1124 -
            assert_eq!(keeper.get(&(100 * n)).ok(), Some(Some(Value::SINGLE(100 * n))));
1125 -
            assert_eq!(keeper.get(&20001).ok(), Some(None));
1126 -
1127 -
            keeper.apply(&remove).unwrap();
1128 -
            assert_eq!(keeper.get(&(2 * n)).ok(), Some(None));
1129 -
            assert_eq!(keeper.get(&(100 * n)).ok(), Some(None));
1130 -
        }
1131 -
    }
1132 -
}
610 +
mod tests;
Files Coverage
src 92.85%
tests 98.56%
Project Totals (30 files) 93.87%
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