atris commented on code in PR #15613: URL: https://github.com/apache/lucene/pull/15613#discussion_r2733126588
########## lucene/core/src/java/org/apache/lucene/codecs/spann/Lucene99SpannVectorsReader.java: ########## @@ -0,0 +1,470 @@ +/* + * 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.HashMap; +import java.util.Map; +import org.apache.lucene.codecs.CodecUtil; +import org.apache.lucene.codecs.KnnVectorsFormat; +import org.apache.lucene.codecs.KnnVectorsReader; +import org.apache.lucene.index.ByteVectorValues; +import org.apache.lucene.index.FieldInfo; +import org.apache.lucene.index.FloatVectorValues; +import org.apache.lucene.index.IndexFileNames; +import org.apache.lucene.index.SegmentReadState; +import org.apache.lucene.index.VectorEncoding; +import org.apache.lucene.search.AcceptDocs; +import org.apache.lucene.search.DocIdSetIterator; +import org.apache.lucene.search.KnnCollector; +import org.apache.lucene.search.ScoreDoc; +import org.apache.lucene.search.TopDocs; +import org.apache.lucene.search.TopKnnCollector; +import org.apache.lucene.search.VectorScorer; +import org.apache.lucene.store.IndexInput; +import org.apache.lucene.util.Bits; +import org.apache.lucene.util.IOUtils; + +/** + * Reader for SPANN (HNSW-IVF) vectors. + * + * @lucene.experimental + */ +public class Lucene99SpannVectorsReader extends KnnVectorsReader { + + private final KnnVectorsReader centroidDelegate; + private final Map<String, SpannFieldEntry> fields = new HashMap<>(); + private final int nprobe; + + public Lucene99SpannVectorsReader( + SegmentReadState state, KnnVectorsFormat centroidFormat, int nprobe) throws IOException { + this.nprobe = nprobe; + this.centroidDelegate = centroidFormat.fieldsReader(state); + + for (FieldInfo fieldInfo : state.fieldInfos) { + if (fieldInfo.hasVectorValues()) { + String metaFileName = + IndexFileNames.segmentFileName( + state.segmentInfo.name, state.segmentSuffix, fieldInfo.name + ".spam"); + String dataFileName = + IndexFileNames.segmentFileName( + state.segmentInfo.name, state.segmentSuffix, fieldInfo.name + ".spad"); + + IndexInput metaIn = null; + IndexInput dataIn = null; + boolean success = false; + + try { + // Probe for the "spam" (Meta) file. + // If it's missing, this field is likely managed by another codec (e.g., HNSW). + // We must catch this to allow mixed-codec segments. + metaIn = state.directory.openInput(metaFileName, state.context); + } catch (java.nio.file.NoSuchFileException | java.io.FileNotFoundException _) { + continue; + } + + try { + // If we found the meta file, we assume this IS a SPANN field. + // Any subsequent missing files (like .spad) or corruption MUST throw an + // exception. + CodecUtil.checkIndexHeader( + metaIn, "Lucene99SpannMeta", 0, 0, state.segmentInfo.getId(), state.segmentSuffix); + + int totalSize = metaIn.readVInt(); + Map<Integer, Long> offsets = new HashMap<>(); + Map<Integer, Long> lengths = new HashMap<>(); + + while (metaIn.getFilePointer() < metaIn.length() - CodecUtil.footerLength()) { + int partitionId = metaIn.readVInt(); + long offset = metaIn.readVLong(); + long length = metaIn.readVLong(); + offsets.put(partitionId, offset); + lengths.put(partitionId, length); + } + + dataIn = state.directory.openInput(dataFileName, state.context); + CodecUtil.checkIndexHeader( + dataIn, "Lucene99SpannData", 0, 0, state.segmentInfo.getId(), state.segmentSuffix); + + // Build ordinal map for random access vectorValue(ord) + int[] ordToDocId = new int[totalSize]; + long[] ordToOffset = new long[totalSize]; + int currentOrd = 0; + IndexInput dataInClone = dataIn.clone(); + int vectorByteWidth = + fieldInfo.getVectorEncoding() == VectorEncoding.BYTE + ? fieldInfo.getVectorDimension() + : fieldInfo.getVectorDimension() * Float.BYTES; + + for (Integer pId : offsets.keySet()) { + long start = offsets.get(pId); + long len = lengths.get(pId); + dataInClone.seek(start); + long end = start + len; + while (dataInClone.getFilePointer() < end) { + ordToDocId[currentOrd] = dataInClone.readInt(); + ordToOffset[currentOrd] = dataInClone.getFilePointer(); + currentOrd++; + dataInClone.skipBytes(vectorByteWidth); + } + } + + fields.put( + fieldInfo.name, + new SpannFieldEntry( + offsets, lengths, dataIn, fieldInfo, totalSize, ordToDocId, ordToOffset)); + success = true; + } finally { + if (!success) { + IOUtils.closeWhileHandlingException(metaIn, dataIn); + } else { + IOUtils.closeWhileHandlingException(metaIn); + } + } + } + } + } + + @Override + public void checkIntegrity() throws IOException { + centroidDelegate.checkIntegrity(); + + for (SpannFieldEntry entry : fields.values()) { + // Meta check is done on open + CodecUtil.checksumEntireFile(entry.dataIn); + } + } + + @Override + public FloatVectorValues getFloatVectorValues(String field) throws IOException { + SpannFieldEntry entry = fields.get(field); + if (entry == null) { + return centroidDelegate.getFloatVectorValues(field); + } + if (entry.fieldInfo.getVectorEncoding() != VectorEncoding.FLOAT32) { + return null; + } + return new SpannFloatVectorValues(entry); + } + + @Override + public ByteVectorValues getByteVectorValues(String field) throws IOException { + SpannFieldEntry entry = fields.get(field); + if (entry == null) { + return centroidDelegate.getByteVectorValues(field); + } + if (entry.fieldInfo.getVectorEncoding() != VectorEncoding.BYTE) { + return null; + } + return new SpannByteVectorValues(entry); + } + + @Override + public void search(String field, float[] target, KnnCollector knnCollector, AcceptDocs acceptDocs) + throws IOException { + SpannFieldEntry entry = fields.get(field); + if (entry == null) { + centroidDelegate.search(field, target, knnCollector, acceptDocs); + return; + } + + TopDocs topCentroids; + if (entry.fieldInfo.getVectorEncoding() == VectorEncoding.BYTE) { + byte[] byteTarget = new byte[target.length]; + for (int i = 0; i < target.length; i++) { + byteTarget[i] = (byte) target[i]; + } + topCentroids = searchCentroids(field, byteTarget, acceptDocs); + } else { + topCentroids = searchCentroids(field, target, acceptDocs); + } + + searchFine(entry, target, topCentroids, knnCollector, acceptDocs); + } + + @Override + public void search(String field, byte[] target, KnnCollector knnCollector, AcceptDocs acceptDocs) + throws IOException { + SpannFieldEntry entry = fields.get(field); + if (entry == null) { + centroidDelegate.search(field, target, knnCollector, acceptDocs); + return; + } + + TopDocs topCentroids; + float[] floatTarget = new float[target.length]; + for (int i = 0; i < target.length; i++) { + floatTarget[i] = (float) target[i]; + } Review Comment: The original loop was casting 8-bit integer dimensions to floats, not reinterpreting a byte stream. However, to eliminate this ambiguity entirely and align with other formats, I’ve removed the mixed-type support. `search(byte[])` now strictly throws if run against a FLOAT32 index. -- 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]
