-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogtop.go
135 lines (112 loc) · 2.81 KB
/
logtop.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
package logtop
import (
"time"
"github.com/timtadh/data-structures/tree"
"github.com/timtadh/data-structures/tree/avl"
"github.com/timtadh/data-structures/types"
)
type Tuple struct {
ID string
Count uint64
UpdatedAt time.Time
indexFieldCache map[string]Field
}
func (tup *Tuple) FlushIndexFieldCache(key string) {
delete(tup.indexFieldCache, key)
}
func (tup *Tuple) IndexedByCount() Field {
if f, ok := tup.indexFieldCache["Count"]; ok {
return f
}
f := &CompoundField{
Fields: []Field{
&Uint64Field{tup.Count},
&StringField{tup.ID},
},
}
tup.indexFieldCache["Count"] = f
return f
}
func (tup *Tuple) IndexedByUpdatedAt() Field {
if f, ok := tup.indexFieldCache["UpdatedAt"]; ok {
return f
}
f := &CompoundField{
Fields: []Field{
&Uint64Field{uint64(tup.UpdatedAt.UnixNano())},
&StringField{tup.ID},
},
}
tup.indexFieldCache["UpdatedAt"] = f
return f
}
func NewTuple(id string) *Tuple {
return &Tuple{
ID: id,
Count: 0,
indexFieldCache: make(map[string]Field),
}
}
type TopNTree struct {
countIndex *avl.AvlTree
updatedAtIndex *avl.AvlTree
table map[string]*Tuple
}
func NewTopNTree() *TopNTree {
return &TopNTree{
countIndex: &avl.AvlTree{},
updatedAtIndex: &avl.AvlTree{},
table: make(map[string]*Tuple),
}
}
func (top *TopNTree) Increment(id string, updatedAt time.Time) {
tup, ok := top.table[id]
if !ok {
tup = NewTuple(id)
top.table[id] = tup
}
if ok {
top.countIndex.Remove(tup.IndexedByCount())
top.updatedAtIndex.Remove(tup.IndexedByUpdatedAt())
tup.FlushIndexFieldCache("Count")
tup.FlushIndexFieldCache("UpdatedAt")
}
tup.Count++
tup.UpdatedAt = updatedAt
top.countIndex.Put(tup.IndexedByCount(), tup)
top.updatedAtIndex.Put(tup.IndexedByUpdatedAt(), tup)
}
func (top *TopNTree) TopN(n uint64) []Tuple {
tups := []Tuple{}
for _, tup, next := top.iterateByCountDesc()(); next != nil; _, tup, next = next() {
if n == 0 {
break
}
n--
if tup, ok := tup.(*Tuple); ok {
tups = append(tups, *tup)
}
}
return tups
}
func (top *TopNTree) PruneBefore(before time.Time) {
for _, tup, next := top.iterateByUpdatedAtAsc()(); next != nil; _, tup, next = next() {
tup := tup.(*Tuple)
if before.Before(tup.UpdatedAt) {
break
}
top.countIndex.Remove(tup.IndexedByCount())
top.updatedAtIndex.Remove(tup.IndexedByUpdatedAt())
delete(top.table, tup.ID)
}
}
func (top *TopNTree) iterateByCountDesc() types.KVIterator {
root := top.countIndex.Root().(*avl.AvlNode)
tni := TraverseBinaryTreeInReverseOrder(root)
return types.MakeKVIteratorFromTreeNodeIterator(tni)
}
func (top *TopNTree) iterateByUpdatedAtAsc() types.KVIterator {
root := top.countIndex.Root().(*avl.AvlNode)
tni := tree.TraverseBinaryTreeInOrder(root)
return types.MakeKVIteratorFromTreeNodeIterator(tni)
}