Skip to content

Commit

Permalink
Support new muilti pop commands (#2051)
Browse files Browse the repository at this point in the history
* Support new muilti pop commands

* remove .only

* clean code

* fix for 4558ec6

* fix tests

Co-authored-by: leibale <leibale1998@gmail.com>
  • Loading branch information
Avital-Fine and leibale committed Apr 25, 2022
1 parent 0f7ae93 commit b1a0b48
Show file tree
Hide file tree
Showing 15 changed files with 298 additions and 10 deletions.
12 changes: 12 additions & 0 deletions packages/client/lib/cluster/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import * as BITFIELD from '../commands/BITFIELD';
import * as BITOP from '../commands/BITOP';
import * as BITPOS from '../commands/BITPOS';
import * as BLMOVE from '../commands/BLMOVE';
import * as BLMPOP from '../commands/BLMPOP';
import * as BLPOP from '../commands/BLPOP';
import * as BRPOP from '../commands/BRPOP';
import * as BRPOPLPUSH from '../commands/BRPOPLPUSH';
import * as BZMPOP from '../commands/BZMPOP';
import * as BZPOPMAX from '../commands/BZPOPMAX';
import * as BZPOPMIN from '../commands/BZPOPMIN';
import * as COPY from '../commands/COPY';
Expand Down Expand Up @@ -59,6 +61,7 @@ import * as LINDEX from '../commands/LINDEX';
import * as LINSERT from '../commands/LINSERT';
import * as LLEN from '../commands/LLEN';
import * as LMOVE from '../commands/LMOVE';
import * as LMPOP from '../commands/LMPOP';
import * as LPOP_COUNT from '../commands/LPOP_COUNT';
import * as LPOP from '../commands/LPOP';
import * as LPOS_COUNT from '../commands/LPOS_COUNT';
Expand Down Expand Up @@ -161,6 +164,7 @@ import * as ZINTER from '../commands/ZINTER';
import * as ZINTERCARD from '../commands/ZINTERCARD';
import * as ZINTERSTORE from '../commands/ZINTERSTORE';
import * as ZLEXCOUNT from '../commands/ZLEXCOUNT';
import * as ZMPOP from '../commands/ZMPOP';
import * as ZMSCORE from '../commands/ZMSCORE';
import * as ZPOPMAX_COUNT from '../commands/ZPOPMAX_COUNT';
import * as ZPOPMAX from '../commands/ZPOPMAX';
Expand Down Expand Up @@ -202,12 +206,16 @@ export default {
bitPos: BITPOS,
BLMOVE,
blMove: BLMOVE,
BLMPOP,
blmPop: BLMPOP,
BLPOP,
blPop: BLPOP,
BRPOP,
brPop: BRPOP,
BRPOPLPUSH,
brPopLPush: BRPOPLPUSH,
BZMPOP,
bzmPop: BZMPOP,
BZPOPMAX,
bzPopMax: BZPOPMAX,
BZPOPMIN,
Expand Down Expand Up @@ -308,6 +316,8 @@ export default {
lLen: LLEN,
LMOVE,
lMove: LMOVE,
LMPOP,
lmPop: LMPOP,
LPOP_COUNT,
lPopCount: LPOP_COUNT,
LPOP,
Expand Down Expand Up @@ -512,6 +522,8 @@ export default {
zInterStore: ZINTERSTORE,
ZLEXCOUNT,
zLexCount: ZLEXCOUNT,
ZMPOP,
zmPop: ZMPOP,
ZMSCORE,
zmScore: ZMSCORE,
ZPOPMAX_COUNT,
Expand Down
6 changes: 3 additions & 3 deletions packages/client/lib/commands/BLMOVE.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { RedisCommandArgument, RedisCommandArguments } from '.';
import { LMoveSide } from './LMOVE';
import { ListSide } from './generic-transformers';

export const FIRST_KEY_INDEX = 1;

export function transformArguments(
source: RedisCommandArgument,
destination: RedisCommandArgument,
sourceDirection: LMoveSide,
destinationDirection: LMoveSide,
sourceDirection: ListSide,
destinationDirection: ListSide,
timeout: number
): RedisCommandArguments {
return [
Expand Down
32 changes: 32 additions & 0 deletions packages/client/lib/commands/BLMPOP.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './BLMPOP';

describe('BLMPOP', () => {
testUtils.isVersionGreaterThanHook([7, 0]);

describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
transformArguments(0, 'key', 'LEFT'),
['BLMPOP', '0', '1', 'key', 'LEFT']
);
});

it('with COUNT', () => {
assert.deepEqual(
transformArguments(0, 'key', 'LEFT', {
COUNT: 2
}),
['BLMPOP', '0', '1', 'key', 'LEFT', 'COUNT', '2']
);
});
});

testUtils.testWithClient('client.blmPop', async client => {
assert.deepEqual(
await client.blmPop(1, 'key', 'RIGHT'),
null
);
}, GLOBAL.SERVERS.OPEN);
});
20 changes: 20 additions & 0 deletions packages/client/lib/commands/BLMPOP.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { RedisCommandArgument, RedisCommandArguments } from '.';
import { transformLMPopArguments, LMPopOptions, ListSide } from './generic-transformers';

export const FIRST_KEY_INDEX = 3;

export function transformArguments(
timeout: number,
keys: RedisCommandArgument | Array<RedisCommandArgument>,
side: ListSide,
options?: LMPopOptions
): RedisCommandArguments {
return transformLMPopArguments(
['BLMPOP', timeout.toString()],
keys,
side,
options
);
}

export { transformReply } from './LMPOP';
2 changes: 1 addition & 1 deletion packages/client/lib/commands/BLPOP.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('BLPOP', () => {
'key',
1
),
cluster.lPush('key', 'element'),
cluster.lPush('key', 'element')
]);

assert.deepEqual(
Expand Down
32 changes: 32 additions & 0 deletions packages/client/lib/commands/BZMPOP.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './BZMPOP';

describe('BZMPOP', () => {
testUtils.isVersionGreaterThanHook([7, 0]);

describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
transformArguments(0, 'key', 'MIN'),
['BZMPOP', '0', '1', 'key', 'MIN']
);
});

it('with COUNT', () => {
assert.deepEqual(
transformArguments(0, 'key', 'MIN', {
COUNT: 2
}),
['BZMPOP', '0', '1', 'key', 'MIN', 'COUNT', '2']
);
});
});

testUtils.testWithClient('client.bzmPop', async client => {
assert.deepEqual(
await client.bzmPop(1, 'key', 'MAX'),
null
);
}, GLOBAL.SERVERS.OPEN);
});
20 changes: 20 additions & 0 deletions packages/client/lib/commands/BZMPOP.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { RedisCommandArgument, RedisCommandArguments } from '.';
import { SortedSetSide, transformZMPopArguments, ZMPopOptions } from './generic-transformers';

export const FIRST_KEY_INDEX = 3;

export function transformArguments(
timeout: number,
keys: RedisCommandArgument | Array<RedisCommandArgument>,
side: SortedSetSide,
options?: ZMPopOptions
): RedisCommandArguments {
return transformZMPopArguments(
['BZMPOP', timeout.toString()],
keys,
side,
options
);
}

export { transformReply } from './ZMPOP';
2 changes: 1 addition & 1 deletion packages/client/lib/commands/BZPOPMAX.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('BZPOPMAX', () => {
client.bzPopMax(
commandOptions({ isolated: true }),
'key',
0
1
),
client.zAdd('key', [{
value: '1',
Expand Down
2 changes: 1 addition & 1 deletion packages/client/lib/commands/BZPOPMIN.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('BZPOPMIN', () => {
client.bzPopMin(
commandOptions({ isolated: true }),
'key',
0
1
),
client.zAdd('key', [{
value: '1',
Expand Down
7 changes: 3 additions & 4 deletions packages/client/lib/commands/LMOVE.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { RedisCommandArgument, RedisCommandArguments } from '.';
import { ListSide } from './generic-transformers';

export const FIRST_KEY_INDEX = 1;

export type LMoveSide = 'LEFT' | 'RIGHT';

export function transformArguments(
source: RedisCommandArgument,
destination: RedisCommandArgument,
sourceSide: LMoveSide,
destinationSide: LMoveSide
sourceSide: ListSide,
destinationSide: ListSide
): RedisCommandArguments {
return [
'LMOVE',
Expand Down
32 changes: 32 additions & 0 deletions packages/client/lib/commands/LMPOP.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './LMPOP';

describe('LMPOP', () => {
testUtils.isVersionGreaterThanHook([7, 0]);

describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
transformArguments('key', 'LEFT'),
['LMPOP', '1', 'key', 'LEFT']
);
});

it('with COUNT', () => {
assert.deepEqual(
transformArguments('key', 'LEFT', {
COUNT: 2
}),
['LMPOP', '1', 'key', 'LEFT', 'COUNT', '2']
);
});
});

testUtils.testWithClient('client.lmPop', async client => {
assert.deepEqual(
await client.lmPop('key', 'RIGHT'),
null
);
}, GLOBAL.SERVERS.OPEN);
});
22 changes: 22 additions & 0 deletions packages/client/lib/commands/LMPOP.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { RedisCommandArgument, RedisCommandArguments } from '.';
import { transformLMPopArguments, LMPopOptions, ListSide } from './generic-transformers';

export const FIRST_KEY_INDEX = 2;

export function transformArguments(
keys: RedisCommandArgument | Array<RedisCommandArgument>,
side: ListSide,
options?: LMPopOptions
): RedisCommandArguments {
return transformLMPopArguments(
['LMPOP'],
keys,
side,
options
);
}

export declare function transformReply(): null | [
key: string,
elements: Array<string>
];
32 changes: 32 additions & 0 deletions packages/client/lib/commands/ZMPOP.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './ZMPOP';

describe('ZMPOP', () => {
testUtils.isVersionGreaterThanHook([7, 0]);

describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
transformArguments('key', 'MIN'),
['ZMPOP', '1', 'key', 'MIN']
);
});

it('with score and count', () => {
assert.deepEqual(
transformArguments('key', 'MIN', {
COUNT: 2
}),
['ZMPOP', '1', 'key', 'MIN', 'COUNT', '2']
);
});
});

testUtils.testWithClient('client.zmPop', async client => {
assert.deepEqual(
await client.zmPop('key', 'MIN'),
null
);
}, GLOBAL.SERVERS.OPEN);
});
34 changes: 34 additions & 0 deletions packages/client/lib/commands/ZMPOP.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { RedisCommandArgument, RedisCommandArguments } from '.';
import { SortedSetSide, transformSortedSetMemberReply, transformZMPopArguments, ZMember, ZMPopOptions } from './generic-transformers';

export const FIRST_KEY_INDEX = 2;

export function transformArguments(
keys: RedisCommandArgument | Array<RedisCommandArgument>,
side: SortedSetSide,
options?: ZMPopOptions
): RedisCommandArguments {
return transformZMPopArguments(
['ZMPOP'],
keys,
side,
options
);
}

type ZMPopRawReply = null | [
key: string,
elements: Array<[RedisCommandArgument, RedisCommandArgument]>
];

type ZMPopReply = null | {
key: string,
elements: Array<ZMember>
};

export function transformReply(reply: ZMPopRawReply): ZMPopReply {
return reply === null ? null : {
key: reply[0],
elements: reply[1].map(transformSortedSetMemberReply)
};
}
Loading

0 comments on commit b1a0b48

Please sign in to comment.