uqa-storage 0.5.3

Document store, inverted index, IVF/HNSW vectors, B-tree, R*Tree, catalog
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
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! Immutable sorted runs of private record changes on encrypted temporary files.

mod cache;
mod cursor;
mod entry;
mod filter;
#[cfg(test)]
pub(in crate::mvcc) mod read_counts;
mod reader;
#[cfg(test)]
mod tests;
#[cfg(test)]
pub(in crate::mvcc) mod write_counts;
mod writer;

pub(in crate::mvcc) use cache::RunCacheReader;
pub(in crate::mvcc) use cursor::RunCursor;
pub(in crate::mvcc) use entry::RunEntry;
pub(in crate::mvcc) use writer::SpilledRunWriter;

use std::io::{Read, Seek, SeekFrom};
use std::ops::Bound;
use std::sync::Arc;

use uqa_core::memory::{BudgetedVec, MemoryBudget};

use crate::read_control::StorageReadControl;
use crate::temporary_file::TemporaryFile;

use crate::mvcc::key::RecordKey;
use crate::mvcc::{VersionError, VersionResult};
use cache::RunCache;
use entry::ValueLocation;
use filter::KeyFilter;

/// The aligned span of the value file a read of a small value decrypts and keeps.
const VALUE_CHUNK: u64 = 16 * 1024;

/// One block of a run's entry file: its complete key interval and byte range.
struct RunBlock {
    first: RecordKey,
    last: RecordKey,
    offset: u64,
    end: u64,
}

/// An immutable, key-ordered run of private changes in two encrypted temporary files: the entries, whose blocks a bounded in-memory index locates, and their values, which a read loads only when it needs them. A key filter answers most lookups of keys the run lacks without reading it. The files are removed with the last handle to the run.
pub(in crate::mvcc) struct SpilledRun {
    entries: TemporaryFile,
    values: TemporaryFile,
    blocks: BudgetedVec<RunBlock>,
    filter: Option<KeyFilter>,
    prefix_filter: Option<KeyFilter>,
    last: RecordKey,
    len: u64,
    key_bytes: u64,
    entry_bytes: u64,
    bytes: u64,
    /// One bit for each write kind code the run holds.
    kinds: u16,
    cache: Arc<RunCache>,
}

impl SpilledRun {
    #[cfg(test)]
    pub(in crate::mvcc) fn read_io_counts(&self) -> (usize, usize) {
        (
            self.entries.block_io_counts().0,
            self.values.block_io_counts().0,
        )
    }

    #[cfg(test)]
    pub(in crate::mvcc) fn file_blocks(&self) -> usize {
        (self.entry_bytes.div_ceil(VALUE_CHUNK)
            + (self.bytes - self.entry_bytes).div_ceil(VALUE_CHUNK)) as usize
    }

    pub(in crate::mvcc) fn shares_allowance(&self, memory: &MemoryBudget) -> bool {
        self.cache.memory().shares_allowance(memory)
    }

    /// Share decoded blocks while a lookup operation is active, without tying their lifetime to this run.
    pub(in crate::mvcc) fn cache_reader(&self) -> RunCacheReader {
        self.cache.reader()
    }

    /// Whether the run holds a write of `kind`.
    pub(in crate::mvcc) fn has_kind(&self, kind: crate::mvcc::commit::RecordWriteKind) -> bool {
        self.kinds & (1 << kind.code()) != 0
    }

    /// Whether some write of the run is not canonical.
    pub(in crate::mvcc) fn typed(&self) -> bool {
        self.kinds & !(1 << crate::mvcc::commit::RecordWriteKind::Canonical.code()) != 0
    }

    /// The number of changes in the run.
    pub(in crate::mvcc) fn len(&self) -> u64 {
        self.len
    }

    /// Literal key bytes, excluding entry metadata and values, for sizing derived filters.
    pub(in crate::mvcc) fn key_bytes(&self) -> u64 {
        self.key_bytes
    }

    /// The bytes of the run's files, which decide which runs merge.
    pub(super) fn bytes(&self) -> u64 {
        self.bytes
    }

    /// The change of `key`, if the run has one.
    pub(in crate::mvcc) fn get(
        &self,
        key: &[u8],
        control: &StorageReadControl,
    ) -> VersionResult<Option<RunEntry>> {
        control.cancellation().check()?;
        if key > self.last.bytes()
            || self
                .filter
                .as_ref()
                .is_some_and(|filter| !filter.may_contain(key))
        {
            return Ok(None);
        }
        let Some(index) = self.block_at_or_before(key) else {
            return Ok(None);
        };
        if key > self.blocks[index].last.bytes() {
            return Ok(None);
        }
        let mut block = reader::EntryReader::new(self, index, control)?;
        block.select(control, |candidate| match candidate.cmp(key) {
            std::cmp::Ordering::Less => reader::Selection::Skip,
            std::cmp::Ordering::Equal => reader::Selection::Take,
            std::cmp::Ordering::Greater => reader::Selection::End,
        })
    }

    /// The change with the greatest key before `end`, if the run has one.
    pub(super) fn last_before(
        &self,
        end: Bound<&[u8]>,
        control: &StorageReadControl,
    ) -> VersionResult<Option<RunEntry>> {
        control.cancellation().check()?;
        let before = |key: &[u8]| match end {
            Bound::Included(end) => key <= end,
            Bound::Excluded(end) => key < end,
            Bound::Unbounded => true,
        };
        let mut index = self
            .blocks
            .partition_point(|block| before(block.first.bytes()));
        while index > 0 {
            index -= 1;
            let mut block = reader::EntryReader::new(self, index, control)?;
            let mut found = None;
            while let Some(entry) = block.select(control, |key| {
                if before(key) {
                    reader::Selection::Take
                } else {
                    reader::Selection::End
                }
            })? {
                found = Some(entry);
            }
            if found.is_some() {
                return Ok(found);
            }
        }
        Ok(None)
    }

    /// Visit the run's changes in key order from `start`.
    pub(in crate::mvcc) fn cursor(self: &Arc<Self>, start: Bound<&[u8]>) -> RunCursor {
        RunCursor::new(Arc::clone(self), start)
    }

    /// Whether the run can contain this literal prefix. Interval bounds and an optional false-positive-only membership filter reject absent prefixes without an entry reader or decoded block.
    pub(super) fn intersects_prefix(&self, prefix: &[u8]) -> bool {
        let index = self
            .blocks
            .partition_point(|block| block.last.bytes() < prefix);
        self.blocks.get(index).is_some_and(|block| {
            (block.first.bytes() <= prefix || block.first.bytes().starts_with(prefix))
                && (prefix.is_empty()
                    || self
                        .prefix_filter
                        .as_ref()
                        .is_none_or(|filter| filter.may_contain(prefix)))
        })
    }

    /// Pass the value at `location` to `sink` in chunks, without holding it whole. A value within one aligned chunk of the value file is passed from that chunk, which the run keeps for the values that follow it.
    pub(in crate::mvcc) fn copy_value(
        &self,
        location: ValueLocation,
        sink: &mut dyn FnMut(&[u8]) -> VersionResult<()>,
        control: &StorageReadControl,
    ) -> VersionResult<()> {
        if let Some((chunk, begin)) = self.value_chunk(location, control)? {
            return sink(&chunk[begin..begin + location.len as usize]);
        }
        let mut reader = self.values.reopen().map_err(spill_error)?;
        reader
            .seek(SeekFrom::Start(location.offset))
            .map_err(spill_error)?;
        let mut chunk = [0_u8; VALUE_CHUNK as usize];
        let mut remaining = location.len;
        while remaining > 0 {
            let count = remaining.min(chunk.len() as u64) as usize;
            let count = read_chunk(&mut reader, &mut chunk[..count], control)?;
            sink(&chunk[..count])?;
            remaining -= count as u64;
        }
        Ok(())
    }

    /// The value at `location`, charged to `control`. A value within one aligned chunk of the value file is copied from that chunk, which the run keeps for the reads of the values that follow it.
    pub(in crate::mvcc) fn load_value(
        &self,
        location: ValueLocation,
        control: &StorageReadControl,
    ) -> VersionResult<Arc<BudgetedVec<u8>>> {
        self.load_value_cached(location, None, control)
    }

    fn load_value_cached(
        &self,
        location: ValueLocation,
        cache: Option<&mut cache::ValueCache>,
        control: &StorageReadControl,
    ) -> VersionResult<Arc<BudgetedVec<u8>>> {
        #[cfg(test)]
        read_counts::value();
        let mut value = BudgetedVec::new(control.memory());
        if let Some((chunk, begin)) = self.value_chunk_cached(location, cache, control)? {
            value.extend_from_slice(&chunk[begin..begin + location.len as usize])?;
            return Ok(Arc::new(value));
        }
        read_range(
            &self.values,
            location.offset,
            location.len,
            control.memory(),
            control,
        )
        .map(Arc::new)
    }

    /// The aligned chunk of the value file that holds the whole value at `location`, and the value's offset in it; `None` for a value that crosses a chunk boundary, or when the run's allowance cannot hold a chunk it does not already keep.
    fn value_chunk(
        &self,
        location: ValueLocation,
        control: &StorageReadControl,
    ) -> VersionResult<Option<(Arc<BudgetedVec<u8>>, usize)>> {
        self.value_chunk_cached(location, None, control)
    }

    fn value_chunk_cached(
        &self,
        location: ValueLocation,
        mut cache: Option<&mut cache::ValueCache>,
        control: &StorageReadControl,
    ) -> VersionResult<Option<(Arc<BudgetedVec<u8>>, usize)>> {
        control.check()?;
        let start = location.offset - location.offset % VALUE_CHUNK;
        let cached = match &mut cache {
            Some(cache) => cache.get(start),
            None => self.cache.chunk(start),
        };
        if location.offset + location.len > start + VALUE_CHUNK {
            return Ok(None);
        }
        let begin = (location.offset - start) as usize;
        if let Some(chunk) = cached {
            return Ok(Some((chunk, begin)));
        }
        let end = (start + VALUE_CHUNK).min(self.bytes - self.entry_bytes);
        // Cache admission must preserve workspace for the value copy and the other active readers, even when the run and consumer share one allowance.
        let available = self
            .cache
            .memory()
            .available()
            .min(control.memory().available());
        if end - start > (available / 2) as u64 {
            return Ok(None);
        }
        match read_range(
            &self.values,
            start,
            end - start,
            self.cache.memory(),
            control,
        ) {
            Ok(bytes) => {
                let bytes = Arc::new(bytes);
                match cache {
                    Some(cache) => cache.keep(start, &bytes),
                    None => self.cache.keep_chunk(start, &bytes),
                }
                Ok(Some((bytes, begin)))
            }
            Err(VersionError::Memory(_)) => Ok(None),
            Err(error) => Err(error),
        }
    }

    /// The index of the last block whose first key is at most `key`.
    fn block_at_or_before(&self, key: &[u8]) -> Option<usize> {
        self.blocks
            .partition_point(|block| block.first.bytes() <= key)
            .checked_sub(1)
    }

    /// The first block that may hold a key in `start`'s range, or the block count when the range is beyond this run.
    fn first_block_from(&self, start: Bound<&[u8]>) -> usize {
        match start {
            Bound::Unbounded => 0,
            Bound::Included(key) => self
                .blocks
                .partition_point(|block| block.last.bytes() < key),
            Bound::Excluded(key) => self
                .blocks
                .partition_point(|block| block.last.bytes() <= key),
        }
    }

    /// The bytes of entry block `index`, which the run keeps for the reads that follow; when its allowance cannot keep them, they are charged to `control` instead.
    fn read_block(
        &self,
        index: usize,
        control: &StorageReadControl,
    ) -> VersionResult<Arc<BudgetedVec<u8>>> {
        if let Some(block) = self.cache.block(index) {
            return Ok(block);
        }
        #[cfg(test)]
        read_counts::block();
        let block = &self.blocks[index];
        let len = block.end - block.offset;
        match read_range(
            &self.entries,
            block.offset,
            len,
            self.cache.memory(),
            control,
        ) {
            Ok(bytes) => {
                let bytes = Arc::new(bytes);
                self.cache.keep_block(index, &bytes);
                Ok(bytes)
            }
            Err(VersionError::Memory(_)) => {
                read_range(&self.entries, block.offset, len, control.memory(), control)
                    .map(Arc::new)
            }
            Err(error) => Err(error),
        }
    }
}

/// The `len` bytes at `offset` of `file`, charged to `memory`.
fn read_range(
    file: &TemporaryFile,
    offset: u64,
    len: u64,
    memory: &MemoryBudget,
    control: &StorageReadControl,
) -> VersionResult<BudgetedVec<u8>> {
    control.cancellation().check()?;
    let len = usize::try_from(len)
        .map_err(|_| VersionError::InvalidEncoding("spilled range exceeds the address space"))?;
    let mut bytes = BudgetedVec::new(memory);
    bytes.reserve(len)?;
    let mut reader = file.reopen().map_err(spill_error)?;
    reader.seek(SeekFrom::Start(offset)).map_err(spill_error)?;
    let mut chunk = [0_u8; 16 * 1024];
    while bytes.len() < len {
        let count = (len - bytes.len()).min(chunk.len());
        let count = read_chunk(&mut reader, &mut chunk[..count], control)?;
        bytes.extend_from_slice(&chunk[..count])?;
    }
    Ok(bytes)
}

/// Consume a physical block's short read before requesting another block. Filling an unaligned logical chunk with `read_exact` would decrypt its trailing block again on the next iteration.
fn read_chunk(
    reader: &mut TemporaryFile,
    output: &mut [u8],
    control: &StorageReadControl,
) -> VersionResult<usize> {
    loop {
        control.cancellation().check()?;
        match reader.read(output) {
            Ok(0) => {
                return Err(spill_error(std::io::Error::new(
                    std::io::ErrorKind::UnexpectedEof,
                    "failed to fill whole buffer",
                )))
            }
            Ok(count) => return Ok(count),
            Err(error) if error.kind() == std::io::ErrorKind::Interrupted => (),
            Err(error) => return Err(spill_error(error)),
        }
    }
}

/// A failed read or write of a spill file.
pub(super) fn spill_error(error: std::io::Error) -> VersionError {
    crate::StorageBackendError::backend("transaction spill file", error).into()
}