1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-16 08:41:57 +03:00

Support new muilti pop commands

This commit is contained in:
Avital-Fine
2022-03-22 16:08:48 +01:00
parent c5c2bf9042
commit 23d44fdd51
10 changed files with 284 additions and 0 deletions

View File

@@ -6,9 +6,11 @@ import * as BITFIELD from '../commands/BITFIELD';
import * as BITOP from '../commands/BITOP'; import * as BITOP from '../commands/BITOP';
import * as BITPOS from '../commands/BITPOS'; import * as BITPOS from '../commands/BITPOS';
import * as BLMOVE from '../commands/BLMOVE'; import * as BLMOVE from '../commands/BLMOVE';
import * as BLMPOP from '../commands/BLMPOP';
import * as BLPOP from '../commands/BLPOP'; import * as BLPOP from '../commands/BLPOP';
import * as BRPOP from '../commands/BRPOP'; import * as BRPOP from '../commands/BRPOP';
import * as BRPOPLPUSH from '../commands/BRPOPLPUSH'; import * as BRPOPLPUSH from '../commands/BRPOPLPUSH';
import * as BZMPOP from '../commands/BZMPOP';
import * as BZPOPMAX from '../commands/BZPOPMAX'; import * as BZPOPMAX from '../commands/BZPOPMAX';
import * as BZPOPMIN from '../commands/BZPOPMIN'; import * as BZPOPMIN from '../commands/BZPOPMIN';
import * as COPY from '../commands/COPY'; import * as COPY from '../commands/COPY';
@@ -58,6 +60,7 @@ import * as LINDEX from '../commands/LINDEX';
import * as LINSERT from '../commands/LINSERT'; import * as LINSERT from '../commands/LINSERT';
import * as LLEN from '../commands/LLEN'; import * as LLEN from '../commands/LLEN';
import * as LMOVE from '../commands/LMOVE'; import * as LMOVE from '../commands/LMOVE';
import * as LMPOP from '../commands/LMPOP';
import * as LPOP_COUNT from '../commands/LPOP_COUNT'; import * as LPOP_COUNT from '../commands/LPOP_COUNT';
import * as LPOP from '../commands/LPOP'; import * as LPOP from '../commands/LPOP';
import * as LPOS_COUNT from '../commands/LPOS_COUNT'; import * as LPOS_COUNT from '../commands/LPOS_COUNT';
@@ -153,6 +156,7 @@ import * as ZINTER from '../commands/ZINTER';
import * as ZINTERCARD from '../commands/ZINTERCARD'; import * as ZINTERCARD from '../commands/ZINTERCARD';
import * as ZINTERSTORE from '../commands/ZINTERSTORE'; import * as ZINTERSTORE from '../commands/ZINTERSTORE';
import * as ZLEXCOUNT from '../commands/ZLEXCOUNT'; import * as ZLEXCOUNT from '../commands/ZLEXCOUNT';
import * as ZMPOP from '../commands/ZMPOP';
import * as ZMSCORE from '../commands/ZMSCORE'; import * as ZMSCORE from '../commands/ZMSCORE';
import * as ZPOPMAX_COUNT from '../commands/ZPOPMAX_COUNT'; import * as ZPOPMAX_COUNT from '../commands/ZPOPMAX_COUNT';
import * as ZPOPMAX from '../commands/ZPOPMAX'; import * as ZPOPMAX from '../commands/ZPOPMAX';
@@ -194,12 +198,16 @@ export default {
bitPos: BITPOS, bitPos: BITPOS,
BLMOVE, BLMOVE,
blMove: BLMOVE, blMove: BLMOVE,
BLMPOP,
blmPop: BLMPOP,
BLPOP, BLPOP,
blPop: BLPOP, blPop: BLPOP,
BRPOP, BRPOP,
brPop: BRPOP, brPop: BRPOP,
BRPOPLPUSH, BRPOPLPUSH,
brPopLPush: BRPOPLPUSH, brPopLPush: BRPOPLPUSH,
BZMPOP,
bzmPop: BZMPOP,
BZPOPMAX, BZPOPMAX,
bzPopMax: BZPOPMAX, bzPopMax: BZPOPMAX,
BZPOPMIN, BZPOPMIN,
@@ -298,6 +306,8 @@ export default {
lLen: LLEN, lLen: LLEN,
LMOVE, LMOVE,
lMove: LMOVE, lMove: LMOVE,
LMPOP,
lmPop: LMPOP,
LPOP_COUNT, LPOP_COUNT,
lPopCount: LPOP_COUNT, lPopCount: LPOP_COUNT,
LPOP, LPOP,
@@ -488,6 +498,8 @@ export default {
zInterStore: ZINTERSTORE, zInterStore: ZINTERSTORE,
ZLEXCOUNT, ZLEXCOUNT,
zLexCount: ZLEXCOUNT, zLexCount: ZLEXCOUNT,
ZMPOP,
zmPop: ZMPOP,
ZMSCORE, ZMSCORE,
zmScore: ZMSCORE, zmScore: ZMSCORE,
ZPOPMAX_COUNT, ZPOPMAX_COUNT,

View File

@@ -0,0 +1,37 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './BLMPOP';
describe.only('BLMPOP', () => {
testUtils.isVersionGreaterThanHook([7, 0]);
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
transformArguments(0, 'key', {
SIDE: 'LEFT'
}),
['BLMPOP', '0', '1', 'key', 'LEFT']
);
});
it('with score and count', () => {
assert.deepEqual(
transformArguments(0, 'key', {
SIDE: 'LEFT',
COUNT: 2
}),
['BLMPOP', '0', '1', 'key', 'LEFT', 'COUNT', '2']
);
});
});
testUtils.testWithClient('client.zmScore', async client => {
assert.deepEqual(
await client.blmPop(0, 'key', {
SIDE: 'LEFT'
}),
null
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -0,0 +1,14 @@
import { RedisCommandArguments } from '.';
import { transformLMPopArguments, LMPopOptions } from './generic-transformers';
export const FIRST_KEY_INDEX = 3;
export function transformArguments(
timeout: number,
keys: string | Array<string>,
options: LMPopOptions
): RedisCommandArguments {
return transformLMPopArguments(['BLMPOP', timeout.toString()], keys, options);
}
export { transformReply } from './LMPOP';

View File

@@ -0,0 +1,37 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './BZMPOP';
describe.only('BZMPOP', () => {
testUtils.isVersionGreaterThanHook([7, 0]);
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
transformArguments(0, 'key', {
SCORE: 'MIN'
}),
['BZMPOP', '0', '1', 'key', 'MIN']
);
});
it('with score and count', () => {
assert.deepEqual(
transformArguments(0, 'key', {
SCORE: 'MIN',
COUNT: 2
}),
['BZMPOP', '0', '1', 'key', 'MIN', 'COUNT', '2']
);
});
});
testUtils.testWithClient('client.zmScore', async client => {
assert.deepEqual(
await client.bzmPop(0, 'key', {
SCORE: 'MAX'
}),
null
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -0,0 +1,14 @@
import { RedisCommandArguments } from '.';
import { transformZMPopArguments, ZMPopOptions } from './generic-transformers';
export const FIRST_KEY_INDEX = 3;
export function transformArguments(
timeout: number,
keys: string | Array<string>,
options: ZMPopOptions
): RedisCommandArguments {
return transformZMPopArguments(['BZMPOP', timeout.toString()], keys, options);
}
export { transformReply } from './ZMPOP';

View File

@@ -0,0 +1,37 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './LMPOP';
describe.only('LMPOP', () => {
testUtils.isVersionGreaterThanHook([7, 0]);
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
transformArguments('key', {
SIDE: 'LEFT'
}),
['LMPOP', '1', 'key', 'LEFT']
);
});
it('with score and count', () => {
assert.deepEqual(
transformArguments('key', {
SIDE: 'LEFT',
COUNT: 2
}),
['LMPOP', '1', 'key', 'LEFT', 'COUNT', '2']
);
});
});
testUtils.testWithClient('client.zmScore', async client => {
assert.deepEqual(
await client.lmPop('key', {
SIDE: 'RIGHT'
}),
null
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -0,0 +1,20 @@
import { RedisCommandArguments } from '.';
import { transformLMPopArguments, LMPopOptions } from './generic-transformers';
export const FIRST_KEY_INDEX = 2;
export const IS_READ_ONLY = true;
export function transformArguments(
keys: string | Array<string>,
options: LMPopOptions
): RedisCommandArguments {
return transformLMPopArguments(['LMPOP'], keys, options);
}
type LMPopReply = null | [
key: string,
elements: Array<String>
];
export declare function transformReply(): LMPopReply;

View File

@@ -0,0 +1,37 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './ZMPOP';
describe.only('ZMPOP', () => {
testUtils.isVersionGreaterThanHook([7, 0]);
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
transformArguments('key', {
SCORE: 'MIN'
}),
['ZMPOP', '1', 'key', 'MIN']
);
});
it('with score and count', () => {
assert.deepEqual(
transformArguments('key', {
SCORE: 'MIN',
COUNT: 2
}),
['ZMPOP', '1', 'key', 'MIN', 'COUNT', '2']
);
});
});
testUtils.testWithClient('client.zmScore', async client => {
assert.deepEqual(
await client.zmPop('key', {
SCORE: 'MAX'
}),
null
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -0,0 +1,26 @@
import { RedisCommandArgument, RedisCommandArguments } from '.';
import { transformSortedSetMemberReply, transformZMPopArguments, ZMember, ZMPopOptions } from './generic-transformers';
export const FIRST_KEY_INDEX = 2;
export const IS_READ_ONLY = true;
export function transformArguments(
keys: string | Array<string>,
options: ZMPopOptions
): RedisCommandArguments {
return transformZMPopArguments(['ZMPOP'], keys, options);
}
type ZMPopRawReply = null | [string, Array<[RedisCommandArgument, RedisCommandArgument]>];
type ZMPopReply = null | [
key: string,
elements: Array<ZMember>
];
export function transformReply(reply: ZMPopRawReply): ZMPopReply {
if (reply == null) return null;
return [reply[0], reply[1].map(transformSortedSetMemberReply)];
}

View File

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