regolith 0.1.1

ACID, performance oriented, embedded key-value database engine for edge systems
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
//! Mirror mode: the database resident in linear memory, written back to
//! OPFS whole-file.
//!
//! This is the strategy defradb's OPFS backend uses, and it exists here
//! for the same reason: `createSyncAccessHandle` is worker-only, so a
//! database opened on the main thread has no synchronous path to storage
//! at all. `FileSystemWritableFileStream` is asynchronous but available
//! everywhere, so the engine runs against RAM and
//! [`super::OpfsEnv::persist`] pushes dirty files out.
//!
//! Two ceilings follow, and both are reported rather than hidden. The
//! whole database is resident, so [`super::OpfsOptions::max_resident_bytes`]
//! fails a write loudly instead of growing linear memory until the tab
//! dies (wasm pages are never returned to the host). And nothing is
//! durable until `persist()` resolves, so
//! [`crate::env::Capabilities::durable_sync`] is `false` here.
//!
//! # Physical naming
//!
//! OPFS directories are flat for regolith's purposes, so a logical path is
//! escaped into one filename: `%` becomes `%25`, then `/` becomes `%2F`
//! and `\` becomes `%5C`. Every mirror file carries the `.regolith-file-`
//! prefix so a mirror database and a slot pool can share one directory.

use std::collections::{BTreeSet, HashMap, HashSet};
use std::io;
use std::path::{Path, PathBuf};

use crate::sync::Mutex;
use wasm_bindgen::JsValue;

use super::js;

const FILE_PREFIX: &str = ".regolith-file-";

/// Escape a logical path into a single OPFS entry name.
fn encode_name(path: &Path) -> String {
    let mut out = String::from(FILE_PREFIX);
    for ch in path.to_string_lossy().chars() {
        match ch {
            '%' => out.push_str("%25"),
            '/' => out.push_str("%2F"),
            '\\' => out.push_str("%5C"),
            other => out.push(other),
        }
    }
    out
}

/// Reverse [`encode_name`]. Returns `None` for an entry that is not a
/// mirror file or whose escapes are malformed.
fn decode_name(name: &str) -> Option<PathBuf> {
    let body = name.strip_prefix(FILE_PREFIX)?;
    let bytes = body.as_bytes();
    let mut out = String::with_capacity(body.len());
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'%' {
            let code = body.get(i + 1..i + 3)?;
            match code {
                "25" => out.push('%'),
                "2F" => out.push('/'),
                "5C" => out.push('\\'),
                _ => return None,
            }
            i += 3;
        } else {
            let ch = body[i..].chars().next()?;
            out.push(ch);
            i += ch.len_utf8();
        }
    }
    Some(PathBuf::from(out))
}

/// One dirty file as `persist` will write it: the logical path, the
/// version that made it dirty, and the bytes to send.
type PendingWrite = (PathBuf, u64, Vec<u8>);

struct Entry {
    data: Vec<u8>,
    /// Bumped on every mutation so [`MirrorFs::take_persist_batch`] can
    /// tell a file that was re-dirtied during the await from one that
    /// was not.
    version: u64,
}

struct State {
    files: HashMap<PathBuf, Entry>,
    dirs: BTreeSet<PathBuf>,
    dirty: HashSet<PathBuf>,
    deleted: HashSet<PathBuf>,
    resident: usize,
    next_version: u64,
}

/// The in-memory mirror of an OPFS-backed database.
pub(super) struct MirrorFs {
    state: Mutex<State>,
    max_resident: usize,
    mount: super::sah::MountId,
}

impl Drop for MirrorFs {
    fn drop(&mut self) {
        super::sah::release_mount(self.mount);
    }
}

impl std::fmt::Debug for MirrorFs {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // `try_lock` so formatting an error while the state is held
        // cannot deadlock a single-threaded module.
        let mut out = f.debug_struct("MirrorFs");
        match self.state.try_lock() {
            Some(state) => out
                .field("files", &state.files.len())
                .field("resident_bytes", &state.resident),
            None => out.field("state", &"locked"),
        }
        .field("max_resident_bytes", &self.max_resident)
        .finish()
    }
}

impl MirrorFs {
    /// Build a mirror from files already read out of OPFS.
    pub(super) fn new(
        mount: super::sah::MountId,
        loaded: Vec<(PathBuf, Vec<u8>)>,
        max_resident: usize,
    ) -> Self {
        let mut state = State {
            files: HashMap::with_capacity(loaded.len()),
            dirs: BTreeSet::new(),
            dirty: HashSet::new(),
            deleted: HashSet::new(),
            resident: 0,
            next_version: 1,
        };
        for (path, data) in loaded {
            state.resident += data.len();
            super::register_ancestors(&mut state.dirs, &path);
            state.files.insert(path, Entry { data, version: 0 });
        }
        Self {
            state: Mutex::new(state),
            max_resident,
            mount,
        }
    }

    /// Read every mirror file out of an OPFS directory.
    pub(super) async fn load(directory: &JsValue) -> Result<Vec<(PathBuf, Vec<u8>)>, JsValue> {
        let entries = js::list_files(directory).await?;
        let mut loaded = Vec::new();
        for (name, handle) in entries {
            let Some(path) = decode_name(&name) else {
                continue;
            };
            loaded.push((path, js::read_whole_file(&handle).await?));
        }
        Ok(loaded)
    }

    pub(super) fn resident_bytes(&self) -> usize {
        self.state.lock().resident
    }

    pub(super) fn pending_bytes(&self) -> usize {
        let state = self.state.lock();
        state
            .dirty
            .iter()
            .filter_map(|path| state.files.get(path))
            .map(|entry| entry.data.len())
            .sum()
    }

    /// Snapshot the work `persist` has to do, releasing the lock before
    /// any `await`.
    fn take_persist_batch(&self) -> (Vec<PendingWrite>, Vec<PathBuf>) {
        let state = self.state.lock();
        let writes = state
            .dirty
            .iter()
            .filter_map(|path| {
                state
                    .files
                    .get(path)
                    .map(|entry| (path.clone(), entry.version, entry.data.clone()))
            })
            .collect();
        let deletes = state.deleted.iter().cloned().collect();
        (writes, deletes)
    }

    /// Clear the tracking entries that `persist` actually wrote out. A
    /// file mutated while the write was in flight keeps its dirty mark.
    fn settle(&self, written: &[(PathBuf, u64)], deleted: &[PathBuf]) {
        let mut state = self.state.lock();
        for (path, version) in written {
            if state.files.get(path).map(|e| e.version) == Some(*version) {
                state.dirty.remove(path);
            }
        }
        for path in deleted {
            state.deleted.remove(path);
        }
    }

    /// Write every dirty file back to OPFS and drop the deleted ones.
    pub(super) async fn persist(&self) -> Result<(), JsValue> {
        let (writes, deletes) = self.take_persist_batch();
        if writes.is_empty() && deletes.is_empty() {
            return Ok(());
        }
        let directory = super::sah::mount_directory(self.mount)
            .map_err(|e| JsValue::from_str(&e.to_string()))?;

        let mut written = Vec::with_capacity(writes.len());
        for (path, version, data) in &writes {
            js::write_whole_file(&directory, &encode_name(path), data).await?;
            written.push((path.clone(), *version));
        }
        for path in &deletes {
            // A file deleted before it was ever persisted has no OPFS
            // entry; that is not an error.
            let _ = js::remove_entry(&directory, &encode_name(path)).await;
        }

        self.settle(&written, &deletes);
        Ok(())
    }

    fn quota_error(&self, want: usize) -> io::Error {
        io::Error::other(format!(
            "OPFS mirror mode holds the whole database in memory and this write \
             would reach {want} bytes, over the {} byte limit; raise \
             OpfsOptions::max_resident_bytes or open the database in a worker, \
             where OpfsMode::Sah streams to storage instead",
            self.max_resident
        ))
    }

    pub(super) fn write_at(&self, path: &Path, at: u64, buf: &[u8]) -> io::Result<()> {
        let mut state = self.state.lock();
        let at = at as usize;
        let entry = state.files.get(path).ok_or_else(|| {
            io::Error::new(io::ErrorKind::NotFound, "file was removed while open")
        })?;
        let old_len = entry.data.len();
        let new_len = old_len.max(at.saturating_add(buf.len()));
        let projected = state.resident.saturating_sub(old_len) + new_len;
        if projected > self.max_resident {
            return Err(self.quota_error(projected));
        }

        let version = state.next_version;
        state.next_version += 1;
        let entry = state.files.get_mut(path).ok_or_else(|| {
            io::Error::new(io::ErrorKind::NotFound, "file was removed while open")
        })?;
        if entry.data.len() < new_len {
            entry.data.resize(new_len, 0);
        }
        entry.data[at..at + buf.len()].copy_from_slice(buf);
        entry.version = version;
        state.resident = projected;
        state.dirty.insert(path.to_path_buf());
        Ok(())
    }

    pub(super) fn read_at(&self, path: &Path, at: u64, buf: &mut [u8]) -> io::Result<usize> {
        let state = self.state.lock();
        let entry = state.files.get(path).ok_or_else(|| {
            io::Error::new(io::ErrorKind::NotFound, "file was removed while open")
        })?;
        let at = at as usize;
        if at >= entry.data.len() {
            return Ok(0);
        }
        let n = buf.len().min(entry.data.len() - at);
        buf[..n].copy_from_slice(&entry.data[at..at + n]);
        Ok(n)
    }

    pub(super) fn file_len(&self, path: &Path) -> io::Result<u64> {
        let state = self.state.lock();
        state
            .files
            .get(path)
            .map(|entry| entry.data.len() as u64)
            .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "file was removed while open"))
    }

    pub(super) fn set_len(&self, path: &Path, len: u64) -> io::Result<()> {
        let mut state = self.state.lock();
        let version = state.next_version;
        state.next_version += 1;
        let entry = state.files.get_mut(path).ok_or_else(|| {
            io::Error::new(io::ErrorKind::NotFound, "file was removed while open")
        })?;
        let old_len = entry.data.len();
        let len = len as usize;
        entry.data.resize(len, 0);
        entry.version = version;
        state.resident = state.resident.saturating_sub(old_len) + len;
        state.dirty.insert(path.to_path_buf());
        Ok(())
    }

    /// Create the file when absent, optionally emptying it first.
    /// Returns the resulting length.
    pub(super) fn create(&self, path: &Path, truncate: bool) -> io::Result<u64> {
        let mut state = self.state.lock();
        let version = state.next_version;
        state.next_version += 1;
        let old_len = state.files.get(path).map(|entry| entry.data.len());
        match state.files.get_mut(path) {
            Some(entry) if truncate => {
                entry.data.clear();
                entry.version = version;
            }
            Some(_) => {}
            None => {
                state.files.insert(
                    path.to_path_buf(),
                    Entry {
                        data: Vec::new(),
                        version,
                    },
                );
            }
        }
        let len = match (old_len, truncate) {
            (Some(old), true) => {
                state.resident = state.resident.saturating_sub(old);
                0
            }
            (Some(old), false) => old,
            (None, _) => 0,
        };
        state.dirty.insert(path.to_path_buf());
        state.deleted.remove(path);
        super::register_ancestors(&mut state.dirs, path);
        Ok(len as u64)
    }

    pub(super) fn create_dir_all(&self, path: &Path) -> io::Result<()> {
        let mut state = self.state.lock();
        state.dirs.insert(path.to_path_buf());
        super::register_ancestors(&mut state.dirs, path);
        Ok(())
    }

    pub(super) fn exists(&self, path: &Path) -> bool {
        let state = self.state.lock();
        state.files.contains_key(path) || state.dirs.contains(path)
    }

    pub(super) fn metadata(&self, path: &Path) -> io::Result<(u64, bool)> {
        let state = self.state.lock();
        if let Some(entry) = state.files.get(path) {
            return Ok((entry.data.len() as u64, false));
        }
        if state.dirs.contains(path) {
            return Ok((0, true));
        }
        Err(super::not_found(path))
    }

    pub(super) fn read_dir(&self, path: &Path) -> io::Result<Vec<(PathBuf, bool)>> {
        let state = self.state.lock();
        if !state.dirs.contains(path) {
            return Err(super::not_found(path));
        }
        Ok(super::children(path, state.files.keys(), state.dirs.iter()))
    }

    pub(super) fn remove_file(&self, path: &Path) -> io::Result<()> {
        let mut state = self.state.lock();
        match state.files.remove(path) {
            Some(entry) => {
                state.resident = state.resident.saturating_sub(entry.data.len());
                state.dirty.remove(path);
                state.deleted.insert(path.to_path_buf());
                Ok(())
            }
            None => Err(super::not_found(path)),
        }
    }

    pub(super) fn rename(&self, from: &Path, to: &Path) -> io::Result<()> {
        let mut state = self.state.lock();
        let entry = state
            .files
            .remove(from)
            .ok_or_else(|| super::not_found(from))?;
        if let Some(replaced) = state.files.insert(to.to_path_buf(), entry) {
            state.resident = state.resident.saturating_sub(replaced.data.len());
        }
        state.dirty.remove(from);
        state.deleted.insert(from.to_path_buf());
        state.dirty.insert(to.to_path_buf());
        state.deleted.remove(to);
        super::register_ancestors(&mut state.dirs, to);
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use wasm_bindgen_test::wasm_bindgen_test;

    #[wasm_bindgen_test]
    fn names_round_trip_through_the_escape() {
        for original in ["db/MANIFEST", "db/sst/000001.sst", "a%b/c\\d", "plain"] {
            let encoded = encode_name(Path::new(original));
            assert!(encoded.starts_with(FILE_PREFIX));
            assert!(!encoded[FILE_PREFIX.len()..].contains('/'));
            assert_eq!(decode_name(&encoded), Some(PathBuf::from(original)));
        }
    }

    #[wasm_bindgen_test]
    fn a_foreign_entry_is_not_decoded() {
        assert_eq!(decode_name(".regolith-sah-0000"), None);
        assert_eq!(decode_name("something-else"), None);
    }
}