-
Notifications
You must be signed in to change notification settings - Fork 17
/
ldb.go
70 lines (60 loc) · 1.74 KB
/
ldb.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
package ctlstore
import (
"os"
"path/filepath"
"sync"
"github.com/segmentio/ctlstore/pkg/ldb"
"github.com/segmentio/ctlstore/pkg/sqlite"
)
const (
DefaultCtlstorePath = "/var/spool/ctlstore/"
DefaultChangelogFilename = "change.log"
defaultLDBVersioningSubdir = "versioned"
)
var (
globalLDBDirPath = DefaultCtlstorePath
globalLDBVersioningDirPath = filepath.Join(DefaultCtlstorePath, defaultLDBVersioningSubdir)
globalCLPath = filepath.Join(DefaultCtlstorePath, DefaultChangelogFilename)
globalLDBReadOnly = true
globalReader *LDBReader
globalReaderMu sync.RWMutex
)
func init() {
envPath := os.Getenv("CTLSTORE_PATH")
if envPath != "" {
globalLDBDirPath = envPath
globalLDBVersioningDirPath = filepath.Join(envPath, defaultLDBVersioningSubdir)
globalCLPath = filepath.Join(envPath, DefaultChangelogFilename)
}
sqlite.InitDriver()
}
// ReaderForPath opens an LDB at the provided path and returns an LDBReader
// instance pointed at that LDB.
func ReaderForPath(path string) (*LDBReader, error) {
return newLDBReader(path)
}
// Reader returns an LDBReader that can be used globally.
func Reader() (*LDBReader, error) {
globalReaderMu.RLock()
defer globalReaderMu.RUnlock()
if globalReader == nil {
globalReaderMu.RUnlock()
defer globalReaderMu.RLock()
globalReaderMu.Lock()
defer globalReaderMu.Unlock()
if globalReader == nil {
var reader *LDBReader
var err error
if ldbVersioning {
reader, err = newVersionedLDBReader(globalLDBVersioningDirPath)
} else {
reader, err = newLDBReader(filepath.Join(globalLDBDirPath, ldb.DefaultLDBFilename))
}
if err != nil {
return nil, err
}
globalReader = reader
}
}
return globalReader, nil
}