hidane-core 0.2.2

Firestore data model for hidane: value ordering and order-preserving key encoding
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
//! In-memory engine: ordered maps of versioned documents, guarded per database.

use std::{
    collections::{BTreeMap, BTreeSet, HashMap},
    ops::{Bound, ControlFlow},
    sync::{Arc, PoisonError, RwLock},
    time::{Duration, SystemTime, UNIX_EPOCH},
};

use hidane_proto::ordered::Fields;

use super::{
    Change, Commit, ListItem, ListOptions, ReadTime, Store, StoreError, StoredDocument, Visit,
    WriteBatch,
};
use crate::{
    key::{encode_escaped, encode_path, encode_path_prefix, path_subtree_end},
    path::ResourcePath,
};

type Clock = Box<dyn Fn() -> i64 + Send + Sync>;

/// How far back reads may go by default. Firestore serves stale reads up to one hour old.
pub const DEFAULT_RETENTION: Duration = Duration::from_secs(60 * 60);

pub struct MemoryStore {
    databases: RwLock<HashMap<String, Arc<RwLock<Database>>>>,
    clock: Clock,
    retention_micros: i64,
}

#[derive(Default)]
struct Database {
    /// Commit time of the last commit, in microseconds.
    last_commit: i64,
    /// Keyed by `encode_path(document path)`: iteration order is `__name__` order.
    documents: BTreeMap<Vec<u8>, Record>,
    /// `encode_escaped(collection id) ++ encode_path(document path)` for every key in
    /// `documents`, so a collection group is one key range in full-path order.
    groups: BTreeSet<Vec<u8>>,
}

impl Database {
    /// Whether any document at or below `prefix` exists at `time`.
    fn subtree_has_live(&self, prefix: &[u8], time: i64) -> bool {
        self.documents
            .range(prefix.to_vec()..path_subtree_end(prefix))
            .any(|(_, record)| record.at(time).is_some())
    }
}

struct Record {
    path: Arc<ResourcePath>,
    /// Ascending by commit time; `None` marks a deletion.
    versions: Vec<(i64, Option<Arc<StoredDocument>>)>,
}

impl Record {
    fn at(&self, time: i64) -> Option<&Arc<StoredDocument>> {
        let newer = self.versions.partition_point(|(t, _)| *t <= time);
        newer
            .checked_sub(1)
            .and_then(|i| self.versions[i].1.as_ref())
    }

    fn latest(&self) -> Option<&Arc<StoredDocument>> {
        self.versions.last().and_then(|(_, doc)| doc.as_ref())
    }

    /// Drops versions no read can reach any more: everything older than the newest version at
    /// or before `cutoff`.
    fn prune(&mut self, cutoff: i64) {
        let reachable_from = self.versions.partition_point(|(t, _)| *t <= cutoff);
        if reachable_from > 1 {
            self.versions.drain(..reachable_from - 1);
        }
    }
}

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

impl MemoryStore {
    pub fn new() -> Self {
        Self::with_clock(system_micros)
    }

    /// A store whose "now" comes from `clock` (microseconds since the epoch). Commit times are
    /// still strictly increasing if the clock stands still or goes backwards.
    pub fn with_clock(clock: impl Fn() -> i64 + Send + Sync + 'static) -> Self {
        Self {
            databases: RwLock::default(),
            clock: Box::new(clock),
            retention_micros: micros(DEFAULT_RETENTION),
        }
    }

    /// How long old versions stay readable after they are overwritten.
    #[must_use]
    pub fn with_retention(mut self, retention: Duration) -> Self {
        self.retention_micros = micros(retention);
        self
    }

    fn existing(&self, database: &str) -> Option<Arc<RwLock<Database>>> {
        self.databases
            .read()
            .unwrap_or_else(PoisonError::into_inner)
            .get(database)
            .cloned()
    }

    fn get_or_create(&self, database: &str) -> Arc<RwLock<Database>> {
        if let Some(db) = self.existing(database) {
            return db;
        }
        self.databases
            .write()
            .unwrap_or_else(PoisonError::into_inner)
            .entry(database.to_owned())
            .or_default()
            .clone()
    }

    fn read<R>(&self, database: &str, empty: R, f: impl FnOnce(&Database) -> R) -> R {
        match self.existing(database) {
            Some(db) => f(&db.read().unwrap_or_else(PoisonError::into_inner)),
            None => empty,
        }
    }
}

fn system_micros() -> i64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |d| i64::try_from(d.as_micros()).unwrap_or(i64::MAX))
}

fn micros(d: Duration) -> i64 {
    i64::try_from(d.as_micros()).unwrap_or(i64::MAX)
}

impl Store for MemoryStore {
    fn latest_read_time(&self, database: &str) -> ReadTime {
        let last = self.read(database, 0, |db| db.last_commit);
        ReadTime((self.clock)().max(last))
    }

    fn earliest_read_time(&self, database: &str) -> ReadTime {
        ReadTime(
            self.latest_read_time(database)
                .0
                .saturating_sub(self.retention_micros),
        )
    }

    fn get(
        &self,
        database: &str,
        path: &ResourcePath,
        at: ReadTime,
    ) -> Option<Arc<StoredDocument>> {
        self.read(database, None, |db| {
            db.documents
                .get(&encode_path(path.segments()))
                .and_then(|record| record.at(at.0))
                .cloned()
        })
    }

    fn list_collection(
        &self,
        database: &str,
        collection: &ResourcePath,
        at: ReadTime,
        options: ListOptions<'_>,
        visit: &mut dyn FnMut(ListItem<'_>) -> ControlFlow<()>,
    ) {
        self.read(database, (), |db| {
            let prefix = encode_path_prefix(collection.segments());
            let end = Bound::Excluded(path_subtree_end(&prefix));
            let depth = collection.len() + 1;
            let mut start = match options.after {
                Some(after) => {
                    Bound::Included(path_subtree_end(&encode_path_prefix(after.segments())))
                }
                None => Bound::Included(prefix),
            };
            'scan: loop {
                // Each pass handles one child of the collection, then seeks past its subtree.
                let Some((_, record)) = db.documents.range((start.clone(), end.clone())).next()
                else {
                    return;
                };
                let child = encode_path_prefix(record.path.segments().take(depth));
                let live = (record.path.len() == depth)
                    .then(|| record.at(at.0))
                    .flatten();
                let flow = match live {
                    Some(doc) => visit(ListItem::Document(doc)),
                    None if options.include_missing && db.subtree_has_live(&child, at.0) => {
                        let path = ResourcePath::from_segments(record.path.segments().take(depth));
                        visit(ListItem::Missing(&path))
                    }
                    None => ControlFlow::Continue(()),
                };
                if flow.is_break() {
                    return;
                }
                start = Bound::Included(path_subtree_end(&child));
                continue 'scan;
            }
        });
    }

    fn scan_collection_group(
        &self,
        database: &str,
        parent: &ResourcePath,
        collection_id: &str,
        at: ReadTime,
        visit: &mut Visit<'_>,
    ) {
        self.read(database, (), |db| {
            let mut prefix = encode_escaped(collection_id.as_bytes());
            let group_len = prefix.len();
            prefix.extend(encode_path_prefix(parent.segments()));
            let end = path_subtree_end(&prefix);
            for entry in db.groups.range(prefix..end) {
                let doc = db
                    .documents
                    .get(&entry[group_len..])
                    // The parent itself is not under the parent.
                    .filter(|record| record.path.len() > parent.len())
                    .and_then(|record| record.at(at.0));
                if let Some(doc) = doc
                    && visit(doc).is_break()
                {
                    return;
                }
            }
        });
    }

    fn scan_descendants(
        &self,
        database: &str,
        parent: &ResourcePath,
        at: ReadTime,
        visit: &mut Visit<'_>,
    ) {
        self.read(database, (), |db| {
            let start = Bound::Excluded(encode_path(parent.segments()));
            let end = Bound::Excluded(path_subtree_end(&encode_path_prefix(parent.segments())));
            for (_, record) in db.documents.range((start, end)) {
                if let Some(doc) = record.at(at.0)
                    && visit(doc).is_break()
                {
                    return;
                }
            }
        });
    }

    fn list_collection_ids(
        &self,
        database: &str,
        parent: &ResourcePath,
        at: ReadTime,
    ) -> Vec<String> {
        self.read(database, Vec::new(), |db| {
            let prefix = encode_path_prefix(parent.segments());
            let end = Bound::Excluded(path_subtree_end(&prefix));
            let mut start = Bound::Excluded(encode_path(parent.segments()));
            let mut ids = Vec::new();
            'scan: loop {
                for (_, record) in db.documents.range((start.clone(), end.clone())) {
                    if record.at(at.0).is_none() {
                        continue;
                    }
                    // A live document somewhere under this collection: record the collection
                    // and skip the rest of it.
                    let collection = record.path.segments().take(parent.len() + 1);
                    if let Some(id) = collection.clone().last() {
                        ids.push(id.to_owned());
                    }
                    let collection_prefix = encode_path_prefix(collection);
                    start = Bound::Included(path_subtree_end(&collection_prefix));
                    continue 'scan;
                }
                return ids;
            }
        })
    }

    fn commit(
        &self,
        database: &str,
        write: &mut dyn FnMut(&mut dyn WriteBatch) -> Result<(), StoreError>,
    ) -> Result<Commit, StoreError> {
        let db = self.get_or_create(database);
        let mut db = db.write().unwrap_or_else(PoisonError::into_inner);
        let commit_time = (self.clock)().max(db.last_commit + 1);

        let mut batch = MemoryBatch {
            db: &db,
            commit_time,
            pending: BTreeMap::new(),
        };
        write(&mut batch)?;
        let pending = batch.pending;

        let cutoff = commit_time.saturating_sub(self.retention_micros);
        let mut changes = Vec::with_capacity(pending.len());
        for (key, (path, after)) in pending {
            let before = db.documents.get(&key).and_then(Record::latest).cloned();
            if before.is_none() && after.is_none() {
                continue; // deleting a document that does not exist changes nothing
            }
            if !db.documents.contains_key(&key)
                && let Some(collection_id) = path.collection_id()
            {
                let mut group_key = encode_escaped(collection_id.as_bytes());
                group_key.extend_from_slice(&key);
                db.groups.insert(group_key);
            }
            let record = db.documents.entry(key).or_insert_with(|| Record {
                path: Arc::clone(&path),
                // Most documents never get a second version inside the retention window.
                versions: Vec::with_capacity(1),
            });
            record.versions.push((commit_time, after.clone()));
            record.prune(cutoff);
            changes.push(Change {
                path,
                before,
                after,
            });
        }
        db.last_commit = commit_time;
        Ok(Commit {
            commit_time: ReadTime(commit_time),
            changes,
        })
    }

    fn clear_database(&self, database: &str) -> ReadTime {
        let db = self.get_or_create(database);
        let mut db = db.write().unwrap_or_else(PoisonError::into_inner);
        let at = (self.clock)().max(db.last_commit + 1);
        db.documents.clear();
        db.groups.clear();
        db.last_commit = at;
        ReadTime(at)
    }

    fn database_names(&self) -> Vec<String> {
        self.databases
            .read()
            .unwrap_or_else(PoisonError::into_inner)
            .keys()
            .cloned()
            .collect()
    }

    fn clear(&self) {
        self.databases
            .write()
            .unwrap_or_else(PoisonError::into_inner)
            .clear();
    }
}

/// A pending write: the document's shared path and its new version (`None` deletes it).
type PendingWrite = (Arc<ResourcePath>, Option<Arc<StoredDocument>>);

struct MemoryBatch<'a> {
    db: &'a Database,
    commit_time: i64,
    /// Keyed like `Database::documents`.
    pending: BTreeMap<Vec<u8>, PendingWrite>,
}

impl MemoryBatch<'_> {
    /// The path as already shared by the document's earlier versions, if any.
    fn shared_path(&self, key: &[u8], path: &ResourcePath) -> Arc<ResourcePath> {
        self.pending
            .get(key)
            .map(|(p, _)| Arc::clone(p))
            .or_else(|| self.db.documents.get(key).map(|r| Arc::clone(&r.path)))
            .unwrap_or_else(|| Arc::new(path.clone()))
    }
}

impl WriteBatch for MemoryBatch<'_> {
    fn commit_time(&self) -> ReadTime {
        ReadTime(self.commit_time)
    }

    fn get(&self, path: &ResourcePath) -> Option<Arc<StoredDocument>> {
        let key = encode_path(path.segments());
        match self.pending.get(&key) {
            Some((_, pending)) => pending.clone(),
            None => self
                .db
                .documents
                .get(&key)
                .and_then(Record::latest)
                .cloned(),
        }
    }

    fn set(&mut self, path: &ResourcePath, fields: Fields) {
        let now = ReadTime(self.commit_time).to_timestamp();
        let create_time = self
            .get(path)
            .map_or_else(|| now, |existing| existing.create_time);
        let key = encode_path(path.segments());
        let shared = self.shared_path(&key, path);
        let document = StoredDocument::new(Arc::clone(&shared), fields, create_time, now);
        self.pending.insert(key, (shared, Some(Arc::new(document))));
    }

    fn delete(&mut self, path: &ResourcePath) {
        let key = encode_path(path.segments());
        let shared = self.shared_path(&key, path);
        self.pending.insert(key, (shared, None));
    }
}

#[cfg(test)]
mod tests;