@@ -12,13 +12,6 @@ import (
12
12
radix "github.com/mediocregopher/radix/v3"
13
13
)
14
14
15
- func min (a , b int ) int {
16
- if a <= b {
17
- return a
18
- }
19
- return b
20
- }
21
-
22
15
func ttlToRedisCmd (k string , val int64 ) []string {
23
16
return []string {"EXPIREAT" , k , fmt .Sprint (time .Now ().Unix () + val )}
24
17
}
@@ -160,7 +153,8 @@ func dumpKeysWorker(client radix.Client, keyBatches <-chan []string, withTTL boo
160
153
// and can be used to provide a progress visualisation such as a progress bar.
161
154
// Done is the number of items dumped, Total is the total number of items to dump.
162
155
type ProgressNotification struct {
163
- Done , Total int
156
+ Db uint8
157
+ Done int
164
158
}
165
159
166
160
func parseKeyspaceInfo (keyspaceInfo string ) ([]uint8 , error ) {
@@ -205,14 +199,9 @@ func getDBIndexes(redisURL string) ([]uint8, error) {
205
199
return parseKeyspaceInfo (keyspaceInfo )
206
200
}
207
201
208
- func scanKeys (client radix.Client , keyBatches chan <- []string , progressNotifications chan <- ProgressNotification ) error {
202
+ func scanKeys (client radix.Client , db uint8 , filter string , keyBatches chan <- []string , progressNotifications chan <- ProgressNotification ) error {
209
203
keyBatchSize := 100
210
- s := radix .NewScanner (client , radix.ScanOpts {Command : "SCAN" , Count : keyBatchSize })
211
-
212
- var dbSize int
213
- if err := client .Do (radix .Cmd (& dbSize , "DBSIZE" )); err != nil {
214
- return err
215
- }
204
+ s := radix .NewScanner (client , radix.ScanOpts {Command : "SCAN" , Pattern : filter , Count : keyBatchSize })
216
205
217
206
nProcessed := 0
218
207
var key string
@@ -223,19 +212,31 @@ func scanKeys(client radix.Client, keyBatches chan<- []string, progressNotificat
223
212
nProcessed += len (keyBatch )
224
213
keyBatches <- keyBatch
225
214
keyBatch = nil
226
- progressNotifications <- ProgressNotification {nProcessed , dbSize }
215
+ progressNotifications <- ProgressNotification {Db : db , Done : nProcessed }
227
216
}
228
217
}
229
218
230
219
keyBatches <- keyBatch
231
220
nProcessed += len (keyBatch )
232
- progressNotifications <- ProgressNotification {nProcessed , dbSize }
221
+ progressNotifications <- ProgressNotification {Db : db , Done : nProcessed }
233
222
234
223
return s .Close ()
235
224
}
236
225
226
+ // RedisURL builds a connect URL given a Host, port, db & password
227
+ func RedisURL (redisHost string , redisPort string , redisDB string , redisPassword string ) string {
228
+ switch {
229
+ case redisDB == "" :
230
+ return "redis://:" + redisPassword + "@" + redisHost + ":" + fmt .Sprint (redisPort )
231
+ case redisDB != "" :
232
+ return "redis://:" + redisPassword + "@" + redisHost + ":" + fmt .Sprint (redisPort ) + "/" + redisDB
233
+ }
234
+
235
+ return ""
236
+ }
237
+
237
238
// DumpDB dumps all keys from a single Redis DB
238
- func DumpDB (redisURL string , nWorkers int , withTTL bool , logger * log.Logger , serializer func ([]string ) string , progress chan <- ProgressNotification ) error {
239
+ func DumpDB (redisHost string , redisPort int , redisPassword string , db uint8 , filter string , nWorkers int , withTTL bool , logger * log.Logger , serializer func ([]string ) string , progress chan <- ProgressNotification ) error {
239
240
var err error
240
241
241
242
errors := make (chan error )
@@ -247,15 +248,13 @@ func DumpDB(redisURL string, nWorkers int, withTTL bool, logger *log.Logger, ser
247
248
}
248
249
}()
249
250
251
+ redisURL := RedisURL (redisHost , fmt .Sprint (redisPort ), fmt .Sprint (db ), redisPassword )
250
252
client , err := radix .NewPool ("tcp" , redisURL , nWorkers )
251
253
if err != nil {
252
254
return err
253
255
}
254
256
defer client .Close ()
255
257
256
- splitURL := strings .Split (redisURL , "/" )
257
- db := splitURL [len (splitURL )- 1 ]
258
-
259
258
if err = client .Do (radix .Cmd (nil , "SELECT" , fmt .Sprint (db ))); err != nil {
260
259
return err
261
260
}
@@ -267,7 +266,7 @@ func DumpDB(redisURL string, nWorkers int, withTTL bool, logger *log.Logger, ser
267
266
go dumpKeysWorker (client , keyBatches , withTTL , logger , serializer , errors , done )
268
267
}
269
268
270
- scanKeys (client , keyBatches , progress )
269
+ scanKeys (client , db , filter , keyBatches , progress )
271
270
close (keyBatches )
272
271
273
272
for i := 0 ; i < nWorkers ; i ++ {
@@ -277,30 +276,18 @@ func DumpDB(redisURL string, nWorkers int, withTTL bool, logger *log.Logger, ser
277
276
return nil
278
277
}
279
278
280
- func RedisURL (redisHost string , redisPort string , redisDB string , redisPassword string ) string {
281
- switch {
282
- case redisDB == "" :
283
- return "redis://:" + redisPassword + "@" + redisHost + ":" + fmt .Sprint (redisPort )
284
- case redisDB != "" :
285
- return "redis://:" + redisPassword + "@" + redisHost + ":" + fmt .Sprint (redisPort ) + "/" + redisDB
286
- }
287
-
288
- return ""
289
- }
290
-
291
279
// DumpServer dumps all Keys from the redis server given by redisURL,
292
280
// to the Logger logger. Progress notification informations
293
281
// are regularly sent to the channel progressNotifications
294
- func DumpServer (redisHost string , redisPort int , redisPassword string , nWorkers int , withTTL bool , logger * log.Logger , serializer func ([]string ) string , progress chan <- ProgressNotification ) error {
282
+ func DumpServer (redisHost string , redisPort int , redisPassword string , filter string , nWorkers int , withTTL bool , logger * log.Logger , serializer func ([]string ) string , progress chan <- ProgressNotification ) error {
295
283
url := RedisURL (redisHost , fmt .Sprint (redisPort ), "" , redisPassword )
296
284
dbs , err := getDBIndexes (url )
297
285
if err != nil {
298
286
return err
299
287
}
300
288
301
289
for _ , db := range dbs {
302
- url = RedisURL (redisHost , fmt .Sprint (redisPort ), fmt .Sprint (db ), redisPassword )
303
- if err = DumpDB (url , nWorkers , withTTL , logger , serializer , progress ); err != nil {
290
+ if err = DumpDB (redisHost , redisPort , redisPassword , db , filter , nWorkers , withTTL , logger , serializer , progress ); err != nil {
304
291
return err
305
292
}
306
293
}
0 commit comments