Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements BITFIELD_RO command #2174

Merged
merged 4 commits into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,10 @@ public void bitfield(final byte[] key, final byte[]... value) {
sendCommand(BITFIELD, joinParameters(key, value));
}

public void bitfieldReadonly(final byte[] key, final byte[]... arguments) {
sendCommand(BITFIELD_RO, joinParameters(key, arguments));
}

public void hstrlen(final byte[] key, final byte[] field) {
sendCommand(HSTRLEN, key, field);
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -4059,6 +4059,13 @@ public List<Long> bitfield(final byte[] key, final byte[]... arguments) {
return client.getIntegerMultiBulkReply();
}

@Override
public List<Long> bitfieldReadonly(byte[] key, final byte[]... arguments) {
checkIsInMultiOrPipeline();
client.bitfieldReadonly(key, arguments);
return client.getIntegerMultiBulkReply();
}

@Override
public Long hstrlen(final byte[] key, final byte[] field) {
checkIsInMultiOrPipeline();
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryJedisCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -2084,6 +2084,16 @@ public List<Long> execute(Jedis connection) {
}.runBinary(key);
}

@Override
public List<Long> bitfieldReadonly(final byte[] key, final byte[]... arguments) {
return new JedisClusterCommand<List<Long>>(connectionHandler, maxAttempts) {
@Override
public List<Long> execute(Jedis connection) {
return connection.bitfieldReadonly(key, arguments);
}
}.runBinary(key);
}

@Override
public Long hstrlen(final byte[] key, final byte[] field) {
return new JedisClusterCommand<Long>(connectionHandler, maxAttempts) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryShardedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,12 @@ public List<Long> bitfield(final byte[] key, final byte[]... arguments) {
return j.bitfield(key, arguments);
}

@Override
public List<Long> bitfieldReadonly(byte[] key, final byte[]... arguments) {
Jedis j = getShard(key);
return j.bitfieldReadonly(key, arguments);
}

@Override
public Long hstrlen(final byte[] key, final byte[] field) {
Jedis j = getShard(key);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/redis/clients/jedis/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,11 @@ public void bitfield(final String key, final String... arguments) {
bitfield(SafeEncoder.encode(key), SafeEncoder.encodeMany(arguments));
}

@Override
public void bitfieldReadonly(String key, final String... arguments) {
bitfieldReadonly(SafeEncoder.encode(key), SafeEncoder.encodeMany(arguments));
}

@Override
public void hstrlen(final String key, final String field) {
hstrlen(SafeEncoder.encode(key), SafeEncoder.encode(field));
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -3746,6 +3746,13 @@ public List<Long> bitfield(final String key, final String...arguments) {
return client.getIntegerMultiBulkReply();
}

@Override
public List<Long> bitfieldReadonly(final String key, final String... arguments) {
checkIsInMultiOrPipeline();
client.bitfieldReadonly(key, arguments);
return client.getIntegerMultiBulkReply();
}

@Override
public Long hstrlen(final String key, final String field) {
checkIsInMultiOrPipeline();
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/redis/clients/jedis/JedisCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -2119,6 +2119,16 @@ public List<Long> execute(Jedis connection) {
}.run(key);
}

@Override
public List<Long> bitfieldReadonly(final String key, final String... arguments) {
return new JedisClusterCommand<List<Long>>(connectionHandler, maxAttempts) {
@Override
public List<Long> execute(Jedis connection) {
return connection.bitfieldReadonly(key, arguments);
}
}.run(key);
}

@Override
public Long hstrlen(final String key, final String field) {
return new JedisClusterCommand<Long>(connectionHandler, maxAttempts) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/redis/clients/jedis/PipelineBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,18 @@ public Response<List<Long>> bitfield(final byte[] key, final byte[]... elements)
return getResponse(BuilderFactory.LONG_LIST);
}

@Override
public Response<List<Long>> bitfieldReadonly(byte[] key, final byte[]... arguments) {
getClient(key).bitfieldReadonly(key, arguments);
return getResponse(BuilderFactory.LONG_LIST);
}

@Override
public Response<List<Long>> bitfieldReadonly(String key, final String... arguments) {
getClient(key).bitfieldReadonly(key, arguments);
return getResponse(BuilderFactory.LONG_LIST);
}

@Override
public Response<Long> hstrlen(final String key, final String field) {
getClient(key).hstrlen(key, field);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/redis/clients/jedis/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public static enum Command implements ProtocolCommand {
PFADD, PFCOUNT, PFMERGE, READONLY, GEOADD, GEODIST, GEOHASH, GEOPOS, GEORADIUS, GEORADIUS_RO,
GEORADIUSBYMEMBER, GEORADIUSBYMEMBER_RO, MODULE, BITFIELD, HSTRLEN, TOUCH, SWAPDB, MEMORY,
XADD, XLEN, XDEL, XTRIM, XRANGE, XREVRANGE, XREAD, XACK, XGROUP, XREADGROUP, XPENDING, XCLAIM,
ACL, XINFO;
ACL, XINFO, BITFIELD_RO;

private final byte[] raw;

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/redis/clients/jedis/ShardedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,12 @@ public List<Long> bitfield(final String key, final String... arguments) {
return j.bitfield(key, arguments);
}

@Override
public List<Long> bitfieldReadonly(String key, final String... arguments) {
Jedis j = getShard(key);
return j.bitfieldReadonly(key, arguments);
}

@Override
public Long hstrlen(final String key, final String field) {
Jedis j = getShard(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ List<GeoRadiusResponse> georadiusByMemberReadonly(byte[] key, byte[] member, dou
* @return
*/
List<Long> bitfield(byte[] key, byte[]... arguments);

List<Long> bitfieldReadonly(byte[] key, byte[]... arguments);

/**
* Used for HSTRLEN Redis command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ List<GeoRadiusResponse> georadiusByMemberReadonly(byte[] key, byte[] member, dou
* @return
*/
List<Long> bitfield(byte[] key, byte[]... arguments);

List<Long> bitfieldReadonly(byte[] key, byte[]... arguments);

/**
* Used for HSTRLEN Redis command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ Response<List<GeoRadiusResponse>> georadiusByMemberReadonly(byte[] key, byte[] m

Response<List<Long>> bitfield(byte[] key, byte[]... elements);

Response<List<Long>> bitfieldReadonly(byte[] key, byte[]... elements);

Response<Long> hstrlen(byte[] key, byte[] field);

Response<byte[]> xadd(byte[] key, byte[] id, Map<byte[], byte[]> hash);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/redis/clients/jedis/commands/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,8 @@ void zrevrangeByScoreWithScores(String key, String max, String min,
*/
void bitfield(String key, String... arguments);

void bitfieldReadonly(String key, String... arguments);

/**
* Used for HSTRLEN Redis command
* @param key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ List<GeoRadiusResponse> georadiusByMemberReadonly(String key, String member, dou
* @return
*/
List<Long> bitfield(String key, String...arguments);

List<Long> bitfieldReadonly(String key, String...arguments);

/**
* Used for HSTRLEN Redis command
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/redis/clients/jedis/commands/JedisCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ List<GeoRadiusResponse> georadiusByMemberReadonly(String key, String member, dou
*/
List<Long> bitfield(String key, String...arguments);

List<Long> bitfieldReadonly(String key, String...arguments);

/**
* Used for HSTRLEN Redis command
* @param key
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/redis/clients/jedis/commands/RedisPipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ Response<Set<String>> zrevrangeByLex(String key, String max, String min,
Response<Long> pfcount(String key);

Response<List<Long>> bitfield(String key, String... arguments);

Response<List<Long>> bitfieldReadonly(String key, String... arguments);

Response<Long> hstrlen(String key, String field);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.Test;

import redis.clients.jedis.BitOP;
import redis.clients.jedis.BitPosParams;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.util.SafeEncoder;

import java.util.List;
Expand Down Expand Up @@ -197,6 +199,21 @@ public void testBitfield() {
assertEquals(0L, responses.get(1).longValue());
}

@Test
public void testBitfieldReadonly() {
List<Long> responses = jedis.bitfield("mykey", "INCRBY","i5","100","1", "GET", "u4", "0");
assertEquals(1L, responses.get(0).longValue());
assertEquals(0L, responses.get(1).longValue());

List<Long> responses2 = jedis.bitfieldReadonly("mykey", "GET", "i5", "100");
assertEquals(1L, responses2.get(0).longValue());

try {
jedis.bitfieldReadonly("mykey", "INCRBY","i5","100","1", "GET", "u4", "0");
fail("Readonly command shouldn't allow INCRBY");
}catch(JedisDataException e) {}
}

@Test
public void testBinaryBitfield() {
List<Long> responses = jedis.bitfield(SafeEncoder.encode("mykey"), SafeEncoder.encode("INCRBY"),
Expand All @@ -207,4 +224,15 @@ public void testBinaryBitfield() {
assertEquals(0L, responses.get(1).longValue());
}

@Test
public void testBinaryBitfieldReadonly() {
List<Long> responses = jedis.bitfield("mykey", "INCRBY","i5","100","1", "GET", "u4", "0");
assertEquals(1L, responses.get(0).longValue());
assertEquals(0L, responses.get(1).longValue());

List<Long> responses2 = jedis.bitfieldReadonly(SafeEncoder.encode("mykey"), SafeEncoder.encode("GET"),
SafeEncoder.encode("i5"), SafeEncoder.encode("100"));
assertEquals(1L, responses2.get(0).longValue());
}

}