kaivalnp commented on code in PR #15979: URL: https://github.com/apache/lucene/pull/15979#discussion_r3475436329
########## lucene/core/src/java/org/apache/lucene/codecs/dedup/DedupFlatVectorsReader.java: ########## @@ -0,0 +1,421 @@ +/* + * 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.dedup; + +import java.io.IOException; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Stream; +import org.apache.lucene.codecs.CodecUtil; +import org.apache.lucene.codecs.hnsw.FlatVectorsReader; +import org.apache.lucene.codecs.hnsw.FlatVectorsScorer; +import org.apache.lucene.codecs.lucene95.OrdToDocDISIReaderConfiguration; +import org.apache.lucene.index.ByteVectorValues; +import org.apache.lucene.index.CorruptIndexException; +import org.apache.lucene.index.FieldInfo; +import org.apache.lucene.index.FieldInfos; +import org.apache.lucene.index.FloatVectorValues; +import org.apache.lucene.index.IndexFileNames; +import org.apache.lucene.index.KnnVectorValues; +import org.apache.lucene.index.SegmentReadState; +import org.apache.lucene.index.VectorEncoding; +import org.apache.lucene.index.VectorSimilarityFunction; +import org.apache.lucene.internal.hppc.IntObjectHashMap; +import org.apache.lucene.store.ChecksumIndexInput; +import org.apache.lucene.store.DataAccessHint; +import org.apache.lucene.store.FileDataHint; +import org.apache.lucene.store.FileTypeHint; +import org.apache.lucene.store.IOContext; +import org.apache.lucene.store.IOContext.FileOpenHint; +import org.apache.lucene.store.IndexInput; +import org.apache.lucene.store.RandomAccessInput; +import org.apache.lucene.util.IOUtils; +import org.apache.lucene.util.LongValues; +import org.apache.lucene.util.RamUsageEstimator; +import org.apache.lucene.util.hnsw.RandomVectorScorer; +import org.apache.lucene.util.packed.DirectReader; + +/** + * Reads a {@link DedupFlatVectorsFormat} segment. + * + * <p>Layout: {@code .dvc} holds pool vector bytes (contiguous per pool) followed by per-field DISI + * + packed {@code docOrd → vecOrd} maps. {@code .dvm} holds pool/field metadata. + * + * @lucene.experimental + */ +public final class DedupFlatVectorsReader extends FlatVectorsReader { + + private static final long SHALLOW_SIZE = + RamUsageEstimator.shallowSizeOfInstance(DedupFlatVectorsReader.class); + + private final FlatVectorsScorer vectorScorer; + private final FlatVectorsScorer translatingScorer; + private final FieldInfos fieldInfos; + private final IndexInput vectorData; + private final IOContext dataContext; + + private final IntObjectHashMap<FieldEntry> fields = new IntObjectHashMap<>(); + + public DedupFlatVectorsReader(SegmentReadState state, FlatVectorsScorer scorer) + throws IOException { + this(state, scorer, DataAccessHint.RANDOM); Review Comment: Although I didn't get why randomness is amplified here? ########## lucene/core/src/java/org/apache/lucene/codecs/dedup/DedupFlatVectorsReader.java: ########## @@ -0,0 +1,421 @@ +/* + * 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.dedup; + +import java.io.IOException; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Stream; +import org.apache.lucene.codecs.CodecUtil; +import org.apache.lucene.codecs.hnsw.FlatVectorsReader; +import org.apache.lucene.codecs.hnsw.FlatVectorsScorer; +import org.apache.lucene.codecs.lucene95.OrdToDocDISIReaderConfiguration; +import org.apache.lucene.index.ByteVectorValues; +import org.apache.lucene.index.CorruptIndexException; +import org.apache.lucene.index.FieldInfo; +import org.apache.lucene.index.FieldInfos; +import org.apache.lucene.index.FloatVectorValues; +import org.apache.lucene.index.IndexFileNames; +import org.apache.lucene.index.KnnVectorValues; +import org.apache.lucene.index.SegmentReadState; +import org.apache.lucene.index.VectorEncoding; +import org.apache.lucene.index.VectorSimilarityFunction; +import org.apache.lucene.internal.hppc.IntObjectHashMap; +import org.apache.lucene.store.ChecksumIndexInput; +import org.apache.lucene.store.DataAccessHint; +import org.apache.lucene.store.FileDataHint; +import org.apache.lucene.store.FileTypeHint; +import org.apache.lucene.store.IOContext; +import org.apache.lucene.store.IOContext.FileOpenHint; +import org.apache.lucene.store.IndexInput; +import org.apache.lucene.store.RandomAccessInput; +import org.apache.lucene.util.IOUtils; +import org.apache.lucene.util.LongValues; +import org.apache.lucene.util.RamUsageEstimator; +import org.apache.lucene.util.hnsw.RandomVectorScorer; +import org.apache.lucene.util.packed.DirectReader; + +/** + * Reads a {@link DedupFlatVectorsFormat} segment. + * + * <p>Layout: {@code .dvc} holds pool vector bytes (contiguous per pool) followed by per-field DISI + * + packed {@code docOrd → vecOrd} maps. {@code .dvm} holds pool/field metadata. + * + * @lucene.experimental + */ +public final class DedupFlatVectorsReader extends FlatVectorsReader { + + private static final long SHALLOW_SIZE = + RamUsageEstimator.shallowSizeOfInstance(DedupFlatVectorsReader.class); + + private final FlatVectorsScorer vectorScorer; + private final FlatVectorsScorer translatingScorer; + private final FieldInfos fieldInfos; + private final IndexInput vectorData; + private final IOContext dataContext; + + private final IntObjectHashMap<FieldEntry> fields = new IntObjectHashMap<>(); + + public DedupFlatVectorsReader(SegmentReadState state, FlatVectorsScorer scorer) + throws IOException { + this(state, scorer, DataAccessHint.RANDOM); + } + + public DedupFlatVectorsReader( + SegmentReadState state, FlatVectorsScorer scorer, DataAccessHint accessHint) + throws IOException { + this.vectorScorer = scorer; + this.translatingScorer = new DedupTranslatingScorer(scorer); + this.fieldInfos = state.fieldInfos; + + int versionMeta; + String metaFileName = + IndexFileNames.segmentFileName( + state.segmentInfo.name, state.segmentSuffix, DedupFlatVectorsFormat.META_EXTENSION); + try (ChecksumIndexInput meta = state.directory.openChecksumInput(metaFileName)) { + Throwable priorE = null; + try { + versionMeta = + CodecUtil.checkIndexHeader( + meta, + DedupFlatVectorsFormat.META_CODEC_NAME, + DedupFlatVectorsFormat.VERSION_START, + DedupFlatVectorsFormat.VERSION_CURRENT, + state.segmentInfo.getId(), + state.segmentSuffix); + readMetaBody(meta, state.fieldInfos); + } catch (Throwable e) { + priorE = e; + throw e; + } finally { + CodecUtil.checkFooter(meta, priorE); + } + } + + FileOpenHint[] hints = + Stream.of(FileTypeHint.DATA, FileDataHint.KNN_VECTORS, accessHint) + .filter(Objects::nonNull) + .toArray(FileOpenHint[]::new); + dataContext = state.context.withHints(hints); + boolean success = false; + IndexInput data = null; + try { + data = openDataInput(state, versionMeta, dataContext); + this.vectorData = data; + success = true; + } finally { + if (!success) { Review Comment: Makes sense, will change! ########## lucene/core/src/java/org/apache/lucene/codecs/dedup/DedupFlatVectorsReader.java: ########## @@ -0,0 +1,421 @@ +/* + * 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.dedup; + +import java.io.IOException; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Stream; +import org.apache.lucene.codecs.CodecUtil; +import org.apache.lucene.codecs.hnsw.FlatVectorsReader; +import org.apache.lucene.codecs.hnsw.FlatVectorsScorer; +import org.apache.lucene.codecs.lucene95.OrdToDocDISIReaderConfiguration; +import org.apache.lucene.index.ByteVectorValues; +import org.apache.lucene.index.CorruptIndexException; +import org.apache.lucene.index.FieldInfo; +import org.apache.lucene.index.FieldInfos; +import org.apache.lucene.index.FloatVectorValues; +import org.apache.lucene.index.IndexFileNames; +import org.apache.lucene.index.KnnVectorValues; +import org.apache.lucene.index.SegmentReadState; +import org.apache.lucene.index.VectorEncoding; +import org.apache.lucene.index.VectorSimilarityFunction; +import org.apache.lucene.internal.hppc.IntObjectHashMap; +import org.apache.lucene.store.ChecksumIndexInput; +import org.apache.lucene.store.DataAccessHint; +import org.apache.lucene.store.FileDataHint; +import org.apache.lucene.store.FileTypeHint; +import org.apache.lucene.store.IOContext; +import org.apache.lucene.store.IOContext.FileOpenHint; +import org.apache.lucene.store.IndexInput; +import org.apache.lucene.store.RandomAccessInput; +import org.apache.lucene.util.IOUtils; +import org.apache.lucene.util.LongValues; +import org.apache.lucene.util.RamUsageEstimator; +import org.apache.lucene.util.hnsw.RandomVectorScorer; +import org.apache.lucene.util.packed.DirectReader; + +/** + * Reads a {@link DedupFlatVectorsFormat} segment. + * + * <p>Layout: {@code .dvc} holds pool vector bytes (contiguous per pool) followed by per-field DISI + * + packed {@code docOrd → vecOrd} maps. {@code .dvm} holds pool/field metadata. + * + * @lucene.experimental + */ +public final class DedupFlatVectorsReader extends FlatVectorsReader { + + private static final long SHALLOW_SIZE = + RamUsageEstimator.shallowSizeOfInstance(DedupFlatVectorsReader.class); + + private final FlatVectorsScorer vectorScorer; + private final FlatVectorsScorer translatingScorer; + private final FieldInfos fieldInfos; + private final IndexInput vectorData; + private final IOContext dataContext; + + private final IntObjectHashMap<FieldEntry> fields = new IntObjectHashMap<>(); + + public DedupFlatVectorsReader(SegmentReadState state, FlatVectorsScorer scorer) + throws IOException { + this(state, scorer, DataAccessHint.RANDOM); + } + + public DedupFlatVectorsReader( + SegmentReadState state, FlatVectorsScorer scorer, DataAccessHint accessHint) + throws IOException { + this.vectorScorer = scorer; + this.translatingScorer = new DedupTranslatingScorer(scorer); + this.fieldInfos = state.fieldInfos; + + int versionMeta; + String metaFileName = + IndexFileNames.segmentFileName( + state.segmentInfo.name, state.segmentSuffix, DedupFlatVectorsFormat.META_EXTENSION); + try (ChecksumIndexInput meta = state.directory.openChecksumInput(metaFileName)) { + Throwable priorE = null; + try { + versionMeta = + CodecUtil.checkIndexHeader( + meta, + DedupFlatVectorsFormat.META_CODEC_NAME, + DedupFlatVectorsFormat.VERSION_START, + DedupFlatVectorsFormat.VERSION_CURRENT, + state.segmentInfo.getId(), + state.segmentSuffix); + readMetaBody(meta, state.fieldInfos); + } catch (Throwable e) { + priorE = e; + throw e; + } finally { + CodecUtil.checkFooter(meta, priorE); + } + } + + FileOpenHint[] hints = + Stream.of(FileTypeHint.DATA, FileDataHint.KNN_VECTORS, accessHint) + .filter(Objects::nonNull) + .toArray(FileOpenHint[]::new); + dataContext = state.context.withHints(hints); + boolean success = false; + IndexInput data = null; + try { + data = openDataInput(state, versionMeta, dataContext); + this.vectorData = data; + success = true; + } finally { + if (!success) { + IOUtils.closeWhileHandlingException(data); + } + } + } + + private void readMetaBody(ChecksumIndexInput meta, FieldInfos fieldInfos) throws IOException { + int numPools = meta.readVInt(); + PoolEntry[] poolsLocal = new PoolEntry[numPools]; + for (int p = 0; p < numPools; p++) { + int dim = meta.readVInt(); + int encOrd = meta.readByte(); + VectorEncoding encoding = VectorEncoding.values()[encOrd]; + long offset = meta.readVLong(); + long length = meta.readVLong(); + int uniqueCount = meta.readVInt(); + poolsLocal[p] = new PoolEntry(p, dim, encoding, offset, length, uniqueCount); + } + for (int fieldNumber = meta.readInt(); fieldNumber != -1; fieldNumber = meta.readInt()) { + FieldInfo info = fieldInfos.fieldInfo(fieldNumber); + if (info == null) { + throw new CorruptIndexException("Invalid field number: " + fieldNumber, meta); + } + int simOrd = meta.readByte(); + VectorSimilarityFunction sim = VectorSimilarityFunction.values()[simOrd]; + int poolId = meta.readVInt(); + if (poolId < 0 || poolId >= poolsLocal.length) { + throw new CorruptIndexException( + "Invalid poolId " + poolId + " (numPools=" + poolsLocal.length + ")", meta); + } + int cardinality = meta.readInt(); + OrdToDocDISIReaderConfiguration ordToDoc = + OrdToDocDISIReaderConfiguration.fromStoredMeta(meta, cardinality); + long mapOffset = meta.readVLong(); + long mapLength = meta.readVLong(); + int bitsPerVecOrd = meta.readByte(); + FieldEntry entry = + new FieldEntry( + info, + sim, + poolId, + cardinality, + mapOffset, + mapLength, + bitsPerVecOrd, + ordToDoc, + poolsLocal[poolId]); + fields.put(info.number, entry); + } + } + + private static IndexInput openDataInput( + SegmentReadState state, int versionMeta, IOContext context) throws IOException { + String fileName = + IndexFileNames.segmentFileName( + state.segmentInfo.name, + state.segmentSuffix, + DedupFlatVectorsFormat.VECTOR_DATA_EXTENSION); + IndexInput in = state.directory.openInput(fileName, context); + boolean success = false; + try { + int versionVectorData = + CodecUtil.checkIndexHeader( + in, + DedupFlatVectorsFormat.VECTOR_DATA_CODEC_NAME, + DedupFlatVectorsFormat.VERSION_START, + DedupFlatVectorsFormat.VERSION_CURRENT, + state.segmentInfo.getId(), + state.segmentSuffix); + if (versionMeta != versionVectorData) { + throw new CorruptIndexException( + "Format versions mismatch: meta=" + + versionMeta + + ", " + + DedupFlatVectorsFormat.VECTOR_DATA_CODEC_NAME + + "=" + + versionVectorData, + in); + } + CodecUtil.retrieveChecksum(in); + success = true; + return in; + } finally { + if (!success) { + IOUtils.closeWhileHandlingException(in); + } + } + } + + private FieldEntry getFieldEntry(String fieldName, VectorEncoding expected) { + FieldInfo info = fieldInfos.fieldInfo(fieldName); + if (info == null) { + throw new IllegalArgumentException("field=\"" + fieldName + "\" not found"); + } + FieldEntry entry = fields.get(info.number); + if (entry == null) { + throw new IllegalArgumentException( + "field=\"" + fieldName + "\" not encoded by DedupFlatVectorsFormat"); Review Comment: Makes sense, will change! -- 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]
