-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
311 lines (268 loc) · 7.1 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright © 2019 National Library of Norway
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package importutil
import (
"bytes"
"context"
"encoding/gob"
"errors"
"fmt"
"io"
"os"
"time"
"github.com/dgraph-io/badger/v4"
"github.com/dgraph-io/ristretto/z"
configV1 "github.com/nlnwa/veidemann-api/go/config/v1"
"github.com/rs/zerolog/log"
)
// badgerLogger is a log adapter that implements badger.Logger
type badgerLogger struct {
prefix string
}
func (l badgerLogger) Errorf(fmt string, args ...interface{}) {
log.Error().Msgf(l.prefix+fmt, args...)
}
func (l badgerLogger) Warningf(fmt string, args ...interface{}) {
log.Warn().Msgf(l.prefix+fmt, args...)
}
func (l badgerLogger) Infof(fmt string, args ...interface{}) {
log.Debug().Msgf(l.prefix+fmt, args...)
}
func (l badgerLogger) Debugf(fmt string, args ...interface{}) {
log.Trace().Msgf(l.prefix+fmt, args...)
}
type ExistsCode int
const (
Undefined ExistsCode = iota
NewKey
NewId
Exists
)
func (e ExistsCode) ExistsInVeidemann() bool {
return e > NewKey
}
func (e ExistsCode) String() string {
if e < Undefined || e > Exists {
return "UNKNOWN"
}
names := []string{
"UNDEFINED",
"NEW_KEY",
"NEW_ID",
"EXISTS"}
return names[e]
}
type KeyNormalizer interface {
Normalize(key string) (string, error)
}
type ImportDb struct {
db *badger.DB
gc *time.Ticker
}
func NewImportDb(dbDir string, truncate bool) (*ImportDb, error) {
if err := os.MkdirAll(dbDir, 0777); err != nil {
return nil, fmt.Errorf("failed to create db dir %s: %w", dbDir, err)
}
opts := badger.DefaultOptions(dbDir)
opts.Logger = badgerLogger{prefix: "Badger: "}
db, err := badger.Open(opts)
if err != nil {
return nil, fmt.Errorf("could not open db %s: %w", dbDir, err)
}
if truncate {
err = db.DropAll()
if err != nil {
return nil, fmt.Errorf("failed to reset db %s: %w", dbDir, err)
}
}
d := &ImportDb{
db: db,
gc: time.NewTicker(5 * time.Minute),
}
// Run GC in background
go func() {
for range d.gc.C {
d.RunValueLogGC(0.7)
}
}()
return d, nil
}
func (d *ImportDb) RunValueLogGC(discardRatio float64) {
var err error
for err == nil {
err = d.db.RunValueLogGC(discardRatio)
}
}
// Close closes the database, stops the GC ticker and waits for
func (d *ImportDb) Close() {
_ = d.db.RunValueLogGC(0.7)
d.gc.Stop()
_ = d.db.Close()
}
func ImportExisting(db *ImportDb, client configV1.ConfigClient, kind configV1.Kind, keyNormalizer KeyNormalizer) error {
req := &configV1.ListRequest{
Kind: kind,
}
r, err := client.ListConfigObjects(context.Background(), req)
if err != nil {
return fmt.Errorf("failed to list %s from Veidemann: %w", kind.String(), err)
}
var count, imported, failed int
start := time.Now()
for {
msg, err := r.Recv()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
return fmt.Errorf("error reading %s from Veidemann: %w", kind.String(), err)
}
id := msg.GetId()
key := msg.GetMeta().GetName()
if keyNormalizer != nil {
key, err = keyNormalizer.Normalize(key)
if err != nil {
failed++
log.Error().Err(err).Str("key", msg.GetMeta().GetName()).Str("id", id).Msg("Normalization failed")
continue
}
}
code, _, err := db.Set(key, id)
if err != nil {
return fmt.Errorf("error writing to db: %w", err)
}
l := log.With().Str("key", key).Str("id", id).Str("kind", kind.String()).Logger()
count++
switch code {
case Undefined:
failed++
case NewKey:
imported++
l.Info().Msg("New key imported from Veidemann")
case NewId:
l.Info().Msg("New id imported from Veidemann")
case Exists:
l.Info().Msg("Already imported from Veidemann")
}
}
elapsed := time.Since(start)
log.Info().Str("kind", kind.String()).Int("total", count).Int("imported", imported).Int("errors", failed).Str("elapsed", elapsed.String()).Msg("Import from Veidemann complete")
return nil
}
// Iterate iterates over all keys in the db and calls the function with the key and value.
// The function is not called in parallel.
func (d *ImportDb) Iterate(fn func([]byte, []byte)) error {
stream := d.db.NewStream()
// -- Optional settings
stream.NumGo = 16 // Set number of goroutines to use for iteration.
stream.LogPrefix = "Badger.Streaming" // For identifying stream logs. Outputs to Logger.
// -- End of optional settings.
// Send is called serially, while Stream.Orchestrate is running.
stream.Send = func(buf *z.Buffer) error {
list, err := badger.BufferToKVList(buf)
if err != nil {
return err
}
for _, kv := range list.GetKv() {
fn(kv.Key, kv.Value)
}
return nil
}
// Run the stream
return stream.Orchestrate(context.Background())
}
// Get returns the ids for the key
func (d *ImportDb) Get(key string) (ids []string, err error) {
for {
err = d.db.View(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(key))
if errors.Is(err, badger.ErrKeyNotFound) {
return nil
}
if err != nil {
return err
}
return item.Value(func(v []byte) error {
ids = d.bytesToStringArray(v)
return nil
})
})
if !errors.Is(err, badger.ErrConflict) {
break
}
}
return ids, err
}
// Set sets the id as a value for the key.
func (d *ImportDb) Set(key string, id string) (code ExistsCode, ids []string, err error) {
for {
err = d.db.Update(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(key))
if errors.Is(err, badger.ErrKeyNotFound) {
code = NewKey
ids = append(ids, id)
v := d.stringArrayToBytes(ids)
return txn.Set([]byte(key), v)
}
if err != nil {
return err
}
err = item.Value(func(v []byte) error {
ids = d.bytesToStringArray(v)
return nil
})
if err != nil {
return err
}
if !stringArrayContains(ids, id) {
code = NewId
ids = append(ids, id)
v := d.stringArrayToBytes(ids)
return txn.Set([]byte(key), v)
} else {
code = Exists
}
return nil
})
if !errors.Is(err, badger.ErrConflict) {
break
}
}
return
}
// stringArrayToBytes returns a byte array from a string array
func (d *ImportDb) stringArrayToBytes(v []string) []byte {
buf := &bytes.Buffer{}
if err := gob.NewEncoder(buf).Encode(v); err != nil {
panic(err)
}
return buf.Bytes()
}
// bytesToStringArray returns a string array from a byte array
func (d *ImportDb) bytesToStringArray(v []byte) []string {
buf := bytes.NewBuffer(v)
strs := []string{}
if err := gob.NewDecoder(buf).Decode(&strs); err != nil && !errors.Is(err, io.EOF) {
panic(err)
}
return strs
}
// stringArrayContains returns true if the string array contains the string
func stringArrayContains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}