-
Notifications
You must be signed in to change notification settings - Fork 9
/
batch.go
277 lines (226 loc) · 6.8 KB
/
batch.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
package neo4j
import (
"encoding/json"
"errors"
"reflect"
"strconv"
)
// Batcher is the interface for structs for making them compatible with Batch.
type Batcher interface {
getBatchQuery(operation string) (map[string]interface{}, error)
mapBatchResponse(neo4j *Neo4j, data interface{}) (bool, error)
}
// Basic operation names
var (
BatchGet = "get"
BatchCreate = "create"
BatchDelete = "delete"
BatchUpdate = "update"
BatchCreateUnique = "createUnique"
)
// Batch Base struct to support request
type Batch struct {
Neo4j *Neo4j
Stack []*BatchRequest
}
// BatchRequest All batch request structs will be encapslated in this struct
type BatchRequest struct {
Operation string
Data Batcher
}
// BatchResponse All returning results from Neo4j will be in BatchResponse format
type BatchResponse struct {
ID int `json:"id"`
Location string `json:"location"`
Body interface{} `json:"body"`
From string `json:"from"`
}
// ManuelBatchRequest is here to support referance passing requests in a transaction
// For more information please check : http://docs.neo4j.org/chunked/stable/rest-api-batch-ops.html
type ManuelBatchRequest struct {
To string
Body map[string]interface{}
StringBody string
Response interface{}
}
// GetManualBatchResponse Implements Batcher interface
func (neo4j *Neo4j) GetManualBatchResponse(mbr *ManuelBatchRequest, result interface{}) error {
//get type of current value
typeOfResult := reflect.TypeOf(result).String()
typeOfResponse := reflect.TypeOf(mbr.Response).String()
switch typeOfResult {
//if we have an complex type, resolve it
case "*[]neo4j.Node":
if typeOfResponse != "[]interface {}" {
return errors.New("Response is not an array")
}
tempResult := make([]Node, len(mbr.Response.([]interface{})))
result = result.(*[]Node)
arrayResult := mbr.Response.([]interface{})
for i, value := range arrayResult {
tempResult[i].mapBatchResponse(neo4j, value)
}
(*result.(*[]Node)) = tempResult
case "*neo4j.Node":
_, err := result.(*Node).mapBatchResponse(neo4j, mbr.Response)
if err != nil {
return err
}
case "*neo4j.Relationship":
_, err := result.(*Relationship).mapBatchResponse(neo4j, mbr.Response)
if err != nil {
return err
}
case "*[]neo4j.Relationship":
if typeOfResponse != "[]interface {}" {
return errors.New("Response is not an array")
}
tempResult := make([]Relationship, len(mbr.Response.([]interface{})))
result = result.(*[]Relationship)
arrayResult := mbr.Response.([]interface{})
for i, value := range arrayResult {
tempResult[i].mapBatchResponse(neo4j, value)
}
(*result.(*[]Relationship)) = tempResult
}
return nil
}
// Implement Batcher interface
func (mbr *ManuelBatchRequest) getBatchQuery(operation string) (map[string]interface{}, error) {
query := make(map[string]interface{})
query["to"] = mbr.To
if mbr.StringBody != "" && (len(mbr.StringBody) > 0) {
query["body"] = mbr.StringBody
} else {
query["body"] = mbr.Body
}
switch operation {
case BatchGet:
query["method"] = "GET"
case BatchUpdate:
query["method"] = "PUT"
case BatchCreate:
query["method"] = "POST"
case BatchDelete:
query["method"] = "DELETE"
case BatchCreateUnique:
query["method"] = "POST"
query["body"] = map[string]interface{}{
"properties": mbr.Body,
}
}
return query, nil
}
func (mbr *ManuelBatchRequest) mapBatchResponse(neo4j *Neo4j, data interface{}) (bool, error) {
mbr.Response = data
return true, nil
}
// GetLastIndex Returns last index of current stack
// This method can be used to obtain the latest index for creating
// manual batch requests or injecting the order number of pre-added request(s) id
func (batch *Batch) GetLastIndex() string {
return strconv.Itoa(len(batch.Stack) - 1)
}
// NewBatch Creates New Batch request handler
func (neo4j *Neo4j) NewBatch() *Batch {
return &Batch{
Neo4j: neo4j,
Stack: make([]*BatchRequest, 0),
}
}
// Get request to Neo4j as batch
func (batch *Batch) Get(obj Batcher) *Batch {
batch.addToStack(BatchGet, obj)
return batch
}
// Create request to Neo4j as batch
func (batch *Batch) Create(obj Batcher) *Batch {
batch.addToStack(BatchCreate, obj)
return batch
}
// Delete request to Neo4j as batch
func (batch *Batch) Delete(obj Batcher) *Batch {
batch.addToStack(BatchDelete, obj)
return batch
}
// Update request to Neo4j as batch
func (batch *Batch) Update(obj Batcher) *Batch {
batch.addToStack(BatchUpdate, obj)
return batch
}
// CreateUnique Batch unique create request to Neo4j
func (batch *Batch) CreateUnique(obj Batcher, properties *Unique) *Batch {
//encapsulating the object
uniqueRequest := &UniqueRequest{
Properties: properties,
Data: obj,
}
batch.addToStack(BatchCreateUnique, uniqueRequest)
return batch
}
// Adds requests to stack
// Used internally to pile up the batch request
func (batch *Batch) addToStack(operation string, obj Batcher) {
batchRequest := &BatchRequest{
Operation: operation,
Data: obj,
}
batch.Stack = append(batch.Stack, batchRequest)
}
// Execute Prepares and sends the request to Neo4j
// If the request is successful then parses the response
func (batch *Batch) Execute() ([]*BatchResponse, error) {
// if Neo4j instance is not created return an error
if batch.Neo4j == nil {
return nil, errors.New("Batch request is not created by NewBatch method!")
}
// cache batch stack length
stackLength := len(batch.Stack)
//create result array
response := make([]*BatchResponse, stackLength)
if stackLength == 0 {
return response, nil
}
// prepare request
request, err := prepareRequest(batch.Stack)
if err != nil {
return nil, err
}
encodedRequest, err := jsonEncode(request)
res, err := batch.Neo4j.doBatchRequest("POST", batch.Neo4j.BatchURL, encodedRequest)
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(res), &response)
if err != nil {
return nil, err
}
// do mapping here for later usage
batch.mapResponse(response)
// do a clean
batch.Stack = make([]*BatchRequest, 0)
return response, nil
}
// prepares batch request as slice of map
func prepareRequest(stack []*BatchRequest) ([]map[string]interface{}, error) {
request := make([]map[string]interface{}, len(stack))
for i, value := range stack {
// interface has this method getBatchQuery()
query, err := value.Data.getBatchQuery(value.Operation)
if err != nil {
return nil, err
}
query["id"] = i
request[i] = query
}
return request, nil
}
// map incoming response, it will update request's nodes and relationships
func (batch *Batch) mapResponse(response []*BatchResponse) {
for _, val := range response {
// id is an Neo4j batch request feature, it returns back the id that we send
// so we can use it here to map results into our stack
id := val.ID
batch.Stack[id].Data.mapBatchResponse(batch.Neo4j, val.Body)
}
}