Skip to content

Commit 729728e

Browse files
Programming Challenge redislabs-training#5
1 parent 2422439 commit 729728e

2 files changed

Lines changed: 209 additions & 211 deletions

File tree

src/main/java/com/redislabs/university/RU102J/dao/SiteGeoDaoRedisImpl.java

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.redislabs.university.RU102J.api.Site;
66

77
import java.util.ArrayList;
8-
import java.util.Collections;
8+
import java.util.HashMap;
99
import java.util.HashSet;
1010
import java.util.List;
1111
import java.util.Map;
@@ -74,41 +74,39 @@ public Set<Site> findByGeo( GeoQuery query ) {
7474

7575
// Challenge #5
7676
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;
78109
}
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-
// }
112110

113111
private Set<Site> findSitesByGeo( GeoQuery query ) {
114112
Coordinate coord = query.getCoordinate();

0 commit comments

Comments
 (0)