-
Notifications
You must be signed in to change notification settings - Fork 84
/
topn.go
209 lines (188 loc) · 5.67 KB
/
topn.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
// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package streaming
import (
"github.com/emirpasic/gods/maps/treemap"
"github.com/emirpasic/gods/utils"
"github.com/pkg/errors"
"github.com/apache/skywalking-banyandb/pkg/flow"
)
// TopNSort defines the order of sorting.
type TopNSort uint8
// The available order of sorting.
const (
DESC TopNSort = iota
ASC
)
type windowedFlow struct {
f *streamingFlow
wa flow.WindowAssigner
}
func (s *windowedFlow) TopN(topNum int, opts ...any) flow.Flow {
s.wa.(*tumblingTimeWindows).aggregationFactory = func() flow.AggregationOp {
topNAggrFunc := &topNAggregatorGroup{
cacheSize: topNum,
sort: DESC,
}
// apply user customized options
for _, opt := range opts {
if applier, ok := opt.(TopNOption); ok {
applier(topNAggrFunc)
}
}
if topNAggrFunc.sortKeyExtractor == nil {
s.f.drainErr(errors.New("sortKeyExtractor must be specified"))
}
if topNAggrFunc.sort == ASC {
topNAggrFunc.comparator = utils.Int64Comparator
} else { // DESC
topNAggrFunc.comparator = func(a, b interface{}) int {
return utils.Int64Comparator(b, a)
}
}
topNAggrFunc.aggregatorGroup = make(map[string]*topNAggregator)
return topNAggrFunc
}
return s.f
}
type topNAggregatorGroup struct {
aggregatorGroup map[string]*topNAggregator
sortKeyExtractor func(flow.StreamRecord) int64
groupKeyExtractor func(flow.StreamRecord) string
comparator utils.Comparator
cacheSize int
sort TopNSort
}
type topNAggregator struct {
*topNAggregatorGroup
treeMap *treemap.Map
currentTopNum int
dirty bool
}
// TopNOption is the option to set up a top-n aggregator group.
type TopNOption func(aggregator *topNAggregatorGroup)
// WithSortKeyExtractor sets a closure to extract the sorting key.
func WithSortKeyExtractor(sortKeyExtractor func(flow.StreamRecord) int64) TopNOption {
return func(aggregator *topNAggregatorGroup) {
aggregator.sortKeyExtractor = sortKeyExtractor
}
}
// WithGroupKeyExtractor extract group key from the StreamRecord.
func WithGroupKeyExtractor(groupKeyExtractor func(flow.StreamRecord) string) TopNOption {
return func(aggregator *topNAggregatorGroup) {
aggregator.groupKeyExtractor = groupKeyExtractor
}
}
// OrderBy sets the sorting order.
func OrderBy(sort TopNSort) TopNOption {
return func(aggregator *topNAggregatorGroup) {
aggregator.sort = sort
}
}
func (t *topNAggregatorGroup) Add(input []flow.StreamRecord) {
for _, item := range input {
sortKey := t.sortKeyExtractor(item)
groupKey := t.groupKeyExtractor(item)
aggregator := t.getOrCreateGroup(groupKey)
if aggregator.checkSortKeyInBufferRange(sortKey) {
aggregator.put(sortKey, item)
aggregator.doCleanUp()
}
}
}
func (t *topNAggregatorGroup) Snapshot() interface{} {
groupRanks := make(map[string][]*Tuple2)
for group, aggregator := range t.aggregatorGroup {
if !aggregator.dirty {
continue
}
aggregator.dirty = false
iter := aggregator.treeMap.Iterator()
items := make([]*Tuple2, 0, aggregator.currentTopNum)
for iter.Next() {
list := iter.Value().([]interface{})
for _, item := range list {
items = append(items, &Tuple2{iter.Key(), item})
}
}
groupRanks[group] = items
}
return groupRanks
}
func (t *topNAggregatorGroup) Dirty() bool {
for _, aggregator := range t.aggregatorGroup {
if aggregator.dirty {
return true
}
}
return false
}
func (t *topNAggregatorGroup) getOrCreateGroup(group string) *topNAggregator {
aggregator, groupExist := t.aggregatorGroup[group]
if groupExist {
return aggregator
}
t.aggregatorGroup[group] = &topNAggregator{
topNAggregatorGroup: t,
treeMap: treemap.NewWith(t.comparator),
}
return t.aggregatorGroup[group]
}
func (t *topNAggregator) doCleanUp() {
// do cleanup: maintain the treeMap windowSize
if t.currentTopNum > t.cacheSize {
lastKey, lastValues := t.treeMap.Max()
size := len(lastValues.([]interface{}))
// remove last one
if size <= 1 {
t.currentTopNum -= size
t.treeMap.Remove(lastKey)
} else {
t.currentTopNum--
t.treeMap.Put(lastKey, lastValues.([]interface{})[0:size-1])
}
}
}
func (t *topNAggregator) put(sortKey int64, data flow.StreamRecord) {
t.currentTopNum++
t.dirty = true
if existingList, ok := t.treeMap.Get(sortKey); ok {
existingList = append(existingList.([]interface{}), data)
t.treeMap.Put(sortKey, existingList)
} else {
t.treeMap.Put(sortKey, []interface{}{data})
}
}
func (t *topNAggregator) checkSortKeyInBufferRange(sortKey int64) bool {
// get the "maximum" item
// - if ASC, the maximum item
// - else DESC, the minimum item
worstKey, _ := t.treeMap.Max()
if worstKey == nil {
// return true if the buffer is empty.
return true
}
if t.comparator(sortKey, worstKey.(int64)) < 0 {
return true
}
return t.currentTopNum < t.cacheSize
}
// Tuple2 is a tuple with 2 fields. Each field may be a separate type.
type Tuple2 struct {
V1 interface{} `json:"v1"`
V2 interface{} `json:"v2"`
}