forked from schwartzmx/gremtune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
183 lines (150 loc) · 4.93 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package gremtune
import (
"encoding/base64"
"encoding/json"
"github.com/gofrs/uuid"
)
type requester interface {
prepare() error
getID() string
getRequest() request
}
// request is a container for all evaluation request parameters to be sent to the Gremlin Server.
type request struct {
RequestID string `json:"requestId"`
Op string `json:"op"`
Processor string `json:"processor"`
Args map[string]interface{} `json:"args"`
}
// prepareRequest packages a query and binding into the format that Gremlin Server accepts
func prepareRequest(query string) (req request, id string, err error) {
var uuID uuid.UUID
uuID, _ = uuid.NewV4()
id = uuID.String()
req.RequestID = id
req.Op = "eval"
req.Processor = ""
req.Args = make(map[string]interface{})
req.Args["language"] = "gremlin-groovy"
req.Args["gremlin"] = query
return
}
// prepareRequest packages a query and binding into the format that Gremlin Server accepts
func prepareRequestWithBindings(query string, bindings, rebindings map[string]string) (req request, id string, err error) {
var uuID uuid.UUID
uuID, _ = uuid.NewV4()
id = uuID.String()
req.RequestID = id
req.Op = "eval"
req.Processor = ""
req.Args = make(map[string]interface{})
req.Args["language"] = "gremlin-groovy"
req.Args["gremlin"] = query
req.Args["bindings"] = bindings
req.Args["rebindings"] = rebindings
return
}
// prepareRequestWithSession packages a query and sessionID into the format that Gremlin Server accepts
func prepareRequestWithSession(query string, sessionID string) (req request, id string, err error) {
if len(sessionID) > 0 {
var uuID uuid.UUID
uuID, _ = uuid.NewV4()
id = uuID.String()
req.RequestID = id
req.Op = "eval"
req.Processor = "session"
req.Args = make(map[string]interface{})
req.Args["language"] = "gremlin-groovy"
req.Args["gremlin"] = query
req.Args["manageTransaction"] = false
req.Args["session"] = sessionID
req.Args["batchSize"] = 64
} else {
req, id, err = prepareRequest(query)
}
return
}
// prepareRequestWithSessionAndTimeout packages a query and sessionID into the format that Gremlin Server accepts
func prepareRequestWithSessionAndTimeout(query string, sessionID string, timeout int) (req request, id string, err error) {
if len(sessionID) > 0 && timeout > 0 {
var uuID uuid.UUID
uuID, _ = uuid.NewV4()
id = uuID.String()
req.RequestID = id
req.Op = "eval"
req.Processor = "session"
req.Args = make(map[string]interface{})
req.Args["language"] = "gremlin-groovy"
req.Args["gremlin"] = query
req.Args["manageTransaction"] = false
req.Args["session"] = sessionID
req.Args["batchSize"] = 64
req.Args["scriptEvaluationTimeout"] = timeout
} else if len(sessionID) <= 0 && timeout > 0 {
var uuID uuid.UUID
uuID, _ = uuid.NewV4()
id = uuID.String()
req.RequestID = id
req.Op = "eval"
req.Processor = ""
req.Args = make(map[string]interface{})
req.Args["language"] = "gremlin-groovy"
req.Args["gremlin"] = query
req.Args["scriptEvaluationTimeout"] = timeout
} else if len(sessionID) > 0 && timeout <= 0 {
req, id, err = prepareRequestWithSession(query, sessionID)
} else {
req, id, err = prepareRequest(query)
}
return
}
// prepareRequest packages a query and binding into the format that Gremlin Server accepts
func prepareCommitSessionRequest(sessionID string) (req request, id string, err error) {
if len(sessionID) > 0 {
var uuID uuid.UUID
uuID, _ = uuid.NewV4()
id = uuID.String()
req.RequestID = id
req.Op = "close"
req.Processor = "session"
req.Args = make(map[string]interface{})
req.Args["language"] = "gremlin-groovy"
req.Args["manageTransaction"] = false
req.Args["session"] = sessionID
req.Args["force"] = false
} else {
req, id, err = prepareRequest("")
}
return
}
//prepareAuthRequest creates a ws request for Gremlin Server
func prepareAuthRequest(requestID string, username string, password string) (req request, err error) {
req.RequestID = requestID
req.Op = "authentication"
req.Processor = "trasversal"
var simpleAuth []byte
user := []byte(username)
pass := []byte(password)
simpleAuth = append(simpleAuth, 0)
simpleAuth = append(simpleAuth, user...)
simpleAuth = append(simpleAuth, 0)
simpleAuth = append(simpleAuth, pass...)
req.Args = make(map[string]interface{})
req.Args["sasl"] = base64.StdEncoding.EncodeToString(simpleAuth)
return
}
// formatMessage takes a request type and formats it into being able to be delivered to Gremlin Server
func packageRequest(req request) (msg []byte, err error) {
j, err := json.Marshal(req) // Formats request into byte format
if err != nil {
return
}
mimeType := []byte("application/vnd.gremlin-v3.0+json")
msg = append([]byte{0x21}, mimeType...) //0x21 is the fixed length of mimeType in hex
msg = append(msg, j...)
return
}
// dispactchRequest sends the request for writing to the remote Gremlin Server
func (c *Client) dispatchRequest(msg []byte) {
c.requests <- msg
}