Skip to content

Commit

Permalink
Fix StoredFieldsConsumer finish (#13927)
Browse files Browse the repository at this point in the history
  • Loading branch information
linfn authored Oct 21, 2024
1 parent d1e017f commit 86457a5
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
1 change: 1 addition & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Bug Fixes
when they were not sorted by startOffset. (Seunghan Jung)
* GITHUB#13884: Remove broken .toArray from Long/CharObjectHashMap entirely. (Pan Guixin)
* GITHUB#12686: Added support for highlighting IndexOrDocValuesQuery. (Prudhvi Godithi)
* GITHUB#13927: Fix StoredFieldsConsumer finish. (linfn)

Build
---------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ void finishDocument() throws IOException {

void finish(int maxDoc) throws IOException {
while (lastDoc < maxDoc - 1) {
startDocument(lastDoc);
startDocument(lastDoc + 1);
finishDocument();
++lastDoc;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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.index;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FlushInfo;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.apache.lucene.util.StringHelper;
import org.apache.lucene.util.Version;

public class TestStoredFieldsConsumer extends LuceneTestCase {

public void testFinish() throws IOException {
Directory dir = newDirectory();
IndexWriterConfig iwc = new IndexWriterConfig();
SegmentInfo si =
new SegmentInfo(
dir,
Version.LATEST,
null,
"_0",
-1,
false,
false,
iwc.getCodec(),
Collections.emptyMap(),
StringHelper.randomId(),
new HashMap<>(),
null);

AtomicInteger startDocCounter = new AtomicInteger(), finishDocCounter = new AtomicInteger();
StoredFieldsConsumer consumer =
new StoredFieldsConsumer(iwc.getCodec(), dir, si) {
@Override
void startDocument(int docID) throws IOException {
super.startDocument(docID);
startDocCounter.incrementAndGet();
}

@Override
void finishDocument() throws IOException {
super.finishDocument();
finishDocCounter.incrementAndGet();
}
};

int numDocs = 3;
consumer.finish(numDocs);

si.setMaxDoc(numDocs);
SegmentWriteState state =
new SegmentWriteState(
null,
dir,
si,
new FieldInfos(new FieldInfo[0]),
null,
new IOContext(new FlushInfo(numDocs, 10)));
consumer.flush(state, null);
dir.close();

assertEquals(numDocs, startDocCounter.get());
assertEquals(numDocs, finishDocCounter.get());
}
}

0 comments on commit 86457a5

Please sign in to comment.