@@ -2,14 +2,15 @@ import { Injectable } from '@nestjs/common';
2
2
import { getTotalKeys } from 'src/modules/redis/utils' ;
3
3
import { KeyInfoProvider } from 'src/modules/database-analysis/scanner/key-info/key-info.provider' ;
4
4
import { RedisClient , RedisClientConnectionType , RedisClientNodeRole } from 'src/modules/redis/client' ;
5
+ import { ScanFilter } from 'src/modules/database-analysis/models/scan-filter' ;
5
6
6
7
@Injectable ( )
7
8
export class KeysScanner {
8
9
constructor (
9
10
private readonly keyInfoProvider : KeyInfoProvider ,
10
11
) { }
11
12
12
- async scan ( client : RedisClient , opts : any ) {
13
+ async scan ( client : RedisClient , opts : { filter : ScanFilter } ) {
13
14
let nodes = [ ] ;
14
15
15
16
if ( client . getConnectionType ( ) === RedisClientConnectionType . CLUSTER ) {
@@ -21,7 +22,7 @@ export class KeysScanner {
21
22
return Promise . all ( nodes . map ( ( node ) => this . nodeScan ( node , opts ) ) ) ;
22
23
}
23
24
24
- async nodeScan ( client : RedisClient , opts : any ) {
25
+ async nodeScan ( client : RedisClient , opts : { filter : ScanFilter } ) {
25
26
const total = await getTotalKeys ( client ) ;
26
27
let indexes : string [ ] ;
27
28
let libraries : string [ ] ;
@@ -44,12 +45,29 @@ export class KeysScanner {
44
45
// Ignore errors
45
46
}
46
47
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
+ }
53
71
54
72
const [ sizes , types , ttls ] = await Promise . all ( [
55
73
client . sendPipeline (
0 commit comments