persy 1.3.0

Transactional Persistence Engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
use crate::{
    address::segment::segment_page_iterator::SegmentPageIterator,
    allocator::Allocator,
    error::PERes,
    id::{RecRef, SegmentId},
    journal::{records::FreedPage, Journal, JournalId},
};
use std::{
    cmp::Ordering,
    collections::{hash_map::Entry, HashMap},
    sync::Mutex,
};

pub type SnapshotId = u64;

#[derive(Clone, Debug)]
pub struct SnapshotEntry {
    id: RecRef,
    case: EntryCase,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Change {
    pub pos: u64,
    pub version: u16,
}
#[derive(Clone, Debug, PartialEq)]
pub enum EntryCase {
    Change(Change),
    Insert,
}

impl SnapshotEntry {
    pub fn change(id: &RecRef, pos: u64, version: u16) -> SnapshotEntry {
        SnapshotEntry {
            id: *id,
            case: EntryCase::Change(Change { pos, version }),
        }
    }
    pub fn insert(id: &RecRef) -> SnapshotEntry {
        SnapshotEntry {
            id: *id,
            case: EntryCase::Insert,
        }
    }
}

#[derive(Clone, Debug)]
pub struct SegmentSnapshot {
    name: String,
    id: SegmentId,
    first_page: u64,
}
impl SegmentSnapshot {
    pub fn new(name: &str, id: SegmentId, first_page: u64) -> SegmentSnapshot {
        SegmentSnapshot {
            name: name.to_string(),
            id,
            first_page,
        }
    }
}

#[derive(Clone, Debug)]
pub struct SnapshotData {
    snapshot_id: SnapshotId,
    journal_id: Option<JournalId>,
    entries: Option<Vec<SnapshotEntry>>,
    freed_pages: Option<Vec<FreedPage>>,
    segments: Option<HashMap<SegmentId, SegmentSnapshot>>,
    segments_name: Option<HashMap<String, SegmentSnapshot>>,
    reference_count: u32,
}

impl SnapshotData {
    fn new(
        id: SnapshotId,
        journal_id: JournalId,
        entries: Vec<SnapshotEntry>,
        freed_pages: Vec<FreedPage>,
        reference_count: u32,
    ) -> Self {
        Self {
            snapshot_id: id,
            journal_id: Some(journal_id),
            entries: Some(entries),
            freed_pages: Some(freed_pages),
            segments: None,
            segments_name: None,
            reference_count,
        }
    }
    fn new_read(id: SnapshotId, reference_count: u32) -> Self {
        Self {
            snapshot_id: id,
            journal_id: None,
            entries: None,
            freed_pages: None,
            segments: None,
            segments_name: None,
            reference_count,
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct RecordVersion {
    snapshot_id: SnapshotId,
    pub case: EntryCase,
}

#[derive(Debug)]
pub struct InternalSnapshots {
    mapping: HashMap<RecRef, Vec<RecordVersion>>,
    active_snapshots: Vec<SnapshotData>,
    snapshot_sequence: u64,
}

impl InternalSnapshots {
    fn next_snapshot_id(&mut self) -> SnapshotId {
        let snapshot_id = self.snapshot_sequence;
        // This should be the default behavior of the u64 anyway, let's hope the compiler trows
        // this away
        if self.snapshot_sequence == u64::MAX {
            self.snapshot_sequence = 0;
        } else {
            self.snapshot_sequence += 1;
        }
        snapshot_id
    }
    fn search(&self, snapshot_id: SnapshotId) -> Result<usize, usize> {
        let snapshot_sequence = self.snapshot_sequence;
        self.active_snapshots
            .binary_search_by(|n| search(n.snapshot_id, snapshot_id, snapshot_sequence))
    }
    fn acquire_last_snapshot(&mut self) -> SnapshotId {
        let last = self.snapshot_sequence - 1;
        self.acquire_snapshot(last);
        last
    }
    fn acquire_snapshot(&mut self, snapshot_id: SnapshotId) {
        if let Ok(pos) = self.search(snapshot_id) {
            if let Some(p) = self.active_snapshots.get_mut(pos) {
                p.reference_count += 1;
            } else {
                unreachable!()
            }
        } else {
            panic!("try to acquire a not existing snapshot")
        }
    }

    fn clear_from(&mut self, snapshot_id: SnapshotId) -> PERes<(Option<Vec<FreedPage>>, Option<Vec<JournalId>>)> {
        let snapshot_sequence = self.snapshot_sequence;
        if let Ok(index) = self.search(snapshot_id) {
            let size = index + 1;
            let mut free_pages: Option<Vec<FreedPage>> = None;
            let mut journal_ids: Option<Vec<JournalId>> = None;
            let left_off = self.active_snapshots.split_off(size);
            let old = std::mem::replace(&mut self.active_snapshots, left_off);
            for tx in old {
                if let Some(id) = tx.journal_id.clone() {
                    if let Some(ids) = &mut journal_ids {
                        ids.push(id.clone());
                    } else {
                        journal_ids = Some(vec![id.clone()]);
                    }
                }
                if let Some(entries) = tx.entries {
                    for record in entries {
                        if let Entry::Occupied(mut v) = self.mapping.entry(record.id) {
                            match v
                                .get()
                                .binary_search_by(|n| search(n.snapshot_id, snapshot_id, snapshot_sequence))
                            {
                                Ok(index) => {
                                    v.get_mut().drain(..=index);
                                    if v.get().is_empty() {
                                        v.remove();
                                    }
                                }
                                Err(_index) => {
                                    v.remove();
                                }
                            }
                        }
                    }
                }
                if let Some(pages) = tx.freed_pages {
                    if let Some(free) = &mut free_pages {
                        free.extend(pages);
                    } else {
                        free_pages = Some(pages);
                    }
                }
            }
            Ok((free_pages, journal_ids))
        } else {
            Ok((None, None))
        }
    }

    fn release(&mut self, snapshot_id: SnapshotId) -> PERes<(Option<Vec<FreedPage>>, Option<Vec<JournalId>>)> {
        //TODO: This work fine but can cause problems if double release is called for the same id,
        //to refactor to something a bit more safe
        let mut clear_id = None;
        if let Ok(index) = self.search(snapshot_id) {
            let mut loop_index = index;
            while let Some(snap) = self.active_snapshots.get_mut(loop_index) {
                snap.reference_count -= 1;
                if snap.reference_count > 0 {
                    break;
                }
                clear_id = Some(snap.snapshot_id);
                loop_index += 1;
            }
        }
        if let Some(c_id) = clear_id {
            self.clear_from(c_id)
        } else {
            Ok((None, None))
        }
    }
}

pub struct Snapshots {
    lock: Mutex<InternalSnapshots>,
}

pub fn search(value: u64, value1: u64, top: u64) -> Ordering {
    if value > top {
        if value1 > top {
            value.cmp(&value1)
        } else {
            Ordering::Less
        }
    } else if value1 > top {
        Ordering::Greater
    } else {
        value.cmp(&value1)
    }
}

impl Default for Snapshots {
    fn default() -> Snapshots {
        Self::new()
    }
}

impl Snapshots {
    pub fn new() -> Snapshots {
        Snapshots {
            lock: Mutex::new(InternalSnapshots {
                mapping: HashMap::new(),
                active_snapshots: Vec::new(),
                snapshot_sequence: 0,
            }),
        }
    }

    pub fn acquire(&self, snapshot_id: SnapshotId) -> PERes<()> {
        let mut lock = self.lock.lock()?;
        lock.acquire_snapshot(snapshot_id);
        Ok(())
    }

    pub fn current_snapshot(&self) -> PERes<SnapshotId> {
        let mut lock = self.lock.lock()?;
        if lock.active_snapshots.is_empty() {
            let snapshot_id = lock.next_snapshot_id();

            let reference_count = if lock.active_snapshots.is_empty() { 1 } else { 2 };
            let snapshot = SnapshotData::new_read(snapshot_id, reference_count);
            if let Err(index) = lock.search(snapshot_id) {
                lock.active_snapshots.insert(index, snapshot);
            }
            Ok(snapshot_id)
        } else {
            Ok(lock.acquire_last_snapshot())
        }
    }

    pub fn read_snapshot(&self) -> PERes<SnapshotId> {
        let mut lock = self.lock.lock()?;
        let snapshot_id = lock.next_snapshot_id();

        let reference_count = if lock.active_snapshots.is_empty() { 1 } else { 2 };
        let snapshot = SnapshotData::new_read(snapshot_id, reference_count);
        if let Err(index) = lock.search(snapshot_id) {
            lock.active_snapshots.insert(index, snapshot);
        }
        Ok(snapshot_id)
    }

    pub fn snapshot(
        &self,
        entries: Vec<SnapshotEntry>,
        freed_pages: Vec<FreedPage>,
        journal_id: JournalId,
    ) -> PERes<SnapshotId> {
        let mut lock = self.lock.lock()?;
        let snapshot_id = lock.next_snapshot_id();
        for entry in &entries {
            let to_add = RecordVersion {
                snapshot_id,
                case: entry.case.clone(),
            };

            let snapshot_sequence = lock.snapshot_sequence;
            match lock.mapping.entry(entry.id) {
                Entry::Occupied(mut v) => {
                    let vec = v.get_mut();
                    if let Err(index) = vec.binary_search_by(|n| search(n.snapshot_id, snapshot_id, snapshot_sequence))
                    {
                        vec.insert(index, to_add);
                    }
                }
                Entry::Vacant(e) => {
                    e.insert(vec![to_add]);
                }
            }
        }

        let reference_count = if lock.active_snapshots.is_empty() { 1 } else { 2 };
        let snapshot = SnapshotData::new(snapshot_id, journal_id, entries, freed_pages, reference_count);
        if let Err(index) = lock.search(snapshot_id) {
            lock.active_snapshots.insert(index, snapshot);
        }
        Ok(snapshot_id)
    }

    pub fn fill_segments(&self, snapshot_id: SnapshotId, segments: &[SegmentSnapshot]) -> PERes<()> {
        let mut segments_id = HashMap::new();
        let mut segments_name = HashMap::new();
        for segment in segments {
            segments_id.insert(segment.id, segment.clone());
            segments_name.insert(segment.name.clone(), segment.clone());
        }
        let mut lock = self.lock.lock()?;
        if let Ok(index) = lock.search(snapshot_id) {
            if let Some(snap) = lock.active_snapshots.get_mut(index) {
                snap.segments = Some(segments_id);
                snap.segments_name = Some(segments_name);
            }
        }
        Ok(())
    }

    pub fn solve_segment_id(&self, snapshot_id: SnapshotId, segment: &str) -> PERes<Option<SegmentId>> {
        let mut lock = self.lock.lock()?;
        let res = if let Ok(index) = lock.search(snapshot_id) {
            if let Some(snap) = lock.active_snapshots.get_mut(index) {
                if let Some(segs) = &snap.segments_name {
                    segs.get(segment).map(|sd| sd.id)
                } else {
                    None
                }
            } else {
                None
            }
        } else {
            None
        };
        Ok(res)
    }

    pub fn solve_segment_name(&self, snapshot_id: SnapshotId, segment_id: SegmentId) -> PERes<Option<String>> {
        let mut lock = self.lock.lock()?;
        let res = if let Ok(index) = lock.search(snapshot_id) {
            if let Some(snap) = lock.active_snapshots.get_mut(index) {
                if let Some(segs) = &snap.segments {
                    segs.get(&segment_id).map(|sd| sd.name.clone())
                } else {
                    None
                }
            } else {
                None
            }
        } else {
            None
        };
        Ok(res)
    }

    pub fn scan(&self, snapshot_id: SnapshotId, segment_id: SegmentId) -> PERes<Option<SegmentPageIterator>> {
        let mut lock = self.lock.lock()?;
        let res = if let Ok(index) = lock.search(snapshot_id) {
            if let Some(snap) = lock.active_snapshots.get_mut(index) {
                if let Some(segs) = &snap.segments {
                    segs.get(&segment_id)
                        .map(|sd| SegmentPageIterator::snapshot(sd.first_page))
                } else {
                    None
                }
            } else {
                None
            }
        } else {
            None
        };
        Ok(res)
    }

    pub fn list(&self, snapshot_id: SnapshotId) -> PERes<Vec<(String, SegmentId)>> {
        let mut lock = self.lock.lock()?;
        let res = if let Ok(index) = lock.search(snapshot_id) {
            if let Some(snap) = lock.active_snapshots.get_mut(index) {
                if let Some(segs) = &snap.segments {
                    segs.values()
                        .map(|data| (data.name.clone(), data.id))
                        .collect::<Vec<_>>()
                } else {
                    Vec::new()
                }
            } else {
                Vec::new()
            }
        } else {
            Vec::new()
        };
        Ok(res)
    }

    pub fn read(&self, snapshot_id: SnapshotId, id: &RecRef) -> PERes<Option<RecordVersion>> {
        let lock = self.lock.lock()?;
        let snapshot_sequence = lock.snapshot_sequence;
        Ok(if let Some(v) = lock.mapping.get(id) {
            let index = match v.binary_search_by(|n| search(n.snapshot_id, snapshot_id, snapshot_sequence)) {
                Ok(index) => index,
                Err(index) => index,
            };
            v.get(index).cloned()
        } else {
            None
        })
    }

    #[allow(unused)]
    fn clear_from(&self, snapshot_id: SnapshotId) -> PERes<(Option<Vec<FreedPage>>, Option<Vec<JournalId>>)> {
        self.lock.lock()?.clear_from(snapshot_id)
    }

    fn release(&self, snapshot_id: SnapshotId) -> PERes<(Option<Vec<FreedPage>>, Option<Vec<JournalId>>)> {
        self.lock.lock()?.release(snapshot_id)
    }
}

pub fn release_snapshot(id: SnapshotId, snapshots: &Snapshots, allocator: &Allocator, journal: &Journal) -> PERes<()> {
    let (to_free, to_clean) = snapshots.release(id)?;
    if let Some(to_free) = to_free {
        allocator.free_pages(to_free.into_iter().map(|p| p.page))?;
    }
    if let Some(to_clean) = to_clean {
        journal.finished_to_clean(&to_clean)?;
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::{search, Change, EntryCase, RecordVersion, SegmentSnapshot, SnapshotEntry, Snapshots};
    use crate::{
        id::{RecRef, SegmentId},
        journal::{records::FreedPage, JournalId},
    };
    use std::cmp::Ordering;

    #[test]
    fn test_search() {
        assert_eq!(search(10, 20, 40), Ordering::Less);
        assert_eq!(search(20, 10, 40), Ordering::Greater);
        assert_eq!(search(10, 30, 20), Ordering::Greater);
        assert_eq!(search(30, 10, 20), Ordering::Less);
        assert_eq!(search(20, 20, 20), Ordering::Equal);
        assert_eq!(search(20, 19, 20), Ordering::Greater);
        assert_eq!(search(20, 21, 20), Ordering::Greater);
        assert_eq!(search(21, 21, 20), Ordering::Equal);
        assert_eq!(search(19, 19, 20), Ordering::Equal);
    }

    #[test]
    fn add_and_read() {
        let snap = Snapshots::new();
        let mut records = Vec::new();
        let mut freed_pages = Vec::new();
        for x in 0..3 {
            records.push(SnapshotEntry::change(&RecRef::new(10, x), x as u64, x as u16));
            freed_pages.push(FreedPage::new(x as u64));
        }
        let tx = snap
            .snapshot(records.clone(), freed_pages.clone(), JournalId::new(0, 0))
            .unwrap();
        snap.clear_from(tx).unwrap();
        let tx = snap.snapshot(records, freed_pages, JournalId::new(0, 0)).unwrap();
        assert_eq!(
            snap.read(tx - 1, &RecRef::new(10, 2)).unwrap(),
            Some(RecordVersion {
                snapshot_id: 1,
                case: EntryCase::Change(Change { pos: 2, version: 2 }),
            })
        );

        assert_eq!(
            snap.read(tx - 1, &RecRef::new(10, 1)).unwrap(),
            Some(RecordVersion {
                snapshot_id: 1,
                case: EntryCase::Change(Change { pos: 1, version: 1 }),
            })
        );

        assert_eq!(
            snap.read(tx, &RecRef::new(10, 2)).unwrap(),
            Some(RecordVersion {
                snapshot_id: 1,
                case: EntryCase::Change(Change { pos: 2, version: 2 }),
            })
        );
        assert_eq!(snap.read(tx + 1, &RecRef::new(10, 2)).unwrap(), None);
        assert_eq!(snap.read(tx + 1, &RecRef::new(10, 10)).unwrap(), None);
    }

    #[test]
    fn add_and_read_multiple_tx() {
        let snap = Snapshots::new();
        let mut txs = Vec::new();
        for t in 0..5 {
            let mut records = Vec::new();
            let mut freed_pages = Vec::new();
            for x in 0..10 {
                // I skip a record for the specific tx because i need a missing record for the test
                if x != t {
                    records.push(SnapshotEntry::change(
                        &RecRef::new(10, x),
                        (10 * t + x) as u64,
                        (10 * t + x) as u16,
                    ));
                    freed_pages.push(FreedPage::new((10 * t + x) as u64));
                }
            }
            txs.push(snap.snapshot(records, freed_pages, JournalId::new(0, 0)).unwrap());
        }
        assert_eq!(
            snap.read(txs[2], &RecRef::new(10, 2)).unwrap(),
            Some(RecordVersion {
                snapshot_id: txs[3],
                case: EntryCase::Change(Change { pos: 32, version: 32 }),
            })
        );

        assert_eq!(
            snap.read(txs[3], &RecRef::new(10, 3)).unwrap(),
            Some(RecordVersion {
                snapshot_id: txs[4],
                case: EntryCase::Change(Change { pos: 43, version: 43 }),
            })
        );
    }

    #[test]
    fn test_snapshot_reference_count() {
        let snap = Snapshots::new();
        let mut records = Vec::new();
        let mut freed_pages = Vec::new();
        for x in 0..3 {
            records.push(SnapshotEntry::change(&RecRef::new(10, x), x as u64, x as u16));
            freed_pages.push(FreedPage::new(x as u64));
        }
        let first = snap.read_snapshot().unwrap();
        let tx = snap
            .snapshot(records.clone(), freed_pages, JournalId::new(0, 0))
            .unwrap();
        let last = snap.read_snapshot().unwrap();
        snap.release(tx).unwrap();
        assert_eq!(
            snap.read(first, &RecRef::new(10, 2)).unwrap(),
            Some(RecordVersion {
                snapshot_id: 1,
                case: EntryCase::Change(Change { pos: 2, version: 2 }),
            })
        );

        snap.release(first).unwrap();
        assert_eq!(snap.read(last, &RecRef::new(10, 2)).unwrap(), None);
    }

    #[test]
    fn test_clanup_after_release() {
        let snap = Snapshots::new();
        let mut snapshots_id = Vec::new();
        for s in 0..100 {
            let mut records = Vec::new();
            let mut freed_pages = Vec::new();
            for x in 0..3 {
                records.push(SnapshotEntry::change(&RecRef::new(s % 10, x), x as u64, x as u16));
                freed_pages.push(FreedPage::new(x as u64));
            }
            let tx = snap
                .snapshot(records.clone(), freed_pages, JournalId::new(0, 0))
                .unwrap();
            snapshots_id.push(tx);
        }
        for s in snapshots_id.into_iter().rev() {
            snap.release(s).unwrap();
        }
        {
            let data = snap.lock.lock().unwrap();
            assert_eq!(data.active_snapshots.len(), 0);
            assert_eq!(data.mapping.len(), 0);
        }
        // Same code twice this time release the snapshot in direct order
        let mut snapshots_id = Vec::new();
        for s in 0..100 {
            let mut records = Vec::new();
            let mut freed_pages = Vec::new();
            for x in 0..3 {
                records.push(SnapshotEntry::change(&RecRef::new(s % 10, x), x as u64, x as u16));
                freed_pages.push(FreedPage::new(x as u64));
            }
            let tx = snap
                .snapshot(records.clone(), freed_pages, JournalId::new(0, 0))
                .unwrap();
            snapshots_id.push(tx);
        }
        for s in snapshots_id {
            snap.release(s).unwrap();
        }

        let data = snap.lock.lock().unwrap();
        assert_eq!(data.active_snapshots.len(), 0);
        assert_eq!(data.mapping.len(), 0);
    }

    #[test]
    fn test_snapshot_release_clear() {
        let snap = Snapshots::new();
        let mut snaps = Vec::new();
        for i in 0..10 {
            let id = snap.read_snapshot().unwrap();
            let mut segments = Vec::new();
            let seg_id = SegmentId::new(i);
            segments.push(SegmentSnapshot::new("one", seg_id, i.into()));
            segments.push(SegmentSnapshot::new("two", seg_id, i.into()));
            snap.fill_segments(id, &segments).expect("fill works");
            snaps.push(id);
        }
        snap.release(snaps[0]).expect("release works");
        snap.release(snaps[1]).expect("release works");
        snap.release(snaps[2]).expect("release works");
        let solved = snap.solve_segment_id(snaps[3], "one").expect("solve id correctly");
        assert_eq!(solved, Some(SegmentId::new(3)));
        let data = snap.lock.lock().unwrap();
        assert_eq!(data.active_snapshots.len(), 7);
    }

    #[test]
    pub fn test_segment_snapshot_fill() {
        let snap = Snapshots::new();
        let id = snap.read_snapshot().unwrap();
        let mut segments = Vec::new();
        let seg_id = SegmentId::new(10);
        segments.push(SegmentSnapshot::new("one", seg_id, 10));
        snap.fill_segments(id, &segments).expect("fill works");
        let solved = snap.solve_segment_id(id, "one").expect("solve id correctly");
        assert_eq!(solved, Some(seg_id));
        let solved = snap.solve_segment_name(id, seg_id).expect("solve id correctly");
        assert_eq!(solved, Some("one".to_string()));
        let solved = snap.solve_segment_id(id, "two").expect("solve id correctly");
        assert_eq!(solved, None);
        let solved = snap.scan(id, seg_id).expect("solve id correctly");
        assert!(solved.is_some());
        snap.release(id).expect("release works");
    }

    #[test]
    pub fn test_segment_snapshot_fill_multiple() {
        let snap = Snapshots::new();
        let mut snaps = Vec::new();
        for i in 0..10 {
            let id = snap.read_snapshot().unwrap();
            let mut segments = Vec::new();
            let seg_id = SegmentId::new(i);
            segments.push(SegmentSnapshot::new("one", seg_id, i.into()));
            segments.push(SegmentSnapshot::new("two", seg_id, i.into()));
            snap.fill_segments(id, &segments).expect("fill works");
            snaps.push(id);
        }
        let solved = snap.solve_segment_id(snaps[3], "one").expect("solve id correctly");
        assert_eq!(solved, Some(SegmentId::new(3)));
        let solved = snap.solve_segment_id(snaps[3], "two").expect("solve id correctly");
        assert_eq!(solved, Some(SegmentId::new(3)));
        let solved = snap.solve_segment_id(snaps[6], "one").expect("solve id correctly");
        assert_eq!(solved, Some(SegmentId::new(6)));
        let solved = snap.solve_segment_id(snaps[6], "two").expect("solve id correctly");
        assert_eq!(solved, Some(SegmentId::new(6)));
    }
}