private <T> NodeResult<T> executeCommandOnSingleNode(LettuceClusterCommandCallback<T> command, RedisClusterNode node) { return connection.getClusterCommandExecutor().executeCommandOnSingleNode(command, node); }
private <T> MultiNodeResult<T> executeCommandOnAllNodes(final LettuceClusterCommandCallback<T> cmd) { return connection.getClusterCommandExecutor().executeCommandOnAllNodes(cmd); }
@Override public Set<byte[]> keys(byte[] pattern) { Assert.notNull(pattern, "Pattern must not be null!"); Collection<List<byte[]>> keysPerNode = connection.getClusterCommandExecutor() .executeCommandOnAllNodes((LettuceClusterCommandCallback<List<byte[]>>) connection -> connection.keys(pattern)) .resultsAsList(); Set<byte[]> keys = new HashSet<>(); for (List<byte[]> keySet : keysPerNode) { keys.addAll(keySet); } return keys; }
@Nullable public Set<byte[]> keys(RedisClusterNode node, byte[] pattern) { Assert.notNull(pattern, "Pattern must not be null!"); return LettuceConverters.toBytesSet(connection.getClusterCommandExecutor() .executeCommandOnSingleNode((LettuceClusterCommandCallback<List<byte[]>>) client -> client.keys(pattern), node) .getValue()); }
@Nullable public byte[] randomKey(RedisClusterNode node) { return connection.getClusterCommandExecutor() .executeCommandOnSingleNode((LettuceClusterCommandCallback<byte[]>) client -> client.randomkey(), node) .getValue(); }
/** * Use a {@link Cursor} to iterate over keys stored at the given {@link RedisClusterNode}. * * @param node must not be {@literal null}. * @param options must not be {@literal null}. * @return never {@literal null}. * @since 2.1 */ Cursor<byte[]> scan(RedisClusterNode node, ScanOptions options) { Assert.notNull(node, "RedisClusterNode must not be null!"); Assert.notNull(options, "Options must not be null!"); return connection.getClusterCommandExecutor() .executeCommandOnSingleNode((LettuceClusterCommandCallback<ScanCursor<byte[]>>) client -> { return new LettuceScanCursor<byte[]>(options) { @Override protected LettuceScanIteration<byte[]> doScan(io.lettuce.core.ScanCursor cursor, ScanOptions options) { ScanArgs scanArgs = LettuceConverters.toScanArgs(options); KeyScanCursor<byte[]> keyScanCursor = client.scan(cursor, scanArgs); return new LettuceScanIteration<>(keyScanCursor, keyScanCursor.getKeys()); } }.open(); }, node).getValue(); }
@Override public Long time() { return convertListOfStringToTime(connection.getClusterCommandExecutor() .executeCommandOnArbitraryNode((LettuceClusterCommandCallback<List<byte[]>>) RedisServerCommands::time) .getValue()); }
@Override public List<byte[]> bRPop(int timeout, byte[]... keys) { Assert.notNull(keys, "Keys must not be null!"); Assert.noNullElements(keys, "Keys must not contain null elements!"); if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) { return super.bRPop(timeout, keys); } List<KeyValue<byte[], byte[]>> resultList = connection.getClusterCommandExecutor().executeMultiKeyCommand( (LettuceMultiKeyClusterCommandCallback<KeyValue<byte[], byte[]>>) (client, key) -> client.brpop(timeout, key), Arrays.asList(keys)).resultsAsList(); for (KeyValue<byte[], byte[]> kv : resultList) { if (kv != null) { return LettuceConverters.toBytesList(kv); } } return Collections.emptyList(); }
@Override public List<byte[]> bLPop(int timeout, byte[]... keys) { Assert.notNull(keys, "Keys must not be null!"); Assert.noNullElements(keys, "Keys must not contain null elements!"); if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) { return super.bLPop(timeout, keys); } List<KeyValue<byte[], byte[]>> resultList = connection.getClusterCommandExecutor().executeMultiKeyCommand( (LettuceMultiKeyClusterCommandCallback<KeyValue<byte[], byte[]>>) (client, key) -> client.blpop(timeout, key), Arrays.asList(keys)).resultsAsList(); for (KeyValue<byte[], byte[]> kv : resultList) { if (kv != null) { return LettuceConverters.toBytesList(kv); } } return Collections.emptyList(); }
@Override public Set<byte[]> sDiff(byte[]... keys) { Assert.notNull(keys, "Keys must not be null!"); Assert.noNullElements(keys, "Keys must not contain null elements!"); if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) { return super.sDiff(keys); } byte[] source = keys[0]; byte[][] others = Arrays.copyOfRange(keys, 1, keys.length); ByteArraySet values = new ByteArraySet(sMembers(source)); Collection<Set<byte[]>> nodeResult = connection.getClusterCommandExecutor() .executeMultiKeyCommand((LettuceMultiKeyClusterCommandCallback<Set<byte[]>>) RedisSetCommands::smembers, Arrays.asList(others)) .resultsAsList(); if (values.isEmpty()) { return Collections.emptySet(); } for (Set<byte[]> toSubstract : nodeResult) { values.removeAll(toSubstract); } return values.asRawSet(); }
@Override public Set<byte[]> sUnion(byte[]... keys) { Assert.notNull(keys, "Keys must not be null!"); Assert.noNullElements(keys, "Keys must not contain null elements!"); if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) { return super.sUnion(keys); } Collection<Set<byte[]>> nodeResult = connection.getClusterCommandExecutor() .executeMultiKeyCommand((LettuceMultiKeyClusterCommandCallback<Set<byte[]>>) RedisSetCommands::smembers, Arrays.asList(keys)) .resultsAsList(); ByteArraySet result = new ByteArraySet(); for (Set<byte[]> entry : nodeResult) { result.addAll(entry); } if (result.isEmpty()) { return Collections.emptySet(); } return result.asRawSet(); }
@Override public Set<byte[]> sInter(byte[]... keys) { Assert.notNull(keys, "Keys must not be null!"); Assert.noNullElements(keys, "Keys must not contain null elements!"); if (ClusterSlotHashUtil.isSameSlotForAllKeys(keys)) { return super.sInter(keys); } Collection<Set<byte[]>> nodeResult = connection.getClusterCommandExecutor() .executeMultiKeyCommand((LettuceMultiKeyClusterCommandCallback<Set<byte[]>>) RedisSetCommands::smembers, Arrays.asList(keys)) .resultsAsList(); ByteArraySet result = null; for (Set<byte[]> entry : nodeResult) { ByteArraySet tmp = new ByteArraySet(entry); if (result == null) { result = tmp; } else { result.retainAll(tmp); if (result.isEmpty()) { break; } } } if (result == null || result.isEmpty()) { return Collections.emptySet(); } return result.asRawSet(); }
private <T> NodeResult<T> executeCommandOnSingleNode(LettuceClusterCommandCallback<T> command, RedisClusterNode node) { return connection.getClusterCommandExecutor().executeCommandOnSingleNode(command, node); }
@Override public Set<byte[]> keys(byte[] pattern) { Assert.notNull(pattern, "Pattern must not be null!"); Collection<List<byte[]>> keysPerNode = connection.getClusterCommandExecutor() .executeCommandOnAllNodes((LettuceClusterCommandCallback<List<byte[]>>) connection -> connection.keys(pattern)) .resultsAsList(); Set<byte[]> keys = new HashSet<>(); for (List<byte[]> keySet : keysPerNode) { keys.addAll(keySet); } return keys; }
@Override public Set<byte[]> keys(byte[] pattern) { Assert.notNull(pattern, "Pattern must not be null!"); Collection<List<byte[]>> keysPerNode = connection.getClusterCommandExecutor() .executeCommandOnAllNodes((LettuceClusterCommandCallback<List<byte[]>>) connection -> connection.keys(pattern)) .resultsAsList(); Set<byte[]> keys = new HashSet<>(); for (List<byte[]> keySet : keysPerNode) { keys.addAll(keySet); } return keys; }
@Nullable public byte[] randomKey(RedisClusterNode node) { return connection.getClusterCommandExecutor() .executeCommandOnSingleNode((LettuceClusterCommandCallback<byte[]>) client -> client.randomkey(), node) .getValue(); }
@Nullable public Set<byte[]> keys(RedisClusterNode node, byte[] pattern) { Assert.notNull(pattern, "Pattern must not be null!"); return LettuceConverters.toBytesSet(connection.getClusterCommandExecutor() .executeCommandOnSingleNode((LettuceClusterCommandCallback<List<byte[]>>) client -> client.keys(pattern), node) .getValue()); }
@Nullable public byte[] randomKey(RedisClusterNode node) { return connection.getClusterCommandExecutor() .executeCommandOnSingleNode((LettuceClusterCommandCallback<byte[]>) client -> client.randomkey(), node) .getValue(); }
@Override public Long time() { return convertListOfStringToTime(connection.getClusterCommandExecutor() .executeCommandOnArbitraryNode((LettuceClusterCommandCallback<List<byte[]>>) RedisServerCommands::time) .getValue()); }
@Override public Long time() { return convertListOfStringToTime(connection.getClusterCommandExecutor() .executeCommandOnArbitraryNode((LettuceClusterCommandCallback<List<byte[]>>) RedisServerCommands::time) .getValue()); }