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


##########
lucene/core/src/java/org/apache/lucene/codecs/lucene106/dedup/DedupFlatVectorsWriter.java:
##########
@@ -0,0 +1,163 @@
+/*
+ * 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 java.io.IOException;
+import org.apache.lucene.codecs.CodecUtil;
+import org.apache.lucene.codecs.hnsw.FlatFieldVectorsWriter;
+import org.apache.lucene.codecs.hnsw.FlatVectorsScorer;
+import org.apache.lucene.codecs.hnsw.FlatVectorsWriter;
+import org.apache.lucene.index.FieldInfo;
+import org.apache.lucene.index.IndexFileNames;
+import org.apache.lucene.index.MergeState;
+import org.apache.lucene.index.SegmentWriteState;
+import org.apache.lucene.index.Sorter;
+import org.apache.lucene.store.IndexOutput;
+import org.apache.lucene.util.IOUtils;
+import org.apache.lucene.util.RamUsageEstimator;
+
+/**
+ * Writes de-duplicated flat vectors. A single instance is used for either 
flushing buffered vectors
+ * or merging existing segments (never both), delegating to {@link 
DedupFlushContext} or {@link
+ * DedupMergeContext} accordingly.
+ *
+ * @lucene.experimental
+ */
+final class DedupFlatVectorsWriter extends FlatVectorsWriter {
+  private static final long SHALLOW_SIZE =
+      RamUsageEstimator.shallowSizeOfInstance(DedupFlatVectorsWriter.class);
+
+  private final IndexOutput meta;
+  private final IndexOutput vectorData;
+  private boolean finished;
+
+  private final DedupFlushContext flushContext;
+  private boolean usedForFlush;
+
+  private final DedupMergeContext mergeContext;
+  private boolean usedForMerge;
+
+  DedupFlatVectorsWriter(SegmentWriteState state, FlatVectorsScorer 
vectorsScorer)
+      throws IOException {
+    super(vectorsScorer);
+
+    this.finished = false;
+    this.flushContext = new DedupFlushContext();
+    this.usedForFlush = false;
+    this.mergeContext = new DedupMergeContext();
+    this.usedForMerge = false;
+
+    String metaFileName =
+        IndexFileNames.segmentFileName(state.segmentInfo.name, 
state.segmentSuffix, META_EXTENSION);
+    String vectorDataFileName =
+        IndexFileNames.segmentFileName(
+            state.segmentInfo.name, state.segmentSuffix, 
VECTOR_DATA_EXTENSION);
+
+    boolean success = false;
+    IndexOutput m = null, v = null;
+    try {
+      m = state.directory.createOutput(metaFileName, state.context);
+      v = state.directory.createOutput(vectorDataFileName, state.context);
+      CodecUtil.writeIndexHeader(
+          m, META_CODEC_NAME, VERSION_CURRENT, state.segmentInfo.getId(), 
state.segmentSuffix);
+      CodecUtil.writeIndexHeader(
+          v,
+          VECTOR_DATA_CODEC_NAME,
+          VERSION_CURRENT,
+          state.segmentInfo.getId(),
+          state.segmentSuffix);
+      this.meta = m;
+      this.vectorData = v;
+      success = true;
+    } finally {
+      if (success == false) {
+        IOUtils.closeWhileHandlingException(m, v);
+      }
+    }
+  }
+
+  @Override
+  public FlatFieldVectorsWriter<?> addField(FieldInfo fieldInfo) {
+    if (usedForMerge) {
+      throw new IllegalStateException("already used for merge");
+    }
+    usedForFlush = true;
+
+    return flushContext.addField(fieldInfo);
+  }
+
+  @Override
+  public void flush(int maxDoc, Sorter.DocMap sortMap) throws IOException {
+    if (usedForMerge) {
+      throw new IllegalStateException("already used for merge");
+    }
+    usedForFlush = true;
+
+    flushContext.flush(meta, vectorData, maxDoc, sortMap);
+  }
+
+  @Override
+  public void finish() throws IOException {
+    if (finished) {
+      throw new IllegalStateException("already finished");
+    }
+    finished = true;
+
+    if (usedForMerge) {
+      finishMerge();
+    }
+
+    if (meta != null) {

Review Comment:
   These are [also present in the flat 
format](https://github.com/kaivalnp/lucene/blob/91936b9b43d7244b2663de254ef1c0715c59d754/lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99FlatVectorsWriter.java#L161-L168).
 I think we have a test setup that randomly throws during IO operations, and 
some files may not be initialized? (though I'm not sure if `finish` will be 
called in that case)
   
   I vaguely remember running into a one-off failure when I removed the block, 
but I can't reproduce it now. I will try to remove it and run a few hundred 
iterations of the test to confirm.



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