tinymist_query/adt/
revision.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use std::{
    collections::HashMap,
    num::NonZeroUsize,
    sync::{Arc, OnceLock},
};

pub struct RevisionLock {
    estimated: usize,
    used: OnceLock<usize>,
}

impl RevisionLock {
    pub fn access(&self, revision: NonZeroUsize) {
        self.used
            .set(revision.get())
            .unwrap_or_else(|_| panic!("revision {revision} is determined"))
    }
}

pub struct RevisionSlot<T> {
    pub revision: usize,
    pub data: T,
}

impl<T> std::ops::Deref for RevisionSlot<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

impl<T> std::ops::DerefMut for RevisionSlot<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.data
    }
}

pub struct RevisionManager<T> {
    estimated: usize,
    locked: HashMap<usize, usize>,
    slots: Vec<Arc<RevisionSlot<T>>>,
}

impl<T> Default for RevisionManager<T> {
    fn default() -> Self {
        Self {
            estimated: 0,
            locked: Default::default(),
            slots: Default::default(),
        }
    }
}

impl<T> RevisionManager<T> {
    pub fn clear(&mut self) {
        self.slots.clear();
    }

    /// Lock the revision in *main thread*.
    #[must_use]
    pub fn lock(&mut self, used: NonZeroUsize) -> RevisionLock {
        let lock = self.lock_estimated();
        lock.access(used);
        lock
    }

    /// Lock the revision in *main thread*.
    #[must_use]
    pub fn lock_estimated(&mut self) -> RevisionLock {
        let estimated = self.estimated;
        *self.locked.entry(estimated).or_default() += 1;
        RevisionLock {
            estimated,
            used: OnceLock::new(),
        }
    }

    /// Find the last revision slot by revision number.
    pub fn find_revision(
        &mut self,
        revision: NonZeroUsize,
        f: impl FnOnce(Option<&Arc<RevisionSlot<T>>>) -> T,
    ) -> Arc<RevisionSlot<T>> {
        let slot_base = self
            .slots
            .iter()
            .filter(|slot| slot.revision <= revision.get())
            .reduce(|x, y| if x.revision > y.revision { x } else { y });

        if let Some(slot) = slot_base {
            if slot.revision == revision.get() {
                return slot.clone();
            }
        }

        let slot = Arc::new(RevisionSlot {
            revision: revision.get(),
            data: f(slot_base),
        });
        self.slots.push(slot.clone());
        self.estimated = revision.get().max(self.estimated);
        slot
    }

    pub fn unlock(&mut self, rev: &mut RevisionLock) -> Option<usize> {
        let rev = rev.estimated;
        let revision_cnt = self
            .locked
            .entry(rev)
            .or_insert_with(|| panic!("revision {rev} is not locked"));
        *revision_cnt -= 1;
        if *revision_cnt != 0 {
            return None;
        }

        self.locked.remove(&rev);
        let existing = self.locked.keys().min().copied();
        existing.or_else(||
            // if there is no locked revision, we only keep the latest revision
            self.slots
                .iter()
                .map(|slot| slot.revision)
                .max())
    }
}

pub trait RevisionManagerLike {
    fn gc(&mut self, min_rev: usize);
}

impl<T> RevisionManagerLike for RevisionManager<T> {
    fn gc(&mut self, min_rev: usize) {
        self.slots.retain(|r| r.revision >= min_rev);
    }
}