Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / dao / RedisAccessWrapper.java
1 package org.onap.msb.apiroute.wrapper.dao;
2
3 import org.onap.msb.apiroute.wrapper.util.JedisUtil;
4 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory;
6 import redis.clients.jedis.Jedis;
7 import redis.clients.jedis.ScanParams;
8 import redis.clients.jedis.ScanResult;
9
10 import java.util.ArrayList;
11 import java.util.HashSet;
12 import java.util.List;
13 import java.util.Set;
14
15 public class RedisAccessWrapper {
16     private static final Logger LOGGER = LoggerFactory.getLogger(RedisAccessWrapper.class);
17     //An iteration starts when the cursor is set to 0
18     private static final String REDIS_SCAN_POINTER_NEW_ITERATION = "0";
19     //An iteration terminated when the cursor returned by the server is 0
20     private static final String REDIS_SCAN_POINTER_ITERATION_END = "0";
21     private static final int REDIS_SCAN_COUNT = 50;
22
23
24     public static void save(String key,String value) throws Exception {
25         Jedis jedis = null;
26         try {
27             jedis = JedisUtil.borrowJedisInstance();
28             jedis.set(key,value);
29         } finally {
30             JedisUtil.returnJedisInstance(jedis);
31         }
32     }
33
34     public static String query(String key) throws Exception {
35         Jedis jedis = null;
36         String value = null;
37         try {
38             jedis = JedisUtil.borrowJedisInstance();
39             value = jedis.get(key);
40         }finally {
41             JedisUtil.returnJedisInstance(jedis);
42         }
43         return value;
44     }
45
46     public static long delete(String key) throws Exception {
47         Jedis jedis = null;
48         long reply = 0L;
49         try {
50             jedis = JedisUtil.borrowJedisInstance();
51             reply = jedis.del(key);
52         } finally {
53             JedisUtil.returnJedisInstance(jedis);
54         }
55         return reply;
56     }
57
58     public static boolean isExist(String key) throws Exception {
59         boolean isExist = false;
60         Jedis jedis = null;
61         try {
62             jedis = JedisUtil.borrowJedisInstance();
63             isExist = jedis.exists(key);
64         } finally {
65             JedisUtil.returnJedisInstance(jedis);
66         }
67         return isExist;
68     }
69
70     public static List<String> queryMultiKeys(String keyPattern) throws Exception {
71         Set<String> keySet = filterKeys(keyPattern);
72         List<String> valueList = new ArrayList<>();
73         Jedis jedis = null;
74         try {
75             jedis = JedisUtil.borrowJedisInstance();
76             for(String key : keySet){
77                 String value = jedis.get(key);
78                 if(value !=null && !"".equals(value)){
79                     valueList.add(value);
80                 }
81             }
82         } finally {
83             JedisUtil.returnJedisInstance(jedis);
84         }
85         return valueList;
86     }
87
88     public static long deleteMultiKeys(String keyPattern) throws Exception {
89         Set<String> keySet = filterKeys(keyPattern);
90         long replySum = 0L;
91         Jedis jedis = null;
92         try {
93             jedis = JedisUtil.borrowJedisInstance();
94             for(String key : keySet){
95                 long reply = jedis.del(key);
96                 replySum = replySum + reply;
97             }
98         } finally {
99             JedisUtil.returnJedisInstance(jedis);
100         }
101         return replySum;
102     }
103
104     /**
105      * filter the keys according to the given pattern
106      * using "scan" instead of using "keys", incrementally iterate the keys space
107      * @param pattern the input filter pattern
108      * @return the matched keys set
109      */
110     public static Set<String> filterKeys(String pattern) throws Exception{
111         long start = System.currentTimeMillis();
112         Jedis jedis = null;
113         Set<String> filteredKeys = new HashSet<>();
114         ScanParams scanParams = new ScanParams();
115         scanParams.match(pattern);
116         scanParams.count(REDIS_SCAN_COUNT);
117         try {
118             jedis = JedisUtil.borrowJedisInstance();
119             ScanResult<String> scanResult = jedis.scan(REDIS_SCAN_POINTER_NEW_ITERATION,scanParams);
120             filteredKeys.addAll(scanResult.getResult());
121             while(!scanResult.getStringCursor().equals(REDIS_SCAN_POINTER_ITERATION_END)){
122                 scanResult = jedis.scan(scanResult.getStringCursor(),scanParams);
123                 filteredKeys.addAll(scanResult.getResult());
124             }
125         } finally {
126             JedisUtil.returnJedisInstance(jedis);
127         }
128         long end = System.currentTimeMillis();
129         long costTime = end-start;
130         LOGGER.info("filterKeys " + pattern + " count:" + filteredKeys.size() + " cost: " + costTime);
131         return filteredKeys;
132     }
133 }