Skip to content

Commit

Permalink
QueryPlanner / indexing / Context / Cursor doc (surrealdb#2229)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuel-keller authored Jul 6, 2023
1 parent 4b690c7 commit e9eeb9a
Show file tree
Hide file tree
Showing 97 changed files with 1,617 additions and 1,810 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ targets = []
addr = { version = "0.15.6", default-features = false, features = ["std"] }
argon2 = "0.5.0"
ascii = { version = "0.3.2", package = "any_ascii" }
async-trait = "0.1.69"
async-recursion = "1.0.4"
base64_lib = { version = "0.21.2", package = "base64" }
bcrypt = "0.14.0"
Expand Down
62 changes: 0 additions & 62 deletions lib/src/ctx/context.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use crate::ctx::canceller::Canceller;
use crate::ctx::reason::Reason;
use crate::dbs::Notification;
use crate::dbs::Transaction;
use crate::err::Error;
use crate::idx::ft::docids::DocId;
use crate::idx::planner::executor::QueryExecutor;
use crate::sql::value::Value;
use crate::sql::Thing;
use channel::Sender;
use std::borrow::Cow;
use std::collections::HashMap;
Expand Down Expand Up @@ -36,18 +32,10 @@ pub struct Context<'a> {
cancelled: Arc<AtomicBool>,
// A collection of read only values stored in this context.
values: HashMap<Cow<'static, str>, Cow<'a, Value>>,
// Stores the current transaction if available
transaction: Option<Transaction>,
// Stores the notification channel if available
notifications: Option<Sender<Notification>>,
// An optional query executor
query_executors: Option<Arc<HashMap<String, QueryExecutor>>>,
// An optional record id
thing: Option<&'a Thing>,
// An optional doc id
doc_id: Option<DocId>,
// An optional cursor document
cursor_doc: Option<&'a Value>,
}

impl<'a> Default for Context<'a> {
Expand All @@ -63,8 +51,6 @@ impl<'a> Debug for Context<'a> {
.field("deadline", &self.deadline)
.field("cancelled", &self.cancelled)
.field("values", &self.values)
.field("thing", &self.thing)
.field("doc", &self.cursor_doc)
.finish()
}
}
Expand All @@ -77,12 +63,8 @@ impl<'a> Context<'a> {
parent: None,
deadline: None,
cancelled: Arc::new(AtomicBool::new(false)),
transaction: None,
notifications: None,
query_executors: None,
thing: None,
doc_id: None,
cursor_doc: None,
}
}

Expand All @@ -93,12 +75,8 @@ impl<'a> Context<'a> {
parent: Some(parent),
deadline: parent.deadline,
cancelled: Arc::new(AtomicBool::new(false)),
transaction: parent.transaction.clone(),
notifications: parent.notifications.clone(),
query_executors: parent.query_executors.clone(),
thing: parent.thing,
doc_id: parent.doc_id,
cursor_doc: parent.cursor_doc,
}
}

Expand Down Expand Up @@ -134,34 +112,12 @@ impl<'a> Context<'a> {
self.add_deadline(Instant::now() + timeout)
}

/// Add the current transaction to the context, so that it can be fetched
/// where necessary, including inside the query planner.
pub fn add_transaction(&mut self, txn: Option<&Transaction>) {
self.transaction = txn.cloned()
}

/// Add the LIVE query notification channel to the context, so that we
/// can send notifications to any subscribers.
pub fn add_notifications(&mut self, chn: Option<&Sender<Notification>>) {
self.notifications = chn.cloned()
}

/// Add a cursor document to this context.
/// Usage: A new child context is created by an iterator for each document.
/// The iterator sets the value of the current document (known as cursor document).
/// The cursor document is copied do the child contexts.
pub fn add_cursor_doc(&mut self, doc: &'a Value) {
self.cursor_doc = Some(doc);
}

pub fn add_thing(&mut self, thing: &'a Thing) {
self.thing = Some(thing);
}

pub fn add_doc_id(&mut self, doc_id: DocId) {
self.doc_id = Some(doc_id);
}

/// Set the query executors
pub(crate) fn set_query_executors(&mut self, executors: HashMap<String, QueryExecutor>) {
self.query_executors = Some(Arc::new(executors));
Expand All @@ -173,28 +129,10 @@ impl<'a> Context<'a> {
self.deadline.map(|v| v.saturating_duration_since(Instant::now()))
}

/// Returns a transaction if any.
/// Otherwise it fails by returning a Error::NoTx error.
pub fn try_clone_transaction(&self) -> Result<Transaction, Error> {
self.transaction.clone().ok_or(Error::Unreachable)
}

pub fn notifications(&self) -> Option<Sender<Notification>> {
self.notifications.clone()
}

pub fn thing(&self) -> Option<&Thing> {
self.thing
}

pub fn doc_id(&self) -> Option<DocId> {
self.doc_id
}

pub fn doc(&self) -> Option<&Value> {
self.cursor_doc
}

pub(crate) fn get_query_executor(&self, tb: &str) -> Option<&QueryExecutor> {
if let Some(qe) = &self.query_executors {
qe.get(tb)
Expand Down
Loading

0 comments on commit e9eeb9a

Please sign in to comment.