-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Encode args in a info doc value rather than using placeholders #132782
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 5 commits
b9bff73
c6f9551
957b2ce
0727314
3969361
90cf48c
874fbf7
e422af9
ff5fae1
f235970
4c6aa6b
ed5bb78
ceeddb4
b0a3c59
68144ea
909adbd
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,105 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.logsdb.patternedtext; | ||
|
||
import org.apache.lucene.store.ByteArrayDataInput; | ||
import org.apache.lucene.store.ByteArrayDataOutput; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Base64; | ||
import java.util.List; | ||
|
||
public class Arg { | ||
|
||
private static final String SPACE = " "; | ||
|
||
public enum Type { | ||
GENERAL(0), | ||
parkertimmins marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
IP4(1), | ||
INTEGER(2); | ||
|
||
private final int code; | ||
private static final Type[] lookup = new Type[values().length]; | ||
static { | ||
for (var type : values()) { | ||
lookup[type.code] = type; | ||
} | ||
} | ||
|
||
Type(int code) { | ||
this.code = code; | ||
} | ||
|
||
public int toCode() { | ||
return code; | ||
} | ||
|
||
public static Type fromCode(int code) { | ||
return lookup[code]; | ||
} | ||
} | ||
|
||
record Schema(Type type, int offsetFromPrevArg) { | ||
parkertimmins marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
void writeTo(ByteArrayDataOutput out) throws IOException { | ||
out.writeVInt(type.toCode()); | ||
out.writeVInt(offsetFromPrevArg); | ||
} | ||
|
||
static Schema readFrom(ByteArrayDataInput in) { | ||
return new Schema(Type.fromCode(in.readVInt()), in.readVInt()); | ||
} | ||
} | ||
|
||
private static final Base64.Decoder DECODER = Base64.getUrlDecoder(); | ||
private static final Base64.Encoder ENCODER = Base64.getUrlEncoder().withoutPadding(); | ||
|
||
public static String encodeSchema(List<Schema> arguments) throws IOException { | ||
int maxSize = Integer.BYTES + arguments.size() * (Integer.BYTES + Integer.BYTES); | ||
byte[] buffer = new byte[maxSize]; | ||
var dataInput = new ByteArrayDataOutput(buffer); | ||
dataInput.writeVInt(arguments.size()); | ||
for (var arg : arguments) { | ||
arg.writeTo(dataInput); | ||
} | ||
|
||
int size = dataInput.getPosition(); | ||
byte[] data = Arrays.copyOfRange(buffer, 0, size); | ||
return ENCODER.encodeToString(data); | ||
} | ||
|
||
public static List<Schema> decodeSchema(String encoded) { | ||
byte[] encodedBytes = DECODER.decode(encoded); | ||
var input = new ByteArrayDataInput(encodedBytes); | ||
|
||
int numArgs = input.readVInt(); | ||
List<Schema> arguments = new ArrayList<>(numArgs); | ||
for (int i = 0; i < numArgs; i++) { | ||
arguments.add(Schema.readFrom(input)); | ||
} | ||
return arguments; | ||
} | ||
|
||
static boolean isArg(String text) { | ||
for (int i = 0; i < text.length(); i++) { | ||
if (Character.isDigit(text.charAt(i))) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
static String encodeRemainingArgs(PatternedTextValueProcessor.Parts parts) { | ||
return String.join(SPACE, parts.args()); | ||
} | ||
|
||
static String[] decodeRemainingArgs(String mergedArgs) { | ||
return mergedArgs.split(SPACE); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -184,9 +184,13 @@ protected void parseCreateField(DocumentParserContext context) throws IOExceptio | |
// Add template_id doc_values | ||
context.doc().add(templateIdMapper.buildKeywordField(new BytesRef(parts.templateId()))); | ||
|
||
// Add args schema | ||
String argsSchemaEncoded = Arg.encodeSchema(parts.schemas()); | ||
context.doc().add(new SortedSetDocValuesField(fieldType().argsSchemaFieldName(), new BytesRef(argsSchemaEncoded))); | ||
|
||
|
||
// Add args doc_values | ||
if (parts.args().isEmpty() == false) { | ||
String remainingArgs = PatternedTextValueProcessor.encodeRemainingArgs(parts); | ||
String remainingArgs = Arg.encodeRemainingArgs(parts); | ||
context.doc().add(new SortedSetDocValuesField(fieldType().argsFieldName(), new BytesRef(remainingArgs))); | ||
} | ||
} | ||
|
@@ -207,7 +211,12 @@ protected SyntheticSourceSupport syntheticSourceSupport() { | |
() -> new CompositeSyntheticFieldLoader( | ||
leafName(), | ||
fullPath(), | ||
new PatternedTextSyntheticFieldLoaderLayer(fieldType().name(), fieldType().templateFieldName(), fieldType().argsFieldName()) | ||
new PatternedTextSyntheticFieldLoaderLayer( | ||
fieldType().name(), | ||
fieldType().templateFieldName(), | ||
fieldType().argsFieldName(), | ||
fieldType().argsSchemaFieldName() | ||
) | ||
) | ||
); | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.