forked from dustin/seriesly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
307 lines (265 loc) · 5.71 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
package main
import (
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/dustin/go-couchstore"
)
type dbOperation uint8
const (
db_store_item = dbOperation(iota)
db_delete_item
db_compact
)
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 *couchstore.Couchstore
}
var dbLock = sync.Mutex{}
var dbConns = map[string]*dbWriter{}
func dbPath(n string) string {
return filepath.Join(*dbRoot, n) + ".couch"
}
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, ".couch") {
right = len(n) - 6
}
return n[left:right]
}
func dbopen(name string) (*couchstore.Couchstore, error) {
db, err := couchstore.Open(dbPath(name), false)
return db, err
}
func dbcreate(path string) error {
db, err := couchstore.Open(path, true)
if err != nil {
return err
}
db.Close()
return nil
}
func dbdelete(dbname string) error {
dbLock.Lock()
defer dbLock.Unlock()
writer := dbConns[dbname]
if writer != nil {
writer.quit <- true
}
delete(dbConns, 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, ".couch") {
rv = append(rv, dbBase(p))
}
} else {
log.Printf("Error on %#v: %v", p, err)
}
return nil
})
return rv
}
func dbCompact(dq *dbWriter, bulk couchstore.BulkWriter, queued int,
qi dbqitem) (couchstore.BulkWriter, error) {
start := time.Now()
if queued > 0 {
bulk.Commit()
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.CompactTo(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")
dq.db.Close()
dq.db, err = dbopen(dq.dbname)
if err != nil {
log.Fatalf("Error reopening DB after compaction: %v", err)
}
return dq.db.Bulk(), nil
}
func dbWriteLoop(dq *dbWriter) {
queued := 0
bulk := dq.db.Bulk()
t := time.NewTimer(*flushTime)
for {
select {
case <-dq.quit:
bulk.Close()
bulk.Commit()
dq.db.Close()
return
case qi := <-dq.ch:
switch qi.op {
case db_store_item:
bulk.Set(couchstore.NewDocInfo(qi.k,
couchstore.DocIsCompressed),
couchstore.NewDocument(qi.k, qi.data))
queued++
case db_delete_item:
queued++
bulk.Delete(couchstore.NewDocInfo(qi.k, 0))
case db_compact:
var err error
bulk, err = dbCompact(dq, bulk, queued, qi)
qi.cherr <- err
queued = 0
default:
log.Panicf("Unhandled case: %v", qi.op)
}
if queued >= *maxOpQueue {
start := time.Now()
bulk.Commit()
log.Printf("Flush of %d items took %v",
queued, time.Since(start))
queued = 0
t.Stop()
t = time.NewTimer(*flushTime)
}
case <-t.C:
if queued > 0 {
start := time.Now()
bulk.Commit()
log.Printf("Flush of %d items from timer took %v",
queued, time.Since(start))
queued = 0
}
t = time.NewTimer(*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,
}
go dbWriteLoop(writer)
return writer, nil
}
func getOrCreateDB(dbname string) (*dbWriter, error) {
dbLock.Lock()
defer dbLock.Unlock()
writer := dbConns[dbname]
var err error
if writer == nil {
writer, err = dbWriteFun(dbname)
if err != nil {
return nil, err
}
dbConns[dbname] = writer
}
return writer, 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, db_store_item, nil}
return nil
}
func dbcompact(dbname string) error {
writer, err := getOrCreateDB(dbname)
if err != nil {
return err
}
cherr := make(chan error)
defer close(cherr)
writer.ch <- dbqitem{dbname: dbname,
op: db_compact,
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 []byte{}, err
}
defer db.Close()
doc, _, err := db.Get(id)
if err != nil {
return []byte{}, err
}
return doc.Value(), 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 db.Close()
return db.WalkDocs(from, func(d *couchstore.Couchstore,
di *couchstore.DocInfo, doc *couchstore.Document) error {
if to != "" && di.ID() >= to {
return couchstore.StopIteration
}
return f(di.ID(), doc.Value())
})
}
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 db.Close()
return db.Walk(from, func(d *couchstore.Couchstore,
di *couchstore.DocInfo) error {
if to != "" && di.ID() >= to {
return couchstore.StopIteration
}
return f(di.ID())
})
}
func parseKey(s string) int64 {
t, err := parseCanonicalTime(s)
if err != nil {
return -1
}
return t.UnixNano()
}