From dc06603d5bd6a363a9ba155bcb789488d075c823 Mon Sep 17 00:00:00 2001 From: Raphael Darley Date: Thu, 5 Sep 2024 01:35:35 -0700 Subject: [PATCH] Simplify `cfg` attributes around codebase (#4695) Co-authored-by: Tobie Morgan Hitchcock --- core/src/cnf/mod.rs | 9 +-- core/src/ctx/context.rs | 80 +++----------------------- core/src/dbs/iterator.rs | 9 +-- core/src/dbs/result.rs | 91 ++++-------------------------- core/src/dbs/statement.rs | 9 +-- core/src/dbs/store.rs | 9 +-- core/src/err/mod.rs | 18 +----- core/src/kvs/ds.rs | 53 ++--------------- sdk/src/api/engine/local/native.rs | 9 +-- sdk/src/api/opt/config.rs | 27 +-------- 10 files changed, 35 insertions(+), 279 deletions(-) diff --git a/core/src/cnf/mod.rs b/core/src/cnf/mod.rs index b875e6e0..effd6573 100644 --- a/core/src/cnf/mod.rs +++ b/core/src/cnf/mod.rs @@ -52,14 +52,7 @@ pub static SCRIPTING_MAX_MEMORY_LIMIT: LazyLock = pub static INSECURE_FORWARD_ACCESS_ERRORS: LazyLock = lazy_env_parse!("SURREAL_INSECURE_FORWARD_ACCESS_ERRORS", bool, false); -#[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", -))] +#[cfg(storage)] /// Specifies the buffer limit for external sorting. pub static EXTERNAL_SORTING_BUFFER_LIMIT: LazyLock = lazy_env_parse!("SURREAL_EXTERNAL_SORTING_BUFFER_LIMIT", usize, 50_000); diff --git a/core/src/ctx/context.rs b/core/src/ctx/context.rs index 38d2ef92..f1e25a99 100644 --- a/core/src/ctx/context.rs +++ b/core/src/ctx/context.rs @@ -16,14 +16,7 @@ use channel::Sender; use std::borrow::Cow; use std::collections::HashMap; use std::fmt::{self, Debug}; -#[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", -))] +#[cfg(storage)] use std::path::PathBuf; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; @@ -71,14 +64,7 @@ pub struct MutableContext { index_builder: Option, // Capabilities capabilities: Arc, - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] + #[cfg(storage)] // The temporary directory temporary_directory: Option>, // An optional transaction @@ -118,15 +104,7 @@ impl MutableContext { capabilities: Capabilities, index_stores: IndexStores, #[cfg(not(target_arch = "wasm32"))] index_builder: IndexBuilder, - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] - temporary_directory: Option>, + #[cfg(storage)] temporary_directory: Option>, ) -> Result { let mut ctx = Self { values: HashMap::default(), @@ -141,14 +119,7 @@ impl MutableContext { index_stores, #[cfg(not(target_arch = "wasm32"))] index_builder: Some(index_builder), - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] + #[cfg(storage)] temporary_directory, transaction: None, isolated: false, @@ -173,14 +144,7 @@ impl MutableContext { index_stores: IndexStores::default(), #[cfg(not(target_arch = "wasm32"))] index_builder: None, - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] + #[cfg(storage)] temporary_directory: None, transaction: None, isolated: false, @@ -201,14 +165,7 @@ impl MutableContext { index_stores: parent.index_stores.clone(), #[cfg(not(target_arch = "wasm32"))] index_builder: parent.index_builder.clone(), - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] + #[cfg(storage)] temporary_directory: parent.temporary_directory.clone(), transaction: parent.transaction.clone(), isolated: false, @@ -240,13 +197,7 @@ impl MutableContext { index_stores: parent.index_stores.clone(), #[cfg(not(target_arch = "wasm32"))] index_builder: parent.index_builder.clone(), - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - ))] + #[cfg(storage)] temporary_directory: parent.temporary_directory.clone(), transaction: parent.transaction.clone(), isolated: true, @@ -268,13 +219,7 @@ impl MutableContext { index_stores: from.index_stores.clone(), #[cfg(not(target_arch = "wasm32"))] index_builder: from.index_builder.clone(), - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - ))] + #[cfg(storage)] temporary_directory: from.temporary_directory.clone(), transaction: None, isolated: false, @@ -409,14 +354,7 @@ impl MutableContext { matches!(self.done(), Some(Reason::Timedout)) } - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] + #[cfg(storage)] /// Return the location of the temporary directory if any pub fn temporary_directory(&self) -> Option<&Arc> { self.temporary_directory.as_ref() diff --git a/core/src/dbs/iterator.rs b/core/src/dbs/iterator.rs index ad4bd76f..dc1587d9 100644 --- a/core/src/dbs/iterator.rs +++ b/core/src/dbs/iterator.rs @@ -301,14 +301,7 @@ impl Iterator { self.setup_start(stk, &cancel_ctx, opt, stm).await?; // Prepare the results with possible optimisations on groups self.results = self.results.prepare( - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] + #[cfg(storage)] ctx, stm, )?; diff --git a/core/src/dbs/result.rs b/core/src/dbs/result.rs index ab97750a..8e948fdd 100644 --- a/core/src/dbs/result.rs +++ b/core/src/dbs/result.rs @@ -1,14 +1,7 @@ use crate::ctx::Context; use crate::dbs::group::GroupsCollector; use crate::dbs::plan::Explanation; -#[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", -))] +#[cfg(storage)] use crate::dbs::store::file_store::FileCollector; use crate::dbs::store::MemoryCollector; use crate::dbs::{Options, Statement}; @@ -19,14 +12,7 @@ use reblessive::tree::Stk; pub(super) enum Results { None, Memory(MemoryCollector), - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] + #[cfg(storage)] File(Box), Groups(GroupsCollector), } @@ -34,28 +20,13 @@ pub(super) enum Results { impl Results { pub(super) fn prepare( &mut self, - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] - ctx: &Context, + #[cfg(storage)] ctx: &Context, stm: &Statement<'_>, ) -> Result { if stm.expr().is_some() && stm.group().is_some() { return Ok(Self::Groups(GroupsCollector::new(stm))); } - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] + #[cfg(storage)] if stm.tempfiles() { if let Some(temp_dir) = ctx.temporary_directory() { return Ok(Self::File(Box::new(FileCollector::new(temp_dir)?))); @@ -77,14 +48,7 @@ impl Results { Self::Memory(s) => { s.push(val); } - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] + #[cfg(storage)] Self::File(e) => { e.push(val)?; } @@ -98,14 +62,7 @@ impl Results { pub(super) fn sort(&mut self, orders: &Orders) { match self { Self::Memory(m) => m.sort(orders), - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] + #[cfg(storage)] Self::File(f) => f.sort(orders), _ => {} } @@ -115,14 +72,7 @@ impl Results { match self { Self::None => {} Self::Memory(m) => m.start_limit(start, limit), - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] + #[cfg(storage)] Self::File(f) => f.start_limit(start, limit), Self::Groups(_) => {} } @@ -132,14 +82,7 @@ impl Results { match self { Self::None => 0, Self::Memory(s) => s.len(), - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] + #[cfg(storage)] Self::File(e) => e.len(), Self::Groups(g) => g.len(), } @@ -148,14 +91,7 @@ impl Results { pub(super) fn take(&mut self) -> Result, Error> { Ok(match self { Self::Memory(m) => m.take_vec(), - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] + #[cfg(storage)] Self::File(f) => f.take_vec()?, _ => vec![], }) @@ -167,14 +103,7 @@ impl Results { Self::Memory(s) => { s.explain(exp); } - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] + #[cfg(storage)] Self::File(e) => { e.explain(exp); } diff --git a/core/src/dbs/statement.rs b/core/src/dbs/statement.rs index 23f38103..6df58e6f 100644 --- a/core/src/dbs/statement.rs +++ b/core/src/dbs/statement.rs @@ -246,14 +246,7 @@ impl<'a> Statement<'a> { /// Returns any TEMPFILES clause if specified #[inline] - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] + #[cfg(storage)] pub fn tempfiles(&self) -> bool { match self { Statement::Select(v) => v.tempfiles, diff --git a/core/src/dbs/store.rs b/core/src/dbs/store.rs index 7af65484..9c9ca48f 100644 --- a/core/src/dbs/store.rs +++ b/core/src/dbs/store.rs @@ -53,14 +53,7 @@ impl From> for MemoryCollector { } } -#[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", -))] +#[cfg(storage)] pub(super) mod file_store { use crate::cnf::EXTERNAL_SORTING_BUFFER_LIMIT; use crate::dbs::plan::Explanation; diff --git a/core/src/err/mod.rs b/core/src/err/mod.rs index 596a42fa..d1a3701d 100644 --- a/core/src/err/mod.rs +++ b/core/src/err/mod.rs @@ -9,14 +9,7 @@ use crate::syn::error::RenderedError as RenderedParserError; use crate::vs::Error as VersionstampError; use base64::DecodeError as Base64Error; use bincode::Error as BincodeError; -#[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", -))] +#[cfg(storage)] use ext_sort::SortError; use fst::Error as FstError; use jsonwebtoken::errors::Error as JWTError; @@ -1217,14 +1210,7 @@ impl From for Error { } } -#[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", -))] +#[cfg(storage)] impl From> for Error where S: std::error::Error, diff --git a/core/src/kvs/ds.rs b/core/src/kvs/ds.rs index 5d3b732a..45f97f73 100644 --- a/core/src/kvs/ds.rs +++ b/core/src/kvs/ds.rs @@ -27,14 +27,7 @@ use channel::{Receiver, Sender}; use futures::Future; use reblessive::TreeStack; use std::fmt; -#[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", -))] +#[cfg(storage)] use std::path::PathBuf; use std::sync::Arc; use std::time::Duration; @@ -83,14 +76,7 @@ pub struct Datastore { #[cfg(feature = "jwks")] // The JWKS object cache jwks_cache: Arc>, - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] + #[cfg(storage)] // The temporary directory temporary_directory: Option>, } @@ -273,13 +259,7 @@ impl Datastore { index_builder: IndexBuilder::new(self.transaction_factory.clone()), #[cfg(feature = "jwks")] jwks_cache: Arc::new(Default::default()), - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - ))] + #[cfg(storage)] temporary_directory: self.temporary_directory, transaction_factory: self.transaction_factory, } @@ -439,14 +419,7 @@ impl Datastore { index_builder: IndexBuilder::new(tf), #[cfg(feature = "jwks")] jwks_cache: Arc::new(RwLock::new(JwksCache::new())), - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] + #[cfg(storage)] temporary_directory: None, } }) @@ -494,14 +467,7 @@ impl Datastore { self } - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] + #[cfg(storage)] /// Set a temporary directory for ordering of large result sets pub fn with_temporary_directory(mut self, path: Option) -> Self { self.temporary_directory = path.map(Arc::new); @@ -856,14 +822,7 @@ impl Datastore { self.index_stores.clone(), #[cfg(not(target_arch = "wasm32"))] self.index_builder.clone(), - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] + #[cfg(storage)] self.temporary_directory.clone(), )?; // Setup the notification channel diff --git a/sdk/src/api/engine/local/native.rs b/sdk/src/api/engine/local/native.rs index 16fad6f2..56ec5bb1 100644 --- a/sdk/src/api/engine/local/native.rs +++ b/sdk/src/api/engine/local/native.rs @@ -108,14 +108,7 @@ pub(crate) async fn run_router( .with_transaction_timeout(address.config.transaction_timeout) .with_capabilities(address.config.capabilities); - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] + #[cfg(storage)] let kvs = kvs.with_temporary_directory(address.config.temporary_directory); let kvs = Arc::new(kvs); diff --git a/sdk/src/api/opt/config.rs b/sdk/src/api/opt/config.rs index 925486c5..c08562c0 100644 --- a/sdk/src/api/opt/config.rs +++ b/sdk/src/api/opt/config.rs @@ -1,12 +1,5 @@ use crate::opt::capabilities::Capabilities; -#[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", -))] +#[cfg(storage)] use std::path::PathBuf; use std::time::Duration; use surrealdb_core::{dbs::Capabilities as CoreCapabilities, iam::Level}; @@ -27,14 +20,7 @@ pub struct Config { pub(crate) password: String, pub(crate) tick_interval: Option, pub(crate) capabilities: CoreCapabilities, - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] + #[cfg(storage)] pub(crate) temporary_directory: Option, } @@ -124,14 +110,7 @@ impl Config { self } - #[cfg(any( - feature = "kv-mem", - feature = "kv-surrealkv", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-surrealcs", - ))] + #[cfg(storage)] pub fn temporary_directory(mut self, path: Option) -> Self { self.temporary_directory = path; self