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
//! Transaction depth resolvers for custom [`Collection`](super::Collection) types.
//!
//! An index field can store either a snapshot of the field during serialization, or incremental
//! changes.
//!
//! The query engine needs to know how deep to look in the commit
//! history to accurately load an index field. These types help encode
//! meaningful transaction walks.
//!
//! Note that during queries and loads, commits are walked in _reverse
//! order_, last one first.

use crate::index::TransactionList;
use crate::object::{AEADReader, DeserializeStream, Pool};

pub trait Depth: sealed::Sealed {
    fn resolve(
        index: Pool<AEADReader>,
        transactions: TransactionList,
    ) -> Box<dyn Iterator<Item = DeserializeStream> + Sync + Send>;
}

mod sealed {
    pub trait Sealed {}

    impl Sealed for super::Incremental {}
    impl Sealed for super::Snapshot {}
}

/// Walk through the full history in reverse order. Useful for incremental types.
pub struct Incremental;

/// Only load the first (peak) commit in the list. Useful for snapshot types.
pub struct Snapshot;

#[inline(always)]
fn full_history(
    pool: Pool<AEADReader>,
    transactions: TransactionList,
) -> impl Iterator<Item = DeserializeStream> {
    transactions
        .into_iter()
        .filter_map(move |(_gen, _field, stream)| {
            pool.lease()
                .ok()
                .map(|r| DeserializeStream::new(stream.open_reader(r)))
        })
}

impl Depth for Incremental {
    #[inline(always)]
    fn resolve(
        index: Pool<AEADReader>,
        transactions: TransactionList,
    ) -> Box<dyn Iterator<Item = DeserializeStream> + Sync + Send> {
        Box::new(full_history(index, transactions))
    }
}

impl Depth for Snapshot {
    #[inline(always)]
    fn resolve(
        index: Pool<AEADReader>,
        transactions: TransactionList,
    ) -> Box<dyn Iterator<Item = DeserializeStream> + Sync + Send> {
        Box::new(full_history(index, transactions).take(1))
    }
}