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

Support SINTERCARD Command #2821

Merged
merged 7 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ default ScanResult<byte[]> sscan(byte[] key, byte[] cursor) {

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

long sintercard(byte[]... keys);

long sintercard(int limit, byte[]... keys);

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

long sunionstore(byte[] dstkey, byte[]... keys);
Expand Down
4 changes: 4 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,10 @@ default ScanResult<String> sscan(String key, String cursor) {

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

long sintercard(String... keys);
AvitalFineRedis marked this conversation as resolved.
Show resolved Hide resolved

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
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