@@ -10,8 +10,7 @@ use std::sync::{Arc, RwLock};
1010use index:: flat:: index:: FlatIndex ;
1111use index:: { IndexType , VectorIndex } ;
1212use snapshot:: Snapshot ;
13- use storage:: rocks_db:: RocksDbStorage ;
14- use storage:: { StorageEngine , StorageType , VectorPage } ;
13+ use storage:: { StorageEngine , StorageType , VectorPage , create_storage_engine} ;
1514
1615use uuid:: Uuid ;
1716
@@ -252,10 +251,7 @@ pub fn restore_from_snapshot(config: &DbRestoreConfig) -> Result<VectorDb, DbErr
252251
253252pub fn init_api ( config : DbConfig ) -> Result < VectorDb > {
254253 // Initialize the storage engine
255- let storage = match config. storage_type {
256- StorageType :: RocksDb => Arc :: new ( RocksDbStorage :: new ( config. data_path ) ?) ,
257- _ => Arc :: new ( RocksDbStorage :: new ( config. data_path ) ?) ,
258- } ;
254+ let storage = create_storage_engine ( config. storage_type , config. data_path ) ?;
259255
260256 // Initialize the vector index
261257 let index: Arc < RwLock < dyn VectorIndex > > = match config. index_type {
@@ -290,9 +286,13 @@ mod tests {
290286
291287 // Helper function to create a test database
292288 fn create_test_db ( ) -> ( VectorDb , TempDir ) {
289+ create_test_db_with_storage ( StorageType :: RocksDb )
290+ }
291+
292+ fn create_test_db_with_storage ( storage_type : StorageType ) -> ( VectorDb , TempDir ) {
293293 let temp_dir = tempdir ( ) . unwrap ( ) ;
294294 let config = DbConfig {
295- storage_type : StorageType :: RocksDb ,
295+ storage_type,
296296 index_type : IndexType :: Flat ,
297297 data_path : temp_dir. path ( ) . to_path_buf ( ) ,
298298 dimension : 3 ,
@@ -301,6 +301,13 @@ mod tests {
301301 ( init_api ( config) . unwrap ( ) , temp_dir)
302302 }
303303
304+ fn test_payload ( content : & str ) -> Payload {
305+ Payload {
306+ content_type : ContentType :: Text ,
307+ content : content. to_string ( ) ,
308+ }
309+ }
310+
304311 #[ test]
305312 fn test_insert_and_get ( ) {
306313 let ( db, _temp_dir) = create_test_db ( ) ;
@@ -326,6 +333,20 @@ mod tests {
326333 assert_eq ! ( point. payload. as_ref( ) . unwrap( ) . content, "Test content" ) ;
327334 }
328335
336+ #[ test]
337+ fn test_insert_and_get_with_in_memory_storage ( ) {
338+ let ( db, _temp_dir) = create_test_db_with_storage ( StorageType :: InMemory ) ;
339+ let vector = vec ! [ 1.0 , 2.0 , 3.0 ] ;
340+ let payload = test_payload ( "Test content" ) ;
341+
342+ let id = db. insert ( vector. clone ( ) , payload. clone ( ) ) . unwrap ( ) ;
343+ let point = db. get ( id) . unwrap ( ) . unwrap ( ) ;
344+
345+ assert_eq ! ( point. id, id) ;
346+ assert_eq ! ( point. vector, Some ( vector) ) ;
347+ assert_eq ! ( point. payload, Some ( payload) ) ;
348+ }
349+
329350 #[ test]
330351 fn test_dimension_mismatch ( ) {
331352 let ( db, _temp_dir) = create_test_db ( ) ;
@@ -593,6 +614,34 @@ mod tests {
593614 assert ! ( loaded_db. get( id2) . unwrap( ) . unwrap( ) . vector. unwrap( ) == v2) ;
594615 }
595616
617+ #[ test]
618+ fn test_create_and_load_snapshot_with_in_memory_storage ( ) {
619+ let ( old_db, temp_dir) = create_test_db_with_storage ( StorageType :: InMemory ) ;
620+
621+ let v1 = vec ! [ 0.0 , 1.0 , 2.0 ] ;
622+ let v2 = vec ! [ 3.0 , 4.0 , 5.0 ] ;
623+ let v3 = vec ! [ 6.0 , 7.0 , 8.0 ] ;
624+
625+ let id1 = old_db. insert ( v1. clone ( ) , test_payload ( "one" ) ) . unwrap ( ) ;
626+ let id2 = old_db. insert ( v2. clone ( ) , test_payload ( "two" ) ) . unwrap ( ) ;
627+
628+ let temp_snapshot_dir = tempdir ( ) . unwrap ( ) ;
629+ let snapshot_path = old_db. create_snapshot ( temp_snapshot_dir. path ( ) ) . unwrap ( ) ;
630+
631+ let id3 = old_db. insert ( v3, test_payload ( "three" ) ) . unwrap ( ) ;
632+
633+ let reload_config = DbRestoreConfig {
634+ data_path : temp_dir. path ( ) . to_path_buf ( ) ,
635+ snapshot_path,
636+ } ;
637+
638+ let loaded_db = restore_from_snapshot ( & reload_config) . unwrap ( ) ;
639+
640+ assert_eq ! ( loaded_db. get( id1) . unwrap( ) . unwrap( ) . vector, Some ( v1) ) ;
641+ assert_eq ! ( loaded_db. get( id2) . unwrap( ) . unwrap( ) . vector, Some ( v2) ) ;
642+ assert ! ( loaded_db. get( id3) . unwrap( ) . is_none( ) ) ;
643+ }
644+
596645 #[ test]
597646 fn test_snapshot_engine ( ) {
598647 let ( _db, _temp_dir) = create_test_db ( ) ;
0 commit comments