mayya-sharipova commented on code in PR #17047:
URL: https://github.com/apache/lucene/pull/17047#discussion_r4647378553


##########
lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99FlatVectorsWriter.java:
##########
@@ -332,6 +342,141 @@ public void mergeOneFlatVectorField(FieldInfo fieldInfo, 
MergeState mergeState)
         docsWithField);
   }
 
+  /**
+   * Merges a field by copying the stored bytes of each source segment's 
vectors, without decoding
+   * and re-encoding them. Every source must expose the field through one of 
the off-heap value
+   * classes that this format's reader creates, which read vector {@code ord} 
as the {@code
+   * vectorByteLength} little-endian bytes at {@code ord * vectorByteLength} 
of their slice: exactly
+   * what the per-vector path would write.
+   *
+   * <p>Documents are visited in the same order as {@code 
KnnVectorsWriter.MergedVectorValues}, and
+   * runs of consecutive live ordinals are copied in one call.
+   *
+   * @return the merged documents with a vector, or {@code null}, having 
written nothing, if the
+   *     merge sorts documents or any source does not qualify
+   */
+  private DocsWithFieldSet copyRawVectorData(FieldInfo fieldInfo, MergeState 
mergeState)
+      throws IOException {
+    if (mergeState.needsIndexSort) {
+      // documents of different segments interleave, so ordinals do not form 
runs
+      return null;
+    }
+    final int dimension = fieldInfo.getVectorDimension();
+    final long vectorByteLength = (long) dimension * 
fieldInfo.getVectorEncoding().byteSize;
+    final List<KnnVectorValues> sources = new ArrayList<>();
+    final List<MergeState.DocMap> docMaps = new ArrayList<>();
+    for (int i = 0; i < mergeState.knnVectorsReaders.length; i++) {
+      if (MergedVectorValues.hasVectorValues(mergeState.fieldInfos[i], 
fieldInfo.name) == false) {
+        continue;
+      }
+      KnnVectorsReader reader = mergeState.knnVectorsReaders[i];
+      if (reader == null) {
+        continue;
+      }
+      KnnVectorValues values =
+          switch (fieldInfo.getVectorEncoding()) {
+            case BYTE -> reader.getByteVectorValues(fieldInfo.name);
+            case FLOAT16 -> reader.getFloat16VectorValues(fieldInfo.name);
+            case FLOAT32 -> reader.getFloatVectorValues(fieldInfo.name);
+          };
+      if (values == null || values.size() == 0) {
+        continue; // contributes no vectors; empty off-heap values have no 
slice
+      }
+      if (isStoredLayout(values, dimension, vectorByteLength) == false) {
+        return null;
+      }
+      sources.add(values);
+      docMaps.add(mergeState.docMaps[i]);
+    }
+
+    final DocsWithFieldSet docsWithField = new DocsWithFieldSet();
+    final StagedCopier copier = new StagedCopier(vectorData);
+    for (int i = 0; i < sources.size(); i++) {
+      final KnnVectorValues values = sources.get(i);
+      final MergeState.DocMap docMap = docMaps.get(i);
+      final IndexInput slice = ((HasIndexSlice) values).getSlice().clone();
+      final KnnVectorValues.DocIndexIterator iterator = values.iterator();
+      // the run of ordinals [runStart, runEnd) not yet copied
+      int runStart = 0;
+      int runEnd = 0;
+      for (int doc = iterator.nextDoc(); doc != NO_MORE_DOCS; doc = 
iterator.nextDoc()) {
+        int mappedDoc = docMap.get(doc);
+        if (mappedDoc == -1) {
+          continue; // deleted
+        }
+        int ord = iterator.index();
+        if (ord != runEnd) {
+          copier.copy(slice, runStart * vectorByteLength, (runEnd - runStart) 
* vectorByteLength);
+          runStart = ord;
+        }
+        runEnd = ord + 1;
+        docsWithField.add(mappedDoc);
+      }
+      copier.copy(slice, runStart * vectorByteLength, (runEnd - runStart) * 
vectorByteLength);
+    }
+    copier.flush();
+    return docsWithField;
+  }
+
+  /**
+   * Gathers copied byte ranges in a buffer and writes it to the output only 
when it is full. Runs
+   * between deleted documents can be a few kilobytes; writing each one 
directly would flush the
+   * output's buffer partly filled, and issue more and smaller writes to the 
file than the
+   * per-vector path does.
+   */
+  private static final class StagedCopier {

Review Comment:
   This was necessary when we had deleted docs. But to make code simpler, I've 
decided to remove this class and use buffering in OutputStreamIndexOutput, but 
skip copying row vector data with deleted docs.
   
   Addressed in 36de3525b90e56d9f55ba4efa755a60e1c362d48



-- 
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]

Reply via email to