Miscellaneous performance optimizations and code cleanup (#240)

This commit is contained in:
Finn Bear 2022-09-22 16:54:53 -07:00 committed by GitHub
parent 58cffa2de6
commit b31dbb28fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 278 additions and 381 deletions

View file

@ -118,14 +118,14 @@ impl<'a> Executor<'a> {
let mut session = ctx.value("session").unwrap_or(&Value::None).clone(); let mut session = ctx.value("session").unwrap_or(&Value::None).clone();
session.put(NS.as_ref(), ns.to_owned().into()); session.put(NS.as_ref(), ns.to_owned().into());
ctx.add_value(String::from("session"), session); ctx.add_value(String::from("session"), session);
opt.ns = Some(Arc::new(ns.to_owned())); opt.ns = Some(ns.into());
} }
async fn set_db(&self, ctx: &mut Context<'_>, opt: &mut Options, db: &str) { async fn set_db(&self, ctx: &mut Context<'_>, opt: &mut Options, db: &str) {
let mut session = ctx.value("session").unwrap_or(&Value::None).clone(); let mut session = ctx.value("session").unwrap_or(&Value::None).clone();
session.put(DB.as_ref(), db.to_owned().into()); session.put(DB.as_ref(), db.to_owned().into());
ctx.add_value(String::from("session"), session); ctx.add_value(String::from("session"), session);
opt.db = Some(Arc::new(db.to_owned())); opt.db = Some(db.into());
} }
pub async fn execute( pub async fn execute(

View file

@ -14,9 +14,9 @@ use std::sync::Arc;
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub struct Options { pub struct Options {
// Currently selected NS // Currently selected NS
pub ns: Option<Arc<String>>, pub ns: Option<Arc<str>>,
// Currently selected DB // Currently selected DB
pub db: Option<Arc<String>>, pub db: Option<Arc<str>>,
// Connection authentication data // Connection authentication data
pub auth: Arc<Auth>, pub auth: Arc<Auth>,
// How many subqueries have we gone into? // How many subqueries have we gone into?

View file

@ -83,12 +83,12 @@ impl Session {
self self
} }
/// Retrieves the selected namespace /// Retrieves the selected namespace
pub(crate) fn ns(&self) -> Option<Arc<String>> { pub(crate) fn ns(&self) -> Option<Arc<str>> {
self.ns.to_owned().map(Arc::new) self.ns.as_deref().map(Into::into)
} }
/// Retrieves the selected database /// Retrieves the selected database
pub(crate) fn db(&self) -> Option<Arc<String>> { pub(crate) fn db(&self) -> Option<Arc<str>> {
self.db.to_owned().map(Arc::new) self.db.as_deref().map(Into::into)
} }
/// Convert a session into a runtime /// Convert a session into a runtime
pub(crate) fn context<'a>(&self, mut ctx: Context<'a>) -> Context<'a> { pub(crate) fn context<'a>(&self, mut ctx: Context<'a>) -> Context<'a> {

View file

@ -83,7 +83,7 @@ impl<'a> Document<'a> {
&self, &self,
opt: &Options, opt: &Options,
txn: &Transaction, txn: &Transaction,
) -> Result<Arc<Vec<DefineTableStatement>>, Error> { ) -> Result<Arc<[DefineTableStatement]>, Error> {
// Get the record id // Get the record id
let id = self.id.as_ref().unwrap(); let id = self.id.as_ref().unwrap();
// Get the table definitions // Get the table definitions
@ -94,7 +94,7 @@ impl<'a> Document<'a> {
&self, &self,
opt: &Options, opt: &Options,
txn: &Transaction, txn: &Transaction,
) -> Result<Arc<Vec<DefineEventStatement>>, Error> { ) -> Result<Arc<[DefineEventStatement]>, Error> {
// Get the record id // Get the record id
let id = self.id.as_ref().unwrap(); let id = self.id.as_ref().unwrap();
// Get the event definitions // Get the event definitions
@ -105,7 +105,7 @@ impl<'a> Document<'a> {
&self, &self,
opt: &Options, opt: &Options,
txn: &Transaction, txn: &Transaction,
) -> Result<Arc<Vec<DefineFieldStatement>>, Error> { ) -> Result<Arc<[DefineFieldStatement]>, Error> {
// Get the record id // Get the record id
let id = self.id.as_ref().unwrap(); let id = self.id.as_ref().unwrap();
// Get the field definitions // Get the field definitions
@ -116,7 +116,7 @@ impl<'a> Document<'a> {
&self, &self,
opt: &Options, opt: &Options,
txn: &Transaction, txn: &Transaction,
) -> Result<Arc<Vec<DefineIndexStatement>>, Error> { ) -> Result<Arc<[DefineIndexStatement]>, Error> {
// Get the record id // Get the record id
let id = self.id.as_ref().unwrap(); let id = self.id.as_ref().unwrap();
// Get the index definitions // Get the index definitions

View file

@ -7,34 +7,25 @@ use crate::sql::array::Union;
use crate::sql::array::Uniq; use crate::sql::array::Uniq;
use crate::sql::value::Value; use crate::sql::value::Value;
pub fn concat((left, right): (Value, Value)) -> Result<Value, Error> { pub fn concat(arrays: (Value, Value)) -> Result<Value, Error> {
match left { Ok(match arrays {
Value::Array(v) => match right { (Value::Array(v), Value::Array(w)) => v.concat(w).into(),
Value::Array(w) => Ok(v.concat(w).into()), _ => Value::None,
_ => Ok(Value::None), })
},
_ => Ok(Value::None),
}
} }
pub fn combine((left, right): (Value, Value)) -> Result<Value, Error> { pub fn combine(arrays: (Value, Value)) -> Result<Value, Error> {
match left { Ok(match arrays {
Value::Array(v) => match right { (Value::Array(v), Value::Array(w)) => v.combine(w).into(),
Value::Array(w) => Ok(v.combine(w).into()), _ => Value::None,
_ => Ok(Value::None), })
},
_ => Ok(Value::None),
}
} }
pub fn difference((left, right): (Value, Value)) -> Result<Value, Error> { pub fn difference(arrays: (Value, Value)) -> Result<Value, Error> {
match left { Ok(match arrays {
Value::Array(v) => match right { (Value::Array(v), Value::Array(w)) => v.difference(w).into(),
Value::Array(w) => Ok(v.difference(w).into()), _ => Value::None,
_ => Ok(Value::None), })
},
_ => Ok(Value::None),
}
} }
pub fn distinct((arg,): (Value,)) -> Result<Value, Error> { pub fn distinct((arg,): (Value,)) -> Result<Value, Error> {
@ -44,14 +35,11 @@ pub fn distinct((arg,): (Value,)) -> Result<Value, Error> {
} }
} }
pub fn intersect((left, right): (Value, Value)) -> Result<Value, Error> { pub fn intersect(arrays: (Value, Value)) -> Result<Value, Error> {
match left { Ok(match arrays {
Value::Array(v) => match right { (Value::Array(v), Value::Array(w)) => v.intersect(w).into(),
Value::Array(w) => Ok(v.intersect(w).into()), _ => Value::None,
_ => Ok(Value::None), })
},
_ => Ok(Value::None),
}
} }
pub fn len((arg,): (Value,)) -> Result<Value, Error> { pub fn len((arg,): (Value,)) -> Result<Value, Error> {
@ -94,14 +82,11 @@ pub fn sort((array, order): (Value, Option<Value>)) -> Result<Value, Error> {
} }
} }
pub fn union((left, right): (Value, Value)) -> Result<Value, Error> { pub fn union(arrays: (Value, Value)) -> Result<Value, Error> {
match left { Ok(match arrays {
Value::Array(v) => match right { (Value::Array(v), Value::Array(w)) => v.union(w).into(),
Value::Array(w) => Ok(v.union(w).into()), _ => Value::None,
_ => Ok(Value::None), })
},
_ => Ok(Value::None),
}
} }
pub mod sort { pub mod sort {

View file

@ -22,50 +22,50 @@ pub fn bool(val: Value) -> Result<Value, Error> {
} }
pub fn int(val: Value) -> Result<Value, Error> { pub fn int(val: Value) -> Result<Value, Error> {
match val { Ok(match val {
Value::Number(Number::Int(_)) => Ok(val), Value::Number(Number::Int(_)) => val,
_ => Ok(Value::Number(Number::Int(val.as_int()))), _ => Value::Number(Number::Int(val.as_int())),
} })
} }
pub fn float(val: Value) -> Result<Value, Error> { pub fn float(val: Value) -> Result<Value, Error> {
match val { Ok(match val {
Value::Number(Number::Float(_)) => Ok(val), Value::Number(Number::Float(_)) => val,
_ => Ok(Value::Number(Number::Float(val.as_float()))), _ => Value::Number(Number::Float(val.as_float())),
} })
} }
pub fn number(val: Value) -> Result<Value, Error> { pub fn number(val: Value) -> Result<Value, Error> {
match val { Ok(match val {
Value::Number(Number::Decimal(_)) => Ok(val), Value::Number(Number::Decimal(_)) => val,
_ => Ok(Value::Number(Number::Decimal(val.as_decimal()))), _ => Value::Number(Number::Decimal(val.as_decimal())),
} })
} }
pub fn decimal(val: Value) -> Result<Value, Error> { pub fn decimal(val: Value) -> Result<Value, Error> {
match val { Ok(match val {
Value::Number(Number::Decimal(_)) => Ok(val), Value::Number(Number::Decimal(_)) => val,
_ => Ok(Value::Number(Number::Decimal(val.as_decimal()))), _ => Value::Number(Number::Decimal(val.as_decimal())),
} })
} }
pub fn string(val: Value) -> Result<Value, Error> { pub fn string(val: Value) -> Result<Value, Error> {
match val { Ok(match val {
Value::Strand(_) => Ok(val), Value::Strand(_) => val,
_ => Ok(Value::Strand(val.as_strand())), _ => Value::Strand(val.as_strand()),
} })
} }
pub fn datetime(val: Value) -> Result<Value, Error> { pub fn datetime(val: Value) -> Result<Value, Error> {
match val { Ok(match val {
Value::Datetime(_) => Ok(val), Value::Datetime(_) => val,
_ => Ok(Value::Datetime(val.as_datetime())), _ => Value::Datetime(val.as_datetime()),
} })
} }
pub fn duration(val: Value) -> Result<Value, Error> { pub fn duration(val: Value) -> Result<Value, Error> {
match val { Ok(match val {
Value::Duration(_) => Ok(val), Value::Duration(_) => val,
_ => Ok(Value::Duration(val.as_duration())), _ => Value::Duration(val.as_duration()),
} })
} }

View file

@ -23,59 +23,38 @@ pub fn area((arg,): (Value,)) -> Result<Value, Error> {
} }
} }
pub fn bearing((a, b): (Value, Value)) -> Result<Value, Error> { pub fn bearing(points: (Value, Value)) -> Result<Value, Error> {
match a { Ok(match points {
Value::Geometry(Geometry::Point(v)) => match b { (Value::Geometry(Geometry::Point(v)), Value::Geometry(Geometry::Point(w))) => {
Value::Geometry(Geometry::Point(w)) => Ok(v.bearing(w).into()), v.bearing(w).into()
_ => Ok(Value::None), }
}, _ => Value::None,
_ => Ok(Value::None), })
}
} }
pub fn centroid((arg,): (Value,)) -> Result<Value, Error> { pub fn centroid((arg,): (Value,)) -> Result<Value, Error> {
match arg { let centroid = match arg {
Value::Geometry(v) => match v { Value::Geometry(v) => match v {
Geometry::Point(v) => Ok(v.centroid().into()), Geometry::Point(v) => Some(v.centroid()),
Geometry::Line(v) => match v.centroid() { Geometry::Line(v) => v.centroid(),
Some(x) => Ok(x.into()), Geometry::Polygon(v) => v.centroid(),
None => Ok(Value::None), Geometry::MultiPoint(v) => v.centroid(),
}, Geometry::MultiLine(v) => v.centroid(),
Geometry::Polygon(v) => match v.centroid() { Geometry::MultiPolygon(v) => v.centroid(),
Some(x) => Ok(x.into()), Geometry::Collection(v) => v.into_iter().collect::<geo::Geometry<f64>>().centroid(),
None => Ok(Value::None),
},
Geometry::MultiPoint(v) => match v.centroid() {
Some(x) => Ok(x.into()),
None => Ok(Value::None),
},
Geometry::MultiLine(v) => match v.centroid() {
Some(x) => Ok(x.into()),
None => Ok(Value::None),
},
Geometry::MultiPolygon(v) => match v.centroid() {
Some(x) => Ok(x.into()),
None => Ok(Value::None),
},
Geometry::Collection(v) => {
match v.into_iter().collect::<geo::Geometry<f64>>().centroid() {
Some(x) => Ok(x.into()),
None => Ok(Value::None),
}
}
}, },
_ => Ok(Value::None), _ => None,
} };
Ok(centroid.map(Into::into).unwrap_or(Value::None))
} }
pub fn distance((from, to): (Value, Value)) -> Result<Value, Error> { pub fn distance(points: (Value, Value)) -> Result<Value, Error> {
match from { Ok(match points {
Value::Geometry(Geometry::Point(v)) => match to { (Value::Geometry(Geometry::Point(v)), Value::Geometry(Geometry::Point(w))) => {
Value::Geometry(Geometry::Point(w)) => Ok(v.haversine_distance(&w).into()), v.haversine_distance(&w).into()
_ => Ok(Value::None), }
}, _ => Value::None,
_ => Ok(Value::None), })
}
} }
pub mod hash { pub mod hash {

View file

@ -1,6 +1,6 @@
use crate::err::Error; use crate::err::Error;
use crate::sql::value::Value; use crate::sql::value::Value;
use crate::sql::Strand; use crate::sql::{Object, Strand};
#[cfg(not(feature = "http"))] #[cfg(not(feature = "http"))]
pub async fn head((_, _): (Value, Option<Value>)) -> Result<Value, Error> { pub async fn head((_, _): (Value, Option<Value>)) -> Result<Value, Error> {
@ -43,57 +43,39 @@ fn try_as_uri(fn_name: &str, value: Value) -> Result<Strand, Error> {
} }
} }
fn try_as_opts(
fn_name: &str,
error_message: &str,
value: Option<Value>,
) -> Result<Option<Object>, Error> {
match value {
Some(Value::Object(opts)) => Ok(Some(opts)),
None => Ok(None),
Some(_) => Err(Error::InvalidArguments {
name: fn_name.to_owned(),
message: error_message.to_owned(),
}),
}
}
#[cfg(feature = "http")] #[cfg(feature = "http")]
pub async fn head((uri, opts): (Value, Option<Value>)) -> Result<Value, Error> { pub async fn head((uri, opts): (Value, Option<Value>)) -> Result<Value, Error> {
let uri = try_as_uri("http::head", uri)?; let uri = try_as_uri("http::head", uri)?;
let opts = try_as_opts("http::head", "The second argument should be an object.", opts)?;
let opts = match opts {
Some(Value::Object(opts)) => Some(opts),
None => None,
Some(_) => {
return Err(Error::InvalidArguments {
name: String::from("http::head"),
message: String::from("The second argument should be an object."),
})
}
};
crate::fnc::util::http::head(uri, opts).await crate::fnc::util::http::head(uri, opts).await
} }
#[cfg(feature = "http")] #[cfg(feature = "http")]
pub async fn get((uri, opts): (Value, Option<Value>)) -> Result<Value, Error> { pub async fn get((uri, opts): (Value, Option<Value>)) -> Result<Value, Error> {
let uri = try_as_uri("http::get", uri)?; let uri = try_as_uri("http::get", uri)?;
let opts = try_as_opts("http::get", "The second argument should be an object.", opts)?;
let opts = match opts {
Some(Value::Object(opts)) => Some(opts),
None => None,
Some(_) => {
return Err(Error::InvalidArguments {
name: String::from("http::get"),
message: String::from("The second argument should be an object."),
})
}
};
crate::fnc::util::http::get(uri, opts).await crate::fnc::util::http::get(uri, opts).await
} }
#[cfg(feature = "http")] #[cfg(feature = "http")]
pub async fn put((uri, body, opts): (Value, Option<Value>, Option<Value>)) -> Result<Value, Error> { pub async fn put((uri, body, opts): (Value, Option<Value>, Option<Value>)) -> Result<Value, Error> {
let uri = try_as_uri("http::put", uri)?; let uri = try_as_uri("http::put", uri)?;
let opts = try_as_opts("http::put", "The third argument should be an object.", opts)?;
let opts = match opts {
Some(Value::Object(opts)) => Some(opts),
None => None,
Some(_) => {
return Err(Error::InvalidArguments {
name: String::from("http::put"),
message: String::from("The third argument should be an object."),
})
}
};
crate::fnc::util::http::put(uri, body.unwrap_or(Value::Null), opts).await crate::fnc::util::http::put(uri, body.unwrap_or(Value::Null), opts).await
} }
@ -102,18 +84,7 @@ pub async fn post(
(uri, body, opts): (Value, Option<Value>, Option<Value>), (uri, body, opts): (Value, Option<Value>, Option<Value>),
) -> Result<Value, Error> { ) -> Result<Value, Error> {
let uri = try_as_uri("http::post", uri)?; let uri = try_as_uri("http::post", uri)?;
let opts = try_as_opts("http::post", "The third argument should be an object.", opts)?;
let opts = match opts {
Some(Value::Object(opts)) => Some(opts),
None => None,
Some(_) => {
return Err(Error::InvalidArguments {
name: String::from("http::post"),
message: String::from("The third argument should be an object."),
})
}
};
crate::fnc::util::http::post(uri, body.unwrap_or(Value::Null), opts).await crate::fnc::util::http::post(uri, body.unwrap_or(Value::Null), opts).await
} }
@ -122,35 +93,13 @@ pub async fn patch(
(uri, body, opts): (Value, Option<Value>, Option<Value>), (uri, body, opts): (Value, Option<Value>, Option<Value>),
) -> Result<Value, Error> { ) -> Result<Value, Error> {
let uri = try_as_uri("http::patch", uri)?; let uri = try_as_uri("http::patch", uri)?;
let opts = try_as_opts("http::patch", "The third argument should be an object.", opts)?;
let opts = match opts {
Some(Value::Object(opts)) => Some(opts),
None => None,
Some(_) => {
return Err(Error::InvalidArguments {
name: String::from("http::patch"),
message: String::from("The third argument should be an object."),
})
}
};
crate::fnc::util::http::patch(uri, body.unwrap_or(Value::Null), opts).await crate::fnc::util::http::patch(uri, body.unwrap_or(Value::Null), opts).await
} }
#[cfg(feature = "http")] #[cfg(feature = "http")]
pub async fn delete((uri, opts): (Value, Option<Value>)) -> Result<Value, Error> { pub async fn delete((uri, opts): (Value, Option<Value>)) -> Result<Value, Error> {
let uri = try_as_uri("http::delete", uri)?; let uri = try_as_uri("http::delete", uri)?;
let opts = try_as_opts("http::delete", "The second argument should be an object.", opts)?;
let opts = match opts {
Some(Value::Object(opts)) => Some(opts),
None => None,
Some(_) => {
return Err(Error::InvalidArguments {
name: String::from("http::delete"),
message: String::from("The second argument should be an object."),
})
}
};
crate::fnc::util::http::delete(uri, opts).await crate::fnc::util::http::delete(uri, opts).await
} }

View file

@ -62,10 +62,11 @@ pub fn semver((arg,): (String,)) -> Result<Value, Error> {
#[inline] #[inline]
pub fn uuid((arg,): (Value,)) -> Result<Value, Error> { pub fn uuid((arg,): (Value,)) -> Result<Value, Error> {
Ok(match arg { Ok(match arg {
Value::Strand(v) => Uuid::parse_str(v.as_string().as_str()).is_ok().into(), Value::Strand(v) => Uuid::parse_str(v.as_string().as_str()).is_ok(),
Value::Uuid(_) => true.into(), Value::Uuid(_) => true,
_ => false.into(), _ => false,
}) }
.into())
} }
#[cfg(test)] #[cfg(test)]

View file

@ -20,10 +20,10 @@ pub fn abs((arg,): (Number,)) -> Result<Value, Error> {
} }
pub fn bottom((array, c): (Value, i64)) -> Result<Value, Error> { pub fn bottom((array, c): (Value, i64)) -> Result<Value, Error> {
match array { Ok(match array {
Value::Array(v) => Ok(v.as_numbers().bottom(c).into()), Value::Array(v) => v.as_numbers().bottom(c).into(),
_ => Ok(Value::None), _ => Value::None,
} })
} }
pub fn ceil((arg,): (Number,)) -> Result<Value, Error> { pub fn ceil((arg,): (Number,)) -> Result<Value, Error> {
@ -46,85 +46,85 @@ pub fn floor((arg,): (Number,)) -> Result<Value, Error> {
} }
pub fn interquartile((array,): (Value,)) -> Result<Value, Error> { pub fn interquartile((array,): (Value,)) -> Result<Value, Error> {
match array { Ok(match array {
Value::Array(v) => Ok(v.as_numbers().interquartile().into()), Value::Array(v) => v.as_numbers().interquartile().into(),
_ => Ok(Value::None), _ => Value::None,
} })
} }
pub fn max((array,): (Value,)) -> Result<Value, Error> { pub fn max((array,): (Value,)) -> Result<Value, Error> {
match array { Ok(match array {
Value::Array(v) => match v.as_numbers().into_iter().max() { Value::Array(v) => match v.as_numbers().into_iter().max() {
Some(v) => Ok(v.into()), Some(v) => v.into(),
None => Ok(Value::None), None => Value::None,
}, },
v => Ok(v), v => v,
} })
} }
pub fn mean((array,): (Value,)) -> Result<Value, Error> { pub fn mean((array,): (Value,)) -> Result<Value, Error> {
match array { Ok(match array {
Value::Array(v) => match v.is_empty() { Value::Array(v) => match v.is_empty() {
true => Ok(Value::None), true => Value::None,
false => Ok(v.as_numbers().mean().into()), false => v.as_numbers().mean().into(),
}, },
_ => Ok(Value::None), _ => Value::None,
} })
} }
pub fn median((array,): (Value,)) -> Result<Value, Error> { pub fn median((array,): (Value,)) -> Result<Value, Error> {
match array { Ok(match array {
Value::Array(v) => match v.is_empty() { Value::Array(v) => match v.is_empty() {
true => Ok(Value::None), true => Value::None,
false => Ok(v.as_numbers().median().into()), false => v.as_numbers().median().into(),
}, },
_ => Ok(Value::None), _ => Value::None,
} })
} }
pub fn midhinge((array,): (Value,)) -> Result<Value, Error> { pub fn midhinge((array,): (Value,)) -> Result<Value, Error> {
match array { Ok(match array {
Value::Array(v) => Ok(v.as_numbers().midhinge().into()), Value::Array(v) => v.as_numbers().midhinge().into(),
_ => Ok(Value::None), _ => Value::None,
} })
} }
pub fn min((array,): (Value,)) -> Result<Value, Error> { pub fn min((array,): (Value,)) -> Result<Value, Error> {
match array { Ok(match array {
Value::Array(v) => match v.as_numbers().into_iter().min() { Value::Array(v) => match v.as_numbers().into_iter().min() {
Some(v) => Ok(v.into()), Some(v) => v.into(),
None => Ok(Value::None), None => Value::None,
}, },
v => Ok(v), v => v,
} })
} }
pub fn mode((array,): (Value,)) -> Result<Value, Error> { pub fn mode((array,): (Value,)) -> Result<Value, Error> {
match array { Ok(match array {
Value::Array(v) => Ok(v.as_numbers().mode().into()), Value::Array(v) => v.as_numbers().mode().into(),
_ => Ok(Value::None), _ => Value::None,
} })
} }
pub fn nearestrank((array, n): (Value, Number)) -> Result<Value, Error> { pub fn nearestrank((array, n): (Value, Number)) -> Result<Value, Error> {
match array { Ok(match array {
Value::Array(v) => Ok(v.as_numbers().nearestrank(n).into()), Value::Array(v) => v.as_numbers().nearestrank(n).into(),
_ => Ok(Value::None), _ => Value::None,
} })
} }
pub fn percentile((array, n): (Value, Number)) -> Result<Value, Error> { pub fn percentile((array, n): (Value, Number)) -> Result<Value, Error> {
match array { Ok(match array {
Value::Array(v) => Ok(v.as_numbers().percentile(n).into()), Value::Array(v) => v.as_numbers().percentile(n).into(),
_ => Ok(Value::None), _ => Value::None,
} })
} }
pub fn product((array,): (Value,)) -> Result<Value, Error> { pub fn product((array,): (Value,)) -> Result<Value, Error> {
match array { Ok(match array {
Value::Array(v) => Ok(v.as_numbers().into_iter().product::<Number>().into()), Value::Array(v) => v.as_numbers().into_iter().product::<Number>().into(),
_ => Ok(Value::None), _ => Value::None,
} })
} }
pub fn round((arg,): (Number,)) -> Result<Value, Error> { pub fn round((arg,): (Number,)) -> Result<Value, Error> {
@ -132,10 +132,10 @@ pub fn round((arg,): (Number,)) -> Result<Value, Error> {
} }
pub fn spread((array,): (Value,)) -> Result<Value, Error> { pub fn spread((array,): (Value,)) -> Result<Value, Error> {
match array { Ok(match array {
Value::Array(v) => Ok(v.as_numbers().spread().into()), Value::Array(v) => v.as_numbers().spread().into(),
_ => Ok(Value::None), _ => Value::None,
} })
} }
pub fn sqrt((arg,): (Number,)) -> Result<Value, Error> { pub fn sqrt((arg,): (Number,)) -> Result<Value, Error> {
@ -146,36 +146,36 @@ pub fn sqrt((arg,): (Number,)) -> Result<Value, Error> {
} }
pub fn stddev((array,): (Value,)) -> Result<Value, Error> { pub fn stddev((array,): (Value,)) -> Result<Value, Error> {
match array { Ok(match array {
Value::Array(v) => Ok(v.as_numbers().deviation().into()), Value::Array(v) => v.as_numbers().deviation().into(),
_ => Ok(Value::None), _ => Value::None,
} })
} }
pub fn sum((array,): (Value,)) -> Result<Value, Error> { pub fn sum((array,): (Value,)) -> Result<Value, Error> {
match array { Ok(match array {
Value::Array(v) => Ok(v.as_numbers().into_iter().sum::<Number>().into()), Value::Array(v) => v.as_numbers().into_iter().sum::<Number>().into(),
v => Ok(v.as_number().into()), v => v.as_number().into(),
} })
} }
pub fn top((array, c): (Value, i64)) -> Result<Value, Error> { pub fn top((array, c): (Value, i64)) -> Result<Value, Error> {
match array { Ok(match array {
Value::Array(v) => Ok(v.as_numbers().top(c).into()), Value::Array(v) => v.as_numbers().top(c).into(),
_ => Ok(Value::None), _ => Value::None,
} })
} }
pub fn trimean((array,): (Value,)) -> Result<Value, Error> { pub fn trimean((array,): (Value,)) -> Result<Value, Error> {
match array { Ok(match array {
Value::Array(v) => Ok(v.as_numbers().trimean().into()), Value::Array(v) => v.as_numbers().trimean().into(),
_ => Ok(Value::None), _ => Value::None,
} })
} }
pub fn variance((array,): (Value,)) -> Result<Value, Error> { pub fn variance((array,): (Value,)) -> Result<Value, Error> {
match array { Ok(match array {
Value::Array(v) => Ok(v.as_numbers().variance().into()), Value::Array(v) => v.as_numbers().variance().into(),
_ => Ok(Value::None), _ => Value::None,
} })
} }

View file

@ -6,23 +6,23 @@ pub mod email {
pub fn host((string,): (String,)) -> Result<Value, Error> { pub fn host((string,): (String,)) -> Result<Value, Error> {
// Parse the email address // Parse the email address
match addr::parse_email_address(&string) { Ok(match addr::parse_email_address(&string) {
// Return the host part // Return the host part
Ok(v) => match v.host() { Ok(v) => match v.host() {
Host::Domain(name) => Ok(name.as_str().into()), Host::Domain(name) => name.as_str().into(),
Host::IpAddr(ip_addr) => Ok(ip_addr.to_string().into()), Host::IpAddr(ip_addr) => ip_addr.to_string().into(),
}, },
Err(_) => Ok(Value::None), Err(_) => Value::None,
} })
} }
pub fn user((string,): (String,)) -> Result<Value, Error> { pub fn user((string,): (String,)) -> Result<Value, Error> {
// Parse the email address // Parse the email address
match addr::parse_email_address(&string) { Ok(match addr::parse_email_address(&string) {
// Return the user part // Return the user part
Ok(v) => Ok(v.user().into()), Ok(v) => v.user().into(),
Err(_) => Ok(Value::None), Err(_) => Value::None,
} })
} }
#[cfg(test)] #[cfg(test)]

View file

@ -4,7 +4,7 @@ use crate::sql::datetime::Datetime;
use crate::sql::uuid::Uuid; use crate::sql::uuid::Uuid;
use crate::sql::value::Value; use crate::sql::value::Value;
use nanoid::nanoid; use nanoid::nanoid;
use rand::distributions::Alphanumeric; use rand::distributions::{Alphanumeric, DistString};
use rand::prelude::IteratorRandom; use rand::prelude::IteratorRandom;
use rand::Rng; use rand::Rng;
@ -103,12 +103,7 @@ pub fn string((arg1, arg2): (Option<i64>, Option<i64>)) -> Result<Value, Error>
32 32
}; };
Ok(rand::thread_rng() Ok(Alphanumeric.sample_string(&mut rand::thread_rng(), len).into())
.sample_iter(&Alphanumeric)
.take(len)
.map(char::from)
.collect::<String>()
.into())
} }
pub fn time((range,): (Option<(i64, i64)>,)) -> Result<Value, Error> { pub fn time((range,): (Option<(i64, i64)>,)) -> Result<Value, Error> {

View file

@ -12,7 +12,7 @@ pub fn ends_with((val, chr): (String, String)) -> Result<Value, Error> {
pub fn join(args: Vec<Value>) -> Result<Value, Error> { pub fn join(args: Vec<Value>) -> Result<Value, Error> {
let mut args = args.into_iter().map(Value::as_string); let mut args = args.into_iter().map(Value::as_string);
let chr = args.next().ok_or(Error::InvalidArguments { let chr = args.next().ok_or_else(|| Error::InvalidArguments {
name: String::from("string::join"), name: String::from("string::join"),
message: String::from("Expected at least one argument"), message: String::from("Expected at least one argument"),
})?; })?;

View file

@ -18,19 +18,16 @@ pub fn day((datetime,): (Option<Value>,)) -> Result<Value, Error> {
} }
pub fn floor((datetime, duration): (Value, Value)) -> Result<Value, Error> { pub fn floor((datetime, duration): (Value, Value)) -> Result<Value, Error> {
match datetime { Ok(match (datetime, duration) {
Value::Datetime(v) => match duration { (Value::Datetime(v), Value::Duration(w)) => match chrono::Duration::from_std(*w) {
Value::Duration(w) => match chrono::Duration::from_std(*w) { Ok(d) => match v.duration_trunc(d) {
Ok(d) => match v.duration_trunc(d) { Ok(v) => v.into(),
Ok(v) => Ok(v.into()), _ => Value::None,
_ => Ok(Value::None),
},
_ => Ok(Value::None),
}, },
_ => Ok(Value::None), _ => Value::None,
}, },
_ => Ok(Value::None), _ => Value::None,
} })
} }
pub fn group((datetime, strand): (Value, Value)) -> Result<Value, Error> { pub fn group((datetime, strand): (Value, Value)) -> Result<Value, Error> {
@ -111,19 +108,16 @@ pub fn now(_: ()) -> Result<Value, Error> {
} }
pub fn round((datetime, duration): (Value, Value)) -> Result<Value, Error> { pub fn round((datetime, duration): (Value, Value)) -> Result<Value, Error> {
match datetime { Ok(match (datetime, duration) {
Value::Datetime(v) => match duration { (Value::Datetime(v), Value::Duration(w)) => match chrono::Duration::from_std(*w) {
Value::Duration(w) => match chrono::Duration::from_std(*w) { Ok(d) => match v.duration_round(d) {
Ok(d) => match v.duration_round(d) { Ok(v) => v.into(),
Ok(v) => Ok(v.into()), _ => Value::None,
_ => Ok(Value::None),
},
_ => Ok(Value::None),
}, },
_ => Ok(Value::None), _ => Value::None,
}, },
_ => Ok(Value::None), _ => Value::None,
} })
} }
pub fn secs((datetime,): (Option<Value>,)) -> Result<Value, Error> { pub fn secs((datetime,): (Option<Value>,)) -> Result<Value, Error> {

View file

@ -17,20 +17,20 @@ pub enum Entry {
Ns(Arc<DefineNamespaceStatement>), Ns(Arc<DefineNamespaceStatement>),
Db(Arc<DefineDatabaseStatement>), Db(Arc<DefineDatabaseStatement>),
Tb(Arc<DefineTableStatement>), Tb(Arc<DefineTableStatement>),
Nss(Arc<Vec<DefineNamespaceStatement>>), Nss(Arc<[DefineNamespaceStatement]>),
Nls(Arc<Vec<DefineLoginStatement>>), Nls(Arc<[DefineLoginStatement]>),
Nts(Arc<Vec<DefineTokenStatement>>), Nts(Arc<[DefineTokenStatement]>),
Dbs(Arc<Vec<DefineDatabaseStatement>>), Dbs(Arc<[DefineDatabaseStatement]>),
Dls(Arc<Vec<DefineLoginStatement>>), Dls(Arc<[DefineLoginStatement]>),
Dts(Arc<Vec<DefineTokenStatement>>), Dts(Arc<[DefineTokenStatement]>),
Scs(Arc<Vec<DefineScopeStatement>>), Scs(Arc<[DefineScopeStatement]>),
Sts(Arc<Vec<DefineTokenStatement>>), Sts(Arc<[DefineTokenStatement]>),
Tbs(Arc<Vec<DefineTableStatement>>), Tbs(Arc<[DefineTableStatement]>),
Evs(Arc<Vec<DefineEventStatement>>), Evs(Arc<[DefineEventStatement]>),
Fds(Arc<Vec<DefineFieldStatement>>), Fds(Arc<[DefineFieldStatement]>),
Ixs(Arc<Vec<DefineIndexStatement>>), Ixs(Arc<[DefineIndexStatement]>),
Fts(Arc<Vec<DefineTableStatement>>), Fts(Arc<[DefineTableStatement]>),
Lvs(Arc<Vec<LiveStatement>>), Lvs(Arc<[LiveStatement]>),
} }
#[derive(Default)] #[derive(Default)]

View file

@ -612,7 +612,7 @@ impl Transaction {
Ok(()) Ok(())
} }
/// Retrieve all namespace definitions in a datastore. /// Retrieve all namespace definitions in a datastore.
pub async fn all_ns(&mut self) -> Result<Arc<Vec<DefineNamespaceStatement>>, Error> { pub async fn all_ns(&mut self) -> Result<Arc<[DefineNamespaceStatement]>, Error> {
let key = crate::key::ns::prefix(); let key = crate::key::ns::prefix();
match self.cache.exi(&key) { match self.cache.exi(&key) {
true => match self.cache.get(&key) { true => match self.cache.get(&key) {
@ -623,14 +623,14 @@ impl Transaction {
let beg = crate::key::ns::prefix(); let beg = crate::key::ns::prefix();
let end = crate::key::ns::suffix(); let end = crate::key::ns::suffix();
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
let val = Arc::new(val.convert()); let val = val.convert().into();
self.cache.set(key, Entry::Nss(val.clone())); self.cache.set(key, Entry::Nss(Arc::clone(&val)));
Ok(val) Ok(val)
} }
} }
} }
/// Retrieve all namespace login definitions for a specific namespace. /// Retrieve all namespace login definitions for a specific namespace.
pub async fn all_nl(&mut self, ns: &str) -> Result<Arc<Vec<DefineLoginStatement>>, Error> { pub async fn all_nl(&mut self, ns: &str) -> Result<Arc<[DefineLoginStatement]>, Error> {
let key = crate::key::nl::prefix(ns); let key = crate::key::nl::prefix(ns);
match self.cache.exi(&key) { match self.cache.exi(&key) {
true => match self.cache.get(&key) { true => match self.cache.get(&key) {
@ -641,14 +641,14 @@ impl Transaction {
let beg = crate::key::nl::prefix(ns); let beg = crate::key::nl::prefix(ns);
let end = crate::key::nl::suffix(ns); let end = crate::key::nl::suffix(ns);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
let val = Arc::new(val.convert()); let val = val.convert().into();
self.cache.set(key, Entry::Nls(val.clone())); self.cache.set(key, Entry::Nls(Arc::clone(&val)));
Ok(val) Ok(val)
} }
} }
} }
/// Retrieve all namespace token definitions for a specific namespace. /// Retrieve all namespace token definitions for a specific namespace.
pub async fn all_nt(&mut self, ns: &str) -> Result<Arc<Vec<DefineTokenStatement>>, Error> { pub async fn all_nt(&mut self, ns: &str) -> Result<Arc<[DefineTokenStatement]>, Error> {
let key = crate::key::nt::prefix(ns); let key = crate::key::nt::prefix(ns);
match self.cache.exi(&key) { match self.cache.exi(&key) {
true => match self.cache.get(&key) { true => match self.cache.get(&key) {
@ -659,14 +659,14 @@ impl Transaction {
let beg = crate::key::nt::prefix(ns); let beg = crate::key::nt::prefix(ns);
let end = crate::key::nt::suffix(ns); let end = crate::key::nt::suffix(ns);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
let val = Arc::new(val.convert()); let val = val.convert().into();
self.cache.set(key, Entry::Nts(val.clone())); self.cache.set(key, Entry::Nts(Arc::clone(&val)));
Ok(val) Ok(val)
} }
} }
} }
/// Retrieve all database definitions for a specific namespace. /// Retrieve all database definitions for a specific namespace.
pub async fn all_db(&mut self, ns: &str) -> Result<Arc<Vec<DefineDatabaseStatement>>, Error> { pub async fn all_db(&mut self, ns: &str) -> Result<Arc<[DefineDatabaseStatement]>, Error> {
let key = crate::key::db::prefix(ns); let key = crate::key::db::prefix(ns);
match self.cache.exi(&key) { match self.cache.exi(&key) {
true => match self.cache.get(&key) { true => match self.cache.get(&key) {
@ -677,8 +677,8 @@ impl Transaction {
let beg = crate::key::db::prefix(ns); let beg = crate::key::db::prefix(ns);
let end = crate::key::db::suffix(ns); let end = crate::key::db::suffix(ns);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
let val = Arc::new(val.convert()); let val = val.convert().into();
self.cache.set(key, Entry::Dbs(val.clone())); self.cache.set(key, Entry::Dbs(Arc::clone(&val)));
Ok(val) Ok(val)
} }
} }
@ -688,7 +688,7 @@ impl Transaction {
&mut self, &mut self,
ns: &str, ns: &str,
db: &str, db: &str,
) -> Result<Arc<Vec<DefineLoginStatement>>, Error> { ) -> Result<Arc<[DefineLoginStatement]>, Error> {
let key = crate::key::dl::prefix(ns, db); let key = crate::key::dl::prefix(ns, db);
match self.cache.exi(&key) { match self.cache.exi(&key) {
true => match self.cache.get(&key) { true => match self.cache.get(&key) {
@ -699,8 +699,8 @@ impl Transaction {
let beg = crate::key::dl::prefix(ns, db); let beg = crate::key::dl::prefix(ns, db);
let end = crate::key::dl::suffix(ns, db); let end = crate::key::dl::suffix(ns, db);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
let val = Arc::new(val.convert()); let val = val.convert().into();
self.cache.set(key, Entry::Dls(val.clone())); self.cache.set(key, Entry::Dls(Arc::clone(&val)));
Ok(val) Ok(val)
} }
} }
@ -710,7 +710,7 @@ impl Transaction {
&mut self, &mut self,
ns: &str, ns: &str,
db: &str, db: &str,
) -> Result<Arc<Vec<DefineTokenStatement>>, Error> { ) -> Result<Arc<[DefineTokenStatement]>, Error> {
let key = crate::key::dt::prefix(ns, db); let key = crate::key::dt::prefix(ns, db);
match self.cache.exi(&key) { match self.cache.exi(&key) {
true => match self.cache.get(&key) { true => match self.cache.get(&key) {
@ -721,8 +721,8 @@ impl Transaction {
let beg = crate::key::dt::prefix(ns, db); let beg = crate::key::dt::prefix(ns, db);
let end = crate::key::dt::suffix(ns, db); let end = crate::key::dt::suffix(ns, db);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
let val = Arc::new(val.convert()); let val = val.convert().into();
self.cache.set(key, Entry::Dts(val.clone())); self.cache.set(key, Entry::Dts(Arc::clone(&val)));
Ok(val) Ok(val)
} }
} }
@ -732,7 +732,7 @@ impl Transaction {
&mut self, &mut self,
ns: &str, ns: &str,
db: &str, db: &str,
) -> Result<Arc<Vec<DefineScopeStatement>>, Error> { ) -> Result<Arc<[DefineScopeStatement]>, Error> {
let key = crate::key::sc::prefix(ns, db); let key = crate::key::sc::prefix(ns, db);
match self.cache.exi(&key) { match self.cache.exi(&key) {
true => match self.cache.get(&key) { true => match self.cache.get(&key) {
@ -743,8 +743,8 @@ impl Transaction {
let beg = crate::key::sc::prefix(ns, db); let beg = crate::key::sc::prefix(ns, db);
let end = crate::key::sc::suffix(ns, db); let end = crate::key::sc::suffix(ns, db);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
let val = Arc::new(val.convert()); let val = val.convert().into();
self.cache.set(key, Entry::Scs(val.clone())); self.cache.set(key, Entry::Scs(Arc::clone(&val)));
Ok(val) Ok(val)
} }
} }
@ -755,7 +755,7 @@ impl Transaction {
ns: &str, ns: &str,
db: &str, db: &str,
sc: &str, sc: &str,
) -> Result<Arc<Vec<DefineTokenStatement>>, Error> { ) -> Result<Arc<[DefineTokenStatement]>, Error> {
let key = crate::key::st::prefix(ns, db, sc); let key = crate::key::st::prefix(ns, db, sc);
match self.cache.exi(&key) { match self.cache.exi(&key) {
true => match self.cache.get(&key) { true => match self.cache.get(&key) {
@ -766,8 +766,8 @@ impl Transaction {
let beg = crate::key::st::prefix(ns, db, sc); let beg = crate::key::st::prefix(ns, db, sc);
let end = crate::key::st::suffix(ns, db, sc); let end = crate::key::st::suffix(ns, db, sc);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
let val = Arc::new(val.convert()); let val = val.convert().into();
self.cache.set(key, Entry::Sts(val.clone())); self.cache.set(key, Entry::Sts(Arc::clone(&val)));
Ok(val) Ok(val)
} }
} }
@ -777,7 +777,7 @@ impl Transaction {
&mut self, &mut self,
ns: &str, ns: &str,
db: &str, db: &str,
) -> Result<Arc<Vec<DefineTableStatement>>, Error> { ) -> Result<Arc<[DefineTableStatement]>, Error> {
let key = crate::key::tb::prefix(ns, db); let key = crate::key::tb::prefix(ns, db);
match self.cache.exi(&key) { match self.cache.exi(&key) {
true => match self.cache.get(&key) { true => match self.cache.get(&key) {
@ -788,8 +788,8 @@ impl Transaction {
let beg = crate::key::tb::prefix(ns, db); let beg = crate::key::tb::prefix(ns, db);
let end = crate::key::tb::suffix(ns, db); let end = crate::key::tb::suffix(ns, db);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
let val = Arc::new(val.convert()); let val = val.convert().into();
self.cache.set(key, Entry::Tbs(val.clone())); self.cache.set(key, Entry::Tbs(Arc::clone(&val)));
Ok(val) Ok(val)
} }
} }
@ -800,7 +800,7 @@ impl Transaction {
ns: &str, ns: &str,
db: &str, db: &str,
tb: &str, tb: &str,
) -> Result<Arc<Vec<DefineEventStatement>>, Error> { ) -> Result<Arc<[DefineEventStatement]>, Error> {
let key = crate::key::ev::prefix(ns, db, tb); let key = crate::key::ev::prefix(ns, db, tb);
match self.cache.exi(&key) { match self.cache.exi(&key) {
true => match self.cache.get(&key) { true => match self.cache.get(&key) {
@ -811,8 +811,8 @@ impl Transaction {
let beg = crate::key::ev::prefix(ns, db, tb); let beg = crate::key::ev::prefix(ns, db, tb);
let end = crate::key::ev::suffix(ns, db, tb); let end = crate::key::ev::suffix(ns, db, tb);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
let val = Arc::new(val.convert()); let val = val.convert().into();
self.cache.set(key, Entry::Evs(val.clone())); self.cache.set(key, Entry::Evs(Arc::clone(&val)));
Ok(val) Ok(val)
} }
} }
@ -823,7 +823,7 @@ impl Transaction {
ns: &str, ns: &str,
db: &str, db: &str,
tb: &str, tb: &str,
) -> Result<Arc<Vec<DefineFieldStatement>>, Error> { ) -> Result<Arc<[DefineFieldStatement]>, Error> {
let key = crate::key::fd::prefix(ns, db, tb); let key = crate::key::fd::prefix(ns, db, tb);
match self.cache.exi(&key) { match self.cache.exi(&key) {
true => match self.cache.get(&key) { true => match self.cache.get(&key) {
@ -834,8 +834,8 @@ impl Transaction {
let beg = crate::key::fd::prefix(ns, db, tb); let beg = crate::key::fd::prefix(ns, db, tb);
let end = crate::key::fd::suffix(ns, db, tb); let end = crate::key::fd::suffix(ns, db, tb);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
let val = Arc::new(val.convert()); let val = val.convert().into();
self.cache.set(key, Entry::Fds(val.clone())); self.cache.set(key, Entry::Fds(Arc::clone(&val)));
Ok(val) Ok(val)
} }
} }
@ -846,7 +846,7 @@ impl Transaction {
ns: &str, ns: &str,
db: &str, db: &str,
tb: &str, tb: &str,
) -> Result<Arc<Vec<DefineIndexStatement>>, Error> { ) -> Result<Arc<[DefineIndexStatement]>, Error> {
let key = crate::key::ix::prefix(ns, db, tb); let key = crate::key::ix::prefix(ns, db, tb);
match self.cache.exi(&key) { match self.cache.exi(&key) {
true => match self.cache.get(&key) { true => match self.cache.get(&key) {
@ -857,8 +857,8 @@ impl Transaction {
let beg = crate::key::ix::prefix(ns, db, tb); let beg = crate::key::ix::prefix(ns, db, tb);
let end = crate::key::ix::suffix(ns, db, tb); let end = crate::key::ix::suffix(ns, db, tb);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
let val = Arc::new(val.convert()); let val = val.convert().into();
self.cache.set(key, Entry::Ixs(val.clone())); self.cache.set(key, Entry::Ixs(Arc::clone(&val)));
Ok(val) Ok(val)
} }
} }
@ -869,7 +869,7 @@ impl Transaction {
ns: &str, ns: &str,
db: &str, db: &str,
tb: &str, tb: &str,
) -> Result<Arc<Vec<DefineTableStatement>>, Error> { ) -> Result<Arc<[DefineTableStatement]>, Error> {
let key = crate::key::ft::prefix(ns, db, tb); let key = crate::key::ft::prefix(ns, db, tb);
match self.cache.exi(&key) { match self.cache.exi(&key) {
true => match self.cache.get(&key) { true => match self.cache.get(&key) {
@ -880,8 +880,8 @@ impl Transaction {
let beg = crate::key::ft::prefix(ns, db, tb); let beg = crate::key::ft::prefix(ns, db, tb);
let end = crate::key::ft::suffix(ns, db, tb); let end = crate::key::ft::suffix(ns, db, tb);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
let val = Arc::new(val.convert()); let val = val.convert().into();
self.cache.set(key, Entry::Fts(val.clone())); self.cache.set(key, Entry::Fts(Arc::clone(&val)));
Ok(val) Ok(val)
} }
} }
@ -892,7 +892,7 @@ impl Transaction {
ns: &str, ns: &str,
db: &str, db: &str,
tb: &str, tb: &str,
) -> Result<Arc<Vec<LiveStatement>>, Error> { ) -> Result<Arc<[LiveStatement]>, Error> {
let key = crate::key::lv::prefix(ns, db, tb); let key = crate::key::lv::prefix(ns, db, tb);
match self.cache.exi(&key) { match self.cache.exi(&key) {
true => match self.cache.get(&key) { true => match self.cache.get(&key) {
@ -903,8 +903,8 @@ impl Transaction {
let beg = crate::key::lv::prefix(ns, db, tb); let beg = crate::key::lv::prefix(ns, db, tb);
let end = crate::key::lv::suffix(ns, db, tb); let end = crate::key::lv::suffix(ns, db, tb);
let val = self.getr(beg..end, u32::MAX).await?; let val = self.getr(beg..end, u32::MAX).await?;
let val = Arc::new(val.convert()); let val = val.convert().into();
self.cache.set(key, Entry::Lvs(val.clone())); self.cache.set(key, Entry::Lvs(Arc::clone(&val)));
Ok(val) Ok(val)
} }
} }
@ -1074,7 +1074,7 @@ impl Transaction {
_ => { _ => {
let val = self.get(key.clone()).await?.ok_or(Error::NsNotFound)?; let val = self.get(key.clone()).await?.ok_or(Error::NsNotFound)?;
let val: Arc<DefineNamespaceStatement> = Arc::new(val.into()); let val: Arc<DefineNamespaceStatement> = Arc::new(val.into());
self.cache.set(key, Entry::Ns(val.clone())); self.cache.set(key, Entry::Ns(Arc::clone(&val)));
Ok(val) Ok(val)
} }
} }
@ -1094,7 +1094,7 @@ impl Transaction {
_ => { _ => {
let val = self.get(key.clone()).await?.ok_or(Error::DbNotFound)?; let val = self.get(key.clone()).await?.ok_or(Error::DbNotFound)?;
let val: Arc<DefineDatabaseStatement> = Arc::new(val.into()); let val: Arc<DefineDatabaseStatement> = Arc::new(val.into());
self.cache.set(key, Entry::Db(val.clone())); self.cache.set(key, Entry::Db(Arc::clone(&val)));
Ok(val) Ok(val)
} }
} }
@ -1115,7 +1115,7 @@ impl Transaction {
_ => { _ => {
let val = self.get(key.clone()).await?.ok_or(Error::TbNotFound)?; let val = self.get(key.clone()).await?.ok_or(Error::TbNotFound)?;
let val: Arc<DefineTableStatement> = Arc::new(val.into()); let val: Arc<DefineTableStatement> = Arc::new(val.into());
self.cache.set(key, Entry::Tb(val.clone())); self.cache.set(key, Entry::Tb(Arc::clone(&val)));
Ok(val) Ok(val)
} }
} }

View file

@ -20,19 +20,13 @@ pub struct Uuid(pub uuid::Uuid);
impl From<&str> for Uuid { impl From<&str> for Uuid {
fn from(s: &str) -> Self { fn from(s: &str) -> Self {
match uuid::Uuid::try_parse(s) { uuid::Uuid::try_parse(s).map(Uuid).unwrap_or_default()
Ok(v) => Uuid(v),
_ => Uuid::default(),
}
} }
} }
impl From<String> for Uuid { impl From<String> for Uuid {
fn from(s: String) -> Self { fn from(s: String) -> Self {
match uuid::Uuid::try_parse(&s) { Self::from(s.as_str())
Ok(v) => Uuid(v),
_ => Uuid::default(),
}
} }
} }