Make temporary table active only if the temporary directory is set (#4079)

This commit is contained in:
Emmanuel Keller 2024-05-22 11:05:32 +01:00 committed by GitHub
parent 246e8021cb
commit e37a6fb18b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 24 additions and 31 deletions

View file

@ -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<PathBuf>,
temporary_directory: Option<Arc<PathBuf>>,
}
impl<'a> Default for Context<'a> {
@ -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(),
@ -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<PathBuf>> {
self.temporary_directory.as_ref()
}

View file

@ -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()))
}

View file

@ -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<PathBuf>,
temporary_directory: Option<Arc<PathBuf>>,
pub(crate) lq_cf_store: Arc<RwLock<LiveQueryTracker>>,
}
@ -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<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
}

View file

@ -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();

View file

@ -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);
}