Skip to content

Commit 66c8556

Browse files
denglimingsazzad16
andauthored
Add support INCR argument to ZADD command (#2415)
* Add support INCR argument to ZADD command * Fix review * Add cluster command and fix review Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com>
1 parent 844477c commit 66c8556

File tree

15 files changed

+213
-134
lines changed

15 files changed

+213
-134
lines changed

src/main/java/redis/clients/jedis/BinaryClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,10 @@ public void zadd(final byte[] key, final Map<byte[], Double> scoreMembers, final
566566
sendCommand(ZADD, params.getByteParams(key, argsArray));
567567
}
568568

569+
public void zaddIncr(final byte[] key, final double score, final byte[] member, final ZAddParams params) {
570+
sendCommand(ZADD, params.getByteParams(key, INCR.getRaw(), toByteArray(score), member));
571+
}
572+
569573
public void zrange(final byte[] key, final long start, final long stop) {
570574
sendCommand(ZRANGE, key, toByteArray(start), toByteArray(stop));
571575
}

src/main/java/redis/clients/jedis/BinaryJedis.java

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,14 +1840,14 @@ public Long sunionstore(final byte[] dstkey, final byte[]... keys) {
18401840
* Return the difference between the Set stored at key1 and all the Sets key2, ..., keyN
18411841
* <p>
18421842
* <b>Example:</b>
1843-
*
1843+
*
18441844
* <pre>
18451845
* key1 = [x, a, b, c]
18461846
* key2 = [c]
18471847
* key3 = [a, d]
18481848
* SDIFF key1,key2,key3 =&gt; [x, b]
18491849
* </pre>
1850-
*
1850+
*
18511851
* Non existing keys are considered like empty sets.
18521852
* <p>
18531853
* <b>Time complexity:</b>
@@ -1947,6 +1947,13 @@ public Long zadd(final byte[] key, final Map<byte[], Double> scoreMembers, final
19471947
return client.getIntegerReply();
19481948
}
19491949

1950+
@Override
1951+
public Double zaddIncr(final byte[] key, final double score, final byte[] member, final ZAddParams params) {
1952+
checkIsInMultiOrPipeline();
1953+
client.zaddIncr(key, score, member, params);
1954+
return BuilderFactory.DOUBLE.build(client.getOne());
1955+
}
1956+
19501957
@Override
19511958
public Set<byte[]> zrange(final byte[] key, final long start, final long stop) {
19521959
checkIsInMultiOrPipeline();
@@ -2205,65 +2212,65 @@ public List<byte[]> sort(final byte[] key) {
22052212
* <b>examples:</b>
22062213
* <p>
22072214
* Given are the following sets and key/values:
2208-
*
2215+
*
22092216
* <pre>
22102217
* x = [1, 2, 3]
22112218
* y = [a, b, c]
2212-
*
2219+
*
22132220
* k1 = z
22142221
* k2 = y
22152222
* k3 = x
2216-
*
2223+
*
22172224
* w1 = 9
22182225
* w2 = 8
22192226
* w3 = 7
22202227
* </pre>
2221-
*
2228+
*
22222229
* Sort Order:
2223-
*
2230+
*
22242231
* <pre>
22252232
* sort(x) or sort(x, sp.asc())
22262233
* -&gt; [1, 2, 3]
2227-
*
2234+
*
22282235
* sort(x, sp.desc())
22292236
* -&gt; [3, 2, 1]
2230-
*
2237+
*
22312238
* sort(y)
22322239
* -&gt; [c, a, b]
2233-
*
2240+
*
22342241
* sort(y, sp.alpha())
22352242
* -&gt; [a, b, c]
2236-
*
2243+
*
22372244
* sort(y, sp.alpha().desc())
22382245
* -&gt; [c, a, b]
22392246
* </pre>
2240-
*
2247+
*
22412248
* Limit (e.g. for Pagination):
2242-
*
2249+
*
22432250
* <pre>
22442251
* sort(x, sp.limit(0, 2))
22452252
* -&gt; [1, 2]
2246-
*
2253+
*
22472254
* sort(y, sp.alpha().desc().limit(1, 2))
22482255
* -&gt; [b, a]
22492256
* </pre>
2250-
*
2257+
*
22512258
* Sorting by external keys:
2252-
*
2259+
*
22532260
* <pre>
22542261
* sort(x, sb.by(w*))
22552262
* -&gt; [3, 2, 1]
2256-
*
2263+
*
22572264
* sort(x, sb.by(w*).desc())
22582265
* -&gt; [1, 2, 3]
22592266
* </pre>
2260-
*
2267+
*
22612268
* Getting external keys:
2262-
*
2269+
*
22632270
* <pre>
22642271
* sort(x, sp.by(w*).get(k*))
22652272
* -&gt; [x, y, z]
2266-
*
2273+
*
22672274
* sort(x, sp.by(w*).get(#).get(k*))
22682275
* -&gt; [3, x, 2, y, 1, z]
22692276
* </pre>
@@ -3198,7 +3205,7 @@ public String shutdown() {
31983205
* <b>Format of the returned String:</b>
31993206
* <p>
32003207
* All the fields are in the form field:value
3201-
*
3208+
*
32023209
* <pre>
32033210
* edis_version:0.07
32043211
* connected_clients:1
@@ -3211,7 +3218,7 @@ public String shutdown() {
32113218
* uptime_in_seconds:25
32123219
* uptime_in_days:0
32133220
* </pre>
3214-
*
3221+
*
32153222
* <b>Notes</b>
32163223
* <p>
32173224
* used_memory is returned in bytes, and is the total number of bytes allocated by the program
@@ -3293,7 +3300,7 @@ public String slaveofNoOne() {
32933300
* are reported as a list of key-value pairs.
32943301
* <p>
32953302
* <b>Example:</b>
3296-
*
3303+
*
32973304
* <pre>
32983305
* $ redis-cli config get '*'
32993306
* 1. "dbfilename"
@@ -3308,7 +3315,7 @@ public String slaveofNoOne() {
33083315
* 10. "everysec"
33093316
* 11. "save"
33103317
* 12. "3600 1 300 100 60 10000"
3311-
*
3318+
*
33123319
* $ redis-cli config get 'm*'
33133320
* 1. "masterauth"
33143321
* 2. (nil)

src/main/java/redis/clients/jedis/BinaryJedisCluster.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,16 @@ public Long execute(Jedis connection) {
931931
}.runBinary(key);
932932
}
933933

934+
@Override
935+
public Double zaddIncr(byte[] key, double score, byte[] member, ZAddParams params) {
936+
return new JedisClusterCommand<Double>(connectionHandler, maxAttempts) {
937+
@Override
938+
public Double execute(Jedis connection) {
939+
return connection.zaddIncr(key, score, member, params);
940+
}
941+
}.runBinary(key);
942+
}
943+
934944
@Override
935945
public Set<byte[]> zrange(final byte[] key, final long start, final long stop) {
936946
return new JedisClusterCommand<Set<byte[]>>(connectionHandler, maxAttempts) {

src/main/java/redis/clients/jedis/BinaryShardedJedis.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,12 @@ public Long zadd(final byte[] key, final Map<byte[], Double> scoreMembers, final
525525
return j.zadd(key, scoreMembers, params);
526526
}
527527

528+
@Override
529+
public Double zaddIncr(final byte[] key, final double score, final byte[] member, final ZAddParams params) {
530+
Jedis j = getShard(key);
531+
return j.zaddIncr(key, score, member, params);
532+
}
533+
528534
@Override
529535
public Set<byte[]> zrange(final byte[] key, final long start, final long stop) {
530536
Jedis j = getShard(key);

src/main/java/redis/clients/jedis/Client.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,11 @@ public void zadd(final String key, final Map<String, Double> scoreMembers, final
470470
zadd(SafeEncoder.encode(key), binaryScoreMembers, params);
471471
}
472472

473+
@Override
474+
public void zaddIncr(final String key, final double score, final String member, final ZAddParams params) {
475+
zaddIncr(SafeEncoder.encode(key), score, SafeEncoder.encode(member), params);
476+
}
477+
473478
@Override
474479
public void zrange(final String key, final long start, final long stop) {
475480
zrange(SafeEncoder.encode(key), start, stop);
@@ -1399,14 +1404,14 @@ public void xreadGroup(String groupname, String consumer, int count, long block,
13991404
for (final Entry<String, StreamEntryID> entry : streams) {
14001405
bhash.put(SafeEncoder.encode(entry.getKey()), SafeEncoder.encode(entry.getValue()==null ? ">" : entry.getValue().toString()));
14011406
}
1402-
xreadGroup(SafeEncoder.encode(groupname), SafeEncoder.encode(consumer), count, block, noAck, bhash);
1407+
xreadGroup(SafeEncoder.encode(groupname), SafeEncoder.encode(consumer), count, block, noAck, bhash);
14031408
}
14041409

14051410
@Override
14061411
public void xpending(String key, String groupname, StreamEntryID start, StreamEntryID end,
14071412
int count, String consumername) {
14081413
xpending(SafeEncoder.encode(key), SafeEncoder.encode(groupname), SafeEncoder.encode(start==null ? "-" : start.toString()),
1409-
SafeEncoder.encode(end==null ? "+" : end.toString()), count, consumername == null? null : SafeEncoder.encode(consumername));
1414+
SafeEncoder.encode(end==null ? "+" : end.toString()), count, consumername == null? null : SafeEncoder.encode(consumername));
14101415
}
14111416

14121417
@Override
@@ -1417,7 +1422,7 @@ public void xclaim(String key, String group, String consumername, long minIdleTi
14171422
for (int i = 0; i < ids.length; i++) {
14181423
bids[i] = SafeEncoder.encode(ids[i].toString());
14191424
}
1420-
xclaim(SafeEncoder.encode(key), SafeEncoder.encode(group), SafeEncoder.encode(consumername), minIdleTime, newIdleTime, retries, force, bids);
1425+
xclaim(SafeEncoder.encode(key), SafeEncoder.encode(group), SafeEncoder.encode(consumername), minIdleTime, newIdleTime, retries, force, bids);
14211426
}
14221427

14231428
@Override

src/main/java/redis/clients/jedis/Jedis.java

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,14 +1523,14 @@ public Long sunionstore(final String dstkey, final String... keys) {
15231523
* Return the difference between the Set stored at key1 and all the Sets key2, ..., keyN
15241524
* <p>
15251525
* <b>Example:</b>
1526-
*
1526+
*
15271527
* <pre>
15281528
* key1 = [x, a, b, c]
15291529
* key2 = [c]
15301530
* key3 = [a, d]
15311531
* SDIFF key1,key2,key3 =&gt; [x, b]
15321532
* </pre>
1533-
*
1533+
*
15341534
* Non existing keys are considered like empty sets.
15351535
* <p>
15361536
* <b>Time complexity:</b>
@@ -1630,6 +1630,13 @@ public Long zadd(final String key, final Map<String, Double> scoreMembers, final
16301630
return client.getIntegerReply();
16311631
}
16321632

1633+
@Override
1634+
public Double zaddIncr(final String key, final double score, final String member, final ZAddParams params) {
1635+
checkIsInMultiOrPipeline();
1636+
client.zaddIncr(key, score, member, params);
1637+
return BuilderFactory.DOUBLE.build(client.getOne());
1638+
}
1639+
16331640
@Override
16341641
public Set<String> zrange(final String key, final long start, final long stop) {
16351642
checkIsInMultiOrPipeline();
@@ -1866,65 +1873,65 @@ public List<String> sort(final String key) {
18661873
* <b>examples:</b>
18671874
* <p>
18681875
* Given are the following sets and key/values:
1869-
*
1876+
*
18701877
* <pre>
18711878
* x = [1, 2, 3]
18721879
* y = [a, b, c]
1873-
*
1880+
*
18741881
* k1 = z
18751882
* k2 = y
18761883
* k3 = x
1877-
*
1884+
*
18781885
* w1 = 9
18791886
* w2 = 8
18801887
* w3 = 7
18811888
* </pre>
1882-
*
1889+
*
18831890
* Sort Order:
1884-
*
1891+
*
18851892
* <pre>
18861893
* sort(x) or sort(x, sp.asc())
18871894
* -&gt; [1, 2, 3]
1888-
*
1895+
*
18891896
* sort(x, sp.desc())
18901897
* -&gt; [3, 2, 1]
1891-
*
1898+
*
18921899
* sort(y)
18931900
* -&gt; [c, a, b]
1894-
*
1901+
*
18951902
* sort(y, sp.alpha())
18961903
* -&gt; [a, b, c]
1897-
*
1904+
*
18981905
* sort(y, sp.alpha().desc())
18991906
* -&gt; [c, a, b]
19001907
* </pre>
1901-
*
1908+
*
19021909
* Limit (e.g. for Pagination):
1903-
*
1910+
*
19041911
* <pre>
19051912
* sort(x, sp.limit(0, 2))
19061913
* -&gt; [1, 2]
1907-
*
1914+
*
19081915
* sort(y, sp.alpha().desc().limit(1, 2))
19091916
* -&gt; [b, a]
19101917
* </pre>
1911-
*
1918+
*
19121919
* Sorting by external keys:
1913-
*
1920+
*
19141921
* <pre>
19151922
* sort(x, sb.by(w*))
19161923
* -&gt; [3, 2, 1]
1917-
*
1924+
*
19181925
* sort(x, sb.by(w*).desc())
19191926
* -&gt; [1, 2, 3]
19201927
* </pre>
1921-
*
1928+
*
19221929
* Getting external keys:
1923-
*
1930+
*
19241931
* <pre>
19251932
* sort(x, sp.by(w*).get(k*))
19261933
* -&gt; [x, y, z]
1927-
*
1934+
*
19281935
* sort(x, sp.by(w*).get(#).get(k*))
19291936
* -&gt; [3, x, 2, y, 1, z]
19301937
* </pre>
@@ -2866,7 +2873,7 @@ public Long bitpos(final String key, final boolean value, final BitPosParams par
28662873
* are reported as a list of key-value pairs.
28672874
* <p>
28682875
* <b>Example:</b>
2869-
*
2876+
*
28702877
* <pre>
28712878
* $ redis-cli config get '*'
28722879
* 1. "dbfilename"
@@ -2881,7 +2888,7 @@ public Long bitpos(final String key, final boolean value, final BitPosParams par
28812888
* 10. "everysec"
28822889
* 11. "save"
28832890
* 12. "3600 1 300 100 60 10000"
2884-
*
2891+
*
28852892
* $ redis-cli config get 'm*'
28862893
* 1. "masterauth"
28872894
* 2. (nil)
@@ -3131,7 +3138,7 @@ public Long bitop(final BitOP op, final String destKey, final String... srcKeys)
31313138
* 22) "2"
31323139
* 23) "quorum"
31333140
* 24) "2"
3134-
*
3141+
*
31353142
* </pre>
31363143
* @return
31373144
*/

src/main/java/redis/clients/jedis/JedisCluster.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,16 @@ public Long execute(Jedis connection) {
10341034
}.run(key);
10351035
}
10361036

1037+
@Override
1038+
public Double zaddIncr(String key, double score, String member, ZAddParams params) {
1039+
return new JedisClusterCommand<Double>(connectionHandler, maxAttempts) {
1040+
@Override
1041+
public Double execute(Jedis connection) {
1042+
return connection.zaddIncr(key, score, member, params);
1043+
}
1044+
}.run(key);
1045+
}
1046+
10371047
@Override
10381048
public Set<String> zrange(final String key, final long start, final long stop) {
10391049
return new JedisClusterCommand<Set<String>>(connectionHandler, maxAttempts) {

src/main/java/redis/clients/jedis/Protocol.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public static enum Keyword {
282282
GETNAME, SETNAME, LIST, MATCH, COUNT, PING, PONG, UNLOAD, REPLACE, KEYS, PAUSE, DOCTOR, BLOCK,
283283
NOACK, STREAMS, KEY, CREATE, MKSTREAM, SETID, DESTROY, DELCONSUMER, MAXLEN, GROUP, ID, IDLE,
284284
TIME, RETRYCOUNT, FORCE, USAGE, SAMPLES, STREAM, GROUPS, CONSUMERS, HELP, FREQ, SETUSER,
285-
GETUSER, DELUSER, WHOAMI, CAT, GENPASS, USERS, LOG;
285+
GETUSER, DELUSER, WHOAMI, CAT, GENPASS, USERS, LOG, INCR;
286286

287287
/**
288288
* @deprecated This will be private in future. Use {@link #getRaw()}.

0 commit comments

Comments
 (0)