forked from AlEinstein/javaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWRedisHelper
More file actions
282 lines (235 loc) · 9.81 KB
/
Copy pathWRedisHelper
File metadata and controls
282 lines (235 loc) · 9.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package com.bj58.bic.pan.scf.utils;
import com.bj58.bic.pan.scf.constants.CfgConstant;
import com.bj58.bic.pan.scf.exception.WRedisException;
import com.bj58.spat.wredis.client.builder.ClientBuilder;
import com.bj58.spat.wredis.client.config.ClientConfig;
import com.bj58.spat.wredis.client.wjedis.WJedis;
import com.bj58.spat.wredis.client.wjedis.WJedisSentinelPool;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.bj58.bic.pan.scf.enums.StatusEnum.Result.*;
public class WRedisHelper {
private static Logger logger = LogManager.getLogger(WRedisHelper.class);
private static final String SET_SUCCESS = "OK";
private static final String SET_IF_NOT_EXIST = "NX";
private static final String SET_EXPIRE_SECONDS = "EX";
private static WJedisSentinelPool redisSentinelPool;
private static final String APP_NAME = "aaa";
private static final int COUNT = 20;
private static final Pattern pattern = Pattern.compile("\\(\\d+\\)$"); //判断是否以带数字的括号结尾
static {
ClientConfig.init(CfgConstant.WREDIS_CONFIG_PATH);
redisSentinelPool = ClientBuilder.redisSentinel(APP_NAME).build();
}
private static WJedis getJedis() {
return redisSentinelPool.getResource();
}
private static void releaseJedis(final WJedis jedis) {
if (jedis != null) {
jedis.close();
}
}
/**
* 重名处理-获取name
* @author : mulin
* @date : 2019/8/23
*/
public static synchronized String setName(String name, Long pid, int type) throws Exception{
logger.info("setName -- name={}, pid={}, type={}", name, pid, type);
WJedis jedis = getJedis();
try {
String key = pid + "_" + name.hashCode() + "_" + type;
long offset = 0L;
//判断name是否以带数字的括号结尾
Matcher matcher = pattern.matcher(name);
if (matcher.find()) { //name后带有括号
GetShortName getShortName = new GetShortName(name, matcher).invoke();
long shortIndex = getShortName.getOffset();
String shortKey = pid + "_" + getShortName.getName().hashCode() + "_" + type;
//判断名字是否重复
if(jedis.getbit(key, offset) || jedis.getbit(shortKey, shortIndex)) {
offset = jedis.bitpos(key, false);
String newKey= pid + "_" + (name + "("+offset+")").hashCode() + "_" + type;
while(jedis.getbit(newKey, 0)) {
jedis.setbit(key, offset, true);
offset++;
newKey = pid + "_" + (name + "("+offset+")").hashCode() + "_" + type;
}
jedis.setbit(newKey, 0, true);
jedis.setbit(key, offset, true);
String newName = name + "("+offset+")";
logger.info("setName -- newName={}", newName);
return newName;
}
jedis.setbit(key, offset, true);
jedis.setbit(shortKey, shortIndex, true);
logger.info("setName -- name={}", name);
return name;
}
//如果名字不重复,则直接setbit
if(!jedis.getbit(key, offset)) {
jedis.setbit(key, offset, true);
return name;
}
logger.info("setName -- offset={}", offset);
offset = jedis.bitpos(key, false); //第一个为false的bit
jedis.setbit(key, offset, true);
String newName = name+"(" + offset + ")";
jedis.setbit((pid + "_" + newName.hashCode() + "_" + type), 0, true);
logger.info("setName -- newName={}", newName);
return newName;
} catch (Exception e) {
logger.error("setName -- name处理失败 name={}, pid={}, type={}", name, pid, type, e);
e.printStackTrace();
throw new WRedisException();
} finally {
releaseJedis(jedis);
}
}
/**
* 重名处理-删除name
* @author : mulin
* @date : 2019/8/23
*/
public static synchronized boolean delName(String name, Long pid, int type) {
logger.info("delName -- name={}, pid={}, type={}", name, pid, type);
WJedis jedis = getJedis();
try {
//获取name后括号中的数字
Matcher matcher = pattern.matcher(name);
long offset = 0L;
if (matcher.find()) {
logger.info("delName -- name={} end by '(number)'", name);
GetShortName getShortName = new GetShortName(name, matcher).invoke();
String shortName = getShortName.getName();
long shortIndex = getShortName.getOffset();
String shortKey = pid + "_" + shortName.hashCode() + "_" + type;
logger.info("delName -- shortName={}, index={}", shortName, shortIndex);
jedis.setbit(shortKey, shortIndex, false);
}
String key = pid + "_" + name.hashCode() + "_" + type;
logger.info("delName -- name={}, index={}", name, offset);
jedis.setbit(key, offset, false);
return true;
} catch (Exception e) {
logger.error("delName -- name处理失败 name={}, pid={}, type={}", name, pid, type, e);
return false;
} finally {
releaseJedis(jedis);
}
}
/**
* 重名处理-判断该name是否重复
* @author : mulin
* @date : 2019/8/26
*/
public static synchronized boolean checkIsExist(String name, Long pid, int type) throws Exception {
logger.info("checkIsExist -- name={}, pid={}, type={}", name, pid, type);
WJedis jedis = getJedis();
try {
String key = pid + "_" + name.hashCode() + "_" + type;
return jedis.bitcount(key) > 0;
} catch (Exception e) {
logger.error("checkIsExist -- name处理失败 name={}, pid={}, type={}", name, pid, type, e);
throw new WRedisException(serverError);
} finally {
releaseJedis(jedis);
}
}
/**
* 批量删除该pid下的所有目录和文件
* @author : mulin
* @date : 2019/9/9
*/
public static Long delDirsAndDocsUnderPid(Long pid) throws Exception {
logger.info("delDirsAndDocsUnderPid -- pid={}", pid);
WJedis jedis = getJedis();
try {
String pattern = pid + "_*";
List<String> keys = new ArrayList<>();
String cursor = ScanParams.SCAN_POINTER_START;
ScanParams scanParams = new ScanParams();
scanParams.count(COUNT);
scanParams.match(pattern);
logger.info("delDirsAndDocsUnderPid -- pid={}, pattern={}", pid, pattern);
do {
ScanResult<String> scanResult = jedis.scan(cursor, scanParams);
keys.addAll(scanResult.getResult());
cursor = scanResult.getStringCursor();
}while(!"0".equals(cursor));
logger.info("delDirsAndDocsUnderPid -- keys={}", keys);
Iterator<String> key = keys.iterator();
Long del = 0L;
while(key.hasNext()) {
String keyStr = key.next();
logger.info("delDirsAndDocsUnderPid delKey -- key={}", keyStr);
del += jedis.del(keyStr);
}
return del;
} catch (Exception e) {
logger.error("delDirsAndDocsUnderPid -- 数据删除失败 pid={}", pid);
throw new WRedisException(serverError);
} finally {
releaseJedis(jedis);
}
}
/**
* 文件/目录移动
*
* @param sourceName 源name
* @param sourcePid 源pid
* @param type 数据类型 文件/目录
* @param targetPid 目标pid
* @author : mulin
* @date : 2019/9/10
*/
public static synchronized void moveDocsAndDirs(String sourceName, Long sourcePid, int type, Long targetPid) throws Exception {
logger.info("moveDocsAndDirs -- sourceName={}, sourcePid={}, type={}, targetPid={}",sourceName, sourcePid, type, targetPid);
WJedis jedis = getJedis();
String name = null;
try {
name = setName(sourceName, targetPid, type);
delName(sourceName, sourcePid, type);
} catch (Exception e) {
logger.error("moveDocsAndDirs 操作失败 -- sourceName={}, sourcePid={}, type={}, targetPid={}",sourceName, sourcePid, type, targetPid);
//回滚
if(StringUtils.isNotEmpty(name)) {
delName(name, targetPid, type);
}
throw new WRedisException(serverError);
} finally {
releaseJedis(jedis);
}
}
private static class GetShortName {
private String name;
private Matcher matcher;
private long offset;
GetShortName(String name, Matcher matcher) {
this.name = name;
this.matcher = matcher;
}
private String getName() {
return name;
}
private long getOffset() {
return offset;
}
private GetShortName invoke() {
String index = matcher.group(0);
index = index.substring(1, index.length()-1);
offset = Long.valueOf(index);
//name去掉括号和数字,获取key
name = name.substring(0, name.length()-index.length()-2);
return this;
}
}
}