File tree Expand file tree Collapse file tree 1 file changed +14
-4
lines changed
crates/codegraph-vector/src Expand file tree Collapse file tree 1 file changed +14
-4
lines changed Original file line number Diff line number Diff line change @@ -75,16 +75,25 @@ impl SimpleFaissManager {
7575 return Ok ( ( ) ) ;
7676 }
7777
78- // Prepare flat vector array for FAISS
7978 let flat_vectors: Vec < f32 > = vectors
8079 . iter ( )
8180 . flat_map ( |( _, embedding) | embedding. iter ( ) . cloned ( ) )
8281 . collect ( ) ;
8382
84- // Add to FAISS index
8583 let mut index_guard = self . index . write ( ) ;
8684 let index = index_guard. as_mut ( ) . unwrap ( ) ;
8785
86+ // CRITICAL FIX: Train the index if it's not already trained
87+ if !index. is_trained ( ) && vectors. len ( ) >= self . config . training_threshold {
88+ info ! (
89+ "Training FAISS index with {} vectors..." ,
90+ flat_vectors. len( ) / self . config. dimension
91+ ) ;
92+ index
93+ . train ( & flat_vectors)
94+ . map_err ( |e| CodeGraphError :: Vector ( format ! ( "Failed to train index: {}" , e) ) ) ?;
95+ }
96+
8897 index
8998 . add ( & flat_vectors)
9099 . map_err ( |e| CodeGraphError :: Vector ( format ! ( "Failed to add vectors: {}" , e) ) ) ?;
@@ -119,9 +128,10 @@ impl SimpleFaissManager {
119128 ) ) ) ;
120129 }
121130
122- let mut index_guard = self . index . write ( ) ;
131+ // Use a read lock for concurrent searches
132+ let index_guard = self . index . read ( ) ;
123133 let index = index_guard
124- . as_mut ( )
134+ . as_ref ( )
125135 . ok_or_else ( || CodeGraphError :: Vector ( "Index not initialized" . to_string ( ) ) ) ?;
126136
127137 let search_result = index
You can’t perform that action at this time.
0 commit comments