Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement `findSitesByGeoWithCapacity()` in `SiteGeoDaoRedisImpl.java`.
  • Loading branch information
J-A-I-L committed Jul 8, 2022
1 parent 28c2a1a commit d9e1491
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public void run(final RediSolarConfiguration configuration,
}

// To use the geospatial features, replace the following lines with:
// SiteGeoResource siteResource =
// new SiteGeoResource(new SiteGeoDaoRedisImpl(jedisPool));
SiteResource siteResource =
new SiteResource(new SiteDaoRedisImpl(jedisPool));
SiteGeoResource siteResource =
new SiteGeoResource(new SiteGeoDaoRedisImpl(jedisPool));
// SiteResource siteResource =
// new SiteResource(new SiteDaoRedisImpl(jedisPool));
environment.jersey().register(siteResource);

// For RedisTimeSeries: replace the next lines with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,19 @@ public Set<Site> findAll() {
try (Jedis jedis = jedisPool.getResource()) {
Set<String> keys = jedis.zrange(RedisSchema.getSiteGeoKey(), 0, -1);
Set<Site> sites = new HashSet<>(keys.size());
for (String key : keys) {
Map<String, String> site = jedis.hgetAll(key);
if (!site.isEmpty()) {
sites.add(new Site(site));
}
}
final Pipeline pipeline = jedis.pipelined();
// for (String key : keys) {
// Map<String, String> site = jedis.hgetAll(key);
// if (!site.isEmpty()) {
// sites.add(new Site(site));
// }
// }
keys.forEach(pipeline::hgetAll);
sites = pipeline.syncAndReturnAll().stream()
.filter(siteObject -> siteObject instanceof Map)
.map(siteObject -> (Map<String, String>) siteObject)
.filter(site -> !site.isEmpty())
.map(Site::new).collect(Collectors.toSet());
return sites;
}
}
Expand All @@ -53,42 +60,51 @@ 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
// TODO: Challenge #5: Get the sites matching the geo query, store them
// in List<GeoRadiusResponse> radiusResponses;
List<GeoRadiusResponse> radiusResponses =
jedis.georadius(RedisSchema.getSiteGeoKey(), 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...
String key = RedisSchema.getCapacityRankingKey();
for (Site site : sites) {
Response<Double> rankResponse = pipeline.zscore(key, site.getId().toString());
scores.put(site.getId(), rankResponse);
}
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 d9e1491

Please sign in to comment.