sva-engine 0.8.1

Renders a resolved graph into per-node buffers a query can be asked of
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
// Concern: node values on a backend beneath memory, staged then committed: version, budget, eviction | Non-concern: the bytes' medium (a Backend) | IO: (key) -> Header; samples -> staged; persist()

use std::collections::BTreeMap;
use std::sync::{Arc, Mutex, MutexGuard};

use sva_formula::{FOURIER_DUAL_RULES_VERSION, Hash};
use sva_samples::{Buffer, Extent};

use super::codec::{self, STORE_FORMAT};
use super::index::{Index, key_of, name_of};
use super::joined;
use super::stored::{Header, Samples};

pub const DEFAULT_STORE_BYTES: u64 = 2 << 30;

fn version() -> String {
    format!("sva store format {STORE_FORMAT} table {FOURIER_DUAL_RULES_VERSION}")
}

pub const INDEX_NAME: &str = "index";

const META: &str = "meta";

/// Named bytes. A `put` is whole or absent: no reader sees half of one.
pub trait Backend: Sized {
    type Lock;
    /// The one exclusive lock over these names, shared by every store over them.
    fn lock(&self) -> impl Future<Output = Result<Self::Lock, String>>;
    fn get(&self, name: &str) -> impl Future<Output = Result<Option<Vec<u8>>, String>>;
    /// At most `len` bytes from `from` on, fewer where the name ends sooner.
    fn get_range(
        &self,
        name: &str,
        from: u64,
        len: u64,
    ) -> impl Future<Output = Result<Option<Vec<u8>>, String>>;
    fn put(&self, name: &str, bytes: &[u8]) -> impl Future<Output = Result<(), String>>;
    /// False where another holder has `name` open, so it stays.
    fn delete(&self, name: &str) -> impl Future<Output = Result<bool, String>>;
    /// Each name, with its size in bytes.
    fn list(&self) -> impl Future<Output = Result<Vec<(String, u64)>, String>>;
    /// An area beside these names that no other store writes and `list` never names.
    fn staging(&self) -> impl Future<Output = Result<Self, String>>;
    /// `name` moved from here into `to` in one step, over whatever `to` held under it; as
    /// `delete`, false where either is open.
    fn rename(&self, name: &str, to: &Self) -> impl Future<Output = Result<bool, String>>;
}

#[derive(Clone, Default)]
struct Staged {
    chunks: Vec<(String, Extent)>,
    meta: bool,
}

/// Node values under their keys, beneath the memory tier alone: it stages, and only `persist`
/// commits each value whole by rename, evicting the least recently used past the budget. Every
/// change to the backend's names runs under its lock; lookups and reads never wait on it.
pub struct Store<B> {
    backend: B,
    staging: B,
    max_bytes: u64,
    index: Mutex<Index>,
    staged: Mutex<BTreeMap<Hash, Staged>>,
    written: Mutex<u64>,
}

enum Prepared {
    Ready { bytes: u64 },
    Dropped,
    Refused,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Persisted {
    pub written: usize,
    pub evicted: usize,
    /// Values larger than the whole budget.
    pub refused: usize,
}

fn locked<T>(held: &Mutex<T>) -> MutexGuard<'_, T> {
    held.lock().unwrap_or_else(|poisoned| poisoned.into_inner())
}

impl<B: Backend> Store<B> {
    /// Over another format's index, a fresh one: what that format left is neither read nor
    /// removed, nor counted against the budget.
    pub async fn open(backend: B, max_bytes: u64) -> Result<Store<B>, String> {
        let version = version();
        let held = backend.lock().await?;
        let found = backend.get(INDEX_NAME).await?;
        let index = match found.and_then(|text| Index::read(&text, &version)) {
            Some(index) => index,
            None => {
                let empty = Index::default();
                backend
                    .put(INDEX_NAME, empty.text(&version).as_bytes())
                    .await?;
                empty
            }
        };
        let staging = backend.staging().await?;
        drop(held);
        Ok(Store {
            backend,
            staging,
            max_bytes,
            index: Mutex::new(index),
            staged: Mutex::new(BTreeMap::new()),
            written: Mutex::new(0),
        })
    }

    pub fn max_bytes(&self) -> u64 {
        self.max_bytes
    }

    pub fn bytes(&self) -> u64 {
        locked(&self.index).bytes
    }

    pub fn holds(&self, key: Hash) -> bool {
        locked(&self.index).held.contains_key(&key)
    }

    pub(crate) fn used(&self, key: Hash) {
        locked(&self.index).used(key);
    }

    /// Reads headers, never a sample; one standing for another's samples reads that one's.
    pub(crate) async fn lookup(&self, key: Hash) -> Option<Header> {
        let found = self.written(key).await?;
        let &Samples::Of { key: of, by } = found.samples() else {
            return Some(found);
        };
        let samples = match self.written(of).await?.into_parts().1 {
            Samples::Entry { file, runs, .. } => Samples::Entry {
                file,
                runs,
                shift: -by,
            },
            Samples::Staged { file, chunks, .. } => Samples::Staged {
                file,
                chunks,
                shift: -by,
            },
            _ => return None,
        };
        Some(Header::new(found.into_parts().0, samples))
    }

    /// Staged first; past the index, the backend, for another store's commits.
    async fn written(&self, key: Hash) -> Option<Header> {
        if let Some(staged) = self.staged_value(key).await {
            return Some(staged);
        }
        let name = name_of(key);
        let Some(mut bytes) = self.backend.get_range(&name, 0, 8).await.ok()? else {
            locked(&self.index).forget(key);
            return None;
        };
        let rest = codec::head_len(&bytes)? - 8;
        bytes.extend(self.backend.get_range(&name, 8, rest as u64).await.ok()??);
        let (found, len) =
            codec::read_head(&bytes, key).filter(|(found, _)| found.stored().key == key)?;
        let mut index = locked(&self.index);
        match index.held.contains_key(&key) {
            true => index.used(key),
            false => index.touch(key, len),
        }
        Some(found)
    }

    async fn staged_value(&self, key: Hash) -> Option<Header> {
        let chunks = {
            let staged = locked(&self.staged);
            let held = staged.get(&key).filter(|staged| staged.meta)?;
            held.chunks.clone()
        };
        let meta = self.staging.get(&staged_name(key, META)).await.ok()??;
        let (found, _) = codec::read_head(&meta, key)?;
        Some(match found.refers() {
            true => found,
            false => {
                let samples = Samples::Staged {
                    file: key,
                    chunks,
                    shift: 0,
                };
                Header::new(found.into_parts().0, samples)
            }
        })
    }

    /// Whole chunks holding `over`; `None` where they are gone or corrupt.
    pub(crate) async fn read(&self, head: &Header, over: Extent) -> Option<Vec<Buffer>> {
        let mut out = Vec::new();
        match head.samples() {
            Samples::None | Samples::Of { .. } => {}
            Samples::Entry { file, runs, shift } => {
                for run in runs {
                    let met = run.extent().intersect(over.shifted(-shift));
                    if met.is_empty() {
                        continue;
                    }
                    let (from, to) = codec::chunks_over(run, met);
                    let (at, len) = codec::span_of(run, from, to);
                    let bytes = self.backend.get_range(&name_of(*file), at, len).await;
                    let mut samples = codec::read_chunks(&bytes.ok()??, run, from, to)?;
                    samples.start += shift;
                    out.push(samples);
                }
            }
            Samples::Staged { chunks, shift, .. } => {
                for (name, e) in chunks {
                    if !e.intersect(over.shifted(-shift)).is_empty() {
                        let bytes = self.staging.get(name).await.ok()??;
                        let mut samples = codec::read_chunk(&bytes)?;
                        samples.start += shift;
                        out.push(samples);
                    }
                }
            }
        }
        Some(out)
    }

    pub(crate) async fn stage(&self, key: Hash, samples: &Buffer) -> Result<(), String> {
        let n = {
            let mut written = locked(&self.written);
            *written += 1;
            *written
        };
        let name = staged_name(key, &n.to_string());
        self.staging.put(&name, &codec::chunk(samples)).await?;
        locked(&self.staged)
            .entry(key)
            .or_default()
            .chunks
            .push((name, samples.extent()));
        Ok(())
    }

    pub(crate) async fn stage_meta(&self, key: Hash, head: &Header) -> Result<(), String> {
        let name = staged_name(key, META);
        self.staging.put(&name, &codec::entry(head, &[])).await?;
        locked(&self.staged).entry(key).or_default().meta = true;
        Ok(())
    }

    /// Free while nothing is staged or used since and the budget holds. The index file is
    /// every store's account and names each value before it moves: one cut short leaves a
    /// gone entry named, never one unnamed. A value staged without its meta is dropped; one
    /// failed or over an open entry stays staged.
    pub(crate) async fn persist(&self) -> Result<Persisted, String> {
        let idle = {
            let index = locked(&self.index);
            index.bytes <= self.max_bytes && index.unsynced().is_empty()
        };
        if idle && locked(&self.staged).is_empty() {
            return Ok(Persisted::default());
        }
        let _held = self.backend.lock().await?;
        let (mut index, seen) = self.current().await?;
        let mut done = Persisted::default();
        let mut moving = Vec::new();
        let keys: Vec<Hash> = locked(&self.staged).keys().copied().collect();
        for key in keys {
            let Some(held) = locked(&self.staged).get(&key).cloned() else {
                continue;
            };
            match self.prepared(key, &held).await? {
                Prepared::Ready { bytes } => {
                    moving.push((key, held, bytes));
                    continue;
                }
                Prepared::Dropped => {}
                Prepared::Refused => done.refused += 1,
            }
            self.unstaged(key, &held).await?;
        }
        if !moving.is_empty() {
            let mut naming = index.clone();
            for (key, _, bytes) in &moving {
                naming.touch(*key, *bytes);
            }
            let text = naming.text(&version());
            self.backend.put(INDEX_NAME, text.as_bytes()).await?;
        }
        for (key, held, bytes) in moving {
            if !self.staging.rename(&name_of(key), &self.backend).await? {
                continue;
            }
            index.touch(key, bytes);
            done.written += 1;
            self.unstaged(key, &held).await?;
        }
        for key in index.oldest_first() {
            if index.bytes <= self.max_bytes {
                break;
            }
            if self.backend.delete(&name_of(key)).await? {
                index.forget(key);
                done.evicted += 1;
            }
        }
        self.backend
            .put(INDEX_NAME, index.text(&version()).as_bytes())
            .await?;
        index.synced();
        let mut local = locked(&self.index);
        for key in local.used_since(seen) {
            index.used(key);
        }
        *local = index;
        Ok(done)
    }

    /// The index file, or else the directory listed, with this store's uses since; its clock.
    async fn current(&self) -> Result<(Index, u64), String> {
        let found = self.backend.get(INDEX_NAME).await?;
        let mut index = match found.and_then(|text| Index::read(&text, &version())) {
            Some(index) => index,
            None => {
                let listed = self.backend.list().await?;
                let listed = listed
                    .into_iter()
                    .filter_map(|(name, bytes)| Some((key_of(&name)?, bytes)));
                Index::listed(listed.collect(), &locked(&self.index))
            }
        };
        let local = locked(&self.index);
        for key in local.unsynced() {
            index.used(key);
        }
        Ok((index, local.clock()))
    }

    async fn unstaged(&self, key: Hash, held: &Staged) -> Result<(), String> {
        for (chunk, _) in &held.chunks {
            self.staging.delete(chunk).await?;
        }
        self.staging.delete(&staged_name(key, META)).await?;
        locked(&self.staged).remove(&key);
        Ok(())
    }

    /// `key`'s staged value joined with its entry's, whole in staging under the entry's name.
    async fn prepared(&self, key: Hash, held: &Staged) -> Result<Prepared, String> {
        let whole = match held.meta {
            true => self.staged_value_of(key, held).await?,
            false => None,
        };
        let whole = whole.filter(|(head, runs)| !runs.is_empty() || head.refers());
        let Some((head, mut runs)) = whole else {
            return Ok(Prepared::Dropped);
        };
        let name = name_of(key);
        if !head.refers() {
            let held = self.backend.get(&name).await?;
            if let Some(held) = held.and_then(|bytes| codec::read_runs(&bytes, key)) {
                runs = joined_runs(runs, held);
            }
        }
        let bytes = codec::entry(&head, &runs);
        if bytes.len() as u64 > self.max_bytes {
            return Ok(Prepared::Refused);
        }
        self.staging.put(&name, &bytes).await?;
        Ok(Prepared::Ready {
            bytes: bytes.len() as u64,
        })
    }

    async fn staged_value_of(
        &self,
        key: Hash,
        held: &Staged,
    ) -> Result<Option<(Header, Vec<Buffer>)>, String> {
        let Some(meta) = self.staging.get(&staged_name(key, META)).await? else {
            return Ok(None);
        };
        let Some((head, _)) = codec::read_head(&meta, key) else {
            return Ok(None);
        };
        let mut runs = Vec::new();
        for (chunk, _) in &held.chunks {
            let bytes = self.staging.get(chunk).await?;
            match bytes.as_deref().and_then(codec::read_chunk) {
                Some(samples) => joined(&mut runs, vec![Arc::new(samples)]),
                None => return Ok(None),
            }
        }
        let runs = runs.into_iter().map(Arc::unwrap_or_clone).collect();
        Ok(Some((head, runs)))
    }
}

fn joined_runs(staged: Vec<Buffer>, held: Vec<Buffer>) -> Vec<Buffer> {
    let mut runs: Vec<Arc<Buffer>> = held.into_iter().map(Arc::new).collect();
    joined(&mut runs, staged.into_iter().map(Arc::new).collect());
    runs.into_iter().map(Arc::unwrap_or_clone).collect()
}

fn staged_name(key: Hash, part: &str) -> String {
    format!("{}.{part}", name_of(key))
}