public void moveZRange(String currentKey, String destKey) { Jedis jedis = null; try { jedis = pool.getResource(); ZParams zParams = new ZParams(); zParams.aggregate(ZParams.Aggregate.MIN); Long count = jedis.zunionstore(destKey, zParams, currentKey, destKey); Long removedCount = jedis.zremrangeByScore(currentKey.getBytes(), MIN_INF, MAX_INF); LOGGER.info("{0} tasks in default queue", count); LOGGER.info("Moved {0} tasks to default queue", removedCount); pool.returnResource(jedis); } catch (Exception e) { LOGGER.severe("Failed to get zunion keys from cache {0}:{1}", currentKey, destKey, e); pool.returnBrokenResource(jedis); throw e; } }
/** * @param dstkey dstkey * @param params params * @param sets sets * @return result */ @Override public Long zinterstore(final String dstkey, final ZParams params, final String... sets) { if (isSharding()) { throw new RedisClientException("The current configuration does not support this operation..."); } return this.performFunction("", new CallBack<Long>() { /** * invoke * @param jedis jedis * @return invoke results */ public Long invoke(Jedis jedis) { return jedis.zinterstore(dstkey, params, sets); } }); }
/** * @param dstkey dstkey * @param params params * @param sets sets * @return result */ @Override public Long zunionstore(final String dstkey, final ZParams params, final String... sets) { if (isSharding()) { throw new RedisClientException("The current configuration does not support this operation..."); } return this.performFunction("", new CallBack<Long>() { /** * invoke * @param jedis jedis * @return invoke results */ public Long invoke(Jedis jedis) { return jedis.zunionstore(dstkey, params, sets); } }); }
@Test public void zinterstore() { redisTransactionImpl.zadd("zset1", 1, "one"); redisTransactionImpl.zadd("zset1", 2, "two"); redisTransactionImpl.zadd("zset2", 1, "one"); redisTransactionImpl.zadd("zset2", 2, "two"); redisTransactionImpl.zadd("zset2", 3, "three"); ZParams params = new ZParams().aggregate(ZParams.Aggregate.SUM); params.weights(2, 3); this.redisTransactionImpl.zinterstore(key, params, "zset1", "zset2"); Set<Tuple> set = this.redisTransactionImpl.zrangeWithScores(key, 0, -1); Json.print(set, "set"); Assert.assertEquals("one,two", StringUtils.join(RedisUtil.tupleToString(set), ",")); Assert.assertEquals("5.0,10.0", StringUtils.join(RedisUtil.tupleToScores(set), ",")); }
/** * 返回WEIGHTS参数列表. * * @param params 参数 * @return */ public static List<Double> getWeights(ZParams params) { Collection<byte[]> collect = params.getParams(); Iterator<byte[]> iterator = collect.iterator(); boolean hasWeights = false; while (iterator.hasNext()) { String value = new String(iterator.next()); // System.out.println("value:" + value); if ("weights".equals(value)) { hasWeights = true; break; } } if (!hasWeights) { return null; } List<Double> result = new ArrayList<Double>(); while (iterator.hasNext()) { String weight = new String(iterator.next()); result.add(Double.parseDouble(weight)); } return result; }
@Test public void zinterstore() { redisTransactionImpl.zadd("zset1", 1, "one"); redisTransactionImpl.zadd("zset1", 2, "two"); redisTransactionImpl.zadd("zset2", 1, "one"); redisTransactionImpl.zadd("zset2", 2, "two"); redisTransactionImpl.zadd("zset2", 3, "three"); ZParams params = new ZParams().aggregate(ZParams.Aggregate.SUM); params.weights(2, 3); this.redisTransactionImpl.zinterstore(key, params, "zset1", "zset2"); Set<Tuple> set = this.redisTransactionImpl.zrangeWithScores(key, 0, -1); // Json.print(set, "set"); Assert.assertEquals("one,two", StringUtils.join(RedisUtil.tupleToString(set), ",")); Assert.assertEquals("5.0,10.0", StringUtils.join(RedisUtil.tupleToScores(set), ",")); }
@Test public void zinterstore2() { redis.zadd("zset1", 1, "one"); redis.zadd("zset1", 2, "two"); redis.zadd("zset2", 1, "one"); redis.zadd("zset2", 2, "two"); redis.zadd("zset2", 3, "three"); ZParams params = new ZParams().aggregate(ZParams.Aggregate.SUM); params.weights(2, 3); this.redis.zinterstore(key, params, "zset1", "zset2"); Set<Tuple> set = this.redis.zrangeWithScores(key, 0, -1); // Json.print(set, "set"); Assert.assertEquals("one,two", StringUtils.join(RedisUtil.tupleToString(set), ",")); Assert.assertEquals("5.0,10.0", StringUtils.join(RedisUtil.tupleToScores(set), ",")); }
@Override String execute(Pipeline pipe) { // Since the circle query doesn't have pre-defined precision, we need to calculate all the // relevant geohashes in our precision manually Set<String> hashKeys = getSearchHashes(); // now let's union them String[] keysArr = hashKeys.toArray(new String[hashKeys.size()]); double[] scoresArr = new double[hashKeys.size()];//all weights are zero String tmpKey = makeTmpKey(keysArr); pipe.zunionstore(tmpKey, new ZParams().aggregate(ZParams.Aggregate.MAX) .weightsByDouble(scoresArr), keysArr); return tmpKey; }
@Override public RedisValue<Long> zinterstore(CacheKey cacheKey, String dstkey, ZParams params, String... sets) { if (DEBUG_REDIS_CACHE) { String[] setList = new String[sets.length + 1]; setList[0] = dstkey; for (int i = 0; i < sets.length; i++) { setList[i + 1] = sets[i]; } logCall("zinterstore", setList); } Jedis jedis = pool.getResource(); try { return (new RedisValue<Long>()).setValue(jedis.zinterstore(dstkey, params, sets)); } finally { if (jedis != null) { jedis.close(); }; } }
/** * @param dstkey dstkey * @param params ZParams * @param sets sets * @return result */ @Override public Long zinterstore(final String dstkey, final ZParams params, final String... sets) { if (isSharding()) { throw new RedisClientException("The current configuration does not support this operation..."); } return this.performFunction("", new CallBack<Long>() { public Long invoke(Jedis jedis) { return jedis.zinterstore(dstkey, params, sets); } }); }
/** * @param dstkey dstkey * @param params params * @param sets sets * @return result */ @Override public Long zunionstore(final String dstkey, final ZParams params, final String... sets) { if (isSharding()) { throw new RedisClientException("The current configuration does not support this operation..."); } return this.performFunction("", new CallBack<Long>() { public Long invoke(Jedis jedis) { return jedis.zunionstore(dstkey, params, sets); } }); }
@Override public Long zunionstore(String dstkey, ZParams params, String... sets) { Span span = helper.buildSpan("zunionstore"); span.setTag("params", TracingHelper.toString(params.getParams())); span.setTag("sets", Arrays.toString(sets)); try { return super.zunionstore(dstkey, params, sets); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public Long zinterstore(String dstkey, ZParams params, String... sets) { Span span = helper.buildSpan("zinterstore"); span.setTag("dstkey", dstkey); span.setTag("params", TracingHelper.toString(params.getParams())); span.setTag("sets", Arrays.toString(sets)); try { return super.zinterstore(dstkey, params, sets); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public Long zunionstore(byte[] dstkey, ZParams params, byte[]... sets) { Span span = helper.buildSpan("zunionstore"); span.setTag("dstkey", Arrays.toString(dstkey)); span.setTag("params", TracingHelper.toString(params.getParams())); span.setTag("sets", TracingHelper.toString(sets)); try { return super.zunionstore(dstkey, params, sets); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public Long zinterstore(byte[] dstkey, ZParams params, byte[]... sets) { Span span = helper.buildSpan("zinterstore"); span.setTag("dstkey", Arrays.toString(dstkey)); span.setTag("params", TracingHelper.toString(params.getParams())); span.setTag("sets", TracingHelper.toString(sets)); try { return super.zinterstore(dstkey, params, sets); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@SuppressWarnings("deprecation") @Override public Long zinterstore(String dstkey, String... sets) { int[] weights = new int[sets.length]; for (int i = 0; i < sets.length; i++) { weights[i] = 1; } ZParams params = new ZParams().aggregate(ZParams.Aggregate.SUM); params.weights(weights); return zinterstore(dstkey, params, sets); }
@Override public Long zinterstore(final String dstkey, final ZParams params, final String... sets) { return (Long) this.execute(new Invoker() { @Override public Object execute(Jedis jedis) { return jedis.zinterstore(dstkey, params, sets); } }); }
@Override public Long zunionstore(final String dstkey, final ZParams params, final String... sets) { return (Long) this.execute(new Invoker() { @Override public Object execute(Jedis jedis) { return jedis.zunionstore(dstkey, params, sets); } }); }
/** * 返回AGGREGATE参数选项. * * @param params 参数 * @return */ public static ZParams.Aggregate getAggregate(ZParams params) { Collection<byte[]> collect = params.getParams(); Iterator<byte[]> iterator = collect.iterator(); String key = new String(iterator.next()); if (!"aggregate".equals(key)) { return ZParams.Aggregate.SUM; } String type = new String(iterator.next()); return ZParams.Aggregate.valueOf(type); }
@Override String execute(Pipeline pipe) { String[] tmpKeys = executeChildren(pipe); String tk = makeTmpKey(tmpKeys); pipe.zinterstore(tk, new ZParams().weightsByDouble(childrenWeights()), tmpKeys); pipe.expire(tk, DEFAULT_EXPIRATION); return tk; }
@Override String execute(Pipeline pipe) { String[] tmpKeys = executeChildren(pipe); String tk = makeTmpKey(tmpKeys); pipe.zunionstore(tk, new ZParams().weightsByDouble(childrenWeights()), tmpKeys); pipe.expire(tk, DEFAULT_EXPIRATION); return tk; }
@Test public void zunionstoreParams() { jedis.zadd("foo", 1, "a"); jedis.zadd("foo", 2, "b"); jedis.zadd("bar", 2, "a"); jedis.zadd("bar", 2, "b"); ZParams params = new ZParams(); params.weights(2, 2.5); params.aggregate(ZParams.Aggregate.SUM); long result = jedis.zunionstore("dst", params, "foo", "bar"); assertEquals(2, result); Set<Tuple> expected = new LinkedHashSet<Tuple>(); expected.add(new Tuple("b", new Double(9))); expected.add(new Tuple("a", new Double(7))); assertEquals(expected, jedis.zrangeWithScores("dst", 0, 100)); // Binary jedis.zadd(bfoo, 1, ba); jedis.zadd(bfoo, 2, bb); jedis.zadd(bbar, 2, ba); jedis.zadd(bbar, 2, bb); ZParams bparams = new ZParams(); bparams.weights(2, 2.5); bparams.aggregate(ZParams.Aggregate.SUM); long bresult = jedis.zunionstore(SafeEncoder.encode("dst"), bparams, bfoo, bbar); assertEquals(2, bresult); Set<Tuple> bexpected = new LinkedHashSet<Tuple>(); bexpected.add(new Tuple(bb, new Double(9))); bexpected.add(new Tuple(ba, new Double(7))); assertEquals(bexpected, jedis.zrangeWithScores(SafeEncoder.encode("dst"), 0, 100)); }
@Test public void zintertoreParams() { jedis.zadd("foo", 1, "a"); jedis.zadd("foo", 2, "b"); jedis.zadd("bar", 2, "a"); ZParams params = new ZParams(); params.weights(2, 2.5); params.aggregate(ZParams.Aggregate.SUM); long result = jedis.zinterstore("dst", params, "foo", "bar"); assertEquals(1, result); Set<Tuple> expected = new LinkedHashSet<Tuple>(); expected.add(new Tuple("a", new Double(7))); assertEquals(expected, jedis.zrangeWithScores("dst", 0, 100)); // Binary jedis.zadd(bfoo, 1, ba); jedis.zadd(bfoo, 2, bb); jedis.zadd(bbar, 2, ba); ZParams bparams = new ZParams(); bparams.weights(2, 2.5); bparams.aggregate(ZParams.Aggregate.SUM); long bresult = jedis.zinterstore(SafeEncoder.encode("dst"), bparams, bfoo, bbar); assertEquals(1, bresult); Set<Tuple> bexpected = new LinkedHashSet<Tuple>(); bexpected.add(new Tuple(ba, new Double(7))); assertEquals(bexpected, jedis.zrangeWithScores(SafeEncoder.encode("dst"), 0, 100)); }
@SuppressWarnings("unchecked") @Override public RedisValue<Long> zinterstore(final CacheKey cacheKey, final String dstkey, final ZParams params, final String... sets) { int len = params.getParams().size() + sets.length + 1; String[] actualParams = new String[len]; actualParams[0] = dstkey; int i = 1; for (byte[] p : params.getParams()) { try { actualParams[i++] = new String(p, "UTF-8"); } catch (Exception e) { throw new RuntimeException(e); } } for (String set : sets) { actualParams[i++] = set; } final String localKey = concat("zinterstore", actualParams); RedisValue<?> res = cacheWrapper.get(cacheKey, localKey, new Callable<RedisValue<?>>() { @Override public RedisValue<?> call() throws Exception { return provider.zinterstore(cacheKey, dstkey, params, sets); } }); return (RedisValue<Long>) res; }
@Override public RedisValue<Long> zinterstore(final CacheKey cacheKey, final String dstkey, final ZParams params, final String... sets) { int len = params.getParams().size() + sets.length + 1; String[] actualParams = new String[len]; actualParams[0] = dstkey; int i = 1; for (byte[] p : params.getParams()) { try { actualParams[i++] = new String(p, "UTF-8"); } catch (Exception e) { throw new RuntimeException(e); } } for (String set : sets) { actualParams[i++] = set; } concat("zinterstore", actualParams); RedisValue<Long> valueImpl = new RedisValue<Long>(); appendCommand(new PipelineCommand<Long>(valueImpl) { @Override public Response<Long> run() { return ((RedisValue<Long>) pipelineProvider.zinterstore(cacheKey, dstkey, params, sets)).getResponse(); } }); return valueImpl; }
@Override public Long zunionstore(String dstkey, ZParams params, String... sets) { redis.clients.jedis.Jedis delegate = pool.getResource(); try { return delegate.zunionstore(dstkey, params, sets); } finally { pool.returnResource(delegate); } }
@Override public Long zunionstore(byte[] dstkey, ZParams params, byte[]... sets) { redis.clients.jedis.Jedis delegate = pool.getResource(); try { return delegate.zunionstore(dstkey, params, sets); } finally { pool.returnResource(delegate); } }