diff --git a/redisson-spring-data/redisson-spring-data-26/src/main/java/org/redisson/spring/data/connection/RedissonConnection.java b/redisson-spring-data/redisson-spring-data-26/src/main/java/org/redisson/spring/data/connection/RedissonConnection.java index 1f0f43eb2..800c3616b 100644 --- a/redisson-spring-data/redisson-spring-data-26/src/main/java/org/redisson/spring/data/connection/RedissonConnection.java +++ b/redisson-spring-data/redisson-spring-data-26/src/main/java/org/redisson/spring/data/connection/RedissonConnection.java @@ -2425,4 +2425,331 @@ public class RedissonConnection extends AbstractRedisConnection { restore(key, ttlInMillis, serializedValue); } + @Override + public byte[] zRandMember(byte[] key) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, RedisCommands.ZRANDMEMBER_SINGLE, (Object) key); + } + + private static final RedisCommand> ZRANDMEMBER_LIST = new RedisCommand<>("ZRANDMEMBER", new ObjectListReplayDecoder<>()); + + @Override + public List zRandMember(byte[] key, long count) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, ZRANDMEMBER_LIST, key, count); + } + + private static final RedisCommand ZRANDMEMBER_SCORE = new RedisCommand<>("ZRANDMEMBER", new ScoredSortedSingleReplayDecoder()); + + @Override + public Tuple zRandMemberWithScore(byte[] key) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, ZRANDMEMBER_SCORE, key, "WITHSCORES"); + } + + private static final RedisCommand> ZRANDMEMBER_SCORE_LIST = new RedisCommand<>("ZRANDMEMBER", new ScoredSortedListReplayDecoder()); + + @Override + public List zRandMemberWithScore(byte[] key, long count) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, ZRANDMEMBER_SCORE_LIST, key, count, "WITHSCORES"); + } + + private static final RedisCommand ZPOPMIN = new RedisCommand<>("ZPOPMIN", new ScoredSortedSingleReplayDecoder()); + + @Override + public Tuple zPopMin(byte[] key) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, ZPOPMIN, (Object) key); + } + + @Override + public Set zPopMin(byte[] key, long count) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, ZPOPMIN, key, count); + } + + private static final RedisCommand BZPOPMIN = new RedisCommand<>("BZPOPMIN", new ScoredSortedSingleReplayDecoder()); + + @Override + public Tuple bZPopMin(byte[] key, long timeout, TimeUnit unit) { + Assert.notNull(key, "Key must not be null!"); + + long seconds = unit.toSeconds(timeout); + return write(key, ByteArrayCodec.INSTANCE, BZPOPMIN , key, seconds); + } + + private static final RedisCommand ZPOPMAX = new RedisCommand<>("ZPOPMAX", new ScoredSortedSingleReplayDecoder()); + + @Override + public Tuple zPopMax(byte[] key) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, ZPOPMAX, (Object) key); + } + + @Override + public Set zPopMax(byte[] key, long count) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, ZPOPMAX, key, count); + } + + private static final RedisCommand BZPOPMAX = new RedisCommand<>("BZPOPMAX", new ScoredSortedSingleReplayDecoder()); + + @Override + public Tuple bZPopMax(byte[] key, long timeout, TimeUnit unit) { + Assert.notNull(key, "Key must not be null!"); + + long seconds = unit.toSeconds(timeout); + return write(key, ByteArrayCodec.INSTANCE, BZPOPMAX , key, seconds); + } + + private static final RedisCommand> ZMSCORE = new RedisCommand<>("ZMSCORE", new ObjectListReplayDecoder<>()); + + @Override + public List zMScore(byte[] key, byte[]... values) { + Assert.notNull(key, "Key must not be null!"); + + List args = new ArrayList<>(values.length + 1); + args.add(key); + args.addAll(Arrays.asList(values)); + + return write(key, DoubleCodec.INSTANCE, ZMSCORE, args.toArray()); + } + + private static final RedisCommand> ZDIFF = new RedisCommand<>("ZDIFF", new ObjectSetReplayDecoder()); + + @Override + public Set zDiff(byte[]... sets) { + List args = new ArrayList<>(sets.length + 1); + args.add(sets.length); + args.addAll(Arrays.asList(sets)); + + return write(sets[0], ByteArrayCodec.INSTANCE, ZDIFF, args.toArray()); + } + + private static final RedisCommand> ZDIFF_SCORE = new RedisCommand<>("ZDIFF", new ScoredSortedSetReplayDecoder()); + + @Override + public Set zDiffWithScores(byte[]... sets) { + List args = new ArrayList<>(sets.length + 1); + args.add(sets.length); + args.addAll(Arrays.asList(sets)); + args.add("WITHSCORES"); + + return write(sets[0], ByteArrayCodec.INSTANCE, ZDIFF_SCORE, args.toArray()); + } + + private static final RedisStrictCommand ZDIFFSTORE = new RedisStrictCommand<>("ZDIFFSTORE"); + + @Override + public Long zDiffStore(byte[] destKey, byte[]... sets) { + Assert.notNull(destKey, "Key must not be null!"); + + List args = new ArrayList<>(sets.length + 2); + args.add(destKey); + args.add(sets.length); + args.addAll(Arrays.asList(sets)); + + return write(destKey, LongCodec.INSTANCE, ZDIFFSTORE, args.toArray()); + } + + private static final RedisCommand> ZINTER = new RedisCommand<>("ZINTER", new ObjectSetReplayDecoder<>()); + + @Override + public Set zInter(byte[]... sets) { + List args = new ArrayList<>(sets.length + 1); + args.add(sets.length); + args.addAll(Arrays.asList(sets)); + + return write(sets[0], ByteArrayCodec.INSTANCE, ZINTER, args.toArray()); + } + + private static final RedisCommand> ZINTER_SCORE = new RedisCommand<>("ZINTER", new ScoredSortedSetReplayDecoder()); + + @Override + public Set zInterWithScores(Aggregate aggregate, Weights weights, byte[]... sets) { + List args = new ArrayList<>(sets.length * 2 + 6); + args.add(sets.length); + args.addAll(Arrays.asList(sets)); + if (weights != null) { + args.add("WEIGHTS"); + for (double weight : weights.toArray()) { + args.add(BigDecimal.valueOf(weight).toPlainString()); + } + } + if (aggregate != null) { + args.add("AGGREGATE"); + args.add(aggregate.name()); + } + args.add("WITHSCORES"); + + return write(sets[0], ByteArrayCodec.INSTANCE, ZINTER_SCORE, args.toArray()); + } + + @Override + public Set zInterWithScores(byte[]... sets) { + return zInterWithScores(null, (Weights) null, sets); + } + + private static final RedisCommand> ZUNION = new RedisCommand<>("ZUNION", new ObjectSetReplayDecoder<>()); + + @Override + public Set zUnion(byte[]... sets) { + List args = new ArrayList<>(sets.length + 1); + args.add(sets.length); + args.addAll(Arrays.asList(sets)); + + return write(sets[0], ByteArrayCodec.INSTANCE, ZUNION, args.toArray()); + } + + private static final RedisCommand> ZUNION_SCORE = new RedisCommand<>("ZUNION", new ScoredSortedSetReplayDecoder()); + + @Override + public Set zUnionWithScores(Aggregate aggregate, Weights weights, byte[]... sets) { + List args = new ArrayList<>(sets.length * 2 + 6); + args.add(sets.length); + args.addAll(Arrays.asList(sets)); + if (weights != null) { + args.add("WEIGHTS"); + for (double weight : weights.toArray()) { + args.add(BigDecimal.valueOf(weight).toPlainString()); + } + } + if (aggregate != null) { + args.add("AGGREGATE"); + args.add(aggregate.name()); + } + args.add("WITHSCORES"); + + return write(sets[0], ByteArrayCodec.INSTANCE, ZUNION_SCORE, args.toArray()); + } + + @Override + public Set zUnionWithScores(byte[]... sets) { + return zUnionWithScores(null, (Weights) null, sets); + } + + private static final RedisCommand HRANDFIELD = new RedisCommand("HRANDFIELD"); + + @Override + public byte[] hRandField(byte[] key) { + Assert.notNull(key, "Key must not be null!"); + + return read(key, ByteArrayCodec.INSTANCE, HRANDFIELD, (Object) key); + } + + @Override + public Entry hRandFieldWithValues(byte[] key) { + Assert.notNull(key, "Key must not be null!"); + + return read(key, ByteArrayCodec.INSTANCE, RedisCommands.HRANDFIELD, (Object) key); + } + + private static final RedisCommand> HRANDFIELD_LIST = new RedisCommand<>("HRANDFIELD", new ObjectListReplayDecoder<>()); + + @Override + public List hRandField(byte[] key, long count) { + Assert.notNull(key, "Key must not be null!"); + + return read(key, ByteArrayCodec.INSTANCE, HRANDFIELD_LIST, key, count); + } + + @Override + public List> hRandFieldWithValues(byte[] key, long count) { + Assert.notNull(key, "Key must not be null!"); + + return read(key, ByteArrayCodec.INSTANCE, RedisCommands.HRANDFIELD, (Object) key, count); + } + + @Override + public Boolean copy(byte[] sourceKey, byte[] targetKey, boolean replace) { + Assert.notNull(sourceKey, "Key must not be null!"); + Assert.notNull(targetKey, "Target must not be null!"); + + List params = new ArrayList<>(); + params.add(sourceKey); + params.add(targetKey); + if (replace) { + params.add("REPLACE"); + } + + return write(sourceKey, StringCodec.INSTANCE, RedisCommands.COPY, params.toArray()); + } + + @Override + public byte[] lMove(byte[] sourceKey, byte[] destinationKey, Direction from, Direction to) { + Assert.notNull(sourceKey, "Key must not be null!"); + Assert.notNull(destinationKey, "Destination key must not be null!"); + Assert.notNull(from, "From must not be null!"); + Assert.notNull(from, "To must not be null!"); + + return write(sourceKey, ByteArrayCodec.INSTANCE, RedisCommands.LMOVE, + sourceKey, destinationKey, from, to); + } + + @Override + public byte[] bLMove(byte[] sourceKey, byte[] destinationKey, Direction from, Direction to, double timeout) { + Assert.notNull(sourceKey, "Key must not be null!"); + Assert.notNull(destinationKey, "Destination key must not be null!"); + Assert.notNull(from, "From must not be null!"); + Assert.notNull(to, "To must not be null!"); + Assert.notNull(timeout, "Timeout must not be null!"); + + return write(sourceKey, ByteArrayCodec.INSTANCE, RedisCommands.BLMOVE, + sourceKey, destinationKey, from, to, destinationKey); + } + + @Override + public List lPop(byte[] key, long count) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, RedisCommands.LPOP_LIST, key, count); + } + + @Override + public List rPop(byte[] key, long count) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, RedisCommands.RPOP_LIST, key, count); + } + + private static final RedisCommand> SMISMEMBER = new RedisCommand<>("SMISMEMBER", new ObjectListReplayDecoder<>()); + + @Override + public List sMIsMember(byte[] key, byte[]... value) { + Assert.notNull(key, "Key must not be null!"); + + List args = new ArrayList<>(); + args.add(key); + args.addAll(Arrays.asList(value)); + + return read(key, StringCodec.INSTANCE, SMISMEMBER, args.toArray()); + } + + private static final RedisCommand GETEX = new RedisCommand<>("GETEX"); + + @Override + public byte[] getEx(byte[] key, Expiration expiration) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, GETEX, key, + "PX", expiration.getExpirationTimeInMilliseconds()); + } + + private static final RedisCommand GETDEL = new RedisCommand<>("GETDEL"); + + @Override + public byte[] getDel(byte[] key) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, GETDEL, key); + } } diff --git a/redisson-spring-data/redisson-spring-data-26/src/main/java/org/redisson/spring/data/connection/ScoredSortedSingleReplayDecoder.java b/redisson-spring-data/redisson-spring-data-26/src/main/java/org/redisson/spring/data/connection/ScoredSortedSingleReplayDecoder.java new file mode 100644 index 000000000..fd3c83dca --- /dev/null +++ b/redisson-spring-data/redisson-spring-data-26/src/main/java/org/redisson/spring/data/connection/ScoredSortedSingleReplayDecoder.java @@ -0,0 +1,49 @@ +/** + * Copyright (c) 2013-2022 Nikita Koksharov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.redisson.spring.data.connection; + +import org.redisson.client.codec.Codec; +import org.redisson.client.codec.DoubleCodec; +import org.redisson.client.handler.State; +import org.redisson.client.protocol.Decoder; +import org.redisson.client.protocol.decoder.MultiDecoder; +import org.springframework.data.redis.connection.DefaultTuple; +import org.springframework.data.redis.connection.RedisZSetCommands.Tuple; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author Nikita Koksharov + * + */ +public class ScoredSortedSingleReplayDecoder implements MultiDecoder { + + @Override + public Decoder getDecoder(Codec codec, int paramNum, State state) { + if (paramNum % 2 != 0) { + return DoubleCodec.INSTANCE.getValueDecoder(); + } + return MultiDecoder.super.getDecoder(codec, paramNum, state); + } + + @Override + public Tuple decode(List parts, State state) { + return new DefaultTuple((byte[])parts.get(0), ((Number)parts.get(1)).doubleValue()); + } + +} diff --git a/redisson-spring-data/redisson-spring-data-26/src/test/java/org/redisson/spring/data/connection/RedissonConnectionTest.java b/redisson-spring-data/redisson-spring-data-26/src/test/java/org/redisson/spring/data/connection/RedissonConnectionTest.java index ebdddbdfc..572cb5919 100644 --- a/redisson-spring-data/redisson-spring-data-26/src/test/java/org/redisson/spring/data/connection/RedissonConnectionTest.java +++ b/redisson-spring-data/redisson-spring-data-26/src/test/java/org/redisson/spring/data/connection/RedissonConnectionTest.java @@ -123,5 +123,14 @@ public class RedissonConnectionTest extends BaseConnectionTest { assertThat(t.next().getValue()).isEqualTo("value2".getBytes()); } + @Test + public void testZPopMin() { + connection.zAdd("key".getBytes(), 1, "value1".getBytes()); + connection.zAdd("key".getBytes(), 2, "value2".getBytes()); + + RedisZSetCommands.Tuple r = connection.zPopMin("key".getBytes()); + assertThat(r.getValue()).isEqualTo("value1".getBytes()); + assertThat(r.getScore()).isEqualTo(1); + } } diff --git a/redisson-spring-data/redisson-spring-data-27/src/main/java/org/redisson/spring/data/connection/RedissonConnection.java b/redisson-spring-data/redisson-spring-data-27/src/main/java/org/redisson/spring/data/connection/RedissonConnection.java index 62af5a6b5..4ccee7448 100644 --- a/redisson-spring-data/redisson-spring-data-27/src/main/java/org/redisson/spring/data/connection/RedissonConnection.java +++ b/redisson-spring-data/redisson-spring-data-27/src/main/java/org/redisson/spring/data/connection/RedissonConnection.java @@ -2424,4 +2424,332 @@ public class RedissonConnection extends AbstractRedisConnection { restore(key, ttlInMillis, serializedValue); } + @Override + public byte[] zRandMember(byte[] key) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, RedisCommands.ZRANDMEMBER_SINGLE, (Object) key); + } + + private static final RedisCommand> ZRANDMEMBER_LIST = new RedisCommand<>("ZRANDMEMBER", new ObjectListReplayDecoder<>()); + + @Override + public List zRandMember(byte[] key, long count) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, ZRANDMEMBER_LIST, key, count); + } + + private static final RedisCommand ZRANDMEMBER_SCORE = new RedisCommand<>("ZRANDMEMBER", new ScoredSortedSingleReplayDecoder()); + + @Override + public Tuple zRandMemberWithScore(byte[] key) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, ZRANDMEMBER_SCORE, key, "WITHSCORES"); + } + + private static final RedisCommand> ZRANDMEMBER_SCORE_LIST = new RedisCommand<>("ZRANDMEMBER", new ScoredSortedListReplayDecoder()); + + @Override + public List zRandMemberWithScore(byte[] key, long count) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, ZRANDMEMBER_SCORE_LIST, key, count, "WITHSCORES"); + } + + private static final RedisCommand ZPOPMIN = new RedisCommand<>("ZPOPMIN", new ScoredSortedSingleReplayDecoder()); + + @Override + public Tuple zPopMin(byte[] key) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, ZPOPMIN, (Object) key); + } + + @Override + public Set zPopMin(byte[] key, long count) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, ZPOPMIN, key, count); + } + + private static final RedisCommand BZPOPMIN = new RedisCommand<>("BZPOPMIN", new ScoredSortedSingleReplayDecoder()); + + @Override + public Tuple bZPopMin(byte[] key, long timeout, TimeUnit unit) { + Assert.notNull(key, "Key must not be null!"); + + long seconds = unit.toSeconds(timeout); + return write(key, ByteArrayCodec.INSTANCE, BZPOPMIN , key, seconds); + } + + private static final RedisCommand ZPOPMAX = new RedisCommand<>("ZPOPMAX", new ScoredSortedSingleReplayDecoder()); + + @Override + public Tuple zPopMax(byte[] key) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, ZPOPMAX, (Object) key); + } + + @Override + public Set zPopMax(byte[] key, long count) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, ZPOPMAX, key, count); + } + + private static final RedisCommand BZPOPMAX = new RedisCommand<>("BZPOPMAX", new ScoredSortedSingleReplayDecoder()); + + @Override + public Tuple bZPopMax(byte[] key, long timeout, TimeUnit unit) { + Assert.notNull(key, "Key must not be null!"); + + long seconds = unit.toSeconds(timeout); + return write(key, ByteArrayCodec.INSTANCE, BZPOPMAX , key, seconds); + } + + private static final RedisCommand> ZMSCORE = new RedisCommand<>("ZMSCORE", new ObjectListReplayDecoder<>()); + + @Override + public List zMScore(byte[] key, byte[]... values) { + Assert.notNull(key, "Key must not be null!"); + + List args = new ArrayList<>(values.length + 1); + args.add(key); + args.addAll(Arrays.asList(values)); + + return write(key, DoubleCodec.INSTANCE, ZMSCORE, args.toArray()); + } + + private static final RedisCommand> ZDIFF = new RedisCommand<>("ZDIFF", new ObjectSetReplayDecoder()); + + @Override + public Set zDiff(byte[]... sets) { + List args = new ArrayList<>(sets.length + 1); + args.add(sets.length); + args.addAll(Arrays.asList(sets)); + + return write(sets[0], ByteArrayCodec.INSTANCE, ZDIFF, args.toArray()); + } + + private static final RedisCommand> ZDIFF_SCORE = new RedisCommand<>("ZDIFF", new ScoredSortedSetReplayDecoder()); + + @Override + public Set zDiffWithScores(byte[]... sets) { + List args = new ArrayList<>(sets.length + 1); + args.add(sets.length); + args.addAll(Arrays.asList(sets)); + args.add("WITHSCORES"); + + return write(sets[0], ByteArrayCodec.INSTANCE, ZDIFF_SCORE, args.toArray()); + } + + private static final RedisStrictCommand ZDIFFSTORE = new RedisStrictCommand<>("ZDIFFSTORE"); + + @Override + public Long zDiffStore(byte[] destKey, byte[]... sets) { + Assert.notNull(destKey, "Key must not be null!"); + + List args = new ArrayList<>(sets.length + 2); + args.add(destKey); + args.add(sets.length); + args.addAll(Arrays.asList(sets)); + + return write(destKey, LongCodec.INSTANCE, ZDIFFSTORE, args.toArray()); + } + + private static final RedisCommand> ZINTER = new RedisCommand<>("ZINTER", new ObjectSetReplayDecoder<>()); + + @Override + public Set zInter(byte[]... sets) { + List args = new ArrayList<>(sets.length + 1); + args.add(sets.length); + args.addAll(Arrays.asList(sets)); + + return write(sets[0], ByteArrayCodec.INSTANCE, ZINTER, args.toArray()); + } + + private static final RedisCommand> ZINTER_SCORE = new RedisCommand<>("ZINTER", new ScoredSortedSetReplayDecoder()); + + @Override + public Set zInterWithScores(Aggregate aggregate, Weights weights, byte[]... sets) { + List args = new ArrayList<>(sets.length * 2 + 6); + args.add(sets.length); + args.addAll(Arrays.asList(sets)); + if (weights != null) { + args.add("WEIGHTS"); + for (double weight : weights.toArray()) { + args.add(BigDecimal.valueOf(weight).toPlainString()); + } + } + if (aggregate != null) { + args.add("AGGREGATE"); + args.add(aggregate.name()); + } + args.add("WITHSCORES"); + + return write(sets[0], ByteArrayCodec.INSTANCE, ZINTER_SCORE, args.toArray()); + } + + @Override + public Set zInterWithScores(byte[]... sets) { + return zInterWithScores(null, (Weights) null, sets); + } + + private static final RedisCommand> ZUNION = new RedisCommand<>("ZUNION", new ObjectSetReplayDecoder<>()); + + @Override + public Set zUnion(byte[]... sets) { + List args = new ArrayList<>(sets.length + 1); + args.add(sets.length); + args.addAll(Arrays.asList(sets)); + + return write(sets[0], ByteArrayCodec.INSTANCE, ZUNION, args.toArray()); + } + + private static final RedisCommand> ZUNION_SCORE = new RedisCommand<>("ZUNION", new ScoredSortedSetReplayDecoder()); + + @Override + public Set zUnionWithScores(Aggregate aggregate, Weights weights, byte[]... sets) { + List args = new ArrayList<>(sets.length * 2 + 6); + args.add(sets.length); + args.addAll(Arrays.asList(sets)); + if (weights != null) { + args.add("WEIGHTS"); + for (double weight : weights.toArray()) { + args.add(BigDecimal.valueOf(weight).toPlainString()); + } + } + if (aggregate != null) { + args.add("AGGREGATE"); + args.add(aggregate.name()); + } + args.add("WITHSCORES"); + + return write(sets[0], ByteArrayCodec.INSTANCE, ZUNION_SCORE, args.toArray()); + } + + @Override + public Set zUnionWithScores(byte[]... sets) { + return zUnionWithScores(null, (Weights) null, sets); + } + + private static final RedisCommand HRANDFIELD = new RedisCommand("HRANDFIELD"); + + @Override + public byte[] hRandField(byte[] key) { + Assert.notNull(key, "Key must not be null!"); + + return read(key, ByteArrayCodec.INSTANCE, HRANDFIELD, (Object) key); + } + + @Override + public Entry hRandFieldWithValues(byte[] key) { + Assert.notNull(key, "Key must not be null!"); + + return read(key, ByteArrayCodec.INSTANCE, RedisCommands.HRANDFIELD, (Object) key); + } + + private static final RedisCommand> HRANDFIELD_LIST = new RedisCommand<>("HRANDFIELD", new ObjectListReplayDecoder<>()); + + @Override + public List hRandField(byte[] key, long count) { + Assert.notNull(key, "Key must not be null!"); + + return read(key, ByteArrayCodec.INSTANCE, HRANDFIELD_LIST, key, count); + } + + @Override + public List> hRandFieldWithValues(byte[] key, long count) { + Assert.notNull(key, "Key must not be null!"); + + return read(key, ByteArrayCodec.INSTANCE, RedisCommands.HRANDFIELD, (Object) key, count); + } + + @Override + public Boolean copy(byte[] sourceKey, byte[] targetKey, boolean replace) { + Assert.notNull(sourceKey, "Key must not be null!"); + Assert.notNull(targetKey, "Target must not be null!"); + + List params = new ArrayList<>(); + params.add(sourceKey); + params.add(targetKey); + if (replace) { + params.add("REPLACE"); + } + + return write(sourceKey, StringCodec.INSTANCE, RedisCommands.COPY, params.toArray()); + } + + @Override + public byte[] lMove(byte[] sourceKey, byte[] destinationKey, Direction from, Direction to) { + Assert.notNull(sourceKey, "Key must not be null!"); + Assert.notNull(destinationKey, "Destination key must not be null!"); + Assert.notNull(from, "From must not be null!"); + Assert.notNull(from, "To must not be null!"); + + return write(sourceKey, ByteArrayCodec.INSTANCE, RedisCommands.LMOVE, + sourceKey, destinationKey, from, to); + } + + @Override + public byte[] bLMove(byte[] sourceKey, byte[] destinationKey, Direction from, Direction to, double timeout) { + Assert.notNull(sourceKey, "Key must not be null!"); + Assert.notNull(destinationKey, "Destination key must not be null!"); + Assert.notNull(from, "From must not be null!"); + Assert.notNull(to, "To must not be null!"); + Assert.notNull(timeout, "Timeout must not be null!"); + + return write(sourceKey, ByteArrayCodec.INSTANCE, RedisCommands.BLMOVE, + sourceKey, destinationKey, from, to, destinationKey); + } + + @Override + public List lPop(byte[] key, long count) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, RedisCommands.LPOP_LIST, key, count); + } + + @Override + public List rPop(byte[] key, long count) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, RedisCommands.RPOP_LIST, key, count); + } + + private static final RedisCommand> SMISMEMBER = new RedisCommand<>("SMISMEMBER", new ObjectListReplayDecoder<>()); + + @Override + public List sMIsMember(byte[] key, byte[]... value) { + Assert.notNull(key, "Key must not be null!"); + + List args = new ArrayList<>(); + args.add(key); + args.addAll(Arrays.asList(value)); + + return read(key, StringCodec.INSTANCE, SMISMEMBER, args.toArray()); + } + + private static final RedisCommand GETEX = new RedisCommand<>("GETEX"); + + @Override + public byte[] getEx(byte[] key, Expiration expiration) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, GETEX, key, + "PX", expiration.getExpirationTimeInMilliseconds()); + } + + private static final RedisCommand GETDEL = new RedisCommand<>("GETDEL"); + + @Override + public byte[] getDel(byte[] key) { + Assert.notNull(key, "Key must not be null!"); + + return write(key, ByteArrayCodec.INSTANCE, GETDEL, key); + } + } diff --git a/redisson-spring-data/redisson-spring-data-27/src/main/java/org/redisson/spring/data/connection/ScoredSortedSingleReplayDecoder.java b/redisson-spring-data/redisson-spring-data-27/src/main/java/org/redisson/spring/data/connection/ScoredSortedSingleReplayDecoder.java new file mode 100644 index 000000000..9c7c84a24 --- /dev/null +++ b/redisson-spring-data/redisson-spring-data-27/src/main/java/org/redisson/spring/data/connection/ScoredSortedSingleReplayDecoder.java @@ -0,0 +1,48 @@ +/** + * Copyright (c) 2013-2022 Nikita Koksharov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.redisson.spring.data.connection; + +import org.redisson.client.codec.Codec; +import org.redisson.client.codec.DoubleCodec; +import org.redisson.client.handler.State; +import org.redisson.client.protocol.Decoder; +import org.redisson.client.protocol.decoder.MultiDecoder; +import org.springframework.data.redis.connection.DefaultTuple; +import org.springframework.data.redis.connection.RedisZSetCommands.Tuple; + +import java.util.List; + +/** + * + * @author Nikita Koksharov + * + */ +public class ScoredSortedSingleReplayDecoder implements MultiDecoder { + + @Override + public Decoder getDecoder(Codec codec, int paramNum, State state) { + if (paramNum % 2 != 0) { + return DoubleCodec.INSTANCE.getValueDecoder(); + } + return MultiDecoder.super.getDecoder(codec, paramNum, state); + } + + @Override + public Tuple decode(List parts, State state) { + return new DefaultTuple((byte[])parts.get(0), ((Number)parts.get(1)).doubleValue()); + } + +}