-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from traveltime-dev/add_more_supported_versions
Add more supported versions
- Loading branch information
Showing
54 changed files
with
2,973 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1 +1 @@ | ||
buildPlugin(this, '7.10', ['0', '1', '2', '3-SNAPSHOT']) | ||
buildPlugin(this, '7.10', revisions(2)) |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
buildPlugin(this, '7.11', ['0', '1', '2', '3-SNAPSHOT']) | ||
buildPlugin(this, '7.11', revisions(2)) |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
buildPlugin(this, '7.12', ['0', '1', '2-SNAPSHOT']) | ||
buildPlugin(this, '7.12', revisions(1)) |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
buildPlugin(this, '7.13', ['0', '1', '2', '3', '4', '5-SNAPSHOT']) | ||
buildPlugin(this, '7.13', revisions(4)) |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
buildPlugin(this, '7.14', ['0', '1', '2', '3-SNAPSHOT']) | ||
buildPlugin(this, '7.14', revisions(2)) |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
buildPlugin(this, '7.15', ['0', '1', '2', '3-SNAPSHOT']) | ||
buildPlugin(this, '7.15', revisions(2)) |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
buildPlugin(this, '7.16', ['0', '1', '2', '3', '4-SNAPSHOT']) | ||
buildPlugin(this, '7.16', revisions(3)) |
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
buildPlugin(this, '7.17', (0..17).collect {it.toString()} + '18-SNAPSHOT') | ||
buildPlugin(this, '7.17', revisions(17)) |
This file contains 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 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 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 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 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 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 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 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,6 @@ | ||
buildPlugin(this, '8.6', revisions(2)) | ||
|
||
compileJava { | ||
sourceCompatibility = JavaVersion.VERSION_17 | ||
targetCompatibility = JavaVersion.VERSION_17 | ||
} |
93 changes: 93 additions & 0 deletions
93
8.6/src/main/java/com/traveltime/plugin/elasticsearch/TraveltimePlugin.java
This file contains 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,93 @@ | ||
package com.traveltime.plugin.elasticsearch; | ||
|
||
|
||
import com.traveltime.plugin.elasticsearch.query.TraveltimeFetchPhase; | ||
import com.traveltime.plugin.elasticsearch.query.TraveltimeQueryBuilder; | ||
import com.traveltime.plugin.elasticsearch.query.TraveltimeQueryParser; | ||
import com.traveltime.plugin.elasticsearch.util.Util; | ||
import com.traveltime.sdk.dto.requests.proto.Country; | ||
import com.traveltime.sdk.dto.requests.proto.Transportation; | ||
import org.elasticsearch.client.internal.Client; | ||
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; | ||
import org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders; | ||
import org.elasticsearch.cluster.service.ClusterService; | ||
import org.elasticsearch.common.io.stream.NamedWriteableRegistry; | ||
import org.elasticsearch.common.settings.Setting; | ||
import org.elasticsearch.core.TimeValue; | ||
import org.elasticsearch.env.Environment; | ||
import org.elasticsearch.env.NodeEnvironment; | ||
import org.elasticsearch.plugins.Plugin; | ||
import org.elasticsearch.plugins.SearchPlugin; | ||
import org.elasticsearch.repositories.RepositoriesService; | ||
import org.elasticsearch.script.ScriptService; | ||
import org.elasticsearch.search.fetch.FetchSubPhase; | ||
import org.elasticsearch.threadpool.ThreadPool; | ||
import org.elasticsearch.tracing.Tracer; | ||
import org.elasticsearch.watcher.ResourceWatcherService; | ||
import org.elasticsearch.xcontent.NamedXContentRegistry; | ||
|
||
import java.net.URI; | ||
import java.time.Duration; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
|
||
public class TraveltimePlugin extends Plugin implements SearchPlugin { | ||
public static final Setting<String> APP_ID = Setting.simpleString("traveltime.app.id", Setting.Property.NodeScope); | ||
public static final Setting<String> API_KEY = Setting.simpleString("traveltime.api.key", Setting.Property.NodeScope, Setting.Property.Filtered); | ||
public static final Setting<Optional<Transportation>> DEFAULT_MODE = new Setting<>("traveltime.default.mode", s -> "", Util::findModeByName, Setting.Property.NodeScope); | ||
public static final Setting<Optional<Country>> DEFAULT_COUNTRY = new Setting<>("traveltime.default.country", s -> "", Util::findCountryByName, Setting.Property.NodeScope); | ||
public static final Setting<URI> API_URI = new Setting<>("traveltime.api.uri", s -> "https://proto.api.traveltimeapp.com/api/v2/", URI::create, Setting.Property.NodeScope); | ||
|
||
private static final Setting<Integer> CACHE_CLEANUP_INTERVAL = Setting.intSetting("traveltime.cache.cleanup.interval", 120, 0, Setting.Property.NodeScope); | ||
private static final Setting<Integer> CACHE_EXPIRY = Setting.intSetting("traveltime.cache.expiry", 60, 0, Setting.Property.NodeScope); | ||
private static final Setting<Integer> CACHE_SIZE = Setting.intSetting("traveltime.cache.size", 50, 0, Setting.Property.NodeScope); | ||
|
||
private void cleanUpAndReschedule(ThreadPool threadPool, TimeValue cleanupSeconds) { | ||
TraveltimeCache.INSTANCE.cleanUp(); | ||
threadPool.scheduleUnlessShuttingDown(cleanupSeconds, "generic", () -> cleanUpAndReschedule(threadPool, cleanupSeconds)); | ||
} | ||
|
||
@Override | ||
public Collection<Object> createComponents( | ||
Client client, | ||
ClusterService clusterService, | ||
ThreadPool threadPool, | ||
ResourceWatcherService resourceWatcherService, | ||
ScriptService scriptService, | ||
NamedXContentRegistry xContentRegistry, | ||
Environment environment, | ||
NodeEnvironment nodeEnvironment, | ||
NamedWriteableRegistry namedWriteableRegistry, | ||
IndexNameExpressionResolver indexNameExpressionResolver, | ||
Supplier<RepositoriesService> repositoriesServiceSupplier, | ||
Tracer tracer, | ||
AllocationDeciders allocationService | ||
) { | ||
TimeValue cleanupSeconds = TimeValue.timeValueSeconds(CACHE_CLEANUP_INTERVAL.get(environment.settings())); | ||
Duration cacheExpiry = Duration.ofSeconds(CACHE_EXPIRY.get(environment.settings())); | ||
Integer cacheSize = CACHE_SIZE.get(environment.settings()); | ||
|
||
TraveltimeCache.INSTANCE.setUp(cacheSize, cacheExpiry); | ||
cleanUpAndReschedule(threadPool, cleanupSeconds); | ||
|
||
return super.createComponents(client, clusterService, threadPool, resourceWatcherService, scriptService, xContentRegistry, environment, nodeEnvironment, namedWriteableRegistry, indexNameExpressionResolver, repositoriesServiceSupplier, tracer, allocationService); | ||
|
||
} | ||
|
||
@Override | ||
public List<Setting<?>> getSettings() { | ||
return List.of(APP_ID, API_KEY, DEFAULT_MODE, DEFAULT_COUNTRY, API_URI, CACHE_SIZE, CACHE_EXPIRY, CACHE_CLEANUP_INTERVAL); | ||
} | ||
|
||
@Override | ||
public List<QuerySpec<?>> getQueries() { | ||
return List.of(new QuerySpec<>(TraveltimeQueryParser.NAME, TraveltimeQueryBuilder::new, new TraveltimeQueryParser())); | ||
} | ||
|
||
@Override | ||
public List<FetchSubPhase> getFetchSubPhases(FetchPhaseConstructionContext context) { | ||
return List.of(new TraveltimeFetchPhase()); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
8.6/src/main/java/com/traveltime/plugin/elasticsearch/query/TraveltimeFetchPhase.java
This file contains 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,77 @@ | ||
package com.traveltime.plugin.elasticsearch.query; | ||
|
||
import com.traveltime.plugin.elasticsearch.TraveltimeCache; | ||
import lombok.val; | ||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.apache.lucene.search.Query; | ||
import org.apache.lucene.search.QueryVisitor; | ||
import org.elasticsearch.common.document.DocumentField; | ||
import org.elasticsearch.search.fetch.FetchContext; | ||
import org.elasticsearch.search.fetch.FetchSubPhase; | ||
import org.elasticsearch.search.fetch.FetchSubPhaseProcessor; | ||
import org.elasticsearch.search.fetch.StoredFieldsSpec; | ||
import org.elasticsearch.search.fetch.subphase.FieldAndFormat; | ||
import org.elasticsearch.search.fetch.subphase.FieldFetcher; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class TraveltimeFetchPhase implements FetchSubPhase { | ||
|
||
private static class ParamFinder extends QueryVisitor { | ||
private final List<TraveltimeSearchQuery> paramList = new ArrayList<>(); | ||
|
||
@Override | ||
public void visitLeaf(Query query) { | ||
if (query instanceof TraveltimeSearchQuery) { | ||
if (!((TraveltimeSearchQuery) query).getOutput().isEmpty()) { | ||
paramList.add(((TraveltimeSearchQuery) query)); | ||
} | ||
} | ||
} | ||
|
||
public TraveltimeSearchQuery getQuery() { | ||
if (paramList.size() == 1) return paramList.get(0); | ||
else return null; | ||
} | ||
} | ||
|
||
@Override | ||
public FetchSubPhaseProcessor getProcessor(FetchContext fetchContext) { | ||
Query query = fetchContext.query(); | ||
val finder = new ParamFinder(); | ||
query.visit(finder); | ||
TraveltimeSearchQuery traveltimeQuery = finder.getQuery(); | ||
if (traveltimeQuery == null) return null; | ||
TraveltimeQueryParameters params = traveltimeQuery.getParams(); | ||
final String output = traveltimeQuery.getOutput(); | ||
|
||
FieldFetcher fieldFetcher = FieldFetcher.create(fetchContext.getSearchExecutionContext(), List.of(new FieldAndFormat(params.getField(), null))); | ||
|
||
return new FetchSubPhaseProcessor() { | ||
|
||
@Override | ||
public void setNextReader(LeafReaderContext readerContext) { | ||
fieldFetcher.setNextReader(readerContext); | ||
} | ||
|
||
@Override | ||
public void process(HitContext hitContext) throws IOException { | ||
val docValues = hitContext.reader().getSortedNumericDocValues(params.getField()); | ||
docValues.advance(hitContext.docId()); | ||
Integer tt = TraveltimeCache.INSTANCE.get(params, docValues.nextValue()); | ||
|
||
if (tt > 0) { | ||
hitContext.hit().setDocumentField(output, new DocumentField(output, List.of(tt))); | ||
} | ||
} | ||
|
||
@Override | ||
public StoredFieldsSpec storedFieldsSpec() { | ||
return new StoredFieldsSpec(false, false, Set.of(params.getField())); | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.