Skip to content

Commit

Permalink
Make temporary table active only if the temporary directory is set (#…
Browse files Browse the repository at this point in the history
…4079)

(cherry picked from commit e37a6fb)
  • Loading branch information
emmanuel-keller committed May 22, 2024
1 parent 5b4fdab commit 5a91a54
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 29 deletions.
20 changes: 6 additions & 14 deletions core/src/ctx/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +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-rocksdb",
feature = "kv-fdb",
feature = "kv-tikv",
feature = "kv-speedb"
))]
use std::env;
use std::fmt::{self, Debug};
#[cfg(any(
feature = "kv-surrealkv",
Expand All @@ -28,7 +20,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;
Expand Down Expand Up @@ -87,7 +79,7 @@ pub struct Context<'a> {
feature = "kv-speedb"
))]
// The temporary directory
temporary_directory: Arc<PathBuf>,
temporary_directory: Option<Arc<PathBuf>>,
}

impl<'a> Default for Context<'a> {
Expand Down Expand Up @@ -127,7 +119,7 @@ impl<'a> Context<'a> {
feature = "kv-tikv",
feature = "kv-speedb"
))]
temporary_directory: Arc<PathBuf>,
temporary_directory: Option<Arc<PathBuf>>,
) -> Result<Context<'a>, Error> {
let mut ctx = Self {
values: HashMap::default(),
Expand Down Expand Up @@ -190,7 +182,7 @@ impl<'a> Context<'a> {
feature = "kv-tikv",
feature = "kv-speedb"
))]
temporary_directory: Arc::new(env::temp_dir()),
temporary_directory: None,
}
}

Expand Down Expand Up @@ -357,8 +349,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<PathBuf>> {
self.temporary_directory.as_ref()
}

Expand Down
4 changes: 3 additions & 1 deletion core/src/dbs/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,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()))
}
Expand Down
16 changes: 4 additions & 12 deletions core/src/kvs/ds.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
use std::borrow::Cow;
use std::collections::{BTreeMap, BTreeSet};
#[cfg(any(
feature = "kv-surrealkv",
feature = "kv-rocksdb",
feature = "kv-fdb",
feature = "kv-tikv",
feature = "kv-speedb"
))]
use std::env;
use std::fmt;
#[cfg(any(
feature = "kv-surrealkv",
Expand Down Expand Up @@ -118,7 +110,7 @@ pub struct Datastore {
feature = "kv-speedb"
))]
// The temporary directory
temporary_directory: Arc<PathBuf>,
temporary_directory: Option<Arc<PathBuf>>,
}

/// We always want to be circulating the live query information
Expand Down Expand Up @@ -400,7 +392,7 @@ impl Datastore {
feature = "kv-tikv",
feature = "kv-speedb"
))]
temporary_directory: Arc::new(env::temp_dir()),
temporary_directory: None,
})
}

Expand Down Expand Up @@ -460,8 +452,8 @@ impl Datastore {
feature = "kv-tikv",
feature = "kv-speedb"
))]
pub fn with_temporary_directory(mut self, path: Option<PathBuf>) -> 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
}

Expand Down
6 changes: 5 additions & 1 deletion lib/src/api/engine/local/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,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(all(
feature = "sql2",
any(
Expand All @@ -156,7 +157,10 @@ pub(crate) fn router(
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();
Expand Down
7 changes: 6 additions & 1 deletion src/dbs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ pub async fn init(
.with_auth_enabled(auth_enabled)
.with_auth_level_enabled(auth_level_enabled)
.with_capabilities(caps);

#[cfg(all(
feature = "sql2",
any(
Expand All @@ -300,7 +301,11 @@ pub async fn init(
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);
}
Expand Down

0 comments on commit 5a91a54

Please sign in to comment.