Skip to content

Commit

Permalink
Finish challenge redislabs-training#5
Browse files Browse the repository at this point in the history
  • Loading branch information
quyluongthanh committed Feb 7, 2023
1 parent 04e047e commit f1f6292
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public void run(final RediSolarConfiguration configuration,
}

// To use the geospatial features, replace the following lines with:
// SiteGeoResource siteResource =
// new SiteGeoResource(new SiteGeoDaoRedisImpl(jedisPool));
SiteResource siteResource =
new SiteResource(new SiteDaoRedisImpl(jedisPool));
SiteGeoResource siteResource =
new SiteGeoResource(new SiteGeoDaoRedisImpl(jedisPool));
// SiteResource siteResource =
// new SiteResource(new SiteDaoRedisImpl(jedisPool));
environment.jersey().register(siteResource);

// For RedisTimeSeries: replace the next lines with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ public Site findById(long id) {
return new Site(fields);
}
}

@Override
public Set<Site> findAll() {
// return findAllNotOptimized();
return findAllOptimized();
}

private Set<Site> findAllNotOptimized() {
try (Jedis jedis = jedisPool.getResource()) {
Set<String> keys = jedis.zrange(RedisSchema.getSiteGeoKey(), 0, -1);
Set<Site> sites = new HashSet<>(keys.size());
Expand All @@ -42,6 +47,33 @@ public Set<Site> findAll() {
return sites;
}
}

private Set<Site> findAllOptimized() {
try (Jedis jedis = jedisPool.getResource()) {
Set<String> keys = jedis.zrange(RedisSchema.getSiteGeoKey(), 0, -1);
if (keys == null || keys.isEmpty()) {
return new HashSet();
}

Pipeline pipelined = jedis.pipelined();

for (String key : keys) {
pipelined.hgetAll(key);
}

List<Object> siteObjects = pipelined.syncAndReturnAll();
Set<Site> sites = new HashSet<>(siteObjects.size());

for (Object siteObject : siteObjects) {
Map<String, String> site = (Map<String, String>) siteObject;
if (!site.isEmpty()) {
sites.add(new Site(site));
}
}

return sites;
}
}

@Override
public Set<Site> findByGeo(GeoQuery query) {
Expand All @@ -53,42 +85,57 @@ public Set<Site> findByGeo(GeoQuery query) {
}

// Challenge #5
private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) {
return Collections.emptySet();
}
// private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) {
// return Collections.emptySet();
// }
// Comment out the above, and uncomment what's below
// private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) {
// Set<Site> results = new HashSet<>();
// Coordinate coord = query.getCoordinate();
// Double radius = query.getRadius();
// GeoUnit radiusUnit = query.getRadiusUnit();
//
// try (Jedis jedis = jedisPool.getResource()) {
// // START Challenge #5
// // TODO: Challenge #5: Get the sites matching the geo query, store them
// // in List<GeoRadiusResponse> radiusResponses;
// // END Challenge #5
//
// Set<Site> sites = radiusResponses.stream()
// .map(response -> jedis.hgetAll(response.getMemberByString()))
// .filter(Objects::nonNull)
// .map(Site::new).collect(Collectors.toSet());
//
// // START Challenge #5
// Pipeline pipeline = jedis.pipelined();
// Map<Long, Response<Double>> scores = new HashMap<>(sites.size());
// // TODO: Challenge #5: Add the code that populates the scores HashMap...
// // END Challenge #5
//
// for (Site site : sites) {
// if (scores.get(site.getId()).get() >= capacityThreshold) {
// results.add(site);
// }
// }
// }
//
// return results;
// }
private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) {
Set<Site> results = new HashSet<>();
Coordinate coord = query.getCoordinate();
Double radius = query.getRadius();
GeoUnit radiusUnit = query.getRadiusUnit();

try (Jedis jedis = jedisPool.getResource()) {
// START Challenge #5
// TODO: Challenge #5: Get the sites matching the geo query, store them
// in List<GeoRadiusResponse> radiusResponses;
// END Challenge #5
List<GeoRadiusResponse> radiusResponses = jedis.georadius(
RedisSchema.getSiteGeoKey(),
coord.getLng(),
coord.getLat(),
radius,
radiusUnit
);

Set<Site> sites = radiusResponses.stream()
.map(response -> jedis.hgetAll(response.getMemberByString()))
.filter(Objects::nonNull)
.map(Site::new).collect(Collectors.toSet());

// START Challenge #5
Pipeline pipeline = jedis.pipelined();
Map<Long, Response<Double>> scores = new HashMap<>(sites.size());
// TODO: Challenge #5: Add the code that populates the scores HashMap...
// END Challenge #5
for (Site site : sites) {
Response<Double> response = pipeline.zscore(
RedisSchema.getCapacityRankingKey(),
String.valueOf(site.getId())
);
scores.put(site.getId(), response);
}
pipeline.sync();

for (Site site : sites) {
if (scores.get(site.getId()).get() >= capacityThreshold) {
results.add(site);
}
}
}

return results;
}

private Set<Site> findSitesByGeo(GeoQuery query) {
Coordinate coord = query.getCoordinate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void findByGeo() {
}

// Challenge #5
@Ignore
// @Ignore
@Test
public void findByGeoWithExcessCapacity() {
SiteGeoDao siteDao = new SiteGeoDaoRedisImpl(jedisPool);
Expand Down

0 comments on commit f1f6292

Please sign in to comment.