Skip to content

Commit 3408b0e

Browse files
committed
hotfix: fix tmp dir to be inside db root dir
1 parent 0a07989 commit 3408b0e

File tree

3 files changed

+49
-29
lines changed

3 files changed

+49
-29
lines changed

src/db/database.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ impl Database {
5353
fs::create_dir_all(&indices_dir)?;
5454
}
5555

56+
let tmp_dir = root_dir.join("tmp");
57+
if !tmp_dir.try_exists()? {
58+
fs::create_dir_all(&tmp_dir)?;
59+
}
60+
5661
let state_file = root_dir.join("odbstate");
5762
let state = if state_file.try_exists()? {
5863
let mut state = DatabaseState::restore(&state_file)?;
@@ -77,7 +82,7 @@ impl Database {
7782

7883
// Persist the new state to the state file.
7984
let state = DatabaseState { source, indices };
80-
file::write_binary_file(state_file, &state)?;
85+
file::write_binary_file(tmp_dir, state_file, &state)?;
8186
state
8287
};
8388

@@ -128,7 +133,8 @@ impl Database {
128133
index.build(records)?;
129134

130135
// Persist the index to a file.
131-
algorithm.persist_index(&index_file, index.as_ref())?;
136+
let tmp_dir = self.tmp_dir();
137+
algorithm.persist_index(tmp_dir, &index_file, index.as_ref())?;
132138

133139
// Insert the index into the pool for easy access.
134140
{
@@ -407,7 +413,11 @@ impl Database {
407413
/// When running this method with other state lock, make sure
408414
/// to release the lock before calling this method.
409415
pub fn persist_state(&self) -> Result<(), Error> {
410-
file::write_binary_file(self.state_file(), &self.state()?)
416+
file::write_binary_file(
417+
self.tmp_dir(),
418+
self.state_file(),
419+
&self.state()?,
420+
)
411421
}
412422
}
413423

@@ -423,6 +433,11 @@ impl Database {
423433
self.root.join("indices")
424434
}
425435

436+
/// Returns the temporary directory path for the database.
437+
fn tmp_dir(&self) -> PathBuf {
438+
self.root.join("tmp")
439+
}
440+
426441
/// Persists an existing index to its file.
427442
/// - `name`: Index name.
428443
/// - `index`: Index trait object.
@@ -443,7 +458,8 @@ impl Database {
443458
Error::new(code, message)
444459
})?;
445460

446-
algorithm.persist_index(file, index)
461+
let tmp_dir = self.tmp_dir();
462+
algorithm.persist_index(tmp_dir, file, index)
447463
}
448464
}
449465

@@ -577,6 +593,7 @@ impl IndexRef {
577593
mod tests {
578594
use super::*;
579595
use sqlx::{Executor, Row};
596+
use std::env;
580597
use std::sync::MutexGuard;
581598

582599
const TABLE: &str = "embeddings";
@@ -710,7 +727,7 @@ mod tests {
710727
fs::remove_dir_all(&path)?;
711728
}
712729

713-
let db_path = file::get_tmp_dir()?.join("sqlite.db");
730+
let db_path = get_tmp_dir()?.join("sqlite.db");
714731
let db_url = format!("sqlite://{}?mode=rwc", db_path.display());
715732

716733
let mut db = Database::open(path, Some(db_url.to_owned()))?;
@@ -755,6 +772,15 @@ mod tests {
755772
)
756773
}
757774

775+
pub fn get_tmp_dir() -> Result<PathBuf, Error> {
776+
let tmp_dir = env::temp_dir().join("oasysdb");
777+
if !tmp_dir.try_exists()? {
778+
fs::create_dir_all(&tmp_dir)?;
779+
}
780+
781+
Ok(tmp_dir)
782+
}
783+
758784
async fn setup_test_source(
759785
url: impl Into<DatabaseURL>,
760786
) -> Result<(), Error> {

src/indices/mod.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,13 @@ impl IndexAlgorithm {
274274
/// - `index`: Index to persist as a trait object.
275275
pub(crate) fn persist_index(
276276
&self,
277+
tmp_dir: impl AsRef<Path>,
277278
path: impl AsRef<Path>,
278279
index: &dyn VectorIndex,
279280
) -> Result<(), Error> {
280281
macro_rules! persist {
281282
($index_type:ident) => {{
282-
Self::_persist_index::<$index_type>(path, index)
283+
Self::_persist_index::<$index_type>(tmp_dir, path, index)
283284
}};
284285
}
285286

@@ -297,6 +298,7 @@ impl IndexAlgorithm {
297298
}
298299

299300
fn _persist_index<T: VectorIndex + IndexOps + 'static>(
301+
tmp_dir: impl AsRef<Path>,
300302
path: impl AsRef<Path>,
301303
index: &dyn VectorIndex,
302304
) -> Result<(), Error> {
@@ -306,7 +308,7 @@ impl IndexAlgorithm {
306308
Error::new(code, message)
307309
})?;
308310

309-
index.persist(path)?;
311+
index.persist(tmp_dir, path)?;
310312
Ok(())
311313
}
312314
}
@@ -375,8 +377,12 @@ pub trait IndexOps: Debug + Serialize + DeserializeOwned {
375377
}
376378

377379
/// Serializes and persists the index to a file.
378-
fn persist(&self, path: impl AsRef<Path>) -> Result<(), Error> {
379-
file::write_binary_file(path, self)
380+
fn persist(
381+
&self,
382+
tmp_dir: impl AsRef<Path>,
383+
path: impl AsRef<Path>,
384+
) -> Result<(), Error> {
385+
file::write_binary_file(tmp_dir, path, self)
380386
}
381387
}
382388

src/utils/file.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use crate::types::err::{Error, ErrorCode};
22
use serde::de::DeserializeOwned;
33
use serde::Serialize;
4-
use std::env;
54
use std::fs::{self, OpenOptions};
65
use std::io::{BufReader, BufWriter};
7-
use std::path::{Path, PathBuf};
6+
use std::path::Path;
87

98
/// Reads a binary file and deserializes its contents to a type.
109
/// - `path`: Path to the binary file.
@@ -18,21 +17,20 @@ pub fn read_binary_file<T: DeserializeOwned>(
1817
}
1918

2019
/// Serializes the data and writes it to a binary file.
20+
/// - `tmp_dir`: Temporary directory path.
2121
/// - `path`: Path to the binary file.
2222
/// - `data`: Data to write.
23+
///
24+
/// This function writes the data to a temporary file first and then renames
25+
/// the temporary file to the target file. This ensures that the target file
26+
/// is not corrupted if the operation is interrupted or fails.
2327
pub fn write_binary_file<T: Serialize>(
28+
tmp_dir: impl AsRef<Path>,
2429
path: impl AsRef<Path>,
2530
data: &T,
2631
) -> Result<(), Error> {
2732
let file_name = parse_file_name(&path)?;
28-
let tmp_dir = get_tmp_dir()?;
29-
30-
// To ensure that the target file is not corrupted if
31-
// the operation is interrupted or fails:
32-
// 1. Write the data to a temporary file.
33-
// 2. Rename the temporary file to the target file.
34-
35-
let tmp_file = tmp_dir.join(file_name);
33+
let tmp_file = tmp_dir.as_ref().join(file_name);
3634
let file = OpenOptions::new()
3735
.write(true)
3836
.create(true)
@@ -46,16 +44,6 @@ pub fn write_binary_file<T: Serialize>(
4644
Ok(())
4745
}
4846

49-
/// Returns the temporary directory path for OasysDB.
50-
pub fn get_tmp_dir() -> Result<PathBuf, Error> {
51-
let tmp_dir = env::temp_dir().join("oasysdb");
52-
if !tmp_dir.try_exists()? {
53-
fs::create_dir_all(&tmp_dir)?;
54-
}
55-
56-
Ok(tmp_dir)
57-
}
58-
5947
/// Parses the file name from a path.
6048
/// - `path`: Path to a file.
6149
pub fn parse_file_name(path: impl AsRef<Path>) -> Result<String, Error> {

0 commit comments

Comments
 (0)