Skip to content

HSCAN NOVALUES support (v5) #2758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/client/lib/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,19 @@ export default class RedisClient<
} while (cursor !== '0');
}

async* hScanValuesIterator(
this: RedisClientType<M, F, S, RESP, TYPE_MAPPING>,
key: RedisArgument,
options?: ScanCommonOptions & ScanIteratorOptions
) {
let cursor = options?.cursor ?? '0';
do {
const reply = await this.hScanNoValues(key, cursor, options);
cursor = reply.cursor;
yield reply.fields;
} while (cursor !== '0');
}

async* sScanIterator(
this: RedisClientType<M, F, S, RESP, TYPE_MAPPING>,
key: RedisArgument,
Expand Down
56 changes: 56 additions & 0 deletions packages/client/lib/commands/HSCAN_NOVALUES.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import HSCAN_NOVALUES from './HSCAN_NOVALUES';

describe('HSCAN_NOVALUES', () => {
describe('transformArguments', () => {
it('cusror only', () => {
assert.deepEqual(
HSCAN_NOVALUES.transformArguments('key', '0'),
['HSCAN', 'key', '0', 'NOVALUES']
);
});

it('with MATCH', () => {
assert.deepEqual(
HSCAN_NOVALUES.transformArguments('key', '0', {
MATCH: 'pattern'
}),
['HSCAN', 'key', '0', 'MATCH', 'pattern', 'NOVALUES']
);
});

it('with COUNT', () => {
assert.deepEqual(
HSCAN_NOVALUES.transformArguments('key', '0', {
COUNT: 1
}),
['HSCAN', 'key', '0', 'COUNT', '1', 'NOVALUES']
);
});

it('with MATCH & COUNT', () => {
assert.deepEqual(
HSCAN_NOVALUES.transformArguments('key', '0', {
MATCH: 'pattern',
COUNT: 1
}),
['HSCAN', 'key', '0', 'MATCH', 'pattern', 'COUNT', '1', 'NOVALUES']
);
});
});

testUtils.testWithClient('client.hScanNoValues', async client => {
const [, reply] = await Promise.all([
client.hSet('key', 'field', 'value'),
client.hScanNoValues('key', '0')
]);

assert.deepEqual(reply, {
cursor: '0',
fields: [
'field',
]
});
}, GLOBAL.SERVERS.OPEN);
});
22 changes: 22 additions & 0 deletions packages/client/lib/commands/HSCAN_NOVALUES.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { RedisArgument, BlobStringReply, Command } from '../RESP/types';
import { ScanCommonOptions, pushScanArguments } from './SCAN';

export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(
key: RedisArgument,
cursor: RedisArgument,
options?: ScanCommonOptions
) {
const args = pushScanArguments(['HSCAN', key], cursor, options);
args.push('NOVALUES');
return args;
},
transformReply([cursor, fields]: [BlobStringReply, Array<BlobStringReply>]) {
return {
cursor,
fields
};
}
} as const satisfies Command;
3 changes: 3 additions & 0 deletions packages/client/lib/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ import HRANDFIELD_COUNT_WITHVALUES from './HRANDFIELD_COUNT_WITHVALUES';
import HRANDFIELD_COUNT from './HRANDFIELD_COUNT';
import HRANDFIELD from './HRANDFIELD';
import HSCAN from './HSCAN';
import HSCAN_NOVALUES from './HSCAN_NOVALUES';
import HSET from './HSET';
import HSETNX from './HSETNX';
import HSTRLEN from './HSTRLEN';
Expand Down Expand Up @@ -623,6 +624,8 @@ export default {
hRandField: HRANDFIELD,
HSCAN,
hScan: HSCAN,
HSCAN_NOVALUES,
hScanNoValues: HSCAN_NOVALUES,
HSET,
hSet: HSET,
HSETNX,
Expand Down