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

Migrate away from per-segment-per-threadlocals on SegmentReader #11998

Merged
merged 21 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ public void skipBytes(long numBytes) throws IOException {
}
}

SerializedDocument document(int docID) throws IOException {
SerializedDocument serializedDocument(int docID) throws IOException {
if (state.contains(docID) == false) {
fieldsStream.seek(indexReader.getStartPointer(docID));
state.reset(docID);
Expand All @@ -702,9 +702,9 @@ SerializedDocument document(int docID) throws IOException {
}

@Override
public void visitDocument(int docID, StoredFieldVisitor visitor) throws IOException {
public void document(int docID, StoredFieldVisitor visitor) throws IOException {

final SerializedDocument doc = document(docID);
final SerializedDocument doc = serializedDocument(docID);

for (int fieldIDX = 0; fieldIDX < doc.numStoredFields; fieldIDX++) {
final long infoAndBits = doc.in.readVLong();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@
import org.apache.lucene.index.SortedNumericDocValues;
import org.apache.lucene.index.SortedSetDocValues;
import org.apache.lucene.index.StandardDirectoryReader;
import org.apache.lucene.index.StoredFields;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermVectors;
import org.apache.lucene.index.Terms;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.index.VectorSimilarityFunction;
Expand Down Expand Up @@ -1099,9 +1101,11 @@ private void doTestHits(ScoreDoc[] hits, int expectedCount, IndexReader reader)
throws IOException {
final int hitCount = hits.length;
assertEquals("wrong number of hits", expectedCount, hitCount);
StoredFields storedFields = reader.storedFields();
TermVectors termVectors = reader.termVectors();
for (int i = 0; i < hitCount; i++) {
reader.document(hits[i].doc);
reader.getTermVectors(hits[i].doc);
storedFields.document(hits[i].doc);
termVectors.get(hits[i].doc);
}
}

Expand All @@ -1118,10 +1122,11 @@ public void searchIndex(

final Bits liveDocs = MultiBits.getLiveDocs(reader);
assertNotNull(liveDocs);
StoredFields storedFields = reader.storedFields();

for (int i = 0; i < DOCS_COUNT; i++) {
if (liveDocs.get(i)) {
Document d = reader.document(i);
Document d = storedFields.document(i);
List<IndexableField> fields = d.getFields();
boolean isProxDoc = d.getField("content3") == null;
if (isProxDoc) {
Expand Down Expand Up @@ -1176,7 +1181,7 @@ public void searchIndex(
MultiDocValues.getSortedNumericValues(reader, "dvSortedNumeric");

for (int i = 0; i < DOCS_COUNT; i++) {
int id = Integer.parseInt(reader.document(i).get("id"));
int id = Integer.parseInt(storedFields.document(i).get("id"));
assertEquals(i, dvByte.nextDoc());
assertEquals(id, dvByte.longValue());

Expand Down Expand Up @@ -1231,7 +1236,7 @@ public void searchIndex(
searcher.search(new TermQuery(new Term(new String("content"), "aaa")), 1000).scoreDocs;

// First document should be #0
Document d = searcher.getIndexReader().document(hits[0].doc);
Document d = searcher.getIndexReader().storedFields().document(hits[0].doc);
assertEquals("didn't get the right document first", "0", d.get("id"));

doTestHits(hits, 34, searcher.getIndexReader());
Expand Down Expand Up @@ -1337,7 +1342,7 @@ public void searchIndex(
// test KNN search
ScoreDoc[] scoreDocs = assertKNNSearch(searcher, KNN_VECTOR, 10, 10, "0");
for (int i = 0; i < scoreDocs.length; i++) {
int id = Integer.parseInt(reader.document(scoreDocs[i].doc).get("id"));
int id = Integer.parseInt(storedFields.document(scoreDocs[i].doc).get("id"));
int expectedId = i < DELETED_ID ? i : i + 1;
assertEquals(expectedId, id);
}
Expand Down Expand Up @@ -1388,7 +1393,7 @@ public void changeIndexWithAdds(Random random, Directory dir, Version nameVersio
IndexReader reader = DirectoryReader.open(dir);
IndexSearcher searcher = newSearcher(reader);
ScoreDoc[] hits = searcher.search(new TermQuery(new Term("content", "aaa")), 1000).scoreDocs;
Document d = searcher.getIndexReader().document(hits[0].doc);
Document d = searcher.getIndexReader().storedFields().document(hits[0].doc);
assertEquals("wrong first document", "0", d.get("id"));
doTestHits(hits, 44, searcher.getIndexReader());

Expand Down Expand Up @@ -1420,7 +1425,7 @@ public void changeIndexWithAdds(Random random, Directory dir, Version nameVersio
// make sure searching sees right # hits fot term search
hits = searcher.search(new TermQuery(new Term("content", "aaa")), 1000).scoreDocs;
assertEquals("wrong number of hits", 44, hits.length);
d = searcher.doc(hits[0].doc);
d = searcher.storedFields().document(hits[0].doc);
doTestHits(hits, 44, searcher.getIndexReader());
assertEquals("wrong first document", "0", d.get("id"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private void readIndex(int size) throws IOException {
}

@Override
public void visitDocument(int n, StoredFieldVisitor visitor) throws IOException {
public void document(int n, StoredFieldVisitor visitor) throws IOException {
in.seek(offsets[n]);

while (true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,21 @@
import java.io.Closeable;
import java.io.IOException;
import org.apache.lucene.index.StoredFieldVisitor;
import org.apache.lucene.index.StoredFields;

/**
* Codec API for reading stored fields.
*
* <p>You need to implement {@link #visitDocument(int, StoredFieldVisitor)} to read the stored
* fields for a document, implement {@link #clone()} (creating clones of any IndexInputs used, etc),
* and {@link #close()}
* <p>You need to implement {@link #document(int, StoredFieldVisitor)} to read the stored fields for
* a document, implement {@link #clone()} (creating clones of any IndexInputs used, etc), and {@link
* #close()}
*
* @lucene.experimental
*/
public abstract class StoredFieldsReader implements Cloneable, Closeable {
public abstract class StoredFieldsReader extends StoredFields implements Cloneable, Closeable {
/** Sole constructor. (For invocation by subclass constructors, typically implicit.) */
protected StoredFieldsReader() {}

/** Visit the stored fields for document <code>docID</code> */
public abstract void visitDocument(int docID, StoredFieldVisitor visitor) throws IOException;

@Override
public abstract StoredFieldsReader clone();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public int merge(MergeState mergeState) throws IOException {
}
assert sub.mappedDocID == docCount;
startDocument();
sub.reader.visitDocument(sub.docID, sub.visitor);
sub.reader.document(sub.docID, sub.visitor);
finishDocument();
docCount++;
}
Expand All @@ -149,7 +149,7 @@ public int merge(MergeState mergeState) throws IOException {
* MergeVisitor visitor = new MergeVisitor(mergeState, readerIndex);
* for (...) {
* startDocument();
* storedFieldsReader.visitDocument(docID, visitor);
* storedFieldsReader.document(docID, visitor);
* finishDocument();
* }
* </pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,18 @@

import java.io.Closeable;
import java.io.IOException;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute; // javadocs
import org.apache.lucene.index.Fields;
import org.apache.lucene.index.TermVectors;

/**
* Codec API for reading term vectors:
*
* @lucene.experimental
*/
public abstract class TermVectorsReader implements Cloneable, Closeable {
public abstract class TermVectorsReader extends TermVectors implements Cloneable, Closeable {

/** Sole constructor. (For invocation by subclass constructors, typically implicit.) */
protected TermVectorsReader() {}

/**
* Returns term vectors for this document, or null if term vectors were not indexed. If offsets
* are available they are in an {@link OffsetAttribute} available from the {@link
* org.apache.lucene.index.PostingsEnum}.
*/
public abstract Fields get(int doc) throws IOException;

/**
* Checks consistency of this reader.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ public void skipBytes(long numBytes) throws IOException {
}
}

SerializedDocument document(int docID) throws IOException {
SerializedDocument serializedDocument(int docID) throws IOException {
if (state.contains(docID) == false) {
fieldsStream.seek(indexReader.getStartPointer(docID));
state.reset(docID);
Expand All @@ -625,9 +625,9 @@ boolean isLoaded(int docID) {
}

@Override
public void visitDocument(int docID, StoredFieldVisitor visitor) throws IOException {
public void document(int docID, StoredFieldVisitor visitor) throws IOException {

final SerializedDocument doc = document(docID);
final SerializedDocument doc = serializedDocument(docID);

for (int fieldIDX = 0; fieldIDX < doc.numStoredFields; fieldIDX++) {
final long infoAndBits = doc.in.readVLong();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ public void finish(int numDocs) throws IOException {
private void copyOneDoc(Lucene90CompressingStoredFieldsReader reader, int docID)
throws IOException {
assert reader.getVersion() == VERSION_CURRENT;
SerializedDocument doc = reader.document(docID);
SerializedDocument doc = reader.serializedDocument(docID);
startDocument();
bufferedDocs.copyBytes(doc.in, doc.length);
numStoredFieldsInDoc = doc.numStoredFields;
Expand Down Expand Up @@ -641,7 +641,7 @@ public int merge(MergeState mergeState) throws IOException {
} else if (sub.mergeStrategy == MergeStrategy.VISITOR) {
assert visitors[sub.readerIndex] != null;
startDocument();
reader.visitDocument(sub.docID, visitors[sub.readerIndex]);
reader.document(sub.docID, visitors[sub.readerIndex]);
finishDocument();
++docCount;
sub = docIDMerger.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@ public final Fields getTermVectors(int docID) throws IOException {
return subReaders[i].getTermVectors(docID - starts[i]); // dispatch to subreader
}

@Override
public final TermVectors termVectors() throws IOException {
ensureOpen();
TermVectors[] subVectors = new TermVectors[subReaders.length];
return new TermVectors() {
@Override
public Fields get(int docID) throws IOException {
final int i = readerIndex(docID); // find subreader num
// dispatch to subreader, reusing if possible
if (subVectors[i] == null) {
subVectors[i] = subReaders[i].termVectors();
}
return subVectors[i].get(docID - starts[i]);
}
};
}

@Override
public final int numDocs() {
// Don't call ensureOpen() here (it could affect performance)
Expand Down Expand Up @@ -154,6 +171,23 @@ public final void document(int docID, StoredFieldVisitor visitor) throws IOExcep
subReaders[i].document(docID - starts[i], visitor); // dispatch to subreader
}

@Override
public final StoredFields storedFields() throws IOException {
ensureOpen();
StoredFields[] subFields = new StoredFields[subReaders.length];
return new StoredFields() {
@Override
public void document(int docID, StoredFieldVisitor visitor) throws IOException {
final int i = readerIndex(docID); // find subreader num
// dispatch to subreader, reusing if possible
if (subFields[i] == null) {
subFields[i] = subReaders[i].storedFields();
}
subFields[i].document(docID - starts[i], visitor);
}
};
}

@Override
public final int docFreq(Term term) throws IOException {
ensureOpen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3047,7 +3047,7 @@ public static Status.StoredFieldStatus testStoredFields(
// Intentionally pull even deleted documents to
// make sure they too are not corrupt:
DocumentStoredFieldVisitor visitor = new DocumentStoredFieldVisitor();
storedFields.visitDocument(j, visitor);
storedFields.document(j, visitor);
Document doc = visitor.getDocument();
if (liveDocs == null || liveDocs.get(j)) {
status.docCount++;
Expand Down
39 changes: 24 additions & 15 deletions lucene/core/src/java/org/apache/lucene/index/CodecReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.lucene.index;

import java.io.IOException;
import java.util.Objects;
import org.apache.lucene.codecs.DocValuesProducer;
import org.apache.lucene.codecs.FieldsProducer;
import org.apache.lucene.codecs.KnnVectorsReader;
Expand All @@ -35,14 +34,14 @@ public abstract class CodecReader extends LeafReader {
protected CodecReader() {}

/**
* Expert: retrieve thread-private StoredFieldsReader
* Expert: retrieve underlying StoredFieldsReader
*
* @lucene.internal
*/
public abstract StoredFieldsReader getFieldsReader();

/**
* Expert: retrieve thread-private TermVectorsReader
* Expert: retrieve underlying TermVectorsReader
*
* @lucene.internal
*/
Expand Down Expand Up @@ -83,24 +82,34 @@ protected CodecReader() {}
*/
public abstract KnnVectorsReader getVectorReader();

// intentionally throw UOE for deprecated APIs: keep CodecReader clean!
// (IndexWriter should not be triggering threadlocals in any way)

@Override
public final void document(int docID, StoredFieldVisitor visitor) throws IOException {
checkBounds(docID);
getFieldsReader().visitDocument(docID, visitor);
@Deprecated
public void document(int docID, StoredFieldVisitor visitor) throws IOException {
throw new UnsupportedOperationException("deprecated document access is not supported");
}

@Override
public final Fields getTermVectors(int docID) throws IOException {
TermVectorsReader termVectorsReader = getTermVectorsReader();
if (termVectorsReader == null) {
return null;
}
checkBounds(docID);
return termVectorsReader.get(docID);
@Deprecated
public Fields getTermVectors(int docID) throws IOException {
throw new UnsupportedOperationException("deprecated term vector access is not supported");
}

@Override
public final StoredFields storedFields() throws IOException {
return getFieldsReader();
}

private void checkBounds(int docID) {
Objects.checkIndex(docID, maxDoc());
@Override
public final TermVectors termVectors() throws IOException {
TermVectorsReader reader = getTermVectorsReader();
if (reader == null) {
return TermVectors.EMPTY;
} else {
return reader;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public final Fields getTermVectors(int docID) throws IOException {
throw new UnsupportedOperationException();
}

@Override
public final TermVectors termVectors() throws IOException {
throw new UnsupportedOperationException();
}

@Override
public final int numDocs() {
throw new UnsupportedOperationException();
Expand All @@ -88,6 +93,11 @@ public final void document(int docID, StoredFieldVisitor visitor) throws IOExcep
throw new UnsupportedOperationException();
}

@Override
public final StoredFields storedFields() throws IOException {
throw new UnsupportedOperationException();
}

@Override
protected final void doClose() throws IOException {
throw new UnsupportedOperationException();
Expand Down
Loading