Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix StoredFieldsConsumer finish #13927

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,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());
}
}