-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattributes.zzzgo
245 lines (229 loc) · 5.63 KB
/
attributes.zzzgo
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// TEMPLATE-FILE
// TEMPLATE-FILE
// TEMPLATE-FILE
// TEMPLATE-FILE
// TEMPLATE-FILE
// TEMPLATE-FILE
package xopconsole
import (
"encoding/json"
"fmt"
"sync"
"github.com/xoplog/xop-go/xopat"
"github.com/xoplog/xop-go/xopbase"
"github.com/xoplog/xop-go/xoputil"
)
const numSinglePrealloc = 20
const numMultiPrealloc = 12
// Writer implements io.Writer interface for json.Encoder
func (a *AttributeBuilder) Write(n []byte) (int, error) {
*a.encodeTarget = append(*a.encodeTarget, n...)
return len(n), nil
}
type AttributeBuilder struct {
lock sync.Mutex
singlesBuf [numSinglePrealloc]singleAttribute
multiBuf [numMultiPrealloc]multiAttribute
singles []singleAttribute
multis []multiAttribute
Type xopbase.DataType
singleMap map[xopat.K]*singleAttribute
multiMap map[xopat.K]*multiAttribute
anyChanged bool
encodeTarget *[]byte
encoder *json.Encoder
request *Span
}
type singleAttribute struct {
attribute
KeyValue []byte
Buf [40]byte
keyLen int
}
type multiAttribute struct {
attribute
Buf [100]byte
Distinct map[string]struct{}
Builder Builder
}
type attribute struct {
Changed bool
Type xopbase.DataType
}
func (a *AttributeBuilder) Init(request *Span) {
a.singles = a.singlesBuf[:0]
a.multis = a.multiBuf[:0]
a.singleMap = make(map[xopat.K]*singleAttribute)
a.multiMap = make(map[xopat.K]*multiAttribute)
a.anyChanged = false
a.request = request
}
// Append will only add data if there is any unflushed data to add.
func (a *AttributeBuilder) Append(b *Builder, onlyChanged bool, attributesObject bool) {
a.lock.Lock()
defer a.lock.Unlock()
if (!a.anyChanged && onlyChanged) || (len(a.multiMap) == 0 && len(a.singleMap) == 0) {
return
}
a.anyChanged = false
for _, m := range a.multiMap {
if m.Changed || !onlyChanged {
b.AppendByte(' ')
b.AppendBytes(m.Builder.B)
m.Changed = false
}
}
for _, s := range a.singleMap {
if s.Changed || !onlyChanged {
b.AppendByte(' ')
b.AppendBytes(s.KeyValue)
s.Changed = false
}
}
}
func (m *multiAttribute) init(a *AttributeBuilder, k xopat.AttributeInterface) {
m.Builder.B = m.Buf[:0]
m.Builder.Reset()
m.Distinct = nil
}
// addMulti sets up the multiMap entry, it does not populate
// the value or builder.
func (a *AttributeBuilder) addMulti(k xopat.AttributeInterface) (*multiAttribute, bool) {
var m *multiAttribute
var ok bool
m, ok = a.multiMap[k.Key()]
if !ok {
if len(a.multis) == cap(a.multis) {
a.multis = make([]multiAttribute, 0, cap(a.multis))
}
a.multis = a.multis[:len(a.multis)+1]
m = &a.multis[len(a.multis)-1]
m.init(a, k)
a.multiMap[k.Key()] = m
}
m.Changed = true
return m, ok
}
func (s *singleAttribute) init(k xopat.AttributeInterface) {
b := xoputil.JBuilder{
B: s.Buf[:0],
}
b.AppendBytes(k.ConsoleKey())
s.Changed = true
s.KeyValue = b.B
}
func (a *AttributeBuilder) addSingle(k xopat.AttributeInterface) (*singleAttribute, bool) {
s, ok := a.singleMap[k.Key()]
if !ok {
if len(a.singles) == cap(a.singles) {
a.singles = make([]singleAttribute, 0, cap(a.singles))
}
a.singles = a.singles[:len(a.singles)+1]
s = &a.singles[len(a.singles)-1]
s.init(k)
a.singleMap[k.Key()] = s
}
s.Changed = true
return s, ok
}
func (a *AttributeBuilder) defineKey(k xopat.AttributeInterface) {
b := xoputil.JBuilder{
B: make([]byte, 0, len(k.DefinitionJSONBytes())+24),
}
b.AppendBytes([]byte("xop Def "))
b.B = DefaultTimeFormatter(b.B, a.request.StartTime)
b.AppendByte(' ')
b.AppendBytes(k.DefinitionJSONBytes())
b.AppendByte('\n')
_, err := a.request.logger.out.Write(b.B)
if err != nil {
a.request.logger.errorReporter(err)
}
}
// MACRO BaseAttribute
func (a *AttributeBuilder) MetadataZZZ(k *xopat.ZZZAttribute, v zzz) {
a.lock.Lock()
defer a.lock.Unlock()
a.anyChanged = true
//CONDITIONAL ONLY:Any
if a.encoder == nil {
a.encoder = json.NewEncoder(a)
a.encoder.SetEscapeHTML(false)
}
//END CONDITIONAL
addAttribute := func(b *Builder) {
//CONDITIONAL ONLY:Int64
if k.SubType() == xopat.AttributeTypeDuration {
b.AttributeDuration(time.Duration(v))
} else {
b.AttributeZZZ(v)
}
//ELSE CONDITIONAL
b.AttributeZZZ(v)
//END CONDITIONAL
}
if k.Multiple() {
m, preExisting := a.addMulti(k)
if !preExisting {
a.defineKey(k)
m.Builder.AppendBytes(k.ConsoleKey())
}
m.Type = xopbase.ZZZDataType
//CONDITIONAL ONLY:Any
a.encodeTarget = &m.Builder.B
m.Builder.encoder = a.encoder
//END CONDITIONAL
lenAfterKey := len(m.Builder.B)
if preExisting {
m.Builder.AppendByte(',')
}
// we add the new value unconditionally but can retroactively remove it by shortening to lenAfterKey
lenBeforeData := len(m.Builder.B)
addAttribute(&m.Builder)
if k.Distinct() {
sk := string(m.Builder.B[lenBeforeData:len(m.Builder.B)])
if m.Distinct == nil {
m.Distinct = make(map[string]struct{})
m.Distinct[sk] = struct{}{}
} else {
if _, ok := m.Distinct[sk]; ok {
m.Builder.B = m.Builder.B[:lenAfterKey]
if m.Builder.B[len(m.Builder.B)-1] == ',' {
m.Builder.B = m.Builder.B[0 : len(m.Builder.B)-1]
}
} else {
m.Distinct[sk] = struct{}{}
}
}
}
return
}
// Single attribute
s, preExisting := a.addSingle(k)
if preExisting {
if k.Locked() {
return
} else {
s.KeyValue = s.KeyValue[:s.keyLen]
}
} else {
s.keyLen = len(s.KeyValue)
a.defineKey(k)
}
s.Type = xopbase.ZZZDataType
// TODO: reuse or pool the builder
b := Builder{
JBuilder: xoputil.JBuilder{
B: s.KeyValue,
},
//CONDITIONAL ONLY:Any
encoder: a.encoder,
//END CONDITIONAL
}
//CONDITIONAL ONLY:Any
a.encodeTarget = &b.B
//END CONDITIONAL
addAttribute(&b)
s.KeyValue = b.B
return
}