-
Notifications
You must be signed in to change notification settings - Fork 29
/
topk.go
210 lines (173 loc) · 4.73 KB
/
topk.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Package topk implements the Filtered Space-Saving TopK streaming algorithm
/*
The original Space-Saving algorithm:
https://icmi.cs.ucsb.edu/research/tech_reports/reports/2005-23.pdf
The Filtered Space-Saving enhancement:
http://www.l2f.inesc-id.pt/~fmmb/wiki/uploads/Work/misnis.ref0a.pdf
This implementation follows the algorithm of the FSS paper, but not the
suggested implementation. Specifically, we use a heap instead of a sorted list
of monitored items, and since we are also using a map to provide O(1) access on
update also don't need the c_i counters in the hash table.
Licensed under the MIT license.
*/
package topk
import (
"bytes"
"container/heap"
"encoding/gob"
"sort"
"github.com/dgryski/go-sip13"
)
// Element is a TopK item
type Element struct {
Key string
Count int
Error int
}
type elementsByCountDescending []Element
func (elts elementsByCountDescending) Len() int { return len(elts) }
func (elts elementsByCountDescending) Less(i, j int) bool {
return (elts[i].Count > elts[j].Count) || (elts[i].Count == elts[j].Count && elts[i].Key < elts[j].Key)
}
func (elts elementsByCountDescending) Swap(i, j int) { elts[i], elts[j] = elts[j], elts[i] }
type keys struct {
m map[string]int
elts []Element
}
// Implement the container/heap interface
func (tk *keys) Len() int { return len(tk.elts) }
func (tk *keys) Less(i, j int) bool {
return (tk.elts[i].Count < tk.elts[j].Count) || (tk.elts[i].Count == tk.elts[j].Count && tk.elts[i].Error > tk.elts[j].Error)
}
func (tk *keys) Swap(i, j int) {
tk.elts[i], tk.elts[j] = tk.elts[j], tk.elts[i]
tk.m[tk.elts[i].Key] = i
tk.m[tk.elts[j].Key] = j
}
func (tk *keys) Push(x interface{}) {
e := x.(Element)
tk.m[e.Key] = len(tk.elts)
tk.elts = append(tk.elts, e)
}
func (tk *keys) Pop() interface{} {
var e Element
e, tk.elts = tk.elts[len(tk.elts)-1], tk.elts[:len(tk.elts)-1]
delete(tk.m, e.Key)
return e
}
// Stream calculates the TopK elements for a stream
type Stream struct {
n int
k keys
alphas []int
}
// New returns a Stream estimating the top n most frequent elements
func New(n int) *Stream {
return &Stream{
n: n,
k: keys{m: make(map[string]int), elts: make([]Element, 0, n)},
alphas: make([]int, n*6), // 6 is the multiplicative constant from the paper
}
}
func reduce(x uint64, n int) uint32 {
return uint32(uint64(uint32(x)) * uint64(n) >> 32)
}
// Insert adds an element to the stream to be tracked
// It returns an estimation for the just inserted element
func (s *Stream) Insert(x string, count int) Element {
xhash := reduce(sip13.Sum64Str(0, 0, x), len(s.alphas))
// are we tracking this element?
if idx, ok := s.k.m[x]; ok {
s.k.elts[idx].Count += count
e := s.k.elts[idx]
heap.Fix(&s.k, idx)
return e
}
// can we track more elements?
if len(s.k.elts) < s.n {
// there is free space
e := Element{Key: x, Count: count}
heap.Push(&s.k, e)
return e
}
if s.alphas[xhash]+count < s.k.elts[0].Count {
e := Element{
Key: x,
Error: s.alphas[xhash],
Count: s.alphas[xhash] + count,
}
s.alphas[xhash] += count
return e
}
// replace the current minimum element
minKey := s.k.elts[0].Key
mkhash := reduce(sip13.Sum64Str(0, 0, minKey), len(s.alphas))
s.alphas[mkhash] = s.k.elts[0].Count
e := Element{
Key: x,
Error: s.alphas[xhash],
Count: s.alphas[xhash] + count,
}
s.k.elts[0] = e
// we're not longer monitoring minKey
delete(s.k.m, minKey)
// but 'x' is as array position 0
s.k.m[x] = 0
heap.Fix(&s.k, 0)
return e
}
// Keys returns the current estimates for the most frequent elements
func (s *Stream) Keys() []Element {
elts := append([]Element(nil), s.k.elts...)
sort.Sort(elementsByCountDescending(elts))
return elts
}
// Estimate returns an estimate for the item x
func (s *Stream) Estimate(x string) Element {
xhash := reduce(sip13.Sum64Str(0, 0, x), len(s.alphas))
// are we tracking this element?
if idx, ok := s.k.m[x]; ok {
e := s.k.elts[idx]
return e
}
count := s.alphas[xhash]
e := Element{
Key: x,
Error: count,
Count: count,
}
return e
}
func (s *Stream) GobEncode() ([]byte, error) {
buf := bytes.Buffer{}
enc := gob.NewEncoder(&buf)
if err := enc.Encode(s.n); err != nil {
return nil, err
}
if err := enc.Encode(s.k.m); err != nil {
return nil, err
}
if err := enc.Encode(s.k.elts); err != nil {
return nil, err
}
if err := enc.Encode(s.alphas); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (s *Stream) GobDecode(b []byte) error {
dec := gob.NewDecoder(bytes.NewBuffer(b))
if err := dec.Decode(&s.n); err != nil {
return err
}
if err := dec.Decode(&s.k.m); err != nil {
return err
}
if err := dec.Decode(&s.k.elts); err != nil {
return err
}
if err := dec.Decode(&s.alphas); err != nil {
return err
}
return nil
}