-
Notifications
You must be signed in to change notification settings - Fork 66
/
request.go
159 lines (121 loc) · 3.46 KB
/
request.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
package tarantool
import(
"github.com/vmihailenco/msgpack"
"errors"
)
type Request struct {
conn *Connection
requestId uint32
requestCode int32
body map[int]interface{}
}
func (conn *Connection) NewRequest(requestCode int32) (req *Request) {
req = &Request{}
req.conn = conn
req.requestId = conn.nextRequestId()
req.requestCode = requestCode
req.body = make(map[int]interface{})
return
}
func (conn *Connection) Ping() (resp *Response, err error) {
request := conn.NewRequest(PingRequest)
resp, err = request.perform()
return
}
func (conn *Connection) Select(spaceNo, indexNo, offset, limit, iterator uint32, key []interface{}) (resp *Response, err error) {
request := conn.NewRequest(SelectRequest)
request.body[KeySpaceNo] = spaceNo
request.body[KeyIndexNo] = indexNo
request.body[KeyIterator] = iterator
request.body[KeyOffset] = offset
request.body[KeyLimit] = limit
request.body[KeyKey] = key
resp, err = request.perform()
return
}
func (conn *Connection) Insert(spaceNo uint32, tuple []interface{}) (resp *Response, err error) {
request := conn.NewRequest(InsertRequest)
request.body[KeySpaceNo] = spaceNo
request.body[KeyTuple] = tuple
resp, err = request.perform()
return
}
func (conn *Connection) Replace(spaceNo uint32, tuple []interface{}) (resp *Response, err error) {
request := conn.NewRequest(ReplaceRequest)
request.body[KeySpaceNo] = spaceNo
request.body[KeyTuple] = tuple
resp, err = request.perform()
return
}
func (conn *Connection) Delete(spaceNo, indexNo uint32, key []interface{}) (resp *Response, err error) {
request := conn.NewRequest(DeleteRequest)
request.body[KeySpaceNo] = spaceNo
request.body[KeyIndexNo] = indexNo
request.body[KeyKey] = key
resp, err = request.perform()
return
}
func (conn *Connection) Update(spaceNo, indexNo uint32, key, tuple []interface{}) (resp *Response, err error) {
request := conn.NewRequest(UpdateRequest)
request.body[KeySpaceNo] = spaceNo
request.body[KeyIndexNo] = indexNo
request.body[KeyKey] = key
request.body[KeyTuple] = tuple
resp, err = request.perform()
return
}
func (conn *Connection) Call(functionName string, tuple []interface{}) (resp *Response, err error) {
request := conn.NewRequest(CallRequest)
request.body[KeyFunctionName] = functionName
request.body[KeyTuple] = tuple
resp, err = request.perform()
return
}
//
// To be implemented
//
func (conn *Connection) Auth(key, tuple []interface{}) (resp *Response, err error) {
return
}
//
// private
//
func (req *Request) perform() (resp *Response, err error) {
packet, err := req.pack()
if err != nil {
return
}
responseChan := make(chan *Response)
req.conn.mutex.Lock()
req.conn.requests[req.requestId] = responseChan
req.conn.mutex.Unlock()
req.conn.packets <- (packet)
resp = <-responseChan
if resp.Error != "" {
err = errors.New(resp.Error)
}
return
}
func (req *Request) pack() (packet []byte, err error) {
var header, body, packetLength []byte
msg_header := make(map[int]interface{})
msg_header[KeyCode] = req.requestCode
msg_header[KeySync] = req.requestId
header, err = msgpack.Marshal(msg_header)
if err != nil {
return
}
body, err = msgpack.Marshal(req.body)
if err != nil {
return
}
length := uint32(len(header) + len(body))
packetLength, err = msgpack.Marshal(length)
if err != nil {
return
}
packet = append(packet, packetLength...)
packet = append(packet, header...)
packet = append(packet, body...)
return
}