-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcardinal.go
More file actions
174 lines (129 loc) · 2.48 KB
/
Copy pathcardinal.go
File metadata and controls
174 lines (129 loc) · 2.48 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
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
package cardinal
import (
"sync"
"time"
)
type (
Cardinal struct {
sync.Mutex
buf []*Filter
duration time.Duration
chunk_duration time.Duration
last_t time.Time
i int
filter *Filter
}
Filter struct {
set map[string]struct{}
uniques uint64
total uint64
}
)
const (
CHUNKS = 10
CHUNK_SIZE = 4096
)
func New(duration time.Duration) *Cardinal {
buf := make([]*Filter, CHUNKS)
for i, _ := range buf {
buf[i] = &Filter{set: map[string]struct{}{}}
}
return &Cardinal{
buf: buf,
filter: buf[0],
duration: duration,
chunk_duration: duration / CHUNKS,
}
}
func (c *Cardinal) Add(token string) {
c.Lock()
t := time.Now().Truncate(c.chunk_duration)
if !t.Equal(c.last_t) {
c.last_t = t
c.i++
next_i := c.i % len(c.buf)
// always create a new filter with the size of the previous
// as the estimated number of items to minimize collisions
c.buf[next_i] = &Filter{set: map[string]struct{}{}}
c.filter = c.buf[next_i]
}
// check all filters before incrementing
if !c.check(token) {
c.filter.set[token] = struct{}{}
c.filter.uniques++
}
c.filter.total++
c.Unlock()
}
func (c *Cardinal) Check(token string) (r bool) {
c.Lock()
r = c.check(token)
c.Unlock()
return
}
func (c *Cardinal) Cardinality() (r float64) {
c.Lock()
r = c.cardinality()
c.Unlock()
return
}
func (c *Cardinal) Count() (r uint64) {
c.Lock()
r = c.count()
c.Unlock()
return
}
func (c *Cardinal) Uniques() (r uint64) {
c.Lock()
r = c.uniques()
c.Unlock()
return
}
func (c *Cardinal) Duration() time.Duration {
return c.duration
}
func (c *Cardinal) Reset() {
c.Lock()
for _, filter := range c.buf {
filter.reset()
}
c.Unlock()
}
func (c *Cardinal) check(token string) bool {
for _, filter := range c.buf {
if _, exists := filter.set[token]; exists {
return true
}
}
return false
}
func (c *Cardinal) cardinality() float64 {
var uniques, total uint64
for _, filter := range c.buf {
uniques, total = uniques+filter.uniques, total+filter.total
}
return float64(uniques) / float64(total)
}
func (c *Cardinal) count() (total uint64) {
for _, filter := range c.buf {
total += filter.total
}
return
}
func (c *Cardinal) uniques() (uniques uint64) {
for _, filter := range c.buf {
uniques += filter.uniques
}
return
}
func (f *Filter) reset() {
f.set = map[string]struct{}{}
f.uniques = 0
f.total = 0
}
func min(a, b uint) uint {
if a < b {
return b
}
return a
}