Struct Datastore

Source
#[non_exhaustive]
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<Self, Error>

Creates a new datastore instance

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

Or to create a file-backed store:

let ds = Datastore::new("surrealkv://temp.skv").await?;

Or to connect to a tikv-backed distributed store:

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

pub async fn new_with_clock( path: &str, clock: Option<Arc<SizedClock>>, ) -> Result<Datastore, Error>

Source

pub fn restart(self) -> Self

Create a new datastore with the same persistent data (inner), with flushed cache. Simulating a server restart

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 with_temporary_directory(self, path: Option<PathBuf>) -> Self

Set a temporary directory for ordering of large result sets

Source

pub fn index_store(&self) -> &IndexStores

Source

pub fn is_auth_enabled(&self) -> bool

Is authentication enabled for this Datastore?

Source

pub fn id(&self) -> Uuid

Source

pub fn allows_http_route(&self, route_target: &RouteTarget) -> bool

Does the datastore allow requesting an HTTP route? This function needs to be public to allow access from the CLI crate.

Source

pub fn get_capabilities(&self) -> &Capabilities

Set specific capabilities for this Datastore

Source

pub fn get_cache(&self) -> Arc<DatastoreCache>

Source

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

Source

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

Source

pub async fn initialise_credentials( &self, user: &str, pass: &str, ) -> Result<(), Error>

Setup the initial cluster access credentials

Source

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

Initialise the cluster and run bootstrap utilities

Source

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

Run the background task to update node registration information

Source

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

Run the background task to process and archive inactive nodes

Source

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

Run the background task to process and cleanup archived nodes

Source

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

Run the background task to perform changefeed garbage collection

Source

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

Run the background task to perform changefeed garbage collection

Source

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

Run the datastore shutdown tasks, perfoming any necessary cleanup

Source

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

Create a new transaction on this datastore

use surrealdb_core::kvs::{Datastore, TransactionType::*, LockType::*};
use surrealdb_core::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_core::kvs::Datastore;
use surrealdb_core::err::Error;
use surrealdb_core::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 execute_import<S>( &self, sess: &Session, vars: Option<BTreeMap<String, Value>>, query: S, ) -> Result<Vec<Response>, Error>
where S: Stream<Item = Result<Bytes, Error>>,

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_core::kvs::Datastore;
use surrealdb_core::err::Error;
use surrealdb_core::dbs::Session;
use surrealdb_core::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_core::kvs::Datastore;
use surrealdb_core::err::Error;
use surrealdb_core::dbs::Session;
use surrealdb_core::sql::Future;
use surrealdb_core::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 record access SIGNUP or SIGNIN clause, which still needs to work without guest access.

use surrealdb_core::kvs::Datastore;
use surrealdb_core::err::Error;
use surrealdb_core::dbs::Session;
use surrealdb_core::sql::Future;
use surrealdb_core::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_core::kvs::Datastore;
use surrealdb_core::err::Error;
use surrealdb_core::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 import( &self, sql: &str, sess: &Session, ) -> Result<Vec<Response>, Error>

Performs a database import from SQL

Source

pub async fn import_stream<S>( &self, sess: &Session, stream: S, ) -> Result<Vec<Response>, Error>
where S: Stream<Item = Result<Bytes, Error>>,

Performs a database import from SQL

Source

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

Performs a full database export as SQL

Source

pub async fn export_with_config( &self, sess: &Session, chn: Sender<Vec<u8>>, cfg: Config, ) -> Result<impl Future<Output = Result<(), Error>>, Error>

Performs a full database export as SQL

Source

pub fn check( &self, sess: &Session, action: Action, resource: Resource, ) -> Result<(), Error>

Checks the required permissions level for this session

Source

pub fn setup_options(&self, sess: &Session) -> Options

Source

pub fn setup_ctx(&self) -> Result<MutableContext, Error>

Source

pub fn check_anon(&self, sess: &Session) -> Result<(), IamError>

check for disallowed anonymous users

Source§

impl Datastore

Source

pub async fn insert_node(&self, id: Uuid) -> Result<(), Error>

Inserts a node for the first time into the cluster.

This function should be run at server or database startup.

This function ensures that this node is entered into the clister membership entries. This function must be run at server or database startup, in order to write the initial entry and timestamp to storage.

Source

pub async fn update_node(&self, id: Uuid) -> Result<(), Error>

Updates an already existing node in the cluster.

This function should be run periodically at a regular interval.

This function updates the entry for this node with an up-to-date timestamp. This ensures that the node is not marked as expired by any garbage collection tasks, preventing any data cleanup for this node.

Source

pub async fn delete_node(&self, id: Uuid) -> Result<(), Error>

Deletes a node from the cluster.

This function should be run when a node is shutting down.

This function marks the node as archived, ready for garbage collection. Later on when garbage collection is running the live queries assigned to this node will be removed, along with the node itself.

Source

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

Expires nodes which have timedout from the cluster.

This function should be run periodically at an interval.

This function marks the node as archived, ready for garbage collection. Later on when garbage collection is running the live queries assigned to this node will be removed, along with the node itself.

Source

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

Removes and cleans up nodes which are no longer in this cluster.

This function should be run periodically at an interval.

This function clears up all nodes which have been marked as archived. When a matching node is found, all node queries, and table queries are garbage collected, before the node itself is completely deleted.

Source

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

Clean up all other miscellaneous data.

This function should be run periodically at an interval.

This function clears up all data which might have been missed from previous cleanup runs, or when previous runs failed. This function currently deletes all live queries, for nodes which no longer exist in the cluster, from all namespaces, databases, and tables. It uses a number of transactions in order to prevent failure of large or long-running transactions on distributed storage engines.

Source

pub async fn delete_queries(&self, ids: Vec<Uuid>) -> Result<(), Error>

Clean up the live queries for a disconnected connection.

This function should be run when a WebSocket disconnects.

This function clears up the live queries on the current node, which are specified by uique live query UUIDs. This is necessary when a WebSocket disconnects, and any associated live queries need to be cleaned up and removed.

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

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T

Source§

impl<T> ParallelSend for T
where T: Send,