Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Grab(group='redis.clients', module='jedis', version='3.7.0')
- import redis.clients.jedis.*
- int SCAN_COUNT = 100;
- hostname = "127.0.0.1"
- port=6379
- password=""
- hashId="myHash"
- Jedis jedis = new Jedis(hostname, port)
- if (password != null && password.size() > 0){
- jedis.auth(password);
- }
- currentCursor = ScanParams.SCAN_POINTER_START;
- Set<String> idsRecovered = new HashSet<>() // To control if there's any data repeated
- long t = System.currentTimeMillis();
- toPrint = [ 'cdrs': 0, 'scans' : 0 , 'zeros' : 0, 'batchs' : 0, 'repeated' : 0 ]
- while (true) {
- currentCursor = ScanParams.SCAN_POINTER_START; // Here lies the problem
- List<String> myDeletingKeys = new ArrayList<>();
- while (true) {
- previousCursor = currentCursor
- ScanParams scanParams = new ScanParams().count(SCAN_COUNT);
- def hscanResult = jedis.hscan(hashId, currentCursor, scanParams);
- currentCursor = hscanResult.getCursor();
- currentData = hscanResult.result
- currentData.forEach( {
- myDeletingKeys.add(it.key)
- if (!idsRecovered.add(it.key)) {
- toPrint['repeated']++
- }
- })
- toPrint['scans']++
- if (currentData.isEmpty() && !hscanResult.isCompleteIteration()) {
- toPrint['zeros']++
- }
- toPrint['cdrs'] += currentData.size()
- println "${toPrint} results: ${currentData.size()} previousCursor ${previousCursor} currentCursor: ${currentCursor}"
- if (hscanResult.isCompleteIteration()) {
- break;
- }
- if (toPrint['scans'] % 20 == 0) {
- break;
- }
- }
- toPrint['batchs']++
- currentSize = jedis.hlen(hashId)
- println "Map is ${currentSize} and ${toPrint} and im going to delete ${myDeletingKeys.size()}"
- println "Deleting elements"
- if (!myDeletingKeys.isEmpty()) {
- String[] toDelArray = myDeletingKeys.toArray(new String[myDeletingKeys.size()])
- jedis.hdel(poolId, toDelArray)
- }
- currentSize = getWithJedis( { jedis -> jedis.hlen(hashId)})
- doWithJedis( { it.ping() })
- println "Map is ${currentSize} and ${toPrint}"
- if (currentSize == 0) {
- break
- }
- if (toPrint['scans'] % 150010 == 0) {
- break
- }
- if (toPrint['batchs'] % 15000 == 0) {
- break
- }
- }
- long tttTotal = System.currentTimeMillis() - t
- print """
- End !
- Cdrs ${toPrint['cdrs']}
- Repeated ${toPrint['repeated']}
- Scans ${toPrint['scans']}
- Zeros ${toPrint['zeros']}
- Batchs ${toPrint['batchs']}
- t ${tttTotal}
- """
- doWithJedis( { it.del(hashId)})
- jedisPool.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement