-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdb.go
192 lines (166 loc) · 3.71 KB
/
rdb.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"fmt"
"log"
"os"
"strconv"
"syscall"
"time"
"unsafe"
"github.com/BitInit/fake-redis/dict"
"github.com/BitInit/fake-redis/rdb"
)
const (
rdbOpCodeAux = 250
rdbOpCodeResizeDB = 251
rdbOpCodeExpireTimeMS = 252
rdbOpCodeSelectDB = 254
rdbOpCodeEOF = 255
)
const (
rdbTypeString = 0
)
const RDB_VERSION = 9
const RDB_OPCODE_AUX = 250
const RDB_6BITLEN = 0
const RDB_14BITLEN = 1
const RDB_32BITLEN = 0x80
const RDB_64BITLEN = 0x81
func saveCommand(c *client) {
if server.rdb_child_pid != -1 {
addReplyError(c, "Background save already in progress")
}
if rdbSave(server.rdb_filename) {
addReply(c, shared.ok)
} else {
addReply(c, shared.err)
}
}
func rdbSave(filename string) bool {
tmpfile := fmt.Sprintf("temp-%d.rdb", syscall.Getpid())
f, err := os.Create(tmpfile)
if err != nil {
log.Printf("Failed opening the RDB file %s\n", tmpfile)
return false
}
defer f.Close()
r := rdb.InitWithFile(f)
if err := rdbSaveRio(r); err != nil {
return false
}
if err := os.Rename(tmpfile, filename); err != nil {
// TODO unlink
return false
}
return true
}
func rdbSaveRio(r *rdb.Rdb) error {
magic := fmt.Sprintf("REDIS%04d", RDB_VERSION)
if _, err := r.WriteRaw([]byte(magic)); err != nil {
return err
}
if err := rdbSaveInfoAuxFields(r); err != nil {
return err
}
for i := 0; i < server.dbnum; i++ {
db := server.db[i]
saveDb(r, i, db.dict, db.expires)
}
return nil
}
func saveDb(r *rdb.Rdb, i int, kv *dict.Dict, expires *dict.Dict) error {
dbSize := kv.Size()
expiresSize := expires.Size()
if dbSize == 0 {
return nil
}
if err := r.SaveType(rdbOpCodeSelectDB); err != nil {
return err
}
if _, err := r.SaveLen(uint64(i)); err != nil {
return err
}
if err := r.SaveType(rdbOpCodeResizeDB); err != nil {
return err
}
if _, err := r.SaveLen(uint64(dbSize)); err != nil {
return err
}
if _, err := r.SaveLen(uint64(expiresSize)); err != nil {
return err
}
di := kv.GetSafeInterator()
for de := di.Next(); de != nil; de = di.Next() {
key := de.GetKey().([]byte)
val := de.GetVal().(*robj)
expire := expires.GetVal(key)
if expire != nil {
if err := r.SaveType(rdbOpCodeExpireTimeMS); err != nil {
return err
}
et := expire.(int64)
if _, err := r.SaveLen(uint64(et)); err != nil {
return err
}
}
if err := saveObjectType(r, val); err != nil {
return err
}
if err := r.SaveRawString(string(key)); err != nil {
return err
}
if err := saveObject(r, val); err != nil {
return err
}
}
if err := r.SaveType(rdbOpCodeEOF); err != nil {
return err
}
if err := r.SaveCheckSum(); err != nil {
return err
}
return nil
}
func rdbSaveInfoAuxFields(r *rdb.Rdb) error {
redisBits := unsafe.Sizeof(r)
if err := saveAuxField(r, "redis-ver", VERSION); err != nil {
return err
}
if err := rdbSaveAuxFieldStrInt(r, "redis-bits", int(redisBits)); err != nil {
return err
}
if err := rdbSaveAuxFieldStrInt(r, "ctime", int(time.Now().UnixMilli())); err != nil {
return err
}
return nil
}
func rdbSaveAuxFieldStrInt(r *rdb.Rdb, key string, val int) error {
v := strconv.Itoa(val)
return saveAuxField(r, key, v)
}
func saveAuxField(r *rdb.Rdb, key string, val string) error {
if err := r.SaveType(rdbOpCodeAux); err != nil {
return err
}
if err := r.SaveRawString(key); err != nil {
return err
}
if err := r.SaveRawString(val); err != nil {
return err
}
return nil
}
func saveObjectType(r *rdb.Rdb, o *robj) error {
switch o.tp {
case OBJ_STRING:
return r.SaveType(rdbTypeString)
// other type
}
return nil
}
func saveObject(r *rdb.Rdb, val *robj) error {
if val.tp == OBJ_STRING {
return r.SaveRawString(string(val.ptr.([]byte)))
}
return fmt.Errorf("no support type")
}