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 d58ae00..c8ba924 100644 --- a/src/main/java/com/redislabs/university/RU102J/dao/SiteGeoDaoRedisImpl.java +++ b/src/main/java/com/redislabs/university/RU102J/dao/SiteGeoDaoRedisImpl.java @@ -8,6 +8,8 @@ import java.util.*; import java.util.stream.Collectors; +import static com.redislabs.university.RU102J.dao.RedisSchema.getSiteGeoKey; + public class SiteGeoDaoRedisImpl implements SiteGeoDao { private JedisPool jedisPool; final static private Double capacityThreshold = 0.2; @@ -31,7 +33,7 @@ public Site findById(long id) { @Override public Set findAll() { try (Jedis jedis = jedisPool.getResource()) { - Set keys = jedis.zrange(RedisSchema.getSiteGeoKey(), 0, -1); + Set keys = jedis.zrange(getSiteGeoKey(), 0, -1); Set sites = new HashSet<>(keys.size()); Pipeline p = jedis.pipelined(); @@ -47,7 +49,7 @@ public Set findAll() { sites.add(new Site(response.get())); } } - + return sites; } } @@ -62,42 +64,45 @@ public Set findByGeo(GeoQuery query) { } // Challenge #5 - 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 + // TODO: Challenge #5: Get the sites matching the geo query, store them + // in List radiusResponses; + List radiusResponses = + jedis.georadius(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()); + // TODO: Challenge #5: Add the code that populates the scores HashMap... + for (Site s : sites) { + Response score = pipeline.zscore(RedisSchema.getCapacityRankingKey(), String.valueOf(s.getId())); + scores.put(s.getId(), score); + } + 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(); @@ -106,7 +111,7 @@ private Set findSitesByGeo(GeoQuery query) { try (Jedis jedis = jedisPool.getResource()) { List radiusResponses = - jedis.georadius(RedisSchema.getSiteGeoKey(), coord.getLng(), + jedis.georadius(getSiteGeoKey(), coord.getLng(), coord.getLat(), radius, radiusUnit); return radiusResponses.stream() @@ -128,7 +133,7 @@ public void insert(Site site) { } Double longitude = site.getCoordinate().getGeoCoordinate().getLongitude(); Double latitude = site.getCoordinate().getGeoCoordinate().getLatitude(); - jedis.geoadd(RedisSchema.getSiteGeoKey(), longitude, latitude, + jedis.geoadd(getSiteGeoKey(), longitude, latitude, key); } } 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..7abaa92 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,7 @@ public void findByGeo() { } // Challenge #5 - @Ignore +// @Ignore @Test public void findByGeoWithExcessCapacity() { SiteGeoDao siteDao = new SiteGeoDaoRedisImpl(jedisPool);