Skip to content

Commit c2780ea

Browse files
author
Simon Prickett
committed
Adds Challenge redislabs-training#5 solution and enables tests.
1 parent 6e39cc0 commit c2780ea

File tree

2 files changed

+44
-36
lines changed

2 files changed

+44
-36
lines changed

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

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -53,42 +53,51 @@ public Set<Site> findByGeo(GeoQuery query) {
5353
}
5454

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

93102
private Set<Site> findSitesByGeo(GeoQuery query) {
94103
Coordinate coord = query.getCoordinate();

src/test/java/com/redislabs/university/RU102J/dao/SiteGeoDaoRedisImplTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ public void findByGeo() {
125125
}
126126

127127
// Challenge #5
128-
@Ignore
129128
@Test
130129
public void findByGeoWithExcessCapacity() {
131130
SiteGeoDao siteDao = new SiteGeoDaoRedisImpl(jedisPool);

0 commit comments

Comments
 (0)