@@ -33,12 +33,19 @@ public Set<Site> findAll() {
3333 try (Jedis jedis = jedisPool .getResource ()) {
3434 Set <String > keys = jedis .zrange (RedisSchema .getSiteGeoKey (), 0 , -1 );
3535 Set <Site > sites = new HashSet <>(keys .size ());
36- for (String key : keys ) {
37- Map <String , String > site = jedis .hgetAll (key );
38- if (!site .isEmpty ()) {
39- sites .add (new Site (site ));
40- }
41- }
36+ final Pipeline pipeline = jedis .pipelined ();
37+ // for (String key : keys) {
38+ // Map<String, String> site = jedis.hgetAll(key);
39+ // if (!site.isEmpty()) {
40+ // sites.add(new Site(site));
41+ // }
42+ // }
43+ keys .forEach (pipeline ::hgetAll );
44+ sites = pipeline .syncAndReturnAll ().stream ()
45+ .filter (siteObject -> siteObject instanceof Map )
46+ .map (siteObject -> (Map <String , String >) siteObject )
47+ .filter (site -> !site .isEmpty ())
48+ .map (Site ::new ).collect (Collectors .toSet ());
4249 return sites ;
4350 }
4451 }
@@ -53,42 +60,51 @@ public Set<Site> findByGeo(GeoQuery query) {
5360 }
5461
5562 // Challenge #5
56- private Set <Site > findSitesByGeoWithCapacity (GeoQuery query ) {
57- return Collections .emptySet ();
58- }
63+ // private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) {
64+ // return Collections.emptySet();
65+ // }
5966 // 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- // }
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+ List <GeoRadiusResponse > radiusResponses =
78+ jedis .georadius (RedisSchema .getSiteGeoKey (), coord .getLng (),
79+ coord .getLat (), radius , radiusUnit );
80+ // END Challenge #5
81+
82+ Set <Site > sites = radiusResponses .stream ()
83+ .map (response -> jedis .hgetAll (response .getMemberByString ()))
84+ .filter (Objects ::nonNull )
85+ .map (Site ::new ).collect (Collectors .toSet ());
86+
87+ // START Challenge #5
88+ Pipeline pipeline = jedis .pipelined ();
89+ Map <Long , Response <Double >> scores = new HashMap <>(sites .size ());
90+ // TODO: Challenge #5: Add the code that populates the scores HashMap...
91+ String key = RedisSchema .getCapacityRankingKey ();
92+ for (Site site : sites ) {
93+ Response <Double > rankResponse = pipeline .zscore (key , site .getId ().toString ());
94+ scores .put (site .getId (), rankResponse );
95+ }
96+ pipeline .sync ();
97+ // END Challenge #5
98+
99+ for (Site site : sites ) {
100+ if (scores .get (site .getId ()).get () >= capacityThreshold ) {
101+ results .add (site );
102+ }
103+ }
104+ }
105+
106+ return results ;
107+ }
92108
93109 private Set <Site > findSitesByGeo (GeoQuery query ) {
94110 Coordinate coord = query .getCoordinate ();
0 commit comments