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

Speed up synthetic source again #89600

Merged
merged 2 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,21 @@ public DocValuesLoader docValuesLoader(LeafReader leafReader, int[] docIdsInLeaf
loaders.add(loader);
}
}
return docId -> {
if (loaders.isEmpty()) {
return null;
}
return new ObjectDocValuesLoader(loaders);
}

private class ObjectDocValuesLoader implements DocValuesLoader {
private final List<SourceLoader.SyntheticFieldLoader.DocValuesLoader> loaders;

private ObjectDocValuesLoader(List<DocValuesLoader> loaders) {
this.loaders = loaders;
}

@Override
public boolean advanceToDoc(int docId) throws IOException {
for (SourceLoader.SyntheticFieldLoader.DocValuesLoader docValueLoader : loaders) {
boolean leafHasValue = docValueLoader.advanceToDoc(docId);
hasValue |= leafHasValue;
Expand All @@ -616,7 +630,7 @@ public DocValuesLoader docValuesLoader(LeafReader leafReader, int[] docIdsInLeaf
* stored field.
*/
return hasValue;
};
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,18 @@ public Set<String> requiredStoredFields() {

@Override
public Leaf leaf(LeafReader reader, int[] docIdsInLeaf) throws IOException {
SyntheticFieldLoader.DocValuesLoader leaf = loader.docValuesLoader(reader, docIdsInLeaf);
return (fieldsVisitor, docId) -> {
return new SyntheticLeaf(loader.docValuesLoader(reader, docIdsInLeaf));
}

private class SyntheticLeaf implements Leaf {
private final SyntheticFieldLoader.DocValuesLoader docValuesLoader;

private SyntheticLeaf(SyntheticFieldLoader.DocValuesLoader docValuesLoader) {
this.docValuesLoader = docValuesLoader;
}

@Override
public BytesReference source(FieldsVisitor fieldsVisitor, int docId) throws IOException {
if (fieldsVisitor != null) {
for (Map.Entry<String, List<Object>> e : fieldsVisitor.fields().entrySet()) {
SyntheticFieldLoader.StoredFieldLoader loader = storedFieldLoaders.get(e.getKey());
Expand All @@ -118,14 +128,14 @@ public Leaf leaf(LeafReader reader, int[] docIdsInLeaf) throws IOException {
}
// TODO accept a requested xcontent type
try (XContentBuilder b = new XContentBuilder(JsonXContent.jsonXContent, new ByteArrayOutputStream())) {
if (leaf.advanceToDoc(docId)) {
if (docValuesLoader.advanceToDoc(docId)) {
loader.write(b);
} else {
b.startObject().endObject();
}
return BytesReference.bytes(b);
}
};
}
}
}

Expand Down