|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.cluster.routing; |
| 11 | + |
| 12 | +import org.elasticsearch.common.ParsingException; |
| 13 | +import org.elasticsearch.core.Nullable; |
| 14 | +import org.elasticsearch.xcontent.XContentParser; |
| 15 | +import org.elasticsearch.xcontent.XContentParserConfiguration; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.Set; |
| 19 | + |
| 20 | +import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; |
| 21 | +import static org.elasticsearch.common.xcontent.XContentParserUtils.expectValueToken; |
| 22 | + |
| 23 | +/** |
| 24 | + * A funnel that extracts dimensions from an {@link XContentParser} and adds them to a {@link TsidBuilder}. |
| 25 | + */ |
| 26 | +class XContentParserTsidFunnel implements TsidBuilder.ThrowingTsidFunnel<XContentParser, IOException> { |
| 27 | + |
| 28 | + private static final XContentParserTsidFunnel INSTANCE = new XContentParserTsidFunnel(); |
| 29 | + |
| 30 | + static XContentParserTsidFunnel get() { |
| 31 | + return INSTANCE; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Adds dimensions extracted from the provided {@link XContentParser} to the given {@link TsidBuilder}. |
| 36 | + * To only extract dimensions, the parser should be configured via |
| 37 | + * {@link XContentParserConfiguration#withFiltering(String, Set, Set, boolean)}. |
| 38 | + * |
| 39 | + * @param parser the parser from which to read the JSON content |
| 40 | + * @param tsidBuilder the builder to which dimensions will be added |
| 41 | + * @throws IOException if an error occurs while reading from the parser |
| 42 | + */ |
| 43 | + @Override |
| 44 | + public void add(XContentParser parser, TsidBuilder tsidBuilder) throws IOException { |
| 45 | + ensureExpectedToken(null, parser.currentToken(), parser); |
| 46 | + if (parser.nextToken() != XContentParser.Token.START_OBJECT) { |
| 47 | + throw new IllegalArgumentException("Error extracting tsid: source didn't contain any dimension fields"); |
| 48 | + } |
| 49 | + ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser); |
| 50 | + extractObject(tsidBuilder, null, parser); |
| 51 | + ensureExpectedToken(null, parser.nextToken(), parser); |
| 52 | + } |
| 53 | + |
| 54 | + private void extractObject(TsidBuilder tsidBuilder, @Nullable String path, XContentParser source) throws IOException { |
| 55 | + while (source.currentToken() != XContentParser.Token.END_OBJECT) { |
| 56 | + ensureExpectedToken(XContentParser.Token.FIELD_NAME, source.currentToken(), source); |
| 57 | + String fieldName = source.currentName(); |
| 58 | + String subPath = path == null ? fieldName : path + "." + fieldName; |
| 59 | + source.nextToken(); |
| 60 | + extractItem(tsidBuilder, subPath, source); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private void extractArray(TsidBuilder tsidBuilder, @Nullable String path, XContentParser source) throws IOException { |
| 65 | + while (source.currentToken() != XContentParser.Token.END_ARRAY) { |
| 66 | + expectValueToken(source.currentToken(), source); |
| 67 | + extractItem(tsidBuilder, path, source); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private void extractItem(TsidBuilder tsidBuilder, String path, XContentParser source) throws IOException { |
| 72 | + switch (source.currentToken()) { |
| 73 | + case START_OBJECT: |
| 74 | + source.nextToken(); |
| 75 | + extractObject(tsidBuilder, path, source); |
| 76 | + source.nextToken(); |
| 77 | + break; |
| 78 | + case VALUE_NUMBER: |
| 79 | + switch (source.numberType()) { |
| 80 | + case INT -> tsidBuilder.addIntDimension(path, source.intValue()); |
| 81 | + case LONG -> tsidBuilder.addLongDimension(path, source.longValue()); |
| 82 | + case FLOAT -> tsidBuilder.addDoubleDimension(path, source.floatValue()); |
| 83 | + case DOUBLE -> tsidBuilder.addDoubleDimension(path, source.doubleValue()); |
| 84 | + case BIG_DECIMAL, BIG_INTEGER -> tsidBuilder.addStringDimension(path, source.optimizedText().bytes()); |
| 85 | + } |
| 86 | + source.nextToken(); |
| 87 | + break; |
| 88 | + case VALUE_BOOLEAN: |
| 89 | + tsidBuilder.addBooleanDimension(path, source.booleanValue()); |
| 90 | + source.nextToken(); |
| 91 | + break; |
| 92 | + case VALUE_STRING: |
| 93 | + tsidBuilder.addStringDimension(path, source.optimizedText().bytes()); |
| 94 | + source.nextToken(); |
| 95 | + break; |
| 96 | + case START_ARRAY: |
| 97 | + source.nextToken(); |
| 98 | + extractArray(tsidBuilder, path, source); |
| 99 | + source.nextToken(); |
| 100 | + break; |
| 101 | + case VALUE_NULL: |
| 102 | + source.nextToken(); |
| 103 | + break; |
| 104 | + default: |
| 105 | + throw new ParsingException( |
| 106 | + source.getTokenLocation(), |
| 107 | + "Cannot extract dimension due to unexpected token [{}]", |
| 108 | + source.currentToken() |
| 109 | + ); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments