@@ -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 {
577593mod 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 > {
0 commit comments