surrealdb::kvs

Struct Datastore

Source
pub struct Datastore { /* private fields */ }
Expand description

The underlying datastore instance which stores the dataset.

Implementations§

Source§

impl Datastore

Source

pub async fn new(path: &str) -> Result<Datastore, Error>

Creates a new datastore instance

§Examples
let ds = Datastore::new("memory").await?;

Or to create a file-backed store:

let ds = Datastore::new("file://temp.db").await?;

Or to connect to a tikv-backed distributed store:

let ds = Datastore::new("tikv://127.0.0.1:2379").await?;
Source

pub fn with_node_id(self, id: Uuid) -> Self

Specify whether this Datastore should run in strict mode

Source

pub fn with_strict_mode(self, strict: bool) -> Self

Specify whether this Datastore should run in strict mode

Source

pub fn with_notifications(self) -> Self

Specify whether this datastore should enable live query notifications

Source

pub fn with_query_timeout(self, duration: Option<Duration>) -> Self

Set a global query timeout for this Datastore

Source

pub fn with_transaction_timeout(self, duration: Option<Duration>) -> Self

Set a global transaction timeout for this Datastore

Source

pub fn with_auth_enabled(self, enabled: bool) -> Self

Set whether authentication is enabled for this Datastore

Source

pub fn with_capabilities(self, caps: Capabilities) -> Self

Set specific capabilities for this Datastore

Source

pub fn is_auth_enabled(&self) -> bool

Is authentication enabled for this Datastore?

Source

pub async fn setup_initial_creds(&self, creds: Root<'_>) -> Result<(), Error>

Setup the initial credentials Trigger the unreachable definition compilation error, probably due to this issue: https://github.com/rust-lang/rust/issues/111370

Source

pub async fn bootstrap(&self) -> Result<(), Error>

Source

pub async fn register_remove_and_archive( &self, tx: &mut Transaction, node_id: &Uuid, ) -> Result<Vec<(LqValue, Option<Error>)>, Error>

Source

pub async fn register_membership( &self, tx: &mut Transaction, node_id: &Uuid, timestamp: Timestamp, ) -> Result<(), Error>

Source

pub async fn remove_dead_nodes( &self, tx: &mut Transaction, ts: &Timestamp, ) -> Result<Vec<Uuid>, Error>

Delete dead heartbeats and nodes Returns node IDs

Source

pub async fn archive_dead_lqs( &self, tx: &mut Transaction, nodes: &[Uuid], this_node_id: &Uuid, ) -> Result<Vec<(LqValue, Option<Error>)>, Error>

Accepts cluster IDs Archives related live queries Returns live query keys that can be used for deletes

The reason we archive first is to stop other nodes from picking it up for further updates This means it will be easier to wipe the range in a subsequent transaction

Source

pub async fn remove_archived( &self, tx: &mut Transaction, archived: Vec<LqValue>, ) -> Result<(), Error>

Source

pub async fn clear_unreachable_state( &self, tx: &mut Transaction, ) -> Result<(), Error>

Source

pub async fn garbage_collect_dead_session( &self, live_queries: &[Uuid], ) -> Result<(), Error>

Source

pub async fn archive_lv_for_node( &self, tx: &mut Transaction, nd: &Uuid, this_node_id: Uuid, ) -> Result<Vec<(LqValue, Option<Error>)>, Error>

Source

pub async fn delete_dead_heartbeats( &self, tx: &mut Transaction, ts: &Timestamp, ) -> Result<Vec<Hb>, Error>

Given a timestamp, delete all the heartbeats that have expired Return the removed heartbeats as they will contain node information

Source

pub async fn tick(&self) -> Result<(), Error>

Source

pub async fn tick_at(&self, ts: u64) -> Result<(), Error>

Source

pub async fn save_timestamp_for_versionstamp( &self, ts: u64, ) -> Result<(), Error>

Source

pub async fn garbage_collect_stale_change_feeds( &self, ts: u64, ) -> Result<(), Error>

Source

pub async fn heartbeat(&self) -> Result<(), Error>

Source

pub async fn heartbeat_full( &self, tx: &mut Transaction, timestamp: Timestamp, node_id: Uuid, ) -> Result<(), Error>

Source

pub async fn transaction( &self, write: TransactionType, lock: LockType, ) -> Result<Transaction, Error>

Create a new transaction on this datastore

use surrealdb::kvs::{Datastore, TransactionType::*, LockType::*};
use surrealdb::err::Error;

#[tokio::main]
async fn main() -> Result<(), Error> {
    let ds = Datastore::new("file://database.db").await?;
    let mut tx = ds.transaction(Write, Optimistic).await?;
    tx.cancel().await?;
    Ok(())
}
Source

pub async fn execute( &self, txt: &str, sess: &Session, vars: Option<BTreeMap<String, Value>>, ) -> Result<Vec<Response>, Error>

Parse and execute an SQL query

use surrealdb::kvs::Datastore;
use surrealdb::err::Error;
use surrealdb::dbs::Session;

#[tokio::main]
async fn main() -> Result<(), Error> {
    let ds = Datastore::new("memory").await?;
    let ses = Session::owner();
    let ast = "USE NS test DB test; SELECT * FROM person;";
    let res = ds.execute(ast, &ses, None).await?;
    Ok(())
}
Source

pub async fn process( &self, ast: Query, sess: &Session, vars: Option<BTreeMap<String, Value>>, ) -> Result<Vec<Response>, Error>

Execute a pre-parsed SQL query

use surrealdb::kvs::Datastore;
use surrealdb::err::Error;
use surrealdb::dbs::Session;
use surrealdb::sql::parse;

#[tokio::main]
async fn main() -> Result<(), Error> {
    let ds = Datastore::new("memory").await?;
    let ses = Session::owner();
    let ast = parse("USE NS test DB test; SELECT * FROM person;")?;
    let res = ds.process(ast, &ses, None).await?;
    Ok(())
}
Source

pub async fn compute( &self, val: Value, sess: &Session, vars: Option<BTreeMap<String, Value>>, ) -> Result<Value, Error>

Ensure a SQL Value is fully computed

use surrealdb::kvs::Datastore;
use surrealdb::err::Error;
use surrealdb::dbs::Session;
use surrealdb::sql::Future;
use surrealdb::sql::Value;

#[tokio::main]
async fn main() -> Result<(), Error> {
    let ds = Datastore::new("memory").await?;
    let ses = Session::owner();
    let val = Value::Future(Box::new(Future::from(Value::Bool(true))));
    let res = ds.compute(val, &ses, None).await?;
    Ok(())
}
Source

pub async fn evaluate( &self, val: Value, sess: &Session, vars: Option<BTreeMap<String, Value>>, ) -> Result<Value, Error>

Evaluates a SQL Value without checking authenticating config This is used in very specific cases, where we do not need to check whether authentication is enabled, or guest access is disabled. For example, this is used when processing a SCOPE SIGNUP or SCOPE SIGNIN clause, which still needs to work without guest access.

use surrealdb::kvs::Datastore;
use surrealdb::err::Error;
use surrealdb::dbs::Session;
use surrealdb::sql::Future;
use surrealdb::sql::Value;

#[tokio::main]
async fn main() -> Result<(), Error> {
    let ds = Datastore::new("memory").await?;
    let ses = Session::owner();
    let val = Value::Future(Box::new(Future::from(Value::Bool(true))));
    let res = ds.evaluate(val, &ses, None).await?;
    Ok(())
}
Source

pub fn notifications(&self) -> Option<Receiver<Notification>>

Subscribe to live notifications

use surrealdb::kvs::Datastore;
use surrealdb::err::Error;
use surrealdb::dbs::Session;

#[tokio::main]
async fn main() -> Result<(), Error> {
    let ds = Datastore::new("memory").await?.with_notifications();
    let ses = Session::owner();
	if let Some(channel) = ds.notifications() {
    	while let Ok(v) = channel.recv().await {
    	    println!("Received notification: {v}");
    	}
	}
    Ok(())
}
Source

pub async fn export( &self, sess: &Session, ns: String, db: String, chn: Sender<Vec<u8>>, ) -> Result<impl Future<Output = Result<(), Error>>, Error>

Performs a full database export as SQL

Source

pub async fn import( &self, sql: &str, sess: &Session, ) -> Result<Vec<Response>, Error>

Performs a database import from SQL

Trait Implementations§

Source§

impl Display for Datastore

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToSmolStr for T
where T: Display + ?Sized,

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<G1, G2> Within<G2> for G1
where G2: Contains<G1>,

Source§

fn is_within(&self, b: &G2) -> bool

Source§

impl<G1, G2> Within<G2> for G1
where G2: Contains<G1>,

Source§

fn is_within(&self, b: &G2) -> bool

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T