-
Notifications
You must be signed in to change notification settings - Fork 2
/
sllb.go
84 lines (75 loc) · 1.79 KB
/
sllb.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
package sllb
import (
"errors"
"math"
metro "github.com/dgryski/go-metro"
)
var (
exp32 = math.Pow(2, 32)
)
/*
Sketch adapts the LogLog-Beta algorithm basen on Flajolet et. al to the
data stream processing by adding a sliding window mechanism. It has the
advantage to estimate at any time the number of flows seen over any duration
bounded by the length of the sliding window.
*/
type Sketch struct {
window uint64
alpha float64
p uint
m uint
regs []*reg
latestTimestamp uint64
}
/*
New return a new Sketch with errorRate
*/
func New(errRate float64) (*Sketch, error) {
if !(0 < errRate && errRate < 1) {
return nil, errors.New("errRate must be between 0 and 1")
}
p := uint(math.Ceil(math.Log2(math.Pow((1.04 / errRate), 2))))
m := uint(1) << p
sk := &Sketch{
p: p,
m: m,
regs: make([]*reg, m, m),
alpha: alpha(float64(m)),
}
for i := range sk.regs {
sk.regs[i] = newReg()
}
return sk, nil
}
// NewDefault returns a sketch with errorRate 0.008
func NewDefault() *Sketch {
sk, _ := New(0.008)
return sk
}
func (sk *Sketch) valAndPos(value []byte) (uint8, uint64) {
val := metro.Hash64(value, 32)
k := 64 - sk.p
j := val >> uint(k)
R := rho(val<<sk.p, 6)
return R, j
}
/*
Insert a value with a timestamp to the Sketch.
*/
func (sk *Sketch) Insert(timestamp uint64, value []byte) {
R, j := sk.valAndPos(value)
sk.regs[j].insert(tR{timestamp, R})
if timestamp > sk.latestTimestamp {
sk.latestTimestamp = timestamp
}
}
/*
Estimate returns the estimated cardinality since a given timestamp
*/
func (sk *Sketch) Estimate(timestamp uint64) uint64 {
m := float64(sk.m)
sum := regSumSince(sk.regs, timestamp)
ez := zerosSince(sk.regs, timestamp)
beta := beta(ez)
return uint64(sk.alpha * m * (m - ez) / (beta + sum))
}