Skip to content

Commit

Permalink
Merge pull request #19 from traveltime-dev/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
mjanuszkiewicz-tt authored Mar 7, 2023
2 parents aeba8d8 + eb8ed5c commit dbc3f61
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.Map;

public abstract class RequestCache<P> extends FastLRUCache<P, TravelTimes> {
public static String NAME = "traveltime";
public static final String NAME = "traveltime";

public abstract TravelTimes getOrFresh(P key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,17 @@

class TravelTimeScorer extends Scorer {

private DocIdSetIterator it;
private final DocIdSetIterator it;
private int doc = -1;
private final float queryWeight;
private final NumericDocValues docValues;
private final int limit;
// Stored as a double so conversion from int to double does not have to be
// done every time the score is calculated.
private final double limitAsDouble;
private final TravelTimes travelTimes;

protected TravelTimeScorer(int limit, TravelTimes travelTimes, Weight weight, float queryWeight, NumericDocValues docValues, DocIdSetIterator docs) {
super(weight);
this.limit = limit;
this.limitAsDouble = limit;
this.travelTimes = travelTimes;
this.queryWeight = queryWeight;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.Map;

public abstract class RequestCache<P> extends FastLRUCache<P, TravelTimes> {
public static String NAME = "traveltime";
public static final String NAME = "traveltime";

public abstract TravelTimes getOrFresh(P key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,17 @@

class TravelTimeScorer extends Scorer {

private DocIdSetIterator it;
private final DocIdSetIterator it;
private int doc = -1;
private final float boost;
private final NumericDocValues docValues;
private final int limit;
// Stored as a double so conversion from int to double does not have to be
// done every time the score is calculated.
private final double limitAsDouble;
private final TravelTimes travelTimes;

protected TravelTimeScorer(int limit, TravelTimes travelTimes, Weight weight, float boost, NumericDocValues docValues) {
super(weight);
this.limit = limit;
this.limitAsDouble = limit;
this.travelTimes = travelTimes;
this.boost = boost;
Expand All @@ -71,34 +69,6 @@ private float score(int travelTime) {
return travelTime == -1.0 ? 0.0f : (float) (boost * (limitAsDouble / (limitAsDouble + travelTime)));
}

/**
* Inverting the score computation is very hard due to all potential
* rounding errors, so we binary search the maximum travel time. The
* limit is set to 1 second.
*/
private int computeMaxTravelTime(float minScore, int previousMaxTravelTime) {
assert score(0) >= minScore;
if (score(previousMaxTravelTime) >= minScore) {
// minScore did not decrease enough to require an update to the max travel time
return previousMaxTravelTime;
}
assert score(previousMaxTravelTime) < minScore;
int min = 0, max = previousMaxTravelTime;
// invariant: score(min) >= minScore && score(max) < minScore
while (max - min > 1) {
int mid = (min + max) / 2;
float score = score(mid);
if (score >= minScore) {
min = mid;
} else {
max = mid;
}
}
assert score(min) >= minScore;
assert min == limit || score(min + 1) < minScore;
return min;
}

@Override
public float score() throws IOException {
long encodedDocumentCoordinate = docValues.longValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,16 @@ public Explanation explain(LeafReaderContext context, int doc) throws IOExceptio
float score = travelTime == -1 ? 0.0f : (float) (boost * (limitAsDouble / (limitAsDouble + travelTime)));

Coordinates queryOrigin = params.getOrigin();
return Explanation.match(score,
params.getTransportMode() + " score, computed as, when present, boost * limit / (limit + travelTime), otherwise 0.0, from:",
Explanation.match(boost, "weight"),
Explanation.match(limit, "maximum travel time"),
Explanation.match(queryOrigin.getLat().floatValue(), "query lat"),
Explanation.match(queryOrigin.getLng().floatValue(), "query lon"),
Explanation.match((float)documentLat, "document lat"),
Explanation.match((float)documentLon, "document lon"),
Explanation.match(travelTime, "travel time")
return Explanation.match(
score,
params.getTransportMode() + " score, computed as, when present, boost * limit / (limit + travelTime), otherwise 0.0, from:",
Explanation.match(boost, "weight"),
Explanation.match(limit, "maximum travel time"),
Explanation.match(queryOrigin.getLat().floatValue(), "query lat"),
Explanation.match(queryOrigin.getLng().floatValue(), "query lon"),
Explanation.match((float) documentLat, "document lat"),
Explanation.match((float) documentLon, "document lon"),
Explanation.match(travelTime, "travel time")
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.Map;

public abstract class RequestCache<P> extends FastLRUCache<P, TravelTimes> {
public static String NAME = "traveltime";
public static final String NAME = "traveltime";

public abstract TravelTimes getOrFresh(P key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,16 @@ public Explanation explain(LeafReaderContext context, int doc) throws IOExceptio
float score = travelTime == -1 ? 0.0f : (float) (boost * (limitAsDouble / (limitAsDouble + travelTime)));

Coordinates queryOrigin = params.getOrigin();
return Explanation.match(score,
params.getTransportMode() + " score, computed as, when present, boost * limit / (limit + travelTime), otherwise 0.0, from:",
Explanation.match(boost, "weight"),
Explanation.match(limit, "maximum travel time"),
Explanation.match(queryOrigin.getLat(), "query lat"),
Explanation.match(queryOrigin.getLng(), "query lon"),
Explanation.match(documentLat, "document lat"),
Explanation.match(documentLon, "document lon"),
Explanation.match(travelTime, "travel time")
return Explanation.match(
score,
params.getTransportMode() + " score, computed as, when present, boost * limit / (limit + travelTime), otherwise 0.0, from:",
Explanation.match(boost, "weight"),
Explanation.match(limit, "maximum travel time"),
Explanation.match(queryOrigin.getLat(), "query lat"),
Explanation.match(queryOrigin.getLng(), "query lon"),
Explanation.match(documentLat, "document lat"),
Explanation.match(documentLon, "document lon"),
Explanation.match(travelTime, "travel time")
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class FuzzyTimeFilterRequestCache extends RequestCache<TimeFilterQueryPar
private Map<String, String> args;

@Override
public Object init(Map args, Object persistence, CacheRegenerator regenerator) {
public Object init(Map<String, String> args, Object persistence, CacheRegenerator regenerator) {
this.args = args;
return super.init(args, persistence, regenerator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.Map;

public abstract class RequestCache<P> extends CaffeineCache<P, TravelTimes> {
public static String NAME = "traveltime";
public static final String NAME = "traveltime";

public abstract TravelTimes getOrFresh(P key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public int getTravelTime() {

private static <T> T findByNameOrError(String what, String name, Function<String, Optional<T>> finder) {
val result = finder.apply(name);
if (!result.isPresent()) {
if (result.isEmpty()) {
throw new IllegalArgumentException(String.format("Couldn't find a %s with the name %s", what, name));
} else {
return result.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.apache.solr.search.SolrIndexSearcher;

import java.io.IOException;
import java.util.Set;

@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
Expand Down Expand Up @@ -132,15 +131,16 @@ public Explanation explain(LeafReaderContext context, int doc) throws IOExceptio
float score = travelTime == -1 ? 0.0f : (float) (boost * (limitAsDouble / (limitAsDouble + travelTime)));

Coordinates queryOrigin = params.getOrigin();
return Explanation.match(score,
params.getTransportMode() + " score, computed as, when present, boost * limit / (limit + travelTime), otherwise 0.0, from:",
Explanation.match(boost, "weight"),
Explanation.match(limit, "maximum travel time"),
Explanation.match(queryOrigin.getLat(), "query lat"),
Explanation.match(queryOrigin.getLng(), "query lon"),
Explanation.match(documentLat, "document lat"),
Explanation.match(documentLon, "document lon"),
Explanation.match(travelTime, "travel time")
return Explanation.match(
score,
params.getTransportMode() + " score, computed as, when present, boost * limit / (limit + travelTime), otherwise 0.0, from:",
Explanation.match(boost, "weight"),
Explanation.match(limit, "maximum travel time"),
Explanation.match(queryOrigin.getLat(), "query lat"),
Explanation.match(queryOrigin.getLng(), "query lon"),
Explanation.match(documentLat, "document lat"),
Explanation.match(documentLon, "document lon"),
Explanation.match(travelTime, "travel time")
);
}

Expand Down

0 comments on commit dbc3f61

Please sign in to comment.