forked from vesoft-inc/nebula-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult_set.go
1310 lines (1172 loc) · 37.8 KB
/
result_set.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2020 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License,
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/
package nebula_go
import (
"bytes"
"encoding/json"
"fmt"
"sort"
"strings"
"time"
"github.com/vesoft-inc/nebula-go/v2/nebula"
"github.com/vesoft-inc/nebula-go/v2/nebula/graph"
)
type ResultSet struct {
resp *graph.ExecutionResponse
columnNames []string
colNameIndexMap map[string]int
timezoneInfo timezoneInfo
}
type Record struct {
columnNames *[]string
_record []*ValueWrapper
colNameIndexMap *map[string]int
timezoneInfo timezoneInfo
}
type Node struct {
vertex *nebula.Vertex
tags []string // tag name
tagNameIndexMap map[string]int
timezoneInfo timezoneInfo
}
type Relationship struct {
edge *nebula.Edge
timezoneInfo timezoneInfo
}
type segment struct {
startNode *Node
relationship *Relationship
endNode *Node
}
type PathWrapper struct {
path *nebula.Path
nodeList []*Node
relationshipList []*Relationship
segments []segment
timezoneInfo timezoneInfo
}
type TimeWrapper struct {
time *nebula.Time
timezoneInfo timezoneInfo
}
type DateWrapper struct {
date *nebula.Date
}
type DateTimeWrapper struct {
dateTime *nebula.DateTime
timezoneInfo timezoneInfo
}
type ErrorCode int64
const (
ErrorCode_SUCCEEDED ErrorCode = ErrorCode(nebula.ErrorCode_SUCCEEDED)
ErrorCode_E_DISCONNECTED ErrorCode = ErrorCode(nebula.ErrorCode_E_DISCONNECTED)
ErrorCode_E_FAIL_TO_CONNECT ErrorCode = ErrorCode(nebula.ErrorCode_E_FAIL_TO_CONNECT)
ErrorCode_E_RPC_FAILURE ErrorCode = ErrorCode(nebula.ErrorCode_E_RPC_FAILURE)
ErrorCode_E_BAD_USERNAME_PASSWORD ErrorCode = ErrorCode(nebula.ErrorCode_E_BAD_USERNAME_PASSWORD)
ErrorCode_E_SESSION_INVALID ErrorCode = ErrorCode(nebula.ErrorCode_E_SESSION_INVALID)
ErrorCode_E_SESSION_TIMEOUT ErrorCode = ErrorCode(nebula.ErrorCode_E_SESSION_TIMEOUT)
ErrorCode_E_SYNTAX_ERROR ErrorCode = ErrorCode(nebula.ErrorCode_E_SYNTAX_ERROR)
ErrorCode_E_EXECUTION_ERROR ErrorCode = ErrorCode(nebula.ErrorCode_E_EXECUTION_ERROR)
ErrorCode_E_STATEMENT_EMPTY ErrorCode = ErrorCode(nebula.ErrorCode_E_STATEMENT_EMPTY)
ErrorCode_E_USER_NOT_FOUND ErrorCode = ErrorCode(nebula.ErrorCode_E_USER_NOT_FOUND)
ErrorCode_E_BAD_PERMISSION ErrorCode = ErrorCode(nebula.ErrorCode_E_BAD_PERMISSION)
ErrorCode_E_SEMANTIC_ERROR ErrorCode = ErrorCode(nebula.ErrorCode_E_SEMANTIC_ERROR)
ErrorCode_E_PARTIAL_SUCCEEDED ErrorCode = ErrorCode(nebula.ErrorCode_E_PARTIAL_SUCCEEDED)
)
func genResultSet(resp *graph.ExecutionResponse, timezoneInfo timezoneInfo) (*ResultSet, error) {
var colNames []string
var colNameIndexMap = make(map[string]int)
if resp.Data == nil { // if resp.Data != nil then resp.Data.row and resp.Data.colNames wont be nil
return &ResultSet{
resp: resp,
columnNames: colNames,
colNameIndexMap: colNameIndexMap,
}, nil
}
for i, name := range resp.Data.ColumnNames {
colNames = append(colNames, string(name))
colNameIndexMap[string(name)] = i
}
return &ResultSet{
resp: resp,
columnNames: colNames,
colNameIndexMap: colNameIndexMap,
timezoneInfo: timezoneInfo,
}, nil
}
func genValWraps(row *nebula.Row, timezoneInfo timezoneInfo) ([]*ValueWrapper, error) {
if row == nil {
return nil, fmt.Errorf("failed to generate valueWrapper: invalid row")
}
var valWraps []*ValueWrapper
for _, val := range row.Values {
if val == nil {
return nil, fmt.Errorf("failed to generate valueWrapper: value is nil")
}
valWraps = append(valWraps, &ValueWrapper{val, timezoneInfo})
}
return valWraps, nil
}
func genNode(vertex *nebula.Vertex, timezoneInfo timezoneInfo) (*Node, error) {
if vertex == nil {
return nil, fmt.Errorf("failed to generate Node: invalid vertex")
}
var tags []string
nameIndex := make(map[string]int)
// Iterate through all tags of the vertex
for i, tag := range vertex.GetTags() {
name := string(tag.Name)
// Get tags
tags = append(tags, name)
nameIndex[name] = i
}
return &Node{
vertex: vertex,
tags: tags,
tagNameIndexMap: nameIndex,
timezoneInfo: timezoneInfo,
}, nil
}
func genRelationship(edge *nebula.Edge, timezoneInfo timezoneInfo) (*Relationship, error) {
if edge == nil {
return nil, fmt.Errorf("failed to generate Relationship: invalid edge")
}
return &Relationship{
edge: edge,
timezoneInfo: timezoneInfo,
}, nil
}
func genPathWrapper(path *nebula.Path, timezoneInfo timezoneInfo) (*PathWrapper, error) {
if path == nil {
return nil, fmt.Errorf("failed to generate Path Wrapper: invalid path")
}
var (
nodeList []*Node
relationshipList []*Relationship
segList []segment
edge *nebula.Edge
segStartNode *Node
segEndNode *Node
segType nebula.EdgeType
)
src, err := genNode(path.Src, timezoneInfo)
if err != nil {
return nil, err
}
nodeList = append(nodeList, src)
for _, step := range path.Steps {
dst, err := genNode(step.Dst, timezoneInfo)
if err != nil {
return nil, err
}
nodeList = append(nodeList, dst)
// determine direction
stepType := step.Type
if stepType > 0 {
segStartNode = src
segEndNode = dst
segType = stepType
} else {
segStartNode = dst // switch with src
segEndNode = src
segType = -stepType
}
edge = &nebula.Edge{
Src: segStartNode.getRawID(),
Dst: segEndNode.getRawID(),
Type: segType,
Name: step.Name,
Ranking: step.Ranking,
Props: step.Props,
}
relationship, err := genRelationship(edge, timezoneInfo)
if err != nil {
return nil, err
}
relationshipList = append(relationshipList, relationship)
// Check segments
if len(segList) > 0 {
prevStart := segList[len(segList)-1].startNode.GetID()
prevEnd := segList[len(segList)-1].endNode.GetID()
nextStart := segStartNode.GetID()
nextEnd := segEndNode.GetID()
if prevStart.String() != nextStart.String() && prevStart.String() != nextEnd.String() &&
prevEnd.String() != nextStart.String() && prevEnd.String() != nextEnd.String() {
return nil, fmt.Errorf("failed to generate PathWrapper, Path received is invalid")
}
}
segList = append(segList, segment{
startNode: segStartNode,
relationship: relationship,
endNode: segEndNode,
})
src = dst
}
return &PathWrapper{
path: path,
nodeList: nodeList,
relationshipList: relationshipList,
segments: segList,
}, nil
}
// Returns a 2D array of strings representing the query result
// If resultSet.resp.data is nil, returns an empty 2D array
func (res ResultSet) AsStringTable() [][]string {
var resTable [][]string
colNames := res.GetColNames()
resTable = append(resTable, colNames)
rows := res.GetRows()
for _, row := range rows {
var tempRow []string
for _, val := range row.Values {
tempRow = append(tempRow, ValueWrapper{val, res.timezoneInfo}.String())
}
resTable = append(resTable, tempRow)
}
return resTable
}
// Returns all values in the given column
func (res ResultSet) GetValuesByColName(colName string) ([]*ValueWrapper, error) {
if !res.hasColName(colName) {
return nil, fmt.Errorf("failed to get values, given column name '%s' does not exist", colName)
}
// Get index
index := res.colNameIndexMap[colName]
var valList []*ValueWrapper
for _, row := range res.resp.Data.Rows {
valList = append(valList, &ValueWrapper{row.Values[index], res.timezoneInfo})
}
return valList, nil
}
// Returns all values in the row at given index
func (res ResultSet) GetRowValuesByIndex(index int) (*Record, error) {
if err := checkIndex(index, res.resp.Data.Rows); err != nil {
return nil, err
}
valWrap, err := genValWraps(res.resp.Data.Rows[index], res.timezoneInfo)
if err != nil {
return nil, err
}
return &Record{
columnNames: &res.columnNames,
_record: valWrap,
colNameIndexMap: &res.colNameIndexMap,
timezoneInfo: res.timezoneInfo,
}, nil
}
// Returns the number of total rows
func (res ResultSet) GetRowSize() int {
if res.resp.Data == nil {
return 0
}
return len(res.resp.Data.Rows)
}
// Returns the number of total columns
func (res ResultSet) GetColSize() int {
if res.resp.Data == nil {
return 0
}
return len(res.resp.Data.ColumnNames)
}
// Returns all rows
func (res ResultSet) GetRows() []*nebula.Row {
if res.resp.Data == nil {
var empty []*nebula.Row
return empty
}
return res.resp.Data.Rows
}
func (res ResultSet) GetColNames() []string {
return res.columnNames
}
// Returns an integer representing an error type
// 0 ErrorCode_SUCCEEDED
// -1 ErrorCode_E_DISCONNECTED
// -2 ErrorCode_E_FAIL_TO_CONNECT
// -3 ErrorCode_E_RPC_FAILURE
// -4 ErrorCode_E_BAD_USERNAME_PASSWORD
// -5 ErrorCode_E_SESSION_INVALID
// -6 ErrorCode_E_SESSION_TIMEOUT
// -7 ErrorCode_E_SYNTAX_ERROR
// -8 ErrorCode_E_EXECUTION_ERROR
// -9 ErrorCode_E_STATEMENT_EMPTY
// -10 ErrorCode_E_USER_NOT_FOUND
// -11 ErrorCode_E_BAD_PERMISSION
// -12 ErrorCode_E_SEMANTIC_ERROR
func (res ResultSet) GetErrorCode() ErrorCode {
return ErrorCode(res.resp.ErrorCode)
}
func (res ResultSet) GetLatency() int32 {
return res.resp.LatencyInUs
}
func (res ResultSet) GetSpaceName() string {
if res.resp.SpaceName == nil {
return ""
}
return string(res.resp.SpaceName)
}
func (res ResultSet) GetErrorMsg() string {
if res.resp.ErrorMsg == nil {
return ""
}
return string(res.resp.ErrorMsg)
}
func (res ResultSet) IsSetPlanDesc() bool {
return res.resp.PlanDesc != nil
}
func (res ResultSet) GetPlanDesc() *graph.PlanDescription {
return res.resp.PlanDesc
}
func (res ResultSet) IsSetComment() bool {
return res.resp.Comment != nil
}
func (res ResultSet) GetComment() string {
if res.resp.Comment == nil {
return ""
}
return string(res.resp.Comment)
}
func (res ResultSet) IsSetData() bool {
return res.resp.Data != nil
}
func (res ResultSet) IsEmpty() bool {
if !res.IsSetData() || len(res.resp.Data.Rows) == 0 {
return true
}
return false
}
func (res ResultSet) IsSucceed() bool {
return res.GetErrorCode() == ErrorCode_SUCCEEDED
}
func (res ResultSet) IsPartialSucceed() bool {
return res.GetErrorCode() == ErrorCode_E_PARTIAL_SUCCEEDED
}
func (res ResultSet) hasColName(colName string) bool {
if _, ok := res.colNameIndexMap[colName]; ok {
return true
}
return false
}
// Returns value in the record at given column index
func (record Record) GetValueByIndex(index int) (*ValueWrapper, error) {
if err := checkIndex(index, record._record); err != nil {
return nil, err
}
return record._record[index], nil
}
// Returns value in the record at given column name
func (record Record) GetValueByColName(colName string) (*ValueWrapper, error) {
if !record.hasColName(colName) {
return nil, fmt.Errorf("failed to get values, given column name '%s' does not exist", colName)
}
// Get index
index := (*record.colNameIndexMap)[colName]
return record._record[index], nil
}
func (record Record) String() string {
var strList []string
for _, val := range record._record {
strList = append(strList, val.String())
}
return strings.Join(strList, ", ")
}
func (record Record) hasColName(colName string) bool {
if _, ok := (*record.colNameIndexMap)[colName]; ok {
return true
}
return false
}
// getRawID returns a list of row vid
func (node Node) getRawID() *nebula.Value {
return node.vertex.GetVid()
}
// GetID returns a list of vid of node
func (node Node) GetID() ValueWrapper {
return ValueWrapper{node.vertex.GetVid(), node.timezoneInfo}
}
// GetTags returns a list of tag names of node
func (node Node) GetTags() []string {
return node.tags
}
// HasTag checks if node contains given label
func (node Node) HasTag(label string) bool {
if _, ok := node.tagNameIndexMap[label]; ok {
return true
}
return false
}
// Properties returns all properties of a tag
func (node Node) Properties(tagName string) (map[string]*ValueWrapper, error) {
kvMap := make(map[string]*ValueWrapper)
// Check if label exists
if !node.HasTag(tagName) {
return nil, fmt.Errorf("failed to get properties: Tag name %s does not exsist in the Node", tagName)
}
index := node.tagNameIndexMap[tagName]
for k, v := range node.vertex.Tags[index].Props {
kvMap[k] = &ValueWrapper{v, node.timezoneInfo}
}
return kvMap, nil
}
// Keys returns all prop names of the given tag name
func (node Node) Keys(tagName string) ([]string, error) {
if !node.HasTag(tagName) {
return nil, fmt.Errorf("failed to get properties: Tag name %s does not exsist in the Node", tagName)
}
var propNameList []string
index := node.tagNameIndexMap[tagName]
for k := range node.vertex.Tags[index].Props {
propNameList = append(propNameList, k)
}
return propNameList, nil
}
// Values returns all prop values of the given tag name
func (node Node) Values(tagName string) ([]*ValueWrapper, error) {
if !node.HasTag(tagName) {
return nil, fmt.Errorf("failed to get properties: Tag name %s does not exsist in the Node", tagName)
}
var propValList []*ValueWrapper
index := node.tagNameIndexMap[tagName]
for _, v := range node.vertex.Tags[index].Props {
propValList = append(propValList, &ValueWrapper{v, node.timezoneInfo})
}
return propValList, nil
}
// String returns a string representing node
// Node format: ("VertexID" :tag1{k0: v0,k1: v1}:tag2{k2: v2})
func (node Node) String() string {
var keyList []string
var kvStr []string
var tagStr []string
vertex := node.vertex
vid := vertex.GetVid()
for _, tag := range vertex.GetTags() {
kvs := tag.GetProps()
tagName := tag.GetName()
for k := range kvs {
keyList = append(keyList, k)
}
sort.Strings(keyList)
for _, k := range keyList {
kvTemp := fmt.Sprintf("%s: %s", k, ValueWrapper{kvs[k], node.timezoneInfo}.String())
kvStr = append(kvStr, kvTemp)
}
tagStr = append(tagStr, fmt.Sprintf("%s{%s}", tagName, strings.Join(kvStr, ", ")))
keyList = nil
kvStr = nil
}
if len(tagStr) == 0 { // No tag
return fmt.Sprintf("(%s)", ValueWrapper{vid, node.timezoneInfo}.String())
}
return fmt.Sprintf("(%s :%s)", ValueWrapper{vid, node.timezoneInfo}.String(), strings.Join(tagStr, " :"))
}
// Returns true if two nodes have same vid
func (n1 Node) IsEqualTo(n2 *Node) bool {
if n1.GetID().IsString() && n2.GetID().IsString() {
s1, _ := n1.GetID().AsString()
s2, _ := n2.GetID().AsString()
return s1 == s2
} else if n1.GetID().IsInt() && n2.GetID().IsInt() {
s1, _ := n1.GetID().AsInt()
s2, _ := n2.GetID().AsInt()
return s1 == s2
}
return false
}
func (relationship Relationship) GetSrcVertexID() ValueWrapper {
if relationship.edge.Type > 0 {
return ValueWrapper{relationship.edge.GetSrc(), relationship.timezoneInfo}
}
return ValueWrapper{relationship.edge.GetDst(), relationship.timezoneInfo}
}
func (relationship Relationship) GetDstVertexID() ValueWrapper {
if relationship.edge.Type > 0 {
return ValueWrapper{relationship.edge.GetDst(), relationship.timezoneInfo}
}
return ValueWrapper{relationship.edge.GetSrc(), relationship.timezoneInfo}
}
func (relationship Relationship) GetEdgeName() string {
return string(relationship.edge.Name)
}
func (relationship Relationship) GetRanking() int64 {
return int64(relationship.edge.Ranking)
}
// Properties returns a map where the key is property name and the value is property name
func (relationship Relationship) Properties() map[string]*ValueWrapper {
kvMap := make(map[string]*ValueWrapper)
var (
keyList []string
valueList []*ValueWrapper
)
for k, v := range relationship.edge.Props {
keyList = append(keyList, k)
valueList = append(valueList, &ValueWrapper{v, relationship.timezoneInfo})
}
for i := 0; i < len(keyList); i++ {
kvMap[keyList[i]] = valueList[i]
}
return kvMap
}
// Keys returns a list of keys
func (relationship Relationship) Keys() []string {
var keys []string
for key := range relationship.edge.GetProps() {
keys = append(keys, key)
}
return keys
}
// Values returns a list of values wrapped as ValueWrappers
func (relationship Relationship) Values() []*ValueWrapper {
var values []*ValueWrapper
for _, value := range relationship.edge.GetProps() {
values = append(values, &ValueWrapper{value, relationship.timezoneInfo})
}
return values
}
// String returns a string representing relationship
// Relationship format: [:edge src->dst @ranking {props}]
func (relationship Relationship) String() string {
edge := relationship.edge
var keyList []string
var kvStr []string
var src string
var dst string
for k := range edge.Props {
keyList = append(keyList, k)
}
sort.Strings(keyList)
for _, k := range keyList {
kvTemp := fmt.Sprintf("%s: %s", k, ValueWrapper{edge.Props[k], relationship.timezoneInfo}.String())
kvStr = append(kvStr, kvTemp)
}
if relationship.edge.Type > 0 {
src = ValueWrapper{edge.Src, relationship.timezoneInfo}.String()
dst = ValueWrapper{edge.Dst, relationship.timezoneInfo}.String()
} else {
src = ValueWrapper{edge.Dst, relationship.timezoneInfo}.String()
dst = ValueWrapper{edge.Src, relationship.timezoneInfo}.String()
}
return fmt.Sprintf(`[:%s %s->%s @%d {%s}]`,
string(edge.Name), src, dst, edge.Ranking, fmt.Sprintf("%s", strings.Join(kvStr, ", ")))
}
func (r1 Relationship) IsEqualTo(r2 *Relationship) bool {
if r1.edge.GetSrc().IsSetSVal() && r2.edge.GetSrc().IsSetSVal() &&
r1.edge.GetDst().IsSetSVal() && r2.edge.GetDst().IsSetSVal() {
s1, _ := ValueWrapper{r1.edge.GetSrc(), r1.timezoneInfo}.AsString()
s2, _ := ValueWrapper{r2.edge.GetSrc(), r2.timezoneInfo}.AsString()
return s1 == s2 && string(r1.edge.Name) == string(r2.edge.Name) && r1.edge.Ranking == r2.edge.Ranking
} else if r1.edge.GetSrc().IsSetIVal() && r2.edge.GetSrc().IsSetIVal() &&
r1.edge.GetDst().IsSetIVal() && r2.edge.GetDst().IsSetIVal() {
s1, _ := ValueWrapper{r1.edge.GetSrc(), r1.timezoneInfo}.AsInt()
s2, _ := ValueWrapper{r2.edge.GetSrc(), r2.timezoneInfo}.AsInt()
return s1 == s2 && string(r1.edge.Name) == string(r2.edge.Name) && r1.edge.Ranking == r2.edge.Ranking
}
return false
}
func (path *PathWrapper) GetPathLength() int {
return len(path.segments)
}
func (path *PathWrapper) GetNodes() []*Node {
return path.nodeList
}
func (path *PathWrapper) GetRelationships() []*Relationship {
return path.relationshipList
}
func (path *PathWrapper) GetSegments() []segment {
return path.segments
}
func (path *PathWrapper) ContainsNode(node Node) bool {
for _, n := range path.nodeList {
if n.IsEqualTo(&node) {
return true
}
}
return false
}
func (path *PathWrapper) ContainsRelationship(relationship *Relationship) bool {
for _, r := range path.relationshipList {
if r.IsEqualTo(relationship) {
return true
}
}
return false
}
func (path *PathWrapper) GetStartNode() (*Node, error) {
if len(path.segments) == 0 {
return nil, fmt.Errorf("failed to get start node, no node in the path")
}
return path.segments[0].startNode, nil
}
func (path *PathWrapper) GetEndNode() (*Node, error) {
if len(path.segments) == 0 {
return nil, fmt.Errorf("failed to get start node, no node in the path")
}
return path.segments[len(path.segments)-1].endNode, nil
}
// Path format: <("VertexID" :tag1{k0: v0,k1: v1})
// -[:TypeName@ranking {edgeProps}]->
// ("VertexID2" :tag1{k0: v0,k1: v1} :tag2{k2: v2})
// -[:TypeName@ranking {edgeProps}]->
// ("VertexID3" :tag1{k0: v0,k1: v1})>
func (pathWrap *PathWrapper) String() string {
path := pathWrap.path
src := path.Src
steps := path.Steps
resStr := ValueWrapper{&nebula.Value{VVal: src}, pathWrap.timezoneInfo}.String()
for _, step := range steps {
var keyList []string
var kvStr []string
for k := range step.Props {
keyList = append(keyList, k)
}
sort.Strings(keyList)
for _, k := range keyList {
kvTemp := fmt.Sprintf("%s: %s", k, ValueWrapper{step.Props[k], pathWrap.timezoneInfo}.String())
kvStr = append(kvStr, kvTemp)
}
var dirChar1 string
var dirChar2 string
if step.Type > 0 {
dirChar1 = "-"
dirChar2 = "->"
} else {
dirChar1 = "<-"
dirChar2 = "-"
}
resStr = resStr + fmt.Sprintf("%s[:%s@%d {%s}]%s%s",
dirChar1,
string(step.Name),
step.Ranking,
fmt.Sprintf("%s", strings.Join(kvStr, ", ")),
dirChar2,
ValueWrapper{&nebula.Value{VVal: step.Dst}, pathWrap.timezoneInfo}.String())
}
return "<" + resStr + ">"
}
func (p1 *PathWrapper) IsEqualTo(p2 *PathWrapper) bool {
// Check length
if len(p1.nodeList) != len(p2.nodeList) || len(p1.relationshipList) != len(p2.relationshipList) ||
len(p1.segments) != len(p2.segments) {
return false
}
// Check nodes
for i := 0; i < len(p1.nodeList); i++ {
if !p1.nodeList[i].IsEqualTo(p2.nodeList[i]) {
return false
}
}
// Check relationships
for i := 0; i < len(p1.relationshipList); i++ {
if !p1.relationshipList[i].IsEqualTo(p2.relationshipList[i]) {
return false
}
}
// Check segments
for i := 0; i < len(p1.segments); i++ {
if !p1.segments[i].startNode.IsEqualTo(p2.segments[i].startNode) ||
!p1.segments[i].endNode.IsEqualTo(p2.segments[i].endNode) ||
!p1.segments[i].relationship.IsEqualTo(p2.segments[i].relationship) {
return false
}
}
return true
}
func genTimeWrapper(time *nebula.Time, timezoneInfo timezoneInfo) (*TimeWrapper, error) {
if time == nil {
return nil, fmt.Errorf("failed to generate Time: invalid Time")
}
return &TimeWrapper{
time: time,
timezoneInfo: timezoneInfo,
}, nil
}
// getHour returns the hour in UTC
func (t TimeWrapper) getHour() int8 {
return t.time.Hour
}
// getHour returns the minute in UTC
func (t TimeWrapper) getMinute() int8 {
return t.time.Minute
}
// getHour returns the second in UTC
func (t TimeWrapper) getSecond() int8 {
return t.time.Sec
}
func (t TimeWrapper) getMicrosec() int32 {
return t.time.Microsec
}
// getRawTime returns a nebula.Time object in UTC.
func (t TimeWrapper) getRawTime() *nebula.Time {
return t.time
}
// getLocalTime returns a nebula.Time object representing
// local time using timezone offset from the server.
func (t TimeWrapper) getLocalTime() (*nebula.Time, error) {
// Original time object generated from server in UTC
// Year, month and day are mocked up to fill the parameters
rawTime := time.Date(2020,
time.Month(1),
1,
int(t.getHour()),
int(t.getMinute()),
int(t.getSecond()),
int(t.getMicrosec()*1000),
time.UTC)
// Use offset in seconds
offset, err := time.ParseDuration(fmt.Sprintf("%ds", t.timezoneInfo.offset))
if err != nil {
return nil, err
}
localTime := rawTime.Add(offset)
return &nebula.Time{
Hour: int8(localTime.Hour()),
Minute: int8(localTime.Minute()),
Sec: int8(localTime.Second()),
Microsec: int32(localTime.Nanosecond() / 1000)}, nil
}
// getLocalTimeWithTimezoneOffset returns a nebula.Time object representing
// local time using user specified offset.
// Year, month, day in time.Time are filled with dummy values.
// Offset is in seconds.
func (t TimeWrapper) getLocalTimeWithTimezoneOffset(timezoneOffsetSeconds int32) (*nebula.Time, error) {
// Original time object generated from server in UTC
// Year, month and day are mocked up to fill the parameters
rawTime := time.Date(2020,
time.Month(1),
1,
int(t.getHour()),
int(t.getMinute()),
int(t.getSecond()),
int(t.getMicrosec()*1000),
time.UTC)
offset, err := time.ParseDuration(fmt.Sprintf("%ds", timezoneOffsetSeconds))
if err != nil {
return nil, err
}
localTime := rawTime.Add(offset)
return &nebula.Time{
Hour: int8(localTime.Hour()),
Minute: int8(localTime.Minute()),
Sec: int8(localTime.Second()),
Microsec: int32(localTime.Nanosecond() / 1000)}, nil
}
// getLocalTimeWithTimezoneName returns a nebula.Time object
// representing local time using user specified timezone name.
// Year, month, day in time.Time are filled with 0.
//
// If the name is "" or "UTC", LoadLocation returns UTC.
// If the name is "Local", LoadLocation returns Local.
//
// Otherwise, the name is taken to be a location name corresponding to a file
// in the IANA Time Zone database, such as "America/New_York".
func (t TimeWrapper) getLocalTimeWithTimezoneName(timezoneName string) (*nebula.Time, error) {
// Original time object generated from server in UTC
// Year, month and day are mocked up to fill the parameters
rawTime := time.Date(2020,
time.Month(1),
1,
int(t.getHour()),
int(t.getMinute()),
int(t.getSecond()),
int(t.getMicrosec()*1000),
time.UTC)
location, err := time.LoadLocation(timezoneName)
if err != nil {
return nil, err
}
localTime := rawTime.In(location)
return &nebula.Time{
Hour: int8(localTime.Hour()),
Minute: int8(localTime.Minute()),
Sec: int8(localTime.Second()),
Microsec: int32(localTime.Nanosecond() / 1000)}, nil
}
func (t1 TimeWrapper) IsEqualTo(t2 TimeWrapper) bool {
return t1.getHour() == t2.getHour() &&
t1.getSecond() == t2.getSecond() &&
t1.getSecond() == t2.getSecond() &&
t1.getMicrosec() == t2.getMicrosec()
}
func genDateWrapper(date *nebula.Date) (*DateWrapper, error) {
if date == nil {
return nil, fmt.Errorf("failed to generate datetime: invalid datetime")
}
return &DateWrapper{
date: date,
}, nil
}
func (d DateWrapper) getYear() int16 {
return d.date.Year
}
func (d DateWrapper) getMonth() int8 {
return d.date.Month
}
func (d DateWrapper) getDay() int8 {
return d.date.Day
}
// getRawDate returns a nebula.Date object in UTC.
func (d DateWrapper) getRawDate() *nebula.Date {
return d.date
}
func (d1 DateWrapper) IsEqualTo(d2 DateWrapper) bool {
return d1.getYear() == d2.getYear() &&
d1.getMonth() == d2.getMonth() &&
d1.getDay() == d2.getDay()
}
func genDateTimeWrapper(datetime *nebula.DateTime, timezoneInfo timezoneInfo) (*DateTimeWrapper, error) {
if datetime == nil {
return nil, fmt.Errorf("failed to generate datetime: invalid datetime")
}
return &DateTimeWrapper{
dateTime: datetime,
timezoneInfo: timezoneInfo,
}, nil
}
func (dt DateTimeWrapper) getYear() int16 {
return dt.dateTime.Year
}
func (dt DateTimeWrapper) getMonth() int8 {
return dt.dateTime.Month
}
func (dt DateTimeWrapper) getDay() int8 {
return dt.dateTime.Day
}
func (dt DateTimeWrapper) getHour() int8 {
return dt.dateTime.Hour
}
func (dt DateTimeWrapper) getMinute() int8 {
return dt.dateTime.Minute
}
func (dt DateTimeWrapper) getSecond() int8 {
return dt.dateTime.Sec
}
func (dt DateTimeWrapper) getMicrosec() int32 {
return dt.dateTime.Microsec
}
func (dt1 DateTimeWrapper) IsEqualTo(dt2 DateTimeWrapper) bool {
return dt1.getYear() == dt2.getYear() &&
dt1.getMonth() == dt2.getMonth() &&
dt1.getDay() == dt2.getDay() &&
dt1.getHour() == dt2.getHour() &&
dt1.getSecond() == dt2.getSecond() &&
dt1.getSecond() == dt2.getSecond() &&
dt1.getMicrosec() == dt2.getMicrosec()
}
// getRawDateTime returns a nebula.DateTime object representing local dateTime in UTC.
func (dt DateTimeWrapper) getRawDateTime() *nebula.DateTime {
return dt.dateTime
}
// getLocalDateTime returns a nebula.DateTime object representing
// local datetime using timezone offset from the server.
func (dt DateTimeWrapper) getLocalDateTime() (*nebula.DateTime, error) {
// Original time object generated from server in UTC
rawTime := time.Date(
int(dt.getYear()), time.Month(dt.getMonth()), int(dt.getDay()),
int(dt.getHour()), int(dt.getMinute()), int(dt.getSecond()), int(dt.dateTime.Microsec*1000),
time.UTC)
// Use offset in seconds
offset, err := time.ParseDuration(fmt.Sprintf("%ds", dt.timezoneInfo.offset))
if err != nil {
return nil, err
}
localDT := rawTime.Add(offset)
return &nebula.DateTime{
Year: int16(localDT.Year()),
Month: int8(localDT.Month()),
Day: int8(localDT.Day()),
Hour: int8(localDT.Hour()),
Minute: int8(localDT.Minute()),
Sec: int8(localDT.Second()),
Microsec: int32(localDT.Nanosecond() / 1000)}, nil
}
// getLocalDateTimeWithTimezoneOffset returns a nebula.DateTime object representing
// local datetime using user specified timezone offset.
// Offset is in seconds.
func (dt DateTimeWrapper) getLocalDateTimeWithTimezoneOffset(timezoneOffsetSeconds int32) (*nebula.DateTime, error) {
// Original time object generated from server in UTC