diff --git a/src/main/java/com/redislabs/university/RU102J/dao/SiteGeoDaoRedisImpl.java b/src/main/java/com/redislabs/university/RU102J/dao/SiteGeoDaoRedisImpl.java index 2114557..22e3755 100644 --- a/src/main/java/com/redislabs/university/RU102J/dao/SiteGeoDaoRedisImpl.java +++ b/src/main/java/com/redislabs/university/RU102J/dao/SiteGeoDaoRedisImpl.java @@ -52,42 +52,49 @@ public Set findByGeo(GeoQuery query) { } // Challenge #5 - private Set findSitesByGeoWithCapacity(GeoQuery query) { - return Collections.emptySet(); - } +// private Set findSitesByGeoWithCapacity(GeoQuery query) { +// return Collections.emptySet(); +// } // Comment out the above, and uncomment what's below -// private Set findSitesByGeoWithCapacity(GeoQuery query) { -// Set 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 radiusResponses; -// // END Challenge #5 -// -// Set 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> 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 findSitesByGeoWithCapacity(GeoQuery query) { + Set 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 radiusResponses; + String key = RedisSchema.getSiteGeoKey(); + List radiusResponses = jedis.georadius(key, coord.getLng(), coord.getLat(), radius, + radiusUnit); + // END Challenge #5 + + Set 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> 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 findSitesByGeo(GeoQuery query) { Coordinate coord = query.getCoordinate(); diff --git a/src/test/java/com/redislabs/university/RU102J/dao/SiteGeoDaoRedisImplTest.java b/src/test/java/com/redislabs/university/RU102J/dao/SiteGeoDaoRedisImplTest.java index e23b976..56bb57a 100644 --- a/src/test/java/com/redislabs/university/RU102J/dao/SiteGeoDaoRedisImplTest.java +++ b/src/test/java/com/redislabs/university/RU102J/dao/SiteGeoDaoRedisImplTest.java @@ -125,7 +125,6 @@ public void findByGeo() { } // Challenge #5 - @Ignore @Test public void findByGeoWithExcessCapacity() { SiteGeoDao siteDao = new SiteGeoDaoRedisImpl(jedisPool);