@@ -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 ();
0 commit comments