/** * @param source * @return * @since 1.6 */ public static byte[] toBytes(Double source) { return toBytes(String.valueOf(source)); }
@Override public <T> T evalSha(String scriptSha1, ReturnType returnType, int numKeys, byte[]... keysAndArgs) { return evalSha(JedisConverters.toBytes(scriptSha1), returnType, numKeys, keysAndArgs); }
/** * Converts a given {@link Boundary} to its binary representation suitable for ZRANGEBYLEX command. * * @param boundary * @return * @since 1.6 */ public static byte[] boundaryToBytesForZRangeByLex(@Nullable Boundary boundary, byte[] defaultValue) { if (boundary == null || boundary.getValue() == null) { return defaultValue; } return boundaryToBytes(boundary, toBytes("["), toBytes("(")); }
@Override public Set<byte[]> zRangeByScore(byte[] key, String min, String max) { Assert.notNull(key, "Key must not be null!"); try { return connection.getCluster().zrangeByScore(key, JedisConverters.toBytes(min), JedisConverters.toBytes(max)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public Set<byte[]> zRangeByScore(byte[] key, String min, String max, long offset, long count) { Assert.notNull(key, "Key must not be null!"); if (offset > Integer.MAX_VALUE || count > Integer.MAX_VALUE) { throw new IllegalArgumentException("Count/Offset cannot exceed Integer.MAX_VALUE!"); } try { return connection.getCluster().zrangeByScore(key, JedisConverters.toBytes(min), JedisConverters.toBytes(max), Long.valueOf(offset).intValue(), Long.valueOf(count).intValue()); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
/** * Converts a given {@link Boundary} to its binary representation suitable for {@literal ZRANGEBY*} commands, despite * {@literal ZRANGEBYLEX}. * * @param boundary * @param defaultValue * @return * @since 1.6 */ public static byte[] boundaryToBytesForZRange(@Nullable Boundary boundary, byte[] defaultValue) { if (boundary == null || boundary.getValue() == null) { return defaultValue; } return boundaryToBytes(boundary, new byte[] {}, toBytes("(")); }
@Override public void restore(byte[] key, long ttlInMillis, byte[] serializedValue, boolean replace) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(serializedValue, "Serialized value must not be null!"); if (ttlInMillis > Integer.MAX_VALUE) { throw new UnsupportedOperationException("Jedis does not support ttlInMillis exceeding Integer.MAX_VALUE."); } connection.getClusterCommandExecutor().executeCommandOnSingleNode((JedisClusterCommandCallback<String>) client -> { if (!replace) { return client.restore(key, Long.valueOf(ttlInMillis).intValue(), serializedValue); } return JedisConverters.toString(this.connection.execute("RESTORE", key, Arrays.asList(JedisConverters.toBytes(ttlInMillis), serializedValue, JedisConverters.toBytes ("REPLACE")))); }, connection.clusterGetNodeForKey(key)); }
@Override public List<byte[]> convert(BitFieldSubCommands source) { if (source == null) { return Collections.emptyList(); } List<byte[]> args = new ArrayList<byte[]>(source.getSubCommands().size() * 4); for (BitFieldSubCommand command : source.getSubCommands()) { if (command instanceof BitFieldIncrBy) { BitFieldIncrBy.Overflow overflow = ((BitFieldIncrBy) command).getOverflow(); if (overflow != null) { args.add(JedisConverters.toBytes("OVERFLOW")); args.add(JedisConverters.toBytes(overflow.name())); } } args.add(JedisConverters.toBytes(command.getCommand())); args.add(JedisConverters.toBytes(command.getType().asString())); args.add(JedisConverters.toBytes(command.getOffset().asString())); if (command instanceof BitFieldSet) { args.add(JedisConverters.toBytes(((BitFieldSet) command).getValue())); } else if (command instanceof BitFieldIncrBy) { args.add(JedisConverters.toBytes(((BitFieldIncrBy) command).getValue())); } } return args; } };
private static byte[] boundaryToBytes(Boundary boundary, byte[] inclPrefix, byte[] exclPrefix) { byte[] prefix = boundary.isIncluding() ? inclPrefix : exclPrefix; byte[] value = null; if (boundary.getValue() instanceof byte[]) { value = (byte[]) boundary.getValue(); } else if (boundary.getValue() instanceof Double) { value = toBytes((Double) boundary.getValue()); } else if (boundary.getValue() instanceof Long) { value = toBytes((Long) boundary.getValue()); } else if (boundary.getValue() instanceof Integer) { value = toBytes((Integer) boundary.getValue()); } else if (boundary.getValue() instanceof String) { value = toBytes((String) boundary.getValue()); } else { throw new IllegalArgumentException(String.format("Cannot convert %s to binary format", boundary.getValue())); } ByteBuffer buffer = ByteBuffer.allocate(prefix.length + value.length); buffer.put(prefix); buffer.put(value); return buffer.array(); }
@Override protected ScanIteration<Entry<byte[], byte[]>> doScan(byte[] key, long cursorId, ScanOptions options) { if (isQueueing() || isPipelined()) { throw new UnsupportedOperationException("'HSCAN' cannot be called in pipeline / transaction mode."); } ScanParams params = JedisConverters.toScanParams(options); ScanResult<Entry<byte[], byte[]>> result = connection.getJedis().hscan(key, JedisConverters.toBytes(cursorId), params); return new ScanIteration<>(Long.valueOf(result.getCursor()), result.getResult()); }
@Override protected ScanIteration<byte[]> doScan(byte[] key, long cursorId, ScanOptions options) { if (isQueueing() || isPipelined()) { throw new UnsupportedOperationException("'SSCAN' cannot be called in pipeline / transaction mode."); } ScanParams params = JedisConverters.toScanParams(options); redis.clients.jedis.ScanResult<byte[]> result = connection.getJedis().sscan(key, JedisConverters.toBytes(cursorId), params); return new ScanIteration<>(Long.valueOf(result.getCursor()), result.getResult()); }
@Override @SuppressWarnings("unchecked") public <T> T eval(byte[] script, ReturnType returnType, int numKeys, byte[]... keysAndArgs) { Assert.notNull(script, "Script must not be null!"); if (isQueueing() || isPipelined()) { throw new UnsupportedOperationException(); } try { return (T) new JedisScriptReturnConverter(returnType) .convert(connection.getJedis().eval(script, JedisConverters.toBytes(numKeys), keysAndArgs)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override protected ScanIteration<Entry<byte[], byte[]>> doScan(long cursorId, ScanOptions options) { ScanParams params = JedisConverters.toScanParams(options); redis.clients.jedis.ScanResult<Map.Entry<byte[], byte[]>> result = connection.getCluster().hscan(key, JedisConverters.toBytes(cursorId), params); return new ScanIteration<>(Long.valueOf(result.getCursor()), result.getResult()); } }.open();
@Override protected ScanIteration<byte[]> doScan(long cursorId, ScanOptions options) { ScanParams params = JedisConverters.toScanParams(options); redis.clients.jedis.ScanResult<byte[]> result = connection.getCluster().sscan(key, JedisConverters.toBytes(cursorId), params); return new ScanIteration<>(Long.valueOf(result.getCursor()), result.getResult()); } }.open();
@Override protected ScanIteration<Tuple> doScan(byte[] key, long cursorId, ScanOptions options) { if (isQueueing() || isPipelined()) { throw new UnsupportedOperationException("'ZSCAN' cannot be called in pipeline / transaction mode."); } ScanParams params = JedisConverters.toScanParams(options); ScanResult<redis.clients.jedis.Tuple> result = connection.getJedis().zscan(key, JedisConverters.toBytes(cursorId), params); return new ScanIteration<>(Long.valueOf(result.getCursor()), JedisConverters.tuplesToTuples().convert(result.getResult())); }
@Override public Set<byte[]> zRangeByLex(byte[] key, Range range, Limit limit) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(range, "Range cannot be null for ZRANGEBYLEX."); Assert.notNull(limit, "Limit must not be null!"); byte[] min = JedisConverters.boundaryToBytesForZRangeByLex(range.getMin(), JedisConverters.toBytes("-")); byte[] max = JedisConverters.boundaryToBytesForZRangeByLex(range.getMax(), JedisConverters.toBytes("+")); try { if (limit.isUnlimited()) { return connection.getCluster().zrangeByLex(key, min, max); } return connection.getCluster().zrangeByLex(key, min, max, limit.getOffset(), limit.getCount()); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override protected ScanIteration<Tuple> doScan(long cursorId, ScanOptions options) { ScanParams params = JedisConverters.toScanParams(options); redis.clients.jedis.ScanResult<redis.clients.jedis.Tuple> result = connection.getCluster().zscan(key, JedisConverters.toBytes(cursorId), params); return new ScanIteration<>(Long.valueOf(result.getCursor()), JedisConverters.tuplesToTuples().convert(result.getResult())); } }.open();
@Override public void restore(byte[] key, long ttlInMillis, byte[] serializedValue, boolean replace) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(serializedValue, "Serialized value must not be null!"); if (replace) { this.connection.execute("RESTORE", new byte[][] { key, JedisConverters.toBytes(ttlInMillis), serializedValue, JedisConverters.toBytes("REPLACE") }); return; } if (ttlInMillis > Integer.MAX_VALUE) { throw new IllegalArgumentException("TtlInMillis must be less than Integer.MAX_VALUE for restore in Jedis."); } try { if (isPipelined()) { pipeline(connection .newStatusResult(connection.getRequiredPipeline().restore(key, (int) ttlInMillis, serializedValue))); return; } if (isQueueing()) { transaction(connection .newStatusResult(connection.getRequiredTransaction().restore(key, (int) ttlInMillis, serializedValue))); return; } connection.getJedis().restore(key, (int) ttlInMillis, serializedValue); } catch (Exception ex) { throw connection.convertJedisAccessException(ex); } }
/** * @param source * @return * @since 1.6 */ public static byte[] toBytes(Double source) { return toBytes(String.valueOf(source)); }
@Override protected ScanIteration<Entry<byte[], byte[]>> doScan(long cursorId, ScanOptions options) { ScanParams params = JedisConverters.toScanParams(options); redis.clients.jedis.ScanResult<Map.Entry<byte[], byte[]>> result = connection.getCluster().hscan(key, JedisConverters.toBytes(cursorId), params); return new ScanIteration<>(Long.valueOf(result.getStringCursor()), result.getResult()); } }.open();