-
Notifications
You must be signed in to change notification settings - Fork 25.8k
Add XContentParserTsidFunnel #133457
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
Merged
Merged
Add XContentParserTsidFunnel #133457
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
server/src/main/java/org/elasticsearch/cluster/routing/XContentParserTsidFunnel.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| /* | ||
| * 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.cluster.routing; | ||
|
|
||
| import org.elasticsearch.common.ParsingException; | ||
| import org.elasticsearch.core.Nullable; | ||
| import org.elasticsearch.xcontent.XContentParser; | ||
| import org.elasticsearch.xcontent.XContentParserConfiguration; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Set; | ||
|
|
||
| import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; | ||
| import static org.elasticsearch.common.xcontent.XContentParserUtils.expectValueToken; | ||
|
|
||
| /** | ||
| * A funnel that extracts dimensions from an {@link XContentParser} and adds them to a {@link TsidBuilder}. | ||
| */ | ||
| class XContentParserTsidFunnel implements TsidBuilder.ThrowingTsidFunnel<XContentParser, IOException> { | ||
|
|
||
| private static final XContentParserTsidFunnel INSTANCE = new XContentParserTsidFunnel(); | ||
|
|
||
| static XContentParserTsidFunnel get() { | ||
| return INSTANCE; | ||
| } | ||
|
|
||
| /** | ||
| * Adds dimensions extracted from the provided {@link XContentParser} to the given {@link TsidBuilder}. | ||
| * To only extract dimensions, the parser should be configured via | ||
| * {@link XContentParserConfiguration#withFiltering(String, Set, Set, boolean)}. | ||
| * | ||
| * @param parser the parser from which to read the JSON content | ||
| * @param tsidBuilder the builder to which dimensions will be added | ||
| * @throws IOException if an error occurs while reading from the parser | ||
| */ | ||
| @Override | ||
| public void add(XContentParser parser, TsidBuilder tsidBuilder) throws IOException { | ||
| ensureExpectedToken(null, parser.currentToken(), parser); | ||
| if (parser.nextToken() != XContentParser.Token.START_OBJECT) { | ||
| throw new IllegalArgumentException("Error extracting tsid: source didn't contain any dimension fields"); | ||
| } | ||
| ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser); | ||
| extractObject(tsidBuilder, null, parser); | ||
| ensureExpectedToken(null, parser.nextToken(), parser); | ||
| } | ||
|
|
||
| private void extractObject(TsidBuilder tsidBuilder, @Nullable String path, XContentParser source) throws IOException { | ||
| while (source.currentToken() != XContentParser.Token.END_OBJECT) { | ||
| ensureExpectedToken(XContentParser.Token.FIELD_NAME, source.currentToken(), source); | ||
| String fieldName = source.currentName(); | ||
| String subPath = path == null ? fieldName : path + "." + fieldName; | ||
| source.nextToken(); | ||
| extractItem(tsidBuilder, subPath, source); | ||
| } | ||
| } | ||
|
|
||
| private void extractArray(TsidBuilder tsidBuilder, @Nullable String path, XContentParser source) throws IOException { | ||
| while (source.currentToken() != XContentParser.Token.END_ARRAY) { | ||
| expectValueToken(source.currentToken(), source); | ||
| extractItem(tsidBuilder, path, source); | ||
| } | ||
| } | ||
|
|
||
| private void extractItem(TsidBuilder tsidBuilder, String path, XContentParser source) throws IOException { | ||
| switch (source.currentToken()) { | ||
| case START_OBJECT: | ||
| source.nextToken(); | ||
| extractObject(tsidBuilder, path, source); | ||
| source.nextToken(); | ||
| break; | ||
| case VALUE_NUMBER: | ||
| switch (source.numberType()) { | ||
| case INT -> tsidBuilder.addIntDimension(path, source.intValue()); | ||
| case LONG -> tsidBuilder.addLongDimension(path, source.longValue()); | ||
| case FLOAT -> tsidBuilder.addDoubleDimension(path, source.floatValue()); | ||
| case DOUBLE -> tsidBuilder.addDoubleDimension(path, source.doubleValue()); | ||
| case BIG_DECIMAL, BIG_INTEGER -> tsidBuilder.addStringDimension(path, source.optimizedText().bytes()); | ||
| } | ||
| source.nextToken(); | ||
| break; | ||
| case VALUE_BOOLEAN: | ||
| tsidBuilder.addBooleanDimension(path, source.booleanValue()); | ||
| source.nextToken(); | ||
| break; | ||
| case VALUE_STRING: | ||
| tsidBuilder.addStringDimension(path, source.optimizedText().bytes()); | ||
| source.nextToken(); | ||
| break; | ||
| case START_ARRAY: | ||
| source.nextToken(); | ||
| extractArray(tsidBuilder, path, source); | ||
| source.nextToken(); | ||
| break; | ||
| case VALUE_NULL: | ||
| source.nextToken(); | ||
| break; | ||
| default: | ||
| throw new ParsingException( | ||
| source.getTokenLocation(), | ||
| "Cannot extract dimension due to unexpected token [{}]", | ||
| source.currentToken() | ||
| ); | ||
| } | ||
| } | ||
| } | ||
90 changes: 90 additions & 0 deletions
90
server/src/test/java/org/elasticsearch/cluster/routing/XContentParserTsidFunnelTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /* | ||
| * 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.cluster.routing; | ||
|
|
||
| import org.elasticsearch.test.ESTestCase; | ||
| import org.elasticsearch.xcontent.XContentParserConfiguration; | ||
| import org.elasticsearch.xcontent.json.JsonXContent; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Set; | ||
|
|
||
| import static org.hamcrest.Matchers.equalTo; | ||
|
|
||
| public class XContentParserTsidFunnelTests extends ESTestCase { | ||
|
|
||
| public void testTsidFunnel() throws IOException { | ||
| TsidBuilder xContentTsidBuilder = new TsidBuilder(); | ||
| xContentTsidBuilder.add(createParser(JsonXContent.jsonXContent, """ | ||
| { | ||
| "string": "value", | ||
| "int": 42, | ||
| "long": 1234567890123, | ||
| "double": 3.14159, | ||
| "boolean": true, | ||
| "null_value": null, | ||
| "object": { | ||
| "nested_string": "nested_value" | ||
| }, | ||
| "array": ["elem1", "elem2", 3, 4.5, false] | ||
| } | ||
| """), XContentParserTsidFunnel.get()); | ||
| TsidBuilder manualTsidBuilder = TsidBuilder.newBuilder() | ||
| .addStringDimension("string", "value") | ||
| .addIntDimension("int", 42) | ||
| .addLongDimension("long", 1234567890123L) | ||
| .addDoubleDimension("double", 3.14159) | ||
| .addBooleanDimension("boolean", true) | ||
| .addStringDimension("object.nested_string", "nested_value") | ||
| .addStringDimension("array", "elem1") | ||
| .addStringDimension("array", "elem2") | ||
| .addIntDimension("array", 3) | ||
| .addDoubleDimension("array", 4.5) | ||
| .addBooleanDimension("array", false); | ||
| assertThat(xContentTsidBuilder.hash(), equalTo(manualTsidBuilder.hash())); | ||
| assertThat(xContentTsidBuilder.buildTsid(), equalTo(manualTsidBuilder.buildTsid())); | ||
| } | ||
|
|
||
| public void testFilteredTsidFunnel() throws IOException { | ||
| TsidBuilder xContentTsidBuilder = new TsidBuilder(); | ||
| xContentTsidBuilder.add(JsonXContent.jsonXContent.createParser(getFilteredConfig(Set.of("attributes.*")), """ | ||
| { | ||
| "attributes": { | ||
| "string": "value", | ||
| "int": 42, | ||
| "long": 1234567890123 | ||
| }, | ||
| "other_field": "should_not_be_included" | ||
| } | ||
| """), XContentParserTsidFunnel.get()); | ||
| TsidBuilder manualTsidBuilder = TsidBuilder.newBuilder() | ||
| .addStringDimension("attributes.string", "value") | ||
| .addIntDimension("attributes.int", 42) | ||
| .addLongDimension("attributes.long", 1234567890123L); | ||
| assertThat(xContentTsidBuilder.hash(), equalTo(manualTsidBuilder.hash())); | ||
| assertThat(xContentTsidBuilder.buildTsid(), equalTo(manualTsidBuilder.buildTsid())); | ||
| } | ||
|
|
||
| public void testNoMatchingDimensions() { | ||
| IllegalArgumentException e = expectThrows( | ||
| IllegalArgumentException.class, | ||
| () -> new TsidBuilder().add(JsonXContent.jsonXContent.createParser(getFilteredConfig(Set.of("attributes.*")), """ | ||
| { | ||
| "other_field": "should_not_be_included" | ||
| } | ||
| """), XContentParserTsidFunnel.get()) | ||
| ); | ||
| assertThat(e.getMessage(), equalTo("Error extracting tsid: source didn't contain any dimension fields")); | ||
| } | ||
|
|
||
| private static XContentParserConfiguration getFilteredConfig(Set<String> includePaths) { | ||
| return XContentParserConfiguration.EMPTY.withFiltering(null, includePaths, null, true); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's duplication with
IndexRouting.ExtractFromSource.Builder. Consider deduplicating the logic, here or in a follow-up.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are some subtle differences that make abstracting this difficult. For example, we're using the string representation for numbers and booleans when creating a
routing_path-based hash. This isn't ideal from a performance perspective but changing that forrouting_pathwould be a breaking change. I'll merge as-is for now and we can discuss whether creating some kind of abstraction for this would still make sense given the differences.