Skip to main content

RelationalDb

Struct RelationalDb 

Source
pub struct RelationalDb<B>
where B: StorageBackend,
{ /* private fields */ }
Expand description

Relational (row/column) facade layered on top of a raw StorageBackend, sharing the same backend instance a crate::graph::GraphDb can use — that shared Arc<B> is what makes the two layers “hybrid” rather than two unrelated databases.

Implementations§

Source§

impl<B> RelationalDb<B>
where B: StorageBackend,

Source

pub fn new(backend: B) -> RelationalDb<B>

Source

pub fn from_arc(backend: Arc<B>) -> RelationalDb<B>

Source

pub fn table(&self, schema: impl Into<TableSchema>) -> RelTable<'_, B>

A handle on the table described by schema (a &RelSchema static or a TableSchema); the table doesn’t have to be registered in the catalog.

Panics if the name collides with one of bkndb-core’s reserved internal table names (see crate::RESERVED_TABLE_NAMES) — a schema-authoring bug caught at first use, not a recoverable runtime condition. (A TableSchema built with its builder can never have a reserved name.)

Source

pub fn table_named(&self, name: &str) -> Result<RelTable<'_, B>, BknError>

A handle on a table registered in the catalog, by name. The handle always uses the table’s current definition, so it stays valid across migrations.

Source

pub fn create_table( &self, schema: impl Into<TableSchema>, ) -> Result<bool, BknError>

Registers a new table. Returns false if an identical definition was already registered; errors if a different one is (see RelationalDb::ensure_table for migrating).

Source

pub fn ensure_table( &self, schema: impl Into<TableSchema>, ) -> Result<(), BknError>

Creates the table if needed, or migrates the registered one to match schema: added/removed columns (defaults are backfilled into existing rows), changed NOT NULL / UNIQUE constraints (validated against existing rows), and added/removed indexes (new ones are backfilled). The primary key and existing columns’ kinds can’t change. Atomic.

Also the way to adopt a table previously used only through a static RelSchema: its indexes are rebuilt from the existing rows.

Source

pub fn drop_table(&self, name: &str) -> Result<bool, BknError>

Deletes a registered table with all its rows and indexes. Returns false if no such table is registered.

Source

pub fn create_index(&self, table: &str, column: &str) -> Result<(), BknError>

Adds a (backfilled) secondary index to a registered table.

Source

pub fn drop_index(&self, table: &str, column: &str) -> Result<(), BknError>

Removes a secondary index (and any UNIQUE constraint relying on it).

Source

pub fn table_schema(&self, name: &str) -> Result<Option<TableSchema>, BknError>

Source

pub fn sql( &self, sql: &str, params: impl Into<Params>, ) -> Result<QueryResult, BknError>

Runs one SQL statement (see crate::lang::sql for the supported subset): a query in a read snapshot, anything else in its own atomic write transaction.

ⓘ
db.sql("SELECT name FROM users WHERE age >= ? ORDER BY name", vec![18.into()])?;
Source

pub fn list_tables(&self) -> Result<Vec<TableSchema>, BknError>

Every table registered in the catalog, sorted by name.

Source

pub fn write_tx<F, R>(&self, f: F) -> Result<R, BknError>
where F: for<'w> FnOnce(&mut RelWriteBatch<<B as StorageBackend>::WriteTx<'w>>) -> Result<R, BknError>,

Runs f against one shared write transaction spanning however many tables it touches (via crate::relational::txn::RelWriteBatch::table), committing only if f returns Ok. An Err return leaves the write tx uncommitted and it is simply dropped — every StorageWriteTx backend buffers writes until commit(), so a dropped, uncommitted tx is already a no-op rollback with no extra bookkeeping needed here.

Source§

impl<B> RelationalDb<B>
where B: StorageBackend,

Source

pub fn create_fulltext_index( &self, table: &str, column: &str, ) -> Result<bool, BknError>

Builds a full-text index over a Str column of a registered table (backfilling existing rows). Returns false if it already exists.

Source

pub fn drop_fulltext_index( &self, table: &str, column: &str, ) -> Result<bool, BknError>

Removes a full-text index; false if there was none.

Source

pub fn fulltext_indexes(&self, table: &str) -> Result<Vec<String>, BknError>

Columns of table with a full-text index.

Source

pub fn search_text( &self, table: &str, column: &str, query: &str, limit: usize, match_all: bool, filter: Option<&Expr>, ) -> Result<Vec<ScoredRow>, BknError>

Up to limit rows whose column best matches query (BM25). See the module docs.

Source

pub fn search_vector( &self, table: &str, column: &str, query: &[f32], limit: usize, metric: VectorMetric, filter: Option<&Expr>, ) -> Result<Vec<ScoredRow>, BknError>

The limit rows whose embedding in column is nearest to query, from the column’s vector index when it has one for metric (approximate), else by scanning (exact). See the module docs.

Source

pub fn search_vector_with( &self, table: &str, column: &str, query: &[f32], limit: usize, metric: VectorMetric, filter: Option<&Expr>, options: VectorSearchOptions, ) -> Result<Vec<ScoredRow>, BknError>

Self::search_vector with per-query options: force an exact scan, or trade speed for recall with ef_search.

Source

pub fn create_vector_index( &self, table: &str, column: &str, options: VectorIndexOptions, ) -> Result<bool, BknError>

Builds an approximate (HNSW) vector index over a List/Bytes embedding column for options.metric, backfilling existing rows. Returns false if the column already has one (drop it first to change the parameters). Once it exists, every indexed vector must have the same number of dimensions. See crate::relational::ann.

Source

pub fn drop_vector_index( &self, table: &str, column: &str, ) -> Result<bool, BknError>

Removes a vector index; false if there was none.

Source

pub fn vector_indexes( &self, table: &str, ) -> Result<Vec<VectorIndexInfo>, BknError>

The vector indexes of table.

Auto Trait Implementations§

§

impl<B> Freeze for RelationalDb<B>
where Arc<B>: Freeze,

§

impl<B> RefUnwindSafe for RelationalDb<B>
where Arc<B>: RefUnwindSafe,

§

impl<B> Send for RelationalDb<B>
where Arc<B>: Send,

§

impl<B> Sync for RelationalDb<B>
where Arc<B>: Sync,

§

impl<B> Unpin for RelationalDb<B>
where Arc<B>: Unpin,

§

impl<B> UnsafeUnpin for RelationalDb<B>
where Arc<B>: UnsafeUnpin,

§

impl<B> UnwindSafe for RelationalDb<B>
where Arc<B>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.