/
bolt.go
191 lines (174 loc) · 4.55 KB
/
bolt.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
package bolt
import (
"fmt"
"io"
"os"
"time"
"github.com/boltdb/bolt"
"github.com/golang/protobuf/proto"
"github.com/octavore/naga/service"
"github.com/octavore/nagax/logger"
"github.com/octavore/nagax/util/errors"
"github.com/ketchuphq/ketchup/db"
"github.com/ketchuphq/ketchup/proto/ketchup/models"
"github.com/ketchuphq/ketchup/server/config"
)
type BoltConfig struct {
Bolt struct {
Path string `json:"path"`
} `json:"bolt"`
}
// Module bolt provides methods for writing and retrieving
// data from a bolt database.
type Module struct {
Bolt *bolt.DB
Backend *db.Module
ConfigModule *config.Module
Logger *logger.Module
config BoltConfig
}
// Init implements service.Init
func (m *Module) Init(c *service.Config) {
c.Setup = func() (err error) {
m.Backend.Register(m)
err = m.ConfigModule.ReadConfig(&m.config)
if err != nil {
return err
}
if c.Env().IsTest() {
return
}
m.config.Bolt.Path = m.ConfigModule.DataPath(m.config.Bolt.Path, "default.db")
return m.connectBolt(m.config.Bolt.Path)
}
var testDB string
c.SetupTest = func() {
suffix := time.Now().Format("20060102150405.999")
testDB = fmt.Sprintf("test-%s.db", suffix)
err := os.RemoveAll(testDB)
if err != nil {
panic(err)
}
err = m.connectBolt(testDB)
if err != nil {
panic(err)
}
return
}
c.Stop = func() {
err := m.Bolt.Close()
if err != nil {
panic(err)
}
if testDB != "" {
err := os.RemoveAll(testDB)
if err != nil {
panic(err)
}
}
}
}
// ErrNoKey is a helper to create key not found errors.
type ErrNoKey string
func (e ErrNoKey) Error() string {
return fmt.Sprintf("boltdb: key not found: %s", string(e))
}
func (m *Module) connectBolt(path string) error {
var err error
m.Bolt, err = bolt.Open(path, os.ModePerm, &bolt.Options{
Timeout: 5 * time.Second,
})
if err != nil {
if err == bolt.ErrTimeout {
m.Logger.Error("bolt: timeout while connecting; it may be that the database is already in use by another process.")
}
return errors.Wrap(err)
}
return m.Bolt.Update(func(tx *bolt.Tx) error {
buckets := []string{PAGE_BUCKET, ROUTE_BUCKET, USER_BUCKET, DATA_BUCKET, FILES_BUCKET}
for _, bucket := range buckets {
_, err := tx.CreateBucketIfNotExists([]byte(bucket))
if err != nil {
return errors.Wrap(err)
}
}
return nil
})
}
// Get is a generic method of getting a proto from the bolt database.
func (m *Module) Get(bucket, key string, pb proto.Message) error {
return m.Bolt.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucket))
data := b.Get([]byte(key))
if data == nil {
return errors.Wrap(ErrNoKey(bucket + ":" + key))
}
return proto.Unmarshal(data, pb)
})
}
func (m *Module) updateTimestampedProto(tsp db.TimestampedProto) {
ts := tsp.GetTimestamps()
if ts == nil {
ts = &models.Timestamp{}
}
// convert time to millis
nowMillis := time.Now().UnixNano() / 1e6
if ts.GetCreatedAt() == 0 {
ts.CreatedAt = proto.Int64(nowMillis)
}
ts.UpdatedAt = proto.Int64(nowMillis)
tsp.SetTimestamps(ts)
}
// Update is a generic way of updating AddressableProto data in the bolt database.
func (m *Module) Update(bucket string, pb db.AddressableProto) error {
if tsp, ok := pb.(db.TimestampedProto); ok {
m.updateTimestampedProto(tsp)
}
data, err := proto.Marshal(pb)
if err != nil {
return errors.Wrap(err)
}
key := []byte(pb.GetUuid())
if len(key) == 0 {
return errors.New("no uuid for proto")
}
return m.Bolt.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucket))
return errors.Wrap(b.Put(key, data))
})
}
func (m *Module) delete(bucket string, pb db.AddressableProto) error {
return m.Bolt.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucket))
return b.Delete([]byte(pb.GetUuid()))
})
}
// BackupToFile writes the entire database to the file at path.
func (m *Module) BackupToFile(path string) error {
f, err := os.Create(path)
if err != nil {
return errors.Wrap(err)
}
defer f.Close()
return m.Backup(f)
}
// Backup writes the entire database to w.
func (m *Module) Backup(w io.Writer) error {
return m.Bolt.View(func(tx *bolt.Tx) error {
_, err := tx.WriteTo(w)
return errors.Wrap(err)
})
}
// Debug prints out all data in the database. Does not deserialize saved protos.
func (m *Module) Debug(w io.Writer) error {
return m.Bolt.View(func(tx *bolt.Tx) error {
return tx.ForEach(func(name []byte, bucket *bolt.Bucket) error {
fmt.Fprintln(w, string(name)+":")
return bucket.ForEach(func(key, value []byte) error {
fmt.Fprintln(w, string(key))
fmt.Fprintln(w, string(value))
return nil
})
})
})
}