Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Govind Balaji S committed Oct 16, 2022
1 parent 069f5a5 commit 952a48b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,42 +52,49 @@ public Set<Site> findByGeo(GeoQuery query) {
}

// Challenge #5
private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) {
return Collections.emptySet();
}
// private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) {
// return Collections.emptySet();
// }
// Comment out the above, and uncomment what's below
// private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) {
// Set<Site> results = new HashSet<>();
// Coordinate coord = query.getCoordinate();
// Double radius = query.getRadius();
// GeoUnit radiusUnit = query.getRadiusUnit();
//
// try (Jedis jedis = jedisPool.getResource()) {
// // START Challenge #5
// // TODO: Challenge #5: Get the sites matching the geo query, store them
// // in List<GeoRadiusResponse> radiusResponses;
// // END Challenge #5
//
// Set<Site> sites = radiusResponses.stream()
// .map(response -> jedis.hgetAll(response.getMemberByString()))
// .filter(Objects::nonNull)
// .map(Site::new).collect(Collectors.toSet());
//
// // START Challenge #5
// Pipeline pipeline = jedis.pipelined();
// Map<Long, Response<Double>> scores = new HashMap<>(sites.size());
// // TODO: Challenge #5: Add the code that populates the scores HashMap...
// // END Challenge #5
//
// for (Site site : sites) {
// if (scores.get(site.getId()).get() >= capacityThreshold) {
// results.add(site);
// }
// }
// }
//
// return results;
// }
private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) {
Set<Site> results = new HashSet<>();
Coordinate coord = query.getCoordinate();
Double radius = query.getRadius();
GeoUnit radiusUnit = query.getRadiusUnit();

try (Jedis jedis = jedisPool.getResource()) {
// START Challenge #5
// TODODONE: Challenge #5: Get the sites matching the geo query, store them
// in List<GeoRadiusResponse> radiusResponses;
String key = RedisSchema.getSiteGeoKey();
List<GeoRadiusResponse> radiusResponses = jedis.georadius(key, coord.getLng(), coord.getLat(), radius,
radiusUnit);
// END Challenge #5

Set<Site> sites = radiusResponses.stream()
.map(response -> jedis.hgetAll(response.getMemberByString()))
.filter(Objects::nonNull)
.map(Site::new).collect(Collectors.toSet());

// START Challenge #5
Pipeline pipeline = jedis.pipelined();
Map<Long, Response<Double>> scores = new HashMap<>(sites.size());
// TODO: Challenge #5: Add the code that populates the scores HashMap...
for (Site site : sites) {
scores.put(site.getId(), pipeline.zscore(RedisSchema.getCapacityRankingKey(), site.getId().toString()));
}
pipeline.sync();
// END Challenge #5

for (Site site : sites) {
if (scores.get(site.getId()).get() >= capacityThreshold) {
results.add(site);
}
}
}

return results;
}

private Set<Site> findSitesByGeo(GeoQuery query) {
Coordinate coord = query.getCoordinate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ public void findByGeo() {
}

// Challenge #5
@Ignore
@Test
public void findByGeoWithExcessCapacity() {
SiteGeoDao siteDao = new SiteGeoDaoRedisImpl(jedisPool);
Expand Down

0 comments on commit 952a48b

Please sign in to comment.