1818import org .apache .lucene .util .ArrayUtil ;
1919import org .apache .lucene .util .Bits ;
2020import org .apache .lucene .util .VectorUtil ;
21- import org .apache . lucene . util . hnsw .NeighborQueue ;
21+ import org .elasticsearch . index . codec . vectors . cluster .NeighborQueue ;
2222import org .elasticsearch .index .codec .vectors .reflect .OffHeapStats ;
2323import org .elasticsearch .simdvec .ES91OSQVectorsScorer ;
2424import org .elasticsearch .simdvec .ES92Int7VectorsScorer ;
@@ -243,22 +243,19 @@ public boolean hasNext() {
243243
244244 @ Override
245245 public CentroidOffsetAndLength nextPostingListOffsetAndLength () throws IOException {
246- int centroidOrdinal = neighborQueue .pop ();
247- updateQueue (); // add one children if available so the queue remains fully populated
246+ int centroidOrdinal = nextCentroid ();
248247 centroids .seek (childrenFileOffsets + (long ) Long .BYTES * 2 * centroidOrdinal );
249248 long postingListOffset = centroids .readLong ();
250249 long postingListLength = centroids .readLong ();
251250 return new CentroidOffsetAndLength (postingListOffset , postingListLength );
252251 }
253252
254- private void updateQueue () throws IOException {
253+ private int nextCentroid () throws IOException {
255254 if (currentParentQueue .size () > 0 ) {
256- // add a children from the current parent queue
257- float score = currentParentQueue .topScore ();
258- int children = currentParentQueue .pop ();
259- neighborQueue .add (children , score );
255+ // return next centroid and add a children from the current parent queue
256+ return neighborQueue .popAndAddRaw (currentParentQueue .popRaw ());
260257 } else if (parentsQueue .size () > 0 ) {
261- // add a new parent from the current parent queue
258+ // current parent queue is empty, populate it again with the next parent
262259 int pop = parentsQueue .pop ();
263260 populateOneChildrenGroup (
264261 currentParentQueue ,
@@ -273,7 +270,9 @@ private void updateQueue() throws IOException {
273270 globalCentroidDp ,
274271 scores
275272 );
276- updateQueue ();
273+ return nextCentroid ();
274+ } else {
275+ return neighborQueue .pop ();
277276 }
278277 }
279278 };
0 commit comments