-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbbhash_opts.go
More file actions
84 lines (71 loc) · 2.15 KB
/
bbhash_opts.go
File metadata and controls
84 lines (71 loc) · 2.15 KB
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 bbhash
const (
// defaultGamma is the default expansion factor for the bit vector.
defaultGamma = 2.0
// minimalGamma is the smallest allowed expansion factor for the bit vector.
minimalGamma = 0.5
// Heuristic: 32 levels should be enough for even very large key sets
initialLevels = 32
// Maximum number of attempts (level) at making a perfect hash function.
// Per the paper, each successive level exponentially reduces the
// probability of collision.
maxLevel = 255
// Maximum number of partitions.
maxPartitions = 255
)
type options struct {
gamma float64
initialLevels int
partitions int
parallel bool
reverseMap bool
}
func newOptions(opts ...Options) *options {
o := &options{
gamma: defaultGamma,
initialLevels: initialLevels,
partitions: 1,
parallel: false,
reverseMap: false,
}
for _, opt := range opts {
opt(o)
}
return o
}
type Options func(*options)
// Gamma sets the gamma parameter for creating a BBHash.
// The gamma parameter is the expansion factor for the bit vector; the paper recommends
// a value of 2.0. The larger the value the more space will be consumed by the BBHash.
func Gamma(gamma float64) Options {
return func(o *options) {
o.gamma = max(gamma, minimalGamma)
}
}
// InitialLevels sets the initial number of levels to use when creating a BBHash.
func InitialLevels(levels int) Options {
return func(o *options) {
o.initialLevels = levels
}
}
// Partitions sets the number of partitions to use when creating a BBHash2.
// The keys are partitioned into the given the number partitions.
// Setting partitions to less than 2 results in a single BBHash, wrapped in a BBHash2.
func Partitions(partitions int) Options {
return func(o *options) {
o.partitions = max(min(partitions, maxPartitions), 1)
}
}
// Parallel creates a BBHash by sharding the keys across multiple goroutines.
// This option is not compatible with the Partitions option.
func Parallel() Options {
return func(o *options) {
o.parallel = true
}
}
// WithReverseMap creates a reverse map when creating a BBHash.
func WithReverseMap() Options {
return func(o *options) {
o.reverseMap = true
}
}