kaivalnp commented on code in PR #15979: URL: https://github.com/apache/lucene/pull/15979#discussion_r3667668561
########## lucene/core/src/java/org/apache/lucene/codecs/lucene106/dedup/DedupFlatVectorsReader.java: ########## @@ -0,0 +1,359 @@ +/* + * 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.codecs.lucene106.dedup.DedupFlatVectorsFormat.META_CODEC_NAME; +import static org.apache.lucene.codecs.lucene106.dedup.DedupFlatVectorsFormat.META_EXTENSION; +import static org.apache.lucene.codecs.lucene106.dedup.DedupFlatVectorsFormat.VECTOR_DATA_CODEC_NAME; +import static org.apache.lucene.codecs.lucene106.dedup.DedupFlatVectorsFormat.VECTOR_DATA_EXTENSION; +import static org.apache.lucene.codecs.lucene106.dedup.DedupFlatVectorsFormat.VERSION_CURRENT; +import static org.apache.lucene.codecs.lucene106.dedup.DedupFlatVectorsFormat.VERSION_START; +import static org.apache.lucene.codecs.lucene106.dedup.DedupUtil.loadDedupBytes; +import static org.apache.lucene.codecs.lucene106.dedup.DedupUtil.loadDedupFloat16s; +import static org.apache.lucene.codecs.lucene106.dedup.DedupUtil.loadDedupFloats; +import static org.apache.lucene.codecs.lucene106.dedup.DedupUtil.readFieldInfo; +import static org.apache.lucene.codecs.lucene106.dedup.DedupUtil.readGroupInfo; +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 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.hnsw.FlatVectorsReader; +import org.apache.lucene.codecs.hnsw.FlatVectorsScorer; +import org.apache.lucene.codecs.lucene106.dedup.DedupUtil.GroupInfo; +import org.apache.lucene.codecs.lucene106.dedup.DedupUtil.ReadFieldInfo; +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.Float16VectorValues; +import org.apache.lucene.index.FloatVectorValues; +import org.apache.lucene.index.IndexFileNames; +import org.apache.lucene.index.MergePolicy; +import org.apache.lucene.index.SegmentReadState; +import org.apache.lucene.index.VectorEncoding; +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.IndexInput; +import org.apache.lucene.util.IOUtils; +import org.apache.lucene.util.RamUsageEstimator; +import org.apache.lucene.util.hnsw.RandomVectorScorer; + +/** + * Reads de-duplicated flat vectors written by {@link DedupFlatVectorsWriter}. Each field exposes a + * view backed by its group's shared vectors and an {@code ordToVecOrd} translation map. + * + * @lucene.experimental + */ +final class DedupFlatVectorsReader extends FlatVectorsReader { + private static final long SHALLOW_SIZE = + RamUsageEstimator.shallowSizeOfInstance(DedupFlatVectorsReader.class); + + private final FlatVectorsScorer vectorsScorer; + private final Map<String, FieldEntry> fields; + private final IndexInput vectorData; + + DedupFlatVectorsReader(SegmentReadState state, FlatVectorsScorer vectorsScorer) + throws IOException { + + this.vectorsScorer = vectorsScorer; + this.fields = new HashMap<>(); + + String metaFileName = + IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, META_EXTENSION); + + int versionMeta; + try (ChecksumIndexInput meta = state.directory.openChecksumInput(metaFileName)) { + Throwable priorE = null; + try { + versionMeta = + CodecUtil.checkIndexHeader( + meta, + META_CODEC_NAME, + VERSION_START, + VERSION_CURRENT, + state.segmentInfo.getId(), + state.segmentSuffix); + readMetaBody(meta, state.fieldInfos); + } catch (Throwable e) { + priorE = e; + throw e; + } finally { + CodecUtil.checkFooter(meta, priorE); + } + } + + this.vectorData = openDataInput(state, versionMeta); + } + + private void readMetaBody(ChecksumIndexInput meta, FieldInfos fieldInfos) throws IOException { + List<GroupInfo> groupInfos = new ArrayList<>(); + while (true) { + GroupInfo groupInfo = readGroupInfo(meta); + if (groupInfo == null) { + break; + } + groupInfos.add(groupInfo); + } + + while (true) { + ReadFieldInfo fieldInfo = readFieldInfo(meta); + if (fieldInfo == null) { + break; + } + + FieldInfo info = fieldInfos.fieldInfo(fieldInfo.fieldNumber()); + if (info == null) { + throw new CorruptIndexException("Invalid field number: " + fieldInfo.fieldNumber(), meta); + } else if (fieldInfo.function() != info.getVectorSimilarityFunction()) { Review Comment: Yes^ -- 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]
