Skip to content

Commit f1f6292

Browse files
committed
Finish challenge redislabs-training#5
1 parent 04e047e commit f1f6292

File tree

3 files changed

+88
-41
lines changed

3 files changed

+88
-41
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: 83 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ public Site findById(long id) {
2727
return new Site(fields);
2828
}
2929
}
30-
30+
3131
@Override
3232
public Set<Site> findAll() {
33+
// return findAllNotOptimized();
34+
return findAllOptimized();
35+
}
36+
37+
private Set<Site> findAllNotOptimized() {
3338
try (Jedis jedis = jedisPool.getResource()) {
3439
Set<String> keys = jedis.zrange(RedisSchema.getSiteGeoKey(), 0, -1);
3540
Set<Site> sites = new HashSet<>(keys.size());
@@ -42,6 +47,33 @@ public Set<Site> findAll() {
4247
return sites;
4348
}
4449
}
50+
51+
private Set<Site> findAllOptimized() {
52+
try (Jedis jedis = jedisPool.getResource()) {
53+
Set<String> keys = jedis.zrange(RedisSchema.getSiteGeoKey(), 0, -1);
54+
if (keys == null || keys.isEmpty()) {
55+
return new HashSet();
56+
}
57+
58+
Pipeline pipelined = jedis.pipelined();
59+
60+
for (String key : keys) {
61+
pipelined.hgetAll(key);
62+
}
63+
64+
List<Object> siteObjects = pipelined.syncAndReturnAll();
65+
Set<Site> sites = new HashSet<>(siteObjects.size());
66+
67+
for (Object siteObject : siteObjects) {
68+
Map<String, String> site = (Map<String, String>) siteObject;
69+
if (!site.isEmpty()) {
70+
sites.add(new Site(site));
71+
}
72+
}
73+
74+
return sites;
75+
}
76+
}
4577

4678
@Override
4779
public Set<Site> findByGeo(GeoQuery query) {
@@ -53,42 +85,57 @@ public Set<Site> findByGeo(GeoQuery query) {
5385
}
5486

5587
// Challenge #5
56-
private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) {
57-
return Collections.emptySet();
58-
}
88+
// private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) {
89+
// return Collections.emptySet();
90+
// }
5991
// 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-
// }
92+
private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) {
93+
Set<Site> results = new HashSet<>();
94+
Coordinate coord = query.getCoordinate();
95+
Double radius = query.getRadius();
96+
GeoUnit radiusUnit = query.getRadiusUnit();
97+
98+
try (Jedis jedis = jedisPool.getResource()) {
99+
// START Challenge #5
100+
// TODO: Challenge #5: Get the sites matching the geo query, store them
101+
// in List<GeoRadiusResponse> radiusResponses;
102+
// END Challenge #5
103+
List<GeoRadiusResponse> radiusResponses = jedis.georadius(
104+
RedisSchema.getSiteGeoKey(),
105+
coord.getLng(),
106+
coord.getLat(),
107+
radius,
108+
radiusUnit
109+
);
110+
111+
Set<Site> sites = radiusResponses.stream()
112+
.map(response -> jedis.hgetAll(response.getMemberByString()))
113+
.filter(Objects::nonNull)
114+
.map(Site::new).collect(Collectors.toSet());
115+
116+
// START Challenge #5
117+
Pipeline pipeline = jedis.pipelined();
118+
Map<Long, Response<Double>> scores = new HashMap<>(sites.size());
119+
// TODO: Challenge #5: Add the code that populates the scores HashMap...
120+
// END Challenge #5
121+
for (Site site : sites) {
122+
Response<Double> response = pipeline.zscore(
123+
RedisSchema.getCapacityRankingKey(),
124+
String.valueOf(site.getId())
125+
);
126+
scores.put(site.getId(), response);
127+
}
128+
pipeline.sync();
129+
130+
for (Site site : sites) {
131+
if (scores.get(site.getId()).get() >= capacityThreshold) {
132+
results.add(site);
133+
}
134+
}
135+
}
136+
137+
return results;
138+
}
92139

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void findByGeo() {
126126
}
127127

128128
// Challenge #5
129-
@Ignore
129+
// @Ignore
130130
@Test
131131
public void findByGeoWithExcessCapacity() {
132132
SiteGeoDao siteDao = new SiteGeoDaoRedisImpl(jedisPool);

0 commit comments

Comments
 (0)