Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mate0021 committed Mar 18, 2022
1 parent 9e9b61b commit 667ced2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.util.*;
import java.util.stream.Collectors;

import static com.redislabs.university.RU102J.dao.RedisSchema.getSiteGeoKey;

public class SiteGeoDaoRedisImpl implements SiteGeoDao {
private JedisPool jedisPool;
final static private Double capacityThreshold = 0.2;
Expand All @@ -31,7 +33,7 @@ public Site findById(long id) {
@Override
public Set<Site> findAll() {
try (Jedis jedis = jedisPool.getResource()) {
Set<String> keys = jedis.zrange(RedisSchema.getSiteGeoKey(), 0, -1);
Set<String> keys = jedis.zrange(getSiteGeoKey(), 0, -1);
Set<Site> sites = new HashSet<>(keys.size());

Pipeline p = jedis.pipelined();
Expand All @@ -47,7 +49,7 @@ public Set<Site> findAll() {
sites.add(new Site(response.get()));
}
}

return sites;
}
}
Expand All @@ -62,42 +64,45 @@ public Set<Site> findByGeo(GeoQuery query) {
}

// Challenge #5
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;
List<GeoRadiusResponse> radiusResponses =
jedis.georadius(getSiteGeoKey(), coord.getLng(), coord.getLat(), radius, radiusUnit);
// 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...
for (Site s : sites) {
Response<Double> score = pipeline.zscore(RedisSchema.getCapacityRankingKey(), String.valueOf(s.getId()));
scores.put(s.getId(), score);
}
pipeline.sync();
// END Challenge #5

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 All @@ -106,7 +111,7 @@ private Set<Site> findSitesByGeo(GeoQuery query) {

try (Jedis jedis = jedisPool.getResource()) {
List<GeoRadiusResponse> radiusResponses =
jedis.georadius(RedisSchema.getSiteGeoKey(), coord.getLng(),
jedis.georadius(getSiteGeoKey(), coord.getLng(),
coord.getLat(), radius, radiusUnit);

return radiusResponses.stream()
Expand All @@ -128,7 +133,7 @@ public void insert(Site site) {
}
Double longitude = site.getCoordinate().getGeoCoordinate().getLongitude();
Double latitude = site.getCoordinate().getGeoCoordinate().getLatitude();
jedis.geoadd(RedisSchema.getSiteGeoKey(), longitude, latitude,
jedis.geoadd(getSiteGeoKey(), longitude, latitude,
key);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void findByGeo() {
}

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

0 comments on commit 667ced2

Please sign in to comment.