uqa-storage-sqlite 0.5.2

SQLite catalog, indexes, compressed storage, graph and key/value providers
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
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! Projected changes coalesce into complete native rows before the provider stages any result. After a whole-index replacement, rows arrive one at a time in ascending key order within each family, so each row is staged once the next row of its family arrives instead of every row being held until the batch ends.

mod encoding;

use super::{
    read::NativeRead,
    records::{self, invalid},
};
use crate::mvcc::native::{NativeRecordFamily as Family, NativeRecordOwner};
use std::collections::BTreeMap;
use uqa_core::memory::{BudgetedVec, MemoryReservation};
use uqa_storage::key_value::occurrence_format::{
    OccurrenceAddress as Address, OccurrenceProjection as Projection,
};
use uqa_storage::{KeyValueBatch, StorageBackendResult};

struct Draft {
    address: BudgetedVec<u8>,
    values: [Option<BudgetedVec<u8>>; 2],
    _key: MemoryReservation,
    _entry: MemoryReservation,
    merge: bool,
}

pub(super) struct ProjectionBatch<'a> {
    read: &'a NativeRead,
    native: &'a mut dyn KeyValueBatch,
    owner: Option<NativeRecordOwner>,
    rows: BTreeMap<Vec<u8>, Draft>,
    cleared: bool,
    /// After a whole-index replacement, the last row of each family.
    replacing: Vec<ReplacedRow>,
    /// Whether a whole-index replacement staged a row.
    replaced: bool,
}

/// The last row of a family a whole-index replacement changed, with the projections it still collects, or none once it is staged.
struct ReplacedRow {
    family: Family,
    key: BudgetedVec<u8>,
    draft: Option<Draft>,
}

/// Stage the native change `draft` makes to the row at `key`.
fn write_row(
    native: &mut dyn KeyValueBatch,
    read: &NativeRead,
    owner: NativeRecordOwner,
    key: &[u8],
    draft: &Draft,
) -> StorageBackendResult<()> {
    if draft.values.iter().all(Option::is_none) {
        if draft.merge {
            native.replace_occurrence_record(key, None)
        } else {
            native.delete(key)
        }
    } else {
        encoding::write(native, read, owner, draft)
    }
}

fn owned_value(
    value: Option<&[u8]>,
    control: &uqa_storage::read_control::StorageReadControl,
) -> StorageBackendResult<Option<BudgetedVec<u8>>> {
    value
        .map(|bytes| {
            let mut value = BudgetedVec::new(control.memory());
            value.extend_from_slice(bytes)?;
            Ok::<_, uqa_storage::StorageBackendError>(value)
        })
        .transpose()
}

impl<'a> ProjectionBatch<'a> {
    pub(super) fn new(read: &'a NativeRead, native: &'a mut dyn KeyValueBatch) -> Self {
        Self {
            read,
            native,
            owner: read.owner,
            rows: BTreeMap::new(),
            cleared: false,
            replacing: Vec::new(),
            replaced: false,
        }
    }

    fn ensure_owner(&mut self) -> StorageBackendResult<NativeRecordOwner> {
        if let Some(owner) = self.owner {
            return Ok(owner);
        }
        let owner = self
            .read
            .snapshot
            .ensure_table_owner(&self.read.table, self.native)?;
        self.owner = Some(owner);
        Ok(owner)
    }

    fn change(
        &mut self,
        key: &[u8],
        value: Option<&[u8]>,
        merge: bool,
    ) -> StorageBackendResult<()> {
        let owner = self.ensure_owner()?;
        let control = &self.read.snapshot.control;
        control.check()?;
        let address = self.read.address(key)?;
        if !address.complete() {
            return Err(invalid("occurrence mutation requires a complete address"));
        }
        let projection = address.projection.expect("complete address");
        let projections = records::projections(
            records::family(projection).expect("complete address has a family"),
        );
        let slot = projections
            .iter()
            .position(|&candidate| candidate == projection)
            .expect("current occurrence projection");
        let key = records::key(address, owner, control)?;
        if self.cleared {
            return self.replace_row(key, address, slot, value, merge);
        }
        let (key, key_memory) = key.into_parts();
        let entry = self.rows.entry(key);
        let draft = match entry {
            std::collections::btree_map::Entry::Occupied(entry) => entry.into_mut(),
            std::collections::btree_map::Entry::Vacant(entry) => {
                let memory = control
                    .memory()
                    .reserve(std::mem::size_of::<(Vec<u8>, Draft)>())?;
                let mut values = [None, None];
                if !self.cleared && projections.len() > 1 {
                    self.read.row(address, control, |row| {
                        if let Some(row) = row {
                            for (slot, &projection) in projections.iter().enumerate() {
                                values[slot] =
                                    Some(records::project(row, projection, control, |bytes| {
                                        let mut value = BudgetedVec::new(control.memory());
                                        value.extend_from_slice(bytes)?;
                                        Ok(value)
                                    })?);
                            }
                        }
                        Ok(())
                    })?;
                }
                entry.insert(Draft {
                    address: address.encode(control)?,
                    values,
                    _key: key_memory,
                    _entry: memory,
                    merge,
                })
            }
        };
        draft.merge &= merge;
        draft.values[slot] = owned_value(value, control)?;
        Ok(())
    }

    /// Change a row after a whole-index replacement, which writes each record once and the rows of a family in ascending key order. A row is staged as soon as its last projection arrives, from that value instead of a copy, and a row of a later key stages the row its family left incomplete; an earlier key, or a record of a staged row, could change a row already staged, so it is rejected.
    fn replace_row(
        &mut self,
        key: BudgetedVec<u8>,
        address: Address<'_>,
        slot: usize,
        value: Option<&[u8]>,
        merge: bool,
    ) -> StorageBackendResult<()> {
        let owner = self.ensure_owner()?;
        let family = records::family(address.projection.expect("complete address"))
            .expect("complete address has a family");
        let width = records::projections(family).len();
        let control = &self.read.snapshot.control;
        let position = self.replacing.iter().position(|row| row.family == family);
        if let Some(index) = position {
            let row = &mut self.replacing[index];
            match (*key).cmp(&*row.key) {
                std::cmp::Ordering::Equal => {
                    let Some(draft) = row.draft.as_mut() else {
                        return Err(invalid(
                            "a whole-index replacement writes each occurrence record once",
                        ));
                    };
                    draft.merge &= merge;
                    let mut values = [draft.values[0].as_deref(), draft.values[1].as_deref()];
                    values[slot] = value;
                    if values[..width].iter().all(Option::is_some) {
                        encoding::write_values(
                            self.native,
                            self.read,
                            owner,
                            &draft.address,
                            values,
                            draft.merge,
                        )?;
                        row.draft = None;
                        self.replaced = true;
                    } else {
                        draft.values[slot] = owned_value(value, control)?;
                    }
                    return Ok(());
                }
                std::cmp::Ordering::Less => {
                    return Err(invalid(
                        "rows a whole-index replacement writes must arrive in key order",
                    ));
                }
                std::cmp::Ordering::Greater => {
                    let finished = self.replacing.swap_remove(index);
                    if let Some(draft) = finished.draft {
                        write_row(self.native, self.read, owner, &finished.key, &draft)?;
                        self.replaced = true;
                    }
                }
            }
        }
        let encoded = address.encode(control)?;
        let draft = if width == 1 && value.is_some() {
            encoding::write_values(
                self.native,
                self.read,
                owner,
                &encoded,
                [value, None],
                merge,
            )?;
            self.replaced = true;
            None
        } else {
            let mut values = [None, None];
            values[slot] = owned_value(value, control)?;
            Some(Draft {
                address: encoded,
                values,
                _key: control.memory().empty_reservation(),
                _entry: control
                    .memory()
                    .reserve(std::mem::size_of::<ReplacedRow>())?,
                merge,
            })
        };
        self.replacing.push(ReplacedRow { family, key, draft });
        Ok(())
    }

    pub(super) fn flush(self) -> StorageBackendResult<()> {
        let Some(owner) = self.owner else {
            return Ok(());
        };
        let replacing = self
            .replacing
            .iter()
            .filter_map(|row| row.draft.as_ref().map(|draft| (&*row.key, draft)));
        for (key, draft) in self
            .rows
            .iter()
            .map(|(key, draft)| (key.as_slice(), draft))
            .chain(replacing)
        {
            self.read.snapshot.control.check()?;
            write_row(self.native, self.read, owner, key, draft)?;
        }
        Ok(())
    }
}

impl KeyValueBatch for ProjectionBatch<'_> {
    fn serializable_participant(&self) -> Option<uqa_storage::mvcc::SerializableTransactionId> {
        self.native.serializable_participant()
    }
    fn observe_serializable_write(
        &mut self,
        predicate: uqa_storage::mvcc::SerializablePredicate<'_>,
    ) -> StorageBackendResult<()> {
        self.native.observe_serializable_write(predicate)
    }
    fn observe_identifier(&mut self, namespace: &[u8], value: u64) -> StorageBackendResult<()> {
        self.native.observe_identifier(namespace, value)
    }
    fn inherit_identifiers(&mut self, from: &[u8], to: &[u8]) -> StorageBackendResult<()> {
        self.native.inherit_identifiers(from, to)
    }
    fn put(&mut self, key: &[u8], value: &[u8]) -> StorageBackendResult<()> {
        self.change(key, Some(value), false)
    }
    fn delete(&mut self, key: &[u8]) -> StorageBackendResult<()> {
        self.change(key, None, false)
    }
    fn replace_occurrence_record(
        &mut self,
        key: &[u8],
        value: Option<&[u8]>,
    ) -> StorageBackendResult<()> {
        self.change(key, value, true)
    }
    fn invalidate_occurrence_prefix(&mut self, prefix: &[u8]) -> StorageBackendResult<()> {
        let address = self.read.address(prefix)?;
        if !matches!(
            address.projection,
            Some(Projection::Skip | Projection::BlockMax)
        ) {
            return Err(invalid("invalid native occurrence cache prefix"));
        }
        if let Some(owner) = self.owner {
            self.native.invalidate_occurrence_prefix(&records::prefix(
                address,
                owner,
                &self.read.snapshot.control,
            )?)?;
        }
        Ok(())
    }
    fn occurrence_document(&mut self, table: &str, document: u64) -> StorageBackendResult<()> {
        if table != self.read.table {
            return Err(invalid("native occurrence document changed table"));
        }
        let owner = self.ensure_owner()?;
        let document = crate::inverted_index::encode_index_u64("document", document)?;
        self.read
            .snapshot
            .occurrence_guard(self.native, table, owner, document)?;
        Ok(())
    }
    fn reset_occurrences(&mut self, table: &str) -> StorageBackendResult<()> {
        if table != self.read.table {
            return Err(invalid("native occurrence reset changed table"));
        }
        let owner = self.ensure_owner()?;
        self.read
            .snapshot
            .reset_occurrence_rows(self.native, owner)?;
        Ok(())
    }
    fn delete_prefix(&mut self, prefix: &[u8]) -> StorageBackendResult<()> {
        let address = self.read.address(prefix)?;
        let control = &self.read.snapshot.control;
        control.check()?;
        match address.projection {
            None => {
                // Whole-index replacement retires rows without decoding discarded payloads. Later puts must not inherit either column from an original row.
                if let Some(owner) = self.owner {
                    for projection in [
                        Projection::Score,
                        Projection::Skip,
                        Projection::BlockMax,
                        Projection::Document,
                        Projection::Length,
                        Projection::Field,
                        Projection::Format,
                    ] {
                        self.native.delete_prefix(&records::prefix(
                            Address {
                                projection: Some(projection),
                                ..address
                            },
                            owner,
                            control,
                        )?)?;
                    }
                }
                self.rows.clear();
                self.replacing.clear();
                self.cleared = true;
                return Ok(());
            }
            Some(projection) if projection.is_legacy() => {
                if let Some(owner) = self.owner.filter(|_| records::family(projection).is_some()) {
                    self.native
                        .delete_prefix(&records::prefix(address, owner, control)?)?;
                }
                return Ok(());
            }
            _ => {}
        }
        if self.replaced {
            return Err(invalid(
                "a whole-index replacement cannot delete part of the rows it staged",
            ));
        }
        if !self.cleared {
            for key in self.read.keys(prefix, None, usize::MAX, control)?.iter() {
                self.delete(key)?;
            }
        }
        // Include rows introduced earlier in this same callback, preserving delete-after-put order.
        for draft in self.rows.values_mut().chain(
            self.replacing
                .iter_mut()
                .filter_map(|row| row.draft.as_mut()),
        ) {
            control.check()?;
            let address = Address::decode(&draft.address)?;
            let family = records::family(address.projection.expect("complete address"))
                .expect("current family");
            for (slot, &projection) in records::projections(family).iter().enumerate() {
                if (Address {
                    projection: Some(projection),
                    ..address
                })
                .encode(control)?
                .starts_with(prefix)
                {
                    draft.values[slot] = None;
                }
            }
        }
        Ok(())
    }
    fn commit(self: Box<Self>) -> StorageBackendResult<()> {
        Err(invalid(
            "native occurrence batches are committed by their enclosing operation",
        ))
    }
}