uqa-storage-sqlite 0.5.3

SQLite catalog, indexes, compressed storage, graph and key/value providers
Documentation
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! Retained vector generations distinguish committed data and non-reusable private changes.

use super::NativeVectorRead;
use crate::mvcc::native::{NativeRecordFamily as Family, NativeRecordIdentity, NativeRecordOwner};
use crate::Result;
use rusqlite::types::ValueRef;
use uqa_storage::mvcc::{CommitSequence, PrivateRecordRevision};

#[derive(Clone, PartialEq, Eq)]
pub(in crate::vector_index) struct VectorIdentity {
    owner: NativeRecordOwner,
    committed: Option<CommitSequence>,
    private: Option<PrivateRecordRevision>,
}

impl VectorIdentity {
    /// The provider must prove that this exact staged view committed immediately after its base sequence, without an intervening commit or later session mutation.
    pub(in crate::vector_index) fn after_uncontended_commit(
        mut self,
        sequence: CommitSequence,
    ) -> Self {
        self.committed = Some(sequence);
        self.private = None;
        self
    }
}

pub(in crate::vector_index) fn identity(
    read: &NativeVectorRead<'_>,
    families: &[Family],
) -> Result<Option<VectorIdentity>> {
    let Some(owner) = read.owner else {
        return Ok(None);
    };
    let snapshot = read.snapshot;
    let key = NativeRecordIdentity::new(
        Family::CacheRevisions,
        NativeRecordOwner::Database(snapshot.database),
    )?
    .encode_key(
        &[
            ValueRef::Text(b"data"),
            ValueRef::Text(read.index.table.as_bytes()),
        ],
        &snapshot.control,
    )?;
    let committed = snapshot
        .view
        .committed()
        .metadata(&key, &snapshot.control)?
        .and_then(|meta| meta.revision);
    let mut private = None;
    for &family in families {
        let prefix = NativeRecordIdentity::new(family, owner)?
            .encode_prefix(&[read.field()], &snapshot.control)?;
        private = private.max(
            snapshot
                .view
                .private_scope_revision(&prefix, &snapshot.control)?,
        );
    }
    Ok(Some(VectorIdentity {
        owner,
        committed,
        private,
    }))
}