add bitset and bitget

This commit is contained in:
Jonathan Leibiusky
2010-12-20 16:26:52 -03:00
parent 586cc9f6d9
commit 0d296a7f1a
6 changed files with 86 additions and 1 deletions

View File

@@ -636,4 +636,12 @@ public class BinaryClient extends Connection {
public void configResetStat() {
sendCommand(CONFIG, Keyword.RESETSTAT.name());
}
public void setbit(byte[] key, int offset, byte[] value) {
sendCommand(SETBIT, key, toByteArray(offset), value);
}
public void getbit(byte[] key, int offset) {
sendCommand(GETBIT, key, toByteArray(offset));
}
}

View File

@@ -2901,4 +2901,28 @@ public class BinaryJedis implements BinaryJedisCommands {
return client.getBulkReply();
}
/**
* Sets or clears the bit at offset in the string value stored at key
*
* @param key
* @param offset
* @param value
* @return
*/
public Long setbit(byte[] key, int offset, byte[] value) {
client.setbit(key, offset, value);
return client.getIntegerReply();
}
/**
* Returns the bit value at offset in the string value stored at key
*
* @param key
* @param offset
* @return
*/
public Long getbit(byte[] key, int offset) {
client.getbit(key, offset);
return client.getIntegerReply();
}
}

View File

@@ -506,4 +506,12 @@ public class Client extends BinaryClient implements Commands {
brpoplpush(SafeEncoder.encode(source), SafeEncoder.encode(destination),
timeout);
}
public void setbit(final String key, final int offset, final String value) {
setbit(SafeEncoder.encode(key), offset, SafeEncoder.encode(value));
}
public void getbit(String key, int offset) {
getbit(SafeEncoder.encode(key), offset);
}
}

View File

@@ -2595,4 +2595,29 @@ public class Jedis extends BinaryJedis implements JedisCommands {
client.brpoplpush(source, destination, timeout);
return client.getBulkReply();
}
/**
* Sets or clears the bit at offset in the string value stored at key
*
* @param key
* @param offset
* @param value
* @return
*/
public Long setbit(String key, int offset, String value) {
client.setbit(key, offset, value);
return client.getIntegerReply();
}
/**
* Returns the bit value at offset in the string value stored at key
*
* @param key
* @param offset
* @return
*/
public Long getbit(String key, int offset) {
client.getbit(key, offset);
return client.getIntegerReply();
}
}

View File

@@ -134,7 +134,7 @@ public final class Protocol {
}
public static enum Command {
PING, SET, GET, QUIT, EXISTS, DEL, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME, RENAMENX, RENAMEX, DBSIZE, EXPIRE, EXPIREAT, TTL, SELECT, MOVE, FLUSHALL, GETSET, MGET, SETNX, SETEX, MSET, MSETNX, DECRBY, DECR, INCRBY, INCR, APPEND, SUBSTR, HSET, HGET, HSETNX, HMSET, HMGET, HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, RPUSH, LPUSH, LLEN, LRANGE, LTRIM, LINDEX, LSET, LREM, LPOP, RPOP, RPOPLPUSH, SADD, SMEMBERS, SREM, SPOP, SMOVE, SCARD, SISMEMBER, SINTER, SINTERSTORE, SUNION, SUNIONSTORE, SDIFF, SDIFFSTORE, SRANDMEMBER, ZADD, ZRANGE, ZREM, ZINCRBY, ZRANK, ZREVRANK, ZREVRANGE, ZCARD, ZSCORE, MULTI, DISCARD, EXEC, WATCH, UNWATCH, SORT, BLPOP, BRPOP, AUTH, SUBSCRIBE, PUBLISH, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, ZCOUNT, ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE, SAVE, BGSAVE, BGREWRITEAOF, LASTSAVE, SHUTDOWN, INFO, MONITOR, SLAVEOF, CONFIG, STRLEN, SYNC, LPUSHX, PERSIST, RPUSHX, ECHO, LINSERT, DEBUG, BRPOPLPUSH;
PING, SET, GET, QUIT, EXISTS, DEL, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME, RENAMENX, RENAMEX, DBSIZE, EXPIRE, EXPIREAT, TTL, SELECT, MOVE, FLUSHALL, GETSET, MGET, SETNX, SETEX, MSET, MSETNX, DECRBY, DECR, INCRBY, INCR, APPEND, SUBSTR, HSET, HGET, HSETNX, HMSET, HMGET, HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, RPUSH, LPUSH, LLEN, LRANGE, LTRIM, LINDEX, LSET, LREM, LPOP, RPOP, RPOPLPUSH, SADD, SMEMBERS, SREM, SPOP, SMOVE, SCARD, SISMEMBER, SINTER, SINTERSTORE, SUNION, SUNIONSTORE, SDIFF, SDIFFSTORE, SRANDMEMBER, ZADD, ZRANGE, ZREM, ZINCRBY, ZRANK, ZREVRANK, ZREVRANGE, ZCARD, ZSCORE, MULTI, DISCARD, EXEC, WATCH, UNWATCH, SORT, BLPOP, BRPOP, AUTH, SUBSCRIBE, PUBLISH, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, ZCOUNT, ZRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE, SAVE, BGSAVE, BGREWRITEAOF, LASTSAVE, SHUTDOWN, INFO, MONITOR, SLAVEOF, CONFIG, STRLEN, SYNC, LPUSHX, PERSIST, RPUSHX, ECHO, LINSERT, DEBUG, BRPOPLPUSH, SETBIT, GETBIT;
public final byte[] raw;

View File

@@ -0,0 +1,20 @@
package redis.clients.jedis.tests.commands;
import org.junit.Test;
public class BitCommandsTest extends JedisCommandTestBase {
@Test
public void setAndgetbit() {
long bit = jedis.setbit("foo", 0, "1");
assertEquals(0, bit);
bit = jedis.getbit("foo", 0);
assertEquals(1, bit);
long bbit = jedis.setbit("bfoo".getBytes(), 0, "1".getBytes());
assertEquals(0, bbit);
bbit = jedis.getbit("bfoo".getBytes(), 0);
assertEquals(1, bbit);
}
}