-
Notifications
You must be signed in to change notification settings - Fork 342
/
driver.go
61 lines (53 loc) · 1.28 KB
/
driver.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
package local
import (
"context"
"fmt"
"sync"
"github.com/dgraph-io/badger/v3"
"github.com/treeverse/lakefs/pkg/kv"
"github.com/treeverse/lakefs/pkg/kv/kvparams"
"github.com/treeverse/lakefs/pkg/logging"
)
const (
DriverName = "local"
)
var (
driverLock = &sync.Mutex{}
dbMap = make(map[string]*Store)
)
type Driver struct{}
func (d *Driver) Open(ctx context.Context, kvParams kvparams.Config) (kv.Store, error) {
params := kvParams.Local
if params == nil {
return nil, fmt.Errorf("missing %s settings: %w", DriverName, kv.ErrDriverConfiguration)
}
driverLock.Lock()
defer driverLock.Unlock()
connection, ok := dbMap[params.Path]
if !ok {
// no database open for this path
var logger logging.Logger = logging.DummyLogger{}
if params.EnableLogging {
logger = logging.FromContext(ctx).WithField("store", "local")
}
opts := badger.DefaultOptions(params.Path)
opts.Logger = &BadgerLogger{logger}
db, err := badger.Open(opts)
if err != nil {
return nil, err
}
connection = &Store{
db: db,
logger: logger,
prefetchSize: params.PrefetchSize,
path: params.Path,
}
dbMap[params.Path] = connection
}
connection.refCount++
return connection, nil
}
//nolint:gochecknoinits
func init() {
kv.Register(DriverName, &Driver{})
}