Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.

Commit 7823c6a

Browse files
Merge pull request #1712 from DiceDB/arpitbbhayani
Z* Commands now fetch Rank in all cases
2 parents 85064dd + e8081d4 commit 7823c6a

File tree

17 files changed

+186
-132
lines changed

17 files changed

+186
-132
lines changed

docs/src/content/docs/commands/ZPOPMAX.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,24 @@ ZPOPMAX removes and returns the member with the highest score from the sorted se
1919

2020
If the key does not exist, the command returns empty list. An optional "count" argument can be provided
2121
to remove and return multiple members (up to the number specified).
22+
23+
When popped, the elements are returned in descending order of score and you get the rank of the element in the sorted set.
24+
The rank is 1-based, which means that the first element is at rank 1 and not rank 0.
25+
The 1), 2), 3), ... is the rank of the element in the sorted set.
2226

2327

2428
#### Examples
2529

2630
```
2731
28-
localhost:7379> ZADD users 1 alice
29-
OK 1
30-
localhost:7379> ZADD users 2 bob
31-
OK 1
32-
localhost:7379> ZADD users 3 charlie
33-
OK 1
32+
localhost:7379> ZADD users 10 alice 20 bob 30 charlie
33+
OK 3
3434
localhost:7379> ZPOPMAX users
3535
OK
36-
0) 3, charlie
36+
3) 30, charlie
3737
localhost:7379> ZPOPMAX users 10
3838
OK
39-
0) 2, bob
40-
1) 1, alice
39+
2) 20, bob
40+
1) 10, alice
4141
4242
```

docs/src/content/docs/commands/ZPOPMIN.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,24 @@ ZPOPMIN removes and returns the member with the lowest score from the sorted set
1919

2020
If the key does not exist, the command returns empty list. An optional "count" argument can be provided
2121
to remove and return multiple members (up to the number specified).
22+
23+
When popped, the elements are returned in ascending order of score and you get the rank of the element in the sorted set.
24+
The rank is 1-based, which means that the first element is at rank 1 and not rank 0.
25+
The 1), 2), 3), ... is the rank of the element in the sorted set.
2226

2327

2428
#### Examples
2529

2630
```
2731
28-
localhost:7379> ZADD users 1 alice
29-
OK 1
30-
localhost:7379> ZADD users 2 bob
31-
OK 1
32-
localhost:7379> ZADD users 3 charlie
33-
OK 1
32+
localhost:7379> ZADD users 10 alice 20 bob 30 charlie
33+
OK 3
3434
localhost:7379> ZPOPMIN users
3535
OK
36-
0) 1, alice
36+
1) 10, alice
3737
localhost:7379> ZPOPMIN users 10
3838
OK
39-
0) 2, bob
40-
1) 3, charlie
39+
1) 20, bob
40+
2) 30, charlie
4141
4242
```

docs/src/content/docs/commands/ZRANGE.WATCH.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ description: ZRANGE.WATCH creates a query subscription over the ZRANGE command
1111
#### Syntax
1212

1313
```
14-
ZRANGE.WATCH key start stop
14+
ZRANGE.WATCH key start stop [BYSCORE | BYRANK]
1515
```
1616

1717

@@ -26,22 +26,22 @@ You can update the key in any other client. The ZRANGE.WATCH client will receive
2626

2727
```
2828
29-
client1:7379> ZADD users 1 alice 2 bob 3 charlie
29+
client1:7379> ZADD users 10 alice 20 bob 30 charlie
3030
OK 3
3131
client1:7379> ZRANGE.WATCH users 1 5
3232
entered the watch mode for ZRANGE.WATCH users
3333
3434
35-
client2:7379> ZADD users 4 daniel
35+
client2:7379> ZADD users 40 daniel
3636
OK 1
3737
3838
3939
client1:7379> ...
4040
entered the watch mode for ZRANGE.WATCH users
4141
OK [fingerprint=1007898011883907067]
42-
0) 1, alice
43-
1) 2, bob
44-
2) 3, charlie
45-
3) 4, daniel
42+
1) 10, alice
43+
2) 20, bob
44+
3) 30, charlie
45+
4) 40, daniel
4646
4747
```

docs/src/content/docs/commands/ZRANGE.md

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,43 @@ description: ZRANGE returns the range of elements from the sorted set stored at
1111
#### Syntax
1212

1313
```
14-
ZRANGE key start stop
14+
ZRANGE key start stop [BYSCORE | BYRANK]
1515
```
1616

1717

1818
ZRANGE returns the range of elements from the sorted set stored at key.
1919

20-
The elements are considered to be ordered from the lowest to the highest score. Both start and
21-
stop are 0-based indexes, where 0 is the first element, 1 is the next element and so on.
20+
The default range is by rank "BYRANK" and this can be changed to "BYSCORE" if you want to range by score spanning the start and stop values.
21+
The rank is 1-based, which means that the first element is at rank 1 and not rank 0.
22+
The 1), 2), 3), ... is the rank of the element in the sorted set.
23+
24+
Both the start and stop values are inclusive and hence the elements having either of the values will be included. The
25+
elements are considered to be ordered from the lowest to the highest. If you want reverse order, consider
26+
storing score with flipped sign.
2227

2328
#### Examples
2429

2530
```
2631
27-
localhost:7379> ZADD s 1 a 2 b 3 c 4 d 5 e
32+
localhost:7379> ZADD s 10 a 20 b 30 c 40 d 50 e
2833
OK 5
2934
localhost:7379> ZRANGE s 1 3
3035
OK
31-
0) 1, a
32-
1) 2, b
33-
2) 3, c
36+
1) 10, a
37+
2) 20, b
38+
3) 30, c
39+
localhost:7379> ZRANGE s 1 4 BYRANK
40+
OK
41+
1) 10, a
42+
2) 20, b
43+
3) 30, c
44+
4) 40, d
45+
localhost:7379> ZRANGE s 1 3 BYSCORE
46+
OK
47+
localhost:7379> ZRANGE s 30 100 BYSCORE
48+
OK
49+
3) 30, c
50+
4) 40, d
51+
5) 50, e
3452
3553
```

docs/src/content/docs/commands/ZRANK.WATCH.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,21 @@ You can update the key in any other client. The ZRANK.WATCH client will receive
2626

2727
```
2828
29-
client1:7379> ZADD users 1 alice 2 bob 3 charlie
29+
client1:7379> ZADD users 10 alice 20 bob 30 charlie
3030
OK 3
3131
client1:7379> ZRANK.WATCH users bob
3232
entered the watch mode for ZRANK.WATCH users
3333
3434
3535
client2:7379> ZADD users 10 bob
3636
OK 0
37+
client2:7379> ZADD users 100 alice
38+
OK 0
3739
3840
3941
client1:7379> ...
4042
entered the watch mode for ZRANK.WATCH users
41-
OK [fingerprint=3262833422269415227] 3, 10, bob
43+
OK [fingerprint=3262833422269415227] 2) 10, bob
44+
OK [fingerprint=3262833422269415227] 1) 10, bob
4245
4346
```

docs/src/content/docs/commands/ZRANK.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ ZRANK key member
1717

1818
ZRANK returns the rank of a member in a sorted set, ordered from low to high scores.
1919

20-
The rank is 1-based which means that the member with the lowest score has rank 1.
21-
The command returns the element (score with member) and the rank.
20+
The rank is 1-based which means that the member with the lowest score has rank 1, the next highest has rank 2, and so on.
21+
The command returns the element - rank, score, and memeber.
22+
23+
Thus, 1), 2), 3) are the rank of the element in the sorted set, followed by the score and the member.
2224

2325
The the member passed as the second argument is not a member of the sorted set, the command returns a
2426
valid response with a rank of 0 and score of 0. If the key does not exist, the command returns a
@@ -29,17 +31,13 @@ valid response with a rank of 0, score of 0, and the member as "".
2931

3032
```
3133
32-
localhost:7379> ZADD users 20 bob
33-
OK 1
34-
localhost:7379> ZADD users 10 alice
35-
OK 1
36-
localhost:7379> ZADD users 30 charlie
37-
OK 1
34+
localhost:7379> ZADD users 10 alice 20 bob 30 charlie
35+
OK 3
3836
localhost:7379> ZRANK users bob
39-
OK 2, 20, bob
37+
OK 2) 20, bob
4038
localhost:7379> ZRANK users charlie
41-
OK 3, 30, charlie
39+
OK 3) 30, charlie
4240
localhost:7379> ZRANK users daniel
43-
OK 0, 0, daniel
41+
OK 0) 0, daniel
4442
4543
```

docs/src/content/docs/commands/ZREM.md

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,17 @@ Returns the number of members removed from the sorted set.
2424

2525
```
2626
27-
localhost:7379> ZADD ss 1 k1
28-
OK 1
29-
localhost:7379> ZADD ss 2 k2
30-
OK 1
31-
localhost:7379> ZADD ss 3 k3
32-
OK 1
33-
localhost:7379> ZRANGE ss 0 6
27+
localhost:7379> ZADD users 10 alice 20 bob 30 charlie
28+
OK 3
29+
localhost:7379> ZRANGE users 0 60 BYSCORE
3430
OK
35-
0) 1, k1
36-
1) 2, k2
37-
2) 3, k3
38-
localhost:7379> ZREM ss k1 k2 k4
31+
1) 10, alice
32+
2) 20, bob
33+
3) 30, charlie
34+
localhost:7379> ZREM users alice bob
3935
OK 2
40-
localhost:7379> ZRANGE ss 0 6
36+
localhost:7379> ZRANGE users 0 60 BYSCORE
4137
OK
42-
0) 3, k3
38+
1) 30, charlie
4339
4440
```

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ require (
4141
github.com/bytedance/sonic v1.13.1
4242
github.com/cespare/xxhash/v2 v2.3.0
4343
github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da
44-
github.com/dicedb/dicedb-go v1.0.8
44+
github.com/dicedb/dicedb-go v1.0.9
4545
github.com/gobwas/glob v0.2.3
4646
github.com/google/btree v1.1.3
4747
github.com/google/go-cmp v0.6.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da h1:aIftn67I1fkbMa5
2020
github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
2121
github.com/dgryski/go-metro v0.0.0-20211217172704-adc40b04c140 h1:y7y0Oa6UawqTFPCDw9JG6pdKt4F9pAhHv0B7FMGaGD0=
2222
github.com/dgryski/go-metro v0.0.0-20211217172704-adc40b04c140/go.mod h1:c9O8+fpSOX1DM8cPNSkX/qsBWdkD4yd2dpciOWQjpBw=
23-
github.com/dicedb/dicedb-go v1.0.8 h1:4Z1Qo9lWdOmSOKOTpV8rzinFSUgQU3ui5mx7OMBaTac=
24-
github.com/dicedb/dicedb-go v1.0.8/go.mod h1:V1fiCJnPfSObKWrOJ/zrhHEGlLwT9k3pKCto3sz1oW8=
23+
github.com/dicedb/dicedb-go v1.0.9 h1:WtXtX6Bu/KFjhnOIlnoo9Z1hbmOtL2tMrUmS6793/Pw=
24+
github.com/dicedb/dicedb-go v1.0.9/go.mod h1:V1fiCJnPfSObKWrOJ/zrhHEGlLwT9k3pKCto3sz1oW8=
2525
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
2626
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
2727
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=

internal/cmd/cmd_zpopmax.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@ ZPOPMAX removes and returns the member with the highest score from the sorted se
2323
2424
If the key does not exist, the command returns empty list. An optional "count" argument can be provided
2525
to remove and return multiple members (up to the number specified).
26+
27+
When popped, the elements are returned in descending order of score and you get the rank of the element in the sorted set.
28+
The rank is 1-based, which means that the first element is at rank 1 and not rank 0.
29+
The 1), 2), 3), ... is the rank of the element in the sorted set.
2630
`,
2731
Examples: `
28-
localhost:7379> ZADD users 1 alice
29-
OK 1
30-
localhost:7379> ZADD users 2 bob
31-
OK 1
32-
localhost:7379> ZADD users 3 charlie
33-
OK 1
32+
localhost:7379> ZADD users 10 alice 20 bob 30 charlie
33+
OK 3
3434
localhost:7379> ZPOPMAX users
3535
OK
36-
0) 3, charlie
36+
3) 30, charlie
3737
localhost:7379> ZPOPMAX users 10
3838
OK
39-
0) 2, bob
40-
1) 1, alice
39+
2) 20, bob
40+
1) 10, alice
4141
`,
4242
Eval: evalZPOPMAX,
4343
Execute: executeZPOPMAX,
@@ -97,6 +97,7 @@ func evalZPOPMAX(c *Cmd, s *dstore.Store) (*CmdRes, error) {
9797

9898
ss = obj.Value.(*types.SortedSet)
9999
elements := make([]*wire.ZElement, 0, count)
100+
totalElements := ss.SortedSet.GetCount()
100101

101102
for i := 0; i < count; i++ {
102103
n := ss.PopMax()
@@ -106,6 +107,7 @@ func evalZPOPMAX(c *Cmd, s *dstore.Store) (*CmdRes, error) {
106107
elements = append(elements, &wire.ZElement{
107108
Member: n.Key(),
108109
Score: int64(n.Score()),
110+
Rank: int64(totalElements) - int64(i),
109111
})
110112
}
111113
return newZPOPMAXRes(elements), nil

0 commit comments

Comments
 (0)