@Test public void zscan() { jedis.zadd("foo", 1, "a"); jedis.zadd("foo", 2, "b"); ScanResult<Tuple> result = jedis.zscan("foo", SCAN_POINTER_START); assertEquals(SCAN_POINTER_START, result.getStringCursor()); assertFalse(result.getResult().isEmpty()); // binary jedis.zadd(bfoo, 1, ba); jedis.zadd(bfoo, 1, bb); ScanResult<Tuple> bResult = jedis.zscan(bfoo, SCAN_POINTER_START_BINARY); assertArrayEquals(SCAN_POINTER_START_BINARY, bResult.getCursorAsBytes()); assertFalse(bResult.getResult().isEmpty()); }
@Test public void sscan() { jedis.sadd("foo", "a", "b"); ScanResult<String> result = jedis.sscan("foo", SCAN_POINTER_START); assertEquals(SCAN_POINTER_START, result.getStringCursor()); assertFalse(result.getResult().isEmpty()); // binary jedis.sadd(bfoo, ba, bb); ScanResult<byte[]> bResult = jedis.sscan(bfoo, SCAN_POINTER_START_BINARY); assertArrayEquals(SCAN_POINTER_START_BINARY, bResult.getCursorAsBytes()); assertFalse(bResult.getResult().isEmpty()); }
@Test public void scan() { jedis.set("b", "b"); jedis.set("a", "a"); ScanResult<String> result = jedis.scan(SCAN_POINTER_START); assertEquals(SCAN_POINTER_START, result.getStringCursor()); assertFalse(result.getResult().isEmpty()); // binary ScanResult<byte[]> bResult = jedis.scan(SCAN_POINTER_START_BINARY); assertArrayEquals(SCAN_POINTER_START_BINARY, bResult.getCursorAsBytes()); assertFalse(bResult.getResult().isEmpty()); }
@Test public void hscan() { jedis.hset("foo", "b", "b"); jedis.hset("foo", "a", "a"); ScanResult<Map.Entry<String, String>> result = jedis.hscan("foo", SCAN_POINTER_START); assertEquals(SCAN_POINTER_START, result.getStringCursor()); assertFalse(result.getResult().isEmpty()); // binary jedis.hset(bfoo, bbar, bcar); ScanResult<Map.Entry<byte[], byte[]>> bResult = jedis.hscan(bfoo, SCAN_POINTER_START_BINARY); assertArrayEquals(SCAN_POINTER_START_BINARY, bResult.getCursorAsBytes()); assertFalse(bResult.getResult().isEmpty()); }
/** * 全局扫描hset * * @param match field匹配模式 */ public List<Map.Entry<String, String>> scanHSet(String domain, String match) { try (ShardedJedis shardedJedis = shardedJedisPool.getResource()) { int cursor = 0; ScanParams scanParams = new ScanParams(); scanParams.match(match); Jedis jedis = shardedJedis.getShard(domain); ScanResult<Map.Entry<String, String>> scanResult; List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(); do { scanResult = jedis.hscan(domain, String.valueOf(cursor), scanParams); list.addAll(scanResult.getResult()); cursor = Integer.parseInt(scanResult.getStringCursor()); } while (cursor > 0); return list; } }
@Override public List<Map.Entry<String, String>> hscan(final String bizkey, final String nameSpace, final String match) { final String key = CacheUtils.getKeyByNamespace(bizkey,nameSpace); return this.performFunction(key, new CallBack<List<Map.Entry<String, String>>>() { public List<Map.Entry<String, String>> invoke(Jedis jedis) { try{ int cursor = 0; ScanParams scanParams = new ScanParams(); scanParams.match(match); ScanResult<Map.Entry<String,String>> scanResult; List<Map.Entry<String, String>> res = new ArrayList<Map.Entry<String, String>>(); do { scanResult = jedis.hscan(key, String.valueOf(cursor), scanParams); res.addAll(scanResult.getResult()); cursor = Integer.parseInt(scanResult.getStringCursor()); } while (cursor > 0); return res; } catch (Exception ex) { logger.error("hscan key:"+key+",match:"+match+",error:", ex); } return null; } }); }
@Override public Set<String> sscan(final String bizkey,final String nameSpace, final String match) { final String key = CacheUtils.getKeyByNamespace(bizkey,nameSpace); return this.performFunction(key, new CallBack<Set<String>>() { public Set<String> invoke(Jedis jedis) { try{ int cursor = 0; ScanParams scanParams = new ScanParams(); scanParams.match(match); ScanResult<String> scanResult; Set<String> res = new HashSet<String>(); do { scanResult = jedis.sscan(key, String.valueOf(cursor), scanParams); res.addAll(scanResult.getResult()); cursor = Integer.parseInt(scanResult.getStringCursor()); } while (cursor > 0); return res; } catch (Exception ex) { logger.error("sscan key:"+key+",match:"+match+",error:", ex); } return null; } }); }
private void listKeys0() { if (StringUtils.isBlank(this.txtKeyPattern.getText())) { this.txtKeyPattern.setText("*"); } String pattern = this.txtKeyPattern.getText(); int limit = Integer.parseInt(String.valueOf(cmbLimit.getValue())); ObservableList<KeyItem> items = this.tblKeys.getItems(); items.clear(); if (pattern.trim().length() > 0) { try (Jedis jedis = JedisManager.getJedis()) { ScanParams scanParams = new ScanParams().match(pattern).count(limit); ScanResult<String> result = jedis.scan(ScanParams.SCAN_POINTER_START, scanParams); result.getResult().forEach(key -> { String type = jedis.type(key); items.add(new KeyItem(key, type)); }); } } }
@Override public Completable removeAll() { return Completable.fromAction(() -> { Set<String> matchingKeys = new HashSet<>(); ScanParams params = new ScanParams(); params.match(prefixAndDelim + "*"); try(Jedis jedis = pool.getResource()) { String nextCursor = "0"; do { ScanResult<String> scanResult = jedis.scan(nextCursor, params); List<String> keys = scanResult.getResult(); nextCursor = scanResult.getStringCursor(); matchingKeys.addAll(keys); } while(!nextCursor.equals("0")); jedis.del(matchingKeys.toArray(new String[matchingKeys.size()])); } }); }
@Override public List<Map.Entry<String, String>> hscan(final String bizkey, final String nameSpace, final String match) { final String key = CacheUtils.getKeyByNamespace(bizkey, nameSpace); return this.performFunction(key, new CallBack<List<Map.Entry<String, String>>>() { public List<Map.Entry<String, String>> invoke(Jedis jedis) { try { int cursor = 0; ScanParams scanParams = new ScanParams(); scanParams.match(match); ScanResult<Map.Entry<String, String>> scanResult; List<Map.Entry<String, String>> res = new ArrayList<Map.Entry<String, String>>(); do { scanResult = jedis.hscan(key, String.valueOf(cursor), scanParams); res.addAll(scanResult.getResult()); cursor = Integer.parseInt(scanResult.getStringCursor()); } while (cursor > 0); return res; } catch (Exception ex) { logger.error("hscan key:" + key + ",match:" + match + ",error:", ex); } return null; } }); }
@Override public Set<String> sscan(final String bizkey, final String nameSpace, final String match) { final String key = CacheUtils.getKeyByNamespace(bizkey, nameSpace); return this.performFunction(key, new CallBack<Set<String>>() { public Set<String> invoke(Jedis jedis) { try { int cursor = 0; ScanParams scanParams = new ScanParams(); scanParams.match(match); ScanResult<String> scanResult; Set<String> res = new HashSet<String>(); do { scanResult = jedis.sscan(key, String.valueOf(cursor), scanParams); res.addAll(scanResult.getResult()); cursor = Integer.parseInt(scanResult.getStringCursor()); } while (cursor > 0); return res; } catch (Exception ex) { logger.error("sscan key:" + key + ",match:" + match + ",error:", ex); } return null; } }); }
public Map<String, String> hscan(String key, int count) { Map<String, String> m = new HashMap<>(); int cursor = 0; do { ScanResult<Entry<String, String>> sr = dynoClient.hscan(key, "" + cursor); cursor = Integer.parseInt(sr.getStringCursor()); for (Entry<String, String> r : sr.getResult()) { m.put(r.getKey(), r.getValue()); } if(m.size() > count) { break; } } while (cursor > 0); return m; }
public Set<String> hkeys(String key) { logger.trace("hkeys {}", key); JedisCommands client = dynoClient; Set<String> keys = new HashSet<>(); int cursor = 0; do { ScanResult<Entry<String, String>> sr = client.hscan(key, "" + cursor); cursor = Integer.parseInt(sr.getStringCursor()); List<Entry<String, String>> result = sr.getResult(); for (Entry<String, String> e : result) { keys.add(e.getKey()); } } while (cursor > 0); return keys; }
public Set<String> smembers(String key) { logger.trace("smembers {}", key); JedisCommands client = dynoClient; Set<String> r = new HashSet<>(); int cursor = 0; ScanParams sp = new ScanParams(); sp.count(50); do { ScanResult<String> sr = client.sscan(key, "" + cursor, sp); cursor = Integer.parseInt(sr.getStringCursor()); r.addAll(sr.getResult()); } while (cursor > 0); return r; }
@Test public void scan() { jedis.set("b", "b"); jedis.set("a", "a"); ScanResult<String> result = jedis.scan(SCAN_POINTER_START); assertEquals(SCAN_POINTER_START, result.getCursor()); assertFalse(result.getResult().isEmpty()); // binary ScanResult<byte[]> bResult = jedis.scan(SCAN_POINTER_START_BINARY); assertArrayEquals(SCAN_POINTER_START_BINARY, bResult.getCursorAsBytes()); assertFalse(bResult.getResult().isEmpty()); }
@Test public void hscan() { jedis.hset("foo", "b", "b"); jedis.hset("foo", "a", "a"); ScanResult<Map.Entry<String, String>> result = jedis.hscan("foo", SCAN_POINTER_START); assertEquals(SCAN_POINTER_START, result.getCursor()); assertFalse(result.getResult().isEmpty()); // binary jedis.hset(bfoo, bbar, bcar); ScanResult<Map.Entry<byte[], byte[]>> bResult = jedis.hscan(bfoo, SCAN_POINTER_START_BINARY); assertArrayEquals(SCAN_POINTER_START_BINARY, bResult.getCursorAsBytes()); assertFalse(bResult.getResult().isEmpty()); }
/** * 返回指定数量的匹配值 * * @param cursor * @param count * @param patterns * @return */ public ScanResult<String> scan(final String cursor, final Integer count, final String... patterns) { ScanParams params = new ScanParams(); for (String pattern : patterns) { params.match(pattern); } if (null != count) { params.count(count); } if (RedisConstants.isInUse()) { return getTemplate().executeRead(new JedisCallBackHandler<ScanResult<String>>() { public ScanResult<String> invoke(Jedis jedis) { return jedis.scan(cursor, params); } }); } else { return null; } }
/** * 命令用于迭代哈希键中的键值对 * * @param key * @param cursor * @param count * @param patterns * @return */ public ScanResult<Entry<String, String>> hscan(final String key, final String cursor, final Integer count, final String... patterns) { ScanParams params = new ScanParams(); for (String pattern : patterns) { params.match(pattern); } if (null != count) { params.count(count); } if (RedisConstants.isInUse()) { return getTemplate().executeRead(new JedisCallBackHandler<ScanResult<Entry<String, String>>>() { public ScanResult<Entry<String, String>> invoke(Jedis jedis) { return jedis.hscan(key, cursor, params); } }); } else { return null; } }
ScanResult<Integer> sscanInteger(final String key, final String cursor) throws FedisException { final ActionResult<ScanResult<Integer>> result = new ActionResult<ScanResult<Integer>>(); redisProxy.runCommand(key, new Action<Jedis>() { @Override public void run(Jedis jedis) { ScanResult<String> returnValue = jedis.sscan(key, String.valueOf(cursor)); List<Integer> integerList = new ArrayList<Integer>(); for (String s : returnValue.getResult()) { integerList.add(Integer.valueOf(s)); } result.setValue(new ScanResult<Integer>(returnValue.getCursorAsBytes(), integerList)); } }); return result.getValue(); }
/** * 命令用于迭代哈希键中的键值对 * * @param key * @param cursor * @param count * @param patterns * @return */ public ScanResult<Entry<String, String>> hscan(final String key, final String cursor, final Integer count, final String... patterns) { ScanParams params = new ScanParams(); for (String pattern : patterns) { params.match(pattern); } if (null != count) { params.count(count); } if (RedisConstants.isInUse()) { return getTemplate().execute(new JedisCallBackHandler<ScanResult<Entry<String, String>>>() { public ScanResult<Entry<String, String>> invoke(Jedis jedis) { return jedis.hscan(key, cursor, params); } }); } else { return null; } }
@Override public ScanResult<Entry<String, String>> hscan(String key, int cursor) { Span span = helper.buildSpan("hscan", key); span.setTag("cursor", cursor); try { return super.hscan(key, cursor); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public ScanResult<String> sscan(String key, int cursor) { Span span = helper.buildSpan("sscan", key); span.setTag("cursor", cursor); try { return super.sscan(key, cursor); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public ScanResult<Tuple> zscan(String key, int cursor) { Span span = helper.buildSpan("zscan", key); span.setTag("cursor", cursor); try { return super.zscan(key, cursor); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public ScanResult<String> scan(String cursor, ScanParams params) { Span span = helper.buildSpan("scan"); span.setTag("cursor", cursor); span.setTag("params", TracingHelper.toString(params.getParams())); try { return super.scan(cursor, params); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public ScanResult<Entry<String, String>> hscan(String key, String cursor) { Span span = helper.buildSpan("hscan", key); span.setTag("cursor", cursor); try { return super.hscan(key, cursor); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public ScanResult<Entry<String, String>> hscan(String key, String cursor, ScanParams params) { Span span = helper.buildSpan("hscan", key); span.setTag("cursor", cursor); span.setTag("params", TracingHelper.toString(params.getParams())); try { return super.hscan(key, cursor, params); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public ScanResult<String> sscan(String key, String cursor) { Span span = helper.buildSpan("sscan", key); span.setTag("cursor", cursor); try { return super.sscan(key, cursor); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public ScanResult<String> sscan(String key, String cursor, ScanParams params) { Span span = helper.buildSpan("sscan", key); span.setTag("cursor", cursor); span.setTag("params", TracingHelper.toString(params.getParams())); try { return super.sscan(key, cursor, params); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public ScanResult<Tuple> zscan(String key, String cursor, ScanParams params) { Span span = helper.buildSpan("zscan", key); span.setTag("cursor", cursor); span.setTag("params", TracingHelper.toString(params.getParams())); try { return super.zscan(key, cursor, params); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public ScanResult<byte[]> scan(byte[] cursor, ScanParams params) { Span span = helper.buildSpan("scan"); span.setTag("cursor", Arrays.toString(cursor)); span.setTag("params", TracingHelper.toString(params.getParams())); try { return super.scan(cursor, params); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public ScanResult<Entry<byte[], byte[]>> hscan(byte[] key, byte[] cursor) { Span span = helper.buildSpan("hscan", key); span.setTag("cursor", Arrays.toString(cursor)); try { return super.hscan(key, cursor); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public ScanResult<Entry<byte[], byte[]>> hscan(byte[] key, byte[] cursor, ScanParams params) { Span span = helper.buildSpan("hscan", key); span.setTag("cursor", Arrays.toString(cursor)); span.setTag("params", TracingHelper.toString(params.getParams())); try { return super.hscan(key, cursor, params); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public ScanResult<byte[]> sscan(byte[] key, byte[] cursor) { Span span = helper.buildSpan("sscan", key); span.setTag("cursor", Arrays.toString(cursor)); try { return super.sscan(key, cursor); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public ScanResult<byte[]> sscan(byte[] key, byte[] cursor, ScanParams params) { Span span = helper.buildSpan("sscan", key); span.setTag("cursor", Arrays.toString(cursor)); span.setTag("params", TracingHelper.toString(params.getParams())); try { return super.sscan(key, cursor, params); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public ScanResult<Tuple> zscan(byte[] key, byte[] cursor) { Span span = helper.buildSpan("zscan", key); span.setTag("cursor", Arrays.toString(cursor)); try { return super.zscan(key, cursor); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public ScanResult<Tuple> zscan(byte[] key, byte[] cursor, ScanParams params) { Span span = helper.buildSpan("zscan", key); span.setTag("cursor", Arrays.toString(cursor)); span.setTag("params", TracingHelper.toString(params.getParams())); try { return super.zscan(key, cursor, params); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public ScanResult<String> scan(int cursor) { Span span = helper.buildSpan("scan"); span.setTag("cursor", cursor); try { return super.scan(cursor); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public ScanResult<String> scan(int cursor, ScanParams params) { Span span = helper.buildSpan("scan"); span.setTag("cursor", cursor); span.setTag("params", TracingHelper.toString(params.getParams())); try { return super.scan(cursor, params); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public ScanResult<Entry<String, String>> hscan(String key, int cursor, ScanParams params) { Span span = helper.buildSpan("hscan", key); span.setTag("cursor", cursor); span.setTag("params", TracingHelper.toString(params.getParams())); try { return super.hscan(key, cursor, params); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }