exists, sismember and hexists return Boolean instead of long

This commit is contained in:
Jonathan Leibiusky
2010-11-30 12:51:01 -03:00
parent 81ec9f8af3
commit cd9e17a709
11 changed files with 70 additions and 86 deletions

View File

@@ -91,12 +91,12 @@ public class BinaryJedis implements BinaryJedisCommands {
* Time complexity: O(1) * Time complexity: O(1)
* *
* @param key * @param key
* @return Integer reply, "0" if the key exists, otherwise "1" * @return Integer reply, "1" if the key exists, otherwise "0"
*/ */
public Long exists(final byte[] key) { public Boolean exists(final byte[] key) {
checkIsInMulti(); checkIsInMulti();
client.exists(key); client.exists(key);
return client.getIntegerReply(); return client.getIntegerReply() == 1;
} }
/** /**
@@ -784,10 +784,10 @@ public class BinaryJedis implements BinaryJedisCommands {
* @return Return 1 if the hash stored at key contains the specified field. * @return Return 1 if the hash stored at key contains the specified field.
* Return 0 if the key is not found or the field is not present. * Return 0 if the key is not found or the field is not present.
*/ */
public Long hexists(final byte[] key, final byte[] field) { public Boolean hexists(final byte[] key, final byte[] field) {
checkIsInMulti(); checkIsInMulti();
client.hexists(key, field); client.hexists(key, field);
return client.getIntegerReply(); return client.getIntegerReply() == 1;
} }
/** /**
@@ -1280,10 +1280,10 @@ public class BinaryJedis implements BinaryJedisCommands {
* set 0 if the element is not a member of the set OR if the key * set 0 if the element is not a member of the set OR if the key
* does not exist * does not exist
*/ */
public Long sismember(final byte[] key, final byte[] member) { public Boolean sismember(final byte[] key, final byte[] member) {
checkIsInMulti(); checkIsInMulti();
client.sismember(key, member); client.sismember(key, member);
return client.getIntegerReply(); return client.getIntegerReply() == 1;
} }
/** /**

View File

@@ -15,7 +15,7 @@ public interface BinaryJedisCommands {
byte[] get(byte[] key); byte[] get(byte[] key);
Long exists(byte[] key); Boolean exists(byte[] key);
String type(byte[] key); String type(byte[] key);
@@ -55,7 +55,7 @@ public interface BinaryJedisCommands {
Long hincrBy(byte[] key, byte[] field, int value); Long hincrBy(byte[] key, byte[] field, int value);
Long hexists(byte[] key, byte[] field); Boolean hexists(byte[] key, byte[] field);
Long hdel(byte[] key, byte[] field); Long hdel(byte[] key, byte[] field);
@@ -97,7 +97,7 @@ public interface BinaryJedisCommands {
Long scard(byte[] key); Long scard(byte[] key);
Long sismember(byte[] key, byte[] member); Boolean sismember(byte[] key, byte[] member);
byte[] srandmember(byte[] key); byte[] srandmember(byte[] key);
@@ -131,29 +131,17 @@ public interface BinaryJedisCommands {
Set<byte[]> zrangeByScore(byte[] key, double min, double max); Set<byte[]> zrangeByScore(byte[] key, double min, double max);
Set<byte[]> zrangeByScore( Set<byte[]> zrangeByScore(byte[] key, double min, double max, int offset,
byte[] key, int count);
double min,
double max,
int offset,
int count);
Set<Tuple> zrangeByScoreWithScores(byte[] key, double min, double max); Set<Tuple> zrangeByScoreWithScores(byte[] key, double min, double max);
Set<Tuple> zrangeByScoreWithScores( Set<Tuple> zrangeByScoreWithScores(byte[] key, double min, double max,
byte[] key, int offset, int count);
double min,
double max,
int offset,
int count);
Long zremrangeByRank(byte[] key, int start, int end); Long zremrangeByRank(byte[] key, int start, int end);
Long zremrangeByScore(byte[] key, double start, double end); Long zremrangeByScore(byte[] key, double start, double end);
Long linsert( Long linsert(byte[] key, LIST_POSITION where, byte[] pivot, byte[] value);
byte[] key,
LIST_POSITION where,
byte[] pivot,
byte[] value);
} }

View File

@@ -50,7 +50,7 @@ public class BinaryShardedJedis extends Sharded<Jedis, JedisShardInfo>
return j.get(key); return j.get(key);
} }
public Long exists(byte[] key) { public Boolean exists(byte[] key) {
Jedis j = getShard(key); Jedis j = getShard(key);
return j.exists(key); return j.exists(key);
} }
@@ -150,7 +150,7 @@ public class BinaryShardedJedis extends Sharded<Jedis, JedisShardInfo>
return j.hincrBy(key, field, value); return j.hincrBy(key, field, value);
} }
public Long hexists(byte[] key, byte[] field) { public Boolean hexists(byte[] key, byte[] field) {
Jedis j = getShard(key); Jedis j = getShard(key);
return j.hexists(key, field); return j.hexists(key, field);
} }
@@ -255,7 +255,7 @@ public class BinaryShardedJedis extends Sharded<Jedis, JedisShardInfo>
return j.scard(key); return j.scard(key);
} }
public Long sismember(byte[] key, byte[] member) { public Boolean sismember(byte[] key, byte[] member) {
Jedis j = getShard(key); Jedis j = getShard(key);
return j.sismember(key, member); return j.sismember(key, member);
} }

View File

@@ -78,19 +78,19 @@ public class Jedis extends BinaryJedis implements JedisCommands {
} }
/** /**
* Test if the specified key exists. The command returns "0" if the key * Test if the specified key exists. The command returns "1" if the key
* exists, otherwise "1" is returned. Note that even keys set with an empty * exists, otherwise "1" is returned. Note that even keys set with an empty
* string as value will return "1". * string as value will return "0".
* *
* Time complexity: O(1) * Time complexity: O(1)
* *
* @param key * @param key
* @return Integer reply, "0" if the key exists, otherwise "1" * @return Integer reply, "0" if the key exists, otherwise "1"
*/ */
public Long exists(final String key) { public Boolean exists(final String key) {
runChecks(); runChecks();
client.exists(key); client.exists(key);
return client.getIntegerReply(); return client.getIntegerReply() == 1;
} }
/** /**
@@ -782,10 +782,10 @@ public class Jedis extends BinaryJedis implements JedisCommands {
* @return Return 1 if the hash stored at key contains the specified field. * @return Return 1 if the hash stored at key contains the specified field.
* Return 0 if the key is not found or the field is not present. * Return 0 if the key is not found or the field is not present.
*/ */
public Long hexists(final String key, final String field) { public Boolean hexists(final String key, final String field) {
runChecks(); runChecks();
client.hexists(key, field); client.hexists(key, field);
return client.getIntegerReply(); return client.getIntegerReply() == 1;
} }
/** /**
@@ -1278,10 +1278,10 @@ public class Jedis extends BinaryJedis implements JedisCommands {
* set 0 if the element is not a member of the set OR if the key * set 0 if the element is not a member of the set OR if the key
* does not exist * does not exist
*/ */
public Long sismember(final String key, final String member) { public Boolean sismember(final String key, final String member) {
runChecks(); runChecks();
client.sismember(key, member); client.sismember(key, member);
return client.getIntegerReply(); return client.getIntegerReply() == 1;
} }
/** /**

View File

@@ -13,7 +13,7 @@ public interface JedisCommands {
String get(String key); String get(String key);
Long exists(String key); Boolean exists(String key);
String type(String key); String type(String key);
@@ -53,7 +53,7 @@ public interface JedisCommands {
Long hincrBy(String key, String field, int value); Long hincrBy(String key, String field, int value);
Long hexists(String key, String field); Boolean hexists(String key, String field);
Long hdel(String key, String field); Long hdel(String key, String field);
@@ -95,7 +95,7 @@ public interface JedisCommands {
Long scard(String key); Long scard(String key);
Long sismember(String key, String member); Boolean sismember(String key, String member);
String srandmember(String key); String srandmember(String key);
@@ -129,18 +129,18 @@ public interface JedisCommands {
Set<String> zrangeByScore(String key, double min, double max); Set<String> zrangeByScore(String key, double min, double max);
Set<String> zrangeByScore(String key, double min, double max, Set<String> zrangeByScore(String key, double min, double max, int offset,
int offset, int count); int count);
Set<Tuple> zrangeByScoreWithScores(String key, double min, double max); Set<Tuple> zrangeByScoreWithScores(String key, double min, double max);
Set<Tuple> zrangeByScoreWithScores(String key, double min, Set<Tuple> zrangeByScoreWithScores(String key, double min, double max,
double max, int offset, int count); int offset, int count);
Long zremrangeByRank(String key, int start, int end); Long zremrangeByRank(String key, int start, int end);
Long zremrangeByScore(String key, double start, double end); Long zremrangeByScore(String key, double start, double end);
Long linsert(String key, Client.LIST_POSITION where, String pivot, Long linsert(String key, Client.LIST_POSITION where, String pivot,
String value); String value);
} }

View File

@@ -29,7 +29,7 @@ public class ShardedJedis extends BinaryShardedJedis implements JedisCommands {
} }
@Override @Override
public void disconnect() throws IOException { public void disconnect() throws IOException {
for (Jedis jedis : getAllShards()) { for (Jedis jedis : getAllShards()) {
jedis.quit(); jedis.quit();
jedis.disconnect(); jedis.disconnect();
@@ -46,7 +46,7 @@ public class ShardedJedis extends BinaryShardedJedis implements JedisCommands {
return j.get(key); return j.get(key);
} }
public Long exists(String key) { public Boolean exists(String key) {
Jedis j = getShard(key); Jedis j = getShard(key);
return j.exists(key); return j.exists(key);
} }
@@ -146,7 +146,7 @@ public class ShardedJedis extends BinaryShardedJedis implements JedisCommands {
return j.hincrBy(key, field, value); return j.hincrBy(key, field, value);
} }
public Long hexists(String key, String field) { public Boolean hexists(String key, String field) {
Jedis j = getShard(key); Jedis j = getShard(key);
return j.hexists(key, field); return j.hexists(key, field);
} }
@@ -251,7 +251,7 @@ public class ShardedJedis extends BinaryShardedJedis implements JedisCommands {
return j.scard(key); return j.scard(key);
} }
public Long sismember(String key, String member) { public Boolean sismember(String key, String member) {
Jedis j = getShard(key); Jedis j = getShard(key);
return j.sismember(key, member); return j.sismember(key, member);
} }

View File

@@ -52,6 +52,5 @@ public class JedisTest extends JedisCommandTestBase {
// every 10 seconds or so // every 10 seconds or so
Thread.sleep(20000); Thread.sleep(20000);
jedis.hmget("foobar", "foo"); jedis.hmget("foobar", "foo");
jedis.configSet("timeout", "300");
} }
} }

View File

@@ -37,23 +37,23 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase {
status = jedis.set(bfoo, bbar); status = jedis.set(bfoo, bbar);
assertEquals("OK", status); assertEquals("OK", status);
long reply = jedis.exists("foo"); boolean reply = jedis.exists("foo");
assertEquals(1, reply); assertTrue(reply);
reply = jedis.exists(bfoo); reply = jedis.exists(bfoo);
assertEquals(1, reply); assertTrue(reply);
reply = jedis.del("foo"); long lreply = jedis.del("foo");
assertEquals(1, reply); assertEquals(1, lreply);
reply = jedis.del(bfoo); lreply = jedis.del(bfoo);
assertEquals(1, reply); assertEquals(1, lreply);
reply = jedis.exists("foo"); reply = jedis.exists("foo");
assertEquals(0, reply); assertFalse(reply);
reply = jedis.exists(bfoo); reply = jedis.exists(bfoo);
assertEquals(0, reply); assertFalse(reply);
} }
@Test @Test
@@ -65,12 +65,12 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase {
long reply = jedis.del("foo1", "foo2", "foo3"); long reply = jedis.del("foo1", "foo2", "foo3");
assertEquals(3, reply); assertEquals(3, reply);
reply = jedis.exists("foo1"); Boolean breply = jedis.exists("foo1");
assertEquals(0, reply); assertFalse(breply);
reply = jedis.exists("foo2"); breply = jedis.exists("foo2");
assertEquals(0, reply); assertFalse(breply);
reply = jedis.exists("foo3"); breply = jedis.exists("foo3");
assertEquals(0, reply); assertFalse(breply);
jedis.set("foo1", "bar1"); jedis.set("foo1", "bar1");
@@ -88,12 +88,12 @@ public class AllKindOfValuesCommandsTest extends JedisCommandTestBase {
reply = jedis.del(bfoo1, bfoo2, bfoo3); reply = jedis.del(bfoo1, bfoo2, bfoo3);
assertEquals(3, reply); assertEquals(3, reply);
reply = jedis.exists(bfoo1); breply = jedis.exists(bfoo1);
assertEquals(0, reply); assertFalse(breply);
reply = jedis.exists(bfoo2); breply = jedis.exists(bfoo2);
assertEquals(0, reply); assertFalse(breply);
reply = jedis.exists(bfoo3); breply = jedis.exists(bfoo3);
assertEquals(0, reply); assertFalse(breply);
jedis.set(bfoo1, bbar1); jedis.set(bfoo1, bbar1);

View File

@@ -150,9 +150,9 @@ public class HashesCommandsTest extends JedisCommandTestBase {
hash.put("car", "bar"); hash.put("car", "bar");
jedis.hmset("foo", hash); jedis.hmset("foo", hash);
assertEquals(0, jedis.hexists("bar", "foo").intValue()); assertFalse(jedis.hexists("bar", "foo"));
assertEquals(0, jedis.hexists("foo", "foo").intValue()); assertFalse(jedis.hexists("foo", "foo"));
assertEquals(1, jedis.hexists("foo", "bar").intValue()); assertTrue(jedis.hexists("foo", "bar"));
// Binary // Binary
Map<byte[], byte[]> bhash = new HashMap<byte[], byte[]>(); Map<byte[], byte[]> bhash = new HashMap<byte[], byte[]>();
@@ -160,9 +160,9 @@ public class HashesCommandsTest extends JedisCommandTestBase {
bhash.put(bcar, bbar); bhash.put(bcar, bbar);
jedis.hmset(bfoo, bhash); jedis.hmset(bfoo, bhash);
assertEquals(0, jedis.hexists(bbar, bfoo).intValue()); assertFalse(jedis.hexists(bbar, bfoo));
assertEquals(0, jedis.hexists(bfoo, bfoo).intValue()); assertFalse(jedis.hexists(bfoo, bfoo));
assertEquals(1, jedis.hexists(bfoo, bbar).intValue()); assertTrue(jedis.hexists(bfoo, bbar));
} }

View File

@@ -28,6 +28,7 @@ public abstract class JedisCommandTestBase extends JedisTestBase {
jedis = new Jedis(hnp.host, hnp.port, 500); jedis = new Jedis(hnp.host, hnp.port, 500);
jedis.connect(); jedis.connect();
jedis.auth("foobared"); jedis.auth("foobared");
jedis.configSet("timeout", "300");
jedis.flushAll(); jedis.flushAll();
} }

View File

@@ -199,21 +199,17 @@ public class SetCommandsTest extends JedisCommandTestBase {
jedis.sadd("foo", "a"); jedis.sadd("foo", "a");
jedis.sadd("foo", "b"); jedis.sadd("foo", "b");
long status = jedis.sismember("foo", "a"); assertTrue(jedis.sismember("foo", "a"));
assertEquals(1, status);
status = jedis.sismember("foo", "c"); assertFalse(jedis.sismember("foo", "c"));
assertEquals(0, status);
// Binary // Binary
jedis.sadd(bfoo, ba); jedis.sadd(bfoo, ba);
jedis.sadd(bfoo, bb); jedis.sadd(bfoo, bb);
long bstatus = jedis.sismember(bfoo, ba); assertTrue(jedis.sismember(bfoo, ba));
assertEquals(1, bstatus);
bstatus = jedis.sismember(bfoo, bc); assertFalse(jedis.sismember(bfoo, bc));
assertEquals(0, bstatus);
} }