forked from hashicorp/raft-wal
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptions.go
75 lines (66 loc) · 1.85 KB
/
options.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
// Copyright (c) HashiCorp, Inc
// SPDX-License-Identifier: MPL-2.0
package wal
import (
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/polarsignals/wal/fs"
"github.com/polarsignals/wal/metadb"
"github.com/polarsignals/wal/segment"
"github.com/polarsignals/wal/types"
)
// WithMetaStore is an option that allows a custom MetaStore to be provided to
// the WAL. If not used the default MetaStore is used.
func WithMetaStore(db types.MetaStore) walOpt {
return func(w *WAL) {
w.metaDB = db
}
}
// WithSegmentFiler is an option that allows a custom SegmentFiler (and hence
// Segment Reader/Writer implementation) to be provided to the WAL. If not used
// the default SegmentFiler is used.
func WithSegmentFiler(sf types.SegmentFiler) walOpt {
return func(w *WAL) {
w.sf = sf
}
}
// WithLogger is an option that allows a custom logger to be used.
func WithLogger(logger log.Logger) walOpt {
return func(w *WAL) {
w.logger = logger
}
}
// WithSegmentSize is an option that allows a custom segmentSize to be set.
func WithSegmentSize(size int) walOpt {
return func(w *WAL) {
w.segmentSize = size
}
}
// WithMetrics is an option that allows specifying a custom metrics object.
func WithMetrics(m *Metrics) walOpt {
return func(w *WAL) {
w.metrics = m
}
}
func (w *WAL) applyDefaultsAndValidate() error {
// Defaults
if w.logger == nil {
w.logger = log.NewNopLogger()
}
if w.sf == nil {
// These are not actually swappable via options right now but we override
// them in tests. Only load the default implementations if they are not set.
vfs := fs.New()
w.sf = segment.NewFiler(w.dir, vfs)
}
if w.metrics == nil {
w.metrics = newWALMetrics(prometheus.NewRegistry())
}
if w.metaDB == nil {
w.metaDB = &metadb.BoltMetaDB{}
}
if w.segmentSize == 0 {
w.segmentSize = DefaultSegmentSize
}
return nil
}