@@ -4,11 +4,17 @@ use defs::{DbError, Dimension, PointId, Similarity};
44use serde:: { Deserialize , Serialize } ;
55use storage:: StorageEngine ;
66
7- use crate :: { IndexSnapshot , IndexType , SerializableIndex , hnsw:: { HNSW_MAGIC_BYTES , HnswIndex , types:: { LevelGenerator , Node , PointIndexation } } } ;
7+ use crate :: {
8+ IndexSnapshot , IndexType , SerializableIndex ,
9+ hnsw:: {
10+ HNSW_MAGIC_BYTES , HnswIndex ,
11+ types:: { LevelGenerator , Node , PointIndexation } ,
12+ } ,
13+ } ;
814
915#[ repr( packed) ]
1016#[ derive( Serialize , Deserialize ) ]
11- pub struct HnswMetadataPack {
17+ pub struct HnswMetadataPack {
1218 pub ef_construction : usize ,
1319 pub data_dimension : Dimension ,
1420 pub ef : usize ,
@@ -26,7 +32,6 @@ pub struct HnswIndexPack {
2632 pub level_scale : f64 ,
2733}
2834
29-
3035impl SerializableIndex for HnswIndex {
3136 fn serialize_topology ( & self ) -> Result < Vec < u8 > , DbError > {
3237 let mut buffer = Vec :: new ( ) ;
@@ -42,7 +47,8 @@ impl SerializableIndex for HnswIndex {
4247 level_scale : self . index . level_generator . level_scale ,
4348 } ;
4449
45- let index_bytes = bincode:: serialize ( & index_pack) . map_err ( |e| DbError :: SerializationError ( e. to_string ( ) ) ) ?;
50+ let index_bytes = bincode:: serialize ( & index_pack)
51+ . map_err ( |e| DbError :: SerializationError ( e. to_string ( ) ) ) ?;
4652 buffer. extend ( index_bytes) ;
4753
4854 return Ok ( buffer) ;
@@ -54,10 +60,11 @@ impl SerializableIndex for HnswIndex {
5460 ef_construction : self . ef_construction ,
5561 data_dimension : self . data_dimension ,
5662 ef : self . ef ,
57- similarity : self . similarity
63+ similarity : self . similarity ,
5864 } ;
5965
60- let metadata_bytes = bincode:: serialize ( & index_pack) . map_err ( |e| DbError :: SerializationError ( e. to_string ( ) ) ) ?;
66+ let metadata_bytes = bincode:: serialize ( & index_pack)
67+ . map_err ( |e| DbError :: SerializationError ( e. to_string ( ) ) ) ?;
6168 buffer. extend ( metadata_bytes) ;
6269 return Ok ( buffer) ;
6370 }
@@ -76,15 +83,18 @@ impl SerializableIndex for HnswIndex {
7683 fn populate_vectors ( & mut self , storage : & dyn StorageEngine ) -> Result < ( ) , DbError > {
7784 // assumes index topology is restored
7885 for id in self . index . nodes . keys ( ) {
79- let vec = storage. get_vector ( * id) ?. ok_or ( DbError :: SerializationError ( format ! ( "Failed to locate vector for id: {} in storage" , id) ) ) ?;
86+ let vec = storage
87+ . get_vector ( * id) ?
88+ . ok_or ( DbError :: SerializationError ( format ! (
89+ "Failed to locate vector for id: {} in storage" ,
90+ id
91+ ) ) ) ?;
8092 self . cache . insert ( * id, vec) ;
8193 }
8294 Ok ( ( ) )
8395 }
8496}
8597
86-
87-
8898impl HnswIndex {
8999 pub fn deserialize (
90100 IndexSnapshot {
@@ -110,7 +120,7 @@ impl HnswIndex {
110120 DbError :: SerializationError ( format ! ( "Failed to deserialize HNSW Metadata: {}" , e) )
111121 } ) ?;
112122
113- let index_pack : HnswIndexPack = bincode:: deserialize ( topology_b) . map_err ( |e| {
123+ let index_pack: HnswIndexPack = bincode:: deserialize ( topology_b) . map_err ( |e| {
114124 DbError :: SerializationError ( format ! ( "Failed to deserialize HNSW Index: {}" , e) )
115125 } ) ?;
116126
@@ -121,24 +131,23 @@ impl HnswIndex {
121131 points_by_layer : index_pack. points_by_layer ,
122132 entry_point : index_pack. entry_point ,
123133 nodes : HashMap :: new ( ) ,
124- level_generator : LevelGenerator {
125- level_scale : index_pack. level_scale
126- }
134+ level_generator : LevelGenerator {
135+ level_scale : index_pack. level_scale ,
136+ } ,
127137 } ;
128138
129139 // restore nodes hashmap
130140 for i in index_pack. nodes {
131141 hnsw_index_restored. nodes . insert ( i. id , i) ;
132142 }
133143
134-
135144 let hnsw = HnswIndex {
136- ef_construction : metadata. ef_construction ,
145+ ef_construction : metadata. ef_construction ,
137146 data_dimension : metadata. data_dimension ,
138147 ef : metadata. ef ,
139148 cache : HashMap :: new ( ) ,
140149 similarity : metadata. similarity ,
141- index : hnsw_index_restored
150+ index : hnsw_index_restored,
142151 } ;
143152
144153 Ok ( hnsw)
0 commit comments