Skip to content

Commit 7340bdf

Browse files
enhance scan for keys mechanism
1 parent 995fa28 commit 7340bdf

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

redisinsight/api/src/modules/database-analysis/models/scan-filter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class ScanFilter {
4646
* Generate scan args array for filter
4747
*/
4848
getScanArgsArray(): Array<number | string> {
49-
const args = ['count', this.count, 'match', this.match];
49+
const args = ['match', this.match];
5050

5151
if (this.type) {
5252
args.push('type', this.type);

redisinsight/api/src/modules/database-analysis/scanner/keys-scanner.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import { Injectable } from '@nestjs/common';
22
import { getTotalKeys } from 'src/modules/redis/utils';
33
import { KeyInfoProvider } from 'src/modules/database-analysis/scanner/key-info/key-info.provider';
44
import { RedisClient, RedisClientConnectionType, RedisClientNodeRole } from 'src/modules/redis/client';
5+
import { ScanFilter } from 'src/modules/database-analysis/models/scan-filter';
56

67
@Injectable()
78
export class KeysScanner {
89
constructor(
910
private readonly keyInfoProvider: KeyInfoProvider,
1011
) {}
1112

12-
async scan(client: RedisClient, opts: any) {
13+
async scan(client: RedisClient, opts: { filter: ScanFilter }) {
1314
let nodes = [];
1415

1516
if (client.getConnectionType() === RedisClientConnectionType.CLUSTER) {
@@ -21,7 +22,7 @@ export class KeysScanner {
2122
return Promise.all(nodes.map((node) => this.nodeScan(node, opts)));
2223
}
2324

24-
async nodeScan(client: RedisClient, opts: any) {
25+
async nodeScan(client: RedisClient, opts: { filter: ScanFilter }) {
2526
const total = await getTotalKeys(client);
2627
let indexes: string[];
2728
let libraries: string[];
@@ -44,12 +45,29 @@ export class KeysScanner {
4445
// Ignore errors
4546
}
4647

47-
const [
48-
,
49-
keys,
50-
] = await client.sendCommand([
51-
'scan', 0, ...opts.filter.getScanArgsArray(),
52-
]) as [string, Buffer[]];
48+
let keys = [];
49+
const COUNT = Math.min(2000, opts.filter.count);
50+
let scanned = 0;
51+
let cursor: number;
52+
53+
while (
54+
scanned < opts.filter.count
55+
&& cursor !== 0
56+
) {
57+
const [
58+
cursorResp,
59+
keysResp,
60+
] = await client.sendCommand([
61+
'scan',
62+
cursor || 0,
63+
'count', COUNT,
64+
...opts.filter.getScanArgsArray(),
65+
]) as [string, Buffer[]];
66+
67+
cursor = parseInt(cursorResp, 10) || 0;
68+
scanned += COUNT;
69+
keys = keys.concat(keysResp);
70+
}
5371

5472
const [sizes, types, ttls] = await Promise.all([
5573
client.sendPipeline(

0 commit comments

Comments
 (0)