msokolov commented on code in PR #15979: URL: https://github.com/apache/lucene/pull/15979#discussion_r3659774355
########## lucene/core/src/java/org/apache/lucene/codecs/lucene106/dedup/DedupFlushContext.java: ########## @@ -0,0 +1,268 @@ +/* + * 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 java.nio.ByteOrder.LITTLE_ENDIAN; +import static org.apache.lucene.codecs.lucene106.dedup.DedupUtil.ORD_UNKNOWN; +import static org.apache.lucene.codecs.lucene106.dedup.DedupUtil.alignBytes; +import static org.apache.lucene.codecs.lucene106.dedup.DedupUtil.hashBytes; +import static org.apache.lucene.codecs.lucene106.dedup.DedupUtil.writeEndOfFields; +import static org.apache.lucene.codecs.lucene106.dedup.DedupUtil.writeEndOfGroups; +import static org.apache.lucene.codecs.lucene106.dedup.DedupUtil.writeFieldInfo; +import static org.apache.lucene.codecs.lucene106.dedup.DedupUtil.writeGroupInfo; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.FloatBuffer; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.lucene.codecs.KnnVectorsWriter; +import org.apache.lucene.codecs.hnsw.FlatFieldVectorsWriter; +import org.apache.lucene.codecs.lucene106.dedup.DedupUtil.GroupInfo; +import org.apache.lucene.codecs.lucene106.dedup.DedupUtil.GroupKey; +import org.apache.lucene.codecs.lucene106.dedup.DedupUtil.OrdToVecOrd; +import org.apache.lucene.codecs.lucene106.dedup.DedupUtil.OrdToVecOrdArrayList; +import org.apache.lucene.codecs.lucene106.dedup.DedupUtil.OrdToVecOrdMappedArrayList; +import org.apache.lucene.codecs.lucene106.dedup.DedupUtil.WriteFieldInfo; +import org.apache.lucene.index.DocsWithFieldSet; +import org.apache.lucene.index.FieldInfo; +import org.apache.lucene.index.Sorter; +import org.apache.lucene.index.VectorEncoding; +import org.apache.lucene.internal.hppc.IntArrayList; +import org.apache.lucene.store.IndexOutput; +import org.apache.lucene.util.Accountable; +import org.apache.lucene.util.RamUsageEstimator; + +/** + * Buffers vectors added during a flush and de-duplicates them in memory. Fields sharing a {@link + * DedupUtil.GroupKey} intern into the same {@link DedupGroup}; on {@link #flush} each group's + * distinct vectors are written once, followed by per-field metadata mapping document ordinals to + * group ordinals. + * + * @lucene.experimental + */ +final class DedupFlushContext implements Accountable { + private final Map<GroupKey, DedupGroup<?>> groups; + private final List<FieldData> fieldDataList; + + DedupFlushContext() { + this.groups = new HashMap<>(); + this.fieldDataList = new ArrayList<>(); + } + + private static DedupGroup<?> getGroup(GroupKey groupKey) { + return switch (groupKey.encoding()) { + case BYTE -> new ByteGroup(groupKey.dimension()); + case FLOAT32 -> new FloatGroup(groupKey.dimension()); + }; + } + + @Override + public long ramBytesUsed() { + long total = 0; + for (DedupGroup<?> group : groups.values()) { + total += group.ramBytesUsed(); + } + for (FieldData data : fieldDataList) { + total += data.ramBytesUsed(); + } + return total; + } + + FlatFieldVectorsWriter<?> addField(FieldInfo fieldInfo) { + GroupKey groupKey = new GroupKey(fieldInfo); + DedupGroup<?> group = groups.computeIfAbsent(groupKey, DedupFlushContext::getGroup); + DedupFlatFieldVectorsWriter<?> fieldVectorsWriter = new DedupFlatFieldVectorsWriter<>(group); + + fieldDataList.add(new FieldData(fieldInfo, groupKey, fieldVectorsWriter)); + return fieldVectorsWriter; + } + + void flush(IndexOutput meta, IndexOutput vectorData, int maxDoc, Sorter.DocMap sortMap) + throws IOException { + + Map<GroupKey, Integer> groupOrds = new HashMap<>(); + + int groupOrd = 0; + for (Map.Entry<GroupKey, DedupGroup<?>> entry : groups.entrySet()) { + GroupKey groupKey = entry.getKey(); + DedupGroup<?> group = entry.getValue(); + + int groupSize = group.size(); + long vectorDataOffset = alignBytes(vectorData, groupKey.encoding()); + + // TODO: Write in sorted order for faster merge? (with sequential IO) + for (int ord = 0; ord < groupSize; ord++) { + byte[] bytes = group.serialize(ord); Review Comment: ok, I don't know why I thought there was extra allocation going on; this seems fine, thanks for explaining -- 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]
