Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class ScanFilter {
* Generate scan args array for filter
*/
getScanArgsArray(): Array<number | string> {
const args = ['count', this.count, 'match', this.match];
const args = ['match', this.match];

if (this.type) {
args.push('type', this.type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { Injectable } from '@nestjs/common';
import { getTotalKeys } from 'src/modules/redis/utils';
import { KeyInfoProvider } from 'src/modules/database-analysis/scanner/key-info/key-info.provider';
import { RedisClient, RedisClientConnectionType, RedisClientNodeRole } from 'src/modules/redis/client';
import { ScanFilter } from 'src/modules/database-analysis/models/scan-filter';

@Injectable()
export class KeysScanner {
constructor(
private readonly keyInfoProvider: KeyInfoProvider,
) {}

async scan(client: RedisClient, opts: any) {
async scan(client: RedisClient, opts: { filter: ScanFilter }) {
let nodes = [];

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

async nodeScan(client: RedisClient, opts: any) {
async nodeScan(client: RedisClient, opts: { filter: ScanFilter }) {
const total = await getTotalKeys(client);
let indexes: string[];
let libraries: string[];
Expand All @@ -44,12 +45,29 @@ export class KeysScanner {
// Ignore errors
}

const [
,
keys,
] = await client.sendCommand([
'scan', 0, ...opts.filter.getScanArgsArray(),
]) as [string, Buffer[]];
let keys = [];
const COUNT = Math.min(2000, opts.filter.count);
let scanned = 0;
let cursor: number;

while (
scanned < opts.filter.count
&& cursor !== 0
) {
const [
cursorResp,
keysResp,
] = await client.sendCommand([
'scan',
cursor || 0,
'count', COUNT,
...opts.filter.getScanArgsArray(),
]) as [string, Buffer[]];

cursor = parseInt(cursorResp, 10) || 0;
scanned += COUNT;
keys = keys.concat(keysResp);
}

const [sizes, types, ttls] = await Promise.all([
client.sendPipeline(
Expand Down