-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Implement runtime script ips #60533
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
nik9000
merged 5 commits into
elastic:feature/runtime_fields
from
nik9000:script_field_ip
Aug 5, 2020
Merged
Implement runtime script ips #60533
Changes from all commits
Commits
Show all changes
5 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
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
113 changes: 113 additions & 0 deletions
113
...ntime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/IpScriptFieldScript.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,113 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.runtimefields; | ||
|
||
import org.apache.lucene.document.InetAddressPoint; | ||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.apache.lucene.util.ArrayUtil; | ||
import org.apache.lucene.util.BytesRef; | ||
import org.elasticsearch.common.network.InetAddresses; | ||
import org.elasticsearch.index.mapper.IpFieldMapper; | ||
import org.elasticsearch.painless.spi.Whitelist; | ||
import org.elasticsearch.painless.spi.WhitelistLoader; | ||
import org.elasticsearch.script.ScriptContext; | ||
import org.elasticsearch.script.ScriptFactory; | ||
import org.elasticsearch.search.lookup.SearchLookup; | ||
|
||
import java.io.IOException; | ||
import java.net.Inet4Address; | ||
import java.net.Inet6Address; | ||
import java.net.InetAddress; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Script producing IP addresses. Unlike the other {@linkplain AbstractScriptFieldScript}s | ||
* which deal with their native java objects this converts its values to the same format | ||
* that Lucene uses to store its fields, {@link InetAddressPoint}. There are a few compelling | ||
* reasons to do this: | ||
* <ul> | ||
* <li>{@link Inet4Address}es and {@link Inet6Address} are not comparable with one another. | ||
* That is correct in some contexts, but not for our queries. Our queries must consider the | ||
* IPv4 address equal to the address that it maps to in IPv6 <a href="https://tools.ietf.org/html/rfc4291">rfc4291</a>). | ||
* <li>{@link InetAddress}es are not ordered, but we need to implement range queries with | ||
* same same ordering as {@link IpFieldMapper}. That also uses {@link InetAddressPoint} | ||
* so it saves us a lot of trouble to use the same representation. | ||
* </ul> | ||
*/ | ||
public abstract class IpScriptFieldScript extends AbstractScriptFieldScript { | ||
public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>("ip_script_field", Factory.class); | ||
|
||
static List<Whitelist> whitelist() { | ||
return List.of(WhitelistLoader.loadFromResourceFiles(RuntimeFieldsPainlessExtension.class, "ip_whitelist.txt")); | ||
} | ||
|
||
public static final String[] PARAMETERS = {}; | ||
|
||
public interface Factory extends ScriptFactory { | ||
LeafFactory newFactory(Map<String, Object> params, SearchLookup searchLookup); | ||
} | ||
|
||
public interface LeafFactory { | ||
IpScriptFieldScript newInstance(LeafReaderContext ctx) throws IOException; | ||
} | ||
|
||
private BytesRef[] values = new BytesRef[1]; | ||
private int count; | ||
|
||
public IpScriptFieldScript(Map<String, Object> params, SearchLookup searchLookup, LeafReaderContext ctx) { | ||
super(params, searchLookup, ctx); | ||
} | ||
|
||
/** | ||
* Execute the script for the provided {@code docId}. | ||
*/ | ||
public final void runForDoc(int docId) { | ||
count = 0; | ||
setDocument(docId); | ||
execute(); | ||
} | ||
|
||
/** | ||
* Values from the last time {@link #runForDoc(int)} was called. This array | ||
* is mutable and will change with the next call of {@link #runForDoc(int)}. | ||
* It is also oversized and will contain garbage at all indices at and | ||
* above {@link #count()}. | ||
* <p> | ||
* All values are IPv6 addresses so they are 16 bytes. IPv4 addresses are | ||
* encoded by <a href="https://tools.ietf.org/html/rfc4291">rfc4291</a>. | ||
*/ | ||
public final BytesRef[] values() { | ||
return values; | ||
} | ||
|
||
/** | ||
* The number of results produced the last time {@link #runForDoc(int)} was called. | ||
*/ | ||
public final int count() { | ||
return count; | ||
} | ||
|
||
private void collectValue(String v) { | ||
if (values.length < count + 1) { | ||
values = ArrayUtil.grow(values, count + 1); | ||
} | ||
values[count++] = new BytesRef(InetAddressPoint.encode(InetAddresses.forString(v))); | ||
} | ||
|
||
public static class StringValue { | ||
private final IpScriptFieldScript script; | ||
|
||
public StringValue(IpScriptFieldScript script) { | ||
this.script = script; | ||
} | ||
|
||
public void stringValue(String v) { | ||
script.collectValue(v); | ||
} | ||
} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
...elds/src/main/java/org/elasticsearch/xpack/runtimefields/fielddata/ScriptIpDocValues.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,44 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.runtimefields.fielddata; | ||
|
||
import org.apache.lucene.util.BytesRef; | ||
import org.elasticsearch.index.fielddata.SortedBinaryDocValues; | ||
import org.elasticsearch.xpack.runtimefields.IpScriptFieldScript; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
|
||
public final class ScriptIpDocValues extends SortedBinaryDocValues { | ||
private final IpScriptFieldScript script; | ||
private int cursor; | ||
|
||
ScriptIpDocValues(IpScriptFieldScript script) { | ||
this.script = script; | ||
} | ||
|
||
@Override | ||
public boolean advanceExact(int docId) { | ||
script.runForDoc(docId); | ||
if (script.count() == 0) { | ||
return false; | ||
} | ||
Arrays.sort(script.values(), 0, script.count()); | ||
cursor = 0; | ||
return true; | ||
} | ||
|
||
@Override | ||
public BytesRef nextValue() throws IOException { | ||
return script.values()[cursor++]; | ||
} | ||
|
||
@Override | ||
public int docValueCount() { | ||
return script.count(); | ||
} | ||
} |
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.
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.
been thinking about these static methods that we are sharing here and there: should we have a better way to formalize the fact that runtime fields share some bits with their corresponding concrete field? e.g. should they share some base class or something along those lines? Or would it make any sense for runtime field type to extend its corresponding concrete field type? probably both are bad ideas, but probably good to look into what the alternatives are.
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.
I talked with @javanna and we decided that we should keep this in mind, but we don't really have a cleaner way to do it for now. There are certainly other ways, but they aren't obviously cleaner.