@@ -53,42 +53,51 @@ public Set<Site> findByGeo(GeoQuery query) {
53
53
}
54
54
55
55
// Challenge #5
56
- private Set <Site > findSitesByGeoWithCapacity (GeoQuery query ) {
57
- return Collections .emptySet ();
58
- }
56
+ // private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) {
57
+ // return Collections.emptySet();
58
+ // }
59
59
// 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
- // }
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
+ List <GeoRadiusResponse > radiusResponses =
71
+ jedis .georadius (RedisSchema .getSiteGeoKey (), coord .getLng (),
72
+ coord .getLat (), radius , radiusUnit );
73
+ // END Challenge #5
74
+
75
+ Set <Site > sites = radiusResponses .stream ()
76
+ .map (response -> jedis .hgetAll (response .getMemberByString ()))
77
+ .filter (Objects ::nonNull )
78
+ .map (Site ::new ).collect (Collectors .toSet ());
79
+
80
+ // START Challenge #5
81
+ Pipeline pipeline = jedis .pipelined ();
82
+ Map <Long , Response <Double >> scores = new HashMap <>(sites .size ());
83
+ // TODO: Challenge #5: Add the code that populates the scores HashMap...
84
+ for (Site site : sites ) {
85
+ Response <Double > score = pipeline .zscore (RedisSchema .getCapacityRankingKey (),
86
+ String .valueOf (site .getId ()));
87
+ scores .put (site .getId (), score );
88
+ }
89
+ pipeline .sync ();
90
+ // END Challenge #5
91
+
92
+ for (Site site : sites ) {
93
+ if (scores .get (site .getId ()).get () >= capacityThreshold ) {
94
+ results .add (site );
95
+ }
96
+ }
97
+ }
98
+
99
+ return results ;
100
+ }
92
101
93
102
private Set <Site > findSitesByGeo (GeoQuery query ) {
94
103
Coordinate coord = query .getCoordinate ();
0 commit comments