Skip to content

Commit

Permalink
Merge pull request #22 from traveltime-dev/9.4-support
Browse files Browse the repository at this point in the history
9.4+ support
  • Loading branch information
mjanuszkiewicz-tt authored May 31, 2024
2 parents 0ed1ffb + da4ec41 commit becf974
Show file tree
Hide file tree
Showing 71 changed files with 105,970 additions and 2 deletions.
2 changes: 1 addition & 1 deletion 9/build.gradle → 9.0-3/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
buildPlugin(this, "9.1.0")
buildPlugin(this, "9.3.0")
configure(this) {
dependencies {
compileOnly 'org.locationtech.spatial4j:spatial4j:0.8'
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions 9.4/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
buildPlugin(this, "9.6.1")
configure(this) {
dependencies {
compileOnly 'org.locationtech.spatial4j:spatial4j:0.8'
compileOnly 'com.github.ben-manes.caffeine:caffeine:3.1.1'
}

compileJava {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.traveltime.plugin.solr;

import com.traveltime.plugin.solr.cache.RequestCache;
import com.traveltime.plugin.solr.fetcher.JsonFetcherSingleton;
import com.traveltime.plugin.solr.query.timefilter.TimeFilterQueryParser;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.search.QParser;
import org.apache.solr.search.QParserPlugin;

import java.net.URI;
import java.util.Optional;

import static com.traveltime.plugin.solr.query.ParamSource.PARAM_PREFIX;

public class TimeFilterQParserPlugin extends QParserPlugin {
private String cacheName = RequestCache.NAME;
private boolean isFilteringDisabled = false;
private String paramPrefix = PARAM_PREFIX;

private static final Integer DEFAULT_LOCATION_SIZE_LIMIT = 2000;

@Override
public void init(NamedList args) {
Object cache = args.get("cache");
if (cache != null) cacheName = cache.toString();

Object filteringDisabled = args.get("filtering_disabled");
if (filteringDisabled != null) this.isFilteringDisabled = Boolean.parseBoolean(filteringDisabled.toString());

Object prefix = args.get("prefix");
if (prefix != null) paramPrefix = prefix.toString();

Object uriVal = args.get("api_uri");
URI uri = null;
if (uriVal != null) uri = URI.create(uriVal.toString());

String appId = args.get("app_id").toString();
String apiKey = args.get("api_key").toString();
int locationLimit =
Optional.ofNullable(args.get("location_limit"))
.map(x -> Integer.parseInt(x.toString()))
.orElse(DEFAULT_LOCATION_SIZE_LIMIT);

JsonFetcherSingleton.INSTANCE.init(uri, appId, apiKey, locationLimit);
}

@Override
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
return new TimeFilterQueryParser(
qstr,
localParams,
params,
req,
JsonFetcherSingleton.INSTANCE.getFetcher(),
cacheName,
isFilteringDisabled,
paramPrefix
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.traveltime.plugin.solr;

import com.traveltime.plugin.solr.cache.RequestCache;
import com.traveltime.plugin.solr.fetcher.ProtoFetcherSingleton;
import com.traveltime.plugin.solr.query.TravelTimeQueryParser;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.search.QParser;
import org.apache.solr.search.QParserPlugin;

import java.net.URI;

import static com.traveltime.plugin.solr.query.ParamSource.PARAM_PREFIX;

public class TravelTimeQParserPlugin extends QParserPlugin {
private String cacheName = RequestCache.NAME;
private boolean isFilteringDisabled = false;
private String paramPrefix = PARAM_PREFIX;

@Override
public void init(NamedList args) {
Object cache = args.get("cache");
if (cache != null) cacheName = cache.toString();

Object filteringDisabled = args.get("filtering_disabled");
if (filteringDisabled != null) this.isFilteringDisabled = Boolean.parseBoolean(filteringDisabled.toString());

Object prefix = args.get("prefix");
if (prefix != null) paramPrefix = prefix.toString();

Object uriVal = args.get("api_uri");
URI uri = null;
if (uriVal != null) uri = URI.create(uriVal.toString());

String appId = args.get("app_id").toString();
String apiKey = args.get("api_key").toString();
ProtoFetcherSingleton.INSTANCE.init(uri, appId, apiKey);
}

@Override
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
return new TravelTimeQueryParser(
qstr,
localParams,
params,
req,
ProtoFetcherSingleton.INSTANCE.getFetcher(),
cacheName,
isFilteringDisabled,
paramPrefix
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.traveltime.plugin.solr.cache;

import com.traveltime.sdk.dto.common.Coordinates;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectCollection;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import lombok.val;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.locks.StampedLock;

public class BasicTravelTimes extends TravelTimes {
private final StampedLock rwLock = new StampedLock();
private final Object2IntOpenHashMap<Coordinates> coordsToTimes = new Object2IntOpenHashMap<>();

@Override
public Set<Coordinates> nonCached(int ignored, ObjectCollection<Coordinates> coords) {
long read = rwLock.readLock();
try {
val nonCachedSet = new ObjectOpenHashSet<Coordinates>();
coords.forEach(coord -> {
if (!coordsToTimes.containsKey(coord)) {
nonCachedSet.add(coord);
}
}
);
return nonCachedSet;
} finally {
rwLock.unlock(read);
}
}

@Override
public void putAll(int ignored, ArrayList<Coordinates> coords, List<Integer> times) {
long write = rwLock.writeLock();
try {
for (int index = 0; index < times.size(); index++) {
coordsToTimes.put(coords.get(index), times.get(index).intValue());
}
} finally {
rwLock.unlock(write);
}
}

@Override
public Object2IntOpenHashMap<Coordinates> mapToTimes(int ignored, ObjectCollection<Coordinates> coords) {
long read = rwLock.readLock();
try {
val pointToTime = new Object2IntOpenHashMap<Coordinates>(coords.size());
coords.forEach(coord -> {
int time = coordsToTimes.getOrDefault(coord, -1);
if (time > 0) {
pointToTime.put(coord, time);
}
});

return pointToTime;
} finally {
rwLock.unlock(read);
}
}

@Override
public int get(Coordinates coord) {
long read = rwLock.tryOptimisticRead();
int time = coordsToTimes.getOrDefault(coord, -1);
if (!rwLock.validate(read)) {
read = rwLock.readLock();
try {
time = coordsToTimes.getOrDefault(coord, -1);
} finally {
rwLock.unlock(read);
}
}
return time;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.traveltime.plugin.solr.cache;

import com.traveltime.plugin.solr.query.TravelTimeQueryParameters;

public class ExactRequestCache extends RequestCache<TravelTimeQueryParameters> {
private final Object[] lock = new Object[0];

@Override
public TravelTimes getOrFresh(TravelTimeQueryParameters key) {
TravelTimes result = get(key);
if (result == null) {
synchronized (lock) {
result = get(key);
if (result == null) {
result = new BasicTravelTimes();
put(key, result);
}
}
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.traveltime.plugin.solr.cache;

import com.traveltime.plugin.solr.query.timefilter.TimeFilterQueryParameters;

public class ExactTimeFilterRequestCache extends RequestCache<TimeFilterQueryParameters> {
private final Object[] lock = new Object[0];

@Override
public TravelTimes getOrFresh(TimeFilterQueryParameters key) {
TravelTimes result = get(key);
if (result == null) {
synchronized (lock) {
result = get(key);
if (result == null) {
result = new BasicTravelTimes();
put(key, result);
}
}
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.traveltime.plugin.solr.cache;

import com.traveltime.plugin.solr.query.TravelTimeQueryParameters;
import org.apache.solr.search.CacheRegenerator;

import java.util.Map;

public class FuzzyRequestCache extends RequestCache<TravelTimeQueryParameters> {
private final Object[] lock = new Object[0];
private Map<String, String> args;

@Override
public Object init(Map<String, String> args, Object persistence, CacheRegenerator regenerator) {
this.args = args;
return super.init(args, persistence, regenerator);
}

@Override
public TravelTimes getOrFresh(TravelTimeQueryParameters key) {
key = new TravelTimeQueryParameters(null, key.getOrigin(), 0, key.getMode(), null);
TravelTimes result = get(key);
if (result == null) {
synchronized (lock) {
result = get(key);
if (result == null) {
result = new LRUTimes(args);
put(key, result);
}
}
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.traveltime.plugin.solr.cache;

import com.traveltime.plugin.solr.query.timefilter.TimeFilterQueryParameters;
import org.apache.solr.search.CacheRegenerator;

import java.util.Map;

public class FuzzyTimeFilterRequestCache extends RequestCache<TimeFilterQueryParameters> {
private final Object[] lock = new Object[0];
private Map<String, String> args;

@Override
public Object init(Map<String, String> args, Object persistence, CacheRegenerator regenerator) {
this.args = args;
return super.init(args, persistence, regenerator);
}

@Override
public TravelTimes getOrFresh(TimeFilterQueryParameters key) {
key = key.withField(null).withTravelTime(0);
TravelTimes result = get(key);
if (result == null) {
synchronized (lock) {
result = get(key);
if (result == null) {
result = new LRUTimes(args);
put(key, result);
}
}
}
return result;
}
}
Loading

0 comments on commit becf974

Please sign in to comment.