-
Notifications
You must be signed in to change notification settings - Fork 9
/
relationship.go
282 lines (226 loc) · 7.27 KB
/
relationship.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
package neo4j
import (
"encoding/json"
"errors"
"fmt"
)
// Relationship struct
type Relationship struct {
ID string
StartNodeID string
EndNodeID string
Type string
Data map[string]interface{}
Payload *RelationshipResponse
}
// RelationshipResponse struct returned from Neo4J server
type RelationshipResponse struct {
Start string `json:"start"`
Property string `json:"property"`
Self string `json:"self"`
Properties string `json:"properties"`
Type string `json:"type"`
End string `json:"end"`
Data map[string]interface{} `json:"data"`
}
// GetRelationshipTypes queries Neo4J for all relationships
func (neo4j *Neo4j) GetRelationshipTypes() ([]string, error) {
url := fmt.Sprintf("%s/types", neo4j.RelationshipURL)
var result = make([]string, 0)
response, err := neo4j.doRequest("GET", url, "")
if err != nil {
return result, err
}
err = json.Unmarshal([]byte(response), &result)
if err != nil {
return result, err
}
return result, err
}
func (r *Relationship) mapBatchResponse(neo4j *Neo4j, data interface{}) (bool, error) {
// because data is a map, convert back to Json
encodedData, err := jsonEncode(data)
result, err := r.decode(neo4j, encodedData)
return result, err
}
func (r *Relationship) getBatchQuery(operation string) (map[string]interface{}, error) {
query := make(map[string]interface{})
switch operation {
case BatchGet:
query, err := prepareRelationshipGetBatchMap(r)
return query, err
case BatchUpdate:
query, err := prepareRelationshipUpdateBatchMap(r)
return query, err
case BatchCreate:
query, err := prepareRelationshipCreateBatchMap(r)
return query, err
case BatchDelete:
query, err := prepareRelationshipDeleteBatchMap(r)
return query, err
case BatchCreateUnique:
query, err := prepareRelationshipCreateUniqueBatchMap(r)
return query, err
}
return query, nil
}
func prepareRelationshipGetBatchMap(r *Relationship) (map[string]interface{}, error) {
query := make(map[string]interface{})
if r.ID == "" {
return query, errors.New("Id not valid")
}
query["method"] = "GET"
query["to"] = fmt.Sprintf("/relationship/%s", r.ID)
return query, nil
}
func prepareRelationshipDeleteBatchMap(r *Relationship) (map[string]interface{}, error) {
query := make(map[string]interface{})
if r.ID == "" {
return query, errors.New("Id not valid")
}
query["method"] = "DELETE"
query["to"] = fmt.Sprintf("/relationship/%s", r.ID)
return query, nil
}
func prepareRelationshipCreateBatchMap(r *Relationship) (map[string]interface{}, error) {
query := make(map[string]interface{})
if r.StartNodeID == "" {
return query, errors.New("Start Node Id not valid")
}
if r.EndNodeID == "" {
return query, errors.New("End Node Id not valid")
}
if r.Type == "" {
return query, errors.New("Relationship type is not valid")
}
url := fmt.Sprintf("/node/%s/relationships", r.StartNodeID)
endNodeURL := fmt.Sprintf("/node/%s", r.EndNodeID)
return map[string]interface{}{
"method": "POST",
"to": url,
"body": map[string]interface{}{
"to": endNodeURL,
"type": r.Type,
"data": r.Data,
},
}, nil
}
func prepareRelationshipCreateUniqueBatchMap(r *Relationship) (map[string]interface{}, error) {
query := make(map[string]interface{})
if r.StartNodeID == "" {
return query, errors.New("Start Node Id not valid")
}
if r.EndNodeID == "" {
return query, errors.New("End Node Id not valid")
}
if r.Type == "" {
return query, errors.New("Relationship type is not valid")
}
startURL := fmt.Sprintf("/node/%s", r.StartNodeID)
endNodeURL := fmt.Sprintf("/node/%s", r.EndNodeID)
return map[string]interface{}{
"method": "POST",
"to": "/index/relationship",
"body": map[string]interface{}{
"start": startURL,
"end": endNodeURL,
"type": r.Type,
"properties": r.Data,
},
}, nil
}
func prepareRelationshipUpdateBatchMap(r *Relationship) (map[string]interface{}, error) {
query := make(map[string]interface{})
if r.ID == "" {
return query, errors.New("Id not valid")
}
query = map[string]interface{}{
"method": "PUT",
"to": fmt.Sprintf("/relationship/%s/properties", r.ID),
"body": r.Data,
}
return query, nil
}
// GetOutgoingRelationships queries outgoing relationships for a node
func (neo4j *Neo4j) GetOutgoingRelationships(node *Node) ([]Relationship, error) {
res, err := getRelationships(neo4j, node, "out")
return res, err
}
// GetAllRelationships queries all relationships for a node
func (neo4j *Neo4j) GetAllRelationships(node *Node) ([]Relationship, error) {
res, err := getRelationships(neo4j, node, "all")
return res, err
}
// GetIncomingRelationships queries incoming realtionships for a node
func (neo4j *Neo4j) GetIncomingRelationships(node *Node) ([]Relationship, error) {
res, err := getRelationships(neo4j, node, "in")
return res, err
}
// GetOutgoingTypedRelationships queries TYPED outgoing relationships for a node
func (neo4j *Neo4j) GetOutgoingTypedRelationships(node *Node, relType string) ([]Relationship, error) {
res, err := getRelationships(neo4j, node, fmt.Sprintf("out/%s", relType))
return res, err
}
// GetAllTypedRelationships queries all TYPED relationships for a node
func (neo4j *Neo4j) GetAllTypedRelationships(node *Node, relType string) ([]Relationship, error) {
res, err := getRelationships(neo4j, node, fmt.Sprintf("all/%s", relType))
return res, err
}
// GetIncomingTypedRelationships queries TYPED incoming realtionships for a node
func (neo4j *Neo4j) GetIncomingTypedRelationships(node *Node, relType string) ([]Relationship, error) {
res, err := getRelationships(neo4j, node, fmt.Sprintf("in/%s", relType))
return res, err
}
func getRelationships(neo4j *Neo4j, node *Node, direction string) ([]Relationship, error) {
if node.ID == "" {
return nil, errors.New("Id is not given")
}
customReq := &ManuelBatchRequest{}
customReq.To = fmt.Sprintf("/node/%s/relationships/%s", node.ID, direction)
neo4j.NewBatch().Get(customReq).Execute()
result := []Relationship{}
err := neo4j.GetManualBatchResponse(customReq, &result)
if err != nil {
return nil, err
}
return result, nil
}
func (r *Relationship) encodeData() (string, error) {
result, err := jsonEncode(r.Data)
return result, err
}
func (r *Relationship) decode(neo4j *Neo4j, data string) (bool, error) {
payload := &RelationshipResponse{}
// Map json to our RelationshipResponse struct
err := json.Unmarshal([]byte(data), payload)
if err != nil {
return false, err
}
// Map returning result to our relationship struct
err = mapRelationship(neo4j, r, payload)
if err != nil {
return false, err
}
return true, nil
}
func mapRelationship(neo4j *Neo4j, relationship *Relationship, payload *RelationshipResponse) error {
relationshipID, err := getIDFromURL(neo4j.RelationshipURL, payload.Self)
if err != nil {
return err
}
startNodeID, err := getIDFromURL(neo4j.NodeURL, payload.Start)
if err != nil {
return err
}
endNodeID, err := getIDFromURL(neo4j.NodeURL, payload.End)
if err != nil {
return err
}
relationship.ID = relationshipID
relationship.StartNodeID = startNodeID
relationship.EndNodeID = endNodeID
relationship.Type = payload.Type
relationship.Data = payload.Data
relationship.Payload = payload
return nil
}