-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Refactor SourceProvider creation to consistently use MappingLookup #128213
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
Changes from 3 commits
e711269
7e549d5
a5be44d
e722106
1a157a5
207587d
ec220d4
b69e817
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pr: 128213 | ||
| summary: Refactor `SourceProvider` creation to consistently use `MappingLookup` | ||
| area: Mapping | ||
| type: enhancement | ||
| issues: [] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.search.lookup; | ||
|
|
||
| import org.apache.lucene.index.LeafReaderContext; | ||
| import org.elasticsearch.common.util.concurrent.ConcurrentCollections; | ||
| import org.elasticsearch.index.fieldvisitor.LeafStoredFieldLoader; | ||
| import org.elasticsearch.index.fieldvisitor.StoredFieldLoader; | ||
| import org.elasticsearch.index.mapper.SourceLoader; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * A {@link SourceProvider} that loads _source from a concurrent search. | ||
| * | ||
| * NOTE: This is written under the assumption that individual segments are accessed by a single | ||
| * thread, even if separate segments may be searched concurrently. If we ever implement | ||
| * within-segment concurrency this will have to work entirely differently. | ||
| * **/ | ||
| class ConcurrentSegmentSourceProvider implements SourceProvider { | ||
| private final SourceLoader sourceLoader; | ||
| private final StoredFieldLoader storedFieldLoader; | ||
| private final Map<Object, Leaf> leaves = ConcurrentCollections.newConcurrentMap(); | ||
|
|
||
| ConcurrentSegmentSourceProvider(SourceLoader loader, boolean loadSource) { | ||
| this.sourceLoader = loader; | ||
| this.storedFieldLoader = StoredFieldLoader.create(loadSource, sourceLoader.requiredStoredFields()); | ||
| } | ||
|
|
||
| @Override | ||
| public Source getSource(LeafReaderContext ctx, int doc) throws IOException { | ||
| final Object id = ctx.id(); | ||
| var leaf = leaves.get(id); | ||
| if (leaf == null) { | ||
| leaf = new Leaf(sourceLoader.leaf(ctx.reader(), null), storedFieldLoader.getLoader(ctx, null)); | ||
| var existing = leaves.put(id, leaf); | ||
| assert existing == null : "unexpected source provider [" + existing + "]"; | ||
| } | ||
| return leaf.getSource(ctx, doc); | ||
| } | ||
|
|
||
| private static class Leaf implements SourceProvider { | ||
| private final SourceLoader.Leaf sourceLoader; | ||
| private final LeafStoredFieldLoader storedFieldLoader; | ||
| int doc = -1; | ||
| Source source = null; | ||
|
|
||
| private Leaf(SourceLoader.Leaf sourceLoader, LeafStoredFieldLoader storedFieldLoader) { | ||
| this.sourceLoader = sourceLoader; | ||
| this.storedFieldLoader = storedFieldLoader; | ||
| } | ||
|
|
||
| @Override | ||
| public Source getSource(LeafReaderContext ctx, int doc) throws IOException { | ||
| if (this.doc == doc) { | ||
| return source; | ||
| } | ||
| this.doc = doc; | ||
| storedFieldLoader.advanceTo(doc); | ||
| return source = sourceLoader.source(storedFieldLoader, doc); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -106,7 +106,11 @@ public final void testFromSourceDoesNotEnforceValuesLimit() throws IOException { | |
| DateFieldScript.LeafFactory leafFactory = fromSource().newFactory( | ||
| "field", | ||
| Collections.emptyMap(), | ||
| new SearchLookup(field -> null, (ft, lookup, fdt) -> null, SourceProvider.fromStoredFields()), | ||
| new SearchLookup( | ||
| field -> null, | ||
| (ft, lookup, fdt) -> null, | ||
| SourceProvider.fromLookup(MappingLookup.EMPTY, null, SourceFieldMetrics.NOOP) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: add a test-only factory:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And maybe another one using
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about that but I like the fact that these tests are purposively using an empty mapping rather than a direct method. It's not really an empty source provider, just a source provider with an empty mapping and I don't want to encourage writing more tests with this pattern. |
||
| ), | ||
| DateFormatter.forPattern("epoch_millis"), | ||
| OnScriptError.FAIL | ||
| ); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.