Fix java check style warning
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / dao / RedisAccessWrapper.java
1 /*******************************************************************************
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  * 
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * 
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper.dao;
15
16 import java.util.ArrayList;
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.Set;
20
21 import org.onap.msb.apiroute.wrapper.util.JedisUtil;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import redis.clients.jedis.Jedis;
26 import redis.clients.jedis.ScanParams;
27 import redis.clients.jedis.ScanResult;
28
29 public class RedisAccessWrapper {
30     private static final Logger LOGGER = LoggerFactory.getLogger(RedisAccessWrapper.class);
31     // An iteration starts when the cursor is set to 0
32     private static final String REDIS_SCAN_POINTER_NEW_ITERATION = "0";
33     // An iteration terminated when the cursor returned by the server is 0
34     private static final String REDIS_SCAN_POINTER_ITERATION_END = "0";
35     private static final int REDIS_SCAN_COUNT = 50;
36
37
38     public static void save(String key, String value) throws Exception {
39         Jedis jedis = null;
40         try {
41             jedis = JedisUtil.borrowJedisInstance();
42             jedis.set(key, value);
43         } finally {
44             JedisUtil.returnJedisInstance(jedis);
45         }
46     }
47
48     public static String query(String key) throws Exception {
49         Jedis jedis = null;
50         String value = null;
51         try {
52             jedis = JedisUtil.borrowJedisInstance();
53             value = jedis.get(key);
54         } finally {
55             JedisUtil.returnJedisInstance(jedis);
56         }
57         return value;
58     }
59
60     public static long delete(String key) throws Exception {
61         Jedis jedis = null;
62         long reply = 0L;
63         try {
64             jedis = JedisUtil.borrowJedisInstance();
65             reply = jedis.del(key);
66         } finally {
67             JedisUtil.returnJedisInstance(jedis);
68         }
69         return reply;
70     }
71
72     public static boolean isExist(String key) throws Exception {
73         boolean isExist = false;
74         Jedis jedis = null;
75         try {
76             jedis = JedisUtil.borrowJedisInstance();
77             isExist = jedis.exists(key);
78         } finally {
79             JedisUtil.returnJedisInstance(jedis);
80         }
81         return isExist;
82     }
83
84     public static List<String> queryMultiKeys(String keyPattern) throws Exception {
85         Set<String> keySet = filterKeys(keyPattern);
86         List<String> valueList = new ArrayList<>();
87         Jedis jedis = null;
88         try {
89             jedis = JedisUtil.borrowJedisInstance();
90             for (String key : keySet) {
91                 String value = jedis.get(key);
92                 if (value != null && !"".equals(value)) {
93                     valueList.add(value);
94                 }
95             }
96         } finally {
97             JedisUtil.returnJedisInstance(jedis);
98         }
99         return valueList;
100     }
101
102     public static long deleteMultiKeys(String keyPattern) throws Exception {
103         Set<String> keySet = filterKeys(keyPattern);
104         long replySum = 0L;
105         Jedis jedis = null;
106         try {
107             jedis = JedisUtil.borrowJedisInstance();
108             for (String key : keySet) {
109                 long reply = jedis.del(key);
110                 replySum = replySum + reply;
111             }
112         } finally {
113             JedisUtil.returnJedisInstance(jedis);
114         }
115         return replySum;
116     }
117
118     /**
119      * filter the keys according to the given pattern using "scan" instead of using "keys",
120      * incrementally iterate the keys space
121      * 
122      * @param pattern the input filter pattern
123      * @return the matched keys set
124      */
125     public static Set<String> filterKeys(String pattern) throws Exception {
126         long start = System.currentTimeMillis();
127         Jedis jedis = null;
128         Set<String> filteredKeys = new HashSet<>();
129         ScanParams scanParams = new ScanParams();
130         scanParams.match(pattern);
131         scanParams.count(REDIS_SCAN_COUNT);
132         try {
133             jedis = JedisUtil.borrowJedisInstance();
134             ScanResult<String> scanResult = jedis.scan(REDIS_SCAN_POINTER_NEW_ITERATION, scanParams);
135             filteredKeys.addAll(scanResult.getResult());
136             while (!scanResult.getStringCursor().equals(REDIS_SCAN_POINTER_ITERATION_END)) {
137                 scanResult = jedis.scan(scanResult.getStringCursor(), scanParams);
138                 filteredKeys.addAll(scanResult.getResult());
139             }
140         } finally {
141             JedisUtil.returnJedisInstance(jedis);
142         }
143         long end = System.currentTimeMillis();
144         long costTime = end - start;
145         LOGGER.info("filterKeys " + pattern + " count:" + filteredKeys.size() + " cost: " + costTime);
146         return filteredKeys;
147     }
148 }