-
Notifications
You must be signed in to change notification settings - Fork 29
/
db_load.go
170 lines (142 loc) · 3.14 KB
/
db_load.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
package flashdb
import (
"time"
"github.com/arriqaaq/aol"
)
// load String, Hash, Set and ZSet stores from append-only log
func (db *FlashDB) load() error {
if db.log == nil {
return nil
}
noOfSegments := db.log.Segments()
for i := 1; i <= noOfSegments; i++ {
j := 0
for {
data, err := db.log.Read(uint64(i), uint64(j))
if err != nil {
if err == aol.ErrEOF {
break
}
return err
}
record, err := decode(data)
if err != nil {
return err
}
if len(record.meta.key) > 0 {
if err := db.loadRecord(record); err != nil {
return err
}
}
j++
}
}
return nil
}
func (db *FlashDB) loadRecord(r *record) (err error) {
switch r.getType() {
case StringRecord:
err = db.buildStringRecord(r)
case HashRecord:
err = db.buildHashRecord(r)
case SetRecord:
err = db.buildSetRecord(r)
case ZSetRecord:
err = db.buildZsetRecord(r)
}
return
}
/*
Utility functions to build stores from aol Record
*/
func (db *FlashDB) buildStringRecord(r *record) error {
key := string(r.meta.key)
member := string(r.meta.member)
switch r.getMark() {
case StringSet:
db.strStore.Insert([]byte(key), member)
case StringRem:
db.strStore.Delete([]byte(key))
db.exps.HDel(String, key)
case StringExpire:
if r.timestamp < uint64(time.Now().Unix()) {
db.strStore.Delete([]byte(key))
db.exps.HDel(String, key)
} else {
db.setTTL(String, key, int64(r.timestamp))
}
}
return nil
}
func (db *FlashDB) buildHashRecord(r *record) error {
key := string(r.meta.key)
member := string(r.meta.member)
value := string(r.meta.value)
switch r.getMark() {
case HashHSet:
db.hashStore.HSet(key, member, value)
case HashHDel:
db.hashStore.HDel(key, member)
case HashHClear:
db.hashStore.HClear(key)
db.exps.HDel(Hash, key)
case HashHExpire:
if r.timestamp < uint64(time.Now().Unix()) {
db.hashStore.HClear(key)
db.exps.HDel(Hash, key)
} else {
db.setTTL(Hash, key, int64(r.timestamp))
}
}
return nil
}
func (db *FlashDB) buildSetRecord(r *record) error {
key := string(r.meta.key)
member := string(r.meta.member)
value := string(r.meta.value)
switch r.getMark() {
case SetSAdd:
db.setStore.SAdd(key, member)
case SetSRem:
db.setStore.SRem(key, member)
case SetSMove:
db.setStore.SMove(key, value, member)
case SetSClear:
db.setStore.SClear(key)
db.exps.HDel(Set, key)
case SetSExpire:
if r.timestamp < uint64(time.Now().Unix()) {
db.setStore.SClear(key)
db.exps.HDel(Set, key)
} else {
db.setTTL(Set, key, int64(r.timestamp))
}
}
return nil
}
func (db *FlashDB) buildZsetRecord(r *record) error {
key := string(r.meta.key)
member := string(r.meta.member)
value := string(r.meta.value)
switch r.getMark() {
case ZSetZAdd:
score, err := strToFloat64(value)
if err != nil {
return err
}
db.zsetStore.ZAdd(key, score, member, nil)
case ZSetZRem:
db.zsetStore.ZRem(key, member)
case ZSetZClear:
db.zsetStore.ZClear(key)
db.exps.HDel(ZSet, key)
case ZSetZExpire:
if r.timestamp < uint64(time.Now().Unix()) {
db.zsetStore.ZClear(key)
db.exps.HDel(ZSet, key)
} else {
db.setTTL(ZSet, key, int64(r.timestamp))
}
}
return nil
}