-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
230 lines (206 loc) · 6.2 KB
/
store.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
package bdodb
import (
"os"
"time"
"github.com/blevesearch/bleve/index/store"
"github.com/blevesearch/bleve/registry"
"github.com/dgraph-io/badger/v2"
"github.com/dgraph-io/badger/v2/options"
)
// Store implements bleve store
type Store struct {
path string
db *badger.DB
mergeOperator store.MergeOperator
}
// RegisterStore will register the Store to bleve registry
//func RegisterStore(encryptionKey []byte, logger badger.Logger) {
// registry.RegisterKVStore(EngineName, NewStore())
//}
// NewStore returns a registry.KVStoreConstructor
func NewStore() registry.KVStoreConstructor {
return func(mergeOperator store.MergeOperator, config map[string]interface{}) (store.KVStore, error) {
path, ok := config["path"].(string)
if !ok {
return nil, os.ErrInvalid
}
if path == "" {
return nil, os.ErrInvalid
}
opt := badger.DefaultOptions(path)
opt.ReadOnly = false
opt.Truncate = true
opt.TableLoadingMode = options.LoadToRAM
opt.ValueLogLoadingMode = options.MemoryMap
opt.Compression = options.Snappy
//if Dir, ok := config["Dir"].(string); ok {
// opt.Dir = Dir
//}
//
//if ValueDir, ok := config["ValueDir"].(string); ok {
// opt.ValueDir = ValueDir
//}
// BdodbConfig
if bdodbConfig, ok := config["BdodbConfig"].(Config); ok {
opt.EncryptionKey = bdodbConfig.EncryptionKey
opt.Logger = bdodbConfig.Logger
} else {
if bdodbConfig, ok := config["BdodbConfig"].(*Config); ok {
opt.EncryptionKey = bdodbConfig.EncryptionKey
opt.Logger = bdodbConfig.Logger
}
}
/* usually modified options */
// SyncWrites
if SyncWrites, ok := config["SyncWrites"].(bool); ok {
opt.SyncWrites = SyncWrites
}
// NumVersionsToKeep
if NumVersionsToKeep, ok := config["NumVersionsToKeep"].(int); ok {
opt.NumVersionsToKeep = NumVersionsToKeep
}
// ReadOnly
if ReadOnly, ok := config["ReadOnly"].(bool); ok {
opt.ReadOnly = ReadOnly
}
// Truncate
if Truncate, ok := config["Truncate"].(bool); ok {
opt.Truncate = Truncate
}
// Compression
if Compression, ok := config["Compression"].(options.CompressionType); ok {
opt.Compression = Compression
}
// EventLogging
if EventLogging, ok := config["EventLogging"].(bool); ok {
opt.EventLogging = EventLogging
}
// InMemory
if InMemory, ok := config["InMemory"].(bool); ok {
opt.InMemory = InMemory
}
/* encryption related options */
// EncryptionKeyRotationDuration
if EncryptionKeyRotationDuration, ok := config["EncryptionKeyRotationDuration"].(time.Duration); ok {
opt.EncryptionKeyRotationDuration = EncryptionKeyRotationDuration
}
/* fine tuning options */
// MaxTableSize
if MaxTableSize, ok := config["MaxTableSize"].(int64); ok {
opt.MaxTableSize = MaxTableSize
}
// LevelSizeMultiplier
if LevelSizeMultiplier, ok := config["LevelSizeMultiplier"].(int); ok {
opt.LevelSizeMultiplier = LevelSizeMultiplier
}
// MaxLevels
if MaxLevels, ok := config["MaxLevels"].(int); ok {
opt.MaxLevels = MaxLevels
}
// ValueThreshold
if ValueThreshold, ok := config["ValueThreshold"].(int); ok {
opt.ValueThreshold = ValueThreshold
}
// NumMemtables
if NumMemtables, ok := config["NumMemtables"].(int); ok {
opt.NumMemtables = NumMemtables
}
// BlockSize
if BlockSize, ok := config["BlockSize"].(int); ok {
opt.BlockSize = BlockSize
}
// BloomFalsePositive
if BloomFalsePositive, ok := config["BloomFalsePositive"].(float64); ok {
opt.BloomFalsePositive = BloomFalsePositive
}
// KeepL0InMemory
if KeepL0InMemory, ok := config["KeepL0InMemory"].(bool); ok {
opt.KeepL0InMemory = KeepL0InMemory
}
// MaxCacheSize
if MaxCacheSize, ok := config["MaxCacheSize"].(int64); ok {
opt.MaxCacheSize = MaxCacheSize
}
// NumLevelZeroTables
if NumLevelZeroTables, ok := config["NumLevelZeroTables"].(int); ok {
opt.NumLevelZeroTables = NumLevelZeroTables
}
// NumLevelZeroTablesStall
if NumLevelZeroTablesStall, ok := config["NumLevelZeroTablesStall"].(int); ok {
opt.NumLevelZeroTablesStall = NumLevelZeroTablesStall
}
// LevelOneSize
if LevelOneSize, ok := config["LevelOneSize"].(int64); ok {
opt.LevelOneSize = LevelOneSize
}
// ValueLogFileSize
if ValueLogFileSize, ok := config["ValueLogFileSize"].(int64); ok {
opt.ValueLogFileSize = ValueLogFileSize
}
// ValueLogMaxEntries
if ValueLogMaxEntries, ok := config["ValueLogMaxEntries"].(uint32); ok {
opt.ValueLogMaxEntries = ValueLogMaxEntries
}
// NumCompactors
if NumCompactors, ok := config["NumCompactors"].(int); ok {
opt.NumCompactors = NumCompactors
}
// CompactL0OnClose
if CompactL0OnClose, ok := config["CompactL0OnClose"].(bool); ok {
opt.CompactL0OnClose = CompactL0OnClose
}
// LogRotatesToFlush
if LogRotatesToFlush, ok := config["LogRotatesToFlush"].(int32); ok {
opt.LogRotatesToFlush = LogRotatesToFlush
}
// ZSTDCompressionLevel
if ZSTDCompressionLevel, ok := config["ZSTDCompressionLevel"].(int); ok {
opt.ZSTDCompressionLevel = ZSTDCompressionLevel
}
// VerifyValueChecksum
if VerifyValueChecksum, ok := config["VerifyValueChecksum"].(bool); ok {
opt.VerifyValueChecksum = VerifyValueChecksum
}
// ChecksumVerificationMode
if ChecksumVerificationMode, ok := config["ChecksumVerificationMode"].(options.ChecksumVerificationMode); ok {
opt.ChecksumVerificationMode = ChecksumVerificationMode
}
if _, err := os.Stat(path); os.IsNotExist(err) {
err := os.MkdirAll(path, os.FileMode(0700))
if err != nil {
return nil, err
}
}
db, err := badger.Open(opt)
if err != nil {
return nil, err
}
rv := Store{
path: path,
db: db,
mergeOperator: mergeOperator,
}
return &rv, nil
}
}
// Close cleanup and close the current store
func (store *Store) Close() error {
return store.db.Close()
}
// Reader initialize a new store.Reader
func (store *Store) Reader() (store.KVReader, error) {
return &Reader{
iteratorOptions: badger.DefaultIteratorOptions,
store: store,
txn: store.db.NewTransaction(false),
}, nil
}
// Writer initialize a new store.Writer
func (store *Store) Writer() (store.KVWriter, error) {
return &Writer{
store: store,
}, nil
}
func init() {
registry.RegisterKVStore(EngineName, NewStore())
}