-
Notifications
You must be signed in to change notification settings - Fork 9
/
node.go
144 lines (119 loc) · 3.75 KB
/
node.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
package neo4j
import (
"encoding/json"
"errors"
"fmt"
)
// Node struct
type Node struct {
ID string
Data map[string]interface{}
Payload *NodeResponse
}
// NodeResponse struct for mapping nodes returned for Neo4J server
type NodeResponse struct {
PagedTraverse string `json:"paged_traverse"`
OutgoingRelationships string `json:"outgoing_relationships"`
Traverse string `json:"traverse"`
AllTypedRelationships string `json:"all_typed_relationships"`
Property string `json:"property"`
AllRelationships string `json:"all_relationships"`
Self string `json:"self"`
Properties string `json:"properties"`
OutgoingTypedRelationships string `json:"outgoing_typed_relationships"`
IncomingRelationships string `json:"incoming_relationships"`
IncomingTypedRelationships string `json:"incoming_typed_relationships"`
CreateRelationship string `json:"create_relationship"`
Data map[string]interface{} `json:"data"`
}
func (node *Node) mapBatchResponse(neo4j *Neo4j, data interface{}) (bool, error) {
encodedData, err := jsonEncode(data)
payload, err := node.decodeResponse(encodedData)
if err != nil {
return false, err
}
id, err := getIDFromURL(neo4j.NodeURL, payload.Self)
if err != nil {
return false, nil
}
node.ID = id
node.Data = payload.Data
node.Payload = payload
return true, nil
}
func (node *Node) getBatchQuery(operation string) (map[string]interface{}, error) {
switch operation {
case BatchGet:
query, err := prepareNodeGetBatchMap(node)
return query, err
case BatchUpdate:
query, err := prepareNodeUpdateBatchMap(node)
return query, err
case BatchCreate:
query, err := prepareNodeCreateBatchMap(node)
return query, err
case BatchDelete:
query, err := prepareNodeDeleteBatchMap(node)
return query, err
case BatchCreateUnique:
query, err := prepareNodeCreateUniqueBatchMap(node)
return query, err
}
return map[string]interface{}{}, nil
}
func prepareNodeGetBatchMap(n *Node) (map[string]interface{}, error) {
query := make(map[string]interface{})
if n.ID == "" {
return query, errors.New("Id field is empty")
}
query["method"] = "GET"
query["to"] = fmt.Sprintf("/node/%s", n.ID)
return query, nil
}
func prepareNodeDeleteBatchMap(n *Node) (map[string]interface{}, error) {
query := make(map[string]interface{})
if n.ID == "" {
return query, errors.New("Id not valid")
}
query["method"] = "DELETE"
query["to"] = fmt.Sprintf("/node/%s", n.ID)
return query, nil
}
func prepareNodeCreateBatchMap(n *Node) (map[string]interface{}, error) {
return map[string]interface{}{
"method": "POST",
"to": "/node",
"body": n.Data,
}, nil
}
func prepareNodeUpdateBatchMap(n *Node) (map[string]interface{}, error) {
query := make(map[string]interface{})
if n.ID == "" {
return query, errors.New("Id not valid")
}
query["method"] = "PUT"
query["to"] = fmt.Sprintf("/node/%s/properties", n.ID)
query["body"] = n.Data
return query, nil
}
func prepareNodeCreateUniqueBatchMap(n *Node) (map[string]interface{}, error) {
return map[string]interface{}{
"method": "POST",
"to": "/index/node",
"body": map[string]interface{}{
"properties": n.Data,
},
}, nil
}
func (node *Node) encodeData() (string, error) {
result, err := jsonEncode(node.Data)
return result, err
}
func (node *Node) decodeResponse(data string) (*NodeResponse, error) {
nodeResponse := &NodeResponse{}
err := json.Unmarshal([]byte(data), nodeResponse)
if err != nil {
return nil, err
}
return nodeResponse, nil
}