diff --git a/redisson/src/main/java/org/redisson/RedissonBitSet.java b/redisson/src/main/java/org/redisson/RedissonBitSet.java index 527130a57..356c006f6 100644 --- a/redisson/src/main/java/org/redisson/RedissonBitSet.java +++ b/redisson/src/main/java/org/redisson/RedissonBitSet.java @@ -278,6 +278,24 @@ public class RedissonBitSet extends RedissonExpirable implements RBitSet { return commandExecutor.readAsync(getRawName(), LongCodec.INSTANCE, RedisCommands.GETBIT, getRawName(), bitIndex); } + @Override + public boolean[] get(long... bitIndexes) { + return get(getAsync(bitIndexes)); + } + + @Override + public RFuture getAsync(long... bitIndexes) { + Object[] indexes = new Object[bitIndexes.length * 3 + 1]; + int j = 0; + indexes[j++] = getRawName(); + for (long l : bitIndexes) { + indexes[j++] = "get"; + indexes[j++] = "u1"; + indexes[j++] = l; + } + return commandExecutor.readAsync(getRawName(), LongCodec.INSTANCE, RedisCommands.BITFIELD_BOOLEANS, indexes); + } + @Override public boolean set(long bitIndex) { return get(setAsync(bitIndex, true)); diff --git a/redisson/src/main/java/org/redisson/api/RBitSet.java b/redisson/src/main/java/org/redisson/api/RBitSet.java index 260bb7a70..79868308b 100644 --- a/redisson/src/main/java/org/redisson/api/RBitSet.java +++ b/redisson/src/main/java/org/redisson/api/RBitSet.java @@ -275,6 +275,14 @@ public interface RBitSet extends RExpirable, RBitSetAsync { * @return true if bit set to one and false overwise. */ boolean get(long bitIndex); + + /** + * Returns a boolean array where each element of the array corresponds to the query result of the input parameters. + * + * @param bitIndexes indexes of bit + * @return Returns a boolean array where each element of the array corresponds to the query result of the input parameters. + */ + boolean[] get(long... bitIndexes); /** * Set bit to one at specified bitIndex diff --git a/redisson/src/main/java/org/redisson/api/RBitSetAsync.java b/redisson/src/main/java/org/redisson/api/RBitSetAsync.java index 9b2608144..45d3ca101 100644 --- a/redisson/src/main/java/org/redisson/api/RBitSetAsync.java +++ b/redisson/src/main/java/org/redisson/api/RBitSetAsync.java @@ -274,6 +274,14 @@ public interface RBitSetAsync extends RExpirableAsync { * @return true if bit set to one and false overwise. */ RFuture getAsync(long bitIndex); + + /** + * Returns a boolean array where each element of the array corresponds to the query result of the input parameters. + * + * @param bitIndexes indexes of bit + * @return Returns a boolean array where each element of the array corresponds to the query result of the input parameters. + */ + RFuture getAsync(long... bitIndexes); /** * Set bit to one at specified bitIndex diff --git a/redisson/src/main/java/org/redisson/api/RBitSetReactive.java b/redisson/src/main/java/org/redisson/api/RBitSetReactive.java index 709602e9e..ad701a1ac 100644 --- a/redisson/src/main/java/org/redisson/api/RBitSetReactive.java +++ b/redisson/src/main/java/org/redisson/api/RBitSetReactive.java @@ -276,6 +276,14 @@ public interface RBitSetReactive extends RExpirableReactive { * @return true if bit set to one and false overwise. */ Mono get(long bitIndex); + + /** + * Returns a boolean array where each element of the array corresponds to the query result of the input parameters. + * + * @param bitIndexes indexes of bit + * @return Returns a boolean array where each element of the array corresponds to the query result of the input parameters. + */ + Mono get(long... bitIndexes); /** * Set bit to one at specified bitIndex diff --git a/redisson/src/main/java/org/redisson/api/RBitSetRx.java b/redisson/src/main/java/org/redisson/api/RBitSetRx.java index 06b00e84c..8771da49d 100644 --- a/redisson/src/main/java/org/redisson/api/RBitSetRx.java +++ b/redisson/src/main/java/org/redisson/api/RBitSetRx.java @@ -278,6 +278,14 @@ public interface RBitSetRx extends RExpirableRx { * @return true if bit set to one and false overwise. */ Single get(long bitIndex); + + /** + * Returns a boolean array where each element of the array corresponds to the query result of the input parameters. + * + * @param bitIndexes indexes of bit + * @return Returns a boolean array where each element of the array corresponds to the query result of the input parameters. + */ + Single get(long... bitIndexes); /** * Set bit to one at specified bitIndex diff --git a/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java b/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java index db4a8dacf..4b187d17e 100644 --- a/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java +++ b/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java @@ -64,6 +64,9 @@ public interface RedisCommands { RedisStrictCommand BITFIELD_SHORT = new RedisStrictCommand<>("BITFIELD", null, new ListFirstObjectDecoder(), new ShortReplayConvertor()); RedisStrictCommand BITFIELD_VOID = new RedisStrictCommand<>("BITFIELD", new VoidReplayConvertor()); + + RedisStrictCommand BITFIELD_BOOLEANS = new RedisStrictCommand<>("BITFIELD", null, + new ArrayBooleanDecoder(), new BooleanReplayConvertor()); RedisStrictCommand GETBIT = new RedisStrictCommand("GETBIT", new BooleanReplayConvertor()); RedisStrictCommand BITS_SIZE = new RedisStrictCommand("STRLEN", new BitsSizeReplayConvertor()); diff --git a/redisson/src/main/java/org/redisson/client/protocol/decoder/ArrayBooleanDecoder.java b/redisson/src/main/java/org/redisson/client/protocol/decoder/ArrayBooleanDecoder.java new file mode 100644 index 000000000..7ec0e2048 --- /dev/null +++ b/redisson/src/main/java/org/redisson/client/protocol/decoder/ArrayBooleanDecoder.java @@ -0,0 +1,46 @@ +/** + * Copyright (c) 2013-2024 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.client.protocol.decoder; + +import org.redisson.client.handler.State; + +import java.util.List; + +/** + * + * @author seakider + * + */ +public class ArrayBooleanDecoder implements MultiDecoder { + + @Override + public boolean[] decode(List parts, State state) { + if (parts.isEmpty()) { + return new boolean[0]; + } + + boolean[] result = new boolean[parts.size()]; + for (int i = 0; i < parts.size(); i++) { + Object part = parts.get(i); + if (part instanceof Boolean) { + result[i] = (boolean) part; + } else { + result[i] = false; + } + } + return result; + } +} diff --git a/redisson/src/test/java/org/redisson/RedissonBitSetReactiveTest.java b/redisson/src/test/java/org/redisson/RedissonBitSetReactiveTest.java index 592c73e3f..1f6ae1be6 100644 --- a/redisson/src/test/java/org/redisson/RedissonBitSetReactiveTest.java +++ b/redisson/src/test/java/org/redisson/RedissonBitSetReactiveTest.java @@ -6,6 +6,8 @@ import org.redisson.api.RBitSetReactive; import java.util.BitSet; +import static org.assertj.core.api.Assertions.assertThat; + public class RedissonBitSetReactiveTest extends BaseReactiveTest { @Test @@ -112,6 +114,17 @@ public class RedissonBitSetReactiveTest extends BaseReactiveTest { Assertions.assertEquals(1, sync(bs1.cardinality()).intValue()); Assertions.assertEquals(16, sync(bs1.size()).intValue()); } - + + @Test + public void testGetWithIndexes() { + RBitSetReactive bitset = redisson.getBitSet("testbitset"); + + sync(bitset.set(4, 10)); + boolean[] result = sync(bitset.get(2, 4, 7, 8)); + assertThat(result[0]).isFalse(); + assertThat(result[1]).isTrue(); + assertThat(result[2]).isTrue(); + assertThat(result[3]).isTrue(); + } } diff --git a/redisson/src/test/java/org/redisson/RedissonBitSetTest.java b/redisson/src/test/java/org/redisson/RedissonBitSetTest.java index d1a550384..36303420b 100644 --- a/redisson/src/test/java/org/redisson/RedissonBitSetTest.java +++ b/redisson/src/test/java/org/redisson/RedissonBitSetTest.java @@ -210,5 +210,16 @@ public class RedissonBitSetTest extends RedisDockerTest { assertThat(bs1.size()).isEqualTo(16); } + @Test + public void testGetWithIndexes() { + RBitSet bitset = redisson.getBitSet("testbitset"); + + bitset.set(4, 10); + boolean[] result = bitset.get(2, 4, 7, 8); + assertThat(result[0]).isFalse(); + assertThat(result[1]).isTrue(); + assertThat(result[2]).isTrue(); + assertThat(result[3]).isTrue(); + } }