-
Notifications
You must be signed in to change notification settings - Fork 294
/
field_map.go
414 lines (337 loc) · 9.19 KB
/
field_map.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Copyright (c) quickfixengine.org All rights reserved.
//
// This file may be distributed under the terms of the quickfixengine.org
// license as defined by quickfixengine.org and appearing in the file
// LICENSE included in the packaging of this file.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
// THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE.
//
// See http://www.quickfixengine.org/LICENSE for licensing information.
//
// Contact [email protected] if any conditions of this licensing
// are not clear to you.
package quickfix
import (
"bytes"
"sort"
"sync"
"time"
)
// field stores a slice of TagValues.
type field []TagValue
func fieldTag(f field) Tag {
return f[0].tag
}
func initField(f field, tag Tag, value []byte) {
f[0].init(tag, value)
}
func writeField(f field, buffer *bytes.Buffer) {
for _, tv := range f {
buffer.Write(tv.bytes)
}
}
// tagOrder true if tag i should occur before tag j.
type tagOrder func(i, j Tag) bool
type tagSort struct {
tags []Tag
compare tagOrder
}
func (t tagSort) Len() int { return len(t.tags) }
func (t tagSort) Swap(i, j int) { t.tags[i], t.tags[j] = t.tags[j], t.tags[i] }
func (t tagSort) Less(i, j int) bool { return t.compare(t.tags[i], t.tags[j]) }
// FieldMap is a collection of fix fields that make up a fix message.
type FieldMap struct {
tagLookup map[Tag]field
tagSort
rwLock *sync.RWMutex
}
// ascending tags.
func normalFieldOrder(i, j Tag) bool { return i < j }
func (m *FieldMap) init() {
m.initWithOrdering(normalFieldOrder)
}
func (m *FieldMap) initWithOrdering(ordering tagOrder) {
m.rwLock = &sync.RWMutex{}
m.tagLookup = make(map[Tag]field)
m.compare = ordering
}
// Tags returns all of the Field Tags in this FieldMap.
func (m FieldMap) Tags() []Tag {
m.rwLock.RLock()
defer m.rwLock.RUnlock()
tags := make([]Tag, 0, len(m.tagLookup))
for t := range m.tagLookup {
tags = append(tags, t)
}
return tags
}
// Get parses out a field in this FieldMap. Returned reject may indicate the field is not present, or the field value is invalid.
func (m FieldMap) Get(parser Field) MessageRejectError {
return m.GetField(parser.Tag(), parser)
}
// Has returns true if the Tag is present in this FieldMap.
func (m FieldMap) Has(tag Tag) bool {
m.rwLock.RLock()
defer m.rwLock.RUnlock()
_, ok := m.tagLookup[tag]
return ok
}
// GetField parses of a field with Tag tag. Returned reject may indicate the field is not present, or the field value is invalid.
func (m FieldMap) GetField(tag Tag, parser FieldValueReader) MessageRejectError {
m.rwLock.RLock()
defer m.rwLock.RUnlock()
f, ok := m.tagLookup[tag]
if !ok {
return ConditionallyRequiredFieldMissing(tag)
}
if err := parser.Read(f[0].value); err != nil {
return IncorrectDataFormatForValue(tag)
}
return nil
}
// GetField parses of a field with Tag tag. Returned reject may indicate the field is not present, or the field value is invalid.
func (m FieldMap) getFieldNoLock(tag Tag, parser FieldValueReader) MessageRejectError {
f, ok := m.tagLookup[tag]
if !ok {
return ConditionallyRequiredFieldMissing(tag)
}
if err := parser.Read(f[0].value); err != nil {
return IncorrectDataFormatForValue(tag)
}
return nil
}
// GetBytes is a zero-copy GetField wrapper for []bytes fields.
func (m FieldMap) GetBytes(tag Tag) ([]byte, MessageRejectError) {
m.rwLock.RLock()
defer m.rwLock.RUnlock()
f, ok := m.tagLookup[tag]
if !ok {
return nil, ConditionallyRequiredFieldMissing(tag)
}
return f[0].value, nil
}
// getBytesNoLock is a lock free zero-copy GetField wrapper for []bytes fields.
func (m FieldMap) getBytesNoLock(tag Tag) ([]byte, MessageRejectError) {
f, ok := m.tagLookup[tag]
if !ok {
return nil, ConditionallyRequiredFieldMissing(tag)
}
return f[0].value, nil
}
// GetBool is a GetField wrapper for bool fields.
func (m FieldMap) GetBool(tag Tag) (bool, MessageRejectError) {
var val FIXBoolean
if err := m.GetField(tag, &val); err != nil {
return false, err
}
return bool(val), nil
}
// GetInt is a GetField wrapper for int fields.
func (m FieldMap) GetInt(tag Tag) (int, MessageRejectError) {
bytes, err := m.GetBytes(tag)
if err != nil {
return 0, err
}
var val FIXInt
if val.Read(bytes) != nil {
err = IncorrectDataFormatForValue(tag)
}
return int(val), err
}
// GetInt is a lock free GetField wrapper for int fields.
func (m FieldMap) getIntNoLock(tag Tag) (int, MessageRejectError) {
bytes, err := m.getBytesNoLock(tag)
if err != nil {
return 0, err
}
var val FIXInt
if val.Read(bytes) != nil {
err = IncorrectDataFormatForValue(tag)
}
return int(val), err
}
// GetTime is a GetField wrapper for utc timestamp fields.
func (m FieldMap) GetTime(tag Tag) (t time.Time, err MessageRejectError) {
m.rwLock.RLock()
defer m.rwLock.RUnlock()
bytes, err := m.GetBytes(tag)
if err != nil {
return
}
var val FIXUTCTimestamp
if val.Read(bytes) != nil {
err = IncorrectDataFormatForValue(tag)
}
return val.Time, err
}
// GetString is a GetField wrapper for string fields.
func (m FieldMap) GetString(tag Tag) (string, MessageRejectError) {
var val FIXString
if err := m.GetField(tag, &val); err != nil {
return "", err
}
return string(val), nil
}
// GetString is a GetField wrapper for string fields.
func (m FieldMap) getStringNoLock(tag Tag) (string, MessageRejectError) {
var val FIXString
if err := m.getFieldNoLock(tag, &val); err != nil {
return "", err
}
return string(val), nil
}
// GetGroup is a Get function specific to Group Fields.
func (m FieldMap) GetGroup(parser FieldGroupReader) MessageRejectError {
m.rwLock.RLock()
defer m.rwLock.RUnlock()
f, ok := m.tagLookup[parser.Tag()]
if !ok {
return ConditionallyRequiredFieldMissing(parser.Tag())
}
if _, err := parser.Read(f); err != nil {
if msgRejErr, ok := err.(MessageRejectError); ok {
return msgRejErr
}
return IncorrectDataFormatForValue(parser.Tag())
}
return nil
}
// SetField sets the field with Tag tag.
func (m *FieldMap) SetField(tag Tag, field FieldValueWriter) *FieldMap {
return m.SetBytes(tag, field.Write())
}
// SetBytes sets bytes.
func (m *FieldMap) SetBytes(tag Tag, value []byte) *FieldMap {
f := m.getOrCreate(tag)
initField(f, tag, value)
return m
}
// SetBool is a SetField wrapper for bool fields.
func (m *FieldMap) SetBool(tag Tag, value bool) *FieldMap {
return m.SetField(tag, FIXBoolean(value))
}
// SetInt is a SetField wrapper for int fields.
func (m *FieldMap) SetInt(tag Tag, value int) *FieldMap {
v := FIXInt(value)
return m.SetBytes(tag, v.Write())
}
// SetString is a SetField wrapper for string fields.
func (m *FieldMap) SetString(tag Tag, value string) *FieldMap {
return m.SetBytes(tag, []byte(value))
}
// Remove removes a tag from field map.
func (m *FieldMap) Remove(tag Tag) {
m.rwLock.Lock()
defer m.rwLock.Unlock()
delete(m.tagLookup, tag)
}
// Clear purges all fields from field map.
func (m *FieldMap) Clear() {
m.rwLock.Lock()
defer m.rwLock.Unlock()
m.tags = m.tags[0:0]
for k := range m.tagLookup {
delete(m.tagLookup, k)
}
}
func (m *FieldMap) clearNoLock() {
m.tags = m.tags[0:0]
for k := range m.tagLookup {
delete(m.tagLookup, k)
}
}
// CopyInto overwrites the given FieldMap with this one.
func (m *FieldMap) CopyInto(to *FieldMap) {
m.rwLock.RLock()
defer m.rwLock.RUnlock()
to.tagLookup = make(map[Tag]field)
for tag, f := range m.tagLookup {
clone := make(field, 1)
clone[0] = f[0]
to.tagLookup[tag] = clone
}
to.tags = make([]Tag, len(m.tags))
copy(to.tags, m.tags)
to.compare = m.compare
}
func (m *FieldMap) add(f field) {
t := fieldTag(f)
if _, ok := m.tagLookup[t]; !ok {
m.tags = append(m.tags, t)
}
m.tagLookup[t] = f
}
func (m *FieldMap) getOrCreate(tag Tag) field {
m.rwLock.Lock()
defer m.rwLock.Unlock()
if f, ok := m.tagLookup[tag]; ok {
f = f[:1]
return f
}
f := make(field, 1)
m.tagLookup[tag] = f
m.tags = append(m.tags, tag)
return f
}
// Set is a setter for fields.
func (m *FieldMap) Set(field FieldWriter) *FieldMap {
f := m.getOrCreate(field.Tag())
initField(f, field.Tag(), field.Write())
return m
}
// SetGroup is a setter specific to group fields.
func (m *FieldMap) SetGroup(field FieldGroupWriter) *FieldMap {
m.rwLock.Lock()
defer m.rwLock.Unlock()
_, ok := m.tagLookup[field.Tag()]
if !ok {
m.tags = append(m.tags, field.Tag())
}
m.tagLookup[field.Tag()] = field.Write()
return m
}
func (m *FieldMap) sortedTags() []Tag {
sort.Sort(m)
return m.tags
}
func (m FieldMap) write(buffer *bytes.Buffer) {
m.rwLock.Lock()
defer m.rwLock.Unlock()
for _, tag := range m.sortedTags() {
if f, ok := m.tagLookup[tag]; ok {
writeField(f, buffer)
}
}
}
func (m FieldMap) total() int {
m.rwLock.RLock()
defer m.rwLock.RUnlock()
total := 0
for _, fields := range m.tagLookup {
for _, tv := range fields {
switch tv.tag {
case tagCheckSum: // Tag does not contribute to total.
default:
total += tv.total()
}
}
}
return total
}
func (m FieldMap) length() int {
m.rwLock.RLock()
defer m.rwLock.RUnlock()
length := 0
for _, fields := range m.tagLookup {
for _, tv := range fields {
switch tv.tag {
case tagBeginString, tagBodyLength, tagCheckSum: // Tags do not contribute to length.
default:
length += tv.length()
}
}
}
return length
}