@@ -133,8 +133,13 @@ private static CentroidIterator getCentroidIteratorWithParents(
133133 OptimizedScalarQuantizer .QuantizationResult queryParams ,
134134 float globalCentroidDp
135135 ) throws IOException {
136- final int maxChildrenSize = centroids . readVInt ();
136+ // build the three queues we are going to use
137137 final NeighborQueue parentsQueue = new NeighborQueue (numParents , true );
138+ final int maxChildrenSize = centroids .readVInt ();
139+ final NeighborQueue currentParentQueue = new NeighborQueue (maxChildrenSize , true );
140+ final int bufferSize = (int ) Math .max (numCentroids * CENTROID_SAMPLING_PERCENTAGE , 1 );
141+ final NeighborQueue neighborQueue = new NeighborQueue (bufferSize , true );
142+ // score the parents
138143 final float [] scores = new float [ES91Int4VectorsScorer .BULK_SIZE ];
139144 score (
140145 parentsQueue ,
@@ -147,14 +152,12 @@ private static CentroidIterator getCentroidIteratorWithParents(
147152 fieldInfo .getVectorSimilarityFunction (),
148153 scores
149154 );
150- final int bufferSize = (int ) Math .max (numCentroids * CENTROID_SAMPLING_PERCENTAGE , 1 );
151- long centroidQuantizeSize = fieldInfo .getVectorDimension () + 3 * Float .BYTES + Short .BYTES ;
152- long offset = centroids .getFilePointer ();
153- long childrenOffset = offset + (long ) Long .BYTES * numParents ;
154- NeighborQueue currentParentQueue = new NeighborQueue (maxChildrenSize , true );
155- NeighborQueue neighborQueue = new NeighborQueue (bufferSize , true );
155+ final long centroidQuantizeSize = fieldInfo .getVectorDimension () + 3 * Float .BYTES + Short .BYTES ;
156+ final long offset = centroids .getFilePointer ();
157+ final long childrenOffset = offset + (long ) Long .BYTES * numParents ;
158+ // populate the children's queue by reading parents one by one
156159 while (parentsQueue .size () > 0 && neighborQueue .size () < bufferSize ) {
157- int pop = parentsQueue .pop ();
160+ final int pop = parentsQueue .pop ();
158161 populateOneChildrenGroup (
159162 currentParentQueue ,
160163 centroids ,
@@ -169,13 +172,12 @@ private static CentroidIterator getCentroidIteratorWithParents(
169172 scores
170173 );
171174 while (currentParentQueue .size () > 0 && neighborQueue .size () < bufferSize ) {
172- float score = currentParentQueue .topScore ();
173- int children = currentParentQueue .pop ();
175+ final float score = currentParentQueue .topScore ();
176+ final int children = currentParentQueue .pop ();
174177 neighborQueue .add (children , score );
175178 }
176179 }
177- long childrenFileOffsets = childrenOffset + centroidQuantizeSize * numCentroids ;
178-
180+ final long childrenFileOffsets = childrenOffset + centroidQuantizeSize * numCentroids ;
179181 return new CentroidIterator () {
180182 @ Override
181183 public boolean hasNext () {
@@ -185,34 +187,34 @@ public boolean hasNext() {
185187 @ Override
186188 public long nextPostingListOffset () throws IOException {
187189 int centroidOrdinal = neighborQueue .pop ();
188- updateQueue ();
190+ updateQueue (); // add one children if available so the queue remains fully populated
189191 centroids .seek (childrenFileOffsets + (long ) Long .BYTES * centroidOrdinal );
190192 return centroids .readLong ();
191193 }
192194
193195 private void updateQueue () throws IOException {
194196 if (currentParentQueue .size () > 0 ) {
197+ // add a children from the current parent queue
195198 float score = currentParentQueue .topScore ();
196199 int children = currentParentQueue .pop ();
197200 neighborQueue .add (children , score );
198- } else {
199- if (parentsQueue .size () > 0 ) {
200- int pop = parentsQueue .pop ();
201- populateOneChildrenGroup (
202- currentParentQueue ,
203- centroids ,
204- offset + 2L * Integer .BYTES * pop ,
205- childrenOffset ,
206- centroidQuantizeSize ,
207- fieldInfo ,
208- scorer ,
209- quantizeQuery ,
210- queryParams ,
211- globalCentroidDp ,
212- scores
213- );
214- updateQueue ();
215- }
201+ } else if (parentsQueue .size () > 0 ) {
202+ // add a new parent from the current parent queue
203+ int pop = parentsQueue .pop ();
204+ populateOneChildrenGroup (
205+ currentParentQueue ,
206+ centroids ,
207+ offset + 2L * Integer .BYTES * pop ,
208+ childrenOffset ,
209+ centroidQuantizeSize ,
210+ fieldInfo ,
211+ scorer ,
212+ quantizeQuery ,
213+ queryParams ,
214+ globalCentroidDp ,
215+ scores
216+ );
217+ updateQueue ();
216218 }
217219 }
218220 };
0 commit comments