-
Notifications
You must be signed in to change notification settings - Fork 3
/
log_cluster.go
140 lines (120 loc) · 3.34 KB
/
log_cluster.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
package loggingdrain
import (
"encoding/json"
"strings"
)
type LogCluster struct {
id int64
logTemplateTokens []string
}
type logClusterMarshalStruct struct {
ID int64
LogTemplateTokens []string
}
func (cluster *LogCluster) MarshalJSON() ([]byte, error) {
marshalStruct := logClusterMarshalStruct{
ID: cluster.id,
LogTemplateTokens: cluster.logTemplateTokens,
}
return json.Marshal(&marshalStruct)
}
func (cluster *LogCluster) UnmarshalJSON(data []byte) error {
var marshalStruct logClusterMarshalStruct
err := json.Unmarshal(data, &marshalStruct)
if err != nil {
return err
}
cluster.id = marshalStruct.ID
cluster.logTemplateTokens = marshalStruct.LogTemplateTokens
return nil
}
func newLogCluster(id int64, templateTokens []string) *LogCluster {
return &LogCluster{
id: id,
logTemplateTokens: templateTokens,
}
}
func (cluster *LogCluster) getTemplate() string {
return strings.Join(cluster.logTemplateTokens, " ")
}
type treeNodeType int
const (
tree_node_type_root treeNodeType = iota
tree_node_type_length
tree_node_type_token
)
type treeNode struct {
nodeType treeNodeType
length int
tokenNodeChildren map[string]*treeNode
lengthNodeChildren map[int]*treeNode
clusters []*LogCluster
}
type treeNodeMarshalStruct struct {
NodeType treeNodeType
Length int
TokenNodeChildren map[string]*treeNode
LengthNodeChildren map[int]*treeNode
Clusters []*LogCluster
}
func (node *treeNode) MarshalJSON() ([]byte, error) {
marshalStruct := treeNodeMarshalStruct{
NodeType: node.nodeType,
Length: node.length,
TokenNodeChildren: node.tokenNodeChildren,
LengthNodeChildren: node.lengthNodeChildren,
Clusters: node.clusters,
}
return json.Marshal(&marshalStruct)
}
func (node *treeNode) UnmarshalJSON(data []byte) error {
var marshalStruct treeNodeMarshalStruct
err := json.Unmarshal(data, &marshalStruct)
if err != nil {
return err
}
node.clusters = marshalStruct.Clusters
node.length = marshalStruct.Length
node.lengthNodeChildren = marshalStruct.LengthNodeChildren
node.nodeType = marshalStruct.NodeType
node.tokenNodeChildren = marshalStruct.TokenNodeChildren
return nil
}
type treeNodes []*treeNode
func (nodes treeNodes) push(node *treeNode) treeNodes {
return append(nodes, node)
}
func (nodes treeNodes) pop() (treeNodes, *treeNode) {
latestNode := nodes[len(nodes)-1]
return nodes[:len(nodes)-1], latestNode
}
func newTreeNodes() treeNodes {
return []*treeNode{}
}
func newRootTreeNode() *treeNode {
return &treeNode{
nodeType: tree_node_type_root,
length: 0,
tokenNodeChildren: make(map[string]*treeNode),
lengthNodeChildren: make(map[int]*treeNode),
clusters: []*LogCluster{},
}
}
func newLengthTreeNode(length int) *treeNode {
return &treeNode{
nodeType: tree_node_type_length,
length: length,
tokenNodeChildren: make(map[string]*treeNode),
lengthNodeChildren: make(map[int]*treeNode),
clusters: []*LogCluster{},
}
}
func newTokenTreeNode() *treeNode {
return &treeNode{
nodeType: tree_node_type_token,
length: 0,
tokenNodeChildren: make(map[string]*treeNode),
lengthNodeChildren: make(map[int]*treeNode),
clusters: []*LogCluster{},
}
}