|
5 | 5 | import com.redislabs.university.RU102J.api.Site; |
6 | 6 |
|
7 | 7 | import java.util.ArrayList; |
8 | | -import java.util.Collections; |
| 8 | +import java.util.HashMap; |
9 | 9 | import java.util.HashSet; |
10 | 10 | import java.util.List; |
11 | 11 | import java.util.Map; |
@@ -74,41 +74,39 @@ public Set<Site> findByGeo( GeoQuery query ) { |
74 | 74 |
|
75 | 75 | // Challenge #5 |
76 | 76 | private Set<Site> findSitesByGeoWithCapacity( GeoQuery query ) { |
77 | | - return Collections.emptySet(); |
| 77 | + //return Collections.emptySet(); |
| 78 | + //} |
| 79 | + // Comment out the above, and uncomment what 's below private Set<Site> findSitesByGeoWithCapacity (GeoQuery query){ |
| 80 | + Set<Site> results = new HashSet<>(); |
| 81 | + Coordinate coord = query.getCoordinate(); |
| 82 | + Double radius = query.getRadius(); |
| 83 | + GeoUnit radiusUnit = query.getRadiusUnit(); |
| 84 | + |
| 85 | + try ( Jedis jedis = jedisPool.getResource() ) { |
| 86 | + // START Challenge #5 |
| 87 | + List<GeoRadiusResponse> radiusResponses = jedis.georadius( RedisSchema.getSiteGeoKey(), coord.getLng(), coord.getLat(), radius, radiusUnit ); |
| 88 | + // END Challenge #5 |
| 89 | + |
| 90 | + Set<Site> sites = radiusResponses.stream().map( response -> jedis.hgetAll( response.getMemberByString() ) ).filter( Objects::nonNull ).map( Site::new ).collect( Collectors.toSet() ); |
| 91 | + |
| 92 | + // START Challenge #5 |
| 93 | + Pipeline pipeline = jedis.pipelined(); |
| 94 | + Map<Long, Response<Double>> scores = new HashMap<>( sites.size() ); |
| 95 | + sites.forEach( site -> { |
| 96 | + scores.put( site.getId(), pipeline.zscore( RedisSchema.getCapacityRankingKey(), String.valueOf( site.getId() ) ) ); |
| 97 | + } ); |
| 98 | + pipeline.sync(); |
| 99 | + // END Challenge #5 |
| 100 | + |
| 101 | + for ( Site site : sites ) { |
| 102 | + if ( scores.get( site.getId() ).get() >= capacityThreshold ) { |
| 103 | + results.add( site ); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return results; |
78 | 109 | } |
79 | | - // Comment out the above, and uncomment what's below |
80 | | - // private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) { |
81 | | - // Set<Site> results = new HashSet<>(); |
82 | | - // Coordinate coord = query.getCoordinate(); |
83 | | - // Double radius = query.getRadius(); |
84 | | - // GeoUnit radiusUnit = query.getRadiusUnit(); |
85 | | - // |
86 | | - // try (Jedis jedis = jedisPool.getResource()) { |
87 | | - // // START Challenge #5 |
88 | | - // // TODO: Challenge #5: Get the sites matching the geo query, store them |
89 | | - // // in List<GeoRadiusResponse> radiusResponses; |
90 | | - // // END Challenge #5 |
91 | | - // |
92 | | - // Set<Site> sites = radiusResponses.stream() |
93 | | - // .map(response -> jedis.hgetAll(response.getMemberByString())) |
94 | | - // .filter(Objects::nonNull) |
95 | | - // .map(Site::new).collect(Collectors.toSet()); |
96 | | - // |
97 | | - // // START Challenge #5 |
98 | | - // Pipeline pipeline = jedis.pipelined(); |
99 | | - // Map<Long, Response<Double>> scores = new HashMap<>(sites.size()); |
100 | | - // // TODO: Challenge #5: Add the code that populates the scores HashMap... |
101 | | - // // END Challenge #5 |
102 | | - // |
103 | | - // for (Site site : sites) { |
104 | | - // if (scores.get(site.getId()).get() >= capacityThreshold) { |
105 | | - // results.add(site); |
106 | | - // } |
107 | | - // } |
108 | | - // } |
109 | | - // |
110 | | - // return results; |
111 | | - // } |
112 | 110 |
|
113 | 111 | private Set<Site> findSitesByGeo( GeoQuery query ) { |
114 | 112 | Coordinate coord = query.getCoordinate(); |
|
0 commit comments