forked from reo7sp/go-tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operator.go
211 lines (183 loc) · 4.42 KB
/
operator.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
package tarantool
import (
"fmt"
"github.com/tinylib/msgp/msgp"
)
type Operator interface {
AsTuple() []interface{}
}
type OpAdd struct {
Field int64
Argument int64
}
type OpSub struct {
Field int64
Argument int64
}
type OpBitAND struct {
Field int64
Argument uint64
}
type OpBitXOR struct {
Field int64
Argument uint64
}
type OpBitOR struct {
Field int64
Argument uint64
}
type OpDelete struct {
From int64
Count uint64
}
type OpInsert struct {
Before int64
Argument interface{}
}
type OpAssign struct {
Field int64
Argument interface{}
}
type OpSplice struct {
Field int64
Offset uint64
Position uint64
Argument string
}
func (op *OpAdd) AsTuple() []interface{} {
return []interface{}{"+", op.Field, op.Argument}
}
func (op *OpSub) AsTuple() []interface{} {
return []interface{}{"-", op.Field, op.Argument}
}
func (op *OpBitAND) AsTuple() []interface{} {
return []interface{}{"&", op.Field, op.Argument}
}
func (op *OpBitXOR) AsTuple() []interface{} {
return []interface{}{"^", op.Field, op.Argument}
}
func (op *OpBitOR) AsTuple() []interface{} {
return []interface{}{"|", op.Field, op.Argument}
}
func (op *OpDelete) AsTuple() []interface{} {
return []interface{}{"#", op.From, op.Count}
}
func (op *OpInsert) AsTuple() []interface{} {
return []interface{}{"!", op.Before, op.Argument}
}
func (op *OpAssign) AsTuple() []interface{} {
return []interface{}{"=", op.Field, op.Argument}
}
func (op *OpSplice) AsTuple() []interface{} {
return []interface{}{":", op.Field, op.Position, op.Offset, op.Argument}
}
func marshalOperator(op Operator, buf []byte) ([]byte, error) {
return msgp.AppendIntf(buf, op.AsTuple())
}
func unmarshalOperator(data []byte) (op Operator, buf []byte, err error) {
buf = data
var n uint32
if n, buf, err = msgp.ReadArrayHeaderBytes(buf); err != nil {
return
}
var str string
if str, buf, err = msgp.ReadStringBytes(buf); err != nil {
return
}
var field0 int64
if field0, buf, err = msgp.ReadInt64Bytes(buf); err != nil {
return
}
switch str {
case "+":
if n != 3 {
return nil, buf, fmt.Errorf("unexpected number of arguments in OpAdd: %d", n)
}
opAdd := &OpAdd{Field: field0}
if opAdd.Argument, buf, err = msgp.ReadInt64Bytes(buf); err != nil {
return
}
op = opAdd
case "-":
if n != 3 {
return nil, buf, fmt.Errorf("unexpected number of arguments in OpSub: %d", n)
}
opSub := &OpSub{Field: field0}
if opSub.Argument, buf, err = msgp.ReadInt64Bytes(buf); err != nil {
return
}
op = opSub
case "&":
if n != 3 {
return nil, buf, fmt.Errorf("unexpected number of arguments in OpBitAND: %d", n)
}
opAnd := &OpBitAND{Field: field0}
if opAnd.Argument, buf, err = msgp.ReadUint64Bytes(buf); err != nil {
return
}
op = opAnd
case "^":
if n != 3 {
return nil, buf, fmt.Errorf("unexpected number of arguments in OpBitXOR: %d", n)
}
opXOR := &OpBitXOR{Field: field0}
if opXOR.Argument, buf, err = msgp.ReadUint64Bytes(buf); err != nil {
return
}
op = opXOR
case "|":
if n != 3 {
return nil, buf, fmt.Errorf("unexpected number of arguments in OpBitOR: %d", n)
}
opOR := &OpBitOR{Field: field0}
if opOR.Argument, buf, err = msgp.ReadUint64Bytes(buf); err != nil {
return
}
op = opOR
case "#":
if n != 3 {
return nil, buf, fmt.Errorf("unexpected number of arguments in OpDelete: %d", n)
}
opDel := &OpDelete{From: field0}
if opDel.Count, buf, err = msgp.ReadUint64Bytes(buf); err != nil {
return
}
op = opDel
case "!":
if n != 3 {
return nil, buf, fmt.Errorf("unexpected number of arguments in OpInsert: %d", n)
}
opIns := &OpInsert{Before: field0}
if opIns.Argument, buf, err = msgp.ReadIntfBytes(buf); err != nil {
return
}
op = opIns
case "=":
if n != 3 {
return nil, buf, fmt.Errorf("unexpected number of arguments in OpAssign: %d", n)
}
opAss := &OpAssign{Field: field0}
if opAss.Argument, buf, err = msgp.ReadIntfBytes(buf); err != nil {
return
}
op = opAss
case ":":
if n != 5 {
return nil, buf, fmt.Errorf("unexpected number of arguments in OpSplice: %d", n)
}
opSpl := &OpSplice{Field: field0}
if opSpl.Position, buf, err = msgp.ReadUint64Bytes(buf); err != nil {
return
}
if opSpl.Offset, buf, err = msgp.ReadUint64Bytes(buf); err != nil {
return
}
if opSpl.Argument, buf, err = msgp.ReadStringBytes(buf); err != nil {
return
}
op = opSpl
default:
return nil, buf, fmt.Errorf("uknown op %s", str)
}
return
}