forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.go
123 lines (100 loc) · 2.4 KB
/
commands.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
package memcache
// Generic memcache command types and helper functions for defining
// binary/text protocol based commands with setters and serializers.
import (
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/streambuf"
)
type commandType struct {
typ commandTypeCode
code commandCode
parse parserStateFn
event eventFn
}
type eventFn func(msg *message, event common.MapStr) error
type argDef struct {
parse argParser
serialize eventFn
}
func argOptional(arg argDef) argDef {
parse := func(parser *parser, hdr, buf *streambuf.Buffer) error {
err := arg.parse(parser, hdr, buf)
if err == errNoMoreArgument {
return nil
}
if err != nil {
debug("optional err: %s", err)
}
return err
}
return argDef{
parse: parse,
serialize: arg.serialize,
}
}
func setValue(msg *message, v uint64) {
msg.value = v
}
func setValue2(msg *message, v uint64) {
msg.value2 = v
}
func setFlags(msg *message, flags uint32) {
msg.flags = flags
}
func setExpTime(msg *message, exptime uint32) {
msg.exptime = exptime
}
func setCasUnique(msg *message, cas uint64) {
msg.isCas = true
msg.casUnique = cas
}
func setByteCount(msg *message, count uint32) {
msg.bytes = uint(count)
}
func serializeNop(msg *message, event common.MapStr) error {
return nil
}
func serializeArgs(msg *message, event common.MapStr, args []argDef) error {
for _, arg := range args {
if err := arg.serialize(msg, event); err != nil {
return err
}
}
return nil
}
func serializeValue(name string) eventFn {
return func(msg *message, event common.MapStr) error {
event[name] = msg.value
return nil
}
}
func serializeValue2(name string) eventFn {
return func(msg *message, event common.MapStr) error {
event[name] = msg.value2
return nil
}
}
func serializeFlags(msg *message, event common.MapStr) error {
event["flags"] = msg.flags
return nil
}
func serializeKeys(msg *message, event common.MapStr) error {
event["keys"] = msg.keys
return nil
}
func serializeExpTime(msg *message, event common.MapStr) error {
event["exptime"] = msg.exptime
return nil
}
func serializeByteCount(msg *message, event common.MapStr) error {
event["bytes"] = msg.bytes
return nil
}
func serializeStats(msg *message, event common.MapStr) error {
event["stats"] = msg.stats
return nil
}
func serializeCas(msg *message, event common.MapStr) error {
event["cas_unique"] = msg.casUnique
return nil
}