From 1e2ba72e3780b13848c3209673a6d5b99d9dd33a Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Fri, 6 May 2022 23:09:08 +0100 Subject: [PATCH] Reduce mem size of Error enum types --- lib/src/dbs/executor.rs | 4 +-- lib/src/dbs/options.rs | 4 +-- lib/src/doc/admit.rs | 10 +++--- lib/src/doc/exist.rs | 2 +- lib/src/doc/field.rs | 4 +-- lib/src/doc/index.rs | 8 ++--- lib/src/err/mod.rs | 52 ++++++++++++++++---------------- lib/src/sql/statements/create.rs | 2 +- lib/src/sql/statements/delete.rs | 2 +- lib/src/sql/statements/insert.rs | 2 +- lib/src/sql/statements/relate.rs | 2 +- lib/src/sql/statements/update.rs | 2 +- 12 files changed, 45 insertions(+), 49 deletions(-) diff --git a/lib/src/dbs/executor.rs b/lib/src/dbs/executor.rs index 3a454d35..a297f7d6 100644 --- a/lib/src/dbs/executor.rs +++ b/lib/src/dbs/executor.rs @@ -245,9 +245,7 @@ impl<'a> Executor<'a> { let res = stm.compute(&ctx, &opt, &self.txn(), None).await; // Catch statement timeout match ctx.is_timedout() { - true => Err(Error::QueryTimeout { - timer: timeout, - }), + true => Err(Error::QueryTimedout), false => res, } } diff --git a/lib/src/dbs/options.rs b/lib/src/dbs/options.rs index 06e7b6e4..564fb3d7 100644 --- a/lib/src/dbs/options.rs +++ b/lib/src/dbs/options.rs @@ -82,9 +82,7 @@ impl Options { ..*self }) } else { - Err(Error::TooManySubqueries { - limit: self.dive, - }) + Err(Error::TooManySubqueries) } } diff --git a/lib/src/doc/admit.rs b/lib/src/doc/admit.rs index 6ea88dec..f1da3576 100644 --- a/lib/src/doc/admit.rs +++ b/lib/src/doc/admit.rs @@ -17,19 +17,19 @@ impl<'a> Document<'a> { if self.id.is_none() { return match stm { Statement::Create(_) => Err(Error::CreateStatement { - value: (*self.initial).clone(), + value: self.initial.to_string(), }), Statement::Update(_) => Err(Error::UpdateStatement { - value: (*self.initial).clone(), + value: self.initial.to_string(), }), Statement::Relate(_) => Err(Error::RelateStatement { - value: (*self.initial).clone(), + value: self.initial.to_string(), }), Statement::Delete(_) => Err(Error::DeleteStatement { - value: (*self.initial).clone(), + value: self.initial.to_string(), }), Statement::Insert(_) => Err(Error::InsertStatement { - value: (*self.initial).clone(), + value: self.initial.to_string(), }), _ => unreachable!(), }; diff --git a/lib/src/doc/exist.rs b/lib/src/doc/exist.rs index 74392212..6b9f8416 100644 --- a/lib/src/doc/exist.rs +++ b/lib/src/doc/exist.rs @@ -19,7 +19,7 @@ impl<'a> Document<'a> { if self.current.is_some() { // The record already exists return Err(Error::RecordExists { - thing: id.clone(), + thing: id.to_string(), }); } } diff --git a/lib/src/doc/field.rs b/lib/src/doc/field.rs index 399b1d1c..7efe1be1 100644 --- a/lib/src/doc/field.rs +++ b/lib/src/doc/field.rs @@ -50,9 +50,9 @@ impl<'a> Document<'a> { // Process the ASSERT clause if !expr.compute(&ctx, opt, txn, Some(&self.current)).await?.is_truthy() { return Err(Error::FieldValue { - value: val.clone(), + value: val.to_string(), field: fd.name.clone(), - check: expr.clone(), + check: expr.to_string(), }); } } diff --git a/lib/src/doc/index.rs b/lib/src/doc/index.rs index e796b4ec..6bb2ea2e 100644 --- a/lib/src/doc/index.rs +++ b/lib/src/doc/index.rs @@ -58,8 +58,8 @@ impl<'a> Document<'a> { let key = crate::key::index::new(opt.ns(), opt.db(), &ix.what, &ix.name, n); if run.putc(key, rid, None).await.is_err() { return Err(Error::IndexExists { - index: ix.name.to_owned(), - thing: rid.to_owned(), + index: ix.name.to_string(), + thing: rid.to_string(), }); } } @@ -77,8 +77,8 @@ impl<'a> Document<'a> { let key = crate::key::point::new(opt.ns(), opt.db(), &ix.what, &ix.name, n, &rid.id); if run.putc(key, rid, None).await.is_err() { return Err(Error::IndexExists { - index: ix.name.to_owned(), - thing: rid.to_owned(), + index: ix.name.to_string(), + thing: rid.to_string(), }); } } diff --git a/lib/src/err/mod.rs b/lib/src/err/mod.rs index 33d4fa0f..81ff4e30 100644 --- a/lib/src/err/mod.rs +++ b/lib/src/err/mod.rs @@ -3,7 +3,6 @@ use crate::sql::thing::Thing; use crate::sql::value::Value; use msgpack::encode::Error as SerdeError; use serde::Serialize; -use std::time::Duration; use storekey::decode::Error as DecodeError; use storekey::encode::Error as EncodeError; use thiserror::Error; @@ -88,17 +87,15 @@ pub enum Error { }, /// The query timedout - #[error("Query timeout of {timer:?} exceeded")] - QueryTimeout { - timer: Duration, - }, + #[error("The query was not executed because it exceeded the timeout")] + QueryTimedout, /// The query did not execute, because the transaction was cancelled - #[error("Query not executed due to cancelled transaction")] + #[error("The query was not executed due to a cancelled transaction")] QueryCancelled, /// The query did not execute, because the transaction has failed - #[error("Query not executed due to failed transaction")] + #[error("The query was not executed due to a failed transaction")] QueryNotExecuted, /// The permissions do not allow for performing the specified query @@ -155,44 +152,41 @@ pub enum Error { /// Too many recursive subqueries have been processed #[error("Too many recursive subqueries have been processed")] - TooManySubqueries { - limit: usize, - }, + TooManySubqueries, /// Can not execute CREATE query using the specified value #[error("Can not execute CREATE query using value '{value}'")] CreateStatement { - value: Value, + value: String, }, /// Can not execute UPDATE query using the specified value #[error("Can not execute UPDATE query using value '{value}'")] UpdateStatement { - value: Value, + value: String, }, /// Can not execute RELATE query using the specified value #[error("Can not execute RELATE query using value '{value}'")] RelateStatement { - value: Value, + value: String, }, /// Can not execute DELETE query using the specified value #[error("Can not execute DELETE query using value '{value}'")] DeleteStatement { - value: Value, + value: String, }, /// Can not execute INSERT query using the specified value #[error("Can not execute INSERT query using value '{value}'")] InsertStatement { - value: Value, + value: String, }, /// The permissions do not allow this query to be run on this table - #[error("You don't have permission to run the `{query}` query on the `{table}` table")] + #[error("You don't have permission to run this query on the `{table}` table")] TablePermissions { - query: String, table: String, }, @@ -205,24 +199,28 @@ pub enum Error { /// A database entry for the specified record already exists #[error("Database record `{thing}` already exists")] RecordExists { - thing: Thing, + thing: String, }, /// A database index entry for the specified record already exists #[error("Database index `{index}` already contains `{thing}`")] IndexExists { index: String, - thing: Thing, + thing: String, }, /// The specified field did not conform to the field ASSERT clause #[error("Found '{value}' for field '{field}' but field must conform to: {check}")] FieldValue { - value: Value, + value: String, field: Idiom, - check: Value, + check: String, }, + /// There was an error processing a value in parallel + #[error("There was an error processing a value in parallel ")] + Channel(String), + /// Represents an underlying error with Serde encoding / decoding #[error("Serde error: {0}")] Serde(#[from] SerdeError), @@ -234,11 +232,6 @@ pub enum Error { /// Represents an error when decoding a key-value entry #[error("Key decoding error: {0}")] Decode(#[from] DecodeError), - - /// Represents an underlying error with Tokio message channels - #[cfg(feature = "parallel")] - #[error("Tokio Error: {0}")] - Tokio(#[from] TokioError<(Option, Value)>), } #[cfg(feature = "kv-echodb")] @@ -262,6 +255,13 @@ impl From for Error { } } +#[cfg(feature = "parallel")] +impl From, Value)>> for Error { + fn from(e: TokioError<(Option, Value)>) -> Error { + Error::Channel(e.to_string()) + } +} + impl Serialize for Error { fn serialize(&self, serializer: S) -> Result where diff --git a/lib/src/sql/statements/create.rs b/lib/src/sql/statements/create.rs index 9becfd09..8c67ddba 100644 --- a/lib/src/sql/statements/create.rs +++ b/lib/src/sql/statements/create.rs @@ -53,7 +53,7 @@ impl CreateStatement { Value::Array(_) => i.prepare(v), v => { return Err(Error::CreateStatement { - value: v, + value: v.to_string(), }) } }; diff --git a/lib/src/sql/statements/delete.rs b/lib/src/sql/statements/delete.rs index e322a5b0..99d121c0 100644 --- a/lib/src/sql/statements/delete.rs +++ b/lib/src/sql/statements/delete.rs @@ -54,7 +54,7 @@ impl DeleteStatement { Value::Array(_) => i.prepare(v), v => { return Err(Error::DeleteStatement { - value: v, + value: v.to_string(), }) } }; diff --git a/lib/src/sql/statements/insert.rs b/lib/src/sql/statements/insert.rs index 29954d11..c2af7a0f 100644 --- a/lib/src/sql/statements/insert.rs +++ b/lib/src/sql/statements/insert.rs @@ -59,7 +59,7 @@ impl InsertStatement { Value::Object(_) => i.prepare(v), v => { return Err(Error::InsertStatement { - value: v, + value: v.to_string(), }) } } diff --git a/lib/src/sql/statements/relate.rs b/lib/src/sql/statements/relate.rs index 383c6119..ed099a76 100644 --- a/lib/src/sql/statements/relate.rs +++ b/lib/src/sql/statements/relate.rs @@ -60,7 +60,7 @@ impl RelateStatement { Value::Array(_) => i.prepare(v), v => { return Err(Error::RelateStatement { - value: v, + value: v.to_string(), }) } }; diff --git a/lib/src/sql/statements/update.rs b/lib/src/sql/statements/update.rs index 239003b5..c8bf8012 100644 --- a/lib/src/sql/statements/update.rs +++ b/lib/src/sql/statements/update.rs @@ -55,7 +55,7 @@ impl UpdateStatement { Value::Array(_) => i.prepare(v), v => { return Err(Error::UpdateStatement { - value: v, + value: v.to_string(), }) } };