forked from qasaur/gremgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
421 lines (372 loc) · 13.3 KB
/
client.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
415
416
417
418
419
420
421
package gremgo
import (
"errors"
"fmt"
"io/ioutil"
"log"
"reflect"
"sync"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/google/uuid"
)
// Client is a container for the gremgo client.
type Client struct {
conn dialer
requests chan []byte
responses chan []byte
results *sync.Map
responseNotifier *sync.Map // responseNotifier notifies the requester that a response has arrived for the request
respMutex *sync.Mutex
Errored bool
}
// NewDialer returns a WebSocket dialer to use when connecting to Gremlin Server
func NewDialer(host string, configs ...DialerConfig) (dialer *Ws) {
dialer = &Ws{
timeout: 5 * time.Second,
pingInterval: 60 * time.Second,
writingWait: 15 * time.Second,
readingWait: 15 * time.Second,
connected: false,
quit: make(chan struct{}),
}
for _, conf := range configs {
conf(dialer)
}
dialer.host = host
return dialer
}
func newClient() (c Client) {
c.requests = make(chan []byte, 3) // c.requests takes any request and delivers it to the WriteWorker for dispatch to Gremlin Server
c.responses = make(chan []byte, 3) // c.responses takes raw responses from ReadWorker and delivers it for sorting to handelResponse
c.results = &sync.Map{}
c.responseNotifier = &sync.Map{}
c.respMutex = &sync.Mutex{} // c.mutex ensures that sorting is thread safe
return
}
// Dial returns a gremgo client for interaction with the Gremlin Server specified in the host IP.
func Dial(conn dialer, errs chan error) (c Client, err error) {
c = newClient()
c.conn = conn
// Connects to Gremlin Server
err = conn.connect()
if err != nil {
return
}
quit := conn.(*Ws).quit
go c.writeWorker(errs, quit)
go c.readWorker(errs, quit)
go conn.ping(errs)
return
}
func (c *Client) executeRequest(query string, bindings, rebindings map[string]string) (resp interface{}, err error) {
req, id, err := prepareRequest(query, bindings, rebindings)
if err != nil {
return
}
msg, err := packageRequest(req)
if err != nil {
log.Println(err)
return
}
c.responseNotifier.Store(id, make(chan int, 1))
c.dispatchRequest(msg)
resp = c.retrieveResponse(id)
return
}
func (c *Client) authenticate(requestId string) (err error) {
auth := c.conn.getAuth()
req, err := prepareAuthRequest(requestId, auth.username, auth.password)
if err != nil {
return
}
msg, err := packageRequest(req)
if err != nil {
log.Println(err)
return
}
c.dispatchRequest(msg)
return
}
// Execute formats a raw Gremlin query, sends it to Gremlin Server, and returns the result.
func (c *Client) Execute(query string, bindings, rebindings map[string]string) (resp interface{}, err error) {
if c.conn.isDisposed() {
return nil, errors.New("you cannot write on a disposed connection")
}
resp, err = c.executeRequest(query, bindings, rebindings)
return
}
// Get formats a raw Gremlin query, sends it to Gremlin Server, and populates the passed []interface.
func (c *Client) Get(query string, ptr interface{}) (err error) {
if c.conn.isDisposed() {
return errors.New("you cannot write on a disposed connection")
}
var strct reflect.Value
// check if it's a a pointer
if reflect.TypeOf(ptr) == reflect.TypeOf(reflect.Value{}) {
strct = ptr.(reflect.Value)
} else {
strct = reflect.ValueOf(ptr).Elem()
}
if strct.Kind() != reflect.Slice {
return errors.New("the passed interface is not a slice")
}
resp, err := c.executeRequest(query, nil, nil)
if err != nil {
return err
}
// get the underlying struct type
sType := reflect.TypeOf(strct.Interface()).Elem()
// test if we can cast resp as slice of interface
if respSlice, ok := resp.([]interface{}); ok {
// iterate of the interface in the slice
for _, item := range respSlice {
// create new slice to later copy back
lenRespSlice := len(respSlice)
sSlice := reflect.MakeSlice(reflect.SliceOf(sType), lenRespSlice, lenRespSlice+1)
// test if we can cast resp as slice of interface
if respSliceSlice, ok := item.([]interface{}); ok {
// iterate over the inner slice
for j, innerItem := range respSliceSlice {
// check if we can cast as a map
if respMap, ok := innerItem.(map[string]interface{}); ok {
spew.Dump(respMap)
// create a new struct to populate
s := reflect.New(sType)
// check for Id field
ok := s.Elem().FieldByName("Id").IsValid()
if !ok {
return errors.New("the passed interface must have an Id field")
}
uuidType := s.Elem().FieldByName("Id").Kind()
// iterate over fields and populate
for i := 0; i < s.Elem().NumField(); i++ {
// get graph tag for field
tag := sType.Field(i).Tag.Get("graph")
name, opts := parseTag(tag)
if len(name) == 0 && len(opts) == 0 {
continue
}
// get the current field
f := s.Elem().Field(i)
// check if we can modify
if f.CanSet() {
if f.Kind() == uuidType { // if its the Id field we look in the base response map
// create a UUID
id, err := uuid.Parse(respMap[name].(string))
if err != nil {
return err
}
f.Set(reflect.ValueOf(id))
} else { // it is a property and we have to looks at the properties map
props, ok := respMap["properties"].(map[string]interface{})
if ok {
// check if the key is in the map
if _, ok := props[name]; ok {
// get the properties slice
propSlice := reflect.ValueOf(props[name])
propSliceLen := propSlice.Len()
// check the length, if its 1 we set it as a single value otherwise we need to create a slice
if propSliceLen == 1 {
// get the value of the property we are looking for
v, err := getPropertyValue(propSlice.Index(0).Interface())
if err != nil {
return err
}
if f.Kind() == reflect.String { // Set as string
_v, ok := v.(string)
if ok {
f.SetString(_v)
}
} else if f.Kind() == reflect.Int || f.Kind() == reflect.Int8 || f.Kind() == reflect.Int16 || f.Kind() == reflect.Int32 || f.Kind() == reflect.Int64 { // Set as int
_v, ok := v.(float64)
__v := int64(_v)
if ok {
if !f.OverflowInt(__v) {
f.SetInt(__v)
}
}
} else if f.Kind() == reflect.Float32 || f.Kind() == reflect.Float64 { // Set as float
_v, ok := v.(float64)
if ok {
if !f.OverflowFloat(_v) {
f.SetFloat(_v)
}
}
} else if f.Kind() == reflect.Uint || f.Kind() == reflect.Uint8 || f.Kind() == reflect.Uint16 || f.Kind() == reflect.Uint32 || f.Kind() == reflect.Uint64 { // Set as uint
_v, ok := v.(float64)
__v := uint64(_v)
if ok {
if !f.OverflowUint(__v) {
f.SetUint(__v)
}
}
} else if f.Kind() == reflect.Bool { // Set as bool
_v, ok := v.(bool)
if ok {
f.SetBool(_v)
}
}
} else if propSliceLen > 1 { // we need to creates slices for the properties
pSlice := reflect.MakeSlice(reflect.SliceOf(f.Type().Elem()), propSliceLen, propSliceLen+1)
// now we iterate over the properties
for i := 0; i < propSliceLen; i++ {
// get the value of the property we are looking for
v, err := getPropertyValue(propSlice.Index(i).Interface())
if err != nil {
return err
}
if f.Type().Elem().Kind() == reflect.String { // Set as string
_v, ok := v.(string)
if ok {
pSlice.Index(i).SetString(_v)
}
} else if f.Type().Elem().Kind() == reflect.Int || f.Type().Elem().Kind() == reflect.Int8 || f.Type().Elem().Kind() == reflect.Int16 || f.Type().Elem().Kind() == reflect.Int32 || f.Type().Elem().Kind() == reflect.Int64 { // Set as int
_v, ok := v.(float64)
__v := int64(_v)
if ok {
if !pSlice.Index(i).OverflowInt(__v) {
pSlice.Index(i).SetInt(__v)
}
}
} else if f.Type().Elem().Kind() == reflect.Float32 || f.Type().Elem().Kind() == reflect.Float64 { // Set as float
_v, ok := v.(float64)
if ok {
if !pSlice.Index(i).OverflowFloat(_v) {
pSlice.Index(i).SetFloat(_v)
}
}
} else if f.Type().Elem().Kind() == reflect.Uint || f.Type().Elem().Kind() == reflect.Uint8 || f.Type().Elem().Kind() == reflect.Uint16 || f.Type().Elem().Kind() == reflect.Uint32 || f.Type().Elem().Kind() == reflect.Uint64 { // Set as uint
_v, ok := v.(float64)
__v := uint64(_v)
if ok {
if !pSlice.Index(i).OverflowUint(__v) {
pSlice.Index(i).SetUint(__v)
}
}
} else if f.Type().Elem().Kind() == reflect.Bool { // Set as bool
_v, ok := v.(bool)
if ok {
pSlice.Index(i).SetBool(_v)
}
}
}
// set the field to the created slice
f.Set(pSlice)
}
}
}
}
}
}
// update slice
sSlice.Index(j).Set(s.Elem())
}
}
// Copy the new slice to the passed data slice
strct.Set(sSlice)
}
}
}
return
}
// getProprtyValue takes a property map slice and return the value
func getPropertyValue(prop interface{}) (interface{}, error) {
propMap, ok := prop.(map[string]interface{})
if ok {
return propMap["value"], nil
}
return nil, errors.New("passed property cannot be cast")
}
// ExecuteFile takes a file path to a Gremlin script, sends it to Gremlin Server, and returns the result.
func (c *Client) ExecuteFile(path string, bindings, rebindings map[string]string) (resp interface{}, err error) {
if c.conn.isDisposed() {
return nil, errors.New("you cannot write on a disposed connection")
}
d, err := ioutil.ReadFile(path) // Read script from file
if err != nil {
log.Println(err)
return
}
query := string(d)
resp, err = c.executeRequest(query, bindings, rebindings)
return
}
// Close closes the underlying connection and marks the client as closed.
func (c *Client) Close() {
if c.conn != nil {
c.conn.close()
}
}
// AddV takes a label and a interface and adds it a vertex to the graph
func (c *Client) AddV(label string, data interface{}) (resp interface{}, err error) {
if c.conn.isDisposed() {
return nil, errors.New("you cannot write on a disposed connection")
}
d := reflect.ValueOf(data)
id := d.FieldByName("Id")
if !id.IsValid() {
return nil, errors.New("the passed interface must have an Id field")
}
q := fmt.Sprintf("g.addV('%s')", label)
tagLength := 0
for i := 0; i < d.NumField(); i++ {
tag := d.Type().Field(i).Tag.Get("graph")
name, opts := parseTag(tag)
if len(name) == 0 && len(opts) == 0 {
continue
}
tagLength++
val := d.Field(i).Interface()
if len(opts) == 0 {
return nil, fmt.Errorf("interface field tag does not contain a tag option type, field type: %T", val)
} else if opts.Contains("string") {
q = fmt.Sprintf("%s.property('%s', '%s')", q, name, val)
} else if opts.Contains("bool") || opts.Contains("number") || opts.Contains("other") {
q = fmt.Sprintf("%s.property('%s', %v)", q, name, val)
} else if opts.Contains("[]string") {
s := reflect.ValueOf(val)
for i := 0; i < s.Len(); i++ {
q = fmt.Sprintf("%s.property('%s', '%s')", q, name, s.Index(i).Interface())
}
} else if opts.Contains("[]bool") || opts.Contains("[]number") || opts.Contains("[]other") {
s := reflect.ValueOf(val)
for i := 0; i < s.Len(); i++ {
q = fmt.Sprintf("%s.property('%s', %v)", q, name, s.Index(i).Interface())
}
}
}
if tagLength == 0 {
return nil, fmt.Errorf("interface of type: %T, does not contain any graph tags", data)
}
resp, err = c.Execute(q, nil, nil)
return
}
// AddE takes a label, from UUID and to UUID then creates a edge between the two vertex in the graph
func (c *Client) AddE(label string, from, to interface{}) (resp interface{}, err error) {
if c.conn.isDisposed() {
return nil, errors.New("you cannot write on a disposed connection")
}
df := reflect.ValueOf(from)
fid := df.FieldByName("Id")
if !fid.IsValid() {
return nil, errors.New("the passed from interface must have an Id field")
}
dt := reflect.ValueOf(to)
tid := dt.FieldByName("Id")
if !tid.IsValid() {
return nil, errors.New("the passed to interface must have an Id field")
}
q := fmt.Sprintf("g.V('%s').addE('%s').to(g.V('%s'))", fid.Interface().(uuid.UUID).String(), label, tid.Interface().(uuid.UUID).String())
resp, err = c.Execute(q, map[string]string{}, map[string]string{})
return
}
// AddEById takes a label, from UUID and to UUID then creates a edge between the two vertex in the graph
func (c *Client) AddEById(label string, from, to uuid.UUID) (resp interface{}, err error) {
if c.conn.isDisposed() {
return nil, errors.New("you cannot write on a disposed connection")
}
q := fmt.Sprintf("g.V('%s').addE('%s').to(g.V('%s'))", from.String(), label, to.String())
resp, err = c.Execute(q, map[string]string{}, map[string]string{})
return
}