|
3 | 3 | import com.redislabs.university.RU102J.api.CapacityReport;
|
4 | 4 | import com.redislabs.university.RU102J.api.MeterReading;
|
5 | 5 | import com.redislabs.university.RU102J.api.SiteCapacityTuple;
|
6 |
| -import redis.clients.jedis.*; |
7 | 6 |
|
8 | 7 | import java.util.List;
|
9 | 8 | import java.util.Set;
|
10 | 9 | import java.util.stream.Collectors;
|
11 | 10 |
|
| 11 | +import redis.clients.jedis.Jedis; |
| 12 | +import redis.clients.jedis.JedisPool; |
| 13 | +import redis.clients.jedis.Pipeline; |
| 14 | +import redis.clients.jedis.Response; |
| 15 | +import redis.clients.jedis.Tuple; |
| 16 | + |
12 | 17 | public class CapacityDaoRedisImpl implements CapacityDao {
|
13 | 18 |
|
14 |
| - private final JedisPool jedisPool; |
15 |
| - |
16 |
| - public CapacityDaoRedisImpl(JedisPool jedisPool) { |
17 |
| - this.jedisPool = jedisPool; |
18 |
| - } |
19 |
| - |
20 |
| - @Override |
21 |
| - public void update(MeterReading reading) { |
22 |
| - String capacityRankingKey = RedisSchema.getCapacityRankingKey(); |
23 |
| - Long siteId = reading.getSiteId(); |
24 |
| - |
25 |
| - double currentCapacity = reading.getWhGenerated() - reading.getWhUsed(); |
26 |
| - |
27 |
| - try (Jedis jedis = jedisPool.getResource()) { |
28 |
| - jedis.zadd(capacityRankingKey, currentCapacity, String.valueOf(siteId)); |
29 |
| - } |
30 |
| - } |
31 |
| - |
32 |
| - @Override |
33 |
| - public CapacityReport getReport(Integer limit) { |
34 |
| - CapacityReport report; |
35 |
| - String key = RedisSchema.getCapacityRankingKey(); |
36 |
| - |
37 |
| - try (Jedis jedis = jedisPool.getResource()) { |
38 |
| - Pipeline p = jedis.pipelined(); |
39 |
| - Response<Set<Tuple>> lowCapacity = p.zrangeWithScores(key, 0, limit-1); |
40 |
| - Response<Set<Tuple>> highCapacity = p.zrevrangeWithScores(key, 0, |
41 |
| - limit-1); |
42 |
| - p.sync(); |
43 |
| - |
44 |
| - List<SiteCapacityTuple> lowCapacityList = lowCapacity.get().stream() |
45 |
| - .map(SiteCapacityTuple::new) |
46 |
| - .collect(Collectors.toList()); |
47 |
| - |
48 |
| - List<SiteCapacityTuple> highCapacityList = highCapacity.get().stream() |
49 |
| - .map(SiteCapacityTuple::new) |
50 |
| - .collect(Collectors.toList()); |
51 |
| - |
52 |
| - report = new CapacityReport(highCapacityList, lowCapacityList); |
53 |
| - } |
54 |
| - |
55 |
| - return report; |
56 |
| - } |
57 |
| - |
58 |
| - // Challenge #4 |
59 |
| - @Override |
60 |
| - public Long getRank(Long siteId) { |
61 |
| - // START Challenge #4 |
62 |
| - return -2L; |
63 |
| - // END Challenge #4 |
64 |
| - } |
| 19 | + private final JedisPool jedisPool; |
| 20 | + |
| 21 | + public CapacityDaoRedisImpl( JedisPool jedisPool ) { |
| 22 | + this.jedisPool = jedisPool; |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public void update( MeterReading reading ) { |
| 27 | + String capacityRankingKey = RedisSchema.getCapacityRankingKey(); |
| 28 | + Long siteId = reading.getSiteId(); |
| 29 | + |
| 30 | + double currentCapacity = reading.getWhGenerated() - reading.getWhUsed(); |
| 31 | + |
| 32 | + try ( Jedis jedis = jedisPool.getResource() ) { |
| 33 | + jedis.zadd( capacityRankingKey, currentCapacity, String.valueOf( siteId ) ); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public CapacityReport getReport( Integer limit ) { |
| 39 | + CapacityReport report; |
| 40 | + String key = RedisSchema.getCapacityRankingKey(); |
| 41 | + |
| 42 | + try ( Jedis jedis = jedisPool.getResource() ) { |
| 43 | + Pipeline p = jedis.pipelined(); |
| 44 | + Response<Set<Tuple>> lowCapacity = p.zrangeWithScores( key, 0, limit - 1 ); |
| 45 | + Response<Set<Tuple>> highCapacity = p.zrevrangeWithScores( key, 0, limit - 1 ); |
| 46 | + p.sync(); |
| 47 | + |
| 48 | + List<SiteCapacityTuple> lowCapacityList = lowCapacity.get().stream().map( SiteCapacityTuple::new ).collect( Collectors.toList() ); |
| 49 | + |
| 50 | + List<SiteCapacityTuple> highCapacityList = highCapacity.get().stream().map( SiteCapacityTuple::new ).collect( Collectors.toList() ); |
| 51 | + |
| 52 | + report = new CapacityReport( highCapacityList, lowCapacityList ); |
| 53 | + } |
| 54 | + |
| 55 | + return report; |
| 56 | + } |
| 57 | + |
| 58 | + // Challenge #4 |
| 59 | + @Override |
| 60 | + public Long getRank( Long siteId ) { |
| 61 | + // START Challenge #4 |
| 62 | + try ( Jedis jedis = jedisPool.getResource() ) { |
| 63 | + return jedis.zrevrank( RedisSchema.getCapacityRankingKey(), String.valueOf( siteId ) ); |
| 64 | + } |
| 65 | + // END Challenge #4 |
| 66 | + } |
65 | 67 | }
|
0 commit comments