Skip to content

Commit

Permalink
Merge pull request #23 from traveltime-dev/add_more_supported_versions
Browse files Browse the repository at this point in the history
Add more supported versions
  • Loading branch information
mjanuszkiewicz-tt committed Sep 29, 2023
2 parents 65be12f + 42e0af1 commit c83f5e1
Show file tree
Hide file tree
Showing 54 changed files with 2,972 additions and 18 deletions.
6 changes: 3 additions & 3 deletions 5.5/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
def revisions = ['0', '1', '2', '3']
buildPlugin(this, '5.5', revisions)
def revisionList = revisions(3, false)
buildPlugin(this, '5.5', revisionList)

compileJava {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

revisions.forEach {
revisionList.forEach {
project.tasks.findByPath("pluginZip_$it").configure { into 'elasticsearch' }
}

Expand Down
2 changes: 1 addition & 1 deletion 7.10/build.gradle
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))
2 changes: 1 addition & 1 deletion 7.11/build.gradle
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))
2 changes: 1 addition & 1 deletion 7.12/build.gradle
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))
2 changes: 1 addition & 1 deletion 7.13/build.gradle
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))
2 changes: 1 addition & 1 deletion 7.14/build.gradle
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))
2 changes: 1 addition & 1 deletion 7.15/build.gradle
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))
2 changes: 1 addition & 1 deletion 7.16/build.gradle
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))
2 changes: 1 addition & 1 deletion 7.17/build.gradle
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))
2 changes: 1 addition & 1 deletion 8.0/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
buildPlugin(this, '8.0', ['0', '1', '2-SNAPSHOT'])
buildPlugin(this, '8.0', revisions(1))

compileJava {
sourceCompatibility = JavaVersion.VERSION_17
Expand Down
2 changes: 1 addition & 1 deletion 8.1/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
buildPlugin(this, '8.1', ['0', '1', '2', '3', '4-SNAPSHOT'])
buildPlugin(this, '8.1', revisions(3))

compileJava {
sourceCompatibility = JavaVersion.VERSION_17
Expand Down
2 changes: 1 addition & 1 deletion 8.10/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
buildPlugin(this, '8.10', ['0', '1', '2', '3-SNAPSHOT'])
buildPlugin(this, '8.10', revisions(2))

compileJava {
sourceCompatibility = JavaVersion.VERSION_17
Expand Down
2 changes: 1 addition & 1 deletion 8.2/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
buildPlugin(this, '8.2', ['0', '1', '2', '3', '4-SNAPSHOT'])
buildPlugin(this, '8.2', revisions(3))

compileJava {
sourceCompatibility = JavaVersion.VERSION_17
Expand Down
2 changes: 1 addition & 1 deletion 8.3/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
buildPlugin(this, '8.3', ['0', '1', '2', '3', '4-SNAPSHOT'])
buildPlugin(this, '8.3', revisions(3))

compileJava {
sourceCompatibility = JavaVersion.VERSION_17
Expand Down
2 changes: 1 addition & 1 deletion 8.4/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
buildPlugin(this, '8.4', ['0', '1', '2', '3', '4-SNAPSHOT'])
buildPlugin(this, '8.4', revisions(3))

compileJava {
sourceCompatibility = JavaVersion.VERSION_17
Expand Down
2 changes: 1 addition & 1 deletion 8.5/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
buildPlugin(this, '8.5', ['0', '1', '2', '3', '4-SNAPSHOT'])
buildPlugin(this, '8.5', revisions(3))

compileJava {
sourceCompatibility = JavaVersion.VERSION_17
Expand Down
6 changes: 6 additions & 0 deletions 8.6/build.gradle
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
}
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());
}
}
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()));
}
};
}
}
Loading

0 comments on commit c83f5e1

Please sign in to comment.