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
  • Loading branch information
emmanuel-keller committed May 22, 2024
1 parent 246e802 commit e37a6fb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 31 deletions.
21 changes: 6 additions & 15 deletions core/src/ctx/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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;
Expand Down Expand Up @@ -91,7 +82,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 @@ -133,7 +124,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 @@ -200,7 +191,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 @@ -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<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 @@ -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()))
}
Expand Down
17 changes: 4 additions & 13 deletions core/src/kvs/ds.rs
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -110,7 +101,7 @@ pub struct Datastore {
feature = "kv-speedb"
))]
// The temporary directory
temporary_directory: Arc<PathBuf>,
temporary_directory: Option<Arc<PathBuf>>,
pub(crate) lq_cf_store: Arc<RwLock<LiveQueryTracker>>,
}

Expand Down Expand Up @@ -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())),
})
}
Expand Down Expand Up @@ -454,8 +445,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 @@ -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",
Expand All @@ -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();
Expand Down
7 changes: 6 additions & 1 deletion src/dbs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,19 @@ 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",
feature = "storage-fdb",
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);
}
Expand Down

0 comments on commit e37a6fb

Please sign in to comment.