-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.go
402 lines (351 loc) · 9.32 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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
* Copyright 2021 National Library of Norway.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package badgeridx
import (
"context"
"errors"
"fmt"
"os"
"path"
"path/filepath"
"runtime"
"sync"
"time"
"github.com/dgraph-io/badger/v4"
"github.com/nlnwa/gowarcserver/index"
"github.com/nlnwa/gowarcserver/internal/keyvalue"
"github.com/nlnwa/gowarcserver/schema"
"github.com/rs/zerolog/log"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
type DB struct {
// IdIndex maps record id to storage ref
IdIndex *badger.DB
// FileIndex maps filepath to fileinfo
FileIndex *badger.DB
// CdxIndex maps cdx key to cdx record
CdxIndex *badger.DB
// ReportIndex maps report id to report
ReportIndex *badger.DB
batch chan index.Record
done chan struct{}
wg sync.WaitGroup
tasks map[string]context.CancelFunc
}
func NewDB(options ...Option) (db *DB, err error) {
// Increase GOMAXPROCS as recommended by badger
// https://github.com/dgraph-io/badger#are-there-any-go-specific-settings-that-i-should-use
runtime.GOMAXPROCS(128)
opts := defaultDbOptions()
for _, opt := range options {
opt(opts)
}
var idIndex *badger.DB
var fileIndex *badger.DB
var cdxIndex *badger.DB
var reportIndex *badger.DB
batch := make(chan index.Record, opts.BatchMaxSize)
done := make(chan struct{})
if idIndex, err = newBadgerDB(path.Join(opts.Path, opts.Database, "id-index"), opts.Compression, opts.ReadOnly, opts.Silent); err != nil {
return
}
if fileIndex, err = newBadgerDB(path.Join(opts.Path, opts.Database, "file-index"), opts.Compression, opts.ReadOnly, opts.Silent); err != nil {
return
}
if cdxIndex, err = newBadgerDB(path.Join(opts.Path, opts.Database, "cdx-index"), opts.Compression, opts.ReadOnly, opts.Silent); err != nil {
return
}
if reportIndex, err = newBadgerDB(path.Join(opts.Path, opts.Database, "report-index"), opts.Compression, opts.ReadOnly, opts.Silent); err != nil {
return
}
db = &DB{
IdIndex: idIndex,
FileIndex: fileIndex,
CdxIndex: cdxIndex,
ReportIndex: reportIndex,
batch: batch,
done: done,
tasks: make(map[string]context.CancelFunc),
}
// We don't need to run batch and gc workers when operating in read-only mode.
if opts.ReadOnly {
return
}
// batch worker
db.wg.Add(1)
go func() {
defer db.wg.Done()
ticker := time.NewTimer(opts.BatchMaxWait)
defer ticker.Stop()
for {
select {
case <-done:
db.FlushBatch()
return
case <-ticker.C:
db.FlushBatch()
}
}
}()
// gc worker
db.wg.Add(1)
go func() {
defer db.wg.Done()
ticker := time.NewTimer(opts.GcInterval)
defer ticker.Stop()
for {
select {
case <-done:
db.runValueLogGC(0.3)
return
case <-ticker.C:
db.runValueLogGC(0.5)
}
}
}()
return
}
func (db *DB) runValueLogGC(discardRatio float64) {
var wg sync.WaitGroup
for _, m := range []*badger.DB{db.IdIndex, db.FileIndex, db.CdxIndex, db.ReportIndex} {
m := m
if m == nil {
continue
}
wg.Add(1)
go func() {
defer wg.Done()
var err error
for err == nil {
err = m.RunValueLogGC(discardRatio)
}
}()
}
wg.Wait()
}
// Close stops the gc and batch workers and closes the index databases.
func (db *DB) Close() {
for _, cancel := range db.tasks {
cancel()
}
close(db.done)
db.wg.Wait()
_ = db.IdIndex.Close()
_ = db.FileIndex.Close()
_ = db.CdxIndex.Close()
_ = db.ReportIndex.Close()
}
// addFile checks if file is indexed or has not changed since indexing, and adds file to file index.
func (db *DB) addFile(path string) error {
stat, err := os.Stat(path)
if err != nil {
return fmt.Errorf("failed to get file info: %s: %w", path, err)
}
fileSize := stat.Size()
fileLastModified := stat.ModTime()
fn := filepath.Base(path)
if fileInfo, err := db.getFileInfo(fn); err == nil {
if err := fileInfo.GetLastModified().CheckValid(); err != nil {
return err
}
fileInfoLastModified := fileInfo.LastModified.AsTime()
if fileInfo.Size == fileSize && fileInfoLastModified.Equal(fileLastModified) {
return index.AlreadyIndexedError
}
}
return db.updateFilePath(path)
}
func (db *DB) updateFilePath(path string) error {
var err error
fileInfo := &schema.FileInfo{}
fileInfo.Path, err = filepath.Abs(path)
if err != nil {
return err
}
fileInfo.Name = filepath.Base(fileInfo.Path)
stat, err := os.Stat(fileInfo.Path)
if err != nil {
return err
}
fileInfo.Size = stat.Size()
fileInfo.LastModified = timestamppb.New(stat.ModTime())
key, value, err := keyvalue.MarshalFileInfo(fileInfo, "")
if err != nil {
return err
}
return db.FileIndex.Update(func(txn *badger.Txn) error {
return txn.Set(key, value)
})
}
// write schedules a Record to be added to the DB via the batch channel.
func (db *DB) write(rec index.Record) {
select {
case <-db.done:
// do nothing
case db.batch <- rec:
// added record to batch
default:
// batch channel is full so flush it before adding record to batch
db.FlushBatch()
db.batch <- rec
}
}
// collectBatch returns a slice of all the records in the batch channel.
func (db *DB) collectBatch() (records []index.Record) {
for {
select {
case record := <-db.batch:
records = append(records, record)
default:
return
}
}
}
// FlushBatch collects all records in the batch channel and updates the id and cdx indices.
func (db *DB) FlushBatch() {
records := db.collectBatch()
if len(records) == 0 {
return
}
// update id index
if err := db.IdIndex.Update(set(records, marshalId)); err != nil {
log.Error().Err(err).Msgf("Failed to update id index")
}
// update cdx index
if err := db.CdxIndex.Update(set(records, marshalCdx)); err != nil {
log.Error().Err(err).Msgf("Failed to update cdx index")
}
}
func marshalId(r index.Record) ([]byte, []byte, error) {
return keyvalue.MarshalId(r, "")
}
func marshalCdx(r index.Record) ([]byte, []byte, error) {
return keyvalue.MarshalCdx(r)
}
func set(records []index.Record, m func(index.Record) ([]byte, []byte, error)) func(*badger.Txn) error {
return func(txn *badger.Txn) error {
for _, r := range records {
key, value, err := m(r)
if err != nil {
return fmt.Errorf("failed to marshal '%s'-'%s': %w", key, r, err)
}
err = txn.Set(key, value)
if err != nil {
return fmt.Errorf("failed to set '%s'-'%s': %w", key, r, err)
}
}
return nil
}
}
func (db *DB) Write(rec index.Record) error {
db.write(rec)
return nil
}
func (db *DB) Index(path string) error {
return db.addFile(path)
}
// Resolve looks up warcId in the id index of the database and returns corresponding storageRef, or an error if not found.
func (db *DB) Resolve(_ context.Context, warcId string) (storageRef string, err error) {
key := keyvalue.Key(warcId)
err = db.IdIndex.View(func(txn *badger.Txn) error {
item, err := txn.Get(key)
if errors.Is(err, badger.ErrKeyNotFound) {
return nil
}
if err != nil {
return err
}
return item.Value(func(val []byte) error {
storageRef = string(val)
return nil
})
})
return
}
// ResolvePath looks up filename in file index and returns the path field.
func (db *DB) ResolvePath(filename string) (filePath string, err error) {
fileInfo, err := db.getFileInfo(filename)
return fileInfo.Path, err
}
func (db *DB) getFileInfo(fileName string) (*schema.FileInfo, error) {
key := keyvalue.Key(fileName)
val := new(schema.FileInfo)
err := db.FileIndex.View(func(txn *badger.Txn) error {
item, err := txn.Get(key)
if err != nil {
return err
}
err = item.Value(func(v []byte) error {
return proto.Unmarshal(v, val)
})
return err
})
return val, err
}
func (db *DB) listFileInfo(ctx context.Context, limit int, results chan<- index.FileInfoResponse) error {
go func() {
_ = db.FileIndex.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = limit
iter := txn.NewIterator(opts)
defer iter.Close()
defer close(results)
count := 0
for iter.Seek(nil); iter.Valid(); iter.Next() {
select {
case <-ctx.Done():
results <- keyvalue.FileInfoResponse{Error: ctx.Err()}
return nil
default:
}
if limit > 0 && count >= limit {
return nil
}
count++
err := iter.Item().Value(func(value []byte) error {
fileInfo := new(schema.FileInfo)
err := proto.Unmarshal(value, fileInfo)
if err != nil {
return err
}
results <- keyvalue.FileInfoResponse{FileInfo: fileInfo, Error: nil}
return nil
})
if err != nil {
results <- keyvalue.FileInfoResponse{Error: err}
return nil
}
}
return nil
})
}()
return nil
}
func (db *DB) GetCdx(key string) (*schema.Cdx, error) {
val := new(schema.Cdx)
err := db.CdxIndex.View(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(key))
if err != nil {
return err
}
err = item.Value(func(v []byte) error {
return proto.Unmarshal(v, val)
})
return err
})
return val, err
}