2022-05-03 20:20:36 +00:00
|
|
|
use super::tx::Transaction;
|
|
|
|
use crate::ctx::Context;
|
|
|
|
use crate::dbs::Attach;
|
|
|
|
use crate::dbs::Executor;
|
|
|
|
use crate::dbs::Options;
|
|
|
|
use crate::dbs::Response;
|
|
|
|
use crate::dbs::Session;
|
|
|
|
use crate::dbs::Variables;
|
|
|
|
use crate::err::Error;
|
2022-07-19 08:28:24 +00:00
|
|
|
use crate::kvs::LOG;
|
2022-05-03 20:20:36 +00:00
|
|
|
use crate::sql;
|
2022-06-20 11:26:27 +00:00
|
|
|
use crate::sql::Query;
|
|
|
|
use crate::sql::Value;
|
2022-05-15 19:38:46 +00:00
|
|
|
use channel::Sender;
|
2022-06-20 11:26:27 +00:00
|
|
|
use futures::lock::Mutex;
|
2023-03-29 18:16:18 +00:00
|
|
|
use std::fmt;
|
2022-06-20 11:26:27 +00:00
|
|
|
use std::sync::Arc;
|
2023-03-29 18:16:18 +00:00
|
|
|
use tracing::instrument;
|
2022-05-03 20:20:36 +00:00
|
|
|
|
|
|
|
/// The underlying datastore instance which stores the dataset.
|
2022-12-07 19:30:29 +00:00
|
|
|
#[allow(dead_code)]
|
2022-05-03 20:20:36 +00:00
|
|
|
pub struct Datastore {
|
|
|
|
pub(super) inner: Inner,
|
|
|
|
}
|
|
|
|
|
2022-05-06 22:09:49 +00:00
|
|
|
#[allow(clippy::large_enum_variant)]
|
2022-05-03 20:20:36 +00:00
|
|
|
pub(super) enum Inner {
|
2022-08-28 13:35:25 +00:00
|
|
|
#[cfg(feature = "kv-mem")]
|
2022-05-03 20:20:36 +00:00
|
|
|
Mem(super::mem::Datastore),
|
2022-08-28 13:35:25 +00:00
|
|
|
#[cfg(feature = "kv-rocksdb")]
|
|
|
|
RocksDB(super::rocksdb::Datastore),
|
2022-05-03 20:20:36 +00:00
|
|
|
#[cfg(feature = "kv-indxdb")]
|
2022-08-28 13:35:25 +00:00
|
|
|
IndxDB(super::indxdb::Datastore),
|
2022-05-03 20:20:36 +00:00
|
|
|
#[cfg(feature = "kv-tikv")]
|
|
|
|
TiKV(super::tikv::Datastore),
|
2022-08-15 18:35:41 +00:00
|
|
|
#[cfg(feature = "kv-fdb")]
|
|
|
|
FDB(super::fdb::Datastore),
|
2022-05-03 20:20:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 18:16:18 +00:00
|
|
|
impl fmt::Display for Datastore {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match &self.inner {
|
|
|
|
#[cfg(feature = "kv-mem")]
|
|
|
|
Inner::Mem(_) => write!(f, "memory"),
|
|
|
|
#[cfg(feature = "kv-rocksdb")]
|
|
|
|
Inner::RocksDB(_) => write!(f, "rocksdb"),
|
|
|
|
#[cfg(feature = "kv-indxdb")]
|
|
|
|
Inner::IndxDB(_) => write!(f, "indexdb"),
|
|
|
|
#[cfg(feature = "kv-tikv")]
|
|
|
|
Inner::TiKV(_) => write!(f, "tikv"),
|
|
|
|
#[cfg(feature = "kv-fdb")]
|
|
|
|
Inner::FDB(_) => write!(f, "fdb"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-03 20:20:36 +00:00
|
|
|
impl Datastore {
|
|
|
|
/// Creates a new datastore instance
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust,no_run
|
2022-12-30 08:23:19 +00:00
|
|
|
/// # use surrealdb::kvs::Datastore;
|
|
|
|
/// # use surrealdb::err::Error;
|
2022-05-04 09:11:51 +00:00
|
|
|
/// # #[tokio::main]
|
|
|
|
/// # async fn main() -> Result<(), Error> {
|
|
|
|
/// let ds = Datastore::new("memory").await?;
|
2022-05-03 20:20:36 +00:00
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Or to create a file-backed store:
|
|
|
|
///
|
|
|
|
/// ```rust,no_run
|
2022-12-30 08:23:19 +00:00
|
|
|
/// # use surrealdb::kvs::Datastore;
|
|
|
|
/// # use surrealdb::err::Error;
|
2022-05-04 09:11:51 +00:00
|
|
|
/// # #[tokio::main]
|
|
|
|
/// # async fn main() -> Result<(), Error> {
|
|
|
|
/// let ds = Datastore::new("file://temp.db").await?;
|
2022-05-03 20:20:36 +00:00
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Or to connect to a tikv-backed distributed store:
|
|
|
|
///
|
|
|
|
/// ```rust,no_run
|
2022-12-30 08:23:19 +00:00
|
|
|
/// # use surrealdb::kvs::Datastore;
|
|
|
|
/// # use surrealdb::err::Error;
|
2022-05-04 09:11:51 +00:00
|
|
|
/// # #[tokio::main]
|
|
|
|
/// # async fn main() -> Result<(), Error> {
|
|
|
|
/// let ds = Datastore::new("tikv://127.0.0.1:2379").await?;
|
2022-05-03 20:20:36 +00:00
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
pub async fn new(path: &str) -> Result<Datastore, Error> {
|
|
|
|
match path {
|
2022-08-28 13:35:25 +00:00
|
|
|
#[cfg(feature = "kv-mem")]
|
2022-05-03 20:20:36 +00:00
|
|
|
"memory" => {
|
2022-07-19 08:28:24 +00:00
|
|
|
info!(target: LOG, "Starting kvs store in {}", path);
|
|
|
|
let v = super::mem::Datastore::new().await.map(|v| Datastore {
|
2022-05-03 20:20:36 +00:00
|
|
|
inner: Inner::Mem(v),
|
2022-07-19 08:28:24 +00:00
|
|
|
});
|
|
|
|
info!(target: LOG, "Started kvs store in {}", path);
|
|
|
|
v
|
2022-05-03 20:20:36 +00:00
|
|
|
}
|
2022-08-28 13:35:25 +00:00
|
|
|
// Parse and initiate an File database
|
|
|
|
#[cfg(feature = "kv-rocksdb")]
|
|
|
|
s if s.starts_with("file:") => {
|
2022-07-19 08:28:24 +00:00
|
|
|
info!(target: LOG, "Starting kvs store at {}", path);
|
2022-08-28 13:35:25 +00:00
|
|
|
let s = s.trim_start_matches("file://");
|
|
|
|
let s = s.trim_start_matches("file:");
|
|
|
|
let v = super::rocksdb::Datastore::new(s).await.map(|v| Datastore {
|
|
|
|
inner: Inner::RocksDB(v),
|
2022-07-19 08:28:24 +00:00
|
|
|
});
|
|
|
|
info!(target: LOG, "Started kvs store at {}", path);
|
|
|
|
v
|
2022-05-03 20:20:36 +00:00
|
|
|
}
|
2022-08-28 13:35:25 +00:00
|
|
|
// Parse and initiate an RocksDB database
|
|
|
|
#[cfg(feature = "kv-rocksdb")]
|
|
|
|
s if s.starts_with("rocksdb:") => {
|
2022-07-19 08:28:24 +00:00
|
|
|
info!(target: LOG, "Starting kvs store at {}", path);
|
2022-08-28 13:35:25 +00:00
|
|
|
let s = s.trim_start_matches("rocksdb://");
|
|
|
|
let s = s.trim_start_matches("rocksdb:");
|
|
|
|
let v = super::rocksdb::Datastore::new(s).await.map(|v| Datastore {
|
|
|
|
inner: Inner::RocksDB(v),
|
2022-07-19 08:28:24 +00:00
|
|
|
});
|
|
|
|
info!(target: LOG, "Started kvs store at {}", path);
|
|
|
|
v
|
2022-05-03 20:20:36 +00:00
|
|
|
}
|
2022-08-28 13:35:25 +00:00
|
|
|
// Parse and initiate an IndxDB database
|
|
|
|
#[cfg(feature = "kv-indxdb")]
|
|
|
|
s if s.starts_with("indxdb:") => {
|
|
|
|
info!(target: LOG, "Starting kvs store at {}", path);
|
|
|
|
let s = s.trim_start_matches("indxdb://");
|
|
|
|
let s = s.trim_start_matches("indxdb:");
|
|
|
|
let v = super::indxdb::Datastore::new(s).await.map(|v| Datastore {
|
|
|
|
inner: Inner::IndxDB(v),
|
|
|
|
});
|
|
|
|
info!(target: LOG, "Started kvs store at {}", path);
|
|
|
|
v
|
|
|
|
}
|
|
|
|
// Parse and initiate a TiKV database
|
2022-05-03 20:20:36 +00:00
|
|
|
#[cfg(feature = "kv-tikv")]
|
|
|
|
s if s.starts_with("tikv:") => {
|
2022-07-19 08:28:24 +00:00
|
|
|
info!(target: LOG, "Connecting to kvs store at {}", path);
|
2022-05-03 20:20:36 +00:00
|
|
|
let s = s.trim_start_matches("tikv://");
|
2022-08-28 13:35:25 +00:00
|
|
|
let s = s.trim_start_matches("tikv:");
|
2022-07-19 08:28:24 +00:00
|
|
|
let v = super::tikv::Datastore::new(s).await.map(|v| Datastore {
|
2022-05-03 20:20:36 +00:00
|
|
|
inner: Inner::TiKV(v),
|
2022-07-19 08:28:24 +00:00
|
|
|
});
|
|
|
|
info!(target: LOG, "Connected to kvs store at {}", path);
|
|
|
|
v
|
2022-05-03 20:20:36 +00:00
|
|
|
}
|
2022-08-28 13:35:25 +00:00
|
|
|
// Parse and initiate a FoundationDB database
|
2022-08-15 18:35:41 +00:00
|
|
|
#[cfg(feature = "kv-fdb")]
|
|
|
|
s if s.starts_with("fdb:") => {
|
|
|
|
info!(target: LOG, "Connecting to kvs store at {}", path);
|
|
|
|
let s = s.trim_start_matches("fdb://");
|
2022-08-28 13:35:25 +00:00
|
|
|
let s = s.trim_start_matches("fdb:");
|
2022-08-15 18:35:41 +00:00
|
|
|
let v = super::fdb::Datastore::new(s).await.map(|v| Datastore {
|
|
|
|
inner: Inner::FDB(v),
|
|
|
|
});
|
|
|
|
info!(target: LOG, "Connected to kvs store at {}", path);
|
|
|
|
v
|
|
|
|
}
|
2022-05-03 20:20:36 +00:00
|
|
|
// The datastore path is not valid
|
2022-12-07 19:30:29 +00:00
|
|
|
_ => {
|
|
|
|
info!(target: LOG, "Unable to load the specified datastore {}", path);
|
|
|
|
Err(Error::Ds("Unable to load the specified datastore".into()))
|
|
|
|
}
|
2022-05-03 20:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-10 07:36:48 +00:00
|
|
|
|
2022-05-03 23:38:16 +00:00
|
|
|
/// Create a new transaction on this datastore
|
2022-08-28 12:18:12 +00:00
|
|
|
///
|
|
|
|
/// ```rust,no_run
|
2022-12-30 08:23:19 +00:00
|
|
|
/// use surrealdb::kvs::Datastore;
|
|
|
|
/// use surrealdb::err::Error;
|
2022-08-28 12:18:12 +00:00
|
|
|
///
|
|
|
|
/// #[tokio::main]
|
|
|
|
/// async fn main() -> Result<(), Error> {
|
|
|
|
/// let ds = Datastore::new("file://database.db").await?;
|
|
|
|
/// let mut tx = ds.transaction(true, false).await?;
|
|
|
|
/// tx.cancel().await?;
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
2022-05-03 20:20:36 +00:00
|
|
|
pub async fn transaction(&self, write: bool, lock: bool) -> Result<Transaction, Error> {
|
2022-12-07 19:30:29 +00:00
|
|
|
#![allow(unused_variables)]
|
2022-05-03 20:20:36 +00:00
|
|
|
match &self.inner {
|
2022-08-28 13:35:25 +00:00
|
|
|
#[cfg(feature = "kv-mem")]
|
2022-05-03 20:20:36 +00:00
|
|
|
Inner::Mem(v) => {
|
|
|
|
let tx = v.transaction(write, lock).await?;
|
|
|
|
Ok(Transaction {
|
|
|
|
inner: super::tx::Inner::Mem(tx),
|
2022-08-08 19:44:35 +00:00
|
|
|
cache: super::cache::Cache::default(),
|
2022-05-03 20:20:36 +00:00
|
|
|
})
|
|
|
|
}
|
2022-08-28 13:35:25 +00:00
|
|
|
#[cfg(feature = "kv-rocksdb")]
|
|
|
|
Inner::RocksDB(v) => {
|
2022-05-03 20:20:36 +00:00
|
|
|
let tx = v.transaction(write, lock).await?;
|
|
|
|
Ok(Transaction {
|
2022-08-28 13:35:25 +00:00
|
|
|
inner: super::tx::Inner::RocksDB(tx),
|
2022-08-08 19:44:35 +00:00
|
|
|
cache: super::cache::Cache::default(),
|
2022-05-03 20:20:36 +00:00
|
|
|
})
|
|
|
|
}
|
2022-08-28 13:35:25 +00:00
|
|
|
#[cfg(feature = "kv-indxdb")]
|
|
|
|
Inner::IndxDB(v) => {
|
2022-05-03 20:20:36 +00:00
|
|
|
let tx = v.transaction(write, lock).await?;
|
|
|
|
Ok(Transaction {
|
2022-08-28 13:35:25 +00:00
|
|
|
inner: super::tx::Inner::IndxDB(tx),
|
2022-08-08 19:44:35 +00:00
|
|
|
cache: super::cache::Cache::default(),
|
2022-05-03 20:20:36 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
#[cfg(feature = "kv-tikv")]
|
|
|
|
Inner::TiKV(v) => {
|
|
|
|
let tx = v.transaction(write, lock).await?;
|
|
|
|
Ok(Transaction {
|
|
|
|
inner: super::tx::Inner::TiKV(tx),
|
2022-08-08 19:44:35 +00:00
|
|
|
cache: super::cache::Cache::default(),
|
2022-05-03 20:20:36 +00:00
|
|
|
})
|
|
|
|
}
|
2022-08-15 18:35:41 +00:00
|
|
|
#[cfg(feature = "kv-fdb")]
|
|
|
|
Inner::FDB(v) => {
|
|
|
|
let tx = v.transaction(write, lock).await?;
|
|
|
|
Ok(Transaction {
|
|
|
|
inner: super::tx::Inner::FDB(tx),
|
|
|
|
cache: super::cache::Cache::default(),
|
|
|
|
})
|
|
|
|
}
|
2022-12-07 19:30:29 +00:00
|
|
|
#[allow(unreachable_patterns)]
|
|
|
|
_ => unreachable!(),
|
2022-05-03 20:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-10 07:36:48 +00:00
|
|
|
|
2022-05-03 23:38:16 +00:00
|
|
|
/// Parse and execute an SQL query
|
2022-08-28 12:18:12 +00:00
|
|
|
///
|
|
|
|
/// ```rust,no_run
|
2022-12-30 08:23:19 +00:00
|
|
|
/// use surrealdb::kvs::Datastore;
|
|
|
|
/// use surrealdb::err::Error;
|
|
|
|
/// use surrealdb::dbs::Session;
|
2022-08-28 12:18:12 +00:00
|
|
|
///
|
|
|
|
/// #[tokio::main]
|
|
|
|
/// async fn main() -> Result<(), Error> {
|
|
|
|
/// let ds = Datastore::new("memory").await?;
|
|
|
|
/// let ses = Session::for_kv();
|
|
|
|
/// let ast = "USE NS test DB test; SELECT * FROM person;";
|
|
|
|
/// let res = ds.execute(ast, &ses, None, false).await?;
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
2023-03-29 18:16:18 +00:00
|
|
|
#[instrument(skip_all)]
|
2022-05-03 20:20:36 +00:00
|
|
|
pub async fn execute(
|
|
|
|
&self,
|
|
|
|
txt: &str,
|
|
|
|
sess: &Session,
|
|
|
|
vars: Variables,
|
2022-07-27 13:05:28 +00:00
|
|
|
strict: bool,
|
2022-05-03 20:20:36 +00:00
|
|
|
) -> Result<Vec<Response>, Error> {
|
|
|
|
// Create a new query options
|
|
|
|
let mut opt = Options::default();
|
|
|
|
// Create a new query executor
|
|
|
|
let mut exe = Executor::new(self);
|
|
|
|
// Create a default context
|
|
|
|
let ctx = Context::default();
|
|
|
|
// Start an execution context
|
|
|
|
let ctx = sess.context(ctx);
|
|
|
|
// Store the query variables
|
2022-11-01 23:55:33 +00:00
|
|
|
let ctx = vars.attach(ctx)?;
|
2022-05-03 20:20:36 +00:00
|
|
|
// Parse the SQL query text
|
|
|
|
let ast = sql::parse(txt)?;
|
2022-06-27 16:01:39 +00:00
|
|
|
// Setup the auth options
|
2022-05-03 20:20:36 +00:00
|
|
|
opt.auth = sess.au.clone();
|
2022-06-27 16:01:39 +00:00
|
|
|
// Setup the live options
|
|
|
|
opt.live = sess.rt;
|
|
|
|
// Set current NS and DB
|
2022-05-03 20:20:36 +00:00
|
|
|
opt.ns = sess.ns();
|
|
|
|
opt.db = sess.db();
|
2022-07-27 13:05:28 +00:00
|
|
|
// Set strict config
|
|
|
|
opt.strict = strict;
|
2022-06-27 16:01:39 +00:00
|
|
|
// Process all statements
|
2022-05-03 20:20:36 +00:00
|
|
|
exe.execute(ctx, opt, ast).await
|
|
|
|
}
|
2022-05-10 07:36:48 +00:00
|
|
|
|
2022-05-03 23:38:16 +00:00
|
|
|
/// Execute a pre-parsed SQL query
|
2022-08-28 12:18:12 +00:00
|
|
|
///
|
|
|
|
/// ```rust,no_run
|
2022-12-30 08:23:19 +00:00
|
|
|
/// use surrealdb::kvs::Datastore;
|
|
|
|
/// use surrealdb::err::Error;
|
|
|
|
/// use surrealdb::dbs::Session;
|
2022-08-28 12:18:12 +00:00
|
|
|
/// use surrealdb::sql::parse;
|
|
|
|
///
|
|
|
|
/// #[tokio::main]
|
|
|
|
/// async fn main() -> Result<(), Error> {
|
|
|
|
/// let ds = Datastore::new("memory").await?;
|
|
|
|
/// let ses = Session::for_kv();
|
|
|
|
/// let ast = parse("USE NS test DB test; SELECT * FROM person;")?;
|
|
|
|
/// let res = ds.process(ast, &ses, None, false).await?;
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
2023-03-29 18:16:18 +00:00
|
|
|
#[instrument(skip_all)]
|
2022-05-03 20:20:36 +00:00
|
|
|
pub async fn process(
|
|
|
|
&self,
|
|
|
|
ast: Query,
|
|
|
|
sess: &Session,
|
|
|
|
vars: Variables,
|
2022-07-27 13:05:28 +00:00
|
|
|
strict: bool,
|
2022-05-03 20:20:36 +00:00
|
|
|
) -> Result<Vec<Response>, Error> {
|
|
|
|
// Create a new query options
|
|
|
|
let mut opt = Options::default();
|
|
|
|
// Create a new query executor
|
|
|
|
let mut exe = Executor::new(self);
|
|
|
|
// Create a default context
|
|
|
|
let ctx = Context::default();
|
|
|
|
// Start an execution context
|
|
|
|
let ctx = sess.context(ctx);
|
|
|
|
// Store the query variables
|
2022-11-01 23:55:33 +00:00
|
|
|
let ctx = vars.attach(ctx)?;
|
2022-06-27 16:01:39 +00:00
|
|
|
// Setup the auth options
|
2022-05-03 20:20:36 +00:00
|
|
|
opt.auth = sess.au.clone();
|
2022-06-27 16:01:39 +00:00
|
|
|
// Setup the live options
|
|
|
|
opt.live = sess.rt;
|
|
|
|
// Set current NS and DB
|
2022-05-03 20:20:36 +00:00
|
|
|
opt.ns = sess.ns();
|
|
|
|
opt.db = sess.db();
|
2022-07-27 13:05:28 +00:00
|
|
|
// Set strict config
|
|
|
|
opt.strict = strict;
|
2022-06-27 16:01:39 +00:00
|
|
|
// Process all statements
|
2022-05-03 20:20:36 +00:00
|
|
|
exe.execute(ctx, opt, ast).await
|
|
|
|
}
|
2022-05-10 07:36:48 +00:00
|
|
|
|
2022-08-28 12:18:12 +00:00
|
|
|
/// Ensure a SQL [`Value`] is fully computed
|
|
|
|
///
|
|
|
|
/// ```rust,no_run
|
2022-12-30 08:23:19 +00:00
|
|
|
/// use surrealdb::kvs::Datastore;
|
|
|
|
/// use surrealdb::err::Error;
|
|
|
|
/// use surrealdb::dbs::Session;
|
2022-10-31 23:12:41 +00:00
|
|
|
/// use surrealdb::sql::Future;
|
2022-08-28 12:18:12 +00:00
|
|
|
/// use surrealdb::sql::Value;
|
|
|
|
///
|
|
|
|
/// #[tokio::main]
|
|
|
|
/// async fn main() -> Result<(), Error> {
|
|
|
|
/// let ds = Datastore::new("memory").await?;
|
|
|
|
/// let ses = Session::for_kv();
|
2023-02-21 14:15:19 +00:00
|
|
|
/// let val = Value::Future(Box::new(Future::from(Value::True)));
|
2022-08-28 12:18:12 +00:00
|
|
|
/// let res = ds.compute(val, &ses, None, false).await?;
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
2023-03-29 18:16:18 +00:00
|
|
|
#[instrument(skip_all)]
|
2022-06-20 11:26:27 +00:00
|
|
|
pub async fn compute(
|
|
|
|
&self,
|
|
|
|
val: Value,
|
|
|
|
sess: &Session,
|
|
|
|
vars: Variables,
|
2022-07-27 13:05:28 +00:00
|
|
|
strict: bool,
|
2022-06-20 11:26:27 +00:00
|
|
|
) -> Result<Value, Error> {
|
|
|
|
// Start a new transaction
|
|
|
|
let txn = self.transaction(val.writeable(), false).await?;
|
|
|
|
//
|
|
|
|
let txn = Arc::new(Mutex::new(txn));
|
|
|
|
// Create a new query options
|
|
|
|
let mut opt = Options::default();
|
|
|
|
// Create a default context
|
|
|
|
let ctx = Context::default();
|
|
|
|
// Start an execution context
|
|
|
|
let ctx = sess.context(ctx);
|
|
|
|
// Store the query variables
|
2022-11-01 23:55:33 +00:00
|
|
|
let ctx = vars.attach(ctx)?;
|
2022-06-27 16:01:39 +00:00
|
|
|
// Setup the auth options
|
2022-06-20 11:26:27 +00:00
|
|
|
opt.auth = sess.au.clone();
|
2022-06-27 16:01:39 +00:00
|
|
|
// Set current NS and DB
|
2022-06-20 11:26:27 +00:00
|
|
|
opt.ns = sess.ns();
|
|
|
|
opt.db = sess.db();
|
2022-07-27 13:05:28 +00:00
|
|
|
// Set strict config
|
|
|
|
opt.strict = strict;
|
2022-06-20 11:26:27 +00:00
|
|
|
// Compute the value
|
|
|
|
let res = val.compute(&ctx, &opt, &txn, None).await?;
|
|
|
|
// Store any data
|
|
|
|
match val.writeable() {
|
|
|
|
true => txn.lock().await.commit().await?,
|
|
|
|
false => txn.lock().await.cancel().await?,
|
|
|
|
};
|
|
|
|
// Return result
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2022-05-10 07:36:48 +00:00
|
|
|
/// Performs a full database export as SQL
|
2023-03-29 18:16:18 +00:00
|
|
|
#[instrument(skip(self, chn))]
|
2022-05-15 19:38:46 +00:00
|
|
|
pub async fn export(&self, ns: String, db: String, chn: Sender<Vec<u8>>) -> Result<(), Error> {
|
2022-05-10 07:36:48 +00:00
|
|
|
// Start a new transaction
|
|
|
|
let mut txn = self.transaction(false, false).await?;
|
2022-05-15 19:38:46 +00:00
|
|
|
// Process the export
|
|
|
|
txn.export(&ns, &db, chn).await?;
|
2022-08-21 12:13:38 +00:00
|
|
|
// Everything ok
|
2022-05-15 19:38:46 +00:00
|
|
|
Ok(())
|
2022-05-10 07:36:48 +00:00
|
|
|
}
|
2022-05-03 20:20:36 +00:00
|
|
|
}
|