Skip to content

Commit

Permalink
Programming Challenge redislabs-training#5
Browse files Browse the repository at this point in the history
  • Loading branch information
ThiernoAmirouDiallo committed Nov 14, 2022
1 parent 2422439 commit 729728e
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.redislabs.university.RU102J.api.Site;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -74,41 +74,39 @@ public Set<Site> findByGeo( GeoQuery query ) {

// Challenge #5
private Set<Site> findSitesByGeoWithCapacity( GeoQuery query ) {
return Collections.emptySet();
//return Collections.emptySet();
//}
// Comment out the above, and uncomment what 's below private Set<Site> findSitesByGeoWithCapacity (GeoQuery query){
Set<Site> results = new HashSet<>();
Coordinate coord = query.getCoordinate();
Double radius = query.getRadius();
GeoUnit radiusUnit = query.getRadiusUnit();

try ( Jedis jedis = jedisPool.getResource() ) {
// START Challenge #5
List<GeoRadiusResponse> radiusResponses = jedis.georadius( RedisSchema.getSiteGeoKey(), coord.getLng(), coord.getLat(), radius, radiusUnit );
// END Challenge #5

Set<Site> sites = radiusResponses.stream().map( response -> jedis.hgetAll( response.getMemberByString() ) ).filter( Objects::nonNull ).map( Site::new ).collect( Collectors.toSet() );

// START Challenge #5
Pipeline pipeline = jedis.pipelined();
Map<Long, Response<Double>> scores = new HashMap<>( sites.size() );
sites.forEach( site -> {
scores.put( site.getId(), pipeline.zscore( RedisSchema.getCapacityRankingKey(), String.valueOf( site.getId() ) ) );
} );
pipeline.sync();
// END Challenge #5

for ( Site site : sites ) {
if ( scores.get( site.getId() ).get() >= capacityThreshold ) {
results.add( site );
}
}
}

return results;
}
// Comment out the above, and uncomment what's below
// private Set<Site> findSitesByGeoWithCapacity(GeoQuery query) {
// Set<Site> results = new HashSet<>();
// Coordinate coord = query.getCoordinate();
// Double radius = query.getRadius();
// GeoUnit radiusUnit = query.getRadiusUnit();
//
// try (Jedis jedis = jedisPool.getResource()) {
// // START Challenge #5
// // TODO: Challenge #5: Get the sites matching the geo query, store them
// // in List<GeoRadiusResponse> radiusResponses;
// // END Challenge #5
//
// Set<Site> sites = radiusResponses.stream()
// .map(response -> jedis.hgetAll(response.getMemberByString()))
// .filter(Objects::nonNull)
// .map(Site::new).collect(Collectors.toSet());
//
// // START Challenge #5
// Pipeline pipeline = jedis.pipelined();
// Map<Long, Response<Double>> scores = new HashMap<>(sites.size());
// // TODO: Challenge #5: Add the code that populates the scores HashMap...
// // END Challenge #5
//
// for (Site site : sites) {
// if (scores.get(site.getId()).get() >= capacityThreshold) {
// results.add(site);
// }
// }
// }
//
// return results;
// }

private Set<Site> findSitesByGeo( GeoQuery query ) {
Coordinate coord = query.getCoordinate();
Expand Down
Loading

0 comments on commit 729728e

Please sign in to comment.