1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-06 02:15:48 +03:00

Support new muilti pop commands (#2051)

* Support new muilti pop commands

* remove .only

* clean code

* fix for 4558ec6a31

* fix tests

Co-authored-by: leibale <leibale1998@gmail.com>
This commit is contained in:
Avital Fine
2022-04-25 14:50:43 +03:00
committed by GitHub
parent 0f7ae937df
commit b1a0b48d2c
15 changed files with 298 additions and 10 deletions

View File

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

View File

@@ -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);
});

View File

@@ -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';

View File

@@ -65,7 +65,7 @@ describe('BLPOP', () => {
'key',
1
),
cluster.lPush('key', 'element'),
cluster.lPush('key', 'element')
]);
assert.deepEqual(

View File

@@ -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);
});

View File

@@ -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';

View File

@@ -45,7 +45,7 @@ describe('BZPOPMAX', () => {
client.bzPopMax(
commandOptions({ isolated: true }),
'key',
0
1
),
client.zAdd('key', [{
value: '1',

View File

@@ -45,7 +45,7 @@ describe('BZPOPMIN', () => {
client.bzPopMin(
commandOptions({ isolated: true }),
'key',
0
1
),
client.zAdd('key', [{
value: '1',

View File

@@ -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',

View File

@@ -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);
});

View File

@@ -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>
];

View File

@@ -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);
});

View File

@@ -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)
};
}

View File

@@ -131,6 +131,13 @@ export function transformSortedSetMemberNullReply(
): ZMember | null {
if (!reply.length) return null;
return transformSortedSetMemberReply(reply);
}
export function transformSortedSetMemberReply(
reply: [RedisCommandArgument, RedisCommandArgument]
): ZMember {
return {
value: reply[0],
score: transformNumberInfinityReply(reply[1])
@@ -150,6 +157,52 @@ export function transformSortedSetWithScoresReply(reply: Array<RedisCommandArgum
return members;
}
export type SortedSetSide = 'MIN' | 'MAX';
export interface ZMPopOptions {
COUNT?: number;
}
export function transformZMPopArguments(
args: RedisCommandArguments,
keys: RedisCommandArgument | Array<RedisCommandArgument>,
side: SortedSetSide,
options?: ZMPopOptions
): RedisCommandArguments {
pushVerdictArgument(args, keys);
args.push(side);
if (options?.COUNT) {
args.push('COUNT', options.COUNT.toString());
}
return args;
}
export type ListSide = 'LEFT' | 'RIGHT';
export interface LMPopOptions {
COUNT?: number;
}
export function transformLMPopArguments(
args: RedisCommandArguments,
keys: RedisCommandArgument | Array<RedisCommandArgument>,
side: ListSide,
options?: LMPopOptions
): RedisCommandArguments {
pushVerdictArgument(args, keys);
args.push(side);
if (options?.COUNT) {
args.push('COUNT', options.COUNT.toString());
}
return args;
}
type GeoCountArgument = number | {
value: number;
ANY?: true