Skip to content

Commit 4f28f8a

Browse files
Programming Challenge redislabs-training#4
1 parent 00d1b01 commit 4f28f8a

File tree

2 files changed

+54
-53
lines changed

2 files changed

+54
-53
lines changed

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

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,65 @@
33
import com.redislabs.university.RU102J.api.CapacityReport;
44
import com.redislabs.university.RU102J.api.MeterReading;
55
import com.redislabs.university.RU102J.api.SiteCapacityTuple;
6-
import redis.clients.jedis.*;
76

87
import java.util.List;
98
import java.util.Set;
109
import java.util.stream.Collectors;
1110

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+
1217
public class CapacityDaoRedisImpl implements CapacityDao {
1318

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+
}
6567
}

src/test/java/com/redislabs/university/RU102J/dao/CapacityDaoRedisImplTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public void getReport() {
7171
}
7272

7373
// Challenge #4
74-
@Ignore
7574
@Test
7675
public void getRank() {
7776
CapacityDao dao = new CapacityDaoRedisImpl(jedisPool);

0 commit comments

Comments
 (0)