pub(super) mod columns;
pub(super) fn probe_equal(
snapshot: &NativeSnapshot,
table: &str,
field: &ValueIndexKey,
probe: &super::probe::EqualityProbe,
) -> Result<Option<Vec<DocId>>> {
let Some(owner) = snapshot.table_owner(table)? else {
return Ok(None);
};
let field = SQLiteValueIndexKey(field);
if !snapshot.contains_row(Family::BtreeIndexes, owner, &[field.as_value_ref()])?
|| snapshot.contains_row(Family::BtreeIndexRepairs, owner, &[field.as_value_ref()])?
{
return Ok(None);
}
snapshot.probe_index_equal(table, owner, field.as_value_ref(), probe)
}
pub(super) fn visit_equal(
snapshot: &NativeSnapshot,
table: &str,
field: &ValueIndexKey,
probe: &super::probe::EqualityProbe,
visit: &mut dyn FnMut(DocId) -> bool,
) -> Result<bool> {
let Some(owner) = snapshot.table_owner(table)? else {
return Ok(false);
};
let field = SQLiteValueIndexKey(field);
if !snapshot.contains_row(Family::BtreeIndexes, owner, &[field.as_value_ref()])?
|| snapshot.contains_row(Family::BtreeIndexRepairs, owner, &[field.as_value_ref()])?
{
return Ok(false);
}
snapshot.visit_index_equal(table, owner, field.as_value_ref(), probe, visit)
}
use rusqlite::types::{FromSql, ValueRef};
use uqa_storage::{document_store::identifiers::DocumentIdNamespace, KeyValueBatch, ValueIndexKey};
use super::{
decode_doc_id, decode_value, encode_doc_id, encode_value, BTreeMap, DocId, Result, SQLiteError,
SQLiteValueIndexKey, Value,
};
use crate::mvcc::native::{
NativeRecordFamily as Family, NativeRecordIdentity, NativeRecordOwner, NativeSnapshot,
};
fn field_value(field: ValueRef<'_>) -> Result<ValueIndexKey> {
SQLiteValueIndexKey::column_result(field)
.map(|key| key.0)
.map_err(|error| {
SQLiteError::StorageBackend(format!("invalid native B-tree field: {error}"))
})
}
pub(super) fn fields(snapshot: &NativeSnapshot, table: &str) -> Result<Vec<ValueIndexKey>> {
let mut fields = Vec::new();
if let Some(owner) = snapshot.table_owner(table)? {
snapshot.visit_rows(Family::BtreeIndexes, Some(owner), &[], |row| {
fields.push(field_value(row[1])?);
Ok(())
})?;
}
Ok(fields)
}
pub(super) fn repairs(snapshot: &NativeSnapshot) -> Result<Vec<(String, ValueIndexKey)>> {
let mut repairs = Vec::new();
snapshot.visit_rows(Family::BtreeIndexRepairs, None, &[], |row| {
let table = row[0].as_str().map_err(|_| {
SQLiteError::StorageBackend("native B-tree repair table must be text".into())
})?;
repairs.push((table.to_owned(), field_value(row[1])?));
Ok(())
})?;
repairs.sort_unstable();
Ok(repairs)
}
pub(super) fn clear_repair(
snapshot: &NativeSnapshot,
batch: &mut dyn KeyValueBatch,
table: &str,
field: &ValueIndexKey,
) -> Result<()> {
if let Some(owner) = snapshot.table_owner(table)? {
snapshot.delete_prefix(
batch,
Family::BtreeIndexRepairs,
owner,
&[SQLiteValueIndexKey(field).as_value_ref()],
)?;
}
Ok(())
}
pub(super) fn read_entry(
snapshot: &NativeSnapshot,
table: &str,
field: &ValueIndexKey,
doc_id: DocId,
) -> Result<uqa_storage::ValueIndexEntry> {
let Some(owner) = snapshot.table_owner(table)? else {
return Ok(uqa_storage::ValueIndexEntry::Unbuilt);
};
let field = SQLiteValueIndexKey(field);
if !snapshot.contains_row(Family::BtreeIndexes, owner, &[field.as_value_ref()])? {
return Ok(uqa_storage::ValueIndexEntry::Unbuilt);
}
let value = snapshot.read_row(
Family::BtreeIndexEntries,
owner,
&[
field.as_value_ref(),
ValueRef::Integer(encode_doc_id(doc_id)?),
],
|row| {
let value = row[3].as_str().map_err(|_| {
SQLiteError::StorageBackend("native B-tree value must be text".into())
})?;
decode_value(value)
},
)?;
Ok(value.map_or(
uqa_storage::ValueIndexEntry::Absent,
uqa_storage::ValueIndexEntry::Present,
))
}
pub(super) fn load(
snapshot: &NativeSnapshot,
table: &str,
field: &ValueIndexKey,
) -> Result<Option<Vec<(DocId, Value)>>> {
let mut values = Vec::new();
let built = visit_entries(snapshot, table, field, &mut |id, value| {
values.push((id, value));
Ok(())
})?;
Ok(built.then_some(values))
}
pub(super) fn visit_entries(
snapshot: &NativeSnapshot,
table: &str,
field: &ValueIndexKey,
visit: &mut dyn FnMut(DocId, Value) -> Result<()>,
) -> Result<bool> {
let Some(owner) = snapshot.table_owner(table)? else {
return Ok(false);
};
let field = SQLiteValueIndexKey(field);
if !snapshot.contains_row(Family::BtreeIndexes, owner, &[field.as_value_ref()])? {
return Ok(false);
}
if snapshot.visit_latest_index_entries(
table,
owner,
field.as_value_ref(),
&snapshot.control,
&mut |doc_id, value| {
visit(decode_doc_id(doc_id)?, decode_value(value)?)?;
Ok(true)
},
)? {
return Ok(true);
}
snapshot.visit_rows(
Family::BtreeIndexEntries,
Some(owner),
&[field.as_value_ref()],
|row| {
let doc_id = row[2].as_i64().map_err(|_| {
SQLiteError::StorageBackend("native B-tree document id must be integer".into())
})?;
let value = row[3].as_str().map_err(|_| {
SQLiteError::StorageBackend("native B-tree value must be text".into())
})?;
visit(decode_doc_id(doc_id)?, decode_value(value)?)?;
Ok(())
},
)?;
Ok(true)
}
fn mark(
snapshot: &NativeSnapshot,
batch: &mut dyn KeyValueBatch,
table: &str,
owner: NativeRecordOwner,
field: ValueRef<'_>,
) -> Result<()> {
if !snapshot.contains_row(Family::BtreeIndexes, owner, &[field])? {
snapshot.put_row(
batch,
Family::BtreeIndexes,
owner,
&[ValueRef::Text(table.as_bytes()), field],
)?;
}
Ok(())
}
fn entry(
snapshot: &NativeSnapshot,
batch: &mut dyn KeyValueBatch,
table: &str,
owner: NativeRecordOwner,
field: ValueRef<'_>,
doc_id: DocId,
value: &Value,
) -> Result<()> {
let (id, row) = entry_row(snapshot, owner, doc_id, value)?;
snapshot.put_row(
batch,
Family::BtreeIndexEntries,
owner,
&[
ValueRef::Text(table.as_bytes()),
field,
ValueRef::Integer(id),
ValueRef::Text(row.as_bytes()),
],
)
}
fn unused_entry(
snapshot: &NativeSnapshot,
batch: &mut dyn KeyValueBatch,
table: &str,
owner: NativeRecordOwner,
field: ValueRef<'_>,
doc_id: DocId,
value: &Value,
) -> Result<()> {
let (id, row) = entry_row(snapshot, owner, doc_id, value)?;
snapshot.put_unused_row(
batch,
Family::BtreeIndexEntries,
owner,
&[
ValueRef::Text(table.as_bytes()),
field,
ValueRef::Integer(id),
ValueRef::Text(row.as_bytes()),
],
)
}
fn entry_row(
snapshot: &NativeSnapshot,
owner: NativeRecordOwner,
doc_id: DocId,
value: &Value,
) -> Result<(i64, String)> {
let id = encode_doc_id(doc_id)?;
if !snapshot.contains_row(Family::Documents, owner, &[ValueRef::Integer(id)])? {
return Err(SQLiteError::StorageBackend(
"persistent B-tree entry has no backing document".into(),
));
}
Ok((id, encode_value(value)?))
}
pub(super) fn replace_many(
snapshot: &NativeSnapshot,
batch: &mut dyn KeyValueBatch,
table: &str,
indexes: &[(&ValueIndexKey, &[(DocId, Value)])],
) -> Result<()> {
if indexes.is_empty() {
return Ok(());
}
let owner = snapshot.ensure_table_owner(table, batch)?;
for (field, values) in indexes {
let field = SQLiteValueIndexKey(*field);
mark(snapshot, batch, table, owner, field.as_value_ref())?;
snapshot.delete_prefix(
batch,
Family::BtreeIndexEntries,
owner,
&[field.as_value_ref()],
)?;
let mut ids = uqa_core::memory::BudgetedVec::new(snapshot.control.memory());
for (id, _) in *values {
ids.push(encode_doc_id(*id)?)?;
}
ids.sort_unstable();
if ids.windows(2).any(|ids| ids[0] == ids[1]) {
return Err(SQLiteError::StorageBackend(
"duplicate document id in persisted B-tree replacement".into(),
));
}
for (id, value) in *values {
entry(
snapshot,
batch,
table,
owner,
field.as_value_ref(),
*id,
value,
)?;
}
}
Ok(())
}
pub(super) fn repair(
snapshot: &NativeSnapshot,
batch: &mut dyn KeyValueBatch,
table: &str,
field: &ValueIndexKey,
stale: &[DocId],
missing: &[(DocId, Value)],
) -> Result<()> {
let owner = snapshot.ensure_table_owner(table, batch)?;
let field = SQLiteValueIndexKey(field);
mark(snapshot, batch, table, owner, field.as_value_ref())?;
for &id in stale {
snapshot.delete_prefix(
batch,
Family::BtreeIndexEntries,
owner,
&[field.as_value_ref(), ValueRef::Integer(encode_doc_id(id)?)],
)?;
}
for (id, value) in missing {
entry(
snapshot,
batch,
table,
owner,
field.as_value_ref(),
*id,
value,
)?;
}
Ok(())
}
pub(super) fn apply_write(
snapshot: &NativeSnapshot,
batch: &mut dyn KeyValueBatch,
table: &str,
doc_id: DocId,
values: Option<&BTreeMap<ValueIndexKey, Value>>,
unused: Option<DocumentIdNamespace>,
) -> Result<()> {
let id = encode_doc_id(doc_id)?;
let Some(owner) = snapshot.table_owner(table)? else {
return Ok(());
};
if let Some(values) = values {
let write = if unused.is_some_and(|namespace| owner.stores(namespace)) {
unused_entry
} else {
entry
};
for (field, value) in values {
let field = SQLiteValueIndexKey(field);
if snapshot.contains_definition_row(
Family::BtreeIndexes,
owner,
&[field.as_value_ref()],
)? {
write(
snapshot,
batch,
table,
owner,
field.as_value_ref(),
doc_id,
value,
)?;
}
}
} else {
delete_document(snapshot, batch, owner, id)?;
}
Ok(())
}
pub(super) fn drop_index(
snapshot: &NativeSnapshot,
batch: &mut dyn KeyValueBatch,
table: &str,
field: &ValueIndexKey,
) -> Result<()> {
if let Some(owner) = snapshot.table_owner(table)? {
for family in [Family::BtreeIndexEntries, Family::BtreeIndexes] {
snapshot.delete_prefix(
batch,
family,
owner,
&[SQLiteValueIndexKey(field).as_value_ref()],
)?;
}
}
Ok(())
}
pub(super) fn clear_table(
snapshot: &NativeSnapshot,
batch: &mut dyn KeyValueBatch,
table: &str,
) -> Result<()> {
if let Some(owner) = snapshot.table_owner(table)? {
snapshot.delete_prefix(batch, Family::BtreeIndexEntries, owner, &[])?;
}
Ok(())
}
pub(crate) fn delete_document(
snapshot: &NativeSnapshot,
batch: &mut dyn KeyValueBatch,
owner: NativeRecordOwner,
id: i64,
) -> Result<()> {
let identity = NativeRecordIdentity::new(Family::BtreeIndexEntries, owner)?;
let control = &snapshot.control;
let prefix = identity.encode_prefix(&[], control)?;
let mut after = None;
loop {
let mut next = None;
snapshot
.view
.visit_keys(&prefix, after.as_deref(), 1, control, &mut |key, _| {
NativeRecordIdentity::visit_key_components(key, control, |component, field| {
if component == 0 {
batch.delete(
&identity.encode_key(&[field, ValueRef::Integer(id)], control)?,
)?;
next = Some(
identity.encode_key(&[field, ValueRef::Integer(i64::MAX)], control)?,
);
}
Ok(())
})?;
Ok(false)
})?;
let Some(next) = next else { break };
after = Some(next);
}
Ok(())
}