-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
94 lines (71 loc) · 2.24 KB
/
config.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
package tdigest
import (
"fmt"
"math"
"github.com/ajwerner/tdigest/internal/scale"
)
type config struct {
// compression factor controls the target maximum number of centroids in a
// fully compressed TDigest.
compression float64
// bufferFactor is multiple of the compression size used to buffer unmerged
// centroids. See config.bufferSize().
bufferFactor int
// scale controls how weight is apportioned to centroids.
scale scale.Func
useWeightLimit bool
}
func (cfg config) bufferSize() int {
return int(math.Ceil(cfg.compression)) * (1 + cfg.bufferFactor)
}
// Option configures a TDigest.
type Option interface {
apply(*config)
}
// BufferFactor configures the size of the buffer for uncompressed data.
// The default value is 5.
func BufferFactor(factor int) Option {
return bufferFactorOption(factor)
}
// Compression controls the maximum number of centroids.
func Compression(compression float64) Option {
return compressionOption(compression)
}
// UseWeightLimit enables the weightLimit compression heuristic. It is cheaper
// to compute but does not provide a strict upper bound on the total compressed
// size of the TDigest.
func UseWeightLimit(useWeightLimit bool) Option {
return weightLimitOption(useWeightLimit)
}
type bufferFactorOption int
func (o bufferFactorOption) apply(cfg *config) { cfg.bufferFactor = int(o) }
func (o bufferFactorOption) String() string {
return fmt.Sprintf("bufferFactor=%d", o)
}
type compressionOption float64
func (o compressionOption) apply(cfg *config) { cfg.compression = float64(o) }
func (o compressionOption) String() string {
return fmt.Sprintf("compression=%.1f", o)
}
type scaleOption struct{ scale.Func }
func (o scaleOption) apply(cfg *config) { cfg.scale = o.Func }
func (o scaleOption) String() string {
return fmt.Sprintf("scale=%v", o.Func)
}
type weightLimitOption bool
func (o weightLimitOption) apply(cfg *config) { cfg.useWeightLimit = bool(o) }
func (o weightLimitOption) String() string {
return fmt.Sprintf("weightLimit=%v", bool(o))
}
var defaultConfig = config{
compression: 128,
bufferFactor: 5,
scale: scaleOption{scale.K2{}},
useWeightLimit: false,
}
type optionList []Option
func (l optionList) apply(cfg *config) {
for _, o := range l {
o.apply(cfg)
}
}