Description
Currently BitFieldSubCommands
supports redis bitfiled
operations by chaining methods to generate multiple subcomamnd, as follows.
BitFieldSubCommands fieldSubCommands = BitFieldSubCommands.create()
.get(BitFieldSubCommands.BitFieldType.unsigned(1)).valueAt(1)
.get(BitFieldSubCommands.BitFieldType.unsigned(1)).valueAt(2);
List<Long> result = redisTemplate.opsForValue().bitField("test", fieldSubCommands);
Problem
However, the bitmap offset to be manipulated in the actual project is generated upstream by some rules, and it is not possible to directly use the above chain method to generate multiple bit fields for the operation of the command
// The offset data is usually obtained through the upstream method
// long[] offsetArray = getBitIndex();
long[] offsetArray = new long[]{1,3,5,7,9};
BitFieldSubCommands fieldSubCommands = BitFieldSubCommands.create();
for (int i = 0; i < offsetArray.length; i++) {
BitFieldSubCommands subCommands = BitFieldSubCommands.create.get(BitFieldSubCommands.BitFieldType.unsigned(1)).valueAt(offsetArray[i]);
//There is no way to combine multiple commands
}
List<Long> result = redisTemplate.opsForValue().bitField("test",fieldSubCommands);
Solution
In order to support such operations as mentioned above in my project, I modified the code related to BitFieldSubCommands
by liberalizing the BitFieldCommands
and BitFieldCommand
implementation classes such as BitFieldGet
constructor to be public, so that I can freely combine various bit operations in the following example way.
long[] offsetArray = new long[]{1,3,5,7,9};
List<BitFieldSubCommands.BitFieldSubCommand> list = new ArrayList<>();
for (int i = 0; i < offsetArray.length; i++) {
BitFieldSubCommands.BitFieldGet bitFieldGet = new BitFieldSubCommands.BitFieldGet(BitFieldSubCommands.BitFieldType.unsigned(1),offsetArray[i]);
list.add(bitFieldGet);
}
BitFieldSubCommands fieldSubCommands = new BitFieldSubCommands(list);
List<Long> result = redisTemplate.opsForValue().bitField("test",fieldSubCommands);
See also https://redis.io/commands/bitfield for further information.