Skip to content

fix: xreadgroup command keys parsing #31

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

Merged
merged 3 commits into from
May 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 66 additions & 16 deletions commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"bgrewriteaof": {
"arity": 1,
"flags": [
"admin"
"admin",
"noscript"
],
"keyStart": 0,
"keyStop": 0,
Expand All @@ -42,7 +43,8 @@
"bgsave": {
"arity": -1,
"flags": [
"admin"
"admin",
"noscript"
],
"keyStart": 0,
"keyStop": 0,
Expand Down Expand Up @@ -161,6 +163,7 @@
"command": {
"arity": 1,
"flags": [
"random",
"loading",
"stale"
],
Expand All @@ -172,6 +175,7 @@
"arity": -2,
"flags": [
"admin",
"noscript",
"loading",
"stale"
],
Expand Down Expand Up @@ -243,7 +247,8 @@
"dump": {
"arity": 2,
"flags": [
"readonly"
"readonly",
"random"
],
"keyStart": 1,
"keyStop": 1,
Expand Down Expand Up @@ -485,7 +490,8 @@
"hgetall": {
"arity": 2,
"flags": [
"readonly"
"readonly",
"random"
],
"keyStart": 1,
"keyStop": 1,
Expand Down Expand Up @@ -652,6 +658,7 @@
"info": {
"arity": -1,
"flags": [
"random",
"loading",
"stale"
],
Expand Down Expand Up @@ -720,6 +727,15 @@
"keyStop": 1,
"step": 1
},
"lolwut": {
"arity": -1,
"flags": [
"readonly"
],
"keyStart": 0,
"keyStop": 0,
"step": 0
},
"lpop": {
"arity": 2,
"flags": [
Expand Down Expand Up @@ -792,7 +808,8 @@
"memory": {
"arity": -2,
"flags": [
"readonly"
"readonly",
"random"
],
"keyStart": 0,
"keyStop": 0,
Expand All @@ -812,6 +829,7 @@
"arity": -6,
"flags": [
"write",
"random",
"movablekeys"
],
"keyStart": 0,
Expand Down Expand Up @@ -881,7 +899,8 @@
"object": {
"arity": -2,
"flags": [
"readonly"
"readonly",
"random"
],
"keyStart": 2,
"keyStop": 2,
Expand Down Expand Up @@ -1022,6 +1041,7 @@
"arity": 2,
"flags": [
"readonly",
"random",
"fast"
],
"keyStart": 1,
Expand Down Expand Up @@ -1134,6 +1154,17 @@
"keyStop": 0,
"step": 0
},
"replicaof": {
"arity": 3,
"flags": [
"admin",
"noscript",
"stale"
],
"keyStart": 0,
"keyStop": 0,
"step": 0
},
"restore": {
"arity": -4,
"flags": [
Expand Down Expand Up @@ -1343,6 +1374,7 @@
"arity": -1,
"flags": [
"admin",
"noscript",
"loading",
"stale"
],
Expand Down Expand Up @@ -1394,7 +1426,8 @@
"slowlog": {
"arity": -2,
"flags": [
"admin"
"admin",
"random"
],
"keyStart": 0,
"keyStop": 0,
Expand Down Expand Up @@ -1568,6 +1601,7 @@
"arity": 2,
"flags": [
"readonly",
"random",
"fast"
],
"keyStart": 1,
Expand Down Expand Up @@ -1636,7 +1670,7 @@
"step": 1
},
"xack": {
"arity": -3,
"arity": -4,
"flags": [
"write",
"fast"
Expand All @@ -1650,24 +1684,26 @@
"flags": [
"write",
"denyoom",
"random",
"fast"
],
"keyStart": 1,
"keyStop": 1,
"step": 1
},
"xclaim": {
"arity": -5,
"arity": -6,
"flags": [
"write",
"random",
"fast"
],
"keyStart": 1,
"keyStop": 1,
"step": 1
},
"xdel": {
"arity": -2,
"arity": -3,
"flags": [
"write",
"fast"
Expand All @@ -1689,7 +1725,8 @@
"xinfo": {
"arity": -2,
"flags": [
"readonly"
"readonly",
"random"
],
"keyStart": 2,
"keyStop": 2,
Expand All @@ -1708,7 +1745,8 @@
"xpending": {
"arity": -3,
"flags": [
"readonly"
"readonly",
"random"
],
"keyStart": 1,
"keyStop": 1,
Expand All @@ -1724,7 +1762,7 @@
"step": 1
},
"xread": {
"arity": -3,
"arity": -4,
"flags": [
"readonly",
"noscript",
Expand All @@ -1735,7 +1773,7 @@
"step": 1
},
"xreadgroup": {
"arity": -3,
"arity": -7,
"flags": [
"write",
"noscript",
Expand All @@ -1754,10 +1792,22 @@
"keyStop": 1,
"step": 1
},
"xsetid": {
"arity": 3,
"flags": [
"write",
"denyoom",
"fast"
],
"keyStart": 1,
"keyStop": 1,
"step": 1
},
"xtrim": {
"arity": -2,
"flags": [
"write",
"random",
"fast"
],
"keyStart": 1,
Expand Down Expand Up @@ -1834,7 +1884,7 @@
"fast"
],
"keyStart": 1,
"keyStop": -1,
"keyStop": 1,
"step": 1
},
"zpopmin": {
Expand All @@ -1844,7 +1894,7 @@
"fast"
],
"keyStart": 1,
"keyStop": -1,
"keyStop": 1,
"step": 1
},
"zrange": {
Expand Down
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ exports.getKeyIndexes = function (commandName, args, options) {
keys.push(2)
}
break
case 'xreadgroup':
case 'xread':
// keys are 1st half of the args after STREAMS argument
for (i = commandName === 'xread' ? 0 : 3; i < args.length - 1; i++) {
if (String(args[i]).toUpperCase() === 'STREAMS') {

for (j = i + 1; j <= i + ((args.length - 1 - i) / 2); j++) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this!

nitpicky microoptimisation -- for xread you need to start at 0, but for xreadgroup you can skip the GROUP group consumer arguments. This almost certainly doesn't matter, but thought I'd mention it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I was considering it, but it should be another if or another case... So i left it like it is :-).

Copy link
Member

@BridgeAR BridgeAR May 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be implemented in line 136 as:

      for (i = commandName === 'xread' ? 0 : 3; i < args.length - 1; i++) {

keys.push(j)
}
break
}
}
break
default:
// step has to be at least one in this case, otherwise the command does not contain a key
if (command.step > 0) {
Expand Down
8 changes: 8 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ describe('redis-commands', function () {
expect(index('migrate', ['127.0.0.1', 6379, 'foo', 0, 0, 'COPY'])).to.eql([2])
expect(index('migrate', ['127.0.0.1', 6379, '', 0, 0, 'REPLACE', 'KEYS', 'foo', 'bar'])).to.eql([7, 8])
expect(index('migrate', ['127.0.0.1', 6379, '', 0, 0, 'KEYS', 'foo', 'bar'])).to.eql([6, 7])
expect(index('xreadgroup', ['GROUP', 'group', 'consumer', 'COUNT', 10, 'BLOCK', 2000, 'NOACK', 'STREAMS', 'key1', 'key2', 'id1', 'id2'])).to.eql([9, 10])
expect(index('xreadgroup', ['GROUP', 'group', 'consumer', 'STREAMS', 'key1', 'id1'])).to.eql([4])
expect(index('xreadgroup', ['GROUP', 'group', 'consumer', 'STREAMS', 'key1', 'key2', 'id1', 'id2'])).to.eql([4, 5])
expect(index('xreadgroup', ['GROUP', 'group', 'consumer', 'STREAMS', 'key1', 'key2', 'key3', 'id1', 'id2', 'id3'])).to.eql([4, 5, 6])
expect(index('xread', ['COUNT', 10, 'BLOCK', 2000, 'STREAMS', 'key1', 'key2', 'id1', 'id2'])).to.eql([5, 6])
expect(index('xread', ['STREAMS', 'key1', 'id1'])).to.eql([1])
expect(index('xread', ['STREAMS', 'key1', 'key2', 'id1', 'id2'])).to.eql([1, 2])
expect(index('xread', ['STREAMS', 'key1', 'key2', 'key3', 'id1', 'id2', 'id3'])).to.eql([1, 2, 3])
})

it('should support numeric argument', function () {
Expand Down