LantaoJin commented on code in PR #16214: URL: https://github.com/apache/lucene/pull/16214#discussion_r3377642047
########## lucene/core/src/java/org/apache/lucene/index/KnnVectorFieldUpdates.java: ########## @@ -0,0 +1,335 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.lucene.index; + +import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS; + +import java.util.Comparator; +import org.apache.lucene.search.DocIdSetIterator; +import org.apache.lucene.util.ArrayUtil; +import org.apache.lucene.util.PriorityQueue; + +/** + * Holds in-place KNN vector updates of a single field, for a set of documents within one segment. + * This is the vector analogue of {@link DocValuesFieldUpdates}: it accumulates (docID -> new + * vector) entries during resolution, sorts them by docID at {@link #finish()} (stable, so that the + * last update to a docID within a packet wins), and exposes an {@link Iterator} that the write path + * overlays onto the existing vectors. + * + * @lucene.experimental + */ +abstract class KnnVectorFieldUpdates { + + final String field; + final VectorEncoding encoding; + final long delGen; + final int maxDoc; + final int dimension; + protected int[] docs = new int[8]; + protected int size; + private boolean finished; + + protected KnnVectorFieldUpdates( + int maxDoc, long delGen, String field, VectorEncoding encoding, int dimension) { + this.maxDoc = maxDoc; + this.delGen = delGen; + this.field = field; + this.encoding = encoding; + this.dimension = dimension; + } + + /** Reserves a slot for the next doc and returns its ordinal. Subclasses store the value. */ + protected final int reserve(int doc) { + if (finished) { + throw new IllegalStateException("already finished"); + } + assert doc < maxDoc; + if (size == docs.length) { + docs = ArrayUtil.grow(docs, size + 1); + } + docs[size] = doc; + return size++; + } + + boolean getFinished() { + return finished; + } + + boolean any() { + return size > 0; + } + + /** Freezes structures and stably sorts updates by docID (last write to a docID wins). */ + final void finish() { Review Comment: Agreed. Replaced with the in-place `IntroSorter` + `int[] ords` pattern from `DocValuesFieldUpdates.finish()` -- stable tie-break preserved, zero boxing, no extra arrays. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
