-
Notifications
You must be signed in to change notification settings - Fork 53
/
store.go
112 lines (94 loc) · 1.97 KB
/
store.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
package main
import (
"fmt"
"strconv"
"strings"
"sync"
"time"
)
const trendSize = 7
type RowData struct {
key []string
trend []float64
count int
data map[string]interface{}
}
func (d RowData) GetCount() string {
return strconv.Itoa(d.count)
}
func (d RowData) Get(key string) interface{} {
return d.data[key]
}
func (d RowData) GetTrend() []float64 {
return d.trend
}
func (d RowData) GetData() map[string]interface{} {
return d.data
}
type Store struct {
sync.RWMutex
duration time.Duration
distance int
keys []string
rows []RowData
}
func NewStore(duration time.Duration, distance int, keys []string) *Store {
return &Store{
duration: duration,
distance: distance,
keys: keys,
rows: make([]RowData, 0),
}
}
func (s *Store) SetKeys(keys []string) {
s.keys = keys
}
func (s *Store) Push(value map[string]interface{}) {
key := s.Key(value)
for i := range s.rows {
if ComputeDistance(key, s.rows[i].key) < s.distance {
s.rows[i].trend[len(s.rows[i].trend)-1] += 1
s.rows[i].count++
s.rows[i].data = value
return
}
}
data := RowData{
key: key,
trend: make([]float64, trendSize),
count: 1,
data: value,
}
data.trend[len(data.trend)-1] += 1
s.rows = append(s.rows, data)
}
func (s *Store) Len() int {
return len(s.rows)
}
func (s *Store) Get(i int) RowData {
if i >= 0 && i < len(s.rows) {
return s.rows[i]
}
return RowData{}
}
func (s *Store) Key(value map[string]interface{}) []string {
key := make([]string, 0)
for _, name := range s.keys {
sub := strings.Split(fmt.Sprintf("%v", value[name]), " ")
// For short parts of key, double sub length x2.
// Doubling levenshtein distance for this part of key.
if len(sub) < s.distance {
sub = append(sub, sub...)
}
key = append(key, sub...)
}
return key
}
func (s *Store) Shift() {
for i := range s.rows {
for j := 0; j < len(s.rows[i].trend)-1; j++ {
s.rows[i].trend[j] = s.rows[i].trend[j+1]
}
s.rows[i].trend[len(s.rows[i].trend)-1] = 0
}
}