Advertisement
OscarBesga_Panel

groovy hscan empty results

Nov 19th, 2021
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 2.61 KB | None | 0 0
  1. @Grab(group='redis.clients', module='jedis', version='3.7.0')
  2.  
  3. import redis.clients.jedis.*
  4.  
  5. int SCAN_COUNT = 100;
  6.  
  7. hostname = "127.0.0.1"
  8. port=6379
  9. password=""
  10.  
  11. hashId="myHash"
  12.  
  13.  
  14. Jedis jedis = new Jedis(hostname, port)
  15. if (password != null && password.size() > 0){
  16.     jedis.auth(password);
  17. }
  18.  
  19. currentCursor = ScanParams.SCAN_POINTER_START;
  20. Set<String> idsRecovered = new HashSet<>() // To control if there's any data repeated
  21. long t = System.currentTimeMillis();
  22. toPrint = [ 'cdrs': 0, 'scans' : 0 , 'zeros' : 0, 'batchs' : 0, 'repeated' : 0 ]
  23. while (true) {
  24.     currentCursor = ScanParams.SCAN_POINTER_START; // Here lies the problem
  25.     List<String> myDeletingKeys = new ArrayList<>();
  26.     while (true) {
  27.         previousCursor = currentCursor
  28.         ScanParams scanParams = new ScanParams().count(SCAN_COUNT);
  29.         def hscanResult = jedis.hscan(hashId, currentCursor, scanParams);
  30.         currentCursor = hscanResult.getCursor();
  31.         currentData = hscanResult.result
  32.         currentData.forEach( {
  33.             myDeletingKeys.add(it.key)
  34.             if (!idsRecovered.add(it.key)) {
  35.                 toPrint['repeated']++
  36.             }
  37.         })
  38.         toPrint['scans']++
  39.         if (currentData.isEmpty() && !hscanResult.isCompleteIteration()) {
  40.             toPrint['zeros']++
  41.         }
  42.         toPrint['cdrs'] += currentData.size()
  43.         println "${toPrint} results: ${currentData.size()} previousCursor ${previousCursor} currentCursor: ${currentCursor}"
  44.         if (hscanResult.isCompleteIteration()) {
  45.             break;
  46.         }
  47.         if (toPrint['scans'] % 20 == 0) {
  48.             break;
  49.         }
  50.     }
  51.     toPrint['batchs']++
  52.     currentSize = jedis.hlen(hashId)
  53.     println "Map is ${currentSize} and ${toPrint} and im going to delete ${myDeletingKeys.size()}"
  54.     println "Deleting elements"
  55.     if (!myDeletingKeys.isEmpty()) {
  56.         String[] toDelArray = myDeletingKeys.toArray(new String[myDeletingKeys.size()])
  57.         jedis.hdel(poolId, toDelArray)
  58.     }
  59.  
  60.     currentSize = getWithJedis( { jedis -> jedis.hlen(hashId)})
  61.     doWithJedis( { it.ping() })
  62.     println "Map is ${currentSize} and ${toPrint}"
  63.     if (currentSize == 0) {
  64.         break
  65.     }
  66.     if (toPrint['scans'] % 150010 == 0) {
  67.         break
  68.     }
  69.     if (toPrint['batchs'] % 15000 == 0) {
  70.         break
  71.     }
  72. }
  73.  
  74. long tttTotal = System.currentTimeMillis() - t
  75.  
  76. print """
  77. End !
  78. Cdrs ${toPrint['cdrs']}
  79. Repeated ${toPrint['repeated']}
  80. Scans ${toPrint['scans']}
  81. Zeros ${toPrint['zeros']}
  82. Batchs ${toPrint['batchs']}
  83. t ${tttTotal}
  84. """
  85.  
  86. doWithJedis( { it.del(hashId)})
  87. jedisPool.close()
  88.  
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement