-
Notifications
You must be signed in to change notification settings - Fork 1
Add XContentFieldFilter (#81970) #3
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
base: pr_013_before
Are you sure you want to change the base?
Changes from all commits
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,76 @@ | ||
| /* | ||
| * 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 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 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| package org.elasticsearch.common.xcontent; | ||
|
|
||
| import org.elasticsearch.common.bytes.BytesReference; | ||
| import org.elasticsearch.common.io.stream.BytesStreamOutput; | ||
| import org.elasticsearch.common.util.CollectionUtils; | ||
| import org.elasticsearch.common.xcontent.support.XContentMapValues; | ||
| import org.elasticsearch.core.Nullable; | ||
| import org.elasticsearch.core.Tuple; | ||
| import org.elasticsearch.xcontent.XContentBuilder; | ||
| import org.elasticsearch.xcontent.XContentFactory; | ||
| import org.elasticsearch.xcontent.XContentParser; | ||
| import org.elasticsearch.xcontent.XContentParserConfiguration; | ||
| import org.elasticsearch.xcontent.XContentType; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.function.Function; | ||
|
|
||
| /** | ||
| * A filter that filter fields away from source | ||
| */ | ||
| public interface XContentFieldFilter { | ||
| /** | ||
| * filter source in {@link BytesReference} format and in {@link XContentType} content type | ||
| * note that xContentType may be null in some case, we should guess xContentType from sourceBytes in such cases | ||
| */ | ||
| BytesReference apply(BytesReference sourceBytes, @Nullable XContentType xContentType) throws IOException; | ||
|
|
||
| /** | ||
| * Construct {@link XContentFieldFilter} using given includes and excludes | ||
| * | ||
| * @param includes fields to keep, wildcard supported | ||
| * @param excludes fields to remove, wildcard supported | ||
| * @return filter using {@link XContentMapValues#filter(String[], String[])} if wildcard found in excludes | ||
| * , otherwise return filter using {@link XContentParser} | ||
| */ | ||
| static XContentFieldFilter newFieldFilter(String[] includes, String[] excludes) { | ||
| if ((CollectionUtils.isEmpty(excludes) == false) && Arrays.stream(excludes).filter(field -> field.contains("*")).count() > 0) { | ||
| return (originalSource, contentType) -> { | ||
| Function<Map<String, ?>, Map<String, Object>> mapFilter = XContentMapValues.filter(includes, excludes); | ||
| Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(originalSource, true, contentType); | ||
| Map<String, Object> filteredSource = mapFilter.apply(mapTuple.v2()); | ||
| BytesStreamOutput bStream = new BytesStreamOutput(); | ||
| XContentType actualContentType = mapTuple.v1(); | ||
| XContentBuilder builder = XContentFactory.contentBuilder(actualContentType, bStream).map(filteredSource); | ||
| builder.close(); | ||
| return bStream.bytes(); | ||
| }; | ||
| } else { | ||
| final XContentParserConfiguration parserConfig = XContentParserConfiguration.EMPTY.withFiltering( | ||
| Set.of(includes), | ||
| Set.of(excludes) | ||
| ); | ||
| return (originalSource, contentType) -> { | ||
| if (contentType == null) { | ||
| contentType = XContentHelper.xContentTypeMayCompressed(originalSource); | ||
| } | ||
| BytesStreamOutput streamOutput = new BytesStreamOutput(Math.min(1024, originalSource.length())); | ||
| XContentBuilder builder = new XContentBuilder(contentType.xContent(), streamOutput); | ||
| XContentParser parser = contentType.xContent().createParser(parserConfig, originalSource.streamInput()); | ||
| builder.copyCurrentStructure(parser); | ||
| return BytesReference.bytes(builder); | ||
| }; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -518,6 +518,32 @@ public static BytesReference toXContent(ToXContent toXContent, XContentType xCon | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Guesses the content type based on the provided bytes which may be compressed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @deprecated the content type should not be guessed except for few cases where we effectively don't know the content type. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * The REST layer should move to reading the Content-Type header instead. There are other places where auto-detection may be needed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * This method is deprecated to prevent usages of it from spreading further without specific reasons. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Deprecated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static XContentType xContentTypeMayCompressed(BytesReference bytes) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compressor compressor = CompressorFactory.compressor(bytes); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (compressor != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| InputStream compressedStreamInput = compressor.threadLocalInputStream(bytes.streamInput()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (compressedStreamInput.markSupported() == false) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| compressedStreamInput = new BufferedInputStream(compressedStreamInput); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return XContentFactory.xContentType(compressedStreamInput); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (IOException e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert false : "Should not happen, we're just reading bytes from memory"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new UncheckedIOException(e); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return XContentHelper.xContentType(bytes); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+521
to
+545
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. Close the decompression stream to avoid leaking buffers.
🔧 Proposed fix- if (compressor != null) {
- try {
- InputStream compressedStreamInput = compressor.threadLocalInputStream(bytes.streamInput());
- if (compressedStreamInput.markSupported() == false) {
- compressedStreamInput = new BufferedInputStream(compressedStreamInput);
- }
- return XContentFactory.xContentType(compressedStreamInput);
+ if (compressor != null) {
+ try (
+ InputStream raw = compressor.threadLocalInputStream(bytes.streamInput());
+ InputStream in = raw.markSupported() ? raw : new BufferedInputStream(raw)
+ ) {
+ return XContentFactory.xContentType(in);
} catch (IOException e) {
assert false : "Should not happen, we're just reading bytes from memory";
throw new UncheckedIOException(e);
}
} else {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Guesses the content type based on the provided bytes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Parse compressed sources and close parser/builder.
xContentTypeMayCompressed(...)only infers type; the parser still reads the compressed bytes, which will fail when source is compressed. Also, the parser/builder aren’t closed. UseXContentHelper.createParser(...)and try-with-resources.🔧 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents