-
Notifications
You must be signed in to change notification settings - Fork 244
/
db.go
59 lines (50 loc) · 1.5 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
package db
import (
"path/filepath"
"github.com/ethereum/go-ethereum/log"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/errors"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/storage"
)
type storagePrefix byte
const (
// PeersCache is used for the db entries used for peers DB
PeersCache storagePrefix = iota
// DeduplicatorCache is used for the db entries used for messages
// deduplication cache
DeduplicatorCache
)
// Key creates a DB key for a specified service with specified data
func Key(prefix storagePrefix, data ...[]byte) []byte {
keyLength := 1
for _, d := range data {
keyLength += len(d)
}
key := make([]byte, keyLength)
key[0] = byte(prefix)
startPos := 1
for _, d := range data {
copy(key[startPos:], d[:])
startPos += len(d)
}
return key
}
// Create returns status pointer to leveldb.DB.
func Create(path, dbName string) (*leveldb.DB, error) {
// Create euphemeral storage if the node config path isn't provided
if path == "" {
return leveldb.Open(storage.NewMemStorage(), nil)
}
path = filepath.Join(path, dbName)
return Open(path, &opt.Options{OpenFilesCacheCapacity: 5})
}
// Open opens an existing leveldb database
func Open(path string, opts *opt.Options) (db *leveldb.DB, err error) {
db, err = leveldb.OpenFile(path, opts)
if _, iscorrupted := err.(*errors.ErrCorrupted); iscorrupted {
log.Info("database is corrupted trying to recover", "path", path)
db, err = leveldb.RecoverFile(path, nil)
}
return
}