/** * 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 Long zCount(byte[] key, Range range) { Assert.notNull(key, "Key must not be null!"); if (isPipelined() || isQueueing()) { throw new UnsupportedOperationException( "ZCOUNT not implemented in jedis for binary protocol on transaction and pipeline"); } // TODO: Implement zcount for pipeline/tx. byte[] min = JedisConverters.boundaryToBytesForZRange(range.getMin(), JedisConverters.NEGATIVE_INFINITY_BYTES); byte[] max = JedisConverters.boundaryToBytesForZRange(range.getMax(), JedisConverters.POSITIVE_INFINITY_BYTES); return connection.getJedis().zcount(key, min, max); }
@Override public Set<byte[]> zRangeByLex(byte[] key, Range range, @Nullable Limit limit) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(range, "Range for ZRANGEBYLEX must not be null!"); byte[] min = JedisConverters.boundaryToBytesForZRangeByLex(range.getMin(), JedisConverters.MINUS_BYTES); byte[] max = JedisConverters.boundaryToBytesForZRangeByLex(range.getMax(), JedisConverters.PLUS_BYTES); pipeline(connection.newJedisResult( connection.getRequiredPipeline().zrangeByLex(key, min, max, limit.getOffset(), limit.getCount()))); } else { pipeline(connection.newJedisResult(connection.getRequiredPipeline().zrangeByLex(key, min, max))); if (limit != null) { transaction(connection.newJedisResult( connection.getRequiredTransaction().zrangeByLex(key, min, max, limit.getOffset(), limit.getCount()))); } else { transaction(connection.newJedisResult(connection.getRequiredTransaction().zrangeByLex(key, min, max))); return connection.getJedis().zrangeByLex(key, min, max, limit.getOffset(), limit.getCount()); return connection.getJedis().zrangeByLex(key, min, max); } catch (Exception ex) { throw convertJedisAccessException(ex);
@Override public Boolean mSetNX(Map<byte[], byte[]> tuples) { Assert.notNull(tuples, "Tuples must not be null!"); try { if (isPipelined()) { pipeline( connection.newJedisResult(connection.getRequiredPipeline().msetnx(JedisConverters.toByteArrays(tuples)), JedisConverters.longToBoolean())); return null; } if (isQueueing()) { transaction( connection.newJedisResult(connection.getRequiredTransaction().msetnx(JedisConverters.toByteArrays(tuples)), JedisConverters.longToBoolean())); return null; } return JedisConverters.toBoolean(connection.getJedis().msetnx(JedisConverters.toByteArrays(tuples))); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public Distance geoDist(byte[] key, byte[] member1, byte[] member2, Metric metric) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(member1, "Member1 must not be null!"); Assert.notNull(member2, "Member2 must not be null!"); Assert.notNull(metric, "Metric must not be null!"); GeoUnit geoUnit = JedisConverters.toGeoUnit(metric); try { return JedisConverters.distanceConverterForMetric(metric) .convert(connection.getCluster().geodist(key, member1, member2, geoUnit)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public Distance geoDist(byte[] key, byte[] member1, byte[] member2, Metric metric) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(member1, "Member1 must not be null!"); Assert.notNull(member2, "Member2 must not be null!"); Assert.notNull(metric, "Metric must not be null!"); GeoUnit geoUnit = JedisConverters.toGeoUnit(metric); Converter<Double, Distance> distanceConverter = JedisConverters.distanceConverterForMetric(metric); try { if (isPipelined()) { pipeline(connection.newJedisResult(connection.getRequiredPipeline().geodist(key, member1, member2, geoUnit), distanceConverter)); return null; } if (isQueueing()) { transaction(connection.newJedisResult( connection.getRequiredTransaction().geodist(key, member1, member2, geoUnit), distanceConverter)); return null; } return distanceConverter.convert(connection.getJedis().geodist(key, member1, member2, geoUnit)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public GeoResults<GeoLocation<byte[]>> geoRadius(byte[] key, Circle within, GeoRadiusCommandArgs args) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(within, "Within must not be null!"); Assert.notNull(args, "Args must not be null!"); GeoRadiusParam geoRadiusParam = JedisConverters.toGeoRadiusParam(args); try { return JedisConverters.geoRadiusResponseToGeoResultsConverter(within.getRadius().getMetric()) .convert(connection.getCluster().georadius(key, within.getCenter().getX(), within.getCenter().getY(), within.getRadius().getValue(), JedisConverters.toGeoUnit(within.getRadius().getMetric()), geoRadiusParam)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public GeoResults<GeoLocation<byte[]>> geoRadiusByMember(byte[] key, byte[] member, Distance radius, GeoRadiusCommandArgs args) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(member, "Member must not be null!"); Assert.notNull(radius, "Radius must not be null!"); Assert.notNull(args, "Args must not be null!"); GeoUnit geoUnit = JedisConverters.toGeoUnit(radius.getMetric()); redis.clients.jedis.params.GeoRadiusParam geoRadiusParam = JedisConverters.toGeoRadiusParam(args); try { return JedisConverters.geoRadiusResponseToGeoResultsConverter(radius.getMetric()) .convert(connection.getCluster().georadiusByMember(key, member, radius.getValue(), geoUnit, geoRadiusParam)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public GeoResults<GeoLocation<byte[]>> geoRadiusByMember(byte[] key, byte[] member, Distance radius, GeoRadiusCommandArgs args) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(member, "Member must not be null!"); Assert.notNull(radius, "Radius must not be null!"); Assert.notNull(args, "Args must not be null!"); GeoUnit geoUnit = JedisConverters.toGeoUnit(radius.getMetric()); Converter<List<redis.clients.jedis.GeoRadiusResponse>, GeoResults<GeoLocation<byte[]>>> converter = JedisConverters .geoRadiusResponseToGeoResultsConverter(radius.getMetric()); redis.clients.jedis.params.GeoRadiusParam geoRadiusParam = JedisConverters.toGeoRadiusParam(args); try { if (isPipelined()) { pipeline(connection.newJedisResult( connection.getRequiredPipeline().georadiusByMember(key, member, radius.getValue(), geoUnit, geoRadiusParam), converter)); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().georadiusByMember(key, member, radius.getValue(), geoUnit, geoRadiusParam), converter)); return null; } return converter .convert(connection.getJedis().georadiusByMember(key, member, radius.getValue(), geoUnit, geoRadiusParam)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public List<Point> geoPos(byte[] key, byte[]... members) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(members, "Members must not be null!"); Assert.noNullElements(members, "Members must not contain null!"); ListConverter<GeoCoordinate, Point> converter = JedisConverters.geoCoordinateToPointConverter(); try { if (isPipelined()) { pipeline(connection.newJedisResult(connection.getRequiredPipeline().geopos(key, members), converter)); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().geopos(key, members), converter)); return null; } return converter.convert(connection.getJedis().geopos(key, members)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public List<String> geoHash(byte[] key, byte[]... members) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(members, "Members must not be null!"); Assert.noNullElements(members, "Members must not contain null!"); try { if (isPipelined()) { pipeline(connection.newJedisResult(connection.getRequiredPipeline().geohash(key, members), JedisConverters.bytesListToStringListConverter())); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().geohash(key, members), JedisConverters.bytesListToStringListConverter())); return null; } return JedisConverters.bytesListToStringListConverter().convert(connection.getJedis().geohash(key, members)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public List<Point> geoPos(byte[] key, byte[]... members) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(members, "Members must not be null!"); Assert.noNullElements(members, "Members must not contain null!"); try { return JedisConverters.geoCoordinateToPointConverter().convert(connection.getCluster().geopos(key, members)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public GeoResults<GeoLocation<byte[]>> geoRadius(byte[] key, Circle within, GeoRadiusCommandArgs args) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(within, "Within must not be null!"); Assert.notNull(args, "Args must not be null!"); redis.clients.jedis.params.GeoRadiusParam geoRadiusParam = JedisConverters.toGeoRadiusParam(args); Converter<List<redis.clients.jedis.GeoRadiusResponse>, GeoResults<GeoLocation<byte[]>>> converter = JedisConverters .geoRadiusResponseToGeoResultsConverter(within.getRadius().getMetric()); try { if (isPipelined()) { pipeline(connection.newJedisResult(connection.getRequiredPipeline().georadius(key, within.getCenter().getX(), within.getCenter().getY(), within.getRadius().getValue(), JedisConverters.toGeoUnit(within.getRadius().getMetric()), geoRadiusParam), converter)); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().georadius(key, within.getCenter().getX(), within.getCenter().getY(), within.getRadius().getValue(), JedisConverters.toGeoUnit(within.getRadius().getMetric()), geoRadiusParam), converter)); return null; } return converter.convert(connection.getJedis().georadius(key, within.getCenter().getX(), within.getCenter().getY(), within.getRadius().getValue(), JedisConverters.toGeoUnit(within.getRadius().getMetric()), geoRadiusParam)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public Long zCount(byte[] key, Range range) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(range, "Range cannot be null for ZCOUNT."); byte[] min = JedisConverters.boundaryToBytesForZRange(range.getMin(), JedisConverters.NEGATIVE_INFINITY_BYTES); byte[] max = JedisConverters.boundaryToBytesForZRange(range.getMax(), JedisConverters.POSITIVE_INFINITY_BYTES); try { return connection.getCluster().zcount(key, min, max); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public Long zRemRangeByScore(byte[] key, Range range) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(range, "Range cannot be null for ZREMRANGEBYSCORE."); byte[] min = JedisConverters.boundaryToBytesForZRange(range.getMin(), JedisConverters.NEGATIVE_INFINITY_BYTES); byte[] max = JedisConverters.boundaryToBytesForZRange(range.getMax(), JedisConverters.POSITIVE_INFINITY_BYTES); try { return connection.getCluster().zremrangeByScore(key, min, max); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@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 public Boolean setNX(byte[] key, byte[] value) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(value, "Value must not be null!"); try { if (isPipelined()) { pipeline(connection.newJedisResult(connection.getRequiredPipeline().setnx(key, value), JedisConverters.longToBoolean())); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().setnx(key, value), JedisConverters.longToBoolean())); return null; } return JedisConverters.toBoolean(connection.getJedis().setnx(key, value)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public Boolean hSet(byte[] key, byte[] field, byte[] value) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(field, "Field must not be null!"); Assert.notNull(value, "Value must not be null!"); try { if (isPipelined()) { pipeline(connection.newJedisResult(connection.getRequiredPipeline().hset(key, field, value), JedisConverters.longToBoolean())); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().hset(key, field, value), JedisConverters.longToBoolean())); return null; } return JedisConverters.toBoolean(connection.getJedis().hset(key, field, value)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public Boolean expire(byte[] key, long seconds) { Assert.notNull(key, "Key must not be null!"); if (seconds > Integer.MAX_VALUE) { return pExpire(key, TimeUnit.SECONDS.toMillis(seconds)); } try { if (isPipelined()) { pipeline(connection.newJedisResult(connection.getRequiredPipeline().expire(key, (int) seconds), JedisConverters.longToBoolean())); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().expire(key, (int) seconds), JedisConverters.longToBoolean())); return null; } return JedisConverters.toBoolean(connection.getJedis().expire(key, (int) seconds)); } catch (Exception ex) { throw connection.convertJedisAccessException(ex); } }
@Override public Boolean pExpire(byte[] key, long millis) { Assert.notNull(key, "Key must not be null!"); try { if (isPipelined()) { pipeline(connection.newJedisResult(connection.getRequiredPipeline().pexpire(key, millis), JedisConverters.longToBoolean())); return null; } if (isQueueing()) { transaction(connection.newJedisResult(connection.getRequiredTransaction().pexpire(key, millis), JedisConverters.longToBoolean())); return null; } return JedisConverters.toBoolean(connection.getJedis().pexpire(key, millis)); } catch (Exception ex) { throw connection.convertJedisAccessException(ex); } }