Skip to content

Commit d9e1491

Browse files
committed
Implement `findSitesByGeoWithCapacity()` in `SiteGeoDaoRedisImpl.java`.
1 parent 28c2a1a commit d9e1491

File tree

3 files changed

+61
-46
lines changed

3 files changed

+61
-46
lines changed

src/main/java/com/redislabs/university/RU102J/RediSolarApplication.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ public void run(final RediSolarConfiguration configuration,
4747
}
4848

4949
// To use the geospatial features, replace the following lines with:
50-
// SiteGeoResource siteResource =
51-
// new SiteGeoResource(new SiteGeoDaoRedisImpl(jedisPool));
52-
SiteResource siteResource =
53-
new SiteResource(new SiteDaoRedisImpl(jedisPool));
50+
SiteGeoResource siteResource =
51+
new SiteGeoResource(new SiteGeoDaoRedisImpl(jedisPool));
52+
// SiteResource siteResource =
53+
// new SiteResource(new SiteDaoRedisImpl(jedisPool));
5454
environment.jersey().register(siteResource);
5555

5656
// For RedisTimeSeries: replace the next lines with

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

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,19 @@ public Set<Site> findAll() {
3333
try (Jedis jedis = jedisPool.getResource()) {
3434
Set<String> keys = jedis.zrange(RedisSchema.getSiteGeoKey(), 0, -1);
3535
Set<Site> sites = new HashSet<>(keys.size());
36-
for (String key : keys) {
37-
Map<String, String> site = jedis.hgetAll(key);
38-
if (!site.isEmpty()) {
39-
sites.add(new Site(site));
40-
}
41-
}
36+
final Pipeline pipeline = jedis.pipelined();
37+
// for (String key : keys) {
38+
// Map<String, String> site = jedis.hgetAll(key);
39+
// if (!site.isEmpty()) {
40+
// sites.add(new Site(site));
41+
// }
42+
// }
43+
keys.forEach(pipeline::hgetAll);
44+
sites = pipeline.syncAndReturnAll().stream()
45+
.filter(siteObject -> siteObject instanceof Map)
46+
.map(siteObject -> (Map<String, String>) siteObject)
47+
.filter(site -> !site.isEmpty())
48+
.map(Site::new).collect(Collectors.toSet());
4249
return sites;
4350
}
4451
}
@@ -53,42 +60,51 @@ public Set<Site> findByGeo(GeoQuery query) {
5360
}
5461

5562
// Challenge #5
56-
private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) {
57-
return Collections.emptySet();
58-
}
63+
// private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) {
64+
// return Collections.emptySet();
65+
// }
5966
// 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-
// }
67+
private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) {
68+
Set<Site> results = new HashSet<>();
69+
Coordinate coord = query.getCoordinate();
70+
Double radius = query.getRadius();
71+
GeoUnit radiusUnit = query.getRadiusUnit();
72+
73+
try (Jedis jedis = jedisPool.getResource()) {
74+
// START Challenge #5
75+
// TODO: Challenge #5: Get the sites matching the geo query, store them
76+
// in List<GeoRadiusResponse> radiusResponses;
77+
List<GeoRadiusResponse> radiusResponses =
78+
jedis.georadius(RedisSchema.getSiteGeoKey(), coord.getLng(),
79+
coord.getLat(), radius, radiusUnit);
80+
// END Challenge #5
81+
82+
Set<Site> sites = radiusResponses.stream()
83+
.map(response -> jedis.hgetAll(response.getMemberByString()))
84+
.filter(Objects::nonNull)
85+
.map(Site::new).collect(Collectors.toSet());
86+
87+
// START Challenge #5
88+
Pipeline pipeline = jedis.pipelined();
89+
Map<Long, Response<Double>> scores = new HashMap<>(sites.size());
90+
// TODO: Challenge #5: Add the code that populates the scores HashMap...
91+
String key = RedisSchema.getCapacityRankingKey();
92+
for (Site site : sites) {
93+
Response<Double> rankResponse = pipeline.zscore(key, site.getId().toString());
94+
scores.put(site.getId(), rankResponse);
95+
}
96+
pipeline.sync();
97+
// END Challenge #5
98+
99+
for (Site site : sites) {
100+
if (scores.get(site.getId()).get() >= capacityThreshold) {
101+
results.add(site);
102+
}
103+
}
104+
}
105+
106+
return results;
107+
}
92108

93109
private Set<Site> findSitesByGeo(GeoQuery query) {
94110
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)