-
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
Merged
parkertimmins
merged 16 commits into
elastic:main
from
parkertimmins:parker/patterned-text-args-schema
Aug 28, 2025
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b9bff73
Encode args in a binary schema instead of using placeholders
parkertimmins c6f9551
[CI] Auto commit changes from spotless
957b2ce
Remove placeholder from template
parkertimmins 0727314
Move args into separate class
parkertimmins 3969361
[CI] Auto commit changes from spotless
90cf48c
Add encoder/decoder tests
parkertimmins 874fbf7
Merge branch 'main' into parker/patterned-text-args-schema
parkertimmins e422af9
Rename Arg.Schema to ArgInfo
parkertimmins ff5fae1
Java doc and a bit of renaming
parkertimmins f235970
Handle offset from previous within encode/decode
parkertimmins 4c6aa6b
[CI] Auto commit changes from spotless
ed5bb78
Update template_id yaml tests due to template change
parkertimmins ceeddb4
Cleanup some things missed during rename
parkertimmins b0a3c59
Merge branch 'main' into parker/patterned-text-args-schema
parkertimmins 68144ea
Merge branch 'main' into parker/patterned-text-args-schema
parkertimmins 909adbd
Merge branch 'main' into parker/patterned-text-args-schema
parkertimmins 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
122 changes: 122 additions & 0 deletions
122
x-pack/plugin/logsdb/src/main/java/org/elasticsearch/xpack/logsdb/patternedtext/Arg.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,122 @@ | ||
/* | ||
* 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 org.apache.lucene.store.DataInput; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Base64; | ||
import java.util.List; | ||
|
||
/** | ||
* Describes the type and location of an argument in the template. A list of argument infos is encoded and stored in a doc value | ||
* column, this is used to re-combine the template and argument columns. Documents with identical templates share the same | ||
* of argument infos, and since indices are sorted by template_id, this doc value column compresses very well. | ||
*/ | ||
public class Arg { | ||
|
||
private static final String SPACE = " "; | ||
private static final Base64.Decoder DECODER = Base64.getUrlDecoder(); | ||
private static final Base64.Encoder ENCODER = Base64.getUrlEncoder().withoutPadding(); | ||
private static int VINT_MAX_BYTES = 5; | ||
|
||
public enum Type { | ||
GENERIC(0); | ||
|
||
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 Info(Type type, int offsetInTemplate) { | ||
public Info { | ||
assert offsetInTemplate >= 0; | ||
} | ||
|
||
void writeTo(ByteArrayDataOutput out, int previousOffset) throws IOException { | ||
out.writeVInt(type.toCode()); | ||
int diff = offsetInTemplate - previousOffset; | ||
out.writeVInt(diff); | ||
} | ||
|
||
static Info readFrom(DataInput in, int previousOffset) throws IOException { | ||
var type = Type.fromCode(in.readVInt()); | ||
int diffFromPrevious = in.readVInt(); | ||
int offsetInfoTemplate = previousOffset + diffFromPrevious; | ||
return new Info(type, offsetInfoTemplate); | ||
} | ||
} | ||
|
||
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 encodeInfo(List<Info> arguments) throws IOException { | ||
int maxSize = VINT_MAX_BYTES + arguments.size() * (VINT_MAX_BYTES + VINT_MAX_BYTES); | ||
byte[] buffer = new byte[maxSize]; | ||
var dataInput = new ByteArrayDataOutput(buffer); | ||
dataInput.writeVInt(arguments.size()); | ||
int previousOffset = 0; | ||
for (var arg : arguments) { | ||
arg.writeTo(dataInput, previousOffset); | ||
previousOffset = arg.offsetInTemplate; | ||
} | ||
|
||
int size = dataInput.getPosition(); | ||
byte[] data = Arrays.copyOfRange(buffer, 0, size); | ||
return ENCODER.encodeToString(data); | ||
} | ||
|
||
static List<Info> decodeInfo(String encoded) throws IOException { | ||
byte[] encodedBytes = DECODER.decode(encoded); | ||
var input = new ByteArrayDataInput(encodedBytes); | ||
|
||
int numArgs = input.readVInt(); | ||
int previousOffset = 0; | ||
List<Info> arguments = new ArrayList<>(numArgs); | ||
for (int i = 0; i < numArgs; i++) { | ||
var argInfo = Info.readFrom(input, previousOffset); | ||
arguments.add(argInfo); | ||
previousOffset = argInfo.offsetInTemplate; | ||
} | ||
return arguments; | ||
} | ||
|
||
static String encodeRemainingArgs(PatternedTextValueProcessor.Parts parts) { | ||
return String.join(SPACE, parts.args()); | ||
} | ||
|
||
static String[] decodeRemainingArgs(String mergedArgs) { | ||
return mergedArgs.split(SPACE); | ||
} | ||
} |
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
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
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
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
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
Oops, something went wrong.
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.