atris commented on code in PR #15613:
URL: https://github.com/apache/lucene/pull/15613#discussion_r2733123303


##########
lucene/core/src/java/org/apache/lucene/codecs/spann/Lucene99SpannVectorsWriter.java:
##########
@@ -0,0 +1,246 @@
+/*
+ * 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.codecs.spann;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.lucene.codecs.CodecUtil;
+import org.apache.lucene.codecs.KnnFieldVectorsWriter;
+import org.apache.lucene.codecs.KnnVectorsFormat;
+import org.apache.lucene.codecs.KnnVectorsWriter;
+import org.apache.lucene.index.FieldInfo;
+import org.apache.lucene.index.IndexFileNames;
+import org.apache.lucene.index.SegmentWriteState;
+import org.apache.lucene.index.Sorter;
+import org.apache.lucene.index.VectorEncoding;
+import org.apache.lucene.index.VectorSimilarityFunction;
+import org.apache.lucene.store.IndexOutput;
+import org.apache.lucene.util.IOUtils;
+
+/**
+ * Writes vectors in the SPANN (HNSW-IVF) format.
+ *
+ * <p>Centroids are computed via K-Means and indexed into an HNSW-based coarse 
quantizer. Vector
+ * data is assigned to the nearest centroid and written sequentially in a 
clustered format.
+ */
+public class Lucene99SpannVectorsWriter extends KnnVectorsWriter {
+
+  private static final int KMEANS_MAX_ITERS = 20;
+
+  private final SegmentWriteState state;
+  private final KnnVectorsWriter centroidDelegate;
+  private final Map<String, SpannFieldVectorsWriter> fieldWriters = new 
HashMap<>();
+  private final int maxPartitions;
+  private final int clusteringSampleSize;
+
+  public Lucene99SpannVectorsWriter(
+      SegmentWriteState state,
+      KnnVectorsFormat centroidFormat,
+      int maxPartitions,
+      int clusteringSampleSize)
+      throws IOException {
+    this.state = state;
+    this.centroidDelegate = centroidFormat.fieldsWriter(state);
+    this.maxPartitions = maxPartitions;
+    this.clusteringSampleSize = clusteringSampleSize;
+  }
+
+  @Override
+  public KnnFieldVectorsWriter<?> addField(FieldInfo fieldInfo) throws 
IOException {
+    SpannFieldVectorsWriter writer = new SpannFieldVectorsWriter(fieldInfo);
+    fieldWriters.put(fieldInfo.name, writer);
+
+    if (fieldInfo.getVectorEncoding() == VectorEncoding.BYTE) {
+      return new KnnFieldVectorsWriter<byte[]>() {
+        @Override
+        public void addValue(int docID, byte[] vectorValue) throws IOException 
{
+          float[] floats = new float[vectorValue.length];
+          for (int i = 0; i < vectorValue.length; i++) {
+            floats[i] = (float) vectorValue[i];
+          }
+          writer.addValue(docID, floats);
+        }
+
+        @Override
+        public byte[] copyValue(byte[] vectorValue) {
+          return vectorValue.clone();
+        }
+
+        @Override
+        public long ramBytesUsed() {
+          return 0; // The delegate writer tracks usage
+        }
+      };
+    }
+
+    return writer;
+  }
+
+  @Override
+  public void flush(int maxDoc, Sorter.DocMap sortMap) throws IOException {
+
+    for (Map.Entry<String, SpannFieldVectorsWriter> entry : 
fieldWriters.entrySet()) {
+      String fieldName = entry.getKey();
+      SpannFieldVectorsWriter writer = entry.getValue();
+      FieldInfo fieldInfo = writer.getFieldInfo();
+
+      if (writer.getVectors().isEmpty()) {
+        continue;
+      }
+
+      float[][] vectorArray = writer.getVectors().toArray(new float[0][]);
+
+      // Cap the number of partitions at configured limit (default 100)
+      int numPartitions = Math.min(vectorArray.length, maxPartitions);
+
+      // Downsample to keep flush time constant

Review Comment:
   Refactored the reservoir sampling logic out of the Writer and into 
`SpannKMeans#downsample`.



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