Skip to content

Commit

Permalink
Support SINTERCARD Command (#2821)
Browse files Browse the repository at this point in the history
* Implement SINTERCARD command

* Implement SINTERCARD command

* add test to unified

* undo format

* undo format

* Add javadoc to interface

Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com>
  • Loading branch information
AvitalFineRedis and sazzad16 committed Jan 18, 2022
1 parent 26a682e commit 4d1de18
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/main/java/redis/clients/jedis/CommandObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,14 @@ public final CommandObject<Long> sinterstore(String dstkey, String... keys) {
return new CommandObject<>(commandArguments(SINTERSTORE).key(dstkey).keys((Object[]) keys), BuilderFactory.LONG);
}

public final CommandObject<Long> sintercard(String... keys) {
return new CommandObject<>(commandArguments(SINTERCARD).add(keys.length).keys((Object[]) keys), BuilderFactory.LONG);
}

public final CommandObject<Long> sintercard(int limit, String... keys) {
return new CommandObject<>(commandArguments(SINTERCARD).add(keys.length).keys((Object[]) keys).add(LIMIT).add(limit),BuilderFactory.LONG);
}

public final CommandObject<Set<byte[]>> sinter(byte[]... keys) {
return new CommandObject<>(commandArguments(SINTER).keys((Object[]) keys), BuilderFactory.BINARY_SET);
}
Expand All @@ -1086,6 +1094,15 @@ public final CommandObject<Long> sinterstore(byte[] dstkey, byte[]... keys) {
return new CommandObject<>(commandArguments(SINTERSTORE).key(dstkey).keys((Object[]) keys), BuilderFactory.LONG);
}

public final CommandObject<Long> sintercard(byte[]... keys) {
return new CommandObject<>(commandArguments(SINTERCARD).add(keys.length).keys((Object[]) keys), BuilderFactory.LONG);
}

public final CommandObject<Long> sintercard(int limit, byte[]... keys) {
return new CommandObject<>(commandArguments(SINTERCARD).add(keys.length).keys((Object[]) keys).add(LIMIT).add(limit),BuilderFactory.LONG);
}


public final CommandObject<Set<String>> sunion(String... keys) {
return new CommandObject<>(commandArguments(SUNION).keys((Object[]) keys), BuilderFactory.STRING_SET);
}
Expand Down
62 changes: 61 additions & 1 deletion src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,7 @@ public Set<byte[]> sinter(final byte[]... keys) {
}

/**
* This commanad works exactly like {@link Jedis#sinter(byte[][]) SINTER} but instead of being
* This command works exactly like {@link Jedis#sinter(byte[][]) SINTER} but instead of being
* returned the resulting set is stored as dstkey.
* <p>
* Time complexity O(N*M) worst case where N is the cardinality of the smallest set and M the
Expand All @@ -1798,6 +1798,36 @@ public long sinterstore(final byte[] dstkey, final byte[]... keys) {
return connection.executeCommand(commandObjects.sinterstore(dstkey, keys));
}

/**
* This command works exactly like {@link Jedis#sinter(byte[][]) SINTER} but instead of returning
* the result set, it returns just the cardinality of the result. LIMIT defaults to 0 and means unlimited
* <p>
* Time complexity O(N*M) worst case where N is the cardinality of the smallest
* @param keys
* @return The cardinality of the set which would result from the intersection of all the given sets
*/
@Override
public long sintercard(byte[]... keys) {
checkIsInMultiOrPipeline();
return connection.executeCommand(commandObjects.sintercard(keys));
}

/**
* This command works exactly like {@link Jedis#sinter(byte[][]) SINTER} but instead of returning
* the result set, it returns just the cardinality of the result.
* <p>
* Time complexity O(N*M) worst case where N is the cardinality of the smallest
* @param limit If the intersection cardinality reaches limit partway through the computation,
* the algorithm will exit and yield limit as the cardinality.
* @param keys
* @return The cardinality of the set which would result from the intersection of all the given sets
*/
@Override
public long sintercard(int limit, byte[]... keys) {
checkIsInMultiOrPipeline();
return connection.executeCommand(commandObjects.sintercard(limit, keys));
}

/**
* Return the members of a set resulting from the union of all the sets hold at the specified
* keys. Like in {@link Jedis#lrange(byte[], long, long)} LRANGE} the result is sent to the
Expand Down Expand Up @@ -5952,6 +5982,36 @@ public long sinterstore(final String dstkey, final String... keys) {
return connection.executeCommand(commandObjects.sinterstore(dstkey, keys));
}

/**
* This command works exactly like {@link Jedis#sinter(String[]) SINTER} but instead of returning
* the result set, it returns just the cardinality of the result.
* <p>
* Time complexity O(N*M) worst case where N is the cardinality of the smallest
* @param keys
* @return The cardinality of the set which would result from the intersection of all the given sets
*/
@Override
public long sintercard(String... keys) {
checkIsInMultiOrPipeline();
return connection.executeCommand(commandObjects.sintercard(keys));
}

/**
* This command works exactly like {@link Jedis#sinter(String[]) SINTER} but instead of returning
* the result set, it returns just the cardinality of the result.
* <p>
* Time complexity O(N*M) worst case where N is the cardinality of the smallest
* @param limit If the intersection cardinality reaches limit partway through the computation,
* the algorithm will exit and yield limit as the cardinality.
* @param keys
* @return The cardinality of the set which would result from the intersection of all the given sets
*/
@Override
public long sintercard(int limit, String... keys) {
checkIsInMultiOrPipeline();
return connection.executeCommand(commandObjects.sintercard(limit, keys));
}

/**
* Return the members of a set resulting from the union of all the sets hold at the specified
* keys. Like in {@link Jedis#lrange(String, long, long) LRANGE} the result is sent to the
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/redis/clients/jedis/MultiNodePipelineBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,16 @@ public Response<Long> sinterstore(String dstKey, String... keys) {
return appendCommand(commandObjects.sinterstore(dstKey, keys));
}

@Override
public Response<Long> sintercard(String... keys){
return appendCommand(commandObjects.sintercard(keys));
}

@Override
public Response<Long> sintercard(int limit, String... keys){
return appendCommand(commandObjects.sintercard(limit, keys));
}

@Override
public Response<Set<String>> sunion(String... keys) {
return appendCommand(commandObjects.sunion(keys));
Expand Down Expand Up @@ -2265,6 +2275,16 @@ public Response<Long> sinterstore(byte[] dstkey, byte[]... keys) {
return appendCommand(commandObjects.sinterstore(dstkey, keys));
}

@Override
public Response<Long> sintercard(byte[]... keys){
return appendCommand(commandObjects.sintercard(keys));
}

@Override
public Response<Long> sintercard(int limit, byte[]... keys){
return appendCommand(commandObjects.sintercard(limit, keys));
}

@Override
public Response<Set<byte[]>> sunion(byte[]... keys) {
return appendCommand(commandObjects.sunion(keys));
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/redis/clients/jedis/Pipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,16 @@ public Response<Long> sinterstore(String dstKey, String... keys) {
return appendCommand(commandObjects.sinterstore(dstKey, keys));
}

@Override
public Response<Long> sintercard(String... keys) {
return appendCommand(commandObjects.sintercard(keys));
}

@Override
public Response<Long> sintercard(int limit, String... keys) {
return appendCommand(commandObjects.sintercard(limit, keys));
}

@Override
public Response<Set<String>> sunion(String... keys) {
return appendCommand(commandObjects.sunion(keys));
Expand Down Expand Up @@ -2275,6 +2285,16 @@ public Response<Long> sinterstore(byte[] dstkey, byte[]... keys) {
return appendCommand(commandObjects.sinterstore(dstkey, keys));
}

@Override
public Response<Long> sintercard(byte[]... keys) {
return appendCommand(commandObjects.sintercard(keys));
}

@Override
public Response<Long> sintercard(int limit, byte[]... keys) {
return appendCommand(commandObjects.sintercard(limit, keys));
}

@Override
public Response<Set<byte[]>> sunion(byte[]... keys) {
return appendCommand(commandObjects.sunion(keys));
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 @@ -235,7 +235,7 @@ public static enum Command implements ProtocolCommand {
GEORADIUSBYMEMBER, GEORADIUSBYMEMBER_RO, BITFIELD, HSTRLEN, TOUCH, SWAPDB, MEMORY, XADD, XLEN,
XDEL, XTRIM, XRANGE, XREVRANGE, XREAD, XACK, XGROUP, XREADGROUP, XPENDING, XCLAIM, XAUTOCLAIM,
XINFO, BITFIELD_RO, LPOS, ZMSCORE, BZPOPMIN, BZPOPMAX, BLMOVE, LMOVE, COPY, ROLE, FAILOVER,
STRALGO, GEOSEARCH, GEOSEARCHSTORE, LOLWUT, REPLICAOF, ZRANGESTORE;
STRALGO, GEOSEARCH, GEOSEARCHSTORE, LOLWUT, REPLICAOF, ZRANGESTORE, SINTERCARD;

private final byte[] raw;

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/redis/clients/jedis/TransactionBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,16 @@ public Response<Long> sinterstore(String dstKey, String... keys) {
return appendCommand(commandObjects.sinterstore(dstKey, keys));
}

@Override
public Response<Long> sintercard(String... keys) {
return appendCommand(commandObjects.sintercard(keys));
}

@Override
public Response<Long> sintercard(int limit, String... keys) {
return appendCommand(commandObjects.sintercard(limit, keys));
}

@Override
public Response<Set<String>> sunion(String... keys) {
return appendCommand(commandObjects.sunion(keys));
Expand Down Expand Up @@ -2319,6 +2329,16 @@ public Response<Long> sinterstore(byte[] dstkey, byte[]... keys) {
return appendCommand(commandObjects.sinterstore(dstkey, keys));
}

@Override
public Response<Long> sintercard(byte[]... keys) {
return appendCommand(commandObjects.sintercard(keys));
}

@Override
public Response<Long> sintercard(int limit, byte[]... keys) {
return appendCommand(commandObjects.sintercard(limit, keys));
}

@Override
public Response<Set<byte[]>> sunion(byte[]... keys) {
return appendCommand(commandObjects.sunion(keys));
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/redis/clients/jedis/UnifiedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,16 @@ public long sinterstore(String dstkey, String... keys) {
return executeCommand(commandObjects.sinterstore(dstkey, keys));
}

@Override
public long sintercard(String... keys) {
return executeCommand(commandObjects.sintercard(keys));
}

@Override
public long sintercard(int limit, String... keys) {
return executeCommand(commandObjects.sintercard(limit, keys));
}

@Override
public Set<String> sunion(String... keys) {
return executeCommand(commandObjects.sunion(keys));
Expand Down Expand Up @@ -1447,6 +1457,16 @@ public long sinterstore(byte[] dstkey, byte[]... keys) {
return executeCommand(commandObjects.sinterstore(dstkey, keys));
}

@Override
public long sintercard(byte[]... keys) {
return executeCommand(commandObjects.sintercard(keys));
}

@Override
public long sintercard(int limit, byte[]... keys) {
return executeCommand(commandObjects.sintercard(limit, keys));
}

@Override
public Set<byte[]> sunion(byte[]... keys) {
return executeCommand(commandObjects.sunion(keys));
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/redis/clients/jedis/commands/SetBinaryCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,28 @@ default ScanResult<byte[]> sscan(byte[] key, byte[] cursor) {

long sinterstore(byte[] dstkey, byte[]... keys);

/**
* This command works exactly like {@link SetBinaryCommands#sinter(byte[][]) SINTER} but instead of returning
* the result set, it returns just the cardinality of the result. LIMIT defaults to 0 and means unlimited
* <p>
* Time complexity O(N*M) worst case where N is the cardinality of the smallest
* @param keys
* @return The cardinality of the set which would result from the intersection of all the given sets
*/
long sintercard(byte[]... keys);

/**
* This command works exactly like {@link SetBinaryCommands#sinter(byte[][]) SINTER} but instead of returning
* the result set, it returns just the cardinality of the result.
* <p>
* Time complexity O(N*M) worst case where N is the cardinality of the smallest
* @param limit If the intersection cardinality reaches limit partway through the computation,
* the algorithm will exit and yield limit as the cardinality.
* @param keys
* @return The cardinality of the set which would result from the intersection of all the given sets
*/
long sintercard(int limit, byte[]... keys);

Set<byte[]> sunion(byte[]... keys);

long sunionstore(byte[] dstkey, byte[]... keys);
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/redis/clients/jedis/commands/SetCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,28 @@ default ScanResult<String> sscan(String key, String cursor) {

long sinterstore(String dstkey, String... keys);

/**
* This command works exactly like {@link SetCommands#sinter(String[]) SINTER} but instead of returning
* the result set, it returns just the cardinality of the result. LIMIT defaults to 0 and means unlimited
* <p>
* Time complexity O(N*M) worst case where N is the cardinality of the smallest
* @param keys
* @return The cardinality of the set which would result from the intersection of all the given sets
*/
long sintercard(String... keys);

/**
* This command works exactly like {@link SetCommands#sinter(String[]) SINTER} but instead of returning
* the result set, it returns just the cardinality of the result.
* <p>
* Time complexity O(N*M) worst case where N is the cardinality of the smallest
* @param limit If the intersection cardinality reaches limit partway through the computation,
* the algorithm will exit and yield limit as the cardinality.
* @param keys
* @return The cardinality of the set which would result from the intersection of all the given sets
*/
long sintercard(int limit, String... keys);

Set<String> sunion(String... keys);

long sunionstore(String dstkey, String... keys);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ default Response<ScanResult<byte[]>> sscan(byte[] key, byte[] cursor) {

Response<Long> sinterstore(byte[] dstkey, byte[]... keys);

Response<Long> sintercard(byte[]... keys);

Response<Long> sintercard(int limit, byte[]... keys);

Response<Set<byte[]>> sunion(byte[]... keys);

Response<Long> sunionstore(byte[] dstkey, byte[]... keys);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ default Response<ScanResult<String>> sscan(String key, String cursor) {

Response<Long> sinterstore(String dstKey, String... keys);

Response<Long> sintercard(String... keys);

Response<Long> sintercard(int limit, String... keys);

Response<Set<String>> sunion(String... keys);

Response<Long> sunionstore(String dstKey, String... keys);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,34 @@ public void sinterstore() {

}

@Test
public void sintercard() {
jedis.sadd("foo", "a");
jedis.sadd("foo", "b");

jedis.sadd("bar", "a");
jedis.sadd("bar", "b");
jedis.sadd("bar", "c");

long card = jedis.sintercard("foo", "bar");
assertEquals(2, card);
long limitedCard = jedis.sintercard(1, "foo", "bar");
assertEquals(1, limitedCard);

// Binary
jedis.sadd(bfoo, ba);
jedis.sadd(bfoo, bb);

jedis.sadd(bbar, ba);
jedis.sadd(bbar, bb);
jedis.sadd(bbar, bc);

long bcard = jedis.sintercard(bfoo, bbar);
assertEquals(2, bcard);
long blimitedCard = jedis.sintercard(1, bfoo, bbar);
assertEquals(1, blimitedCard);
}

@Test
public void sunion() {
jedis.sadd("foo", "a");
Expand Down

0 comments on commit 4d1de18

Please sign in to comment.