/
db.go
93 lines (74 loc) · 1.76 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
package ttdb
import (
"encoding/binary"
"os"
"path"
"path/filepath"
"time"
"github.com/alanxoc3/ttrack/internal/types"
bolt "go.etcd.io/bbolt"
)
func open(dir string) (*bolt.DB, error) {
dbpath := path.Join(dir, "db")
err := os.MkdirAll(filepath.Dir(dbpath), 0755)
if err != nil {
return nil, err
}
db, err := bolt.Open(dbpath, 0666, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
return nil, err
}
return db, nil
}
func GetSeconds(b *bolt.Bucket, key string) types.DaySeconds {
return types.CreateSecondsFromBytes(b.Get([]byte(key)))
}
func SetSeconds(b *bolt.Bucket, key string, secs types.DaySeconds) {
timeout_bytes := make([]byte, 4, 4)
binary.BigEndian.PutUint32(timeout_bytes[:], uint32(secs.GetAsUint32()))
if secs.IsZero() {
b.Delete([]byte(key))
} else {
b.Put([]byte(key), timeout_bytes) // TODO: Error handling.
}
}
// For adding seconds, do this:
// SetSeconds(b, date_key, GetSeconds(b, date_key).Add(secs))
func GetTimestamp(b *bolt.Bucket, key string) time.Time {
t := time.Time{}
b_val := b.Get([]byte(key))
if b_val != nil {
t.UnmarshalBinary(b_val) // TODO: Error handling.
}
return t
}
func SetTimestamp(b *bolt.Bucket, key string, t time.Time) {
raw, _ := t.MarshalBinary() // TODO: Error handling.
b.Put([]byte(key), raw) // TODO: Error handling.
}
func ViewCmd(cacheDir string, f func(*bolt.Tx) error) {
db, err := open(cacheDir)
if err != nil {
panic(err)
}
defer db.Close()
err = db.View(func(tx *bolt.Tx) error {
return f(tx)
})
if err != nil {
panic(err)
}
}
func UpdateCmd(cacheDir string, f func(*bolt.Tx) error) {
db, err := open(cacheDir)
if err != nil {
panic(err)
}
defer db.Close()
err = db.Update(func(tx *bolt.Tx) error {
return f(tx)
})
if err != nil {
panic(err)
}
}