-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdbcfg.go
84 lines (68 loc) · 3.12 KB
/
dbcfg.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
package config
import (
"fmt"
"time"
"github.com/lightningnetwork/lnd/kvdb"
)
const (
defaultDbName = "staker.db"
)
type DBConfig struct {
// DBPath is the directory path in which the database file should be
// stored.
DBPath string `long:"dbpath" description:"The directory path in which the database file should be stored."`
// DBFileName is the name of the database file.
DBFileName string `long:"dbfilename" description:"The name of the database file."`
// NoFreelistSync, if true, prevents the database from syncing its
// freelist to disk, resulting in improved performance at the expense of
// increased startup time.
NoFreelistSync bool `long:"nofreelistsync" description:"Prevents the database from syncing its freelist to disk, resulting in improved performance at the expense of increased startup time."`
// AutoCompact specifies if a Bolt based database backend should be
// automatically compacted on startup (if the minimum age of the
// database file is reached). This will require additional disk space
// for the compacted copy of the database but will result in an overall
// lower database size after the compaction.
AutoCompact bool `long:"autocompact" description:"Specifies if a Bolt based database backend should be automatically compacted on startup (if the minimum age of the database file is reached). This will require additional disk space for the compacted copy of the database but will result in an overall lower database size after the compaction."`
// AutoCompactMinAge specifies the minimum time that must have passed
// since a bolt database file was last compacted for the compaction to
// be considered again.
AutoCompactMinAge time.Duration `long:"autocompactminage" description:"Specifies the minimum time that must have passed since a bolt database file was last compacted for the compaction to be considered again."`
// DBTimeout specifies the timeout value to use when opening the wallet
// database.
DBTimeout time.Duration `long:"dbtimeout" description:"Specifies the timeout value to use when opening the wallet database."`
}
func DefaultDBConfig() *DBConfig {
return DefaultDBConfigWithHomePath(DefaultHomeDir)
}
func DefaultDBConfigWithHomePath(homePath string) *DBConfig {
return &DBConfig{
DBPath: DataDir(homePath),
DBFileName: defaultDbName,
NoFreelistSync: true,
AutoCompact: false,
AutoCompactMinAge: kvdb.DefaultBoltAutoCompactMinAge,
DBTimeout: kvdb.DefaultDBTimeout,
}
}
func (cfg *DBConfig) DBConfigToBoltBackenCondfig() *kvdb.BoltBackendConfig {
return &kvdb.BoltBackendConfig{
DBPath: cfg.DBPath,
DBFileName: cfg.DBFileName,
NoFreelistSync: cfg.NoFreelistSync,
AutoCompact: cfg.AutoCompact,
AutoCompactMinAge: cfg.AutoCompactMinAge,
DBTimeout: cfg.DBTimeout,
}
}
func (cfg *DBConfig) Validate() error {
if cfg.DBPath == "" {
return fmt.Errorf("DB path cannot be empty")
}
if cfg.DBFileName == "" {
return fmt.Errorf("DB file name cannot be empty")
}
return nil
}
func (cfg *DBConfig) GetDbBackend() (kvdb.Backend, error) {
return kvdb.GetBoltBackend(cfg.DBConfigToBoltBackenCondfig())
}