Skip to content

Commit 6170608

Browse files
committed
1 parent 9f339bf commit 6170608

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

src/main/java/com/redislabs/university/RU102J/dao/SiteGeoDaoRedisImpl.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,49 @@ public Set<Site> findByGeo(GeoQuery query) {
5252
}
5353
}
5454

55+
private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) {
56+
Set<Site> results = new HashSet<>();
57+
Coordinate coord = query.getCoordinate();
58+
Double radius = query.getRadius();
59+
GeoUnit radiusUnit = query.getRadiusUnit();
60+
61+
try (Jedis jedis = jedisPool.getResource()) {
62+
// START Challenge #5
63+
List<GeoRadiusResponse> radiusResponses =
64+
jedis.georadius(RedisSchema.getSiteGeoKey(), coord.getLng(),
65+
coord.getLat(), radius, radiusUnit);
66+
// END Challenge #5
67+
68+
Set<Site> sites = radiusResponses.stream()
69+
.map(response -> jedis.hgetAll(response.getMemberByString()))
70+
.filter(Objects::nonNull)
71+
.map(Site::new).collect(Collectors.toSet());
72+
73+
// START Challenge #5
74+
Pipeline pipeline = jedis.pipelined();
75+
Map<Long, Response<Double>> scores = new HashMap<>(sites.size());
76+
for (Site site : sites) {
77+
Response<Double> score = pipeline.zscore(RedisSchema.getCapacityRankingKey(),
78+
String.valueOf(site.getId()));
79+
scores.put(site.getId(), score);
80+
}
81+
pipeline.sync();
82+
// END Challenge #5
83+
84+
for (Site site : sites) {
85+
if (scores.get(site.getId()).get() >= capacityThreshold) {
86+
results.add(site);
87+
}
88+
}
89+
}
90+
91+
return results;
92+
}
93+
5594
// Challenge #5
56-
private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) {
57-
return Collections.emptySet();
58-
}
95+
// private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) {
96+
// return Collections.emptySet();
97+
// }
5998
// Comment out the above, and uncomment what's below
6099
// private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) {
61100
// Set<Site> results = new HashSet<>();

0 commit comments

Comments
 (0)