From e37a6fb18bb0beaca37ea259ee1e0c36066b7e01 Mon Sep 17 00:00:00 2001 From: Emmanuel Keller Date: Wed, 22 May 2024 11:05:32 +0100 Subject: [PATCH] Make temporary table active only if the temporary directory is set (#4079) --- core/src/ctx/context.rs | 21 ++++++--------------- core/src/dbs/result.rs | 4 +++- core/src/kvs/ds.rs | 17 ++++------------- lib/src/api/engine/local/native.rs | 6 +++++- src/dbs/mod.rs | 7 ++++++- 5 files changed, 24 insertions(+), 31 deletions(-) diff --git a/core/src/ctx/context.rs b/core/src/ctx/context.rs index 9d008741..ed68fb4d 100644 --- a/core/src/ctx/context.rs +++ b/core/src/ctx/context.rs @@ -12,15 +12,6 @@ use crate::sql::value::Value; use channel::Sender; use std::borrow::Cow; use std::collections::HashMap; -#[cfg(any( - feature = "kv-surrealkv", - feature = "kv-file", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-speedb" -))] -use std::env; use std::fmt::{self, Debug}; #[cfg(any( feature = "kv-surrealkv", @@ -30,7 +21,7 @@ use std::fmt::{self, Debug}; feature = "kv-tikv", feature = "kv-speedb" ))] -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use std::str::FromStr; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; @@ -91,7 +82,7 @@ pub struct Context<'a> { feature = "kv-speedb" ))] // The temporary directory - temporary_directory: Arc, + temporary_directory: Option>, } impl<'a> Default for Context<'a> { @@ -133,7 +124,7 @@ impl<'a> Context<'a> { feature = "kv-tikv", feature = "kv-speedb" ))] - temporary_directory: Arc, + temporary_directory: Option>, ) -> Result, Error> { let mut ctx = Self { values: HashMap::default(), @@ -200,7 +191,7 @@ impl<'a> Context<'a> { feature = "kv-tikv", feature = "kv-speedb" ))] - temporary_directory: Arc::new(env::temp_dir()), + temporary_directory: None, } } @@ -371,8 +362,8 @@ impl<'a> Context<'a> { feature = "kv-tikv", feature = "kv-speedb" ))] - /// Return the location of the temporary directory - pub fn temporary_directory(&self) -> &Path { + /// 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/result.rs b/core/src/dbs/result.rs index 52116b95..c07c6c31 100644 --- a/core/src/dbs/result.rs +++ b/core/src/dbs/result.rs @@ -57,7 +57,9 @@ impl Results { feature = "kv-speedb" ))] if !ctx.is_memory() { - return Ok(Self::File(Box::new(FileCollector::new(ctx.temporary_directory())?))); + if let Some(temp_dir) = ctx.temporary_directory() { + return Ok(Self::File(Box::new(FileCollector::new(temp_dir)?))); + } } Ok(Self::Memory(Default::default())) } diff --git a/core/src/kvs/ds.rs b/core/src/kvs/ds.rs index 99eab051..f4f47b08 100644 --- a/core/src/kvs/ds.rs +++ b/core/src/kvs/ds.rs @@ -1,13 +1,4 @@ use std::collections::{BTreeMap, BTreeSet}; -#[cfg(any( - feature = "kv-surrealkv", - feature = "kv-file", - feature = "kv-rocksdb", - feature = "kv-fdb", - feature = "kv-tikv", - feature = "kv-speedb" -))] -use std::env; use std::fmt; #[cfg(any( feature = "kv-surrealkv", @@ -110,7 +101,7 @@ pub struct Datastore { feature = "kv-speedb" ))] // The temporary directory - temporary_directory: Arc, + temporary_directory: Option>, pub(crate) lq_cf_store: Arc>, } @@ -392,7 +383,7 @@ impl Datastore { feature = "kv-tikv", feature = "kv-speedb" ))] - temporary_directory: Arc::new(env::temp_dir()), + temporary_directory: None, lq_cf_store: Arc::new(RwLock::new(LiveQueryTracker::new())), }) } @@ -454,8 +445,8 @@ impl Datastore { feature = "kv-tikv", feature = "kv-speedb" ))] - pub fn with_temporary_directory(mut self, path: Option) -> Self { - self.temporary_directory = Arc::new(path.unwrap_or_else(env::temp_dir)); + pub fn with_temporary_directory(mut self, path: PathBuf) -> Self { + self.temporary_directory = Some(Arc::new(path)); self } diff --git a/lib/src/api/engine/local/native.rs b/lib/src/api/engine/local/native.rs index c334c266..b22a6ff8 100644 --- a/lib/src/api/engine/local/native.rs +++ b/lib/src/api/engine/local/native.rs @@ -145,6 +145,7 @@ pub(crate) fn router( .with_query_timeout(address.config.query_timeout) .with_transaction_timeout(address.config.transaction_timeout) .with_capabilities(address.config.capabilities); + #[cfg(any( feature = "kv-surrealkv", feature = "kv-file", @@ -153,7 +154,10 @@ pub(crate) fn router( feature = "kv-tikv", feature = "kv-speedb" ))] - let kvs = kvs.with_temporary_directory(address.config.temporary_directory); + let kvs = match address.config.temporary_directory { + Some(tmp_dir) => kvs.with_temporary_directory(tmp_dir), + _ => kvs, + }; let kvs = Arc::new(kvs); let mut vars = BTreeMap::new(); diff --git a/src/dbs/mod.rs b/src/dbs/mod.rs index a506588a..c843786a 100644 --- a/src/dbs/mod.rs +++ b/src/dbs/mod.rs @@ -279,6 +279,7 @@ pub async fn init( .with_auth_enabled(!unauthenticated) .with_auth_level_enabled(auth_level_enabled) .with_capabilities(caps); + #[cfg(any( feature = "storage-surrealkv", feature = "storage-rocksdb", @@ -286,7 +287,11 @@ pub async fn init( feature = "storage-tikv", feature = "storage-speedb" ))] - let mut dbs = dbs.with_temporary_directory(temporary_directory); + let mut dbs = match temporary_directory { + Some(tmp_dir) => dbs.with_temporary_directory(tmp_dir), + _ => dbs, + }; + if let Some(engine_options) = opt.engine { dbs = dbs.with_engine_options(engine_options); }