-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_manager_graph_edge.go
196 lines (174 loc) · 8.24 KB
/
client_manager_graph_edge.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
package goC8
import (
"github.com/marvin-hansen/goC8/requests/collection_req"
"github.com/marvin-hansen/goC8/requests/graph_req"
"github.com/marvin-hansen/goC8/requests/graph_req/edge_req"
"github.com/marvin-hansen/goC8/types"
"github.com/marvin-hansen/goC8/utils"
"strings"
"time"
)
// GetEdge
// Gets an edge from the given collection.
// https://macrometa.com/docs/api#/operations/GetAnEdge
func (c GraphManager) GetEdge(fabric, graphName, collectionName, edgeKey string) (response *graph_req.ResponseForEdge, err error) {
if benchmark {
defer utils.TimeTrack(time.Now(), "GetEdge")
}
req := edge_req.NewRequestForGetEdge(fabric, graphName, collectionName, edgeKey)
response = graph_req.NewResponseForEdge()
if err = c.client.Request(req, response); err != nil {
return nil, err
}
return response, nil
}
// GetAllEdges
// Lists all edge collections within this graph.
// https://macrometa.com/docs/api#/operations/ListEdgedefinitions
func (c GraphManager) GetAllEdges(fabric, graphName string) (response *edge_req.ResponseForGetAllEdges, err error) {
if benchmark {
defer utils.TimeTrack(time.Now(), "GetAllEdges")
}
req := edge_req.NewRequestForGetAllEdges(fabric, graphName)
response = edge_req.NewResponseForGetAllEdges()
if err = c.client.Request(req, response); err != nil {
return nil, err
}
return response, nil
}
// GetGetAllInOutEdges
// Returns an array of edges starting or ending in the vertex identified by vertex.
// direction - Select in or out direction for edges. If ANY is set, all edges are returned.
func (c GraphManager) GetGetAllInOutEdges(fabric, edgeCollectionName, vertexKey string, direction types.EdgeDirection) (response *edge_req.ResponseForGetAllInOutEdges, err error) {
if benchmark {
defer utils.TimeTrack(time.Now(), "GetGetAllInOutEdges")
}
req := edge_req.NewRequestForGetAllInOutEdges(fabric, edgeCollectionName, vertexKey, direction)
response = edge_req.NewResponseForGetAllInOutEdges()
if err = c.client.Request(req, response); err != nil {
return nil, err
}
return response, nil
}
// CheckEdgeCollectionExists
// Returns true if the collection of the name exists. False otherwise.
func (c GraphManager) CheckEdgeCollectionExists(fabric, collectionName string) (exists bool, err error) {
req := collection_req.NewRequestForGetCollectionInfo(fabric, collectionName)
response := collection_req.NewResponseForGetCollectionInfo()
if err = c.client.Request(req, response); err != nil {
if strings.Contains(err.Error(), "1203") { // Number=1203, Error Message=collection or view not found
return false, nil
} else {
return false, err // Any other error
}
}
return true, nil
}
// AddEdgeCollection
// Adds an additional edge definition to the graph. This edge definition has to contain a collection and an array of each from and to vertex collections.
// An edge definition can only be added if this definition is either not used in any other graph, or it is used with exactly the same definition.
// * edgeCollectionName: The name of the edge collection to be used.
// * sourceVertex (string): One or many vertex collections that can contain source vertices.
// * destinationVertex (string): One or many vertex collections that can contain target vertices.
// https://macrometa.com/docs/api#/operations/AddEdgedefinition
func (c GraphManager) AddEdgeCollection(fabric, graphName, edgeCollectionName, sourceVertex, destinationVertex string) (response *edge_req.ResponseForAddEdgeCollection, err error) {
if benchmark {
defer utils.TimeTrack(time.Now(), "AddEdgeCollection")
}
req := edge_req.NewRequestForAddEdgeCollection(fabric, graphName, edgeCollectionName, sourceVertex, destinationVertex)
response = edge_req.NewResponseForAddEdgeCollection()
if err = c.client.Request(req, response); err != nil {
return nil, err
}
return response, nil
}
// CreateEdge
// Creates a new edge in the collection
// There are two ways to call CreateEdge()
// A) Custom edge
// With a custom Json payload, as shown in the example
// By providing from / to, which in the example are left as empty strings. When using this, jsonPayload must be nil.
// Using a custom payload allows for custom attributes i.e. "online" and custom primary keys set in the _key field.
// B) Default edge
//Just setting from/to fields means no custom attributes are possible and primary keys will be generated by the system.
//The second scenario helps with bulk inserts of basic relations in which the key isn't of interest.
// sourceVertex: The source vertex of this edge. Has to be valid within the used edge definition.
// destinationVertex: The target vertex of this edge. Has to be valid within the used edge definition.
func (c GraphManager) CreateEdge(fabric, graphName, edgeCollectionName, sourceVertex, destinationVertex string, jsonPayload []byte, returnNew bool) (response *edge_req.ResponseForCreateEdge, err error) {
if benchmark {
defer utils.TimeTrack(time.Now(), "CreateEdge")
}
req := edge_req.NewRequestForCreateEdge(fabric, graphName, edgeCollectionName, sourceVertex, destinationVertex, jsonPayload, returnNew)
response = edge_req.NewResponseForCreateEdge()
if err = c.client.Request(req, response); err != nil {
return nil, err
}
return response, nil
}
// CheckEdgeExists
// returns true if the edge exists in the given collection
func (c GraphManager) CheckEdgeExists(fabric, graphName, collectionName, edgeKey string) (exists bool, err error) {
if benchmark {
defer utils.TimeTrack(time.Now(), "CheckEdgeExists")
}
req := edge_req.NewRequestForGetEdge(fabric, graphName, collectionName, edgeKey)
response := graph_req.NewResponseForEdge()
if err = c.client.Request(req, response); err != nil {
if strings.Contains(err.Error(), "1202") { // Code=404, Number=1202, Error Message=document not found
return false, nil
} else {
return false, err // Any other error
}
}
return true, nil
}
// ReplaceEdge
// Replaces the data of an edge in the collection.
// https://macrometa.com/docs/api#/operations/ReplaceAnEdge
func (c GraphManager) ReplaceEdge(fabric, graphName, edgeCollectionName, edgeKey string, jsonReplace []byte, returnOld, returnNew bool) (response *edge_req.ResponseForReplaceEdge, err error) {
if benchmark {
defer utils.TimeTrack(time.Now(), "ReplaceEdge")
}
req := edge_req.NewRequestForReplaceEdge(fabric, graphName, edgeCollectionName, edgeKey, jsonReplace, true, returnOld, returnNew)
response = edge_req.NewResponseForReplaceEdge()
if err = c.client.Request(req, response); err != nil {
return nil, err
}
return response, nil
}
func (c GraphManager) UpdateEdge(fabric, graphName, edgeCollectionName, edgeKey string, jsonUpdate []byte, keepNull, returnOld, returnNew bool) (response *graph_req.ResponseForEdge, err error) {
if benchmark {
defer utils.TimeTrack(time.Now(), "UpdateEdge")
}
req := edge_req.NewRequestForUpdateEdge(fabric, graphName, edgeCollectionName, edgeKey, jsonUpdate, keepNull, returnOld, returnNew)
response = graph_req.NewResponseForEdge()
if err = c.client.Request(req, response); err != nil {
return nil, err
}
return response, nil
}
// DeleteEdge
// Removes an edge from the collection.
// https://macrometa.com/docs/api#/operations/RemoveAnEdge
func (c GraphManager) DeleteEdge(fabric, graphName, collectionName, edgeKey string, returnOld bool) (response *graph_req.ResponseForEdge, err error) {
if benchmark {
defer utils.TimeTrack(time.Now(), "DeleteEdge")
}
req := edge_req.NewRequestForDeleteEdge(fabric, graphName, collectionName, edgeKey, returnOld)
response = graph_req.NewResponseForEdge()
err = c.client.Request(req, response)
return response, CheckReturnError(err)
}
// DeleteEdgeCollection
// Remove one edge definition from the graph.
// This only removes the edge collection, the vertex collections remain untouched and can still be used in your queries.
// https://macrometa.com/docs/api#/operations/RemoveAnEdgedefinitionFromTheGraph
func (c GraphManager) DeleteEdgeCollection(fabric, graphName, collectionName string, dropCollections bool) (response *graph_req.ResponseForGraph, err error) {
if benchmark {
defer utils.TimeTrack(time.Now(), "DeleteEdgeCollection")
}
req := edge_req.NewRequestForDeleteEdgeCollection(fabric, graphName, collectionName, dropCollections)
response = graph_req.NewResponseForGraph()
err = c.client.Request(req, response)
return response, CheckReturnError(err)
}