-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdb.go
288 lines (252 loc) · 7.31 KB
/
db.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package main
import (
"bytes"
"encoding/gob"
"io/ioutil"
"os"
"path/filepath"
"time"
)
// Fugly solution, TODO switch to real DB like bolt
func setupDB() {
if logger != nil {
logger.Println("Reading in links and data from db")
}
for _, domain := range config.DomainNames {
restoreLinkLen(&domainLinkLens[domain].LinkLen1, "len1", domain)
restoreLinkLen(&domainLinkLens[domain].LinkLen2, "len2", domain)
restoreLinkLen(&domainLinkLens[domain].LinkLen3, "len3", domain)
restoreLinkLen(&domainLinkLens[domain].LinkCustom, "custom", domain)
}
}
func restoreLinkLen(l *LinkLen, typ, domain string) {
var backupLinkLen []Link
fileName := "backupdb-" + domain + "-" + typ + ".gob"
d, err := ioutil.ReadFile(filepath.Join(config.BaseDir, domain, fileName))
if err != nil && logger != nil {
logger.Println(err, "ReadFile - Skipping "+fileName)
} else {
buf := bytes.NewBuffer(d)
dec := gob.NewDecoder(buf)
err := dec.Decode(&backupLinkLen)
if err != nil && logger != nil {
logger.Println(err, "Unmarshal - Skipping"+fileName)
} else {
if len(backupLinkLen) > 0 && backupLinkLen[0].Key != "" {
l.NextClear = &backupLinkLen[0]
l.EndClear = &backupLinkLen[len(backupLinkLen)-1]
l.Links = len(backupLinkLen)
l.LinkMap[backupLinkLen[0].Key] = &backupLinkLen[0]
delete(l.FreeMap, backupLinkLen[0].Key)
}
for i := 1; i < len(backupLinkLen); i++ {
if backupLinkLen[i].Key != "" {
l.LinkMap[backupLinkLen[i].Key] = &backupLinkLen[i]
backupLinkLen[i-1].NextClear = &backupLinkLen[i]
delete(l.FreeMap, backupLinkLen[i].Key)
}
}
}
}
}
// part 2 of the fugly solution
func BackupRoutine() {
for {
time.Sleep(time.Minute * 30)
for _, domain := range config.DomainNames {
saveBackup(&domainLinkLens[domain].LinkLen1, "len1", domain)
saveBackup(&domainLinkLens[domain].LinkLen2, "len2", domain)
saveBackup(&domainLinkLens[domain].LinkLen3, "len3", domain)
saveBackup(&domainLinkLens[domain].LinkCustom, "custom", domain)
}
logger.Println("Finished saving new backup")
}
}
func saveBackup(l *LinkLen, typ, domain string) {
var err error
var backupLinkLen []Link
filename := "backupdb-" + domain + "-" + typ + ".gob"
if l == nil {
logger.Println("*LinkLen is nil, skipping ", filename)
return
}
if l.NextClear == nil {
logger.Println("l.NextClear is nil, skipping ", filename)
return
}
l.Mutex.Lock()
next := *l.NextClear
stop := false
for !stop {
backupLinkLen = append(backupLinkLen, next)
if next.NextClear != nil {
next = *next.NextClear
} else {
stop = true
}
}
l.Mutex.Unlock()
var backupBuffer bytes.Buffer
enc := gob.NewEncoder(&backupBuffer)
err = enc.Encode(backupLinkLen)
if err != nil {
logger.Println(err, "Error while saving backup in enc.Encode()")
}
backupLinkLen = nil
if err = os.WriteFile(filepath.Join(config.BaseDir, domain, filename), backupBuffer.Bytes(), 0644); err != nil && logger != nil {
logger.Println(err, "failed to save DB")
}
logger.Println("Backed up:", filename)
}
// New BoltDB restore
//startRestoreDB(&domainLinkLens[domain].LinkLen1, domain, "linkLen1")
//startRestoreDB(&domainLinkLens[domain].LinkLen2, domain, "linkLen2")
//startRestoreDB(&domainLinkLens[domain].LinkLen3, domain, "linkLen3")
//startRestoreDB(&domainLinkLens[domain].LinkCustom, domain, "linkCustom")
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
// new bolt implementation of backup ============
// Config2 type
/*
type Config2 struct {
Height float64 `json:"height"`
Birthday time.Time `json:"birthday"`
}
// Entry type
type Entry struct {
Calories int `json:"calories"`
Food string `json:"food"`
}
func startRestoreDB(l *LinkLen, domain, linkLen string) {
db := setupDB2(domain)
defer db.Close()
restoreDBLinkLen(db, domain)
}
// restoreDBLinkLen will read out all links for all linkLen from the bolt.DB for the specified domain and populate the domainLinkLens map
func restoreDBLinkLen(db *bolt.DB, domain string) {
err := db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(domain)).Bucket([]byte("linkLen1"))
b.ForEach(func(k, v []byte) error {
// TODO Restore entries from DB HERE
var lnk Link
err := json.Unmarshal(v, &lnk)
if err != nil {
logger.Fatalln("Unable to restore link,", err)
}
l1 := &domainLinkLens[domain].LinkLen1
l1.Add(&lnk)
if len(backupLinkLen) > 0 && backupLinkLen[0].Key != "" {
l.NextClear = &backupLinkLen[0]
l.EndClear = &backupLinkLen[len(backupLinkLen)-1]
l.Links = len(backupLinkLen)
l.LinkMap[backupLinkLen[0].Key] = &backupLinkLen[0]
delete(l.FreeMap, backupLinkLen[0].Key)
}
for i := 1; i < len(backupLinkLen); i++ {
if backupLinkLen[i].Key != "" {
l.LinkMap[backupLinkLen[i].Key] = &backupLinkLen[i]
backupLinkLen[i-1].NextClear = &backupLinkLen[i]
delete(l.FreeMap, backupLinkLen[i].Key)
}
}
fmt.Println(string(k), string(v))
return nil
})
return nil
})
if err != nil {
log.Fatal(err)
}
}
func setupDB2(domain string) *bolt.DB {
db, err := bolt.Open(filepath.Join(config.BaseDir, domain, domain+".db"), 0600, nil)
if err != nil && logger != nil {
logger.Fatalln("could not open db,", err)
}
err = db.Update(func(tx *bolt.Tx) error {
root, err := tx.CreateBucketIfNotExists([]byte(domain))
if err != nil {
logger.Fatalln("could not create root bucket:", err)
}
_, err = root.CreateBucketIfNotExists([]byte("linkLen1"))
if err != nil {
logger.Fatalln("could not create linkLen1 bucket:", err)
}
_, err = root.CreateBucketIfNotExists([]byte("linkLen2"))
if err != nil {
logger.Fatalln("could not create linkLen2 bucket:", err)
}
_, err = root.CreateBucketIfNotExists([]byte("linkLen3"))
if err != nil {
logger.Fatalln("could not create linkLen3 bucket:", err)
}
_, err = root.CreateBucketIfNotExists([]byte("linkCustom"))
if err != nil {
logger.Fatalln("could not create linkCustom bucket:", err)
}
return nil
})
if err != nil {
logger.Fatalln("could not set up buckets,", err)
}
logger.Println("")
fmt.Println("DB Setup for", domain, "Done")
return db
}
func setConfig2(db *bolt.DB, Config2 Config2) error {
confBytes, err := json.Marshal(Config2)
if err != nil {
return fmt.Errorf("could not marshal Config2 json: %v", err)
}
err = db.Update(func(tx *bolt.Tx) error {
err = tx.Bucket([]byte("DB")).Put([]byte("CONFIG"), confBytes)
if err != nil {
return fmt.Errorf("could not set Config2: %v", err)
}
return nil
})
fmt.Println("Set Config2")
return err
}
func addWeight(db *bolt.DB, weight string, date time.Time) error {
err := db.Update(func(tx *bolt.Tx) error {
err := tx.Bucket([]byte("DB")).Bucket([]byte("WEIGHT")).Put([]byte(date.Format(time.RFC3339)), []byte(weight))
if err != nil {
return fmt.Errorf("could not insert weight: %v", err)
}
return nil
})
fmt.Println("Added Weight")
return err
}
func addEntry(db *bolt.DB, calories int, food string, date time.Time) error {
entry := Entry{Calories: calories, Food: food}
entryBytes, err := json.Marshal(entry)
if err != nil {
return fmt.Errorf("could not marshal entry json: %v", err)
}
err = db.Update(func(tx *bolt.Tx) error {
err := tx.Bucket([]byte("DB")).Bucket([]byte("ENTRIES")).Put([]byte(date.Format(time.RFC3339)), entryBytes)
if err != nil {
return fmt.Errorf("could not insert entry: %v", err)
}
return nil
})
fmt.Println("Added Entry")
return err
}
*/