This repository has been archived by the owner on Oct 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
database.go
372 lines (323 loc) · 7.16 KB
/
database.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package main
import (
"errors"
"log"
"os"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/dustin/seriesly/timelib"
"github.com/mschoch/gouchstore"
)
type dbOperation uint8
const (
opStoreItem = dbOperation(iota)
opDeleteItem
opCompact
)
const dbExt = ".couch"
type dbqitem struct {
dbname string
k string
data []byte
op dbOperation
cherr chan error
}
type dbWriter struct {
dbname string
ch chan dbqitem
quit chan bool
db *gouchstore.Gouchstore
}
var errClosed = errors.New("closed")
func (w *dbWriter) Close() error {
select {
case <-w.quit:
return errClosed
default:
}
close(w.quit)
return nil
}
var dbLock = sync.Mutex{}
var dbConns = map[string]*dbWriter{}
func dbPath(n string) string {
return filepath.Join(*dbRoot, n) + dbExt
}
func dbBase(n string) string {
left := 0
right := len(n)
if strings.HasPrefix(n, *dbRoot) {
left = len(*dbRoot)
if n[left] == '/' {
left++
}
}
if strings.HasSuffix(n, dbExt) {
right = len(n) - len(dbExt)
}
return n[left:right]
}
func dbopen(name string) (*gouchstore.Gouchstore, error) {
path := dbPath(name)
db, err := gouchstore.Open(dbPath(name), 0)
if err == nil {
recordDBConn(path, db)
}
return db, err
}
func dbcreate(path string) error {
db, err := gouchstore.Open(path, gouchstore.OPEN_CREATE)
if err != nil {
return err
}
recordDBConn(path, db)
closeDBConn(db)
return nil
}
func dbRemoveConn(dbname string) {
dbLock.Lock()
defer dbLock.Unlock()
writer := dbConns[dbname]
if writer != nil && writer.quit != nil {
writer.Close()
}
delete(dbConns, dbname)
}
func dbCloseAll() {
dbLock.Lock()
defer dbLock.Unlock()
for n, c := range dbConns {
log.Printf("Shutting down open conn %v", n)
c.Close()
}
}
func dbdelete(dbname string) error {
dbRemoveConn(dbname)
return os.Remove(dbPath(dbname))
}
func dblist(root string) []string {
rv := []string{}
filepath.Walk(root, func(p string, info os.FileInfo, err error) error {
if err == nil {
if !info.IsDir() && strings.HasSuffix(p, dbExt) {
rv = append(rv, dbBase(p))
}
} else {
log.Printf("Error on %#v: %v", p, err)
}
return nil
})
return rv
}
func dbCompact(dq *dbWriter, bulk gouchstore.BulkWriter, queued int,
qi dbqitem) (gouchstore.BulkWriter, error) {
start := time.Now()
if queued > 0 {
bulk.Commit()
if *verbose {
log.Printf("Flushed %d items in %v for pre-compact",
queued, time.Since(start))
}
bulk.Close()
}
dbn := dbPath(dq.dbname)
queued = 0
start = time.Now()
err := dq.db.Compact(dbn + ".compact")
if err != nil {
log.Printf("Error compacting: %v", err)
return dq.db.Bulk(), err
}
log.Printf("Finished compaction of %v in %v", dq.dbname,
time.Since(start))
err = os.Rename(dbn+".compact", dbn)
if err != nil {
log.Printf("Error putting compacted data back")
return dq.db.Bulk(), err
}
log.Printf("Reopening post-compact")
closeDBConn(dq.db)
dq.db, err = dbopen(dq.dbname)
if err != nil {
log.Fatalf("Error reopening DB after compaction: %v", err)
}
return dq.db.Bulk(), nil
}
var dbWg = sync.WaitGroup{}
func dbWriteLoop(dq *dbWriter) {
defer dbWg.Done()
queued := 0
bulk := dq.db.Bulk()
t := time.NewTimer(*flushTime)
defer t.Stop()
liveTracker := time.NewTicker(*liveTime)
defer liveTracker.Stop()
liveOps := 0
dbst := dbStats.getOrCreate(dq.dbname)
defer atomic.StoreUint32(&dbst.qlen, 0)
defer atomic.AddUint32(&dbst.closes, 1)
for {
atomic.StoreUint32(&dbst.qlen, uint32(queued))
select {
case <-dq.quit:
sdt := time.Now()
bulk.Commit()
bulk.Close()
closeDBConn(dq.db)
dbRemoveConn(dq.dbname)
log.Printf("Closed %v with %v items in %v",
dq.dbname, queued, time.Since(sdt))
return
case <-liveTracker.C:
if queued == 0 && liveOps == 0 {
log.Printf("Closing idle DB: %v", dq.dbname)
close(dq.quit)
}
liveOps = 0
case qi := <-dq.ch:
liveOps++
switch qi.op {
case opStoreItem:
bulk.Set(gouchstore.NewDocumentInfo(qi.k),
gouchstore.NewDocument(qi.k, qi.data))
queued++
case opDeleteItem:
queued++
bulk.Delete(gouchstore.NewDocumentInfo(qi.k))
case opCompact:
var err error
bulk, err = dbCompact(dq, bulk, queued, qi)
qi.cherr <- err
atomic.AddUint64(&dbst.written, uint64(queued))
queued = 0
default:
log.Panicf("Unhandled case: %v", qi.op)
}
if queued >= *maxOpQueue {
start := time.Now()
bulk.Commit()
if *verbose {
log.Printf("Flush of %d items took %v",
queued, time.Since(start))
}
atomic.AddUint64(&dbst.written, uint64(queued))
queued = 0
t.Reset(*flushTime)
}
case <-t.C:
if queued > 0 {
start := time.Now()
bulk.Commit()
if *verbose {
log.Printf("Flush of %d items from timer took %v",
queued, time.Since(start))
}
atomic.AddUint64(&dbst.written, uint64(queued))
queued = 0
}
t.Reset(*flushTime)
}
}
}
func dbWriteFun(dbname string) (*dbWriter, error) {
db, err := dbopen(dbname)
if err != nil {
return nil, err
}
writer := &dbWriter{
dbname,
make(chan dbqitem, *maxOpQueue),
make(chan bool),
db,
}
dbWg.Add(1)
go dbWriteLoop(writer)
return writer, nil
}
func getOrCreateDB(dbname string) (*dbWriter, bool, error) {
dbLock.Lock()
defer dbLock.Unlock()
writer := dbConns[dbname]
var err error
opened := false
if writer == nil {
writer, err = dbWriteFun(dbname)
if err != nil {
return nil, false, err
}
dbConns[dbname] = writer
opened = true
}
return writer, opened, nil
}
func dbstore(dbname string, k string, body []byte) error {
writer, _, err := getOrCreateDB(dbname)
if err != nil {
return err
}
writer.ch <- dbqitem{dbname, k, body, opStoreItem, nil}
return nil
}
func dbcompact(dbname string) error {
writer, opened, err := getOrCreateDB(dbname)
if err != nil {
return err
}
if opened {
log.Printf("Requesting post-compaction close of %v", dbname)
defer writer.Close()
}
cherr := make(chan error)
defer close(cherr)
writer.ch <- dbqitem{dbname: dbname,
op: opCompact,
cherr: cherr,
}
return <-cherr
}
func dbGetDoc(dbname, id string) ([]byte, error) {
db, err := dbopen(dbname)
if err != nil {
log.Printf("Error opening db: %v - %v", dbname, err)
return nil, err
}
defer closeDBConn(db)
doc, err := db.DocumentById(id)
if err != nil {
return nil, err
}
return doc.Body, err
}
func dbwalk(dbname, from, to string, f func(k string, v []byte) error) error {
db, err := dbopen(dbname)
if err != nil {
log.Printf("Error opening db: %v - %v", dbname, err)
return err
}
defer closeDBConn(db)
return db.WalkDocs(from, to, func(d *gouchstore.Gouchstore,
di *gouchstore.DocumentInfo, doc *gouchstore.Document) error {
return f(di.ID, doc.Body)
})
}
func dbwalkKeys(dbname, from, to string, f func(k string) error) error {
db, err := dbopen(dbname)
if err != nil {
log.Printf("Error opening db: %v - %v", dbname, err)
return err
}
defer closeDBConn(db)
return db.AllDocuments(from, to, func(db *gouchstore.Gouchstore, documentInfo *gouchstore.DocumentInfo, userContext interface{}) error {
return f(documentInfo.ID)
}, nil)
}
func parseKey(s string) int64 {
t, err := timelib.ParseCanonicalTime(s)
if err != nil {
return -1
}
return t.UnixNano()
}