-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
242 lines (200 loc) · 5.41 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
package minidoc
import (
"encoding/binary"
"encoding/json"
"fmt"
"github.com/joyrexus/buckets"
"strconv"
"time"
)
const defaultDbFileName = ".minidoc/store.db"
type BucketHandler struct {
debug func(string)
bx *buckets.DB
DBPath string
}
type BucketHandlerOption func(*BucketHandler)
func WithBucketHandlerDebug(debug func(string)) BucketHandlerOption {
return func(bh *BucketHandler) {
bh.debug = debug
}
}
func WithBucketHandlerDBPath(path string) BucketHandlerOption {
return func(bh *BucketHandler) {
bh.DBPath = path
log.Debug("opt() db path = " + path)
}
}
func NewBucketHandler(opts ...BucketHandlerOption) *BucketHandler {
bh := &BucketHandler{
debug: func(string) {},
DBPath: defaultDbFileName,
}
for _, opt := range opts {
opt(bh)
}
return bh
}
func (bh *BucketHandler) Write(doc MiniDoc) (uint32, error) {
bx, err := buckets.Open(bh.DBPath)
if err != nil {
log.Errorf("error while opening bucket[%s] at %s: %v", doc.GetType(), bh.DBPath, err)
return 0, err
}
defer bx.Close()
doctype := doc.GetType()
// Create a new bucket
bucket, err := bx.New([]byte(doctype))
if err != nil {
log.Errorf("error while opening or creating bucket[%s]: %v", doctype, err)
return 0, err
}
key := toBytes(doc.GetID())
if doc.GetID() == 0 {
log.Debugf("id == 0 doctype [%s] generating new sequence", doctype)
key, err = NextSequence(bx, doctype)
if err != nil {
log.Errorf("error while next sequence doctype [%s]: %v", doctype, err)
}
doc.SetID(toUint32(key))
}
nowstr := time.Now().Format("2006-01-02 15:04:05")
doc.SetCreatedDate(nowstr)
data, err := json.Marshal(doc.GetJSON())
if err != nil {
log.Errorf("error while marshalling: %v", err)
return 0, err
}
err = bucket.Put(key, data)
if err != nil {
log.Errorf("error while bucket put: %v", err)
}
return toUint32(key), nil
}
func (bh *BucketHandler) ReadAll(doctype string) ([]MiniDoc, error) {
bx, err := buckets.Open(bh.DBPath)
if err != nil {
log.Errorf("error while opening bucket[%s] at %s: %v", doctype, bh.DBPath, err)
}
defer bx.Close()
bucket, err := bx.New([]byte(doctype))
if err != nil {
log.Errorf("error while opening or creating bucket[%s]: %v", doctype, err)
return nil, err
}
items, err := bucket.Items()
if err != nil {
log.Errorf("error while iterating all items in bucket[%s]: %v", doctype, err)
return nil, err
}
docs := make([]MiniDoc, len(items))
for i, item := range items {
doc, err := NewDoc(doctype)
if err != nil {
log.Errorf("instantiating %s", doctype)
return nil, err
}
err = json.Unmarshal(item.Value, doc)
if err != nil {
log.Errorf("error while unmarshalling %s: %v", doctype, err)
return nil, err
}
docs[i] = doc
}
return docs, nil
}
func (bh *BucketHandler) Read(key uint32, doctype string) (MiniDoc, error) {
bx, err := buckets.Open(bh.DBPath)
if err != nil {
log.Errorf("error while opening bucket[%s] at %s: %v", doctype, bh.DBPath, err)
}
defer bx.Close()
bucket, err := bx.New([]byte(doctype))
if err != nil {
log.Errorf("error while opening or creating bucket[%s]: %v", doctype, err)
return nil, err
}
data, err := bucket.Get(toBytes(key))
if err != nil {
log.Errorf("error while getting item in bucket[%s] with key[%d]: %v", doctype, key, err)
return nil, err
}
if data == nil {
msg := fmt.Sprintf("item not found in bucket[%s] with key[%d]", doctype, key)
log.Errorf(msg)
return nil, fmt.Errorf(msg)
}
doc, err := NewDoc(doctype)
if err != nil {
log.Errorf("instantiating %s", doctype)
return nil, err
}
err = json.Unmarshal(data, doc)
if err != nil {
log.Errorf("error while unmarshalling in bucket[%s] with key[%d]: %v", doctype, key, err)
return nil, err
}
return doc, nil
}
func (bh *BucketHandler) Delete(doc MiniDoc) error {
key := toBytes(doc.GetID())
doctype := doc.GetType()
bx, err := buckets.Open(bh.DBPath)
if err != nil {
log.Fatal(err)
}
defer bx.Close()
bucket, err := bx.New([]byte(doctype))
if err != nil {
log.Errorf("error while opening or creating bucket[%s]: %v", doctype, err)
return err
}
log.Debugf("deleting in bucket[%s] with key[%d]", doctype, key)
err = bucket.Delete(key)
if err != nil {
log.Errorf("deleting in bucket[%s] with key[%d]: %v", doctype, key, err)
return err
}
return nil
}
// NextSequence returns next sequence
func NextSequence(bx *buckets.DB, sequenceName string) ([]byte, error) {
// get or create sequence bucket
bucket, err := bx.New([]byte("_sequence"))
if err != nil {
log.Errorf("error while opening bucket for _sequence for doctype[%s]: %v", sequenceName, err)
return nil, err
}
// get or create next sequence for given bucket
key := []byte(sequenceName)
next, err := bucket.Get(key)
if err != nil {
log.Errorf("error while retrieving current key for _sequence for doctype[%s]: %v", sequenceName, err)
return nil, err
}
if next == nil || len(next) == 0 {
bs := toBytes(1)
bucket.Put(key, bs)
return bs, nil
}
nextVal := toUint32(next)
nextVal++
err = bucket.Put(key, toBytes(nextVal))
if err != nil {
log.Errorf("error while putting next key for _sequence for doctype[%s]: %v", sequenceName, err)
return nil, err
}
return toBytes(nextVal), nil
}
func toBytes(value uint32) []byte {
bs := make([]byte, 4)
binary.LittleEndian.PutUint32(bs, value)
return bs
}
func toUint32(bs []byte) uint32 {
return binary.LittleEndian.Uint32(bs)
}
func toUnit32FromString(s string) uint32 {
n, _ := strconv.Atoi(s)
return uint32(n)
}