Skip to content

Commit

Permalink
new "transform arguments" API for better key and metadata extraction (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
sjpotter authored Oct 31, 2024
1 parent 5ace34b commit 4708736
Show file tree
Hide file tree
Showing 1,016 changed files with 6,345 additions and 6,540 deletions.
2,511 changes: 754 additions & 1,757 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion packages/bloom/lib/commands/bloom/ADD.spec.ts
Original file line number Diff line number Diff line change
@@ -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('BF.ADD', () => {
it('transformArguments', () => {
assert.deepEqual(
ADD.transformArguments('key', 'item'),
parseArgs(ADD, 'key', 'item'),
['BF.ADD', 'key', 'item']
);
});
Expand Down
12 changes: 7 additions & 5 deletions packages/bloom/lib/commands/bloom/ADD.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { RedisArgument, Command } from '@redis/client/dist/lib/RESP/types';
import { transformBooleanReply } 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 { transformBooleanReply } from '@redis/client/lib/commands/generic-transformers';

export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(key: RedisArgument, item: RedisArgument) {
return ['BF.ADD', key, item];
parseCommand(parser: CommandParser, key: RedisArgument, item: RedisArgument) {
parser.push('BF.ADD');
parser.pushKey(key);
parser.push(item);
},
transformReply: transformBooleanReply
} as const satisfies Command;
3 changes: 2 additions & 1 deletion packages/bloom/lib/commands/bloom/CARD.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import CARD from './CARD';
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';

describe('BF.CARD', () => {
it('transformArguments', () => {
assert.deepEqual(
CARD.transformArguments('bloom'),
parseArgs(CARD, 'bloom'),
['BF.CARD', 'bloom']
);
});
Expand Down
9 changes: 5 additions & 4 deletions packages/bloom/lib/commands/bloom/CARD.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { RedisArgument, NumberReply, Command } from '@redis/client/dist/lib/RESP/types';
import { CommandParser } from '@redis/client/lib/client/parser';
import { RedisArgument, NumberReply, Command } from '@redis/client/lib/RESP/types';

export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(key: RedisArgument) {
return ['BF.CARD', key];
parseCommand(parser: CommandParser, key: RedisArgument) {
parser.push('BF.CARD');
parser.pushKey(key);
},
transformReply: undefined as unknown as () => NumberReply
} as const satisfies Command;
3 changes: 2 additions & 1 deletion packages/bloom/lib/commands/bloom/EXISTS.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import EXISTS from './EXISTS';
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';

describe('BF.EXISTS', () => {
it('transformArguments', () => {
assert.deepEqual(
EXISTS.transformArguments('key', 'item'),
parseArgs(EXISTS, 'key', 'item'),
['BF.EXISTS', 'key', 'item']
);
});
Expand Down
12 changes: 7 additions & 5 deletions packages/bloom/lib/commands/bloom/EXISTS.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { RedisArgument, Command } from '@redis/client/dist/lib/RESP/types';
import { transformBooleanReply } 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 { transformBooleanReply } from '@redis/client/lib/commands/generic-transformers';

export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(key: RedisArgument, item: RedisArgument) {
return ['BF.EXISTS', key, item];
parseCommand(parser: CommandParser, key: RedisArgument, item: RedisArgument) {
parser.push('BF.EXISTS');
parser.pushKey(key);
parser.push(item);
},
transformReply: transformBooleanReply
} as const satisfies Command;
3 changes: 2 additions & 1 deletion packages/bloom/lib/commands/bloom/INFO.spec.ts
Original file line number Diff line number Diff line change
@@ -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('BF.INFO', () => {
it('transformArguments', () => {
assert.deepEqual(
INFO.transformArguments('bloom'),
parseArgs(INFO, 'bloom'),
['BF.INFO', 'bloom']
);
});
Expand Down
17 changes: 5 additions & 12 deletions packages/bloom/lib/commands/bloom/INFO.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RedisArgument, Command, UnwrapReply, NullReply, NumberReply, TuplesToMapReply, Resp2Reply, SimpleStringReply, TypeMapping } from '@redis/client/dist/lib/RESP/types';
import { CommandParser } from '@redis/client/lib/client/parser';
import { RedisArgument, Command, UnwrapReply, NullReply, NumberReply, TuplesToMapReply, Resp2Reply, SimpleStringReply, TypeMapping } from '@redis/client/lib/RESP/types';
import { transformInfoV2Reply } from '.';

export type BfInfoReplyMap = TuplesToMapReply<[
Expand All @@ -9,19 +10,11 @@ export type BfInfoReplyMap = TuplesToMapReply<[
[SimpleStringReply<'Expansion rate'>, NullReply | NumberReply]
]>;

export interface BfInfoReply {
capacity: NumberReply;
size: NumberReply;
numberOfFilters: NumberReply;
numberOfInsertedItems: NumberReply;
expansionRate: NullReply | NumberReply;
}

export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(key: RedisArgument) {
return ['BF.INFO', key];
parseCommand(parser: CommandParser, key: RedisArgument) {
parser.push('BF.INFO');
parser.pushKey(key);
},
transformReply: {
2: (reply: UnwrapReply<Resp2Reply<BfInfoReplyMap>>, _, typeMapping?: TypeMapping): BfInfoReplyMap => {
Expand Down
15 changes: 8 additions & 7 deletions packages/bloom/lib/commands/bloom/INSERT.spec.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,55 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import INSERT from './INSERT';
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';

describe('BF.INSERT', () => {
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
INSERT.transformArguments('key', 'item'),
parseArgs(INSERT, 'key', 'item'),
['BF.INSERT', 'key', 'ITEMS', 'item']
);
});

it('with CAPACITY', () => {
assert.deepEqual(
INSERT.transformArguments('key', 'item', { CAPACITY: 100 }),
parseArgs(INSERT, 'key', 'item', { CAPACITY: 100 }),
['BF.INSERT', 'key', 'CAPACITY', '100', 'ITEMS', 'item']
);
});

it('with ERROR', () => {
assert.deepEqual(
INSERT.transformArguments('key', 'item', { ERROR: 0.01 }),
parseArgs(INSERT, 'key', 'item', { ERROR: 0.01 }),
['BF.INSERT', 'key', 'ERROR', '0.01', 'ITEMS', 'item']
);
});

it('with EXPANSION', () => {
assert.deepEqual(
INSERT.transformArguments('key', 'item', { EXPANSION: 1 }),
parseArgs(INSERT, 'key', 'item', { EXPANSION: 1 }),
['BF.INSERT', 'key', 'EXPANSION', '1', 'ITEMS', 'item']
);
});

it('with NOCREATE', () => {
assert.deepEqual(
INSERT.transformArguments('key', 'item', { NOCREATE: true }),
parseArgs(INSERT, 'key', 'item', { NOCREATE: true }),
['BF.INSERT', 'key', 'NOCREATE', 'ITEMS', 'item']
);
});

it('with NONSCALING', () => {
assert.deepEqual(
INSERT.transformArguments('key', 'item', { NONSCALING: true }),
parseArgs(INSERT, 'key', 'item', { NONSCALING: true }),
['BF.INSERT', 'key', 'NONSCALING', 'ITEMS', 'item']
);
});

it('with CAPACITY, ERROR, EXPANSION, NOCREATE and NONSCALING', () => {
assert.deepEqual(
INSERT.transformArguments('key', 'item', {
parseArgs(INSERT, 'key', 'item', {
CAPACITY: 100,
ERROR: 0.01,
EXPANSION: 1,
Expand Down
28 changes: 15 additions & 13 deletions packages/bloom/lib/commands/bloom/INSERT.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RedisArgument, Command } from '@redis/client/dist/lib/RESP/types';
import { RedisVariadicArgument, pushVariadicArguments } from '@redis/client/dist/lib/commands/generic-transformers';
import { transformBooleanArrayReply } 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 { RedisVariadicArgument } from '@redis/client/lib/commands/generic-transformers';
import { transformBooleanArrayReply } from '@redis/client/lib/commands/generic-transformers';

export interface BfInsertOptions {
CAPACITY?: number;
Expand All @@ -11,37 +12,38 @@ export interface BfInsertOptions {
}

export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(
parseCommand(
parser: CommandParser,
key: RedisArgument,
items: RedisVariadicArgument,
options?: BfInsertOptions
) {
const args = ['BF.INSERT', key];
parser.push('BF.INSERT');
parser.pushKey(key);

if (options?.CAPACITY !== undefined) {
args.push('CAPACITY', options.CAPACITY.toString());
parser.push('CAPACITY', options.CAPACITY.toString());
}

if (options?.ERROR !== undefined) {
args.push('ERROR', options.ERROR.toString());
parser.push('ERROR', options.ERROR.toString());
}

if (options?.EXPANSION !== undefined) {
args.push('EXPANSION', options.EXPANSION.toString());
parser.push('EXPANSION', options.EXPANSION.toString());
}

if (options?.NOCREATE) {
args.push('NOCREATE');
parser.push('NOCREATE');
}

if (options?.NONSCALING) {
args.push('NONSCALING');
parser.push('NONSCALING');
}

args.push('ITEMS');
return pushVariadicArguments(args, items);
parser.push('ITEMS');
parser.pushVariadic(items);
},
transformReply: transformBooleanArrayReply
} as const satisfies Command;
3 changes: 2 additions & 1 deletion packages/bloom/lib/commands/bloom/LOADCHUNK.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import LOADCHUNK from './LOADCHUNK';
import { RESP_TYPES } from '@redis/client';
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';

describe('BF.LOADCHUNK', () => {
it('transformArguments', () => {
assert.deepEqual(
LOADCHUNK.transformArguments('key', 0, ''),
parseArgs(LOADCHUNK, 'key', 0, ''),
['BF.LOADCHUNK', 'key', '0', '']
);
});
Expand Down
10 changes: 6 additions & 4 deletions packages/bloom/lib/commands/bloom/LOADCHUNK.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
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, iterator: number, chunk: RedisArgument) {
return ['BF.LOADCHUNK', key, iterator.toString(), chunk];
parseCommand(parser: CommandParser, key: RedisArgument, iterator: number, chunk: RedisArgument) {
parser.push('BF.LOADCHUNK');
parser.pushKey(key);
parser.push(iterator.toString(), chunk);
},
transformReply: undefined as unknown as () => SimpleStringReply<'OK'>
} as const satisfies Command;
3 changes: 2 additions & 1 deletion packages/bloom/lib/commands/bloom/MADD.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import MADD from './MADD';
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';

describe('BF.MADD', () => {
it('transformArguments', () => {
assert.deepEqual(
MADD.transformArguments('key', ['1', '2']),
parseArgs(MADD, 'key', ['1', '2']),
['BF.MADD', 'key', '1', '2']
);
});
Expand Down
14 changes: 8 additions & 6 deletions packages/bloom/lib/commands/bloom/MADD.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { RedisArgument, Command } from '@redis/client/dist/lib/RESP/types';
import { RedisVariadicArgument, pushVariadicArguments } from '@redis/client/dist/lib/commands/generic-transformers';
import { transformBooleanArrayReply } 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 { RedisVariadicArgument } from '@redis/client/lib/commands/generic-transformers';
import { transformBooleanArrayReply } from '@redis/client/lib/commands/generic-transformers';

export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: false,
transformArguments(key: RedisArgument, items: RedisVariadicArgument) {
return pushVariadicArguments(['BF.MADD', key], items);
parseCommand(parser: CommandParser, key: RedisArgument, items: RedisVariadicArgument) {
parser.push('BF.MADD');
parser.pushKey(key);
parser.pushVariadic(items);
},
transformReply: transformBooleanArrayReply
} as const satisfies Command;
3 changes: 2 additions & 1 deletion packages/bloom/lib/commands/bloom/MEXISTS.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../../test-utils';
import MEXISTS from './MEXISTS';
import { parseArgs } from '@redis/client/lib/commands/generic-transformers';

describe('BF.MEXISTS', () => {
it('transformArguments', () => {
assert.deepEqual(
MEXISTS.transformArguments('key', ['1', '2']),
parseArgs(MEXISTS, 'key', ['1', '2']),
['BF.MEXISTS', 'key', '1', '2']
);
});
Expand Down
14 changes: 8 additions & 6 deletions packages/bloom/lib/commands/bloom/MEXISTS.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { RedisArgument, Command } from '@redis/client/dist/lib/RESP/types';
import { RedisVariadicArgument, pushVariadicArguments } from '@redis/client/dist/lib/commands/generic-transformers';
import { transformBooleanArrayReply } 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 { RedisVariadicArgument } from '@redis/client/lib/commands/generic-transformers';
import { transformBooleanArrayReply } from '@redis/client/lib/commands/generic-transformers';

export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(key: RedisArgument, items: RedisVariadicArgument) {
return pushVariadicArguments(['BF.MEXISTS', key], items);
parseCommand(parser: CommandParser, key: RedisArgument, items: RedisVariadicArgument) {
parser.push('BF.MEXISTS');
parser.pushKey(key);
parser.pushVariadic(items);
},
transformReply: transformBooleanArrayReply
} as const satisfies Command;
Loading

0 comments on commit 4708736

Please sign in to comment.