-
Notifications
You must be signed in to change notification settings - Fork 79
/
edge_collection_documents_impl.go
591 lines (571 loc) · 20.7 KB
/
edge_collection_documents_impl.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
//
// DISCLAIMER
//
// Copyright 2017-2023 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
package driver
import (
"context"
"fmt"
"path"
"reflect"
"strings"
)
// DocumentExists checks if a document with given key exists in the collection.
func (c *edgeCollection) DocumentExists(ctx context.Context, key string) (bool, error) {
if result, err := c.rawCollection().DocumentExists(ctx, key); err != nil {
return false, WithStack(err)
} else {
return result, nil
}
}
// ReadDocument reads a single document with given key from the collection.
// The document data is stored into result, the document meta data is returned.
// If no document exists with given key, a NotFoundError is returned.
func (c *edgeCollection) ReadDocument(ctx context.Context, key string, result interface{}) (DocumentMeta, error) {
meta, _, err := c.readDocument(ctx, key, result)
if err != nil {
return DocumentMeta{}, WithStack(err)
}
return meta, nil
}
func (c *edgeCollection) readDocument(ctx context.Context, key string, result interface{}) (DocumentMeta, contextSettings, error) {
if err := validateKey(key); err != nil {
return DocumentMeta{}, contextSettings{}, WithStack(err)
}
escapedKey := pathEscape(key)
req, err := c.conn.NewRequest("GET", path.Join(c.relPath(), escapedKey))
if err != nil {
return DocumentMeta{}, contextSettings{}, WithStack(err)
}
cs := applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return DocumentMeta{}, contextSettings{}, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return DocumentMeta{}, contextSettings{}, WithStack(err)
}
// Concerns: ReadDocuments reads multiple documents via multiple calls to readDocument (this function).
// Currently with AllowDirtyReads the wasDirtyFlag is only set according to the last read request.
loadContextResponseValues(cs, resp)
// Parse metadata
var meta DocumentMeta
if err := resp.ParseBody("edge", &meta); err != nil {
return DocumentMeta{}, contextSettings{}, WithStack(err)
}
// Parse result
if result != nil {
if err := resp.ParseBody("edge", result); err != nil {
return meta, contextSettings{}, WithStack(err)
}
}
return meta, cs, nil
}
// ReadDocuments reads multiple documents with given keys from the collection.
// The documents data is stored into elements of the given results slice,
// the documents meta data is returned.
// If no document exists with a given key, a NotFoundError is returned at its errors index.
func (c *edgeCollection) ReadDocuments(ctx context.Context, keys []string, results interface{}) (DocumentMetaSlice, ErrorSlice, error) {
resultsVal := reflect.ValueOf(results)
switch resultsVal.Kind() {
case reflect.Array, reflect.Slice:
// OK
default:
return nil, nil, WithStack(InvalidArgumentError{Message: fmt.Sprintf("results data must be of kind Array, got %s", resultsVal.Kind())})
}
if keys == nil {
return nil, nil, WithStack(InvalidArgumentError{Message: "keys nil"})
}
resultCount := resultsVal.Len()
if len(keys) != resultCount {
return nil, nil, WithStack(InvalidArgumentError{Message: fmt.Sprintf("expected %d keys, got %d", resultCount, len(keys))})
}
for _, key := range keys {
if err := validateKey(key); err != nil {
return nil, nil, WithStack(err)
}
}
metas := make(DocumentMetaSlice, resultCount)
errs := make(ErrorSlice, resultCount)
silent := false
for i := 0; i < resultCount; i++ {
result := resultsVal.Index(i).Addr()
ctx, err := withDocumentAt(ctx, i)
if err != nil {
return nil, nil, WithStack(err)
}
key := keys[i]
meta, cs, err := c.readDocument(ctx, key, result.Interface())
if cs.Silent {
silent = true
} else {
metas[i], errs[i] = meta, err
}
}
if silent {
return nil, nil, nil
}
return metas, errs, nil
}
// CreateDocument creates a single document in the collection.
// The document data is loaded from the given document, the document meta data is returned.
// If the document data already contains a `_key` field, this will be used as key of the new document,
// otherwise a unique key is created.
// A ConflictError is returned when a `_key` field contains a duplicate key, other any other field violates an index constraint.
// To return the NEW document, prepare a context with `WithReturnNew`.
// To wait until document has been synced to disk, prepare a context with `WithWaitForSync`.
func (c *edgeCollection) CreateDocument(ctx context.Context, document interface{}) (DocumentMeta, error) {
meta, _, err := c.createDocument(ctx, document)
if err != nil {
return DocumentMeta{}, WithStack(err)
}
return meta, nil
}
func (c *edgeCollection) createDocument(ctx context.Context, document interface{}) (DocumentMeta, contextSettings, error) {
if document == nil {
return DocumentMeta{}, contextSettings{}, WithStack(InvalidArgumentError{Message: "document nil"})
}
req, err := c.conn.NewRequest("POST", c.relPath())
if err != nil {
return DocumentMeta{}, contextSettings{}, WithStack(err)
}
if _, err := req.SetBody(document); err != nil {
return DocumentMeta{}, contextSettings{}, WithStack(err)
}
cs := applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return DocumentMeta{}, cs, WithStack(err)
}
if err := resp.CheckStatus(201, 202); err != nil {
return DocumentMeta{}, cs, WithStack(err)
}
if cs.Silent {
// Empty response, we're done
return DocumentMeta{}, cs, nil
}
// Parse metadata
var meta DocumentMeta
if err := resp.ParseBody("edge", &meta); err != nil {
return DocumentMeta{}, cs, WithStack(err)
}
// Parse returnNew (if needed)
if cs.ReturnNew != nil {
if err := resp.ParseBody("new", cs.ReturnNew); err != nil {
return meta, cs, WithStack(err)
}
}
return meta, cs, nil
}
// CreateDocuments creates multiple documents in the collection.
// The document data is loaded from the given documents slice, the documents meta data is returned.
// If a documents element already contains a `_key` field, this will be used as key of the new document,
// otherwise a unique key is created.
// If a documents element contains a `_key` field with a duplicate key, other any other field violates an index constraint,
// a ConflictError is returned at its index in the errors slice.
// To return the NEW documents, prepare a context with `WithReturnNew`. The data argument passed to `WithReturnNew` must be
// a slice with the same number of entries as the `documents` slice.
// To wait until document has been synced to disk, prepare a context with `WithWaitForSync`.
// If the create request itself fails or one of the arguments is invalid, an error is returned.
func (c *edgeCollection) CreateDocuments(ctx context.Context, documents interface{}) (DocumentMetaSlice, ErrorSlice, error) {
documentsVal := reflect.ValueOf(documents)
switch documentsVal.Kind() {
case reflect.Array, reflect.Slice:
// OK
default:
return nil, nil, WithStack(InvalidArgumentError{Message: fmt.Sprintf("documents data must be of kind Array, got %s", documentsVal.Kind())})
}
documentCount := documentsVal.Len()
metas := make(DocumentMetaSlice, documentCount)
errs := make(ErrorSlice, documentCount)
silent := false
for i := 0; i < documentCount; i++ {
doc := documentsVal.Index(i)
ctx, err := withDocumentAt(ctx, i)
if err != nil {
return nil, nil, WithStack(err)
}
meta, cs, err := c.createDocument(ctx, doc.Interface())
if cs.Silent {
silent = true
} else {
metas[i], errs[i] = meta, err
}
}
if silent {
return nil, nil, nil
}
return metas, errs, nil
}
// UpdateDocument updates a single document with given key in the collection.
// The document meta data is returned.
// To return the NEW document, prepare a context with `WithReturnNew`.
// To return the OLD document, prepare a context with `WithReturnOld`.
// To wait until document has been synced to disk, prepare a context with `WithWaitForSync`.
// If no document exists with given key, a NotFoundError is returned.
func (c *edgeCollection) UpdateDocument(ctx context.Context, key string, update interface{}) (DocumentMeta, error) {
meta, _, err := c.updateDocument(ctx, key, update)
if err != nil {
return DocumentMeta{}, WithStack(err)
}
return meta, nil
}
func (c *edgeCollection) updateDocument(ctx context.Context, key string, update interface{}) (DocumentMeta, contextSettings, error) {
if err := validateKey(key); err != nil {
return DocumentMeta{}, contextSettings{}, WithStack(err)
}
if update == nil {
return DocumentMeta{}, contextSettings{}, WithStack(InvalidArgumentError{Message: "update nil"})
}
escapedKey := pathEscape(key)
req, err := c.conn.NewRequest("PATCH", path.Join(c.relPath(), escapedKey))
if err != nil {
return DocumentMeta{}, contextSettings{}, WithStack(err)
}
if _, err := req.SetBody(update); err != nil {
return DocumentMeta{}, contextSettings{}, WithStack(err)
}
cs := applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return DocumentMeta{}, cs, WithStack(err)
}
if err := resp.CheckStatus(200, 201, 202); err != nil {
return DocumentMeta{}, cs, WithStack(err)
}
if cs.Silent {
// Empty response, we're done
return DocumentMeta{}, cs, nil
}
// Parse metadata
var meta DocumentMeta
if err := resp.ParseBody("edge", &meta); err != nil {
return DocumentMeta{}, cs, WithStack(err)
}
// Parse returnOld (if needed)
if cs.ReturnOld != nil {
if err := resp.ParseBody("old", cs.ReturnOld); err != nil {
return meta, cs, WithStack(err)
}
}
// Parse returnNew (if needed)
if cs.ReturnNew != nil {
if err := resp.ParseBody("new", cs.ReturnNew); err != nil {
return meta, cs, WithStack(err)
}
}
return meta, cs, nil
}
// UpdateDocuments updates multiple document with given keys in the collection.
// The updates are loaded from the given updates slice, the documents meta data are returned.
// To return the NEW documents, prepare a context with `WithReturnNew` with a slice of documents.
// To return the OLD documents, prepare a context with `WithReturnOld` with a slice of documents.
// To wait until documents has been synced to disk, prepare a context with `WithWaitForSync`.
// If no document exists with a given key, a NotFoundError is returned at its errors index.
func (c *edgeCollection) UpdateDocuments(ctx context.Context, keys []string, updates interface{}) (DocumentMetaSlice, ErrorSlice, error) {
updatesVal := reflect.ValueOf(updates)
switch updatesVal.Kind() {
case reflect.Array, reflect.Slice:
// OK
default:
return nil, nil, WithStack(InvalidArgumentError{Message: fmt.Sprintf("updates data must be of kind Array, got %s", updatesVal.Kind())})
}
updateCount := updatesVal.Len()
if keys != nil {
if len(keys) != updateCount {
return nil, nil, WithStack(InvalidArgumentError{Message: fmt.Sprintf("expected %d keys, got %d", updateCount, len(keys))})
}
for _, key := range keys {
if err := validateKey(key); err != nil {
return nil, nil, WithStack(err)
}
}
}
metas := make(DocumentMetaSlice, updateCount)
errs := make(ErrorSlice, updateCount)
silent := false
for i := 0; i < updateCount; i++ {
update := updatesVal.Index(i)
ctx, err := withDocumentAt(ctx, i)
if err != nil {
return nil, nil, WithStack(err)
}
var key string
if keys != nil {
key = keys[i]
} else {
var err error
key, err = getKeyFromDocument(update)
if err != nil {
errs[i] = err
continue
}
}
meta, cs, err := c.updateDocument(ctx, key, update.Interface())
if cs.Silent {
silent = true
} else {
metas[i], errs[i] = meta, err
}
}
if silent {
return nil, nil, nil
}
return metas, errs, nil
}
// ReplaceDocument replaces a single document with given key in the collection with the document given in the document argument.
// The document meta data is returned.
// To return the NEW document, prepare a context with `WithReturnNew`.
// To return the OLD document, prepare a context with `WithReturnOld`.
// To wait until document has been synced to disk, prepare a context with `WithWaitForSync`.
// If no document exists with given key, a NotFoundError is returned.
func (c *edgeCollection) ReplaceDocument(ctx context.Context, key string, document interface{}) (DocumentMeta, error) {
meta, _, err := c.replaceDocument(ctx, key, document)
if err != nil {
return DocumentMeta{}, WithStack(err)
}
return meta, nil
}
func (c *edgeCollection) replaceDocument(ctx context.Context, key string, document interface{}) (DocumentMeta, contextSettings, error) {
if err := validateKey(key); err != nil {
return DocumentMeta{}, contextSettings{}, WithStack(err)
}
if document == nil {
return DocumentMeta{}, contextSettings{}, WithStack(InvalidArgumentError{Message: "document nil"})
}
escapedKey := pathEscape(key)
req, err := c.conn.NewRequest("PUT", path.Join(c.relPath(), escapedKey))
if err != nil {
return DocumentMeta{}, contextSettings{}, WithStack(err)
}
if _, err := req.SetBody(document); err != nil {
return DocumentMeta{}, contextSettings{}, WithStack(err)
}
cs := applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return DocumentMeta{}, cs, WithStack(err)
}
if err := resp.CheckStatus(201, 202); err != nil {
return DocumentMeta{}, cs, WithStack(err)
}
if cs.Silent {
// Empty response, we're done
return DocumentMeta{}, cs, nil
}
// Parse metadata
var meta DocumentMeta
if err := resp.ParseBody("edge", &meta); err != nil {
return DocumentMeta{}, cs, WithStack(err)
}
// Parse returnOld (if needed)
if cs.ReturnOld != nil {
if err := resp.ParseBody("old", cs.ReturnOld); err != nil {
return meta, cs, WithStack(err)
}
}
// Parse returnNew (if needed)
if cs.ReturnNew != nil {
if err := resp.ParseBody("new", cs.ReturnNew); err != nil {
return meta, cs, WithStack(err)
}
}
return meta, cs, nil
}
// ReplaceDocuments replaces multiple documents with given keys in the collection with the documents given in the documents argument.
// The replacements are loaded from the given documents slice, the documents meta data are returned.
// To return the NEW documents, prepare a context with `WithReturnNew` with a slice of documents.
// To return the OLD documents, prepare a context with `WithReturnOld` with a slice of documents.
// To wait until documents has been synced to disk, prepare a context with `WithWaitForSync`.
// If no document exists with a given key, a NotFoundError is returned at its errors index.
func (c *edgeCollection) ReplaceDocuments(ctx context.Context, keys []string, documents interface{}) (DocumentMetaSlice, ErrorSlice, error) {
documentsVal := reflect.ValueOf(documents)
switch documentsVal.Kind() {
case reflect.Array, reflect.Slice:
// OK
default:
return nil, nil, WithStack(InvalidArgumentError{Message: fmt.Sprintf("documents data must be of kind Array, got %s", documentsVal.Kind())})
}
documentCount := documentsVal.Len()
if keys != nil {
if len(keys) != documentCount {
return nil, nil, WithStack(InvalidArgumentError{Message: fmt.Sprintf("expected %d keys, got %d", documentCount, len(keys))})
}
for _, key := range keys {
if err := validateKey(key); err != nil {
return nil, nil, WithStack(err)
}
}
}
metas := make(DocumentMetaSlice, documentCount)
errs := make(ErrorSlice, documentCount)
silent := false
for i := 0; i < documentCount; i++ {
doc := documentsVal.Index(i)
ctx, err := withDocumentAt(ctx, i)
if err != nil {
return nil, nil, WithStack(err)
}
var key string
if keys != nil {
key = keys[i]
} else {
var err error
key, err = getKeyFromDocument(doc)
if err != nil {
errs[i] = err
continue
}
}
meta, cs, err := c.replaceDocument(ctx, key, doc.Interface())
if cs.Silent {
silent = true
} else {
metas[i], errs[i] = meta, err
}
}
if silent {
return nil, nil, nil
}
return metas, errs, nil
}
// RemoveDocument removes a single document with given key from the collection.
// The document meta data is returned.
// To return the OLD document, prepare a context with `WithReturnOld`.
// To wait until removal has been synced to disk, prepare a context with `WithWaitForSync`.
// If no document exists with given key, a NotFoundError is returned.
func (c *edgeCollection) RemoveDocument(ctx context.Context, key string) (DocumentMeta, error) {
meta, _, err := c.removeDocument(ctx, key)
if err != nil {
return DocumentMeta{}, WithStack(err)
}
return meta, nil
}
func (c *edgeCollection) removeDocument(ctx context.Context, key string) (DocumentMeta, contextSettings, error) {
if err := validateKey(key); err != nil {
return DocumentMeta{}, contextSettings{}, WithStack(err)
}
escapedKey := pathEscape(key)
req, err := c.conn.NewRequest("DELETE", path.Join(c.relPath(), escapedKey))
if err != nil {
return DocumentMeta{}, contextSettings{}, WithStack(err)
}
cs := applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return DocumentMeta{}, cs, WithStack(err)
}
if err := resp.CheckStatus(200, 202); err != nil {
return DocumentMeta{}, cs, WithStack(err)
}
if cs.Silent {
// Empty response, we're done
return DocumentMeta{}, cs, nil
}
// Parse metadata
var meta DocumentMeta
if err := resp.ParseBody("edge", &meta); err != nil {
return DocumentMeta{}, cs, WithStack(err)
}
// Parse returnOld (if needed)
if cs.ReturnOld != nil {
if err := resp.ParseBody("old", cs.ReturnOld); err != nil {
return meta, cs, WithStack(err)
}
}
return meta, cs, nil
}
// RemoveDocuments removes multiple documents with given keys from the collection.
// The document meta data are returned.
// To return the OLD documents, prepare a context with `WithReturnOld` with a slice of documents.
// To wait until removal has been synced to disk, prepare a context with `WithWaitForSync`.
// If no document exists with a given key, a NotFoundError is returned at its errors index.
func (c *edgeCollection) RemoveDocuments(ctx context.Context, keys []string) (DocumentMetaSlice, ErrorSlice, error) {
keyCount := len(keys)
for _, key := range keys {
if err := validateKey(key); err != nil {
return nil, nil, WithStack(err)
}
}
metas := make(DocumentMetaSlice, keyCount)
errs := make(ErrorSlice, keyCount)
silent := false
for i := 0; i < keyCount; i++ {
key := keys[i]
ctx, err := withDocumentAt(ctx, i)
if err != nil {
return nil, nil, WithStack(err)
}
meta, cs, err := c.removeDocument(ctx, key)
if cs.Silent {
silent = true
} else {
metas[i], errs[i] = meta, err
}
}
if silent {
return nil, nil, nil
}
return metas, errs, nil
}
// ImportDocuments imports one or more documents into the collection.
// The document data is loaded from the given documents argument, statistics are returned.
// The documents argument can be one of the following:
// - An array of structs: All structs will be imported as individual documents.
// - An array of maps: All maps will be imported as individual documents.
// To wait until all documents have been synced to disk, prepare a context with `WithWaitForSync`.
// To return details about documents that could not be imported, prepare a context with `WithImportDetails`.
func (c *edgeCollection) ImportDocuments(ctx context.Context, documents interface{}, options *ImportDocumentOptions) (ImportDocumentStatistics, error) {
stats, err := c.rawCollection().ImportDocuments(ctx, documents, options)
if err != nil {
return ImportDocumentStatistics{}, WithStack(err)
}
return stats, nil
}
// getKeyFromDocument looks for a `_key` document in the given document and returns it.
func getKeyFromDocument(doc reflect.Value) (string, error) {
if doc.IsNil() {
return "", WithStack(InvalidArgumentError{Message: "Document is nil"})
}
if doc.Kind() == reflect.Ptr {
doc = doc.Elem()
}
switch doc.Kind() {
case reflect.Struct:
structType := doc.Type()
fieldCount := structType.NumField()
for i := 0; i < fieldCount; i++ {
f := structType.Field(i)
tagParts := strings.Split(f.Tag.Get("json"), ",")
if tagParts[0] == "_key" {
// We found the _key field
keyVal := doc.Field(i)
return keyVal.String(), nil
}
}
return "", WithStack(InvalidArgumentError{Message: "Document contains no '_key' field"})
case reflect.Map:
keyVal := doc.MapIndex(reflect.ValueOf("_key"))
if keyVal.IsNil() {
return "", WithStack(InvalidArgumentError{Message: "Document contains no '_key' entry"})
}
return keyVal.String(), nil
default:
return "", WithStack(InvalidArgumentError{Message: fmt.Sprintf("Document must be struct or map. Got %s", doc.Kind())})
}
}