uqa-storage-sqlite 0.4.0

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
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! `SQLite` persistence for shared logical records, with short physical transactions and durable receipts.

mod admission;
mod codec;
mod identifiers;
mod key_value;
#[cfg(any(windows, all(unix, not(target_os = "emscripten"))))]
pub(crate) mod leases;
pub mod native;
mod read;
pub(crate) mod receipts;
mod reclamation;
pub(crate) mod restore;
mod retention;
mod runs;
mod schema;
mod serializable;
#[cfg(test)]
mod tests;
mod write;

pub(crate) use schema::WritePermit;
pub use serializable::SQLiteSerializableAdmission;

use std::sync::Arc;

use rusqlite::Connection;
use uqa_storage::mvcc::{
    CommitResult, CommitStatus, CommittedRecordSnapshot, DatabaseId, PreparedRecordCommit,
    StorageTransactionId, VersionError, VersionResult, VersionedPersistence,
};
use uqa_storage::read_control::StorageReadControl;

use crate::{ManagedConnection, SQLiteError};

#[derive(Debug, thiserror::Error)]
enum Error {
    #[error(transparent)]
    SQLite(#[from] rusqlite::Error),
    #[error(transparent)]
    Version(#[from] VersionError),
}

impl Error {
    fn into_version(self) -> VersionError {
        match self {
            Self::SQLite(error) => sqlite_error(error),
            Self::Version(error) => error,
        }
    }
}

impl From<SQLiteError> for Error {
    fn from(error: SQLiteError) -> Self {
        match error {
            SQLiteError::Memory(error) => Self::Version(VersionError::Memory(error)),
            SQLiteError::Cancelled(error) => Self::Version(VersionError::Cancelled(error)),
            SQLiteError::SQLite(error) => Self::SQLite(error),
            error => Self::Version(VersionError::Storage(error.into())),
        }
    }
}

type PhysicalResult<T> = std::result::Result<T, Error>;

fn sqlite_error(error: rusqlite::Error) -> VersionError {
    VersionError::Storage(SQLiteError::SQLite(error).into())
}

/// Record persistence over a managed `SQLite` pool, including `SQLCipher` and compressed connections. Snapshots retain logical sequences; this adapter does not retain physical transactions between operations.
///
/// Logical snapshot leases protect predecessor histories across bounded reads and native processes; reclamation preserves head tombstones and all commit receipts. The Key/Value provider uses these records directly. `Self::for_native` converts a native catalog and atomically maintains its current rows with their history; `ManagedConnection::bind_native_records` binds catalog and data handles to the same logical session.
#[derive(Clone)]
pub struct SQLiteRecordStore {
    connection: ManagedConnection,
    identity: DatabaseId,
    native: Option<native::NativeRecordNamespace>,
    snapshots: Arc<uqa_storage::mvcc::SnapshotRegistry>,
}

impl SQLiteRecordStore {
    pub(crate) fn has_native_mapping(connection: &Connection) -> VersionResult<bool> {
        native::present(connection).map_err(Error::into_version)
    }

    /// Prepare a native baseline on the initial restore's physical transaction. Its owner publishes both the restored catalog and this baseline with one COMMIT.
    pub(crate) fn initialize_native_in(
        connection: &ManagedConnection,
        transaction: &Connection,
        control: &StorageReadControl,
    ) -> VersionResult<Self> {
        let native::NativeMapping {
            identity,
            namespace,
        } = native::initialize_in(transaction, control).map_err(Error::into_version)?;
        Ok(Self {
            snapshots: retention::registry(connection, identity)?,
            connection: connection.record_connection(),
            identity,
            native: Some(namespace),
        })
    }

    pub fn new(connection: &ManagedConnection) -> VersionResult<Self> {
        if connection.in_transaction() {
            return Err(VersionError::Storage(
                SQLiteError::TransactionAlreadyActive.into(),
            ));
        }
        let connection = connection.record_connection();
        let identity = connection
            .with(|connection| Ok(schema::initialize(connection)))
            .map_err(|error| VersionError::Storage(error.into()))?
            .map_err(Error::into_version)?;
        Ok(Self {
            snapshots: retention::registry(&connection, identity)?,
            connection,
            identity,
            native: None,
        })
    }

    /// Atomically initialize or upgrade a native catalog and import its baseline, or reopen its versioned materialization. Unbound native store handles are disabled after conversion; callers must bind a routed native session or prepare native records through the shared transaction contract.
    pub fn for_native(
        connection: &ManagedConnection,
        control: &StorageReadControl,
    ) -> VersionResult<Self> {
        if connection.in_transaction() {
            return Err(VersionError::Storage(
                SQLiteError::TransactionAlreadyActive.into(),
            ));
        }
        let connection = connection.record_connection();
        let native::NativeMapping {
            identity,
            namespace,
        } = connection
            .with(|connection| Ok(native::initialize(connection, control)))
            .map_err(|error| VersionError::Storage(error.into()))?
            .map_err(Error::into_version)?;
        Ok(Self {
            snapshots: retention::registry(&connection, identity)?,
            connection,
            identity,
            native: Some(namespace),
        })
    }

    pub(crate) fn for_key_value(
        connection: &ManagedConnection,
        control: &StorageReadControl,
    ) -> VersionResult<Self> {
        let connection = connection.record_connection();
        let identity = connection
            .with(|connection| Ok(key_value::initialize(connection, control)))
            .map_err(|error| VersionError::Storage(error.into()))?
            .map_err(Error::into_version)?;
        Ok(Self {
            snapshots: retention::registry(&connection, identity)?,
            connection,
            identity,
            native: None,
        })
    }

    /// Immutable namespace for database-owned native record keys. Use it with `NativeRecordOwner::Database`; `VersionedPersistence::database_id` identifies the transaction history and may differ after restoration. Key/Value and raw record stores return `None`.
    #[must_use]
    pub fn native_namespace(&self) -> Option<DatabaseId> {
        self.native.map(|namespace| namespace.0)
    }

    fn with<T>(
        &self,
        operation: impl FnOnce(&Connection) -> PhysicalResult<T>,
    ) -> VersionResult<T> {
        self.connection
            .with(|connection| Ok(operation(connection)))
            .map_err(|error| VersionError::Storage(error.into()))?
            .map_err(Error::into_version)
    }

    fn with_write<T>(
        &self,
        control: &StorageReadControl,
        operation: impl FnOnce(&Connection) -> PhysicalResult<T>,
    ) -> VersionResult<T> {
        let mut operation = Some(operation);
        loop {
            control.cancellation().check()?;
            let result = self
                .connection
                .with_record_connection(control, |connection| {
                    let _timeout = admission::BusyTimeout::new(connection)?;
                    control.cancellation().check()?;
                    Ok(operation.take().expect("record write runs once")(
                        connection,
                    ))
                });
            match result {
                Err(SQLiteError::SQLite(error))
                    if operation.is_some() && admission::is_busy(&error) =>
                {
                    // Checkout/configuration failed before the operation was invoked.
                    admission::wait(control).map_err(Error::into_version)?;
                }
                result => {
                    return result
                        .map_err(|error| Error::from(error).into_version())?
                        .map_err(Error::into_version);
                }
            }
        }
    }

    fn check_transaction(&self, transaction: StorageTransactionId) -> VersionResult<()> {
        if transaction.database() != self.identity {
            return Err(VersionError::WrongDatabase);
        }
        Ok(())
    }
}

impl VersionedPersistence for SQLiteRecordStore {
    fn serializable_coordinator(&self) -> Option<&dyn uqa_storage::mvcc::SerializableCoordinator> {
        Some(self)
    }

    fn identifier_watermark(
        &self,
        namespace: &[u8],
        control: &StorageReadControl,
    ) -> VersionResult<Option<u64>> {
        control.cancellation().check()?;
        self.with(|connection| {
            identifiers::read(connection, self.identity, self.native, namespace, control)
        })
    }

    fn allocate_identifiers(
        &self,
        namespace: &[u8],
        request: uqa_storage::mvcc::IdentifierRequest,
        control: &StorageReadControl,
    ) -> VersionResult<uqa_storage::mvcc::IdentifierAllocation> {
        control.cancellation().check()?;
        self.with_write(control, |connection| {
            identifiers::allocate(
                connection,
                self.identity,
                self.native,
                namespace,
                request,
                control,
            )
        })
    }

    fn database_id(&self) -> DatabaseId {
        self.identity
    }
    fn graph_record_layout(&self) -> Option<&dyn uqa_storage::mvcc::GraphRecordLayout> {
        match self.native.as_ref() {
            Some(namespace) => Some(namespace),
            None => Some(&uqa_storage::key_value::KeyValueGraphRecords),
        }
    }
    fn occurrence_record_layout(&self) -> &dyn uqa_storage::mvcc::OccurrenceRecordLayout {
        if self.native.is_some() {
            &crate::inverted_index::NativeOccurrenceRecords
        } else {
            &uqa_storage::key_value::KeyValueOccurrenceRecords
        }
    }
    fn notification_record_layout(&self) -> &dyn uqa_storage::mvcc::NotificationRecordLayout {
        match self.native.as_ref() {
            Some(namespace) => namespace,
            None => &uqa_storage::key_value::KeyValueNotificationRecords,
        }
    }

    fn maintenance_record_layout(&self) -> &dyn uqa_storage::mvcc::MaintenanceRecordLayout {
        if self.native.is_some() {
            &native::NativeMaintenanceRecords
        } else {
            &uqa_storage::key_value::KeyValueMaintenanceRecords
        }
    }
    fn ivf_record_layout(&self) -> &dyn uqa_storage::mvcc::IVFRecordLayout {
        if self.native.is_some() {
            &crate::vector_index::NativeIVFRecords
        } else {
            &uqa_storage::key_value::KeyValueIVFRecords
        }
    }
    fn hnsw_record_layout(&self) -> Option<&dyn uqa_storage::mvcc::HNSWRecordLayout> {
        if self.native.is_some() {
            Some(&crate::vector_index::NativeHNSWRecords)
        } else {
            Some(&uqa_storage::key_value::KeyValueHNSWRecords)
        }
    }
    fn allocate_transaction(
        &self,
        control: &StorageReadControl,
    ) -> VersionResult<StorageTransactionId> {
        control.cancellation().check()?;
        self.with_write(control, |connection| {
            write::allocate(connection, self.identity, self.native, control)
        })
    }

    fn allocate_managed_transaction(
        &self,
        control: &StorageReadControl,
    ) -> VersionResult<uqa_storage::mvcc::RetainedTransactionAllocation> {
        self.allocate_receipt_owner(control)
    }
    fn snapshot(
        &self,
        control: &StorageReadControl,
    ) -> VersionResult<Arc<dyn CommittedRecordSnapshot>> {
        control.cancellation().check()?;
        let lease = self.snapshots.capture(control, || {
            self.with(|connection| {
                let read = connection.unchecked_transaction()?;
                native::check_mapping(&read, self.native)?;
                let sequence = codec::header(&read, self.identity)?.sequence;
                read.commit()?;
                Ok(sequence)
            })
        })?;
        uqa_storage::mvcc::retain_record_snapshot(
            read::Snapshot {
                store: self.clone(),
                sequence: lease.sequence(),
                _lease: lease,
            },
            control,
        )
    }
    fn reclaim_versions(&self, control: &StorageReadControl) -> VersionResult<u64> {
        self.snapshots.reclaim(control, |oldest| {
            self.with_write(control, |connection| {
                reclamation::reclaim(connection, self.identity, self.native, oldest, control)
            })
        })
    }

    fn commit(
        &self,
        transaction: StorageTransactionId,
        prepared: &PreparedRecordCommit,
        control: &StorageReadControl,
    ) -> CommitResult {
        self.check_transaction(transaction)?;
        control.cancellation().check().map_err(VersionError::from)?;
        let _bindings = write::reserve_bindings(prepared, control)?;
        self.with_write(control, |connection| {
            Ok(write::commit(
                connection,
                transaction,
                prepared,
                self.native,
                control,
            ))
        })?
    }
    fn commit_status(
        &self,
        transaction: StorageTransactionId,
        control: &StorageReadControl,
    ) -> VersionResult<CommitStatus> {
        self.check_transaction(transaction)?;
        control.cancellation().check()?;
        self.with(|connection| {
            let read = connection.unchecked_transaction()?;
            native::check_mapping(&read, self.native)?;
            codec::header(&read, self.identity)?;
            let status = codec::status(&read, transaction)?;
            control.cancellation().check().map_err(VersionError::from)?;
            read.commit()?;
            Ok(status)
        })
    }

    fn acknowledge_transaction(
        &self,
        acknowledgement: uqa_storage::mvcc::ReceiptAcknowledgement,
        control: &StorageReadControl,
    ) -> VersionResult<()> {
        self.check_transaction(acknowledgement.transaction())?;
        self.with_write(control, |connection| {
            receipts::acknowledge(connection, self.native, acknowledgement, control)
        })
    }

    fn reclaim_transaction_receipts(&self, control: &StorageReadControl) -> VersionResult<u64> {
        self.reclaim_receipts(control)
    }
    fn abort(
        &self,
        transaction: StorageTransactionId,
        control: &StorageReadControl,
    ) -> VersionResult<CommitStatus> {
        self.check_transaction(transaction)?;
        control.cancellation().check()?;
        self.with(|connection| write::abort(connection, transaction, self.native, control))
    }
}