|
1 | 1 | package com.redislabs.university.RU102J.dao; |
2 | 2 |
|
3 | 3 | import com.redislabs.university.RU102J.api.Site; |
| 4 | + |
| 5 | +import java.util.HashSet; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Set; |
| 8 | + |
4 | 9 | import redis.clients.jedis.Jedis; |
5 | 10 | import redis.clients.jedis.JedisPool; |
6 | 11 |
|
7 | | -import java.util.*; |
8 | | - |
9 | 12 | public class SiteDaoRedisImpl implements SiteDao { |
10 | | - private final JedisPool jedisPool; |
11 | | - |
12 | | - public SiteDaoRedisImpl(JedisPool jedisPool) { |
13 | | - this.jedisPool = jedisPool; |
14 | | - } |
15 | | - |
16 | | - // When we insert a site, we set all of its values into a single hash. |
17 | | - // We then store the site's id in a set for easy access. |
18 | | - @Override |
19 | | - public void insert(Site site) { |
20 | | - try (Jedis jedis = jedisPool.getResource()) { |
21 | | - String hashKey = RedisSchema.getSiteHashKey(site.getId()); |
22 | | - String siteIdKey = RedisSchema.getSiteIDsKey(); |
23 | | - jedis.hmset(hashKey, site.toMap()); |
24 | | - jedis.sadd(siteIdKey, hashKey); |
25 | | - } |
26 | | - } |
27 | | - |
28 | | - @Override |
29 | | - public Site findById(long id) { |
30 | | - try(Jedis jedis = jedisPool.getResource()) { |
31 | | - String key = RedisSchema.getSiteHashKey(id); |
32 | | - Map<String, String> fields = jedis.hgetAll(key); |
33 | | - if (fields == null || fields.isEmpty()) { |
34 | | - return null; |
35 | | - } else { |
36 | | - return new Site(fields); |
37 | | - } |
38 | | - } |
39 | | - } |
40 | | - |
41 | | - // Challenge #1 |
42 | | - @Override |
43 | | - public Set<Site> findAll() { |
44 | | - Set<Site> sites = new HashSet<>(); |
45 | | - try ( Jedis jedis = jedisPool.getResource() ) { |
46 | | - for ( String siteHashKey : jedis.smembers( RedisSchema.getSiteIDsKey() ) ) { |
47 | | - sites.add( new Site( jedis.hgetAll( siteHashKey ) ) ); |
48 | | - } |
49 | | - } |
50 | | - |
51 | | - return sites; |
52 | | - } |
| 13 | + |
| 14 | + private final JedisPool jedisPool; |
| 15 | + |
| 16 | + public SiteDaoRedisImpl( JedisPool jedisPool ) { |
| 17 | + this.jedisPool = jedisPool; |
| 18 | + } |
| 19 | + |
| 20 | + // When we insert a site, we set all of its values into a single hash. |
| 21 | + // We then store the site's id in a set for easy access. |
| 22 | + @Override |
| 23 | + public void insert( Site site ) { |
| 24 | + try ( Jedis jedis = jedisPool.getResource() ) { |
| 25 | + String hashKey = RedisSchema.getSiteHashKey( site.getId() ); |
| 26 | + String siteIdKey = RedisSchema.getSiteIDsKey(); |
| 27 | + jedis.hmset( hashKey, site.toMap() ); |
| 28 | + jedis.sadd( siteIdKey, hashKey ); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public Site findById( long id ) { |
| 34 | + try ( Jedis jedis = jedisPool.getResource() ) { |
| 35 | + String key = RedisSchema.getSiteHashKey( id ); |
| 36 | + Map<String, String> fields = jedis.hgetAll( key ); |
| 37 | + if ( fields == null || fields.isEmpty() ) { |
| 38 | + return null; |
| 39 | + } else { |
| 40 | + return new Site( fields ); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // Challenge #1 |
| 46 | + @Override |
| 47 | + public Set<Site> findAll() { |
| 48 | + Set<Site> sites = new HashSet<>(); |
| 49 | + try ( Jedis jedis = jedisPool.getResource() ) { |
| 50 | + for ( String siteHashKey : jedis.smembers( RedisSchema.getSiteIDsKey() ) ) { |
| 51 | + Map<String, String> fields = jedis.hgetAll( siteHashKey ); |
| 52 | + if ( fields != null && !fields.isEmpty() ) { |
| 53 | + sites.add( new Site( fields ) ); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return sites; |
| 59 | + } |
53 | 60 | } |
0 commit comments