Skip to content

Commit 37ea6b3

Browse files
author
Qilu Huang
committed
challenge redislabs-training#5 enable excess capacity filter feature when geo query
1 parent 61a9f00 commit 37ea6b3

2 files changed

Lines changed: 41 additions & 36 deletions

File tree

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

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -60,42 +60,48 @@ public Set<Site> findByGeo(GeoQuery query) {
6060
}
6161

6262
// Challenge #5
63-
private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) {
64-
return Collections.emptySet();
65-
}
63+
// private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) {
64+
// return Collections.emptySet();
65+
// }
6666
// Comment out the above, and uncomment what's below
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-
// // END Challenge #5
78-
//
79-
// Set<Site> sites = radiusResponses.stream()
80-
// .map(response -> jedis.hgetAll(response.getMemberByString()))
81-
// .filter(Objects::nonNull)
82-
// .map(Site::new).collect(Collectors.toSet());
83-
//
84-
// // START Challenge #5
85-
// Pipeline pipeline = jedis.pipelined();
86-
// Map<Long, Response<Double>> scores = new HashMap<>(sites.size());
87-
// // TODO: Challenge #5: Add the code that populates the scores HashMap...
88-
// // END Challenge #5
89-
//
90-
// for (Site site : sites) {
91-
// if (scores.get(site.getId()).get() >= capacityThreshold) {
92-
// results.add(site);
93-
// }
94-
// }
95-
// }
96-
//
97-
// return results;
98-
// }
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+
// Get the sites matching the geo query, store them in a list
75+
List<GeoRadiusResponse> radiusResponses =
76+
jedis.georadius(RedisSchema.getSiteGeoKey(), coord.getLng(),
77+
coord.getLat(), radius, radiusUnit);
78+
79+
Set<Site> sites = radiusResponses.stream()
80+
.map(response -> jedis.hgetAll(response.getMemberByString()))
81+
.filter(Objects::nonNull)
82+
.map(Site::new).collect(Collectors.toSet());
83+
84+
// Get the score of a site using Redis pipeline
85+
Pipeline pipeline = jedis.pipelined();
86+
Map<Long, Response<Double>> scores = sites.stream()
87+
.collect(Collectors.toMap(
88+
Site::getId,
89+
site -> pipeline.zscore(
90+
RedisSchema.getCapacityRankingKey(),
91+
String.valueOf(site.getId())
92+
)
93+
));
94+
pipeline.sync();
95+
96+
for (Site site : sites) {
97+
if (scores.get(site.getId()).get() >= capacityThreshold) {
98+
results.add(site);
99+
}
100+
}
101+
}
102+
103+
return results;
104+
}
99105

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