kaivalnp commented on code in PR #15979:
URL: https://github.com/apache/lucene/pull/15979#discussion_r3668177051


##########
lucene/core/src/java/org/apache/lucene/codecs/lucene106/dedup/DedupFlatVectorsFormat.java:
##########
@@ -0,0 +1,133 @@
+/*
+ * 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.lucene106.dedup;
+
+import java.io.IOException;
+import org.apache.lucene.codecs.hnsw.FlatVectorsFormat;
+import org.apache.lucene.codecs.hnsw.FlatVectorsReader;
+import org.apache.lucene.codecs.hnsw.FlatVectorsWriter;
+import org.apache.lucene.index.SegmentReadState;
+import org.apache.lucene.index.SegmentWriteState;
+
+/**
+ * Flat vector format that stores each distinct vector once.
+ *
+ * <p>Vectors that share the same dimension and encoding form a <i>group</i>. 
Within a group, an
+ * identical vector is stored a single time regardless of how many documents 
(across all fields that
+ * map to that group) reference it; each field then keeps a per-document 
{@code ordToVecOrd} map

Review Comment:
   > Remove per-document?
   
   Sure, done!
   
   > Should we rename to `docToVecOrd`? It's mapping `docid` to vector ordinal?
   
   This mapping is from field ordinal to group ordinal, renamed to 
`fieldOrdToGroupOrd`.
   
   Within a vector format, everything is in ordinal space (vector addressing, 
HNSW graph edges, etc). Each field has a separate monotonic `ordToDoc` mapping 
to go from field ordinals to Lucene `docid`.



##########
lucene/core/src/java/org/apache/lucene/codecs/lucene106/dedup/DedupUtil.java:
##########
@@ -0,0 +1,699 @@
+/*
+ * 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.lucene106.dedup;
+
+import static org.apache.lucene.index.VectorEncoding.BYTE;
+import static org.apache.lucene.index.VectorEncoding.FLOAT16;
+import static org.apache.lucene.index.VectorEncoding.FLOAT32;
+import static org.apache.lucene.util.StringHelper.GOOD_FAST_HASH_SEED;
+import static org.apache.lucene.util.StringHelper.murmurhash3_x64_128;
+
+import java.io.IOException;
+import org.apache.lucene.codecs.hnsw.FlatVectorsScorer;
+import org.apache.lucene.codecs.lucene95.OffHeapByteVectorValues;
+import org.apache.lucene.codecs.lucene95.OffHeapFloat16VectorValues;
+import org.apache.lucene.codecs.lucene95.OffHeapFloatVectorValues;
+import org.apache.lucene.codecs.lucene95.OrdToDocDISIReaderConfiguration;
+import org.apache.lucene.index.ByteVectorValues;
+import org.apache.lucene.index.DocsWithFieldSet;
+import org.apache.lucene.index.FieldInfo;
+import org.apache.lucene.index.Float16VectorValues;
+import org.apache.lucene.index.FloatVectorValues;
+import org.apache.lucene.index.KnnVectorValues;
+import org.apache.lucene.index.VectorEncoding;
+import org.apache.lucene.index.VectorSimilarityFunction;
+import org.apache.lucene.internal.hppc.IntArrayList;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.search.VectorScorer;
+import org.apache.lucene.store.IndexInput;
+import org.apache.lucene.store.IndexOutput;
+import org.apache.lucene.store.RandomAccessInput;
+import org.apache.lucene.util.ArrayUtil;
+import org.apache.lucene.util.LongValues;
+import org.apache.lucene.util.hnsw.RandomVectorScorer;
+import org.apache.lucene.util.packed.DirectReader;
+import org.apache.lucene.util.packed.DirectWriter;
+
+/**
+ * Shared helpers for the de-duplicating flat format: reading / writing field 
and group metadata,
+ * vector hashing and alignment, and the {@link DedupVectorValues} views used 
on the read path.
+ *
+ * @lucene.experimental
+ */
+final class DedupUtil {
+
+  private static final int DIRECT_MONOTONIC_BLOCK_SHIFT = 16;
+
+  private static final int END_MARKER = -1;
+
+  private static final int ORD_TO_VEC_ALIGN_BYTES = 4;
+
+  // TODO: This is the number of bits used to write each group ordinal in the 
index-backed per-field
+  //  OrdToVecOrd mapping. Evaluate using fewer bits to reduce index size, at 
the expense of
+  //  costlier lookups.
+  private static final int ORD_TO_VEC_BITS_PER_VALUE = 32;
+
+  static final int SCRATCH_SIZE = 16;
+
+  /** Key used to group vectors (dimension + encoding). */
+  record GroupKey(int dimension, VectorEncoding encoding) {
+    GroupKey(FieldInfo fieldInfo) {
+      this(fieldInfo.getVectorDimension(), fieldInfo.getVectorEncoding());
+    }
+  }
+
+  /**
+   * Vector values that share a single copy of each distinct vector across the 
documents and fields
+   * that reference it.
+   *
+   * <p>Every instance is backed by two views: the {@code fieldView} maps 
ordinals to docs and
+   * drives iteration (one entry per document), while the {@code groupView} 
holds the de-duplicated
+   * vectors (one entry per distinct vector). {@code ordToVecOrd} translates a 
document ordinal into
+   * its group ordinal.
+   */
+  sealed interface DedupVectorValues {
+    /** The dense view over distinct vectors, indexed by group ordinal. */
+    KnnVectorValues getGroupView();
+
+    /** Maps a per-document ordinal to its group ordinal in {@link 
#getGroupView()}. */
+    OrdToVecOrd getOrdToVecOrd();
+  }
+
+  /**
+   * Maps a field's per-document ordinal to the ordinal of its (shared) vector 
within the group.
+   * Backed on-heap while writing and off-heap while reading.
+   */
+  sealed interface OrdToVecOrd {
+    int get(int ord);
+
+    OrdToVecOrd copy() throws IOException;
+  }
+
+  record GroupInfo(
+      int groupOrd,
+      int dimension,
+      VectorEncoding encoding,
+      int groupSize,
+      long vectorDataOffset,
+      long vectorDataSize) {}
+
+  static void writeGroupInfo(IndexOutput meta, GroupInfo groupInfo) throws 
IOException {
+    meta.writeInt(groupInfo.groupOrd);
+    meta.writeInt(groupInfo.dimension);
+    meta.writeInt(groupInfo.encoding.ordinal());
+    meta.writeInt(groupInfo.groupSize);
+    meta.writeLong(groupInfo.vectorDataOffset);
+    meta.writeLong(groupInfo.vectorDataSize);
+  }
+
+  static void writeEndOfGroups(IndexOutput meta) throws IOException {
+    meta.writeInt(END_MARKER);
+  }
+
+  static GroupInfo readGroupInfo(IndexInput meta) throws IOException {
+    int groupOrd = meta.readInt();
+    if (groupOrd == END_MARKER) {
+      return null;
+    }
+
+    int dimension = meta.readInt();
+    VectorEncoding encoding = VectorEncoding.values()[meta.readInt()];
+    int groupSize = meta.readInt();
+    long vectorDataOffset = meta.readLong();
+    long vectorDataSize = meta.readLong();
+
+    return new GroupInfo(
+        groupOrd, dimension, encoding, groupSize, vectorDataOffset, 
vectorDataSize);
+  }
+
+  record WriteFieldInfo(
+      int fieldNumber,
+      VectorSimilarityFunction function,
+      int dimension,
+      VectorEncoding encoding,
+      int groupOrd,
+      int vectorCount,
+      int maxDoc,
+      DocsWithFieldSet docs,
+      OrdToVecOrd ordToVecOrd) {}
+
+  static void writeFieldInfo(IndexOutput meta, IndexOutput vectorData, 
WriteFieldInfo fieldInfo)
+      throws IOException {
+
+    meta.writeInt(fieldInfo.fieldNumber);
+    meta.writeInt(fieldInfo.function.ordinal());
+    meta.writeInt(fieldInfo.dimension);
+    meta.writeInt(fieldInfo.encoding.ordinal());
+    meta.writeInt(fieldInfo.groupOrd);
+    meta.writeInt(fieldInfo.vectorCount);
+
+    // write ordToDoc
+    OrdToDocDISIReaderConfiguration.writeStoredMeta(
+        DIRECT_MONOTONIC_BLOCK_SHIFT,
+        meta,
+        vectorData,
+        fieldInfo.vectorCount,
+        fieldInfo.maxDoc,
+        fieldInfo.docs);
+
+    // write ordToVec
+    long ordToVecOffset = vectorData.alignFilePointer(ORD_TO_VEC_ALIGN_BYTES);
+    DirectWriter writer =
+        DirectWriter.getInstance(vectorData, fieldInfo.vectorCount, 
ORD_TO_VEC_BITS_PER_VALUE);
+    for (int i = 0; i < fieldInfo.vectorCount; i++) {
+      writer.add(fieldInfo.ordToVecOrd.get(i));
+    }
+    writer.finish();
+    long ordToVecSize = vectorData.getFilePointer() - ordToVecOffset;
+
+    meta.writeLong(ordToVecOffset);
+    meta.writeLong(ordToVecSize);
+  }
+
+  static void writeEndOfFields(IndexOutput meta) throws IOException {
+    meta.writeInt(END_MARKER);
+  }
+
+  record ReadFieldInfo(
+      int fieldNumber,
+      VectorSimilarityFunction function,
+      int dimension,
+      VectorEncoding encoding,
+      int groupOrd,
+      int vectorCount,
+      OrdToDocDISIReaderConfiguration ordToDoc,
+      long ordToVecOffset,
+      long ordToVecSize) {}
+
+  static ReadFieldInfo readFieldInfo(IndexInput meta) throws IOException {
+
+    int fieldNumber = meta.readInt();
+    if (fieldNumber == END_MARKER) {
+      return null;
+    }
+
+    VectorSimilarityFunction function = 
VectorSimilarityFunction.values()[meta.readInt()];
+    int dimension = meta.readInt();
+    VectorEncoding encoding = VectorEncoding.values()[meta.readInt()];
+    int groupOrd = meta.readInt();
+    int vectorCount = meta.readInt();
+    OrdToDocDISIReaderConfiguration ordToDoc =
+        OrdToDocDISIReaderConfiguration.fromStoredMeta(meta, vectorCount);
+    long ordToVecOffset = meta.readLong();
+    long ordToVecSize = meta.readLong();
+
+    return new ReadFieldInfo(
+        fieldNumber,
+        function,
+        dimension,
+        encoding,
+        groupOrd,
+        vectorCount,
+        ordToDoc,
+        ordToVecOffset,
+        ordToVecSize);
+  }
+
+  static long hashBytes(byte[] bytes) {
+    return murmurhash3_x64_128(bytes, 0, bytes.length, GOOD_FAST_HASH_SEED)[0];
+  }
+
+  static long alignBytes(IndexOutput output, VectorEncoding encoding) throws 
IOException {

Review Comment:
   Makes sense, added!



##########
lucene/core/src/java/org/apache/lucene/codecs/lucene106/dedup/DedupUtil.java:
##########
@@ -0,0 +1,699 @@
+/*
+ * 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.lucene106.dedup;
+
+import static org.apache.lucene.index.VectorEncoding.BYTE;
+import static org.apache.lucene.index.VectorEncoding.FLOAT16;
+import static org.apache.lucene.index.VectorEncoding.FLOAT32;
+import static org.apache.lucene.util.StringHelper.GOOD_FAST_HASH_SEED;
+import static org.apache.lucene.util.StringHelper.murmurhash3_x64_128;
+
+import java.io.IOException;
+import org.apache.lucene.codecs.hnsw.FlatVectorsScorer;
+import org.apache.lucene.codecs.lucene95.OffHeapByteVectorValues;
+import org.apache.lucene.codecs.lucene95.OffHeapFloat16VectorValues;
+import org.apache.lucene.codecs.lucene95.OffHeapFloatVectorValues;
+import org.apache.lucene.codecs.lucene95.OrdToDocDISIReaderConfiguration;
+import org.apache.lucene.index.ByteVectorValues;
+import org.apache.lucene.index.DocsWithFieldSet;
+import org.apache.lucene.index.FieldInfo;
+import org.apache.lucene.index.Float16VectorValues;
+import org.apache.lucene.index.FloatVectorValues;
+import org.apache.lucene.index.KnnVectorValues;
+import org.apache.lucene.index.VectorEncoding;
+import org.apache.lucene.index.VectorSimilarityFunction;
+import org.apache.lucene.internal.hppc.IntArrayList;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.search.VectorScorer;
+import org.apache.lucene.store.IndexInput;
+import org.apache.lucene.store.IndexOutput;
+import org.apache.lucene.store.RandomAccessInput;
+import org.apache.lucene.util.ArrayUtil;
+import org.apache.lucene.util.LongValues;
+import org.apache.lucene.util.hnsw.RandomVectorScorer;
+import org.apache.lucene.util.packed.DirectReader;
+import org.apache.lucene.util.packed.DirectWriter;
+
+/**
+ * Shared helpers for the de-duplicating flat format: reading / writing field 
and group metadata,
+ * vector hashing and alignment, and the {@link DedupVectorValues} views used 
on the read path.
+ *
+ * @lucene.experimental
+ */
+final class DedupUtil {
+
+  private static final int DIRECT_MONOTONIC_BLOCK_SHIFT = 16;
+
+  private static final int END_MARKER = -1;
+
+  private static final int ORD_TO_VEC_ALIGN_BYTES = 4;
+
+  // TODO: This is the number of bits used to write each group ordinal in the 
index-backed per-field
+  //  OrdToVecOrd mapping. Evaluate using fewer bits to reduce index size, at 
the expense of
+  //  costlier lookups.
+  private static final int ORD_TO_VEC_BITS_PER_VALUE = 32;
+
+  static final int SCRATCH_SIZE = 16;

Review Comment:
   Makes sense, done!



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