-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_flush.go
106 lines (91 loc) · 2.19 KB
/
db_flush.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package storager
import (
"context"
"fmt"
)
// FlushDB flushes the data.
func (db *DB) FlushDB(ctx context.Context) (drop int64, err error) {
all := [](func(ctx context.Context) (int64, error)){
db.stringFlush,
db.listFlush,
db.hashFlush,
db.setFlush,
db.zsetFlush,
}
for _, flush := range all {
n, e := flush(ctx)
if e != nil {
err = e
return
}
drop += n
}
return
}
func (db *DB) flushType(ctx context.Context, t *Batch, dataType byte) (drop int64, err error) {
var deleteFunc func(t *Batch, key []byte) (int64, error)
var metaDataType byte
switch dataType {
case StringType:
deleteFunc = db.string.delete
metaDataType = StringType
case ListType:
deleteFunc = db.list.delete
metaDataType = LMetaType
case HashType:
deleteFunc = db.hash.delete
metaDataType = HSizeType
case SetType:
deleteFunc = db.set.delete
metaDataType = SSizeType
case ZSetType:
deleteFunc = db.zset.delete
metaDataType = ZSizeType
default:
return 0, fmt.Errorf("invalid data type: %s", TypeName[dataType])
}
var keys [][]byte
keys, err = db.scanGeneric(metaDataType, nil, 1024, false, "", false)
for len(keys) != 0 && err == nil {
for _, key := range keys {
deleteFunc(t, key)
db.rmExpire(t, dataType, key)
}
if err = t.Commit(ctx); err != nil {
return
}
drop += int64(len(keys))
keys, err = db.scanGeneric(metaDataType, nil, 1024, false, "", false)
}
return
}
func (db *DB) stringFlush(ctx context.Context) (drop int64, err error) {
t := db.string.batch
t.Lock()
defer t.Unlock()
return db.flushType(ctx, t, StringType)
}
func (db *DB) listFlush(ctx context.Context) (drop int64, err error) {
t := db.list.batch
t.Lock()
defer t.Unlock()
return db.flushType(ctx, t, ListType)
}
func (db *DB) hashFlush(ctx context.Context) (drop int64, err error) {
t := db.hash.batch
t.Lock()
defer t.Unlock()
return db.flushType(ctx, t, HashType)
}
func (db *DB) setFlush(ctx context.Context) (drop int64, err error) {
t := db.set.batch
t.Lock()
defer t.Unlock()
return db.flushType(ctx, t, SetType)
}
func (db *DB) zsetFlush(ctx context.Context) (drop int64, err error) {
t := db.zset.batch
t.Lock()
defer t.Unlock()
return db.flushType(ctx, t, ZSetType)
}