pub mod all;
pub mod dc;
#[cfg(diskann)]
pub mod dd;
#[cfg(diskann)]
pub mod de;
#[cfg(diskann)]
pub mod dg;
#[cfg(diskann)]
pub mod dh;
#[cfg(diskann)]
pub mod di;
pub mod dl;
#[cfg(diskann)]
pub mod dn;
#[cfg(diskann)]
#[allow(dead_code)]
pub mod dp;
#[cfg(diskann)]
pub mod dq;
#[cfg(diskann)]
pub mod dr;
#[cfg(diskann)]
pub mod ds;
pub mod dv;
#[cfg(diskann)]
pub mod dw;
#[cfg(diskann)]
pub mod dy;
pub mod hd;
pub mod he;
pub mod hg;
pub mod hh;
pub mod hi;
pub mod hl;
pub mod hn;
pub mod hp;
pub mod hr;
pub mod hs;
pub mod hv;
pub mod ib;
pub mod id;
pub mod ig;
pub mod ii;
pub mod ip;
pub mod is;
pub mod iu;
pub mod iv;
pub mod td;
pub mod tt;
pub mod tv;
use std::borrow::Cow;
use anyhow::Result;
use storekey::{BorrowDecode, Encode};
use crate::catalog::{DatabaseId, IndexId, NamespaceId};
use crate::key::category::{Categorise, Category};
use crate::kvs::{KVKey, impl_kv_key_storekey};
use crate::val::{Array, IndexFormat, RecordId, RecordIdKey, TableName};
#[derive(Clone, Debug, PartialEq, PartialOrd, Encode, BorrowDecode)]
#[storekey(format = "()")]
struct Prefix<'a> {
__: u8,
_a: u8,
pub ns: NamespaceId,
_b: u8,
pub db: DatabaseId,
_c: u8,
pub tb: Cow<'a, TableName>,
_d: u8,
pub ix: IndexId,
_e: u8,
}
impl_kv_key_storekey!(Prefix<'_> => Vec<u8>);
impl<'a> Prefix<'a> {
fn new(ns: NamespaceId, db: DatabaseId, tb: &'a TableName, ix: IndexId) -> Self {
Self {
__: b'/',
_a: b'*',
ns,
_b: b'*',
db,
_c: b'*',
tb: Cow::Borrowed(tb),
_d: b'+',
ix,
_e: b'*',
}
}
}
#[derive(Clone, Debug, PartialEq, PartialOrd, Encode, BorrowDecode)]
#[storekey(format = "IndexFormat")]
struct PrefixIds<'a> {
__: u8,
_a: u8,
pub ns: NamespaceId,
_b: u8,
pub db: DatabaseId,
_c: u8,
pub tb: Cow<'a, TableName>,
_d: u8,
pub ix: IndexId,
_e: u8,
pub fd: Cow<'a, Array>,
}
impl crate::kvs::KVKey for PrefixIds<'_> {
type ValueType = Vec<u8>;
fn encode_key(&self) -> anyhow::Result<Vec<u8>> {
Ok(storekey::encode_vec_format::<IndexFormat, _>(self)
.map_err(|_| crate::err::Error::Unencodable)?)
}
fn value_context(&self) {}
}
impl<'a> PrefixIds<'a> {
fn new(ns: NamespaceId, db: DatabaseId, tb: &'a TableName, ix: IndexId, fd: &'a Array) -> Self {
Self {
__: b'/',
_a: b'*',
ns,
_b: b'*',
db,
_c: b'*',
tb: Cow::Borrowed(tb),
_d: b'+',
ix,
_e: b'*',
fd: Cow::Borrowed(fd),
}
}
}
#[derive(Clone, Debug, PartialEq, PartialOrd, Encode, BorrowDecode)]
#[storekey(format = "IndexFormat")]
pub(crate) struct Index<'a> {
__: u8,
_a: u8,
pub ns: NamespaceId,
_b: u8,
pub db: DatabaseId,
_c: u8,
pub tb: Cow<'a, TableName>,
_d: u8,
pub ix: IndexId,
_e: u8,
pub fd: Cow<'a, Array>,
pub id: Option<Cow<'a, RecordIdKey>>,
}
impl crate::kvs::KVKey for Index<'_> {
type ValueType = RecordId;
fn encode_key(&self) -> ::anyhow::Result<Vec<u8>> {
Ok(storekey::encode_vec_format::<IndexFormat, _>(self)
.map_err(|_| crate::err::Error::Unencodable)?)
}
fn value_context(&self) {}
}
impl Categorise for Index<'_> {
fn categorise(&self) -> Category {
Category::Index
}
}
impl<'a> Index<'a> {
pub fn new(
ns: NamespaceId,
db: DatabaseId,
tb: &'a TableName,
ix: IndexId,
fd: &'a Array,
id: Option<&'a RecordIdKey>,
) -> Self {
Self {
__: b'/',
_a: b'*',
ns,
_b: b'*',
db,
_c: b'*',
tb: Cow::Borrowed(tb),
_d: b'+',
ix,
_e: b'*',
fd: Cow::Borrowed(fd),
id: id.map(Cow::Borrowed),
}
}
fn prefix(ns: NamespaceId, db: DatabaseId, tb: &TableName, ix: IndexId) -> Result<Vec<u8>> {
Prefix::new(ns, db, tb, ix).encode_key()
}
pub fn prefix_beg(
ns: NamespaceId,
db: DatabaseId,
tb: &TableName,
ix: IndexId,
) -> Result<Vec<u8>> {
let mut beg = Self::prefix(ns, db, tb, ix)?;
beg.extend_from_slice(&[0x00]); Ok(beg)
}
pub fn prefix_end(
ns: NamespaceId,
db: DatabaseId,
tb: &TableName,
ix: IndexId,
) -> Result<Vec<u8>> {
let mut beg = Self::prefix(ns, db, tb, ix)?;
beg.extend_from_slice(&[0xff]); Ok(beg)
}
fn prefix_ids(
ns: NamespaceId,
db: DatabaseId,
tb: &TableName,
ix: IndexId,
fd: &Array,
) -> Result<Vec<u8>> {
PrefixIds::new(ns, db, tb, ix, fd).encode_key()
}
pub fn prefix_ids_beg(
ns: NamespaceId,
db: DatabaseId,
tb: &TableName,
ix: IndexId,
fd: &Array,
) -> Result<Vec<u8>> {
let mut beg = Self::prefix_ids(ns, db, tb, ix, fd)?;
beg.extend_from_slice(&[0x00]);
Ok(beg)
}
pub fn prefix_ids_end(
ns: NamespaceId,
db: DatabaseId,
tb: &TableName,
ix: IndexId,
fd: &Array,
) -> Result<Vec<u8>> {
let mut beg = Self::prefix_ids(ns, db, tb, ix, fd)?;
beg.extend_from_slice(&[0xff]);
Ok(beg)
}
pub fn prefix_ids_composite_beg(
ns: NamespaceId,
db: DatabaseId,
tb: &TableName,
ix: IndexId,
fd: &Array,
) -> Result<Vec<u8>> {
let mut beg = Self::prefix_ids(ns, db, tb, ix, fd)?;
*beg.last_mut().expect("prefix buffer is non-empty") = 0x00; Ok(beg)
}
pub fn prefix_ids_composite_end(
ns: NamespaceId,
db: DatabaseId,
tb: &TableName,
ix: IndexId,
fd: &Array,
) -> Result<Vec<u8>> {
let mut beg = Self::prefix_ids(ns, db, tb, ix, fd)?;
*beg.last_mut().expect("prefix buffer is non-empty") = 0xff; Ok(beg)
}
}
#[cfg(test)]
mod tests {
use surrealdb_strand::Strand;
use super::*;
use crate::val::Array;
#[test]
fn key() {
let fd: Array = vec!["testfd1", "testfd2"].into();
let id = RecordIdKey::String(Strand::new_static("testid"));
let tb = TableName::from("testtb");
let val = Index::new(NamespaceId(1), DatabaseId(2), &tb, IndexId(3), &fd, Some(&id));
let enc = Index::encode_key(&val).unwrap();
assert_eq!(
enc,
b"/*\x00\x00\x00\x01*\x00\x00\x00\x02*testtb\0+\0\0\0\x03*\x06testfd1\0\x06testfd2\0\0\x03\x03testid\0"
);
}
#[test]
fn key_none() {
let fd: Array = vec!["testfd1", "testfd2"].into();
let tb = TableName::from("testtb");
let val = Index::new(NamespaceId(1), DatabaseId(2), &tb, IndexId(3), &fd, None);
let enc = Index::encode_key(&val).unwrap();
assert_eq!(
enc,
b"/*\0\0\0\x01*\0\0\0\x02*testtb\0+\0\0\0\x03*\x06testfd1\0\x06testfd2\0\0\x02"
);
}
#[test]
fn check_composite() {
let fd: Array = vec!["testfd1"].into();
let tb = TableName::from("testtb");
let enc =
Index::prefix_ids_composite_beg(NamespaceId(1), DatabaseId(2), &tb, IndexId(3), &fd)
.unwrap();
assert_eq!(
enc,
b"/*\x00\x00\x00\x01*\x00\x00\x00\x02*testtb\0+\0\0\0\x03*\x06testfd1\0\x00"
);
let enc =
Index::prefix_ids_composite_end(NamespaceId(1), DatabaseId(2), &tb, IndexId(3), &fd)
.unwrap();
assert_eq!(
enc,
b"/*\x00\x00\x00\x01*\x00\x00\x00\x02*testtb\0+\0\0\0\x03*\x06testfd1\0\xff"
);
}
}