1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-07 13:22:56 +03:00

new "transform arguments" API for better key and metadata extraction (#2733)

* Parser support with all commands

* remove "dist" from all imports for consistency

* address most of my review comments

* small tweak to multi type mapping handling

* tweak multi commands / fix addScript cases

* nits

* addressed all in person review comments

* revert addCommand/addScript changes to multi-commands

addCommand needs to be there for sendCommand like ability within a multi.

If its there, it might as well be used by createCommand() et al, to avoid repeating code.

addScript is there (even though only used once), but now made private to keep the logic for bookkeeping near each other.
This commit is contained in:
Shaya Potter
2024-10-31 18:16:59 +02:00
committed by GitHub
parent 5ace34b9c9
commit 4708736f3b
1016 changed files with 6347 additions and 6542 deletions

View File

@@ -1,11 +1,12 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import ADD from './ADD';
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
describe('TDIGEST.ADD', () => {
it('transformArguments', () => {
assert.deepEqual(
ADD.transformArguments('key', [1, 2]),
parseArgs(ADD, 'key', [1, 2]),
['TDIGEST.ADD', 'key', '1', '2']
);
});

View File

@@ -1,16 +1,15 @@
import { SimpleStringReply, Command, RedisArgument } from '@redis/client/dist/lib/RESP/types';
import { CommandParser } from '@redis/client/lib/client/parser';
import { SimpleStringReply, Command, RedisArgument } from '@redis/client/lib/RESP/types';
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(key: RedisArgument, values: Array<number>) {
const args = ['TDIGEST.ADD', key];
parseCommand(parser: CommandParser, key: RedisArgument, values: Array<number>) {
parser.push('TDIGEST.ADD');
parser.pushKey(key);
for (const value of values) {
args.push(value.toString());
parser.push(value.toString());
}
return args;
},
transformReply: undefined as unknown as () => SimpleStringReply<'OK'>
} as const satisfies Command;

View File

@@ -1,11 +1,12 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import BYRANK from './BYRANK';
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
describe('TDIGEST.BYRANK', () => {
it('transformArguments', () => {
assert.deepEqual(
BYRANK.transformArguments('key', [1, 2]),
parseArgs(BYRANK, 'key', [1, 2]),
['TDIGEST.BYRANK', 'key', '1', '2']
);
});

View File

@@ -1,24 +1,25 @@
import { RedisArgument, Command } from '@redis/client/dist/lib/RESP/types';
import { transformDoubleArrayReply } from '@redis/client/dist/lib/commands/generic-transformers';
import { CommandParser } from '@redis/client/lib/client/parser';
import { RedisArgument, Command } from '@redis/client/lib/RESP/types';
import { transformDoubleArrayReply } from '@redis/client/lib/commands/generic-transformers';
export function transformByRankArguments(
command: RedisArgument,
parser: CommandParser,
key: RedisArgument,
ranks: Array<number>
) {
const args = [command, key];
parser.pushKey(key);
for (const rank of ranks) {
args.push(rank.toString());
parser.push(rank.toString());
}
return args;
}
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments: transformByRankArguments.bind(undefined, 'TDIGEST.BYRANK'),
parseCommand(...args: Parameters<typeof transformByRankArguments>) {
args[0].push('TDIGEST.BYRANK');
transformByRankArguments(...args);
},
transformReply: transformDoubleArrayReply
} as const satisfies Command;

View File

@@ -1,11 +1,12 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import BYREVRANK from './BYREVRANK';
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
describe('TDIGEST.BYREVRANK', () => {
it('transformArguments', () => {
assert.deepEqual(
BYREVRANK.transformArguments('key', [1, 2]),
parseArgs(BYREVRANK, 'key', [1, 2]),
['TDIGEST.BYREVRANK', 'key', '1', '2']
);
});

View File

@@ -1,9 +1,11 @@
import { Command } from '@redis/client/dist/lib/RESP/types';
import { Command } from '@redis/client/lib/RESP/types';
import BYRANK, { transformByRankArguments } from './BYRANK';
export default {
FIRST_KEY_INDEX: BYRANK.FIRST_KEY_INDEX,
IS_READ_ONLY: BYRANK.IS_READ_ONLY,
transformArguments: transformByRankArguments.bind(undefined, 'TDIGEST.BYREVRANK'),
parseCommand(...args: Parameters<typeof transformByRankArguments>) {
args[0].push('TDIGEST.BYREVRANK');
transformByRankArguments(...args);
},
transformReply: BYRANK.transformReply
} as const satisfies Command;

View File

@@ -1,11 +1,12 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import CDF from './CDF';
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
describe('TDIGEST.CDF', () => {
it('transformArguments', () => {
assert.deepEqual(
CDF.transformArguments('key', [1, 2]),
parseArgs(CDF, 'key', [1, 2]),
['TDIGEST.CDF', 'key', '1', '2']
);
});

View File

@@ -1,17 +1,16 @@
import { RedisArgument, Command } from '@redis/client/dist/lib/RESP/types';
import { transformDoubleArrayReply } from '@redis/client/dist/lib/commands/generic-transformers';
import { CommandParser } from '@redis/client/lib/client/parser';
import { RedisArgument, Command } from '@redis/client/lib/RESP/types';
import { transformDoubleArrayReply } from '@redis/client/lib/commands/generic-transformers';
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(key: RedisArgument, values: Array<number>) {
const args = ['TDIGEST.CDF', key];
parseCommand(parser: CommandParser, key: RedisArgument, values: Array<number>) {
parser.push('TDIGEST.CDF');
parser.pushKey(key);
for (const item of values) {
args.push(item.toString());
parser.push(item.toString());
}
return args;
},
transformReply: transformDoubleArrayReply
} as const satisfies Command;

View File

@@ -1,19 +1,20 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import CREATE from './CREATE';
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
describe('TDIGEST.CREATE', () => {
describe('transformArguments', () => {
it('without options', () => {
assert.deepEqual(
CREATE.transformArguments('key'),
parseArgs(CREATE, 'key'),
['TDIGEST.CREATE', 'key']
);
});
it('with COMPRESSION', () => {
assert.deepEqual(
CREATE.transformArguments('key', {
parseArgs(CREATE, 'key', {
COMPRESSION: 100
}),
['TDIGEST.CREATE', 'key', 'COMPRESSION', '100']

View File

@@ -1,20 +1,19 @@
import { RedisArgument, SimpleStringReply, Command } from '@redis/client/dist/lib/RESP/types';
import { CommandParser } from '@redis/client/lib/client/parser';
import { RedisArgument, SimpleStringReply, Command } from '@redis/client/lib/RESP/types';
export interface TDigestCreateOptions {
COMPRESSION?: number;
}
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(key: RedisArgument, options?: TDigestCreateOptions) {
const args = ['TDIGEST.CREATE', key];
parseCommand(parser: CommandParser, key: RedisArgument, options?: TDigestCreateOptions) {
parser.push('TDIGEST.CREATE');
parser.pushKey(key);
if (options?.COMPRESSION !== undefined) {
args.push('COMPRESSION', options.COMPRESSION.toString());
parser.push('COMPRESSION', options.COMPRESSION.toString());
}
return args;
},
transformReply: undefined as unknown as () => SimpleStringReply<'OK'>
} as const satisfies Command;

View File

@@ -1,11 +1,12 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import INFO from './INFO';
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
describe('TDIGEST.INFO', () => {
it('transformArguments', () => {
assert.deepEqual(
INFO.transformArguments('key'),
parseArgs(INFO, 'key'),
['TDIGEST.INFO', 'key']
);
});

View File

@@ -1,4 +1,5 @@
import { RedisArgument, Command, NumberReply, TuplesToMapReply, UnwrapReply, Resp2Reply, SimpleStringReply, TypeMapping } from '@redis/client/dist/lib/RESP/types';
import { CommandParser } from '@redis/client/lib/client/parser';
import { RedisArgument, Command, NumberReply, TuplesToMapReply, UnwrapReply, Resp2Reply, SimpleStringReply, TypeMapping } from '@redis/client/lib/RESP/types';
import { transformInfoV2Reply } from '../bloom';
export type TdInfoReplyMap = TuplesToMapReply<[
@@ -14,10 +15,10 @@ export type TdInfoReplyMap = TuplesToMapReply<[
]>;
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(key: RedisArgument) {
return ['TDIGEST.INFO', key];
parseCommand(parser: CommandParser, key: RedisArgument) {
parser.push('TDIGEST.INFO');
parser.pushKey(key);
},
transformReply: {
2: (reply: UnwrapReply<Resp2Reply<TdInfoReplyMap>>, _, typeMapping?: TypeMapping): TdInfoReplyMap => {

View File

@@ -1,11 +1,12 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import MAX from './MAX';
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
describe('TDIGEST.MAX', () => {
it('transformArguments', () => {
assert.deepEqual(
MAX.transformArguments('key'),
parseArgs(MAX, 'key'),
['TDIGEST.MAX', 'key']
);
});

View File

@@ -1,11 +1,12 @@
import { RedisArgument, Command } from '@redis/client/dist/lib/RESP/types';
import { transformDoubleReply } from '@redis/client/dist/lib/commands/generic-transformers';
import { CommandParser } from '@redis/client/lib/client/parser';
import { RedisArgument, Command } from '@redis/client/lib/RESP/types';
import { transformDoubleReply } from '@redis/client/lib/commands/generic-transformers';
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(key: RedisArgument) {
return ['TDIGEST.MAX', key];
parseCommand(parser: CommandParser, key: RedisArgument) {
parser.push('TDIGEST.MAX');
parser.pushKey(key);
},
transformReply: transformDoubleReply
} as const satisfies Command;

View File

@@ -1,20 +1,21 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import MERGE from './MERGE';
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
describe('TDIGEST.MERGE', () => {
describe('transformArguments', () => {
describe('source', () => {
it('string', () => {
assert.deepEqual(
MERGE.transformArguments('destination', 'source'),
parseArgs(MERGE, 'destination', 'source'),
['TDIGEST.MERGE', 'destination', '1', 'source']
);
});
it('Array', () => {
assert.deepEqual(
MERGE.transformArguments('destination', ['1', '2']),
parseArgs(MERGE, 'destination', ['1', '2']),
['TDIGEST.MERGE', 'destination', '2', '1', '2']
);
});
@@ -22,7 +23,7 @@ describe('TDIGEST.MERGE', () => {
it('with COMPRESSION', () => {
assert.deepEqual(
MERGE.transformArguments('destination', 'source', {
parseArgs(MERGE, 'destination', 'source', {
COMPRESSION: 100
}),
['TDIGEST.MERGE', 'destination', '1', 'source', 'COMPRESSION', '100']
@@ -31,7 +32,7 @@ describe('TDIGEST.MERGE', () => {
it('with OVERRIDE', () => {
assert.deepEqual(
MERGE.transformArguments('destination', 'source', {
parseArgs(MERGE, 'destination', 'source', {
OVERRIDE: true
}),
['TDIGEST.MERGE', 'destination', '1', 'source', 'OVERRIDE']

View File

@@ -1,5 +1,6 @@
import { RedisArgument, SimpleStringReply, Command } from '@redis/client/dist/lib/RESP/types';
import { RedisVariadicArgument, pushVariadicArgument } from '@redis/client/dist/lib/commands/generic-transformers';
import { CommandParser } from '@redis/client/lib/client/parser';
import { RedisArgument, SimpleStringReply, Command } from '@redis/client/lib/RESP/types';
import { RedisVariadicArgument } from '@redis/client/lib/commands/generic-transformers';
export interface TDigestMergeOptions {
COMPRESSION?: number;
@@ -7,24 +8,24 @@ export interface TDigestMergeOptions {
}
export default {
FIRST_KEY_INDEX: undefined,
IS_READ_ONLY: false,
transformArguments(
parseCommand(
parser: CommandParser,
destination: RedisArgument,
source: RedisVariadicArgument,
options?: TDigestMergeOptions
) {
const args = pushVariadicArgument(['TDIGEST.MERGE', destination], source);
parser.push('TDIGEST.MERGE');
parser.pushKey(destination);
parser.pushKeysLength(source);
if (options?.COMPRESSION !== undefined) {
args.push('COMPRESSION', options.COMPRESSION.toString());
parser.push('COMPRESSION', options.COMPRESSION.toString());
}
if (options?.OVERRIDE) {
args.push('OVERRIDE');
parser.push('OVERRIDE');
}
return args;
},
transformReply: undefined as unknown as () => SimpleStringReply<'OK'>
} as const satisfies Command;

View File

@@ -1,11 +1,12 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import MIN from './MIN';
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
describe('TDIGEST.MIN', () => {
it('transformArguments', () => {
assert.deepEqual(
MIN.transformArguments('key'),
parseArgs(MIN, 'key'),
['TDIGEST.MIN', 'key']
);
});

View File

@@ -1,11 +1,12 @@
import { RedisArgument, Command } from '@redis/client/dist/lib/RESP/types';
import { transformDoubleReply } from '@redis/client/dist/lib/commands/generic-transformers';
import { CommandParser } from '@redis/client/lib/client/parser';
import { RedisArgument, Command } from '@redis/client/lib/RESP/types';
import { transformDoubleReply } from '@redis/client/lib/commands/generic-transformers';
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(key: RedisArgument) {
return ['TDIGEST.MIN', key];
parseCommand(parser: CommandParser, key: RedisArgument) {
parser.push('TDIGEST.MIN');
parser.pushKey(key);
},
transformReply: transformDoubleReply
} as const satisfies Command;

View File

@@ -1,11 +1,12 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import QUANTILE from './QUANTILE';
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
describe('TDIGEST.QUANTILE', () => {
it('transformArguments', () => {
assert.deepEqual(
QUANTILE.transformArguments('key', [1, 2]),
parseArgs(QUANTILE, 'key', [1, 2]),
['TDIGEST.QUANTILE', 'key', '1', '2']
);
});

View File

@@ -1,17 +1,16 @@
import { RedisArgument, Command } from '@redis/client/dist/lib/RESP/types';
import { transformDoubleArrayReply } from '@redis/client/dist/lib/commands/generic-transformers';
import { CommandParser } from '@redis/client/lib/client/parser';
import { RedisArgument, Command } from '@redis/client/lib/RESP/types';
import { transformDoubleArrayReply } from '@redis/client/lib/commands/generic-transformers';
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(key: RedisArgument, quantiles: Array<number>) {
const args = ['TDIGEST.QUANTILE', key];
parseCommand(parser: CommandParser, key: RedisArgument, quantiles: Array<number>) {
parser.push('TDIGEST.QUANTILE');
parser.pushKey(key);
for (const quantile of quantiles) {
args.push(quantile.toString());
parser.push(quantile.toString());
}
return args;
},
transformReply: transformDoubleArrayReply
} as const satisfies Command;

View File

@@ -1,11 +1,12 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import RANK from './RANK';
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
describe('TDIGEST.RANK', () => {
it('transformArguments', () => {
assert.deepEqual(
RANK.transformArguments('key', [1, 2]),
parseArgs(RANK, 'key', [1, 2]),
['TDIGEST.RANK', 'key', '1', '2']
);
});

View File

@@ -1,22 +1,23 @@
import { RedisArgument, ArrayReply, NumberReply, Command } from '@redis/client/dist/lib/RESP/types';
import { CommandParser } from '@redis/client/lib/client/parser';
import { RedisArgument, ArrayReply, NumberReply, Command } from '@redis/client/lib/RESP/types';
export function transformRankArguments(
command: RedisArgument,
parser: CommandParser,
key: RedisArgument,
values: Array<number>
) {
const args = [command, key];
parser.pushKey(key);
for (const value of values) {
args.push(value.toString());
parser.push(value.toString());
}
return args;
}
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments: transformRankArguments.bind(undefined, 'TDIGEST.RANK'),
parseCommand(...args: Parameters<typeof transformRankArguments>) {
args[0].push('TDIGEST.RANK');
transformRankArguments(...args);
},
transformReply: undefined as unknown as () => ArrayReply<NumberReply>
} as const satisfies Command;

View File

@@ -1,11 +1,12 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import RESET from './RESET';
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
describe('TDIGEST.RESET', () => {
it('transformArguments', () => {
assert.deepEqual(
RESET.transformArguments('key'),
parseArgs(RESET, 'key'),
['TDIGEST.RESET', 'key']
);
});

View File

@@ -1,10 +1,11 @@
import { RedisArgument, SimpleStringReply, Command } from '@redis/client/dist/lib/RESP/types';
import { CommandParser } from '@redis/client/lib/client/parser';
import { RedisArgument, SimpleStringReply, Command } from '@redis/client/lib/RESP/types';
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(key: RedisArgument) {
return ['TDIGEST.RESET', key];
parseCommand(parser: CommandParser, key: RedisArgument) {
parser.push('TDIGEST.RESET');
parser.pushKey(key);
},
transformReply: undefined as unknown as () => SimpleStringReply<'OK'>
} as const satisfies Command;

View File

@@ -1,11 +1,12 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import REVRANK from './REVRANK';
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
describe('TDIGEST.REVRANK', () => {
it('transformArguments', () => {
assert.deepEqual(
REVRANK.transformArguments('key', [1, 2]),
parseArgs(REVRANK, 'key', [1, 2]),
['TDIGEST.REVRANK', 'key', '1', '2']
);
});

View File

@@ -1,9 +1,11 @@
import { Command } from '@redis/client/dist/lib/RESP/types';
import { Command } from '@redis/client/lib/RESP/types';
import RANK, { transformRankArguments } from './RANK';
export default {
FIRST_KEY_INDEX: RANK.FIRST_KEY_INDEX,
IS_READ_ONLY: RANK.IS_READ_ONLY,
transformArguments: transformRankArguments.bind(undefined, 'TDIGEST.REVRANK'),
parseCommand(...args: Parameters<typeof transformRankArguments>) {
args[0].push('TDIGEST.REVRANK');
transformRankArguments(...args);
},
transformReply: RANK.transformReply
} as const satisfies Command;

View File

@@ -1,11 +1,12 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import TRIMMED_MEAN from './TRIMMED_MEAN';
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';
describe('TDIGEST.TRIMMED_MEAN', () => {
it('transformArguments', () => {
assert.deepEqual(
TRIMMED_MEAN.transformArguments('key', 0, 1),
parseArgs(TRIMMED_MEAN, 'key', 0, 1),
['TDIGEST.TRIMMED_MEAN', 'key', '0', '1']
);
});

View File

@@ -1,20 +1,18 @@
import { RedisArgument, Command } from '@redis/client/dist/lib/RESP/types';
import { transformDoubleReply } from '@redis/client/dist/lib/commands/generic-transformers';
import { CommandParser } from '@redis/client/lib/client/parser';
import { RedisArgument, Command } from '@redis/client/lib/RESP/types';
import { transformDoubleReply } from '@redis/client/lib/commands/generic-transformers';
export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(
parseCommand(
parser: CommandParser,
key: RedisArgument,
lowCutPercentile: number,
highCutPercentile: number
) {
return [
'TDIGEST.TRIMMED_MEAN',
key,
lowCutPercentile.toString(),
highCutPercentile.toString()
];
parser.push('TDIGEST.TRIMMED_MEAN');
parser.pushKey(key);
parser.push(lowCutPercentile.toString(), highCutPercentile.toString());
},
transformReply: transformDoubleReply
} as const satisfies Command;

View File

@@ -1,4 +1,4 @@
import type { RedisCommands } from '@redis/client/dist/lib/RESP/types';
import type { RedisCommands } from '@redis/client/lib/RESP/types';
import ADD from './ADD';
import BYRANK from './BYRANK';
import BYREVRANK from './BYREVRANK';