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 b3c69bf..6f2f4c5 100644 --- a/src/main/java/com/redislabs/university/RU102J/dao/SiteGeoDaoRedisImpl.java +++ b/src/main/java/com/redislabs/university/RU102J/dao/SiteGeoDaoRedisImpl.java @@ -52,10 +52,49 @@ public Set findByGeo(GeoQuery query) { } } + 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 + List radiusResponses = + jedis.georadius(RedisSchema.getSiteGeoKey(), 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()); + for (Site site : sites) { + Response score = pipeline.zscore(RedisSchema.getCapacityRankingKey(), + String.valueOf(site.getId())); + scores.put(site.getId(), score); + } + pipeline.sync(); + // END Challenge #5 + + for (Site site : sites) { + if (scores.get(site.getId()).get() >= capacityThreshold) { + results.add(site); + } + } + } + + return results; + } + // 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<>();