slaventius@odnookno.info 2 years ago
parent 7e251bccaa
commit 6814a13a9b
  1. 4
      go.mod
  2. 6
      go.sum
  3. 18
      vendor/github.com/arangodb/go-driver/.gitignore
  4. 52
      vendor/github.com/arangodb/go-driver/.golangci.yaml
  5. 52
      vendor/github.com/arangodb/go-driver/.travis.yml
  6. 91
      vendor/github.com/arangodb/go-driver/CHANGELOG.md
  7. 63
      vendor/github.com/arangodb/go-driver/CONTRIBUTING.md
  8. 11
      vendor/github.com/arangodb/go-driver/Dockerfile.debug
  9. 18
      vendor/github.com/arangodb/go-driver/HEADER
  10. 202
      vendor/github.com/arangodb/go-driver/LICENSE
  11. 27
      vendor/github.com/arangodb/go-driver/MAINTAINERS.md
  12. 567
      vendor/github.com/arangodb/go-driver/Makefile
  13. 39
      vendor/github.com/arangodb/go-driver/README.md
  14. 1
      vendor/github.com/arangodb/go-driver/VERSION
  15. 114
      vendor/github.com/arangodb/go-driver/authentication.go
  16. 124
      vendor/github.com/arangodb/go-driver/client.go
  17. 149
      vendor/github.com/arangodb/go-driver/client_admin_backup.go
  18. 305
      vendor/github.com/arangodb/go-driver/client_admin_backup_impl.go
  19. 33
      vendor/github.com/arangodb/go-driver/client_cluster.go
  20. 46
      vendor/github.com/arangodb/go-driver/client_cluster_impl.go
  21. 85
      vendor/github.com/arangodb/go-driver/client_databases.go
  22. 154
      vendor/github.com/arangodb/go-driver/client_databases_impl.go
  23. 47
      vendor/github.com/arangodb/go-driver/client_foxx.go
  24. 28
      vendor/github.com/arangodb/go-driver/client_foxx_impl.go
  25. 153
      vendor/github.com/arangodb/go-driver/client_impl.go
  26. 29
      vendor/github.com/arangodb/go-driver/client_replication.go
  27. 28
      vendor/github.com/arangodb/go-driver/client_replication_impl.go
  28. 204
      vendor/github.com/arangodb/go-driver/client_server_admin.go
  29. 243
      vendor/github.com/arangodb/go-driver/client_server_admin_impl.go
  30. 61
      vendor/github.com/arangodb/go-driver/client_server_info.go
  31. 152
      vendor/github.com/arangodb/go-driver/client_server_info_impl.go
  32. 52
      vendor/github.com/arangodb/go-driver/client_users.go
  33. 144
      vendor/github.com/arangodb/go-driver/client_users_impl.go
  34. 357
      vendor/github.com/arangodb/go-driver/cluster.go
  35. 365
      vendor/github.com/arangodb/go-driver/cluster/cluster.go
  36. 26
      vendor/github.com/arangodb/go-driver/cluster/doc.go
  37. 489
      vendor/github.com/arangodb/go-driver/cluster_impl.go
  38. 331
      vendor/github.com/arangodb/go-driver/collection.go
  39. 677
      vendor/github.com/arangodb/go-driver/collection_document_impl.go
  40. 178
      vendor/github.com/arangodb/go-driver/collection_documents.go
  41. 387
      vendor/github.com/arangodb/go-driver/collection_impl.go
  42. 302
      vendor/github.com/arangodb/go-driver/collection_indexes.go
  43. 394
      vendor/github.com/arangodb/go-driver/collection_indexes_impl.go
  44. 172
      vendor/github.com/arangodb/go-driver/connection.go
  45. 46
      vendor/github.com/arangodb/go-driver/content_type.go
  46. 543
      vendor/github.com/arangodb/go-driver/context.go
  47. 91
      vendor/github.com/arangodb/go-driver/cursor.go
  48. 345
      vendor/github.com/arangodb/go-driver/cursor_impl.go
  49. 129
      vendor/github.com/arangodb/go-driver/database.go
  50. 61
      vendor/github.com/arangodb/go-driver/database_arangosearch_analyzers.go
  51. 184
      vendor/github.com/arangodb/go-driver/database_arangosearch_analyzers_impl.go
  52. 224
      vendor/github.com/arangodb/go-driver/database_collections.go
  53. 190
      vendor/github.com/arangodb/go-driver/database_collections_impl.go
  54. 54
      vendor/github.com/arangodb/go-driver/database_collections_schema.go
  55. 93
      vendor/github.com/arangodb/go-driver/database_graphs.go
  56. 271
      vendor/github.com/arangodb/go-driver/database_graphs_impl.go
  57. 250
      vendor/github.com/arangodb/go-driver/database_impl.go
  58. 182
      vendor/github.com/arangodb/go-driver/database_pregel.go
  59. 116
      vendor/github.com/arangodb/go-driver/database_pregel_impl.go
  60. 76
      vendor/github.com/arangodb/go-driver/database_transactions.go
  61. 100
      vendor/github.com/arangodb/go-driver/database_transactions_impl.go
  62. 58
      vendor/github.com/arangodb/go-driver/database_views.go
  63. 202
      vendor/github.com/arangodb/go-driver/database_views_impl.go
  64. 43
      vendor/github.com/arangodb/go-driver/doc.go
  65. 31
      vendor/github.com/arangodb/go-driver/edge.go
  66. 596
      vendor/github.com/arangodb/go-driver/edge_collection_documents_impl.go
  67. 187
      vendor/github.com/arangodb/go-driver/edge_collection_impl.go
  68. 148
      vendor/github.com/arangodb/go-driver/edge_collection_indexes_impl.go
  69. 39
      vendor/github.com/arangodb/go-driver/encode-go_1_8.go
  70. 39
      vendor/github.com/arangodb/go-driver/encode.go
  71. 316
      vendor/github.com/arangodb/go-driver/error.go
  72. 85
      vendor/github.com/arangodb/go-driver/foxx.go
  73. 80
      vendor/github.com/arangodb/go-driver/graph.go
  74. 67
      vendor/github.com/arangodb/go-driver/graph_edge_collections.go
  75. 239
      vendor/github.com/arangodb/go-driver/graph_edge_collections_impl.go
  76. 139
      vendor/github.com/arangodb/go-driver/graph_impl.go
  77. 54
      vendor/github.com/arangodb/go-driver/graph_vertex_collections.go
  78. 176
      vendor/github.com/arangodb/go-driver/graph_vertex_collections_impl.go
  79. 279
      vendor/github.com/arangodb/go-driver/http/authentication.go
  80. 497
      vendor/github.com/arangodb/go-driver/http/connection.go
  81. 111
      vendor/github.com/arangodb/go-driver/http/connection_wrapper.go
  82. 68
      vendor/github.com/arangodb/go-driver/http/doc.go
  83. 84
      vendor/github.com/arangodb/go-driver/http/mergeObject.go
  84. 369
      vendor/github.com/arangodb/go-driver/http/request_json.go
  85. 144
      vendor/github.com/arangodb/go-driver/http/request_vpack.go
  86. 206
      vendor/github.com/arangodb/go-driver/http/response_json.go
  87. 113
      vendor/github.com/arangodb/go-driver/http/response_json_element.go
  88. 151
      vendor/github.com/arangodb/go-driver/http/response_vpack.go
  89. 124
      vendor/github.com/arangodb/go-driver/http/response_vpack_element.go
  90. 98
      vendor/github.com/arangodb/go-driver/id.go
  91. 102
      vendor/github.com/arangodb/go-driver/index.go
  92. 287
      vendor/github.com/arangodb/go-driver/index_impl.go
  93. 69
      vendor/github.com/arangodb/go-driver/meta.go
  94. 56
      vendor/github.com/arangodb/go-driver/protocol.go
  95. 297
      vendor/github.com/arangodb/go-driver/query.go
  96. 68
      vendor/github.com/arangodb/go-driver/replication.go
  97. 165
      vendor/github.com/arangodb/go-driver/replication_impl.go
  98. 273
      vendor/github.com/arangodb/go-driver/revision.go
  99. 80
      vendor/github.com/arangodb/go-driver/transaction.go
  100. 128
      vendor/github.com/arangodb/go-driver/user.go
  101. Some files were not shown because too many files have changed in this diff Show More

@ -4,19 +4,21 @@ go 1.19
require (
git.slaventius.ru/test3k/umate v0.0.0-20230224092743-ac0be38b33ed
github.com/arangodb/go-driver v1.5.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/pkg/errors v0.9.1
github.com/segmentio/kafka-go v0.4.38
google.golang.org/grpc v1.53.0
)
require (
github.com/arangodb/go-velocypack v0.0.0-20200318135517-5af53c29c67e // indirect
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d // indirect
github.com/evalphobia/logrus_sentry v0.8.2 // indirect
github.com/getsentry/raven-go v0.2.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/pierrec/lz4/v4 v4.1.15 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/sys v0.4.0 // indirect

@ -1,5 +1,9 @@
git.slaventius.ru/test3k/umate v0.0.0-20230224092743-ac0be38b33ed h1:PGPP77D64ZkbZ7al3weWQS6fgCExMV9dLNE2zs9h9xE=
git.slaventius.ru/test3k/umate v0.0.0-20230224092743-ac0be38b33ed/go.mod h1:xE7ik2EnLB+CNDJA5+HbRIwAk+V7sgUjS0HPeXS5Ka0=
github.com/arangodb/go-driver v1.5.0 h1:PaSwAMbxGATf6M5uJpm5fvO/6FiT8ZgJIxEl+AQj6EY=
github.com/arangodb/go-driver v1.5.0/go.mod h1:+Kn5y+rHkSpjXmYOQiBDhwSJvIhOHdNI1ODl/aqD/fc=
github.com/arangodb/go-velocypack v0.0.0-20200318135517-5af53c29c67e h1:Xg+hGrY2LcQBbxd0ZFdbGSyRKTYMZCfBbw/pMJFOk1g=
github.com/arangodb/go-velocypack v0.0.0-20200318135517-5af53c29c67e/go.mod h1:mq7Shfa/CaixoDxiyAAc5jZ6CVBAyPaNQCGS7mkj4Ho=
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d h1:S2NE3iHSwP0XV47EEXL8mWmRdEfGscSJ+7EgePNgt0s=
github.com/certifi/gocertifi v0.0.0-20210507211836-431795d63e8d/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@ -30,6 +34,7 @@ github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
@ -68,6 +73,7 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

@ -0,0 +1,18 @@
#Go packages
.gobuild
#Temporary tests files
.tmp
#IDE's files
.idea
#Vendor files
vendor
# Helper files
debug/
*.log
# direnv files
.envrc

@ -0,0 +1,52 @@
---
run:
issues-exit-code: 3
timeout: 30m
skip-dirs:
- vendor
linters:
fast: false
enable-all: false
disable-all: false
presets:
- performance
- format
- complexity
- bugs
- unused
disable:
- staticcheck
- errcheck
- govet
- gosec
- ineffassign
- noctx
- contextcheck
- unparam
- scopelint
- exhaustive
- cyclop
- errorlint
- errchkjson
- nestif
- prealloc
- maligned
- funlen
- typecheck
- deadcode
- unused
- maintidx
- varcheck
- gocognit
- gofumpt
- gocyclo
linters-settings:
gci:
sections:
- standard
- default
- prefix(github.com/arangodb)
- prefix(github.com/arangodb/go-driver)

@ -0,0 +1,52 @@
sudo: required
before_install:
- |
mkdir -p $HOME/resources
for i in {0..3}
do
if ! [ -f "$HOME/resources/itzpapalotl-v1.2.0.zip" ]; then
curl -L0 -o $HOME/resources/itzpapalotl-v1.2.0.zip "https://github.com/arangodb-foxx/demo-itzpapalotl/archive/v1.2.0.zip"
fi
SHA=$(sha256sum $HOME/resources/itzpapalotl-v1.2.0.zip | cut -f 1 -d " ")
if [ "${SHA}" = "86117db897efe86cbbd20236abba127a08c2bdabbcd63683567ee5e84115d83a" ]; then
break
fi
$HOME/resources/itzpapalotl-v1.2.0.zip
done
- |
if ! [ -f "$HOME/resources/itzpapalotl-v1.2.0.zip" ]; then
travis_terminate 1
fi
services:
- docker
language: go
go:
- 1.19.4
env:
- TEST_SUITE=run-unit-tests GOIMAGE=gcr.io/gcr-for-testing/golang:1.19.4 ALWAYS=1
- TEST_SUITE=run-tests-single GOIMAGE=gcr.io/gcr-for-testing/golang:1.19.4 STARTER=gcr.io/gcr-for-testing/arangodb/arangodb-starter:latest ALPINE_IMAGE=gcr.io/gcr-for-testing/alpine:3.4 ARANGODB=eu.gcr.io/arangodb-ci/official/arangodb/arangodb:3.9.0
- TEST_SUITE=run-tests-resilientsingle GOIMAGE=gcr.io/gcr-for-testing/golang:1.19.4 STARTER=gcr.io/gcr-for-testing/arangodb/arangodb-starter:latest ALPINE_IMAGE=gcr.io/gcr-for-testing/alpine:3.4 ARANGODB=gcr.io/gcr-for-testing/arangodb/arangodb:3.10.0
- TEST_SUITE=run-tests-cluster GOIMAGE=gcr.io/gcr-for-testing/golang:1.19.4 STARTER=gcr.io/gcr-for-testing/arangodb/arangodb-starter:latest ALPINE_IMAGE=gcr.io/gcr-for-testing/alpine:3.4 ARANGODB=gcr.io/gcr-for-testing/arangodb/arangodb:3.10.0
- TEST_SUITE=run-tests-single GOIMAGE=gcr.io/gcr-for-testing/golang:1.19.4 STARTER=gcr.io/gcr-for-testing/arangodb/arangodb-starter:latest ALPINE_IMAGE=gcr.io/gcr-for-testing/alpine:3.4 ARANGODB=gcr.io/gcr-for-testing/arangodb/arangodb:3.10.0 TEST_DISALLOW_UNKNOWN_FIELDS=false ALWAYS=1
- TEST_SUITE=run-v2-tests-single GOIMAGE=gcr.io/gcr-for-testing/golang:1.19.4 STARTER=gcr.io/gcr-for-testing/arangodb/arangodb-starter:latest ALPINE_IMAGE=gcr.io/gcr-for-testing/alpine:3.4 ARANGODB=eu.gcr.io/arangodb-ci/official/arangodb/arangodb:3.9.0
- TEST_SUITE=run-v2-tests-single GOIMAGE=gcr.io/gcr-for-testing/golang:1.19.4 STARTER=gcr.io/gcr-for-testing/arangodb/arangodb-starter:latest ALPINE_IMAGE=gcr.io/gcr-for-testing/alpine:3.4 ARANGODB=gcr.io/gcr-for-testing/arangodb/arangodb:3.10.0 ALWAYS=1
before_script:
- |
if [ "$TRAVIS_PULL_REQUEST" != "false" ] || [ ! -z "$ALWAYS" ]; then
make tools
make linter
fi
script:
- |
if [ "$TRAVIS_PULL_REQUEST" != "false" ] || [ ! -z "$ALWAYS" ]; then
make $TEST_SUITE TEST_RESOURCES="$HOME/resources/" VERBOSE=1;
fi

@ -0,0 +1,91 @@
# Change Log
## [master](https://github.com/arangodb/go-driver/tree/master) (N/A)
## [1.5.0](https://github.com/arangodb/go-driver/tree/v1.5.0) (2023-02-17)
- Use Go 1.19.4
- Add `IsExternalStorageError` to check for [external storage errors](https://www.arangodb.com/docs/stable/appendix-error-codes.html#external-arangodb-storage-errors)
- `nested` field in arangosearch type View
- Fix: TTL index creation fails when expireAt is 0
- [V2] Support for Collection Indexes
- Fix: Fetching single InvertedIndex fails with Marshalling error
## [1.4.1](https://github.com/arangodb/go-driver/tree/v1.4.1) (2022-12-14)
- Add support for `checksum` in Collections
- Fix reusing same connection with different Authentication parameters passed via driver.NewClient
- Add `cache` for ArangoSearchView Link and StoredValue types and `primarySortCache`, `primaryKeyCache` for ArangoSearchView type
## [1.4.0](https://github.com/arangodb/go-driver/tree/v1.4.0) (2022-10-04)
- Add `hex` property to analyzer's properties
- Add support for `computedValues`
- Optional `computeOn` field in `computedValues`
- Add support for `computedValues` into collection inventory
- Update the structures to align them with the ArangoDB 3.10 release
- Add `IsNotFoundGeneral` and `IsDataSourceOrDocumentNotFound` methods - deprecate `IsNotFound`
- Add support for optimizer rules (AQL query)
- New `LegacyPolygons` parameter for Geo Indexes
- New parameters (`cacheEnabled` and `storedValues`) for Persistent Indexes
- New analyzers: `classification`, `nearest neighbors`, `minhash`
- Add support for Inverted index
- Deprecate fulltext index
- Add support for Pregel API
- Add tests to check support for Enterprise Graphs
- Search View v2 (`search-alias`)
- Add Rename View support
- Add support for `Metrics`
## [1.3.3](https://github.com/arangodb/go-driver/tree/v1.3.3) (2022-07-27)
- Fix `lastValue` field type
- Setup Go-lang linter with minimal configuration
- Use Go 1.17.6
- Add missing `deduplicate` param to PersistentIndex
## [1.3.2](https://github.com/arangodb/go-driver/tree/v1.3.2) (2022-05-16)
- Fix selectivityEstimate Index field type
## [1.3.1](https://github.com/arangodb/go-driver/tree/v1.3.1) (2022-03-23)
- Add support for `exclusive` field for transaction options
- Fix cursor executionTime statistics getter
- Fix cursor warnings field type
- Fix for DocumentMeta name field overrides name field
## [1.3.0](https://github.com/arangodb/go-driver/tree/v1.3.0) (2022-03-17)
- Disallow unknown fields feature
- inBackground parameter in ArangoSearch links
- ZKD indexes
- Hybrid SmartGraphs
- Segmentation and Collation Analyzers
- Bypass caching for specific collections
- Overload Control
- [V2] Add support for streaming the response body by the caller.
- [V2] Bugfix with escaping the URL path twice.
- Bugfix for the satellites' collection shard info.
- [V2] Support for satellites' collections.
## [1.2.1](https://github.com/arangodb/go-driver/tree/v1.2.1) (2021-09-21)
- Add support for fetching shards' info by the given collection name.
- Change versioning to be go mod compatible
- Add support for ForceOneShardAttributeValue in Query
## [1.2.0](https://github.com/arangodb/go-driver/tree/1.2.0) (2021-08-04)
- Add support for AQL, Pipeline, Stopwords, GeoJSON and GeoPoint Arango Search analyzers.
- Add `estimates` field to indexes properties.
- Add tests for 3.8 ArangoDB and remove tests for 3.5.
- Add Plan support in Query execution.
- Change Golang version from 1.13.4 to 1.16.6.
- Add graceful shutdown for the coordinators.
- Replace 'github.com/dgrijalva/jwt-go' with 'github.com/golang-jwt/jwt'
## [1.1.1](https://github.com/arangodb/go-driver/tree/1.1.1) (2020-11-13)
- Add Driver V2 in Alpha version
- Add HTTP2 support for V1 and V2
- Don't omit the `stopwords` field. The field is mandatory in 3.6 ArangoDB
## [1.1.0](https://github.com/arangodb/go-driver/tree/1.1.0) (2020-08-11)
- Use internal coordinator communication for cursors if specified coordinator was not found on endpoint list
- Add support for Overwrite Mode (ArangoDB 3.7)
- Add support for Schema Collection options (ArangoDB 3.7)
- Add support for Disjoint and Satellite Graphs options (ArangoDB 3.7)
## [1.0.0](https://github.com/arangodb/go-driver/tree/1.0.0) (N/A)
- Enable proper CHANGELOG and versioning

@ -0,0 +1,63 @@
Contributing
============
We welcome bug fixes and patches from 3rd party contributors. Please
see the [Contributor Agreement](https://www.arangodb.com/community#contribute)
for details.
Please follow these guidelines if you want to contribute to ArangoDB:
Reporting Bugs
--------------
When reporting bugs, please use our issue tracker on GitHub. Please make sure
to include the version number of ArangoDB and the commit hash of the go-driver in your bug report, along with the
platform you are using (e.g. `Linux OpenSuSE x86_64`). Please also include the
ArangoDB startup mode (daemon, console, supervisor mode), type of connection used
towards ArangoDB plus any special configuration.
This will help us reproducing and finding bugs.
Please also take the time to check there are no similar/identical issues open
yet.
Contributing features, documentation, tests
-------------------------------------------
* Create a new branch in your fork, based on the **master** branch
* Develop and test your modifications there
* Commit as you like, but preferably in logical chunks. Use meaningful commit
messages and make sure you do not commit unnecessary files (e.g. object
files). It is normally a good idea to reference the issue number from the
commit message so the issues will get updated automatically with comments.
* If the modifications change any documented behavior or add new features,
document the changes and provide application tests in the `test` folder.
All documentation should be written in American English (AE).
* When done, run the complete test suite (`make run-tests`) and make sure all tests pass.
* When finished, push the changes to your GitHub repository and send a pull
request from your fork to the ArangoDB repository. Please make sure to select
the appropriate branches there. This will most likely be **master**.
* You must use the Apache License for your changes and have signed our
[CLA](https://www.arangodb.com/documents/cla.pdf). We cannot accept pull requests
from contributors that didn't sign the CLA.
* Please let us know if you plan to work on a ticket. This way we can make sure
redundant work is avoided.
Additional Resources
--------------------
* [ArangoDB website](https://www.arangodb.com/)
* [ArangoDB on Twitter](https://twitter.com/arangodb)
* [General GitHub documentation](https://help.github.com/)
* [GitHub pull request documentation](https://help.github.com/send-pull-requests/)

@ -0,0 +1,11 @@
FROM golang:1.19.4 as builder
ARG TESTS_DIRECTORY
ARG TESTS_ROOT_PATH="."
RUN go install github.com/go-delve/delve/cmd/dlv@latest
WORKDIR /go/src/github.com/arangodb/go-driver
ADD . /go/src/github.com/arangodb/go-driver/
RUN cd $TESTS_ROOT_PATH && go test -gcflags "all=-N -l" -c -o /test_debug.test $TESTS_DIRECTORY

@ -0,0 +1,18 @@
DISCLAIMER
Copyright 2020 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

@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2017 ArangoDB GmbH
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.

@ -0,0 +1,27 @@
# Maintainer Instructions
- Always preserve backward compatibility
- Build using `make clean && make`
- After merging PR, always run `make changelog` and commit changes
- Set ArangoDB docker container (used for testing) using `export ARANGODB=<image-name>`
- Run tests using:
- `make run-tests-single`
- `make run-tests-resilientsingle`
- `make run-tests-cluster`.
- The test can be launched with the flag `RACE=on` which means that test will be performed with the race detector, e.g:
- `RACE=on make run-tests-single`
- Always create changes in a PR
# Change Golang version
- Edit the .travis file and change all occurrences of `golang:x.y.z-stretch` to the appropriate version.
- Edit the Makefile and change the line `GOVERSION ?= 1.16.6` into the required version.
## Debugging with DLV
To attach DLV debugger run tests with `DEBUG=true` flag e.g.:
```shell
DEBUG=true TESTOPTIONS="-test.run TestResponseHeader -test.v" make run-tests-single-json-with-auth
```

@ -0,0 +1,567 @@
PROJECT := go-driver
SCRIPTDIR := $(shell pwd)
CURR=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
ROOTDIR:=$(CURR)
GOVERSION ?= 1.19.4
GOIMAGE ?= golang:$(GOVERSION)
GOV2IMAGE ?= $(GOIMAGE)
ALPINE_IMAGE ?= alpine:3.14
TMPDIR := ${SCRIPTDIR}/.tmp
DOCKER_CMD:=docker run
GOBUILDTAGS:=$(TAGS)
GOBUILDTAGSOPT=-tags "$(GOBUILDTAGS)"
ARANGODB ?= arangodb/arangodb:latest
STARTER ?= arangodb/arangodb-starter:latest
ifndef TESTOPTIONS
TESTOPTIONS :=
endif
ifdef VERBOSE
TESTVERBOSEOPTIONS := -v
endif
CGO_ENABLED=0
ifdef RACE
TESTVERBOSEOPTIONS += -race
CGO_ENABLED=1
endif
ORGPATH := github.com/arangodb
REPONAME := $(PROJECT)
REPODIR := $(ORGDIR)/$(REPONAME)
REPOPATH := $(ORGPATH)/$(REPONAME)
SOURCES_EXCLUDE:=vendor
SOURCES := $(shell find "$(ROOTDIR)" $(foreach SOURCE,$(SOURCES_EXCLUDE),-not -path '$(ROOTDIR)/$(SOURCE)/*') -name '*.go')
# Test variables
ifndef TESTCONTAINER
TESTCONTAINER := $(PROJECT)-test
endif
ifndef DBCONTAINER
DBCONTAINER := $(TESTCONTAINER)-db
endif
ifeq ("$(TEST_AUTH)", "none")
ARANGOENV := -e ARANGO_NO_AUTH=1
TEST_AUTHENTICATION :=
TESTS := $(REPOPATH) $(REPOPATH)/test
else ifeq ("$(TEST_AUTH)", "rootpw")
ARANGOENV := -e ARANGO_ROOT_PASSWORD=rootpw
TEST_AUTHENTICATION := basic:root:rootpw
GOBUILDTAGS += auth
TESTS := $(REPOPATH)/test
else ifeq ("$(TEST_AUTH)", "jwt")
ARANGOENV := -e ARANGO_ROOT_PASSWORD=rootpw
TEST_AUTHENTICATION := jwt:root:rootpw
GOBUILDTAGS += auth
TESTS := $(REPOPATH)/test
JWTSECRET := testing
JWTSECRETFILE := "${TMPDIR}/${TESTCONTAINER}-jwtsecret"
ARANGOVOL := -v "$(JWTSECRETFILE):/jwtsecret"
ARANGOARGS := --server.jwt-secret=/jwtsecret
endif
TEST_NET := --net=container:$(TESTCONTAINER)-ns
TEST_ENDPOINTS := http://localhost:7001
TESTS := $(REPOPATH)/test
ifeq ("$(TEST_AUTH)", "rootpw")
CLUSTERENV := JWTSECRET=testing
TEST_JWTSECRET := testing
TEST_AUTHENTICATION := basic:root:
endif
ifeq ("$(TEST_AUTH)", "jwt")
CLUSTERENV := JWTSECRET=testing
TEST_JWTSECRET := testing
TEST_AUTHENTICATION := jwt:root:
endif
ifeq ("$(TEST_AUTH)", "jwtsuper")
CLUSTERENV := JWTSECRET=testing
TEST_JWTSECRET := testing
TEST_AUTHENTICATION := super:testing
endif
ifeq ("$(TEST_SSL)", "auto")
CLUSTERENV := SSL=auto $(CLUSTERENV)
TEST_ENDPOINTS = https://localhost:7001
endif
ifeq ("$(TEST_CONNECTION)", "vst")
TESTS := $(REPOPATH)/test
ifndef TEST_CONTENT_TYPE
TEST_CONTENT_TYPE := vpack
endif
endif
ifeq ("$(TEST_BENCHMARK)", "true")
TAGS := -bench=. -run=notests -cpu=1,2,4
TESTS := $(REPOPATH)/test
endif
ifdef TEST_ENDPOINTS_OVERRIDE
TEST_NET := --net=host
TEST_ENDPOINTS := $(TEST_ENDPOINTS_OVERRIDE)
endif
ifdef TEST_NET_OVERRIDE
TEST_NET := $(TEST_NET_OVERRIDE)
endif
ifdef ENABLE_VST11
VST11_SINGLE_TESTS := run-tests-single-vst-1.1
VST11_RESILIENTSINGLE_TESTS := run-tests-resilientsingle-vst-1.1
VST11_CLUSTER_TESTS := run-tests-cluster-vst-1.1
endif
TEST_RESOURCES_VOLUME :=
ifdef TEST_RESOURCES
TEST_RESOURCES_VOLUME := -v ${TEST_RESOURCES}:/tmp/resources
endif
ifeq ("$(DEBUG)", "true")
GOIMAGE := go-driver-tests:debug
DOCKER_DEBUG_ARGS := --security-opt=seccomp:unconfined
DEBUG_PORT := 2345
DOCKER_RUN_CMD := $(DOCKER_DEBUG_ARGS) $(GOIMAGE) /go/bin/dlv --listen=:$(DEBUG_PORT) --headless=true --api-version=2 exec /test_debug.test -- $(TESTOPTIONS)
DOCKER_V2_RUN_CMD := $(DOCKER_RUN_CMD)
else
DOCKER_RUN_CMD := $(GOIMAGE) go test $(GOBUILDTAGSOPT) $(TESTOPTIONS) $(TESTVERBOSEOPTIONS) $(TESTS)
DOCKER_V2_RUN_CMD := $(GOV2IMAGE) go test $(GOBUILDTAGSOPT) $(TESTOPTIONS) $(TESTVERBOSEOPTIONS) ./tests
endif
.PHONY: all build clean linter run-tests
all: build
build: __dir_setup $(SOURCES)
go build -v $(REPOPATH) $(REPOPATH)/http $(REPOPATH)/vst $(REPOPATH)/agency $(REPOPATH)/jwt
clean:
@rm -rf "${TMPDIR}"
.PHONY: changelog
changelog:
@$(DOCKER_CMD) --rm \
-e CHANGELOG_GITHUB_TOKEN=$(shell cat ~/.arangodb/github-token) \
-v "${ROOTDIR}":/usr/local/src/your-app \
ferrarimarco/github-changelog-generator \
--user arangodb \
--project go-driver \
--no-author \
--unreleased-label "Master"
run-tests: run-unit-tests run-tests-single run-tests-resilientsingle run-tests-cluster
# The below rule exists only for backward compatibility.
run-tests-http: run-unit-tests
run-unit-tests: run-v2-unit-tests
@$(DOCKER_CMD) \
--rm \
-v "${ROOTDIR}":/usr/code \
-e CGO_ENABLED=$(CGO_ENABLED) \
-w /usr/code/ \
$(GOIMAGE) \
go test $(TESTOPTIONS) $(REPOPATH)/http $(REPOPATH)/agency
run-v2-unit-tests:
@$(DOCKER_CMD) \
--rm \
-v "${ROOTDIR}"/v2:/usr/code \
-e CGO_ENABLED=$(CGO_ENABLED) \
-w /usr/code/ \
$(GOIMAGE) \
go test $(TESTOPTIONS) $(REPOPATH)/v2/connection
# Single server tests
run-tests-single: run-tests-single-json run-tests-single-vpack run-tests-single-vst-1.0 $(VST11_SINGLE_TESTS)
run-tests-single-json: run-tests-single-json-with-auth run-tests-single-json-no-auth run-tests-single-json-jwt-super run-tests-single-json-ssl
run-tests-single-vpack: run-tests-single-vpack-with-auth run-tests-single-vpack-no-auth run-tests-single-vpack-ssl
run-tests-single-vst-1.0: run-tests-single-vst-1.0-with-auth run-tests-single-vst-1.0-no-auth run-tests-single-vst-1.0-ssl
run-tests-single-vst-1.1: run-tests-single-vst-1.1-with-auth run-tests-single-vst-1.1-jwt-auth run-tests-single-vst-1.1-no-auth run-tests-single-vst-1.1-ssl run-tests-single-vst-1.1-jwt-ssl
run-tests-single-json-no-auth:
@echo "Single server, HTTP+JSON, no authentication"
@${MAKE} TEST_MODE="single" TEST_AUTH="none" TEST_CONTENT_TYPE="json" __run_tests
run-tests-single-vpack-no-auth:
@echo "Single server, HTTP+Velocypack, no authentication"
@${MAKE} TEST_MODE="single" TEST_AUTH="none" TEST_CONTENT_TYPE="vpack" __run_tests
run-tests-single-vst-1.0-no-auth:
@echo "Single server, Velocystream 1.0, no authentication"
@${MAKE} TEST_MODE="single" TEST_AUTH="none" TEST_CONNECTION="vst" TEST_CVERSION="1.0" __run_tests
run-tests-single-vst-1.1-no-auth:
@echo "Single server, Velocystream 1.1, no authentication"
@${MAKE} TEST_MODE="single" TEST_AUTH="none" TEST_CONNECTION="vst" TEST_CVERSION="1.1" __run_tests
run-tests-single-json-with-auth:
@echo "Single server, HTTP+JSON, with authentication"
@${MAKE} TEST_MODE="single" TEST_AUTH="rootpw" TEST_CONTENT_TYPE="json" __run_tests
run-tests-single-json-http2-with-auth:
@echo "Single server, HTTP+JSON, with authentication"
@${MAKE} TEST_MODE="single" TAGS="http2" TEST_AUTH="rootpw" TEST_CONTENT_TYPE="json" __run_tests
run-tests-single-vpack-with-auth:
@echo "Single server, HTTP+Velocypack, with authentication"
@${MAKE} TEST_MODE="single" TEST_AUTH="rootpw" TEST_CONTENT_TYPE="vpack" __run_tests
run-tests-single-vst-1.0-with-auth:
@echo "Single server, Velocystream 1.0, with authentication"
@${MAKE} TEST_MODE="single" TEST_AUTH="rootpw" TEST_CONNECTION="vst" TEST_CVERSION="1.0" __run_tests
run-tests-single-vst-1.1-with-auth:
@echo "Single server, Velocystream 1.1, with authentication"
@${MAKE} TEST_MODE="single" TEST_AUTH="rootpw" TEST_CONNECTION="vst" TEST_CVERSION="1.1" __run_tests
run-tests-single-vst-1.1-jwt-auth:
@echo "Single server, Velocystream 1.1, JWT authentication"
@${MAKE} TEST_MODE="single" TEST_AUTH="jwt" TEST_CONNECTION="vst" TEST_CVERSION="1.1" __run_tests
run-tests-single-json-jwt-super:
@echo "Single server, HTTP+JSON, JWT super authentication"
@${MAKE} TEST_MODE="single" TEST_AUTH="jwtsuper" TEST_CONTENT_TYPE="json" __run_tests
run-tests-single-json-ssl:
@echo "Single server, HTTP+JSON, with authentication, SSL"
@${MAKE} TEST_MODE="single" TEST_AUTH="rootpw" TEST_SSL="auto" TEST_CONTENT_TYPE="json" __run_tests
run-tests-single-vpack-ssl:
@echo "Single server, HTTP+Velocypack, with authentication, SSL"
@${MAKE} TEST_MODE="single" TEST_AUTH="rootpw" TEST_SSL="auto" TEST_CONTENT_TYPE="vpack" __run_tests
run-tests-single-vst-1.0-ssl:
@echo "Single server, Velocystream 1.0, with authentication, SSL"
@${MAKE} TEST_MODE="single" TEST_AUTH="rootpw" TEST_SSL="auto" TEST_CONNECTION="vst" TEST_CVERSION="1.0" __run_tests
run-tests-single-vst-1.1-ssl:
@echo "Single server, Velocystream 1.1, with authentication, SSL"
@${MAKE} TEST_MODE="single" TEST_AUTH="rootpw" TEST_SSL="auto" TEST_CONNECTION="vst" TEST_CVERSION="1.1" __run_tests
run-tests-single-vst-1.1-jwt-ssl:
@echo "Single server, Velocystream 1.1, JWT authentication, SSL"
@${MAKE} TEST_MODE="single" TEST_AUTH="jwt" TEST_SSL="auto" TEST_CONNECTION="vst" TEST_CVERSION="1.1" __run_tests
# ResilientSingle server tests
run-tests-resilientsingle: run-tests-resilientsingle-json run-tests-resilientsingle-vpack run-tests-resilientsingle-vst-1.0 $(VST11_RESILIENTSINGLE_TESTS)
run-tests-resilientsingle-json: run-tests-resilientsingle-json-with-auth run-tests-resilientsingle-json-no-auth
run-tests-resilientsingle-vpack: run-tests-resilientsingle-vpack-with-auth run-tests-resilientsingle-vpack-no-auth
run-tests-resilientsingle-vst-1.0: run-tests-resilientsingle-vst-1.0-with-auth run-tests-resilientsingle-vst-1.0-no-auth
run-tests-resilientsingle-vst-1.1: run-tests-resilientsingle-vst-1.1-with-auth run-tests-resilientsingle-vst-1.1-jwt-auth run-tests-resilientsingle-vst-1.1-no-auth
run-tests-resilientsingle-json-no-auth:
@echo "Resilient Single server, HTTP+JSON, no authentication"
@${MAKE} TEST_MODE="resilientsingle" TEST_AUTH="none" TEST_CONTENT_TYPE="json" __run_tests
run-tests-resilientsingle-vpack-no-auth:
@echo "Resilient Single server, HTTP+Velocypack, no authentication"
@${MAKE} TEST_MODE="resilientsingle" TEST_AUTH="none" TEST_CONTENT_TYPE="vpack" __run_tests
run-tests-resilientsingle-vst-1.0-no-auth:
@echo "Resilient Single server, Velocystream 1.0, no authentication"
@${MAKE} TEST_MODE="resilientsingle" TEST_AUTH="none" TEST_CONNECTION="vst" TEST_CVERSION="1.0" __run_tests
run-tests-resilientsingle-vst-1.1-no-auth:
@echo "Resilient Single server, Velocystream 1.1, no authentication"
@${MAKE} TEST_MODE="resilientsingle" TEST_AUTH="none" TEST_CONNECTION="vst" TEST_CVERSION="1.1" __run_tests
run-tests-resilientsingle-json-with-auth:
@echo "Resilient Single server, HTTP+JSON, with authentication"
@${MAKE} TEST_MODE="resilientsingle" TEST_AUTH="rootpw" TEST_CONTENT_TYPE="json" __run_tests
run-tests-resilientsingle-vpack-with-auth:
@echo "Resilient Single server, HTTP+Velocypack, with authentication"
@${MAKE} TEST_MODE="resilientsingle" TEST_AUTH="rootpw" TEST_CONTENT_TYPE="vpack" __run_tests
run-tests-resilientsingle-vst-1.0-with-auth:
@echo "Resilient Single server, Velocystream 1.0, with authentication"
@${MAKE} TEST_MODE="resilientsingle" TEST_AUTH="rootpw" TEST_CONNECTION="vst" TEST_CVERSION="1.0" __run_tests
run-tests-resilientsingle-vst-1.1-with-auth:
@echo "Resilient Single server, Velocystream 1.1, with authentication"
@${MAKE} TEST_MODE="resilientsingle" TEST_AUTH="rootpw" TEST_CONNECTION="vst" TEST_CVERSION="1.1" __run_tests
run-tests-resilientsingle-vst-1.1-jwt-auth:
@echo "Resilient Single server, Velocystream 1.1, JWT authentication"
@${MAKE} TEST_MODE="resilientsingle" TEST_AUTH="jwt" TEST_CONNECTION="vst" TEST_CVERSION="1.1" __run_tests
# Cluster mode tests
run-tests-cluster: run-tests-cluster-json run-tests-cluster-vpack run-tests-cluster-vst-1.0 $(VST11_CLUSTER_TESTS)
run-tests-cluster-json: run-tests-cluster-json-no-auth run-tests-cluster-json-with-auth run-tests-cluster-json-ssl
run-tests-cluster-vpack: run-tests-cluster-vpack-no-auth run-tests-cluster-vpack-with-auth run-tests-cluster-vpack-ssl
run-tests-cluster-vst-1.0: run-tests-cluster-vst-1.0-no-auth run-tests-cluster-vst-1.0-with-auth run-tests-cluster-vst-1.0-ssl
run-tests-cluster-vst-1.1: run-tests-cluster-vst-1.1-no-auth run-tests-cluster-vst-1.1-with-auth run-tests-cluster-vst-1.1-ssl
run-tests-cluster-json-no-auth:
@echo "Cluster server, JSON, no authentication"
@${MAKE} TEST_MODE="cluster" TEST_AUTH="none" TEST_CONTENT_TYPE="json" __run_tests
run-tests-cluster-vpack-no-auth:
@echo "Cluster server, Velocypack, no authentication"
@${MAKE} TEST_MODE="cluster" TEST_AUTH="none" TEST_CONTENT_TYPE="vpack" __run_tests
run-tests-cluster-vst-1.0-no-auth:
@echo "Cluster server, Velocystream 1.0, no authentication"
@${MAKE} TEST_MODE="cluster" TEST_AUTH="none" TEST_CONNECTION="vst" TEST_CVERSION="1.0" __run_tests
run-tests-cluster-vst-1.1-no-auth:
@echo "Cluster server, Velocystream 1.1, no authentication"
@${MAKE} TEST_MODE="cluster" TEST_AUTH="none" TEST_CONNECTION="vst" TEST_CVERSION="1.1" __run_tests
run-tests-cluster-json-with-auth:
@echo "Cluster server, with authentication"
@${MAKE} TEST_MODE="cluster" TEST_AUTH="rootpw" TEST_CONTENT_TYPE="json" __run_tests
run-tests-cluster-json-jwt-super:
@echo "Cluster server, HTTP+JSON, JWT super authentication"
@${MAKE} TEST_MODE="cluster" TEST_AUTH="jwtsuper" TEST_CONTENT_TYPE="json" __run_tests
run-tests-cluster-vpack-with-auth:
@echo "Cluster server, Velocypack, with authentication"
@${MAKE} TEST_MODE="cluster" TEST_AUTH="rootpw" TEST_CONTENT_TYPE="vpack" __run_tests
run-tests-cluster-vst-1.0-with-auth:
@echo "Cluster server, Velocystream 1.0, with authentication"
@${MAKE} TEST_MODE="cluster" TEST_AUTH="rootpw" TEST_CONNECTION="vst" TEST_CVERSION="1.0" __run_tests
run-tests-cluster-vst-1.1-with-auth:
@echo "Cluster server, Velocystream 1.1, with authentication"
@${MAKE} TEST_MODE="cluster" TEST_AUTH="rootpw" TEST_CONNECTION="vst" TEST_CVERSION="1.1" __run_tests
run-tests-cluster-json-ssl:
@echo "Cluster server, SSL, with authentication"
@${MAKE} TEST_MODE="cluster" TEST_AUTH="rootpw" TEST_SSL="auto" TEST_CONTENT_TYPE="json" __run_tests
run-tests-cluster-vpack-ssl:
@echo "Cluster server, Velocypack, SSL, with authentication"
@${MAKE} TEST_MODE="cluster" TEST_AUTH="rootpw" TEST_SSL="auto" TEST_CONTENT_TYPE="vpack" __run_tests
run-tests-cluster-vst-1.0-ssl:
@echo "Cluster server, Velocystream 1.0, SSL, with authentication"
@${MAKE} TEST_MODE="cluster" TEST_AUTH="rootpw" TEST_SSL="auto" TEST_CONNECTION="vst" TEST_CVERSION="1.0" __run_tests
run-tests-cluster-vst-1.1-ssl:
@echo "Cluster server, Velocystream 1.1, SSL, with authentication"
@${MAKE} TEST_MODE="cluster" TEST_AUTH="rootpw" TEST_SSL="auto" TEST_CONNECTION="vst" TEST_CVERSION="1.1" __run_tests
# Internal test tasks
__run_tests: __test_debug__ __test_prepare __test_go_test __test_cleanup
__test_go_test:
$(DOCKER_CMD) \
--name=$(TESTCONTAINER) \
$(TEST_NET) \
-v "${ROOTDIR}":/usr/code ${TEST_RESOURCES_VOLUME} \
-e TEST_ENDPOINTS=$(TEST_ENDPOINTS) \
-e TEST_NOT_WAIT_UNTIL_READY=$(TEST_NOT_WAIT_UNTIL_READY) \
-e TEST_AUTHENTICATION=$(TEST_AUTHENTICATION) \
-e TEST_JWTSECRET=$(TEST_JWTSECRET) \
-e TEST_CONNECTION=$(TEST_CONNECTION) \
-e TEST_CVERSION=$(TEST_CVERSION) \
-e TEST_CONTENT_TYPE=$(TEST_CONTENT_TYPE) \
-e TEST_PPROF=$(TEST_PPROF) \
-e TEST_MODE=$(TEST_MODE) \
-e TEST_BACKUP_REMOTE_REPO=$(TEST_BACKUP_REMOTE_REPO) \
-e TEST_BACKUP_REMOTE_CONFIG='$(TEST_BACKUP_REMOTE_CONFIG)' \
-e TEST_DEBUG='$(TEST_DEBUG)' \
-e TEST_ENABLE_SHUTDOWN=$(TEST_ENABLE_SHUTDOWN) \
-e TEST_REQUEST_LOG=$(TEST_REQUEST_LOG) \
-e TEST_DISALLOW_UNKNOWN_FIELDS=$(TEST_DISALLOW_UNKNOWN_FIELDS) \
-e GODEBUG=tls13=1 \
-e CGO_ENABLED=$(CGO_ENABLED) \
-w /usr/code/ \
$(DOCKER_RUN_CMD) && echo "success!" || \
{ echo "failure! \n\nARANGODB-STARTER logs:"; docker logs ${TESTCONTAINER}-s; \
echo "\nARANGODB logs:"; docker ps -f name=${TESTCONTAINER}-s- -q | xargs -L 1 docker logs; exit 1; }
# Internal test tasks
__run_v2_tests: __test_v2_debug__ __test_prepare __test_v2_go_test __test_cleanup
__test_v2_go_test:
$(DOCKER_CMD) \
--name=$(TESTCONTAINER) \
$(TEST_NET) \
-v "${ROOTDIR}":/usr/code:ro ${TEST_RESOURCES_VOLUME} \
-e TEST_ENDPOINTS=$(TEST_ENDPOINTS) \
-e TEST_NOT_WAIT_UNTIL_READY=$(TEST_NOT_WAIT_UNTIL_READY) \
-e TEST_AUTHENTICATION=$(TEST_AUTHENTICATION) \
-e TEST_JWTSECRET=$(TEST_JWTSECRET) \
-e TEST_MODE=$(TEST_MODE) \
-e TEST_BACKUP_REMOTE_REPO=$(TEST_BACKUP_REMOTE_REPO) \
-e TEST_BACKUP_REMOTE_CONFIG='$(TEST_BACKUP_REMOTE_CONFIG)' \
-e TEST_DEBUG='$(TEST_DEBUG)' \
-e TEST_ENABLE_SHUTDOWN=$(TEST_ENABLE_SHUTDOWN) \
-e GODEBUG=tls13=1 \
-e CGO_ENABLED=$(CGO_ENABLED) \
-w /usr/code/v2/ \
$(DOCKER_V2_RUN_CMD) && echo "success!" || \
{ echo "failure! \n\nARANGODB-STARTER logs:"; docker logs ${TESTCONTAINER}-s; \
echo "\nARANGODB logs:"; docker ps -f name=${TESTCONTAINER}-s- -q | xargs -L 1 docker logs; exit 1; }
__test_debug__:
ifeq ("$(DEBUG)", "true")
@docker build -f Dockerfile.debug --build-arg "TESTS_DIRECTORY=./test" -t $(GOIMAGE) .
endif
__test_v2_debug__:
ifeq ("$(DEBUG)", "true")
@docker build -f Dockerfile.debug --build-arg "TESTS_DIRECTORY=./tests" --build-arg "TESTS_ROOT_PATH=v2" -t $(GOIMAGE) .
endif
__dir_setup:
@mkdir -p "${TMPDIR}"
@echo "${TMPDIR}"
__test_prepare: __dir_setup
ifdef TEST_ENDPOINTS_OVERRIDE
@-docker rm -f -v $(TESTCONTAINER) &> /dev/null
@sleep 3
else
ifdef JWTSECRET
echo "$JWTSECRET" > "${JWTSECRETFILE}"
endif
@-docker rm -f -v $(TESTCONTAINER) &> /dev/null
@TESTCONTAINER=$(TESTCONTAINER) ARANGODB=$(ARANGODB) ALPINE_IMAGE=$(ALPINE_IMAGE) ENABLE_BACKUP=$(ENABLE_BACKUP) ARANGO_LICENSE_KEY=$(ARANGO_LICENSE_KEY) STARTER=$(STARTER) STARTERMODE=$(TEST_MODE) TMPDIR="${TMPDIR}" DEBUG_PORT=$(DEBUG_PORT) $(CLUSTERENV) "${ROOTDIR}/test/cluster.sh" start
endif
__test_cleanup:
ifdef TESTCONTAINER
@TESTCONTAINERS=$$(docker ps -a -q --filter="name=$(TESTCONTAINER)")
@if [ -n "$$TESTCONTAINERS" ]; then docker rm -f -v $$(docker ps -a -q --filter="name=$(TESTCONTAINER)"); fi
endif
ifndef TEST_ENDPOINTS_OVERRIDE
@TESTCONTAINER=$(TESTCONTAINER) ARANGODB=$(ARANGODB) ALPINE_IMAGE=$(ALPINE_IMAGE) STARTER=$(STARTER) STARTERMODE=$(TEST_MODE) "${ROOTDIR}/test/cluster.sh" cleanup
else
@-docker rm -f -v $(TESTCONTAINER) &> /dev/null
endif
@sleep 3
run-tests-cluster-failover:
# Note that we use 127.0.0.1:7001.. as endpoints, so we force using IPv4
# This is essential since we only block IPv4 ports in the test.
@echo "Cluster server, failover, no authentication"
@TESTCONTAINER=$(TESTCONTAINER) ARANGODB=$(ARANGODB) ALPINE_IMAGE=$(ALPINE_IMAGE) "${ROOTDIR}/test/cluster.sh" start
go get github.com/coreos/go-iptables/iptables
$(DOCKER_CMD) \
--rm \
$(TEST_NET) \
--privileged \
-v "${ROOTDIR}":/usr/code \
-e TEST_ENDPOINTS=http://127.0.0.1:7001,http://127.0.0.1:7006,http://127.0.0.1:7011 \
-e TEST_NOT_WAIT_UNTIL_READY=$(TEST_NOT_WAIT_UNTIL_READY) \
-e TEST_AUTHENTICATION=basic:root: \
-e GODEBUG=tls13=1 \
-w /usr/code/ \
golang:$(GOVERSION) \
go test -run ".*Failover.*" -tags failover $(TESTOPTIONS) $(REPOPATH)/test
@TESTCONTAINER=$(TESTCONTAINER) ARANGODB=$(ARANGODB) ALPINE_IMAGE=$(ALPINE_IMAGE) "${ROOTDIR}/test/cluster.sh" cleanup
run-tests-cluster-cleanup:
@TESTCONTAINER=$(TESTCONTAINER) ARANGODB=$(ARANGODB) ALPINE_IMAGE=$(ALPINE_IMAGE) "${ROOTDIR}/test/cluster.sh" cleanup
# Benchmarks
run-benchmarks-single-json-no-auth:
@echo "Benchmarks: Single server, JSON no authentication"
@${MAKE} TEST_MODE="single" TEST_AUTH="none" TEST_CONTENT_TYPE="json" TEST_BENCHMARK="true" __run_tests
run-benchmarks-single-vpack-no-auth:
@echo "Benchmarks: Single server, Velocypack, no authentication"
@${MAKE} TEST_MODE="single" TEST_AUTH="none" TEST_CONTENT_TYPE="vpack" TEST_BENCHMARK="true" __run_tests
## Lint
.PHONY: tools
tools: __dir_setup
@echo ">> Fetching golangci-lint linter"
@GOBIN=$(TMPDIR)/bin go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.50.1
@echo ">> Fetching goimports"
@GOBIN=$(TMPDIR)/bin go install golang.org/x/tools/cmd/goimports@v0.1.12
@echo ">> Fetching license check"
@GOBIN=$(TMPDIR)/bin go install github.com/google/addlicense@v1.0.0
.PHONY: license
license:
@echo ">> Ensuring license of files"
@$(TMPDIR)/bin/addlicense -f "$(ROOTDIR)/HEADER" $(SOURCES)
.PHONY: license-verify
license-verify:
@echo ">> Verify license of files"
@$(TMPDIR)/bin/addlicense -f "$(ROOTDIR)/HEADER" -check $(SOURCES)
.PHONY: fmt
fmt:
@echo ">> Ensuring style of files"
@$(TMPDIR)/bin/goimports -w $(SOURCES)
.PHONY: fmt-verify
fmt-verify: license-verify
@echo ">> Verify files style"
@if [ X"$$($(TMPDIR)/bin/goimports -l $(SOURCES) | wc -l)" != X"0" ]; then echo ">> Style errors"; $(TMPDIR)/bin/goimports -l $(SOURCES); exit 1; fi
.PHONY: linter
linter: fmt-verify
@$(TMPDIR)/bin/golangci-lint run ./...
# V2
v2-%:
@(cd "$(ROOTDIR)/v2"; make)
run-v2-tests: run-v2-tests-single run-v2-tests-cluster run-v2-tests-resilientsingle
run-v2-tests-cluster: run-v2-tests-cluster-with-basic-auth run-v2-tests-cluster-without-ssl run-v2-tests-cluster-without-auth run-v2-tests-cluster-with-jwt-auth
run-v2-tests-cluster-with-basic-auth:
@echo "Cluster server, with basic authentication, v2"
@${MAKE} TEST_MODE="cluster" TEST_SSL="auto" TEST_AUTH="rootpw" __run_v2_tests
run-v2-tests-cluster-with-jwt-auth:
@echo "Cluster server, with JWT authentication, v2"
@${MAKE} TEST_MODE="cluster" TEST_SSL="auto" TEST_AUTH="jwt" __run_v2_tests
run-v2-tests-cluster-without-auth:
@echo "Cluster server, without authentication, v2"
@${MAKE} TEST_MODE="cluster" TEST_SSL="auto" TEST_AUTH="none" __run_v2_tests
run-v2-tests-cluster-without-ssl:
@echo "Cluster server, without authentication and SSL, v2"
@${MAKE} TEST_MODE="cluster" TEST_AUTH="none" __run_v2_tests
run-v2-tests-single: run-v2-tests-single-without-auth run-v2-tests-single-with-auth
run-v2-tests-single-without-auth:
@echo "Single server, without authentication, v2"
@${MAKE} TEST_MODE="single" TEST_AUTH="none" __run_v2_tests
run-v2-tests-single-with-auth:
@echo "Single server, with authentication, v2"
@${MAKE} TEST_MODE="single" TEST_SSL="auto" TEST_AUTH="rootpw" __run_v2_tests
run-v2-tests-resilientsingle: run-v2-tests-resilientsingle-with-auth
run-v2-tests-resilientsingle-with-auth:
@echo "Resilient Single, with authentication, v2"
@${MAKE} TEST_MODE="resilientsingle" TEST_AUTH="rootpw" __run_v2_tests

@ -0,0 +1,39 @@
![ArangoDB-Logo](https://user-images.githubusercontent.com/3998723/207981337-79d49127-48fc-4c7c-9411-8a688edca1dd.png)
# ArangoDB GO Driver
This project contains the official Go driver for the [ArangoDB database](https://arangodb.com).
[![Build Status](https://travis-ci.org/arangodb/go-driver.svg?branch=master)](https://travis-ci.org/arangodb/go-driver)
[![GoDoc](https://godoc.org/github.com/arangodb/go-driver?status.svg)](http://godoc.org/github.com/arangodb/go-driver)
- [Getting Started](https://www.arangodb.com/docs/stable/drivers/go-getting-started.html)
- [Example Requests](https://www.arangodb.com/docs/stable/drivers/go-example-requests.html)
- [Connection Management](https://www.arangodb.com/docs/stable/drivers/go-connection-management.html)
- [Reference](https://godoc.org/github.com/arangodb/go-driver)
# Supported Go Versions
| | Go 1.13 | Go 1.14 | Go 1.16 | Go 1.17 |
|---------------|---------|---------|---------|---------|
| `1.0.0-1.4.0` | ✓ | ✓ | ✓ | ✓ |
| `master` | ✓ | ✓ | ✓ | ✓ |
# Supported Versions
| | < ArangoDB 3.6 | ArangoDB 3.6 | ArangoDB 3.7 | ArangoDB 3.8 | ArangoDB 3.9 | ArangoDB 3.10 |
|----------|----------------|--------------|--------------|--------------|--------------|---------------|
| `1.0.0` | ✓ | ✓ | - | - | - | - |
| `1.1.0` | + | + | ✓ | - | - | - |
| `1.2.1` | + | + | ✓ | ✓ | - | - |
| `1.3.0` | + | + | ✓ | ✓ | ✓ | - |
| `1.4.0` | + | + | ✓ | ✓ | ✓ | ✓ |
| `master` | + | + | + | + | + | + |
Key:
* `✓` Exactly the same features in both driver and the ArangoDB version.
* `+` Features included in driver may be not present in the ArangoDB API. Calls to the ArangoDB may result in unexpected responses (404).
* `-` The ArangoDB has features which are not supported by driver.

@ -0,0 +1,114 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
type AuthenticationType int
const (
// AuthenticationTypeBasic uses username+password basic authentication
AuthenticationTypeBasic AuthenticationType = iota
// AuthenticationTypeJWT uses username+password JWT token based authentication
AuthenticationTypeJWT
// AuthenticationTypeRaw uses a raw value for the Authorization header
AuthenticationTypeRaw
)
// Authentication implements a kind of authentication.
type Authentication interface {
// Returns the type of authentication
Type() AuthenticationType
// Get returns a configuration property of the authentication.
// Supported properties depend on type of authentication.
Get(property string) string
}
// BasicAuthentication creates an authentication implementation based on the given username & password.
func BasicAuthentication(userName, password string) Authentication {
return &userNameAuthentication{
authType: AuthenticationTypeBasic,
userName: userName,
password: password,
}
}
// JWTAuthentication creates a JWT token authentication implementation based on the given username & password.
func JWTAuthentication(userName, password string) Authentication {
return &userNameAuthentication{
authType: AuthenticationTypeJWT,
userName: userName,
password: password,
}
}
// basicAuthentication implements HTTP Basic authentication.
type userNameAuthentication struct {
authType AuthenticationType
userName string
password string
}
// Returns the type of authentication
func (a *userNameAuthentication) Type() AuthenticationType {
return a.authType
}
// Get returns a configuration property of the authentication.
// Supported properties depend on type of authentication.
func (a *userNameAuthentication) Get(property string) string {
switch property {
case "username":
return a.userName
case "password":
return a.password
default:
return ""
}
}
// RawAuthentication creates a raw authentication implementation based on the given value for the Authorization header.
func RawAuthentication(value string) Authentication {
return &rawAuthentication{
value: value,
}
}
// rawAuthentication implements Raw authentication.
type rawAuthentication struct {
value string
}
// Returns the type of authentication
func (a *rawAuthentication) Type() AuthenticationType {
return AuthenticationTypeRaw
}
// Get returns a configuration property of the authentication.
// Supported properties depend on type of authentication.
func (a *rawAuthentication) Get(property string) string {
switch property {
case "value":
return a.value
default:
return ""
}
}

@ -0,0 +1,124 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
"fmt"
"sort"
"strings"
"time"
)
// Client provides access to a single ArangoDB database server, or an entire cluster of ArangoDB servers.
type Client interface {
// SynchronizeEndpoints fetches all endpoints from an ArangoDB cluster and updates the
// connection to use those endpoints.
// When this client is connected to a single server, nothing happens.
// When this client is connected to a cluster of servers, the connection will be updated to reflect
// the layout of the cluster.
// This function requires ArangoDB 3.1.15 or up.
SynchronizeEndpoints(ctx context.Context) error
// SynchronizeEndpoints2 fetches all endpoints from an ArangoDB cluster and updates the
// connection to use those endpoints.
// When this client is connected to a single server, nothing happens.
// When this client is connected to a cluster of servers, the connection will be updated to reflect
// the layout of the cluster.
// Compared to SynchronizeEndpoints, this function expects a database name as additional parameter.
// This database name is used to call `_db/<dbname>/_api/cluster/endpoints`. SynchronizeEndpoints uses
// the default database, i.e. `_system`. In the case the user does not have access to `_system`,
// SynchronizeEndpoints does not work with earlier versions of arangodb.
SynchronizeEndpoints2(ctx context.Context, dbname string) error
// Connection returns the connection used by this client
Connection() Connection
// Database functions
ClientDatabases
// User functions
ClientUsers
// Cluster functions
ClientCluster
// Individual server information functions
ClientServerInfo
// Server/cluster administration functions
ClientServerAdmin
// Replication functions
ClientReplication
// Backup functions
ClientAdminBackup
ClientFoxx
}
// ClientConfig contains all settings needed to create a client.
type ClientConfig struct {
// Connection is the actual server/cluster connection.
// See http.NewConnection.
Connection Connection
// Authentication implements authentication on the server.
Authentication Authentication
// SynchronizeEndpointsInterval is the interval between automatic synchronization of endpoints.
// If this value is 0, no automatic synchronization is performed.
// If this value is > 0, automatic synchronization is started on a go routine.
// This feature requires ArangoDB 3.1.15 or up.
SynchronizeEndpointsInterval time.Duration
}
// VersionInfo describes the version of a database server.
type VersionInfo struct {
// This will always contain "arango"
Server string `json:"server,omitempty"`
// The server version string. The string has the format "major.minor.sub".
// Major and minor will be numeric, and sub may contain a number or a textual version.
Version Version `json:"version,omitempty"`
// Type of license of the server
License string `json:"license,omitempty"`
// Optional additional details. This is returned only if the context is configured using WithDetails.
Details map[string]interface{} `json:"details,omitempty"`
}
func (v *VersionInfo) IsEnterprise() bool {
return v.License == "enterprise"
}
// String creates a string representation of the given VersionInfo.
func (v VersionInfo) String() string {
result := fmt.Sprintf("%s, version %s, license %s", v.Server, v.Version, v.License)
if len(v.Details) > 0 {
lines := make([]string, 0, len(v.Details))
for k, v := range v.Details {
lines = append(lines, fmt.Sprintf("%s: %v", k, v))
}
sort.Strings(lines)
result = result + "\n" + strings.Join(lines, "\n")
}
return result
}

@ -0,0 +1,149 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Lars Maier
//
package driver
import (
"context"
"time"
)
// BackupMeta provides meta data of a backup
type BackupMeta struct {
ID BackupID `json:"id,omitempty"`
Version string `json:"version,omitempty"`
DateTime time.Time `json:"datetime,omitempty"`
NumberOfFiles uint `json:"nrFiles,omitempty"`
NumberOfDBServers uint `json:"nrDBServers,omitempty"`
SizeInBytes uint64 `json:"sizeInBytes,omitempty"`
PotentiallyInconsistent bool `json:"potentiallyInconsistent,omitempty"`
Available bool `json:"available,omitempty"`
NumberOfPiecesPresent uint `json:"nrPiecesPresent,omitempty"`
Keys []BackupMetaSha256 `json:"keys,omitempty"`
}
// BackupMetaSha256 backup sha details
type BackupMetaSha256 struct {
SHA256 string `json:"sha256"`
}
// BackupRestoreOptions provides options for Restore
type BackupRestoreOptions struct {
// do not version check when doing a restore (expert only)
IgnoreVersion bool `json:"ignoreVersion,omitempty"`
}
// BackupListOptions provides options for List
type BackupListOptions struct {
// Only receive meta data about a specific id
ID BackupID `json:"id,omitempty"`
}
// BackupCreateOptions provides options for Create
type BackupCreateOptions struct {
Label string `json:"label,omitempty"`
AllowInconsistent bool `json:"allowInconsistent,omitempty"`
Timeout time.Duration `json:"timeout,omitempty"`
}
// BackupTransferStatus represents all possible states a transfer job can be in
type BackupTransferStatus string
const (
TransferAcknowledged BackupTransferStatus = "ACK"
TransferStarted BackupTransferStatus = "STARTED"
TransferCompleted BackupTransferStatus = "COMPLETED"
TransferFailed BackupTransferStatus = "FAILED"
TransferCancelled BackupTransferStatus = "CANCELLED"
)
// BackupTransferReport provides progress information of a backup transfer job for a single dbserver
type BackupTransferReport struct {
Status BackupTransferStatus `json:"Status,omitempty"`
Error int `json:"Error,omitempty"`
ErrorMessage string `json:"ErrorMessage,omitempty"`
Progress struct {
Total int `json:"Total,omitempty"`
Done int `json:"Done,omitempty"`
Timestamp string `json:"Timestamp,omitempty"`
} `json:"Progress,omitempty"`
}
// BackupTransferProgressReport provides progress information for a backup transfer job
type BackupTransferProgressReport struct {
BackupID BackupID `json:"BackupID,omitempty"`
Cancelled bool `json:"Cancelled,omitempty"`
Timestamp string `json:"Timestamp,omitempty"`
DBServers map[string]BackupTransferReport `json:"DBServers,omitempty"`
}
// BackupTransferJobID represents a Transfer (upload/download) job
type BackupTransferJobID string
// BackupID identifies a backup
type BackupID string
// ClientAdminBackup provides access to the Backup API via the Client interface
type ClientAdminBackup interface {
Backup() ClientBackup
}
// BackupCreateResponse contains information about a newly created backup
type BackupCreateResponse struct {
NumberOfFiles uint
NumberOfDBServers uint
SizeInBytes uint64
PotentiallyInconsistent bool
CreationTime time.Time
}
// ClientBackup provides access to server/cluster backup functions of an arangodb database server
// or an entire cluster of arangodb servers.
type ClientBackup interface {
// Create creates a new backup and returns its id
Create(ctx context.Context, opt *BackupCreateOptions) (BackupID, BackupCreateResponse, error)
// Delete deletes the backup with given id
Delete(ctx context.Context, id BackupID) error
// Restore restores the backup with given id
Restore(ctx context.Context, id BackupID, opt *BackupRestoreOptions) error
// List returns meta data about some/all backups available
List(ctx context.Context, opt *BackupListOptions) (map[BackupID]BackupMeta, error)
// only enterprise version
// Upload triggers an upload to the remote repository of backup with id using the given config
// and returns the job id.
Upload(ctx context.Context, id BackupID, remoteRepository string, config interface{}) (BackupTransferJobID, error)
// Download triggers an download to the remote repository of backup with id using the given config
// and returns the job id.
Download(ctx context.Context, id BackupID, remoteRepository string, config interface{}) (BackupTransferJobID, error)
// Progress returns the progress state of the given Transfer job
Progress(ctx context.Context, job BackupTransferJobID) (BackupTransferProgressReport, error)
// Abort aborts the Transfer job if possible
Abort(ctx context.Context, job BackupTransferJobID) error
}

@ -0,0 +1,305 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Lars Maier
//
package driver
import (
"context"
"time"
)
type clientBackup struct {
conn Connection
}
func (c *client) Backup() ClientBackup {
return &clientBackup{
conn: c.conn,
}
}
// Create creates a new backup and returns its id
func (c *clientBackup) Create(ctx context.Context, opt *BackupCreateOptions) (BackupID, BackupCreateResponse, error) {
req, err := c.conn.NewRequest("POST", "_admin/backup/create")
if err != nil {
return "", BackupCreateResponse{}, WithStack(err)
}
applyContextSettings(ctx, req)
if opt != nil {
body := struct {
Label string `json:"label,omitempty"`
AllowInconsistent bool `json:"allowInconsistent,omitempty"`
Timeout float64 `json:"timeout,omitempty"`
}{
Label: opt.Label,
AllowInconsistent: opt.AllowInconsistent,
Timeout: opt.Timeout.Seconds(),
}
req, err = req.SetBody(body)
if err != nil {
return "", BackupCreateResponse{}, WithStack(err)
}
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return "", BackupCreateResponse{}, WithStack(err)
}
if err := resp.CheckStatus(201); err != nil {
return "", BackupCreateResponse{}, WithStack(err)
}
var result struct {
ID BackupID `json:"id,omitempty"`
PotentiallyInconsistent bool `json:"potentiallyInconsistent,omitempty"`
NumberOfFiles uint `json:"nrFiles,omitempty"`
NumberOfDBServers uint `json:"nrDBServers,omitempty"`
SizeInBytes uint64 `json:"sizeInBytes,omitempty"`
CreationTime time.Time `json:"datetime,omitempty"`
}
if err := resp.ParseBody("result", &result); err != nil {
return "", BackupCreateResponse{}, WithStack(err)
}
return result.ID, BackupCreateResponse{
PotentiallyInconsistent: result.PotentiallyInconsistent,
NumberOfFiles: result.NumberOfFiles,
NumberOfDBServers: result.NumberOfDBServers,
SizeInBytes: result.SizeInBytes,
CreationTime: result.CreationTime,
}, nil
}
// Delete deletes the backup with given id
func (c *clientBackup) Delete(ctx context.Context, id BackupID) error {
req, err := c.conn.NewRequest("POST", "_admin/backup/delete")
if err != nil {
return WithStack(err)
}
applyContextSettings(ctx, req)
body := struct {
ID BackupID `json:"id,omitempty"`
}{
ID: id,
}
req, err = req.SetBody(body)
if err != nil {
return WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return WithStack(err)
}
return nil
}
// Restore restores the backup with given id
func (c *clientBackup) Restore(ctx context.Context, id BackupID, opt *BackupRestoreOptions) error {
req, err := c.conn.NewRequest("POST", "_admin/backup/restore")
if err != nil {
return WithStack(err)
}
applyContextSettings(ctx, req)
body := struct {
ID BackupID `json:"id,omitempty"`
IgnoreVersion bool `json:"ignoreVersion,omitempty"`
}{
ID: id,
}
if opt != nil {
body.IgnoreVersion = opt.IgnoreVersion
}
req, err = req.SetBody(body)
if err != nil {
return WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
// THIS SHOULD BE 202 ACCEPTED and not OK, because it is not completed when returns (at least for single server)
if err := resp.CheckStatus(200); err != nil {
return WithStack(err)
}
return nil
}
// List returns meta data about some/all backups available
func (c *clientBackup) List(ctx context.Context, opt *BackupListOptions) (map[BackupID]BackupMeta, error) {
req, err := c.conn.NewRequest("POST", "_admin/backup/list")
if err != nil {
return nil, WithStack(err)
}
applyContextSettings(ctx, req)
if opt != nil {
req, err = req.SetBody(opt)
if err != nil {
return nil, WithStack(err)
}
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
var result struct {
List map[BackupID]BackupMeta `json:"list,omitempty"`
}
if err := resp.ParseBody("result", &result); err != nil {
return nil, WithStack(err)
}
return result.List, nil
}
// Upload triggers an upload to the remote repository of backup with id using the given config
// and returns the job id.
func (c *clientBackup) Upload(ctx context.Context, id BackupID, remoteRepository string, config interface{}) (BackupTransferJobID, error) {
req, err := c.conn.NewRequest("POST", "_admin/backup/upload")
if err != nil {
return "", WithStack(err)
}
applyContextSettings(ctx, req)
body := struct {
ID BackupID `json:"id,omitempty"`
RemoteRepo string `json:"remoteRepository,omitempty"`
Config interface{} `json:"config,omitempty"`
}{
ID: id,
RemoteRepo: remoteRepository,
Config: config,
}
req, err = req.SetBody(body)
if err != nil {
return "", WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return "", WithStack(err)
}
if err := resp.CheckStatus(202); err != nil {
return "", WithStack(err)
}
var result struct {
UploadID BackupTransferJobID `json:"uploadId,omitempty"`
}
if err := resp.ParseBody("result", &result); err != nil {
return "", WithStack(err)
}
return result.UploadID, nil
}
// Download triggers an download to the remote repository of backup with id using the given config
// and returns the job id.
func (c *clientBackup) Download(ctx context.Context, id BackupID, remoteRepository string, config interface{}) (BackupTransferJobID, error) {
req, err := c.conn.NewRequest("POST", "_admin/backup/download")
if err != nil {
return "", WithStack(err)
}
applyContextSettings(ctx, req)
body := struct {
ID BackupID `json:"id,omitempty"`
RemoteRepo string `json:"remoteRepository,omitempty"`
Config interface{} `json:"config,omitempty"`
}{
ID: id,
RemoteRepo: remoteRepository,
Config: config,
}
req, err = req.SetBody(body)
if err != nil {
return "", WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return "", WithStack(err)
}
if err := resp.CheckStatus(202); err != nil {
return "", WithStack(err)
}
var result struct {
DownloadID BackupTransferJobID `json:"downloadId,omitempty"`
}
if err := resp.ParseBody("result", &result); err != nil {
return "", WithStack(err)
}
return result.DownloadID, nil
}
// Progress returns the progress state of the given Transfer job
func (c *clientBackup) Progress(ctx context.Context, job BackupTransferJobID) (result BackupTransferProgressReport, error error) {
req, err := c.conn.NewRequest("POST", "_admin/backup/upload")
if err != nil {
return BackupTransferProgressReport{}, WithStack(err)
}
applyContextSettings(ctx, req)
body := struct {
ID BackupTransferJobID `json:"uploadId,omitempty"`
}{
ID: job,
}
req, err = req.SetBody(body)
if err != nil {
return BackupTransferProgressReport{}, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return BackupTransferProgressReport{}, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return BackupTransferProgressReport{}, WithStack(err)
}
if err := resp.ParseBody("result", &result); err != nil {
return BackupTransferProgressReport{}, WithStack(err)
}
return result, nil
}
// Abort aborts the Transfer job if possible
func (c *clientBackup) Abort(ctx context.Context, job BackupTransferJobID) error {
req, err := c.conn.NewRequest("POST", "_admin/backup/upload")
if err != nil {
return WithStack(err)
}
applyContextSettings(ctx, req)
body := struct {
ID BackupTransferJobID `json:"uploadId,omitempty"`
Abort bool `json:"abort,omitempty"`
}{
ID: job,
Abort: true,
}
req, err = req.SetBody(body)
if err != nil {
return WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(202); err != nil {
return WithStack(err)
}
return nil
}

@ -0,0 +1,33 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import "context"
// ClientCluster provides methods needed to access cluster functionality from a client.
type ClientCluster interface {
// Cluster provides access to cluster wide specific operations.
// To use this interface, an ArangoDB cluster is required.
// If this method is a called without a cluster, a PreconditionFailed error is returned.
Cluster(ctx context.Context) (Cluster, error)
}

@ -0,0 +1,46 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
)
// Cluster provides access to cluster wide specific operations.
// To use this interface, an ArangoDB cluster is required.
// If this method is a called without a cluster, a PreconditionFailed error is returned.
func (c *client) Cluster(ctx context.Context) (Cluster, error) {
role, err := c.ServerRole(ctx)
if err != nil {
return nil, WithStack(err)
}
if role == ServerRoleSingle || role == ServerRoleSingleActive || role == ServerRoleSinglePassive {
// Standalone server, this is wrong
return nil, WithStack(newArangoError(412, 0, "Cluster expected, found SINGLE server"))
}
cl, err := newCluster(c.conn)
if err != nil {
return nil, WithStack(err)
}
return cl, nil
}

@ -0,0 +1,85 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import "context"
type DatabaseSharding string
const (
DatabaseShardingSingle DatabaseSharding = "single"
DatabaseShardingNone DatabaseSharding = ""
)
// ClientDatabases provides access to the databases in a single arangodb database server, or an entire cluster of arangodb servers.
type ClientDatabases interface {
// Database opens a connection to an existing database.
// If no database with given name exists, an NotFoundError is returned.
Database(ctx context.Context, name string) (Database, error)
// DatabaseExists returns true if a database with given name exists.
DatabaseExists(ctx context.Context, name string) (bool, error)
// Databases returns a list of all databases found by the client.
Databases(ctx context.Context) ([]Database, error)
// AccessibleDatabases returns a list of all databases that can be accessed by the authenticated user.
AccessibleDatabases(ctx context.Context) ([]Database, error)
// CreateDatabase creates a new database with given name and opens a connection to it.
// If the a database with given name already exists, a DuplicateError is returned.
CreateDatabase(ctx context.Context, name string, options *CreateDatabaseOptions) (Database, error)
}
// CreateDatabaseOptions contains options that customize the creating of a database.
type CreateDatabaseOptions struct {
// List of users to initially create for the new database. User information will not be changed for users that already exist.
// If users is not specified or does not contain any users, a default user root will be created with an empty string password.
// This ensures that the new database will be accessible after it is created.
Users []CreateDatabaseUserOptions `json:"users,omitempty"`
// Options database defaults
Options CreateDatabaseDefaultOptions `json:"options,omitempty"`
}
// CreateDatabaseDefaultOptions contains options that change defaults for collections
type CreateDatabaseDefaultOptions struct {
// Default replication factor for collections in database
ReplicationFactor int `json:"replicationFactor,omitempty"`
// Default write concern for collections in database
WriteConcern int `json:"writeConcern,omitempty"`
// Default sharding for collections in database
Sharding DatabaseSharding `json:"sharding,omitempty"`
}
// CreateDatabaseUserOptions contains options for creating a single user for a database.
type CreateDatabaseUserOptions struct {
// Loginname of the user to be created
UserName string `json:"user,omitempty"`
// The user password as a string. If not specified, it will default to an empty string.
Password string `json:"passwd,omitempty"`
// A flag indicating whether the user account should be activated or not. The default value is true. If set to false, the user won't be able to log into the database.
Active *bool `json:"active,omitempty"`
// A JSON object with extra user information. The data contained in extra will be stored for the user but not be interpreted further by ArangoDB.
Extra interface{} `json:"extra,omitempty"`
}

@ -0,0 +1,154 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
"path"
)
// Database opens a connection to an existing database.
// If no database with given name exists, an NotFoundError is returned.
func (c *client) Database(ctx context.Context, name string) (Database, error) {
escapedName := pathEscape(name)
req, err := c.conn.NewRequest("GET", path.Join("_db", escapedName, "_api/database/current"))
if err != nil {
return nil, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
db, err := newDatabase(name, c.conn)
if err != nil {
return nil, WithStack(err)
}
return db, nil
}
// DatabaseExists returns true if a database with given name exists.
func (c *client) DatabaseExists(ctx context.Context, name string) (bool, error) {
escapedName := pathEscape(name)
req, err := c.conn.NewRequest("GET", path.Join("_db", escapedName, "_api/database/current"))
if err != nil {
return false, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return false, WithStack(err)
}
if err := resp.CheckStatus(200); err == nil {
return true, nil
} else if IsNotFound(err) {
return false, nil
} else {
return false, WithStack(err)
}
}
type getDatabaseResponse struct {
Result []string `json:"result,omitempty"`
ArangoError
}
// Databases returns a list of all databases found by the client.
func (c *client) Databases(ctx context.Context) ([]Database, error) {
result, err := listDatabases(ctx, c.conn, path.Join("/_db/_system/_api/database"))
if err != nil {
return nil, WithStack(err)
}
return result, nil
}
// AccessibleDatabases returns a list of all databases that can be accessed by the authenticated user.
func (c *client) AccessibleDatabases(ctx context.Context) ([]Database, error) {
result, err := listDatabases(ctx, c.conn, path.Join("/_db/_system/_api/database/user"))
if err != nil {
return nil, WithStack(err)
}
return result, nil
}
// listDatabases returns a list of databases using a GET to the given path.
func listDatabases(ctx context.Context, conn Connection, path string) ([]Database, error) {
req, err := conn.NewRequest("GET", path)
if err != nil {
return nil, WithStack(err)
}
resp, err := conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
var data getDatabaseResponse
if err := resp.ParseBody("", &data); err != nil {
return nil, WithStack(err)
}
result := make([]Database, 0, len(data.Result))
for _, name := range data.Result {
db, err := newDatabase(name, conn)
if err != nil {
return nil, WithStack(err)
}
result = append(result, db)
}
return result, nil
}
// CreateDatabase creates a new database with given name and opens a connection to it.
// If the a database with given name already exists, a DuplicateError is returned.
func (c *client) CreateDatabase(ctx context.Context, name string, options *CreateDatabaseOptions) (Database, error) {
input := struct {
CreateDatabaseOptions
Name string `json:"name"`
}{
Name: name,
}
if options != nil {
input.CreateDatabaseOptions = *options
}
req, err := c.conn.NewRequest("POST", path.Join("_db/_system/_api/database"))
if err != nil {
return nil, WithStack(err)
}
if _, err := req.SetBody(input); err != nil {
return nil, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(201); err != nil {
return nil, WithStack(err)
}
db, err := newDatabase(name, c.conn)
if err != nil {
return nil, WithStack(err)
}
return db, nil
}

@ -0,0 +1,47 @@
//
// DISCLAIMER
//
// Copyright 2020 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
//
// Author Tomasz Mielech
//
package driver
import (
"context"
)
type FoxxCreateOptions struct {
Mount string
}
type FoxxDeleteOptions struct {
Mount string
Teardown bool
}
type ClientFoxx interface {
Foxx() FoxxService
}
type FoxxService interface {
// InstallFoxxService installs a new service at a given mount path.
InstallFoxxService(ctx context.Context, zipFile string, options FoxxCreateOptions) error
// UninstallFoxxService uninstalls service at a given mount path.
UninstallFoxxService(ctx context.Context, options FoxxDeleteOptions) error
}

@ -0,0 +1,28 @@
//
// DISCLAIMER
//
// Copyright 2020 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
//
// Author Tomasz Mielech
//
package driver
// Foxx provides access to foxx services specific operations.
func (c *client) Foxx() FoxxService {
return c
}

@ -0,0 +1,153 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
"path"
"time"
"github.com/arangodb/go-driver/util"
)
// NewClient creates a new Client based on the given config setting.
func NewClient(config ClientConfig) (Client, error) {
if config.Connection == nil {
return nil, WithStack(InvalidArgumentError{Message: "Connection is not set"})
}
conn := config.Connection
if config.Authentication != nil {
var err error
conn, err = conn.SetAuthentication(config.Authentication)
if err != nil {
return nil, WithStack(err)
}
}
c := &client{
conn: conn,
}
if config.SynchronizeEndpointsInterval > 0 {
go c.autoSynchronizeEndpoints(config.SynchronizeEndpointsInterval)
}
return c, nil
}
// client implements the Client interface.
type client struct {
conn Connection
}
// Connection returns the connection used by this client
func (c *client) Connection() Connection {
return c.conn
}
// SynchronizeEndpoints fetches all endpoints from an ArangoDB cluster and updates the
// connection to use those endpoints.
// When this client is connected to a single server, nothing happens.
// When this client is connected to a cluster of servers, the connection will be updated to reflect
// the layout of the cluster.
func (c *client) SynchronizeEndpoints(ctx context.Context) error {
return c.SynchronizeEndpoints2(ctx, "")
}
// SynchronizeEndpoints2 fetches all endpoints from an ArangoDB cluster and updates the
// connection to use those endpoints.
// When this client is connected to a single server, nothing happens.
// When this client is connected to a cluster of servers, the connection will be updated to reflect
// the layout of the cluster.
// Compared to SynchronizeEndpoints, this function expects a database name as additional parameter.
// This database name is used to call `_db/<dbname>/_api/cluster/endpoints`. SynchronizeEndpoints uses
// the default database, i.e. `_system`. In the case the user does not have access to `_system`,
// SynchronizeEndpoints does not work with earlier versions of arangodb.
func (c *client) SynchronizeEndpoints2(ctx context.Context, dbname string) error {
// Cluster mode, fetch endpoints
cep, err := c.clusterEndpoints(ctx, dbname)
if err != nil {
// ignore Forbidden: automatic failover is not enabled errors
if !IsArangoErrorWithErrorNum(err, ErrHttpForbidden, ErrHttpInternal, 0, ErrNotImplemented, ErrForbidden) {
// 3.2 returns no error code, thus check for 0
// 501 with ErrorNum 9 is in there since 3.7, earlier versions returned 403 and ErrorNum 11.
return WithStack(err)
}
return nil
}
var endpoints []string
for _, ep := range cep.Endpoints {
endpoints = append(endpoints, util.FixupEndpointURLScheme(ep.Endpoint))
}
// Update connection
if err := c.conn.UpdateEndpoints(endpoints); err != nil {
return WithStack(err)
}
return nil
}
// autoSynchronizeEndpoints performs automatic endpoint synchronization.
func (c *client) autoSynchronizeEndpoints(interval time.Duration) {
for {
// SynchronizeEndpoints endpoints
c.SynchronizeEndpoints(nil)
// Wait a bit
time.Sleep(interval)
}
}
type clusterEndpointsResponse struct {
Endpoints []clusterEndpoint `json:"endpoints,omitempty"`
}
type clusterEndpoint struct {
Endpoint string `json:"endpoint,omitempty"`
}
// clusterEndpoints returns the endpoints of a cluster.
func (c *client) clusterEndpoints(ctx context.Context, dbname string) (clusterEndpointsResponse, error) {
var url string
if dbname == "" {
url = "_api/cluster/endpoints"
} else {
url = path.Join("_db", pathEscape(dbname), "_api/cluster/endpoints")
}
req, err := c.conn.NewRequest("GET", url)
if err != nil {
return clusterEndpointsResponse{}, WithStack(err)
}
applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return clusterEndpointsResponse{}, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return clusterEndpointsResponse{}, WithStack(err)
}
var data clusterEndpointsResponse
if err := resp.ParseBody("", &data); err != nil {
return clusterEndpointsResponse{}, WithStack(err)
}
return data, nil
}

@ -0,0 +1,29 @@
//
// DISCLAIMER
//
// Copyright 2018 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
//
// Author Ewout Prangsma
//
package driver
// ClientReplication provides methods needed to access replication functionality from a client.
type ClientReplication interface {
// Replication provides access to replication specific operations.
Replication() Replication
}

@ -0,0 +1,28 @@
//
// DISCLAIMER
//
// Copyright 2018 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
//
// Author Ewout Prangsma
//
package driver
// Replication provides access to replication specific operations.
func (c *client) Replication() Replication {
return c
}

@ -0,0 +1,204 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import "context"
// ClientServerAdmin provides access to server administrations functions of an arangodb database server
// or an entire cluster of arangodb servers.
type ClientServerAdmin interface {
// ServerMode returns the current mode in which the server/cluster is operating.
// This call needs ArangoDB 3.3 and up.
ServerMode(ctx context.Context) (ServerMode, error)
// SetServerMode changes the current mode in which the server/cluster is operating.
// This call needs a client that uses JWT authentication.
// This call needs ArangoDB 3.3 and up.
SetServerMode(ctx context.Context, mode ServerMode) error
// Shutdown a specific server, optionally removing it from its cluster.
Shutdown(ctx context.Context, removeFromCluster bool) error
// Metrics returns the metrics of the server in Prometheus format.
// List of metrics: https://www.arangodb.com/docs/devel/http/administration-and-monitoring-metrics.html
// You can parse it using Prometheus client:
/*
var parser expfmt.TextParser
metricsProm, err := parser.TextToMetricFamilies(strings.NewReader(string(metrics)))
*/
Metrics(ctx context.Context) ([]byte, error)
// MetricsForSingleServer returns the metrics of the specific server in Prometheus format.
// This parameter 'serverID' is only meaningful on Coordinators.
// List of metrics: https://www.arangodb.com/docs/devel/http/administration-and-monitoring-metrics.html
// You can parse it using Prometheus client:
/*
var parser expfmt.TextParser
metricsProm, err := parser.TextToMetricFamilies(strings.NewReader(string(metrics)))
*/
MetricsForSingleServer(ctx context.Context, serverID string) ([]byte, error)
// Deprecated: Use Metrics instead.
// Statistics queries statistics from a specific server
Statistics(ctx context.Context) (ServerStatistics, error)
// ShutdownV2 shuts down a specific coordinator, optionally removing it from the cluster with a graceful manner.
ShutdownV2(ctx context.Context, removeFromCluster, graceful bool) error
// ShutdownInfoV2 queries information about shutdown progress.
ShutdownInfoV2(ctx context.Context) (ShutdownInfo, error)
// Logs retrieve logs from server in ArangoDB 3.8.0+ format
Logs(ctx context.Context) (ServerLogs, error)
}
type ServerLogs struct {
Total int `json:"total"`
Messages []ServerLogMessage `json:"messages,omitempty"`
}
type ServerLogMessage struct {
ID int `json:"id"`
Topic string `json:"topic"`
Level string `json:"level"`
Date string `json:"date"`
Message string `json:"message"`
}
type ServerMode string
// ServerStatistics contains statistical data about the server as a whole.
type ServerStatistics struct {
Time float64 `json:"time"`
Enabled bool `json:"enabled"`
System SystemStats `json:"system"`
Client ClientStats `json:"client"`
ClientUser ClientStats `json:"clientUser,omitempty"`
HTTP HTTPStats `json:"http"`
Server ServerStats `json:"server"`
ArangoError
}
// SystemStats contains statistical data about the system, this is part of
// ServerStatistics.
type SystemStats struct {
MinorPageFaults int64 `json:"minorPageFaults"`
MajorPageFaults int64 `json:"majorPageFaults"`
UserTime float64 `json:"userTime"`
SystemTime float64 `json:"systemTime"`
NumberOfThreads int64 `json:"numberOfThreads"`
ResidentSize int64 `json:"residentSize"`
ResidentSizePercent float64 `json:"residentSizePercent"`
VirtualSize int64 `json:"virtualSize"`
}
// Stats is used for various time-related statistics.
type Stats struct {
Sum float64 `json:"sum"`
Count int64 `json:"count"`
Counts []int64 `json:"counts"`
}
type ClientStats struct {
HTTPConnections int64 `json:"httpConnections"`
ConnectionTime Stats `json:"connectionTime"`
TotalTime Stats `json:"totalTime"`
RequestTime Stats `json:"requestTime"`
QueueTime Stats `json:"queueTime"`
IoTime Stats `json:"ioTime"`
BytesSent Stats `json:"bytesSent"`
BytesReceived Stats `json:"bytesReceived"`
}
// HTTPStats contains statistics about the HTTP traffic.
type HTTPStats struct {
RequestsTotal int64 `json:"requestsTotal"`
RequestsAsync int64 `json:"requestsAsync"`
RequestsGet int64 `json:"requestsGet"`
RequestsHead int64 `json:"requestsHead"`
RequestsPost int64 `json:"requestsPost"`
RequestsPut int64 `json:"requestsPut"`
RequestsPatch int64 `json:"requestsPatch"`
RequestsDelete int64 `json:"requestsDelete"`
RequestsOptions int64 `json:"requestsOptions"`
RequestsOther int64 `json:"requestsOther"`
RequestsSuperuser int64 `json:"requestsSuperuser,omitempty"`
RequestsUser int64 `json:"requestsUser,omitempty"`
}
// TransactionStats contains statistics about transactions.
type TransactionStats struct {
Started int64 `json:"started"`
Aborted int64 `json:"aborted"`
Committed int64 `json:"committed"`
IntermediateCommits int64 `json:"intermediateCommits"`
ReadOnly int64 `json:"readOnly,omitempty"`
DirtyReadOnly int64 `json:"dirtyReadOnly,omitempty"`
}
// MemoryStats contains statistics about memory usage.
type MemoryStats struct {
ContextID int64 `json:"contextId"`
TMax float64 `json:"tMax"`
CountOfTimes int64 `json:"countOfTimes"`
HeapMax int64 `json:"heapMax"`
HeapMin int64 `json:"heapMin"`
Invocations int64 `json:"invocations,omitempty"`
}
// V8ContextStats contains statistics about V8 contexts.
type V8ContextStats struct {
Available int64 `json:"available"`
Busy int64 `json:"busy"`
Dirty int64 `json:"dirty"`
Free int64 `json:"free"`
Min int64 `json:"min,omitempty"`
Max int64 `json:"max"`
Memory []MemoryStats `json:"memory"`
}
// ThreadsStats contains statistics about threads.
type ThreadStats struct {
SchedulerThreads int64 `json:"scheduler-threads"`
Blocked int64 `json:"blocked"`
Queued int64 `json:"queued"`
InProgress int64 `json:"in-progress"`
DirectExec int64 `json:"direct-exec"`
}
// ServerStats contains statistics about the server.
type ServerStats struct {
Uptime float64 `json:"uptime"`
PhysicalMemory int64 `json:"physicalMemory"`
Transactions TransactionStats `json:"transactions"`
V8Context V8ContextStats `json:"v8Context"`
Threads ThreadStats `json:"threads"`
}
const (
// ServerModeDefault is the normal mode of the database in which read and write requests
// are allowed.
ServerModeDefault ServerMode = "default"
// ServerModeReadOnly is the mode in which all modifications to th database are blocked.
// Behavior is the same as user that has read-only access to all databases & collections.
ServerModeReadOnly ServerMode = "readonly"
)

@ -0,0 +1,243 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
)
type serverModeResponse struct {
Mode ServerMode `json:"mode"`
ArangoError
}
type serverModeRequest struct {
Mode ServerMode `json:"mode"`
}
// ShutdownInfo stores information about shutdown of the coordinator.
type ShutdownInfo struct {
// AQLCursors stores a number of AQL cursors that are still active.
AQLCursors int `json:"AQLcursors"`
// Transactions stores a number of ongoing transactions.
Transactions int `json:"transactions"`
// PendingJobs stores a number of ongoing asynchronous requests.
PendingJobs int `json:"pendingJobs"`
// DoneJobs stores a number of finished asynchronous requests, whose result has not yet been collected.
DoneJobs int `json:"doneJobs"`
// PregelConductors stores a number of ongoing Pregel jobs.
PregelConductors int `json:"pregelConductors"`
// LowPrioOngoingRequests stores a number of ongoing low priority requests.
LowPrioOngoingRequests int `json:"lowPrioOngoingRequests"`
// LowPrioQueuedRequests stores a number of queued low priority requests.
LowPrioQueuedRequests int `json:"lowPrioQueuedRequests"`
// AllClear is set if all operations are closed.
AllClear bool `json:"allClear"`
// SoftShutdownOngoing describes whether a soft shutdown of the Coordinator is in progress.
SoftShutdownOngoing bool `json:"softShutdownOngoing"`
}
// ServerMode returns the current mode in which the server/cluster is operating.
// This call needs ArangoDB 3.3 and up.
func (c *client) ServerMode(ctx context.Context) (ServerMode, error) {
req, err := c.conn.NewRequest("GET", "_admin/server/mode")
if err != nil {
return "", WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return "", WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return "", WithStack(err)
}
var result serverModeResponse
if err := resp.ParseBody("", &result); err != nil {
return "", WithStack(err)
}
return result.Mode, nil
}
// SetServerMode changes the current mode in which the server/cluster is operating.
// This call needs a client that uses JWT authentication.
// This call needs ArangoDB 3.3 and up.
func (c *client) SetServerMode(ctx context.Context, mode ServerMode) error {
req, err := c.conn.NewRequest("PUT", "_admin/server/mode")
if err != nil {
return WithStack(err)
}
input := serverModeRequest{
Mode: mode,
}
req, err = req.SetBody(input)
if err != nil {
return WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return WithStack(err)
}
return nil
}
// Logs retrieve logs from server in ArangoDB 3.8.0+ format
func (c *client) Logs(ctx context.Context) (ServerLogs, error) {
req, err := c.conn.NewRequest("GET", "_admin/log/entries")
if err != nil {
return ServerLogs{}, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return ServerLogs{}, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return ServerLogs{}, WithStack(err)
}
var data ServerLogs
if err := resp.ParseBody("", &data); err != nil {
return ServerLogs{}, WithStack(err)
}
return data, nil
}
// Shutdown a specific server, optionally removing it from its cluster.
func (c *client) Shutdown(ctx context.Context, removeFromCluster bool) error {
req, err := c.conn.NewRequest("DELETE", "_admin/shutdown")
if err != nil {
return WithStack(err)
}
if removeFromCluster {
req.SetQuery("remove_from_cluster", "1")
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return WithStack(err)
}
return nil
}
// Metrics returns the metrics of the server in Prometheus format.
func (c *client) Metrics(ctx context.Context) ([]byte, error) {
return c.getMetrics(ctx, "")
}
// MetricsForSingleServer returns the metrics of the specific server in Prometheus format.
// This parameter 'serverID' is only meaningful on Coordinators.
func (c *client) MetricsForSingleServer(ctx context.Context, serverID string) ([]byte, error) {
return c.getMetrics(ctx, serverID)
}
// Metrics returns the metrics of the server in Prometheus format.
func (c *client) getMetrics(ctx context.Context, serverID string) ([]byte, error) {
var rawResponse []byte
ctx = WithRawResponse(ctx, &rawResponse)
req, err := c.conn.NewRequest("GET", "_admin/metrics/v2")
if err != nil {
return rawResponse, WithStack(err)
}
if serverID != "" {
req.SetQuery("serverId", serverID)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return rawResponse, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return rawResponse, WithStack(err)
}
return rawResponse, nil
}
// Statistics queries statistics from a specific server.
func (c *client) Statistics(ctx context.Context) (ServerStatistics, error) {
req, err := c.conn.NewRequest("GET", "_admin/statistics")
if err != nil {
return ServerStatistics{}, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return ServerStatistics{}, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return ServerStatistics{}, WithStack(err)
}
var data ServerStatistics
if err := resp.ParseBody("", &data); err != nil {
return ServerStatistics{}, WithStack(err)
}
return data, nil
}
// ShutdownV2 shuts down a specific coordinator, optionally removing it from the cluster with a graceful manner.
// When `graceful` is true then run soft shutdown process and the `ShutdownInfoV2` can be used to check the progress.
// It is available since versions: v3.7.12, v3.8.1, v3.9.0.
func (c *client) ShutdownV2(ctx context.Context, removeFromCluster, graceful bool) error {
req, err := c.conn.NewRequest("DELETE", "_admin/shutdown")
if err != nil {
return WithStack(err)
}
if removeFromCluster {
req.SetQuery("remove_from_cluster", "1")
}
if graceful {
req.SetQuery("soft", "true")
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return WithStack(err)
}
return nil
}
// ShutdownInfoV2 returns information about shutdown progress.
// It is available since versions: v3.7.12, v3.8.1, v3.9.0.
func (c *client) ShutdownInfoV2(ctx context.Context) (ShutdownInfo, error) {
req, err := c.conn.NewRequest("GET", "_admin/shutdown")
if err != nil {
return ShutdownInfo{}, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return ShutdownInfo{}, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return ShutdownInfo{}, WithStack(err)
}
data := ShutdownInfo{}
if err := resp.ParseBody("", &data); err != nil {
return ShutdownInfo{}, WithStack(err)
}
return data, nil
}

@ -0,0 +1,61 @@
//
// DISCLAIMER
//
// Copyright 2018 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
//
// Author Ewout Prangsma
//
package driver
import "context"
// ClientServerInfo provides access to information about a single ArangoDB server.
// When your client uses multiple endpoints, it is undefined which server
// will respond to requests of this interface.
type ClientServerInfo interface {
// Version returns version information from the connected database server.
// Use WithDetails to configure a context that will include additional details in the return VersionInfo.
Version(ctx context.Context) (VersionInfo, error)
// ServerRole returns the role of the server that answers the request.
ServerRole(ctx context.Context) (ServerRole, error)
// Gets the ID of this server in the cluster.
// An error is returned when calling this to a server that is not part of a cluster.
ServerID(ctx context.Context) (string, error)
}
// ServerRole is the role of an arangod server
type ServerRole string
const (
// ServerRoleSingle indicates that the server is a single-server instance
ServerRoleSingle ServerRole = "Single"
// ServerRoleSingleActive indicates that the server is a the leader of a single-server resilient pair
ServerRoleSingleActive ServerRole = "SingleActive"
// ServerRoleSinglePassive indicates that the server is a a follower of a single-server resilient pair
ServerRoleSinglePassive ServerRole = "SinglePassive"
// ServerRoleDBServer indicates that the server is a dbserver within a cluster
ServerRoleDBServer ServerRole = "DBServer"
// ServerRoleCoordinator indicates that the server is a coordinator within a cluster
ServerRoleCoordinator ServerRole = "Coordinator"
// ServerRoleAgent indicates that the server is an agent within a cluster
ServerRoleAgent ServerRole = "Agent"
// ServerRoleUndefined indicates that the role of the server cannot be determined
ServerRoleUndefined ServerRole = "Undefined"
)

@ -0,0 +1,152 @@
//
// DISCLAIMER
//
// Copyright 2018 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
)
// Version returns version information from the connected database server.
func (c *client) Version(ctx context.Context) (VersionInfo, error) {
req, err := c.conn.NewRequest("GET", "_api/version")
if err != nil {
return VersionInfo{}, WithStack(err)
}
applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return VersionInfo{}, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return VersionInfo{}, WithStack(err)
}
var data VersionInfo
if err := resp.ParseBody("", &data); err != nil {
return VersionInfo{}, WithStack(err)
}
return data, nil
}
// roleResponse contains the response body of the `/admin/server/role` api.
type roleResponse struct {
// Role of the server within a cluster
Role string `json:"role,omitempty"`
Mode string `json:"mode,omitempty"`
ArangoError
}
// asServerRole converts the response into a ServerRole
func (r roleResponse) asServerRole(ctx context.Context, c *client) (ServerRole, error) {
switch r.Role {
case "SINGLE":
switch r.Mode {
case "resilient":
if err := c.echo(ctx); IsNoLeader(err) {
return ServerRoleSinglePassive, nil
} else if err != nil {
return ServerRoleUndefined, WithStack(err)
}
return ServerRoleSingleActive, nil
default:
return ServerRoleSingle, nil
}
case "PRIMARY":
return ServerRoleDBServer, nil
case "COORDINATOR":
return ServerRoleCoordinator, nil
case "AGENT":
return ServerRoleAgent, nil
case "UNDEFINED":
return ServerRoleUndefined, nil
default:
return ServerRoleUndefined, nil
}
}
// ServerRole returns the role of the server that answers the request.
func (c *client) ServerRole(ctx context.Context) (ServerRole, error) {
req, err := c.conn.NewRequest("GET", "_admin/server/role")
if err != nil {
return ServerRoleUndefined, WithStack(err)
}
applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return ServerRoleUndefined, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return ServerRoleUndefined, WithStack(err)
}
var data roleResponse
if err := resp.ParseBody("", &data); err != nil {
return ServerRoleUndefined, WithStack(err)
}
role, err := data.asServerRole(ctx, c)
if err != nil {
return ServerRoleUndefined, WithStack(err)
}
return role, nil
}
type idResponse struct {
ID string `json:"id,omitempty"`
}
// Gets the ID of this server in the cluster.
// An error is returned when calling this to a server that is not part of a cluster.
func (c *client) ServerID(ctx context.Context) (string, error) {
req, err := c.conn.NewRequest("GET", "_admin/server/id")
if err != nil {
return "", WithStack(err)
}
applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return "", WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return "", WithStack(err)
}
var data idResponse
if err := resp.ParseBody("", &data); err != nil {
return "", WithStack(err)
}
return data.ID, nil
}
// clusterEndpoints returns the endpoints of a cluster.
func (c *client) echo(ctx context.Context) error {
req, err := c.conn.NewRequest("GET", "_admin/echo")
if err != nil {
return WithStack(err)
}
applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return WithStack(err)
}
return nil
}

@ -0,0 +1,52 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import "context"
// ClientUsers provides access to the users in a single arangodb database server, or an entire cluster of arangodb servers.
type ClientUsers interface {
// User opens a connection to an existing user.
// If no user with given name exists, an NotFoundError is returned.
User(ctx context.Context, name string) (User, error)
// UserExists returns true if a user with given name exists.
UserExists(ctx context.Context, name string) (bool, error)
// Users returns a list of all users found by the client.
Users(ctx context.Context) ([]User, error)
// CreateUser creates a new user with given name and opens a connection to it.
// If a user with given name already exists, a Conflict error is returned.
CreateUser(ctx context.Context, name string, options *UserOptions) (User, error)
}
// UserOptions contains options for creating a new user, updating or replacing a user.
type UserOptions struct {
// The user password as a string. If not specified, it will default to an empty string.
Password string `json:"passwd,omitempty"`
// A flag indicating whether the user account should be activated or not. The default value is true. If set to false, the user won't be able to log into the database.
Active *bool `json:"active,omitempty"`
// A JSON object with extra user information. The data contained in extra will be stored for the user but not be interpreted further by ArangoDB.
Extra interface{} `json:"extra,omitempty"`
}

@ -0,0 +1,144 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
"path"
)
// User opens a connection to an existing user.
// If no user with given name exists, an NotFoundError is returned.
func (c *client) User(ctx context.Context, name string) (User, error) {
escapedName := pathEscape(name)
req, err := c.conn.NewRequest("GET", path.Join("_api/user", escapedName))
if err != nil {
return nil, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
var data userData
if err := resp.ParseBody("", &data); err != nil {
return nil, WithStack(err)
}
u, err := newUser(data, c.conn)
if err != nil {
return nil, WithStack(err)
}
return u, nil
}
// UserExists returns true if a database with given name exists.
func (c *client) UserExists(ctx context.Context, name string) (bool, error) {
escapedName := pathEscape(name)
req, err := c.conn.NewRequest("GET", path.Join("_api", "user", escapedName))
if err != nil {
return false, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return false, WithStack(err)
}
if err := resp.CheckStatus(200); err == nil {
return true, nil
} else if IsNotFound(err) {
return false, nil
} else {
return false, WithStack(err)
}
}
type listUsersResponse struct {
Result []userData `json:"result,omitempty"`
ArangoError
}
// Users returns a list of all users found by the client.
func (c *client) Users(ctx context.Context) ([]User, error) {
req, err := c.conn.NewRequest("GET", "/_api/user")
if err != nil {
return nil, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
var data listUsersResponse
if err := resp.ParseBody("", &data); err != nil {
return nil, WithStack(err)
}
result := make([]User, 0, len(data.Result))
for _, userData := range data.Result {
u, err := newUser(userData, c.conn)
if err != nil {
return nil, WithStack(err)
}
result = append(result, u)
}
return result, nil
}
// CreateUser creates a new user with given name and opens a connection to it.
// If a user with given name already exists, a DuplicateError is returned.
func (c *client) CreateUser(ctx context.Context, name string, options *UserOptions) (User, error) {
input := struct {
UserOptions
Name string `json:"user"`
}{
Name: name,
}
if options != nil {
input.UserOptions = *options
}
req, err := c.conn.NewRequest("POST", path.Join("_api/user"))
if err != nil {
return nil, WithStack(err)
}
if _, err := req.SetBody(input); err != nil {
return nil, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(201); err != nil {
return nil, WithStack(err)
}
var data userData
if err := resp.ParseBody("", &data); err != nil {
return nil, WithStack(err)
}
u, err := newUser(data, c.conn)
if err != nil {
return nil, WithStack(err)
}
return u, nil
}

@ -0,0 +1,357 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
"encoding/json"
"fmt"
"reflect"
"time"
)
// Cluster provides access to cluster wide specific operations.
// To use this interface, an ArangoDB cluster is required.
type Cluster interface {
// Get the cluster configuration & health
Health(ctx context.Context) (ClusterHealth, error)
// Get the inventory of the cluster containing all collections (with entire details) of a database.
DatabaseInventory(ctx context.Context, db Database) (DatabaseInventory, error)
// MoveShard moves a single shard of the given collection from server `fromServer` to
// server `toServer`.
MoveShard(ctx context.Context, col Collection, shard ShardID, fromServer, toServer ServerID) error
// CleanOutServer triggers activities to clean out a DBServer.
CleanOutServer(ctx context.Context, serverID string) error
// ResignServer triggers activities to let a DBServer resign for all shards.
ResignServer(ctx context.Context, serverID string) error
// IsCleanedOut checks if the dbserver with given ID has been cleaned out.
IsCleanedOut(ctx context.Context, serverID string) (bool, error)
// RemoveServer is a low-level option to remove a server from a cluster.
// This function is suitable for servers of type coordinator or dbserver.
// The use of `ClientServerAdmin.Shutdown` is highly recommended above this function.
RemoveServer(ctx context.Context, serverID ServerID) error
}
// ServerID identifies an arangod server in a cluster.
type ServerID string
// ClusterHealth contains health information for all servers in a cluster.
type ClusterHealth struct {
// Unique identifier of the entire cluster.
// This ID is created when the cluster was first created.
ID string `json:"ClusterId"`
// Health per server
Health map[ServerID]ServerHealth `json:"Health"`
}
// ServerSyncStatus describes the servers sync status
type ServerSyncStatus string
const (
ServerSyncStatusUnknown ServerSyncStatus = "UNKNOWN"
ServerSyncStatusUndefined ServerSyncStatus = "UNDEFINED"
ServerSyncStatusStartup ServerSyncStatus = "STARTUP"
ServerSyncStatusStopping ServerSyncStatus = "STOPPING"
ServerSyncStatusStopped ServerSyncStatus = "STOPPED"
ServerSyncStatusServing ServerSyncStatus = "SERVING"
ServerSyncStatusShutdown ServerSyncStatus = "SHUTDOWN"
)
// ServerHealth contains health information of a single server in a cluster.
type ServerHealth struct {
Endpoint string `json:"Endpoint"`
LastHeartbeatAcked time.Time `json:"LastHeartbeatAcked"`
LastHeartbeatSent time.Time `json:"LastHeartbeatSent"`
LastHeartbeatStatus string `json:"LastHeartbeatStatus"`
Role ServerRole `json:"Role"`
ShortName string `json:"ShortName"`
Status ServerStatus `json:"Status"`
CanBeDeleted bool `json:"CanBeDeleted"`
HostID string `json:"Host,omitempty"`
Version Version `json:"Version,omitempty"`
Engine EngineType `json:"Engine,omitempty"`
SyncStatus ServerSyncStatus `json:"SyncStatus,omitempty"`
// Only for Coordinators
AdvertisedEndpoint *string `json:"AdvertisedEndpoint,omitempty"`
// Only for Agents
Leader *string `json:"Leader,omitempty"`
Leading *bool `json:"Leading,omitempty"`
}
// ServerStatus describes the health status of a server
type ServerStatus string
const (
// ServerStatusGood indicates server is in good state
ServerStatusGood ServerStatus = "GOOD"
// ServerStatusBad indicates server has missed 1 heartbeat
ServerStatusBad ServerStatus = "BAD"
// ServerStatusFailed indicates server has been declared failed by the supervision, this happens after about 15s being bad.
ServerStatusFailed ServerStatus = "FAILED"
)
// DatabaseInventory describes a detailed state of the collections & shards of a specific database within a cluster.
type DatabaseInventory struct {
// Details of database, this is present since ArangoDB 3.6
Info DatabaseInfo `json:"properties,omitempty"`
// Details of all collections
Collections []InventoryCollection `json:"collections,omitempty"`
// Details of all views
Views []InventoryView `json:"views,omitempty"`
State State `json:"state,omitempty"`
Tick string `json:"tick,omitempty"`
}
type State struct {
Running bool `json:"running,omitempty"`
LastLogTick string `json:"lastLogTick,omitempty"`
LastUncommittedLogTick string `json:"lastUncommittedLogTick,omitempty"`
TotalEvents int64 `json:"totalEvents,omitempty"`
Time time.Time `json:"time,omitempty"`
}
// UnmarshalJSON marshals State to arangodb json representation
func (s *State) UnmarshalJSON(d []byte) error {
var internal interface{}
if err := json.Unmarshal(d, &internal); err != nil {
return err
}
if val, ok := internal.(string); ok {
if val != "unused" {
fmt.Printf("unrecognized State value: %s\n", val)
}
*s = State{}
return nil
} else {
type Alias State
out := Alias{}
if err := json.Unmarshal(d, &out); err != nil {
return &json.UnmarshalTypeError{
Value: string(d),
Type: reflect.TypeOf(s).Elem(),
}
}
*s = State(out)
}
return nil
}
// IsReady returns true if the IsReady flag of all collections is set.
func (i DatabaseInventory) IsReady() bool {
for _, c := range i.Collections {
if !c.IsReady {
return false
}
}
return true
}
// PlanVersion returns the plan version of the first collection in the given inventory.
func (i DatabaseInventory) PlanVersion() int64 {
if len(i.Collections) == 0 {
return 0
}
return i.Collections[0].PlanVersion
}
// CollectionByName returns the InventoryCollection with given name.
// Return false if not found.
func (i DatabaseInventory) CollectionByName(name string) (InventoryCollection, bool) {
for _, c := range i.Collections {
if c.Parameters.Name == name {
return c, true
}
}
return InventoryCollection{}, false
}
// ViewByName returns the InventoryView with given name.
// Return false if not found.
func (i DatabaseInventory) ViewByName(name string) (InventoryView, bool) {
for _, v := range i.Views {
if v.Name == name {
return v, true
}
}
return InventoryView{}, false
}
// InventoryCollection is a single element of a DatabaseInventory, containing all information
// of a specific collection.
type InventoryCollection struct {
Parameters InventoryCollectionParameters `json:"parameters"`
Indexes []InventoryIndex `json:"indexes,omitempty"`
PlanVersion int64 `json:"planVersion,omitempty"`
IsReady bool `json:"isReady,omitempty"`
AllInSync bool `json:"allInSync,omitempty"`
}
// IndexByFieldsAndType returns the InventoryIndex with given fields & type.
// Return false if not found.
func (i InventoryCollection) IndexByFieldsAndType(fields []string, indexType string) (InventoryIndex, bool) {
for _, idx := range i.Indexes {
if idx.Type == indexType && idx.FieldsEqual(fields) {
return idx, true
}
}
return InventoryIndex{}, false
}
// InventoryCollectionParameters contains all configuration parameters of a collection in a database inventory.
type InventoryCollectionParameters struct {
// Available from 3.7 ArangoD version.
CacheEnabled bool `json:"cacheEnabled,omitempty"`
Deleted bool `json:"deleted,omitempty"`
DistributeShardsLike string `json:"distributeShardsLike,omitempty"`
// Deprecated: since 3.7 version. It is related only to MMFiles.
DoCompact bool `json:"doCompact,omitempty"`
// Available from 3.7 ArangoD version.
GloballyUniqueId string `json:"globallyUniqueId,omitempty"`
ID string `json:"id,omitempty"`
// Deprecated: since 3.7 version. It is related only to MMFiles.
IndexBuckets int `json:"indexBuckets,omitempty"`
Indexes []InventoryIndex `json:"indexes,omitempty"`
// Available from 3.9 ArangoD version.
InternalValidatorType int `json:"internalValidatorType,omitempty"`
// Available from 3.7 ArangoD version.
IsDisjoint bool `json:"isDisjoint,omitempty"`
IsSmart bool `json:"isSmart,omitempty"`
// Available from 3.7 ArangoD version.
IsSmartChild bool `json:"isSmartChild,omitempty"`
IsSystem bool `json:"isSystem,omitempty"`
// Deprecated: since 3.7 version. It is related only to MMFiles.
IsVolatile bool `json:"isVolatile,omitempty"`
// Deprecated: since 3.7 version. It is related only to MMFiles.
JournalSize int64 `json:"journalSize,omitempty"`
KeyOptions struct {
AllowUserKeys bool `json:"allowUserKeys,omitempty"`
// Deprecated: this field has wrong type and will be removed in the future. It is not used anymore since it can cause parsing issues.
LastValue int64 `json:"-"`
LastValueV2 uint64 `json:"lastValue,omitempty"`
Type string `json:"type,omitempty"`
} `json:"keyOptions"`
// Deprecated: use 'WriteConcern' instead.
MinReplicationFactor int `json:"minReplicationFactor,omitempty"`
Name string `json:"name,omitempty"`
NumberOfShards int `json:"numberOfShards,omitempty"`
// Deprecated: since 3.7 ArangoD version.
Path string `json:"path,omitempty"`
PlanID string `json:"planId,omitempty"`
ReplicationFactor int `json:"replicationFactor,omitempty"`
// Schema for collection validation.
Schema *CollectionSchemaOptions `json:"schema,omitempty"`
ShadowCollections []int `json:"shadowCollections,omitempty"`
ShardingStrategy ShardingStrategy `json:"shardingStrategy,omitempty"`
ShardKeys []string `json:"shardKeys,omitempty"`
Shards map[ShardID][]ServerID `json:"shards,omitempty"`
// Optional only for some collections.
SmartGraphAttribute string `json:"smartGraphAttribute,omitempty"`
// Optional only for some collections.
SmartJoinAttribute string `json:"smartJoinAttribute,omitempty"`
Status CollectionStatus `json:"status,omitempty"`
// Available from 3.7 ArangoD version.
SyncByRevision bool `json:"syncByRevision,omitempty"`
Type CollectionType `json:"type,omitempty"`
// Available from 3.7 ArangoD version.
UsesRevisionsAsDocumentIds bool `json:"usesRevisionsAsDocumentIds,omitempty"`
WaitForSync bool `json:"waitForSync,omitempty"`
// Available from 3.6 ArangoD version.
WriteConcern int `json:"writeConcern,omitempty"`
// Available from 3.10 ArangoD version.
ComputedValues []ComputedValue `json:"computedValues,omitempty"`
}
// IsSatellite returns true if the collection is a satellite collection
func (icp *InventoryCollectionParameters) IsSatellite() bool {
return icp.ReplicationFactor == ReplicationFactorSatellite
}
// ShardID is an internal identifier of a specific shard
type ShardID string
// InventoryIndex contains all configuration parameters of a single index of a collection in a database inventory.
type InventoryIndex struct {
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Fields []string `json:"fields,omitempty"`
Unique bool `json:"unique"`
Sparse bool `json:"sparse"`
Deduplicate bool `json:"deduplicate"`
MinLength int `json:"minLength,omitempty"`
GeoJSON bool `json:"geoJson,omitempty"`
Name string `json:"name,omitempty"`
ExpireAfter int `json:"expireAfter,omitempty"`
Estimates bool `json:"estimates,omitempty"`
FieldValueTypes string `json:"fieldValueTypes,omitempty"`
CacheEnabled *bool `json:"cacheEnabled,omitempty"`
}
// FieldsEqual returns true when the given fields list equals the
// Fields list in the InventoryIndex.
// The order of fields is irrelevant.
func (i InventoryIndex) FieldsEqual(fields []string) bool {
return stringSliceEqualsIgnoreOrder(i.Fields, fields)
}
// InventoryView is a single element of a DatabaseInventory, containing all information
// of a specific view.
type InventoryView struct {
Name string `json:"name,omitempty"`
Deleted bool `json:"deleted,omitempty"`
ID string `json:"id,omitempty"`
IsSystem bool `json:"isSystem,omitempty"`
PlanID string `json:"planId,omitempty"`
Type ViewType `json:"type,omitempty"`
// Include all properties from an arangosearch view.
ArangoSearchViewProperties
}
// stringSliceEqualsIgnoreOrder returns true when the given lists contain the same elements.
// The order of elements is irrelevant.
func stringSliceEqualsIgnoreOrder(a, b []string) bool {
if len(a) != len(b) {
return false
}
bMap := make(map[string]struct{})
for _, x := range b {
bMap[x] = struct{}{}
}
for _, x := range a {
if _, found := bMap[x]; !found {
return false
}
}
return true
}

@ -0,0 +1,365 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package cluster
import (
"context"
"math"
"net/http"
"sort"
"strings"
"sync"
"time"
driver "github.com/arangodb/go-driver"
)
const (
keyFollowLeaderRedirect driver.ContextKey = "arangodb-followLeaderRedirect"
)
// ConnectionConfig provides all configuration options for a cluster connection.
type ConnectionConfig struct {
// DefaultTimeout is the timeout used by requests that have no timeout set in the given context.
DefaultTimeout time.Duration
}
// ServerConnectionBuilder specifies a function called by the cluster connection when it
// needs to create an underlying connection to a specific endpoint.
type ServerConnectionBuilder func(endpoint string) (driver.Connection, error)
// NewConnection creates a new cluster connection to a cluster of servers.
// The given connections are existing connections to each of the servers.
func NewConnection(config ConnectionConfig, connectionBuilder ServerConnectionBuilder, endpoints []string) (driver.Connection, error) {
if connectionBuilder == nil {
return nil, driver.WithStack(driver.InvalidArgumentError{Message: "Must provide a connection builder"})
}
if len(endpoints) == 0 {
return nil, driver.WithStack(driver.InvalidArgumentError{Message: "Must provide at least 1 endpoint"})
}
if config.DefaultTimeout == 0 {
config.DefaultTimeout = defaultTimeout
}
cConn := &clusterConnection{
connectionBuilder: connectionBuilder,
defaultTimeout: config.DefaultTimeout,
}
// Initialize endpoints
if err := cConn.UpdateEndpoints(endpoints); err != nil {
return nil, driver.WithStack(err)
}
return cConn, nil
}
const (
defaultTimeout = 9 * time.Minute
keyEndpoint driver.ContextKey = "arangodb-endpoint"
)
type clusterConnection struct {
connectionBuilder ServerConnectionBuilder
servers []driver.Connection
endpoints []string
current int
mutex sync.RWMutex
defaultTimeout time.Duration
auth driver.Authentication
}
// NewRequest creates a new request with given method and path.
func (c *clusterConnection) NewRequest(method, path string) (driver.Request, error) {
c.mutex.RLock()
servers := c.servers
c.mutex.RUnlock()
// It is assumed that all servers used the same protocol.
if len(servers) > 0 {
return servers[0].NewRequest(method, path)
}
return nil, driver.WithStack(driver.ArangoError{
HasError: true,
Code: http.StatusServiceUnavailable,
ErrorMessage: "no servers available",
})
}
// Do performs a given request, returning its response.
func (c *clusterConnection) Do(ctx context.Context, req driver.Request) (driver.Response, error) {
followLeaderRedirect := true
if ctx == nil {
ctx = context.Background()
} else {
if v := ctx.Value(keyFollowLeaderRedirect); v != nil {
if on, ok := v.(bool); ok {
followLeaderRedirect = on
}
}
}
// Timeout management.
// We take the given timeout and divide it in 3 so we allow for other servers
// to give it a try if an earlier server fails.
deadline, hasDeadline := ctx.Deadline()
var timeout time.Duration
if hasDeadline {
timeout = deadline.Sub(time.Now())
} else {
timeout = c.defaultTimeout
}
var server driver.Connection
var serverCount int
var durationPerRequest time.Duration
if v := ctx.Value(keyEndpoint); v != nil {
if endpoint, ok := v.(string); ok {
// Override pool to only specific server if it is found
if s, ok := c.getSpecificServer(endpoint); ok {
server = s
durationPerRequest = timeout
serverCount = 1
}
}
}
if server == nil {
server, serverCount = c.getCurrentServer()
timeoutDivider := math.Max(1.0, math.Min(3.0, float64(serverCount)))
durationPerRequest = time.Duration(float64(timeout) / timeoutDivider)
}
attempt := 1
for {
// Send request to specific endpoint with a 1/3 timeout (so we get 3 attempts)
serverCtx, cancel := context.WithTimeout(ctx, durationPerRequest)
resp, err := server.Do(serverCtx, req)
cancel()
isNoLeaderResponse := false
if err == nil && resp.StatusCode() == 503 {
// Service unavailable, parse the body, perhaps this is a "no leader"
// case where we have to failover.
var aerr driver.ArangoError
if perr := resp.ParseBody("", &aerr); perr == nil && aerr.HasError {
if driver.IsNoLeader(aerr) {
isNoLeaderResponse = true
// Save error in case we have no more servers
err = aerr
}
}
}
if !isNoLeaderResponse || !followLeaderRedirect {
if err == nil {
// We're done
return resp, nil
}
// No success yet
if driver.IsCanceled(err) {
// Request was cancelled, we return directly.
return nil, driver.WithStack(err)
}
// If we've completely written the request, we return the error,
// otherwise we'll failover to a new server.
if req.Written() {
// Request has been written to network, do not failover
if driver.IsArangoError(err) {
// ArangoError, so we got an error response from server.
return nil, driver.WithStack(err)
}
// Not an ArangoError, so it must be some kind of timeout, network ... error.
return nil, driver.WithStack(&driver.ResponseError{Err: err})
}
}
// Failed, try next server
attempt++
if attempt > serverCount {
// A specific server was specified, no failover.
// or
// We've tried all servers. Giving up.
return nil, driver.WithStack(err)
}
server = c.getNextServer()
}
}
/*func printError(err error, indent string) {
if err == nil {
return
}
fmt.Printf("%sGot %T %+v\n", indent, err, err)
if xerr, ok := err.(*os.SyscallError); ok {
printError(xerr.Err, indent+" ")
} else if xerr, ok := err.(*net.OpError); ok {
printError(xerr.Err, indent+" ")
} else if xerr, ok := err.(*url.Error); ok {
printError(xerr.Err, indent+" ")
}
}*/
// Unmarshal unmarshals the given raw object into the given result interface.
func (c *clusterConnection) Unmarshal(data driver.RawObject, result interface{}) error {
c.mutex.RLock()
servers := c.servers
c.mutex.RUnlock()
if len(servers) > 0 {
if err := c.servers[0].Unmarshal(data, result); err != nil {
return driver.WithStack(err)
}
return nil
}
return driver.WithStack(driver.ArangoError{
HasError: true,
Code: http.StatusServiceUnavailable,
ErrorMessage: "no servers available",
})
}
// Endpoints returns the endpoints used by this connection.
func (c *clusterConnection) Endpoints() []string {
c.mutex.RLock()
defer c.mutex.RUnlock()
var result []string
for _, s := range c.servers {
result = append(result, s.Endpoints()...)
}
return result
}
// UpdateEndpoints reconfigures the connection to use the given endpoints.
func (c *clusterConnection) UpdateEndpoints(endpoints []string) error {
if len(endpoints) == 0 {
return driver.WithStack(driver.InvalidArgumentError{Message: "Must provide at least 1 endpoint"})
}
sort.Strings(endpoints)
if strings.Join(endpoints, ",") == strings.Join(c.endpoints, ",") {
// No changes
return nil
}
// Create new connections
servers := make([]driver.Connection, 0, len(endpoints))
for _, ep := range endpoints {
conn, err := c.connectionBuilder(ep)
if err != nil {
return driver.WithStack(err)
}
if c.auth != nil {
conn, err = conn.SetAuthentication(c.auth)
if err != nil {
return driver.WithStack(err)
}
}
servers = append(servers, conn)
}
// Swap connections
c.mutex.Lock()
defer c.mutex.Unlock()
c.servers = servers
c.endpoints = endpoints
c.current = 0
return nil
}
// SetAuthentication creates a copy of connection wrapper for given auth parameters.
func (c *clusterConnection) SetAuthentication(auth driver.Authentication) (driver.Connection, error) {
c.mutex.Lock()
defer c.mutex.Unlock()
// Configure underlying servers
newServerConnections := make([]driver.Connection, len(c.servers))
for i, s := range c.servers {
authConn, err := s.SetAuthentication(auth)
if err != nil {
return nil, driver.WithStack(err)
}
newServerConnections[i] = authConn
}
// These two lines are not required for normal work but left for backward compatibility
// of SetAuthentication method - it was returning self object
c.auth = auth
c.servers = newServerConnections
return &clusterConnection{
connectionBuilder: c.connectionBuilder,
servers: c.servers,
endpoints: c.endpoints,
current: c.current,
mutex: sync.RWMutex{},
defaultTimeout: c.defaultTimeout,
auth: c.auth,
}, nil
}
// Protocols returns all protocols used by this connection.
func (c *clusterConnection) Protocols() driver.ProtocolSet {
c.mutex.RLock()
defer c.mutex.RUnlock()
var result driver.ProtocolSet
for _, s := range c.servers {
for _, p := range s.Protocols() {
if !result.Contains(p) {
result = append(result, p)
}
}
}
return result
}
// getCurrentServer returns the currently used server and number of servers.
func (c *clusterConnection) getCurrentServer() (driver.Connection, int) {
c.mutex.RLock()
defer c.mutex.RUnlock()
return c.servers[c.current], len(c.servers)
}
// getSpecificServer returns the server with the given endpoint.
func (c *clusterConnection) getSpecificServer(endpoint string) (driver.Connection, bool) {
c.mutex.RLock()
defer c.mutex.RUnlock()
for _, s := range c.servers {
for _, x := range s.Endpoints() {
if x == endpoint {
return s, true
}
}
}
// If endpoint is not found allow to use default connection pool - request will be routed thru coordinators
return nil, false
}
// getNextServer changes the currently used server and returns the new server.
func (c *clusterConnection) getNextServer() driver.Connection {
c.mutex.Lock()
defer c.mutex.Unlock()
c.current = (c.current + 1) % len(c.servers)
return c.servers[c.current]
}

@ -0,0 +1,26 @@
//
// DISCLAIMER
//
// Copyright 2018 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
//
// Author Ewout Prangsma
//
/*
Package cluster implements a driver.Connection that provides cluster failover support (it is not intended to be used directly).
*/
package cluster

@ -0,0 +1,489 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
"encoding/json"
"path"
"reflect"
)
// newCluster creates a new Cluster implementation.
func newCluster(conn Connection) (Cluster, error) {
if conn == nil {
return nil, WithStack(InvalidArgumentError{Message: "conn is nil"})
}
return &cluster{
conn: conn,
}, nil
}
type cluster struct {
conn Connection
}
// Health returns the state of the cluster
func (c *cluster) Health(ctx context.Context) (ClusterHealth, error) {
req, err := c.conn.NewRequest("GET", "_admin/cluster/health")
if err != nil {
return ClusterHealth{}, WithStack(err)
}
applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return ClusterHealth{}, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return ClusterHealth{}, WithStack(err)
}
var result ClusterHealth
if err := resp.ParseBody("", &result); err != nil {
return ClusterHealth{}, WithStack(err)
}
return result, nil
}
// DatabaseInventory Get the inventory of the cluster containing all collections (with entire details) of a database.
func (c *cluster) DatabaseInventory(ctx context.Context, db Database) (DatabaseInventory, error) {
req, err := c.conn.NewRequest("GET", path.Join("_db", db.Name(), "_api/replication/clusterInventory"))
if err != nil {
return DatabaseInventory{}, WithStack(err)
}
applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return DatabaseInventory{}, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return DatabaseInventory{}, WithStack(err)
}
var result DatabaseInventory
if err := resp.ParseBody("", &result); err != nil {
return DatabaseInventory{}, WithStack(err)
}
return result, nil
}
type moveShardRequest struct {
Database string `json:"database"`
Collection string `json:"collection"`
Shard ShardID `json:"shard"`
FromServer ServerID `json:"fromServer"`
ToServer ServerID `json:"toServer"`
}
// MoveShard moves a single shard of the given collection from server `fromServer` to
// server `toServer`.
func (c *cluster) MoveShard(ctx context.Context, col Collection, shard ShardID, fromServer, toServer ServerID) error {
req, err := c.conn.NewRequest("POST", "_admin/cluster/moveShard")
if err != nil {
return WithStack(err)
}
input := moveShardRequest{
Database: col.Database().Name(),
Collection: col.Name(),
Shard: shard,
FromServer: fromServer,
ToServer: toServer,
}
if _, err := req.SetBody(input); err != nil {
return WithStack(err)
}
cs := applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(202); err != nil {
return WithStack(err)
}
var result jobIDResponse
if err := resp.ParseBody("", &result); err != nil {
return WithStack(err)
}
if cs.JobIDResponse != nil {
*cs.JobIDResponse = result.JobID
}
return nil
}
type cleanOutServerRequest struct {
Server string `json:"server"`
}
type jobIDResponse struct {
JobID string `json:"id"`
}
// CleanOutServer triggers activities to clean out a DBServers.
func (c *cluster) CleanOutServer(ctx context.Context, serverID string) error {
req, err := c.conn.NewRequest("POST", "_admin/cluster/cleanOutServer")
if err != nil {
return WithStack(err)
}
input := cleanOutServerRequest{
Server: serverID,
}
if _, err := req.SetBody(input); err != nil {
return WithStack(err)
}
cs := applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(200, 202); err != nil {
return WithStack(err)
}
var result jobIDResponse
if err := resp.ParseBody("", &result); err != nil {
return WithStack(err)
}
if cs.JobIDResponse != nil {
*cs.JobIDResponse = result.JobID
}
return nil
}
// ResignServer triggers activities to let a DBServer resign for all shards.
func (c *cluster) ResignServer(ctx context.Context, serverID string) error {
req, err := c.conn.NewRequest("POST", "_admin/cluster/resignLeadership")
if err != nil {
return WithStack(err)
}
input := cleanOutServerRequest{
Server: serverID,
}
if _, err := req.SetBody(input); err != nil {
return WithStack(err)
}
cs := applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(200, 202); err != nil {
return WithStack(err)
}
var result jobIDResponse
if err := resp.ParseBody("", &result); err != nil {
return WithStack(err)
}
if cs.JobIDResponse != nil {
*cs.JobIDResponse = result.JobID
}
return nil
}
// IsCleanedOut checks if the dbserver with given ID has been cleaned out.
func (c *cluster) IsCleanedOut(ctx context.Context, serverID string) (bool, error) {
r, err := c.NumberOfServers(ctx)
if err != nil {
return false, WithStack(err)
}
for _, id := range r.CleanedServerIDs {
if id == serverID {
return true, nil
}
}
return false, nil
}
// NumberOfServersResponse holds the data returned from a NumberOfServer request.
type NumberOfServersResponse struct {
NoCoordinators int `json:"numberOfCoordinators,omitempty"`
NoDBServers int `json:"numberOfDBServers,omitempty"`
CleanedServerIDs []string `json:"cleanedServers,omitempty"`
}
// NumberOfServers returns the number of coordinator & dbservers in a clusters and the
// ID's of cleaned out servers.
func (c *cluster) NumberOfServers(ctx context.Context) (NumberOfServersResponse, error) {
req, err := c.conn.NewRequest("GET", "_admin/cluster/numberOfServers")
if err != nil {
return NumberOfServersResponse{}, WithStack(err)
}
applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return NumberOfServersResponse{}, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return NumberOfServersResponse{}, WithStack(err)
}
var result NumberOfServersResponse
if err := resp.ParseBody("", &result); err != nil {
return NumberOfServersResponse{}, WithStack(err)
}
return result, nil
}
// RemoveServer is a low-level option to remove a server from a cluster.
// This function is suitable for servers of type coordinator or dbserver.
// The use of `ClientServerAdmin.Shutdown` is highly recommended above this function.
func (c *cluster) RemoveServer(ctx context.Context, serverID ServerID) error {
req, err := c.conn.NewRequest("POST", "_admin/cluster/removeServer")
if err != nil {
return WithStack(err)
}
if _, err := req.SetBody(serverID); err != nil {
return WithStack(err)
}
applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(200, 202); err != nil {
return WithStack(err)
}
return nil
}
// replicationFactor represents the replication factor of a collection
// Has special value ReplicationFactorSatellite for satellite collections
type replicationFactor int
type inventoryCollectionParametersInternal struct {
// Available from 3.7 ArangoD version.
CacheEnabled bool `json:"cacheEnabled,omitempty"`
Deleted bool `json:"deleted,omitempty"`
DistributeShardsLike string `json:"distributeShardsLike,omitempty"`
DoCompact bool `json:"doCompact,omitempty"`
// Available from 3.7 ArangoD version.
GloballyUniqueId string `json:"globallyUniqueId,omitempty"`
ID string `json:"id,omitempty"`
IndexBuckets int `json:"indexBuckets,omitempty"`
Indexes []InventoryIndex `json:"indexes,omitempty"`
// Available from 3.9 ArangoD version.
InternalValidatorType int `json:"internalValidatorType,omitempty"`
// Available from 3.7 ArangoD version.
IsDisjoint bool `json:"isDisjoint,omitempty"`
IsSmart bool `json:"isSmart,omitempty"`
// Available from 3.7 ArangoD version.
IsSmartChild bool `json:"isSmartChild,omitempty"`
IsSystem bool `json:"isSystem,omitempty"`
// Deprecated: since 3.7 version. It is related only to MMFiles.
IsVolatile bool `json:"isVolatile,omitempty"`
// Deprecated: since 3.7 version. It is related only to MMFiles.
JournalSize int64 `json:"journalSize,omitempty"`
KeyOptions struct {
AllowUserKeys bool `json:"allowUserKeys,omitempty"`
LastValue uint64 `json:"lastValue,omitempty"`
Type string `json:"type,omitempty"`
} `json:"keyOptions"`
// Deprecated: use 'WriteConcern' instead
MinReplicationFactor int `json:"minReplicationFactor,omitempty"`
Name string `json:"name,omitempty"`
NumberOfShards int `json:"numberOfShards,omitempty"`
Path string `json:"path,omitempty"`
PlanID string `json:"planId,omitempty"`
ReplicationFactor replicationFactor `json:"replicationFactor,omitempty"`
// Schema for collection validation
Schema *CollectionSchemaOptions `json:"schema,omitempty"`
ShadowCollections []int `json:"shadowCollections,omitempty"`
ShardingStrategy ShardingStrategy `json:"shardingStrategy,omitempty"`
ShardKeys []string `json:"shardKeys,omitempty"`
Shards map[ShardID][]ServerID `json:"shards,omitempty"`
// Optional only for some collections.
SmartGraphAttribute string `json:"smartGraphAttribute,omitempty"`
// Optional only for some collections.
SmartJoinAttribute string `json:"smartJoinAttribute,omitempty"`
Status CollectionStatus `json:"status,omitempty"`
// Available from 3.7 ArangoD version
SyncByRevision bool `json:"syncByRevision,omitempty"`
Type CollectionType `json:"type,omitempty"`
// Available from 3.7 ArangoD version
UsesRevisionsAsDocumentIds bool `json:"usesRevisionsAsDocumentIds,omitempty"`
WaitForSync bool `json:"waitForSync,omitempty"`
// Available from 3.6 ArangoD version.
WriteConcern int `json:"writeConcern,omitempty"`
// Available from 3.10 ArangoD version.
ComputedValues []ComputedValue `json:"computedValues,omitempty"`
}
func (p *InventoryCollectionParameters) asInternal() inventoryCollectionParametersInternal {
lastValue := p.KeyOptions.LastValueV2
if lastValue == 0 && p.KeyOptions.LastValue != 0 {
lastValue = uint64(p.KeyOptions.LastValue)
}
return inventoryCollectionParametersInternal{
CacheEnabled: p.CacheEnabled,
Deleted: p.Deleted,
DistributeShardsLike: p.DistributeShardsLike,
DoCompact: p.DoCompact,
GloballyUniqueId: p.GloballyUniqueId,
ID: p.ID,
IndexBuckets: p.IndexBuckets,
Indexes: p.Indexes,
InternalValidatorType: p.InternalValidatorType,
IsDisjoint: p.IsDisjoint,
IsSmart: p.IsSmart,
IsSmartChild: p.IsSmartChild,
IsSystem: p.IsSystem,
IsVolatile: p.IsVolatile,
JournalSize: p.JournalSize,
KeyOptions: struct {
AllowUserKeys bool `json:"allowUserKeys,omitempty"`
LastValue uint64 `json:"lastValue,omitempty"`
Type string `json:"type,omitempty"`
}{
p.KeyOptions.AllowUserKeys,
lastValue,
p.KeyOptions.Type},
MinReplicationFactor: p.MinReplicationFactor,
Name: p.Name,
NumberOfShards: p.NumberOfShards,
Path: p.Path,
PlanID: p.PlanID,
ReplicationFactor: replicationFactor(p.ReplicationFactor),
Schema: p.Schema,
ShadowCollections: p.ShadowCollections,
ShardingStrategy: p.ShardingStrategy,
ShardKeys: p.ShardKeys,
Shards: p.Shards,
SmartGraphAttribute: p.SmartGraphAttribute,
SmartJoinAttribute: p.SmartJoinAttribute,
Status: p.Status,
SyncByRevision: p.SyncByRevision,
Type: p.Type,
UsesRevisionsAsDocumentIds: p.UsesRevisionsAsDocumentIds,
WaitForSync: p.WaitForSync,
WriteConcern: p.WriteConcern,
ComputedValues: p.ComputedValues,
}
}
func (p *InventoryCollectionParameters) fromInternal(i inventoryCollectionParametersInternal) {
*p = i.asExternal()
}
func (p *inventoryCollectionParametersInternal) asExternal() InventoryCollectionParameters {
return InventoryCollectionParameters{
CacheEnabled: p.CacheEnabled,
Deleted: p.Deleted,
DistributeShardsLike: p.DistributeShardsLike,
DoCompact: p.DoCompact,
GloballyUniqueId: p.GloballyUniqueId,
ID: p.ID,
IndexBuckets: p.IndexBuckets,
Indexes: p.Indexes,
InternalValidatorType: p.InternalValidatorType,
IsDisjoint: p.IsDisjoint,
IsSmart: p.IsSmart,
IsSmartChild: p.IsSmartChild,
IsSystem: p.IsSystem,
IsVolatile: p.IsVolatile,
JournalSize: p.JournalSize,
KeyOptions: struct {
AllowUserKeys bool `json:"allowUserKeys,omitempty"`
LastValue int64 `json:"-"`
LastValueV2 uint64 `json:"lastValue,omitempty"`
Type string `json:"type,omitempty"`
}{
p.KeyOptions.AllowUserKeys,
// cast to int64 to keep backwards compatibility for most cases
int64(p.KeyOptions.LastValue),
p.KeyOptions.LastValue,
p.KeyOptions.Type},
MinReplicationFactor: p.MinReplicationFactor,
Name: p.Name,
NumberOfShards: p.NumberOfShards,
Path: p.Path,
PlanID: p.PlanID,
ReplicationFactor: int(p.ReplicationFactor),
Schema: p.Schema,
ShadowCollections: p.ShadowCollections,
ShardingStrategy: p.ShardingStrategy,
ShardKeys: p.ShardKeys,
Shards: p.Shards,
SmartGraphAttribute: p.SmartGraphAttribute,
SmartJoinAttribute: p.SmartJoinAttribute,
Status: p.Status,
SyncByRevision: p.SyncByRevision,
Type: p.Type,
UsesRevisionsAsDocumentIds: p.UsesRevisionsAsDocumentIds,
WaitForSync: p.WaitForSync,
WriteConcern: p.WriteConcern,
ComputedValues: p.ComputedValues,
}
}
// MarshalJSON converts InventoryCollectionParameters into json
func (p *InventoryCollectionParameters) MarshalJSON() ([]byte, error) {
return json.Marshal(p.asInternal())
}
// UnmarshalJSON loads InventoryCollectionParameters from json
func (p *InventoryCollectionParameters) UnmarshalJSON(d []byte) error {
var internal inventoryCollectionParametersInternal
if err := json.Unmarshal(d, &internal); err != nil {
return err
}
p.fromInternal(internal)
return nil
}
const (
replicationFactorSatelliteString string = "satellite"
)
// MarshalJSON marshals InventoryCollectionParameters to arangodb json representation
func (r replicationFactor) MarshalJSON() ([]byte, error) {
var replicationFactor interface{}
if int(r) == ReplicationFactorSatellite {
replicationFactor = replicationFactorSatelliteString
} else {
replicationFactor = int(r)
}
return json.Marshal(replicationFactor)
}
// UnmarshalJSON marshals InventoryCollectionParameters to arangodb json representation
func (r *replicationFactor) UnmarshalJSON(d []byte) error {
var internal interface{}
if err := json.Unmarshal(d, &internal); err != nil {
return err
}
if i, ok := internal.(float64); ok {
*r = replicationFactor(i)
return nil
} else if str, ok := internal.(string); ok {
if ok && str == replicationFactorSatelliteString {
*r = replicationFactor(ReplicationFactorSatellite)
return nil
}
}
return &json.UnmarshalTypeError{
Value: string(d),
Type: reflect.TypeOf(r).Elem(),
}
}

@ -0,0 +1,331 @@
//
// DISCLAIMER
//
// Copyright 2017-2021 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
//
// Author Ewout Prangsma
// Author Tomasz Mielech
//
package driver
import (
"context"
"time"
)
// Collection provides access to the information of a single collection, all its documents and all its indexes.
type Collection interface {
// Name returns the name of the collection.
Name() string
// Database returns the database containing the collection.
Database() Database
// Status fetches the current status of the collection.
Status(ctx context.Context) (CollectionStatus, error)
// Count fetches the number of document in the collection.
Count(ctx context.Context) (int64, error)
// Statistics returns the number of documents and additional statistical information about the collection.
Statistics(ctx context.Context) (CollectionStatistics, error)
// Revision fetches the revision ID of the collection.
// The revision ID is a server-generated string that clients can use to check whether data
// in a collection has changed since the last revision check.
Revision(ctx context.Context) (string, error)
// Checksum returns a checksum for the specified collection
// withRevisions - Whether to include document revision ids in the checksum calculation.
// withData - Whether to include document body data in the checksum calculation.
Checksum(ctx context.Context, withRevisions bool, withData bool) (CollectionChecksum, error)
// Properties fetches extended information about the collection.
Properties(ctx context.Context) (CollectionProperties, error)
// SetProperties changes properties of the collection.
SetProperties(ctx context.Context, options SetCollectionPropertiesOptions) error
// Shards fetches shards information of the collection.
Shards(ctx context.Context, details bool) (CollectionShards, error)
// Load the collection into memory.
Load(ctx context.Context) error
// Unload unloads the collection from memory.
Unload(ctx context.Context) error
// Remove removes the entire collection.
// If the collection does not exist, a NotFoundError is returned.
Remove(ctx context.Context) error
// Truncate removes all documents from the collection, but leaves the indexes intact.
Truncate(ctx context.Context) error
// All index functions
CollectionIndexes
// All document functions
CollectionDocuments
}
// CollectionChecksum contains information about a collection checksum response
type CollectionChecksum struct {
ArangoError
CollectionInfo
// The collection revision id as a string.
Revision string `json:"revision,omitempty"`
}
// CollectionInfo contains information about a collection
type CollectionInfo struct {
// The identifier of the collection.
ID string `json:"id,omitempty"`
// The name of the collection.
Name string `json:"name,omitempty"`
// The status of the collection
Status CollectionStatus `json:"status,omitempty"`
// StatusString represents status as a string.
StatusString string `json:"statusString,omitempty"`
// The type of the collection
Type CollectionType `json:"type,omitempty"`
// If true then the collection is a system collection.
IsSystem bool `json:"isSystem,omitempty"`
// Global unique name for the collection
GloballyUniqueId string `json:"globallyUniqueId,omitempty"`
// The calculated checksum as a number.
Checksum string `json:"checksum,omitempty"`
}
// CollectionProperties contains extended information about a collection.
type CollectionProperties struct {
CollectionInfo
ArangoError
// WaitForSync; If true then creating, changing or removing documents will wait until the data has been synchronized to disk.
WaitForSync bool `json:"waitForSync,omitempty"`
// DoCompact specifies whether or not the collection will be compacted.
DoCompact bool `json:"doCompact,omitempty"`
// JournalSize is the maximal size setting for journals / datafiles in bytes.
JournalSize int64 `json:"journalSize,omitempty"`
// CacheEnabled set cacheEnabled option in collection properties
CacheEnabled bool `json:"cacheEnabled,omitempty"`
// ComputedValues let configure collections to generate document attributes when documents are created or modified, using an AQL expression
ComputedValues []ComputedValue `json:"computedValues,omitempty"`
// KeyOptions
KeyOptions struct {
// Type specifies the type of the key generator. The currently available generators are traditional and autoincrement.
Type KeyGeneratorType `json:"type,omitempty"`
// AllowUserKeys; if set to true, then it is allowed to supply own key values in the _key attribute of a document.
// If set to false, then the key generator is solely responsible for generating keys and supplying own key values in
// the _key attribute of documents is considered an error.
AllowUserKeys bool `json:"allowUserKeys,omitempty"`
LastValue uint64 `json:"lastValue,omitempty"`
} `json:"keyOptions,omitempty"`
// NumberOfShards is the number of shards of the collection.
// Only available in cluster setup.
NumberOfShards int `json:"numberOfShards,omitempty"`
// ShardKeys contains the names of document attributes that are used to determine the target shard for documents.
// Only available in cluster setup.
ShardKeys []string `json:"shardKeys,omitempty"`
// ReplicationFactor contains how many copies of each shard are kept on different DBServers.
// Only available in cluster setup.
ReplicationFactor int `json:"-"`
// Deprecated: use 'WriteConcern' instead
MinReplicationFactor int `json:"minReplicationFactor,omitempty"`
// WriteConcern contains how many copies must be available before a collection can be written.
// It is required that 1 <= WriteConcern <= ReplicationFactor.
// Default is 1. Not available for satellite collections.
// Available from 3.6 arangod version.
WriteConcern int `json:"writeConcern,omitempty"`
// SmartJoinAttribute
// See documentation for smart joins.
// This requires ArangoDB Enterprise Edition.
SmartJoinAttribute string `json:"smartJoinAttribute,omitempty"`
// This attribute specifies the name of the sharding strategy to use for the collection.
// Can not be changed after creation.
ShardingStrategy ShardingStrategy `json:"shardingStrategy,omitempty"`
// This attribute specifies that the sharding of a collection follows that of another
// one.
DistributeShardsLike string `json:"distributeShardsLike,omitempty"`
// This attribute specifies if the new format introduced in 3.7 is used for this
// collection.
UsesRevisionsAsDocumentIds bool `json:"usesRevisionsAsDocumentIds,omitempty"`
// The following attribute specifies if the new MerkleTree based sync protocol
// can be used on the collection.
SyncByRevision bool `json:"syncByRevision,omitempty"`
// The collection revision id as a string.
Revision string `json:"revision,omitempty"`
// Schema for collection validation
Schema *CollectionSchemaOptions `json:"schema,omitempty"`
// IsDisjoint set isDisjoint flag for Graph. Required ArangoDB 3.7+
IsDisjoint bool `json:"isDisjoint,omitempty"`
IsSmartChild bool `json:"isSmartChild,omitempty"`
InternalValidatorType *int `json:"internalValidatorType, omitempty"`
// Set to create a smart edge or vertex collection.
// This requires ArangoDB Enterprise Edition.
IsSmart bool `json:"isSmart,omitempty"`
// StatusString represents status as a string.
StatusString string `json:"statusString,omitempty"`
TempObjectId string `json:"tempObjectId,omitempty"`
ObjectId string `json:"objectId,omitempty"`
}
const (
// ReplicationFactorSatellite represents a satellite collection's replication factor
ReplicationFactorSatellite int = -1
)
// IsSatellite returns true if the collection is a satellite collection
func (p *CollectionProperties) IsSatellite() bool {
return p.ReplicationFactor == ReplicationFactorSatellite
}
// SetCollectionPropertiesOptions contains data for Collection.SetProperties.
type SetCollectionPropertiesOptions struct {
// If true then creating or changing a document will wait until the data has been synchronized to disk.
WaitForSync *bool `json:"waitForSync,omitempty"`
// The maximal size of a journal or datafile in bytes. The value must be at least 1048576 (1 MB). Note that when changing the journalSize value, it will only have an effect for additional journals or datafiles that are created. Already existing journals or datafiles will not be affected.
JournalSize int64 `json:"journalSize,omitempty"`
// ReplicationFactor contains how many copies of each shard are kept on different DBServers.
// Only available in cluster setup.
ReplicationFactor int `json:"replicationFactor,omitempty"`
// Deprecated: use 'WriteConcern' instead
MinReplicationFactor int `json:"minReplicationFactor,omitempty"`
// WriteConcern contains how many copies must be available before a collection can be written.
// Available from 3.6 arangod version.
WriteConcern int `json:"writeConcern,omitempty"`
// CacheEnabled set cacheEnabled option in collection properties
CacheEnabled *bool `json:"cacheEnabled,omitempty"`
// Schema for collection validation
Schema *CollectionSchemaOptions `json:"schema,omitempty"`
// ComputedValues let configure collections to generate document attributes when documents are created or modified, using an AQL expression
ComputedValues []ComputedValue `json:"computedValues,omitempty"`
}
// CollectionStatus indicates the status of a collection.
type CollectionStatus int
const (
CollectionStatusNewBorn = CollectionStatus(1)
CollectionStatusUnloaded = CollectionStatus(2)
CollectionStatusLoaded = CollectionStatus(3)
CollectionStatusUnloading = CollectionStatus(4)
CollectionStatusDeleted = CollectionStatus(5)
CollectionStatusLoading = CollectionStatus(6)
)
// CollectionStatistics contains the number of documents and additional statistical information about a collection.
type CollectionStatistics struct {
ArangoError
CollectionProperties
//The number of documents currently present in the collection.
Count int64 `json:"count,omitempty"`
// The maximal size of a journal or datafile in bytes.
JournalSize int64 `json:"journalSize,omitempty"`
Figures struct {
DataFiles struct {
// The number of datafiles.
Count int64 `json:"count,omitempty"`
// The total filesize of datafiles (in bytes).
FileSize int64 `json:"fileSize,omitempty"`
} `json:"datafiles"`
// The number of markers in the write-ahead log for this collection that have not been transferred to journals or datafiles.
UncollectedLogfileEntries int64 `json:"uncollectedLogfileEntries,omitempty"`
// The number of references to documents in datafiles that JavaScript code currently holds. This information can be used for debugging compaction and unload issues.
DocumentReferences int64 `json:"documentReferences,omitempty"`
CompactionStatus struct {
// The action that was performed when the compaction was last run for the collection. This information can be used for debugging compaction issues.
Message string `json:"message,omitempty"`
// The point in time the compaction for the collection was last executed. This information can be used for debugging compaction issues.
Time time.Time `json:"time,omitempty"`
} `json:"compactionStatus"`
Compactors struct {
// The number of compactor files.
Count int64 `json:"count,omitempty"`
// The total filesize of all compactor files (in bytes).
FileSize int64 `json:"fileSize,omitempty"`
} `json:"compactors"`
Dead struct {
// The number of dead documents. This includes document versions that have been deleted or replaced by a newer version. Documents deleted or replaced that are contained the write-ahead log only are not reported in this figure.
Count int64 `json:"count,omitempty"`
// The total number of deletion markers. Deletion markers only contained in the write-ahead log are not reporting in this figure.
Deletion int64 `json:"deletion,omitempty"`
// The total size in bytes used by all dead documents.
Size int64 `json:"size,omitempty"`
} `json:"dead"`
Indexes struct {
// The total number of indexes defined for the collection, including the pre-defined indexes (e.g. primary index).
Count int64 `json:"count,omitempty"`
// The total memory allocated for indexes in bytes.
Size int64 `json:"size,omitempty"`
} `json:"indexes"`
ReadCache struct {
// The number of revisions of this collection stored in the document revisions cache.
Count int64 `json:"count,omitempty"`
// The memory used for storing the revisions of this collection in the document revisions cache (in bytes). This figure does not include the document data but only mappings from document revision ids to cache entry locations.
Size int64 `json:"size,omitempty"`
} `json:"readcache"`
// An optional string value that contains information about which object type is at the head of the collection's cleanup queue. This information can be used for debugging compaction and unload issues.
WaitingFor string `json:"waitingFor,omitempty"`
Alive struct {
// The number of currently active documents in all datafiles and journals of the collection. Documents that are contained in the write-ahead log only are not reported in this figure.
Count int64 `json:"count,omitempty"`
// The total size in bytes used by all active documents of the collection. Documents that are contained in the write-ahead log only are not reported in this figure.
Size int64 `json:"size,omitempty"`
} `json:"alive"`
// The tick of the last marker that was stored in a journal of the collection. This might be 0 if the collection does not yet have a journal.
LastTick int64 `json:"lastTick,omitempty"`
Journals struct {
// The number of journal files.
Count int64 `json:"count,omitempty"`
// The total filesize of all journal files (in bytes).
FileSize int64 `json:"fileSize,omitempty"`
} `json:"journals"`
Revisions struct {
// The number of revisions of this collection managed by the storage engine.
Count int64 `json:"count,omitempty"`
// The memory used for storing the revisions of this collection in the storage engine (in bytes). This figure does not include the document data but only mappings from document revision ids to storage engine datafile positions.
Size int64 `json:"size,omitempty"`
} `json:"revisions"`
DocumentsSize *int64 `json:"documentsSize,omitempty"`
// RocksDB cache statistics
CacheInUse *bool `json:"cacheInUse,omitempty"`
CacheSize *int64 `json:"cacheSize,omitempty"`
CacheUsage *int64 `json:"cacheUsage,omitempty"`
} `json:"figures"`
}
// CollectionShards contains shards information about a collection.
type CollectionShards struct {
CollectionProperties
// Shards is a list of shards that belong to the collection.
// Each shard contains a list of DB servers where the first one is the leader and the rest are followers.
Shards map[ShardID][]ServerID `json:"shards,omitempty"`
}

@ -0,0 +1,677 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
"fmt"
"path"
"reflect"
)
// DocumentExists checks if a document with given key exists in the collection.
func (c *collection) DocumentExists(ctx context.Context, key string) (bool, error) {
if err := validateKey(key); err != nil {
return false, WithStack(err)
}
escapedKey := pathEscape(key)
req, err := c.conn.NewRequest("HEAD", path.Join(c.relPath("document"), escapedKey))
if err != nil {
return false, WithStack(err)
}
applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return false, WithStack(err)
}
found := resp.StatusCode() == 200
return found, 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 *collection) ReadDocument(ctx context.Context, key string, result interface{}) (DocumentMeta, error) {
if err := validateKey(key); err != nil {
return DocumentMeta{}, WithStack(err)
}
escapedKey := pathEscape(key)
req, err := c.conn.NewRequest("GET", path.Join(c.relPath("document"), escapedKey))
if err != nil {
return DocumentMeta{}, WithStack(err)
}
// This line introduces a lot of side effects. In particular If-Match headers are now set (which is a bugfix)
// and invalid query parameters like waitForSync (which is potentially breaking change)
cs := applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return DocumentMeta{}, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return DocumentMeta{}, WithStack(err)
}
// Parse metadata
var meta DocumentMeta
if err := resp.ParseBody("", &meta); err != nil {
return DocumentMeta{}, WithStack(err)
}
// load context response values
loadContextResponseValues(cs, resp)
// Parse result
if result != nil {
if err := resp.ParseBody("", result); err != nil {
return meta, WithStack(err)
}
}
return meta, 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 *collection) 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)
}
}
req, err := c.conn.NewRequest("PUT", c.relPath("document"))
if err != nil {
return nil, nil, WithStack(err)
}
req = req.SetQuery("onlyget", "1")
cs := applyContextSettings(ctx, req)
if _, err := req.SetBodyArray(keys, nil); err != nil {
return nil, nil, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return nil, nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, nil, WithStack(err)
}
// load context response values
loadContextResponseValues(cs, resp)
// Parse response array
metas, errs, err := parseResponseArray(resp, resultCount, cs, results)
if err != nil {
return nil, nil, WithStack(err)
}
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 *collection) CreateDocument(ctx context.Context, document interface{}) (DocumentMeta, error) {
if document == nil {
return DocumentMeta{}, WithStack(InvalidArgumentError{Message: "document nil"})
}
req, err := c.conn.NewRequest("POST", c.relPath("document"))
if err != nil {
return DocumentMeta{}, WithStack(err)
}
if _, err := req.SetBody(document); err != nil {
return DocumentMeta{}, WithStack(err)
}
cs := applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return DocumentMeta{}, WithStack(err)
}
if err := resp.CheckStatus(201, 202); err != nil {
return DocumentMeta{}, WithStack(err)
}
if cs.Silent {
// Empty response, we're done
return DocumentMeta{}, nil
}
// Parse metadata
var meta DocumentMeta
if err := resp.ParseBody("", &meta); err != nil {
return DocumentMeta{}, WithStack(err)
}
// Parse returnNew (if needed)
if cs.ReturnNew != nil {
if err := resp.ParseBody("new", cs.ReturnNew); err != nil {
return meta, WithStack(err)
}
}
return meta, 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 in its inded 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 *collection) 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()
req, err := c.conn.NewRequest("POST", c.relPath("document"))
if err != nil {
return nil, nil, WithStack(err)
}
if _, err := req.SetBody(documents); err != nil {
return nil, nil, WithStack(err)
}
cs := applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return nil, nil, WithStack(err)
}
if err := resp.CheckStatus(201, 202); err != nil {
return nil, nil, WithStack(err)
}
if cs.Silent {
// Empty response, we're done
return nil, nil, nil
}
// Parse response array
metas, errs, err := parseResponseArray(resp, documentCount, cs, nil)
if err != nil {
return nil, nil, WithStack(err)
}
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 *collection) UpdateDocument(ctx context.Context, key string, update interface{}) (DocumentMeta, error) {
if err := validateKey(key); err != nil {
return DocumentMeta{}, WithStack(err)
}
if update == nil {
return DocumentMeta{}, WithStack(InvalidArgumentError{Message: "update nil"})
}
escapedKey := pathEscape(key)
req, err := c.conn.NewRequest("PATCH", path.Join(c.relPath("document"), escapedKey))
if err != nil {
return DocumentMeta{}, WithStack(err)
}
if _, err := req.SetBody(update); err != nil {
return DocumentMeta{}, WithStack(err)
}
cs := applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return DocumentMeta{}, WithStack(err)
}
if err := resp.CheckStatus(201, 202); err != nil {
return DocumentMeta{}, WithStack(err)
}
if cs.Silent {
// Empty response, we're done
return DocumentMeta{}, nil
}
// Parse metadata
var meta DocumentMeta
if err := resp.ParseBody("", &meta); err != nil {
return DocumentMeta{}, WithStack(err)
}
// Parse returnOld (if needed)
if cs.ReturnOld != nil {
if err := resp.ParseBody("old", cs.ReturnOld); err != nil {
return meta, WithStack(err)
}
}
// Parse returnNew (if needed)
if cs.ReturnNew != nil {
if err := resp.ParseBody("new", cs.ReturnNew); err != nil {
return meta, WithStack(err)
}
}
return meta, 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 *collection) 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)
}
}
}
req, err := c.conn.NewRequest("PATCH", c.relPath("document"))
if err != nil {
return nil, nil, WithStack(err)
}
cs := applyContextSettings(ctx, req)
mergeArray, err := createMergeArray(keys, cs.Revisions)
if err != nil {
return nil, nil, WithStack(err)
}
if _, err := req.SetBodyArray(updates, mergeArray); err != nil {
return nil, nil, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return nil, nil, WithStack(err)
}
if err := resp.CheckStatus(201, 202); err != nil {
return nil, nil, WithStack(err)
}
if cs.Silent {
// Empty response, we're done
return nil, nil, nil
}
// Parse response array
metas, errs, err := parseResponseArray(resp, updateCount, cs, nil)
if err != nil {
return nil, nil, WithStack(err)
}
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 *collection) ReplaceDocument(ctx context.Context, key string, document interface{}) (DocumentMeta, error) {
if err := validateKey(key); err != nil {
return DocumentMeta{}, WithStack(err)
}
if document == nil {
return DocumentMeta{}, WithStack(InvalidArgumentError{Message: "document nil"})
}
escapedKey := pathEscape(key)
req, err := c.conn.NewRequest("PUT", path.Join(c.relPath("document"), escapedKey))
if err != nil {
return DocumentMeta{}, WithStack(err)
}
if _, err := req.SetBody(document); err != nil {
return DocumentMeta{}, WithStack(err)
}
cs := applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return DocumentMeta{}, WithStack(err)
}
if err := resp.CheckStatus(201, 202); err != nil {
return DocumentMeta{}, WithStack(err)
}
if cs.Silent {
// Empty response, we're done
return DocumentMeta{}, nil
}
// Parse metadata
var meta DocumentMeta
if err := resp.ParseBody("", &meta); err != nil {
return DocumentMeta{}, WithStack(err)
}
// Parse returnOld (if needed)
if cs.ReturnOld != nil {
if err := resp.ParseBody("old", cs.ReturnOld); err != nil {
return meta, WithStack(err)
}
}
// Parse returnNew (if needed)
if cs.ReturnNew != nil {
if err := resp.ParseBody("new", cs.ReturnNew); err != nil {
return meta, WithStack(err)
}
}
return meta, 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 *collection) 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)
}
}
}
req, err := c.conn.NewRequest("PUT", c.relPath("document"))
if err != nil {
return nil, nil, WithStack(err)
}
cs := applyContextSettings(ctx, req)
mergeArray, err := createMergeArray(keys, cs.Revisions)
if err != nil {
return nil, nil, WithStack(err)
}
if _, err := req.SetBodyArray(documents, mergeArray); err != nil {
return nil, nil, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return nil, nil, WithStack(err)
}
if err := resp.CheckStatus(201, 202); err != nil {
return nil, nil, WithStack(err)
}
if cs.Silent {
// Empty response, we're done
return nil, nil, nil
}
// Parse response array
metas, errs, err := parseResponseArray(resp, documentCount, cs, nil)
if err != nil {
return nil, nil, WithStack(err)
}
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 *collection) RemoveDocument(ctx context.Context, key string) (DocumentMeta, error) {
if err := validateKey(key); err != nil {
return DocumentMeta{}, WithStack(err)
}
escapedKey := pathEscape(key)
req, err := c.conn.NewRequest("DELETE", path.Join(c.relPath("document"), escapedKey))
if err != nil {
return DocumentMeta{}, WithStack(err)
}
cs := applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return DocumentMeta{}, WithStack(err)
}
if err := resp.CheckStatus(200, 202); err != nil {
return DocumentMeta{}, WithStack(err)
}
if cs.Silent {
// Empty response, we're done
return DocumentMeta{}, nil
}
// Parse metadata
var meta DocumentMeta
if err := resp.ParseBody("", &meta); err != nil {
return DocumentMeta{}, WithStack(err)
}
// Parse returnOld (if needed)
if cs.ReturnOld != nil {
if err := resp.ParseBody("old", cs.ReturnOld); err != nil {
return meta, WithStack(err)
}
}
return meta, 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 *collection) RemoveDocuments(ctx context.Context, keys []string) (DocumentMetaSlice, ErrorSlice, error) {
for _, key := range keys {
if err := validateKey(key); err != nil {
return nil, nil, WithStack(err)
}
}
keyCount := len(keys)
req, err := c.conn.NewRequest("DELETE", c.relPath("document"))
if err != nil {
return nil, nil, WithStack(err)
}
cs := applyContextSettings(ctx, req)
metaArray, err := createMergeArray(keys, cs.Revisions)
if err != nil {
return nil, nil, WithStack(err)
}
if _, err := req.SetBodyArray(metaArray, nil); err != nil {
return nil, nil, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return nil, nil, WithStack(err)
}
if err := resp.CheckStatus(200, 202); err != nil {
return nil, nil, WithStack(err)
}
if cs.Silent {
// Empty response, we're done
return nil, nil, nil
}
// Parse response array
metas, errs, err := parseResponseArray(resp, keyCount, cs, nil)
if err != nil {
return nil, nil, WithStack(err)
}
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 *collection) ImportDocuments(ctx context.Context, documents interface{}, options *ImportDocumentOptions) (ImportDocumentStatistics, error) {
documentsVal := reflect.ValueOf(documents)
switch documentsVal.Kind() {
case reflect.Array, reflect.Slice:
// OK
default:
return ImportDocumentStatistics{}, WithStack(InvalidArgumentError{Message: fmt.Sprintf("documents data must be of kind Array, got %s", documentsVal.Kind())})
}
req, err := c.conn.NewRequest("POST", path.Join(c.db.relPath(), "_api/import"))
if err != nil {
return ImportDocumentStatistics{}, WithStack(err)
}
req.SetQuery("collection", c.name)
req.SetQuery("type", "documents")
if options != nil {
if v := options.FromPrefix; v != "" {
req.SetQuery("fromPrefix", v)
}
if v := options.ToPrefix; v != "" {
req.SetQuery("toPrefix", v)
}
if v := options.Overwrite; v {
req.SetQuery("overwrite", "true")
}
if v := options.OnDuplicate; v != "" {
req.SetQuery("onDuplicate", string(v))
}
if v := options.Complete; v {
req.SetQuery("complete", "true")
}
}
if _, err := req.SetBodyImportArray(documents); err != nil {
return ImportDocumentStatistics{}, WithStack(err)
}
cs := applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return ImportDocumentStatistics{}, WithStack(err)
}
if err := resp.CheckStatus(201); err != nil {
return ImportDocumentStatistics{}, WithStack(err)
}
// Parse response
var data ImportDocumentStatistics
if err := resp.ParseBody("", &data); err != nil {
return ImportDocumentStatistics{}, WithStack(err)
}
// Import details (if needed)
if details := cs.ImportDetails; details != nil {
if err := resp.ParseBody("details", details); err != nil {
return ImportDocumentStatistics{}, WithStack(err)
}
}
return data, nil
}
// createMergeArray returns an array of metadata maps with `_key` and/or `_rev` elements.
func createMergeArray(keys, revs []string) ([]map[string]interface{}, error) {
if keys == nil && revs == nil {
return nil, nil
}
if revs == nil {
mergeArray := make([]map[string]interface{}, len(keys))
for i, k := range keys {
mergeArray[i] = map[string]interface{}{
"_key": k,
}
}
return mergeArray, nil
}
if keys == nil {
mergeArray := make([]map[string]interface{}, len(revs))
for i, r := range revs {
mergeArray[i] = map[string]interface{}{
"_rev": r,
}
}
return mergeArray, nil
}
if len(keys) != len(revs) {
return nil, WithStack(InvalidArgumentError{Message: fmt.Sprintf("#keys must be equal to #revs, got %d, %d", len(keys), len(revs))})
}
mergeArray := make([]map[string]interface{}, len(keys))
for i, k := range keys {
mergeArray[i] = map[string]interface{}{
"_key": k,
"_rev": revs[i],
}
}
return mergeArray, nil
}
// parseResponseArray parses an array response in the given response
func parseResponseArray(resp Response, count int, cs contextSettings, results interface{}) (DocumentMetaSlice, ErrorSlice, error) {
resps, err := resp.ParseArrayBody()
if err != nil {
return nil, nil, WithStack(err)
}
metas := make(DocumentMetaSlice, count)
errs := make(ErrorSlice, count)
returnOldVal := reflect.ValueOf(cs.ReturnOld)
returnNewVal := reflect.ValueOf(cs.ReturnNew)
resultsVal := reflect.ValueOf(results)
for i := 0; i < count; i++ {
resp := resps[i]
var meta DocumentMeta
if err := resp.CheckStatus(200, 201, 202); err != nil {
errs[i] = err
} else {
if err := resp.ParseBody("", &meta); err != nil {
errs[i] = err
} else {
metas[i] = meta
// Parse returnOld (if needed)
if cs.ReturnOld != nil {
returnOldEntryVal := returnOldVal.Index(i).Addr()
if err := resp.ParseBody("old", returnOldEntryVal.Interface()); err != nil {
errs[i] = err
}
}
// Parse returnNew (if needed)
if cs.ReturnNew != nil {
returnNewEntryVal := returnNewVal.Index(i).Addr()
if err := resp.ParseBody("new", returnNewEntryVal.Interface()); err != nil {
errs[i] = err
}
}
}
if results != nil {
// Parse compare result document
resultsEntryVal := resultsVal.Index(i).Addr()
if err := resp.ParseBody("", resultsEntryVal.Interface()); err != nil {
errs[i] = err
}
}
}
}
return metas, errs, nil
}

@ -0,0 +1,178 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import "context"
// CollectionDocuments provides access to the documents in a single collection.
type CollectionDocuments interface {
// DocumentExists checks if a document with given key exists in the collection.
DocumentExists(ctx context.Context, key string) (bool, error)
// 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.
ReadDocument(ctx context.Context, key string, result interface{}) (DocumentMeta, error)
// 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.
ReadDocuments(ctx context.Context, keys []string, results interface{}) (DocumentMetaSlice, ErrorSlice, error)
// 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`.
CreateDocument(ctx context.Context, document interface{}) (DocumentMeta, error)
// 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 in 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.
CreateDocuments(ctx context.Context, documents interface{}) (DocumentMetaSlice, ErrorSlice, error)
// 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.
UpdateDocument(ctx context.Context, key string, update interface{}) (DocumentMeta, error)
// 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.
// If keys is nil, each element in the updates slice must contain a `_key` field.
UpdateDocuments(ctx context.Context, keys []string, updates interface{}) (DocumentMetaSlice, ErrorSlice, error)
// 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.
ReplaceDocument(ctx context.Context, key string, document interface{}) (DocumentMeta, error)
// 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.
// If keys is nil, each element in the documents slice must contain a `_key` field.
ReplaceDocuments(ctx context.Context, keys []string, documents interface{}) (DocumentMetaSlice, ErrorSlice, error)
// 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.
RemoveDocument(ctx context.Context, key string) (DocumentMeta, error)
// 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.
RemoveDocuments(ctx context.Context, keys []string) (DocumentMetaSlice, ErrorSlice, error)
// 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`.
ImportDocuments(ctx context.Context, documents interface{}, options *ImportDocumentOptions) (ImportDocumentStatistics, error)
}
// ImportDocumentOptions holds optional options that control the import document process.
type ImportDocumentOptions struct {
// FromPrefix is an optional prefix for the values in _from attributes. If specified, the value is automatically
// prepended to each _from input value. This allows specifying just the keys for _from.
FromPrefix string `json:"fromPrefix,omitempty"`
// ToPrefix is an optional prefix for the values in _to attributes. If specified, the value is automatically
// prepended to each _to input value. This allows specifying just the keys for _to.
ToPrefix string `json:"toPrefix,omitempty"`
// Overwrite is a flag that if set, then all data in the collection will be removed prior to the import.
// Note that any existing index definitions will be preseved.
Overwrite bool `json:"overwrite,omitempty"`
// OnDuplicate controls what action is carried out in case of a unique key constraint violation.
// Possible values are:
// - ImportOnDuplicateError
// - ImportOnDuplicateUpdate
// - ImportOnDuplicateReplace
// - ImportOnDuplicateIgnore
OnDuplicate ImportOnDuplicate `json:"onDuplicate,omitempty"`
// Complete is a flag that if set, will make the whole import fail if any error occurs.
// Otherwise the import will continue even if some documents cannot be imported.
Complete bool `json:"complete,omitempty"`
}
// ImportOnDuplicate is a type to control what action is carried out in case of a unique key constraint violation.
type ImportOnDuplicate string
const (
// ImportOnDuplicateError will not import the current document because of the unique key constraint violation.
// This is the default setting.
ImportOnDuplicateError = ImportOnDuplicate("error")
// ImportOnDuplicateUpdate will update an existing document in the database with the data specified in the request.
// Attributes of the existing document that are not present in the request will be preserved.
ImportOnDuplicateUpdate = ImportOnDuplicate("update")
// ImportOnDuplicateReplace will replace an existing document in the database with the data specified in the request.
ImportOnDuplicateReplace = ImportOnDuplicate("replace")
// ImportOnDuplicateIgnore will not update an existing document and simply ignore the error caused by a unique key constraint violation.
ImportOnDuplicateIgnore = ImportOnDuplicate("ignore")
)
// ImportDocumentStatistics holds statistics of an import action.
type ImportDocumentStatistics struct {
// Created holds the number of documents imported.
Created int64 `json:"created,omitempty"`
// Errors holds the number of documents that were not imported due to an error.
Errors int64 `json:"errors,omitempty"`
// Empty holds the number of empty lines found in the input (will only contain a value greater zero for types documents or auto).
Empty int64 `json:"empty,omitempty"`
// Updated holds the number of updated/replaced documents (in case onDuplicate was set to either update or replace).
Updated int64 `json:"updated,omitempty"`
// Ignored holds the number of failed but ignored insert operations (in case onDuplicate was set to ignore).
Ignored int64 `json:"ignored,omitempty"`
// if query parameter details is set to true, the result will contain a details attribute which is an array
// with more detailed information about which documents could not be inserted.
Details []string
ArangoError
}

@ -0,0 +1,387 @@
//
// DISCLAIMER
//
// Copyright 2017-2021 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
//
// Author Ewout Prangsma
// Author Tomasz Mielech
//
package driver
import (
"context"
"encoding/json"
"path"
)
// newCollection creates a new Collection implementation.
func newCollection(name string, db *database) (Collection, error) {
if name == "" {
return nil, WithStack(InvalidArgumentError{Message: "name is empty"})
}
if db == nil {
return nil, WithStack(InvalidArgumentError{Message: "db is nil"})
}
return &collection{
name: name,
db: db,
conn: db.conn,
}, nil
}
type collection struct {
name string
db *database
conn Connection
}
// relPath creates the relative path to this collection (`_db/<db-name>/_api/<api-name>/<col-name>`)
func (c *collection) relPath(apiName string) string {
escapedName := pathEscape(c.name)
return path.Join(c.db.relPath(), "_api", apiName, escapedName)
}
// Name returns the name of the collection.
func (c *collection) Name() string {
return c.name
}
// Database returns the database containing the collection.
func (c *collection) Database() Database {
return c.db
}
// Status fetches the current status of the collection.
func (c *collection) Status(ctx context.Context) (CollectionStatus, error) {
req, err := c.conn.NewRequest("GET", c.relPath("collection"))
if err != nil {
return CollectionStatus(0), WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return CollectionStatus(0), WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return CollectionStatus(0), WithStack(err)
}
var data CollectionInfo
if err := resp.ParseBody("", &data); err != nil {
return CollectionStatus(0), WithStack(err)
}
return data.Status, nil
}
// Count fetches the number of document in the collection.
func (c *collection) Count(ctx context.Context) (int64, error) {
req, err := c.conn.NewRequest("GET", path.Join(c.relPath("collection"), "count"))
if err != nil {
return 0, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return 0, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return 0, WithStack(err)
}
var data struct {
Count int64 `json:"count,omitempty"`
}
if err := resp.ParseBody("", &data); err != nil {
return 0, WithStack(err)
}
return data.Count, nil
}
// Statistics returns the number of documents and additional statistical information about the collection.
func (c *collection) Statistics(ctx context.Context) (CollectionStatistics, error) {
req, err := c.conn.NewRequest("GET", path.Join(c.relPath("collection"), "figures"))
if err != nil {
return CollectionStatistics{}, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return CollectionStatistics{}, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return CollectionStatistics{}, WithStack(err)
}
var data CollectionStatistics
if err := resp.ParseBody("", &data); err != nil {
return CollectionStatistics{}, WithStack(err)
}
return data, nil
}
// Revision fetches the revision ID of the collection.
// The revision ID is a server-generated string that clients can use to check whether data
// in a collection has changed since the last revision check.
func (c *collection) Revision(ctx context.Context) (string, error) {
req, err := c.conn.NewRequest("GET", path.Join(c.relPath("collection"), "revision"))
if err != nil {
return "", WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return "", WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return "", WithStack(err)
}
var data CollectionProperties
if err := resp.ParseBody("", &data); err != nil {
return "", WithStack(err)
}
return data.Revision, nil
}
// Checksum returns a checksum for the specified collection
// withRevisions - Whether to include document revision ids in the checksum calculation.
// withData - Whether to include document body data in the checksum calculation.
func (c *collection) Checksum(ctx context.Context, withRevisions bool, withData bool) (CollectionChecksum, error) {
var data CollectionChecksum
req, err := c.conn.NewRequest("GET", path.Join(c.relPath("collection"), "checksum"))
if err != nil {
return data, WithStack(err)
}
if withRevisions {
req.SetQuery("withRevisions", "true")
}
if withData {
req.SetQuery("withData", "true")
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return data, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return data, WithStack(err)
}
if err := resp.ParseBody("", &data); err != nil {
return data, WithStack(err)
}
return data, nil
}
// Properties fetches extended information about the collection.
func (c *collection) Properties(ctx context.Context) (CollectionProperties, error) {
req, err := c.conn.NewRequest("GET", path.Join(c.relPath("collection"), "properties"))
if err != nil {
return CollectionProperties{}, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return CollectionProperties{}, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return CollectionProperties{}, WithStack(err)
}
var data struct {
CollectionProperties
ReplicationFactorV2 replicationFactor `json:"replicationFactor,omitempty"`
}
if err := resp.ParseBody("", &data); err != nil {
return CollectionProperties{}, WithStack(err)
}
data.ReplicationFactor = int(data.ReplicationFactorV2)
return data.CollectionProperties, nil
}
// SetProperties changes properties of the collection.
func (c *collection) SetProperties(ctx context.Context, options SetCollectionPropertiesOptions) error {
req, err := c.conn.NewRequest("PUT", path.Join(c.relPath("collection"), "properties"))
if err != nil {
return WithStack(err)
}
if _, err := req.SetBody(options.asInternal()); err != nil {
return WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return WithStack(err)
}
return nil
}
// Shards fetches shards information of the collection.
func (c *collection) Shards(ctx context.Context, details bool) (CollectionShards, error) {
req, err := c.conn.NewRequest("GET", path.Join(c.relPath("collection"), "shards"))
if err != nil {
return CollectionShards{}, WithStack(err)
}
if details {
req.SetQuery("details", "true")
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return CollectionShards{}, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return CollectionShards{}, WithStack(err)
}
var data struct {
CollectionShards
ReplicationFactorV2 replicationFactor `json:"replicationFactor,omitempty"`
}
if err := resp.ParseBody("", &data); err != nil {
return CollectionShards{}, WithStack(err)
}
data.ReplicationFactor = int(data.ReplicationFactorV2)
return data.CollectionShards, nil
}
// Load the collection into memory.
func (c *collection) Load(ctx context.Context) error {
req, err := c.conn.NewRequest("PUT", path.Join(c.relPath("collection"), "load"))
if err != nil {
return WithStack(err)
}
opts := struct {
Count bool `json:"count"`
}{
Count: false,
}
if _, err := req.SetBody(opts); err != nil {
return WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return WithStack(err)
}
return nil
}
// UnLoad the collection from memory.
func (c *collection) Unload(ctx context.Context) error {
req, err := c.conn.NewRequest("PUT", path.Join(c.relPath("collection"), "unload"))
if err != nil {
return WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return WithStack(err)
}
return nil
}
// Remove removes the entire collection.
// If the collection does not exist, a NotFoundError is returned.
func (c *collection) Remove(ctx context.Context) error {
req, err := c.conn.NewRequest("DELETE", c.relPath("collection"))
if err != nil {
return WithStack(err)
}
applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return WithStack(err)
}
return nil
}
// Truncate removes all documents from the collection, but leaves the indexes intact.
func (c *collection) Truncate(ctx context.Context) error {
req, err := c.conn.NewRequest("PUT", path.Join(c.relPath("collection"), "truncate"))
if err != nil {
return WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return WithStack(err)
}
return nil
}
type setCollectionPropertiesOptionsInternal struct {
WaitForSync *bool `json:"waitForSync,omitempty"`
JournalSize int64 `json:"journalSize,omitempty"`
ReplicationFactor replicationFactor `json:"replicationFactor,omitempty"`
CacheEnabled *bool `json:"cacheEnabled,omitempty"`
ComputedValues []ComputedValue `json:"computedValues,omitempty"`
// Deprecated: use 'WriteConcern' instead
MinReplicationFactor int `json:"minReplicationFactor,omitempty"`
// Available from 3.6 arangod version.
WriteConcern int `json:"writeConcern,omitempty"`
Schema *CollectionSchemaOptions `json:"schema,omitempty"`
}
func (p *SetCollectionPropertiesOptions) asInternal() setCollectionPropertiesOptionsInternal {
return setCollectionPropertiesOptionsInternal{
WaitForSync: p.WaitForSync,
JournalSize: p.JournalSize,
CacheEnabled: p.CacheEnabled,
ComputedValues: p.ComputedValues,
ReplicationFactor: replicationFactor(p.ReplicationFactor),
MinReplicationFactor: p.MinReplicationFactor,
WriteConcern: p.WriteConcern,
Schema: p.Schema,
}
}
func (p *SetCollectionPropertiesOptions) fromInternal(i *setCollectionPropertiesOptionsInternal) {
p.WaitForSync = i.WaitForSync
p.JournalSize = i.JournalSize
p.CacheEnabled = i.CacheEnabled
p.ReplicationFactor = int(i.ReplicationFactor)
p.MinReplicationFactor = i.MinReplicationFactor
p.WriteConcern = i.WriteConcern
p.Schema = i.Schema
}
// MarshalJSON converts SetCollectionPropertiesOptions into json
func (p *SetCollectionPropertiesOptions) MarshalJSON() ([]byte, error) {
return json.Marshal(p.asInternal())
}
// UnmarshalJSON loads SetCollectionPropertiesOptions from json
func (p *SetCollectionPropertiesOptions) UnmarshalJSON(d []byte) error {
var internal setCollectionPropertiesOptionsInternal
if err := json.Unmarshal(d, &internal); err != nil {
return err
}
p.fromInternal(&internal)
return nil
}

@ -0,0 +1,302 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import "context"
// CollectionIndexes provides access to the indexes in a single collection.
type CollectionIndexes interface {
// Index opens a connection to an existing index within the collection.
// If no index with given name exists, an NotFoundError is returned.
Index(ctx context.Context, name string) (Index, error)
// IndexExists returns true if an index with given name exists within the collection.
IndexExists(ctx context.Context, name string) (bool, error)
// Indexes returns a list of all indexes in the collection.
Indexes(ctx context.Context) ([]Index, error)
// Deprecated: since 3.10 version. Use ArangoSearch view instead.
// EnsureFullTextIndex creates a fulltext index in the collection, if it does not already exist.
// Fields is a slice of attribute names. Currently, the slice is limited to exactly one attribute.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
EnsureFullTextIndex(ctx context.Context, fields []string, options *EnsureFullTextIndexOptions) (Index, bool, error)
// EnsureGeoIndex creates a hash index in the collection, if it does not already exist.
// Fields is a slice with one or two attribute paths. If it is a slice with one attribute path location,
// then a geo-spatial index on all documents is created using location as path to the coordinates.
// The value of the attribute must be a slice with at least two double values. The slice must contain the latitude (first value)
// and the longitude (second value). All documents, which do not have the attribute path or with value that are not suitable, are ignored.
// If it is a slice with two attribute paths latitude and longitude, then a geo-spatial index on all documents is created
// using latitude and longitude as paths the latitude and the longitude. The value of the attribute latitude and of the
// attribute longitude must a double. All documents, which do not have the attribute paths or which values are not suitable, are ignored.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
EnsureGeoIndex(ctx context.Context, fields []string, options *EnsureGeoIndexOptions) (Index, bool, error)
// EnsureHashIndex creates a hash index in the collection, if it does not already exist.
// Fields is a slice of attribute paths.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
EnsureHashIndex(ctx context.Context, fields []string, options *EnsureHashIndexOptions) (Index, bool, error)
// EnsurePersistentIndex creates a persistent index in the collection, if it does not already exist.
// Fields is a slice of attribute paths.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
EnsurePersistentIndex(ctx context.Context, fields []string, options *EnsurePersistentIndexOptions) (Index, bool, error)
// EnsureSkipListIndex creates a skiplist index in the collection, if it does not already exist.
// Fields is a slice of attribute paths.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
EnsureSkipListIndex(ctx context.Context, fields []string, options *EnsureSkipListIndexOptions) (Index, bool, error)
// EnsureTTLIndex creates a TLL collection, if it does not already exist.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
EnsureTTLIndex(ctx context.Context, field string, expireAfter int, options *EnsureTTLIndexOptions) (Index, bool, error)
// EnsureZKDIndex creates a ZKD multi-dimensional index for the collection, if it does not already exist.
// Note that zkd indexes are an experimental feature in ArangoDB 3.9.
EnsureZKDIndex(ctx context.Context, fields []string, options *EnsureZKDIndexOptions) (Index, bool, error)
// EnsureInvertedIndex creates an inverted index in the collection, if it does not already exist.
// Available in ArangoDB 3.10 and later.
EnsureInvertedIndex(ctx context.Context, options *InvertedIndexOptions) (Index, bool, error)
}
// Deprecated: since 3.10 version. Use ArangoSearch view instead.
// EnsureFullTextIndexOptions contains specific options for creating a full text index.
type EnsureFullTextIndexOptions struct {
// MinLength is the minimum character length of words to index. Will default to a server-defined
// value if unspecified (0). It is thus recommended to set this value explicitly when creating the index.
MinLength int
// InBackground if true will not hold an exclusive collection lock for the entire index creation period (rocksdb only).
InBackground bool
// Name optional user defined name used for hints in AQL queries
Name string
// Estimates determines if the to-be-created index should maintain selectivity estimates or not.
Estimates *bool
}
// EnsureGeoIndexOptions contains specific options for creating a geo index.
type EnsureGeoIndexOptions struct {
// If a geo-spatial index on a location is constructed and GeoJSON is true, then the order within the array
// is longitude followed by latitude. This corresponds to the format described in http://geojson.org/geojson-spec.html#positions
GeoJSON bool
// InBackground if true will not hold an exclusive collection lock for the entire index creation period (rocksdb only).
InBackground bool
// Name optional user defined name used for hints in AQL queries
Name string
// Estimates determines if the to-be-created index should maintain selectivity estimates or not.
Estimates *bool
// LegacyPolygons determines if the to-be-created index should use legacy polygons or not.
// It is relevant for those that have geoJson set to true only.
// Old geo indexes from versions from below 3.10 will always implicitly have the legacyPolygons option set to true.
// Newly generated geo indexes from 3.10 on will have the legacyPolygons option by default set to false,
// however, it can still be explicitly overwritten with true to create a legacy index but is not recommended.
LegacyPolygons bool
}
// EnsureHashIndexOptions contains specific options for creating a hash index.
// Note: "hash" and "skiplist" are only aliases for "persistent" with the RocksDB storage engine which is only storage engine since 3.7
type EnsureHashIndexOptions struct {
// If true, then create a unique index.
Unique bool
// If true, then create a sparse index.
Sparse bool
// If true, de-duplication of array-values, before being added to the index, will be turned off.
// This flag requires ArangoDB 3.2.
// Note: this setting is only relevant for indexes with array fields (e.g. "fieldName[*]")
NoDeduplicate bool
// InBackground if true will not hold an exclusive collection lock for the entire index creation period (rocksdb only).
InBackground bool
// Name optional user defined name used for hints in AQL queries
Name string
// Estimates determines if the to-be-created index should maintain selectivity estimates or not.
Estimates *bool
}
// EnsurePersistentIndexOptions contains specific options for creating a persistent index.
// Note: "hash" and "skiplist" are only aliases for "persistent" with the RocksDB storage engine which is only storage engine since 3.7
type EnsurePersistentIndexOptions struct {
// If true, then create a unique index.
Unique bool
// If true, then create a sparse index.
Sparse bool
// If true, de-duplication of array-values, before being added to the index, will be turned off.
// This flag requires ArangoDB 3.2.
// Note: this setting is only relevant for indexes with array fields (e.g. "fieldName[*]")
NoDeduplicate bool
// InBackground if true will not hold an exclusive collection lock for the entire index creation period (rocksdb only).
InBackground bool
// Name optional user defined name used for hints in AQL queries
Name string
// Estimates determines if the to-be-created index should maintain selectivity estimates or not.
Estimates *bool
// CacheEnabled if true, then the index will be cached in memory. Caching is turned off by default.
CacheEnabled bool
// StoreValues if true, then the additional attributes will be included.
// These additional attributes cannot be used for index lookups or sorts, but they can be used for projections.
// There must be no overlap of attribute paths between `fields` and `storedValues`. The maximum number of values is 32.
StoredValues []string
}
// EnsureSkipListIndexOptions contains specific options for creating a skip-list index.
// Note: "hash" and "skiplist" are only aliases for "persistent" with the RocksDB storage engine which is only storage engine since 3.7
type EnsureSkipListIndexOptions struct {
// If true, then create a unique index.
Unique bool
// If true, then create a sparse index.
Sparse bool
// If true, de-duplication of array-values, before being added to the index, will be turned off.
// This flag requires ArangoDB 3.2.
// Note: this setting is only relevant for indexes with array fields (e.g. "fieldName[*]")
NoDeduplicate bool
// InBackground if true will not hold an exclusive collection lock for the entire index creation period (rocksdb only).
InBackground bool
// Name optional user defined name used for hints in AQL queries
Name string
// Estimates determines if the to-be-created index should maintain selectivity estimates or not.
Estimates *bool
}
// EnsureTTLIndexOptions provides specific options for creating a TTL index
type EnsureTTLIndexOptions struct {
// InBackground if true will not hold an exclusive collection lock for the entire index creation period (rocksdb only).
InBackground bool
// Name optional user defined name used for hints in AQL queries
Name string
// Estimates determines if the to-be-created index should maintain selectivity estimates or not.
Estimates *bool
}
// EnsureZKDIndexOptions provides specific options for creating a ZKD index
type EnsureZKDIndexOptions struct {
// If true, then create a unique index.
Unique bool
// InBackground if true will not hold an exclusive collection lock for the entire index creation period (rocksdb only).
InBackground bool
// Name optional user defined name used for hints in AQL queries
Name string
// fieldValueTypes is required and the only allowed value is "double". Future extensions of the index will allow other types.
FieldValueTypes string
// If true, then create a sparse index.
// TODO: The sparse property is not supported yet
// Sparse bool
}
// InvertedIndexOptions provides specific options for creating an inverted index
// Available since ArangoDB 3.10
type InvertedIndexOptions struct {
// Name optional user defined name used for hints in AQL queries
Name string `json:"name"`
// InBackground This attribute can be set to true to create the index in the background,
// not write-locking the underlying collection for as long as if the index is built in the foreground.
// The default value is false.
InBackground bool `json:"inBackground,omitempty"`
IsNewlyCreated bool `json:"isNewlyCreated,omitempty"`
// The number of threads to use for indexing the fields. Default: 2
Parallelism int `json:"parallelism,omitempty"`
// PrimarySort You can define a primary sort order to enable an AQL optimization.
// If a query iterates over all documents of a collection, wants to sort them by attribute values, and the (left-most) fields to sort by,
// as well as their sorting direction, match with the primarySort definition, then the SORT operation is optimized away.
PrimarySort InvertedIndexPrimarySort `json:"primarySort,omitempty"`
// StoredValues The optional storedValues attribute can contain an array of paths to additional attributes to store in the index.
// These additional attributes cannot be used for index lookups or for sorting, but they can be used for projections.
// This allows an index to fully cover more queries and avoid extra document lookups.
StoredValues []StoredValue `json:"storedValues,omitempty"`
// Analyzer The name of an Analyzer to use by default. This Analyzer is applied to the values of the indexed fields for which you don’t define Analyzers explicitly.
Analyzer string `json:"analyzer,omitempty"`
// Features list of analyzer features, default []
Features []ArangoSearchAnalyzerFeature `json:"features,omitempty"`
// IncludeAllFields If set to true, all fields of this element will be indexed. Defaults to false.
IncludeAllFields bool `json:"includeAllFields,omitempty"`
// TrackListPositions If set to true, values in a listed are treated as separate values. Defaults to false.
TrackListPositions bool `json:"trackListPositions,omitempty"`
// This option only applies if you use the inverted index in a search-alias Views.
// You can set the option to true to get the same behavior as with arangosearch Views regarding the indexing of array values as the default.
// If enabled, both, array and primitive values (strings, numbers, etc.) are accepted. Every element of an array is indexed according to the trackListPositions option.
// If set to false, it depends on the attribute path. If it explicitly expand an array ([*]), then the elements are indexed separately.
// Otherwise, the array is indexed as a whole, but only geopoint and aql Analyzers accept array inputs.
// You cannot use an array expansion if searchField is enabled.
SearchField bool `json:"searchField,omitempty"`
// Fields contains the properties for individual fields of the element.
// The key of the map are field names.
Fields []InvertedIndexField `json:"fields,omitempty"`
// ConsolidationIntervalMsec Wait at least this many milliseconds between applying ‘consolidationPolicy’ to consolidate View data store
// and possibly release space on the filesystem (default: 1000, to disable use: 0).
ConsolidationIntervalMsec *int64 `json:"consolidationIntervalMsec,omitempty"`
// CommitIntervalMsec Wait at least this many milliseconds between committing View data store changes and making
// documents visible to queries (default: 1000, to disable use: 0).
CommitIntervalMsec *int64 `json:"commitIntervalMsec,omitempty"`
// CleanupIntervalStep Wait at least this many commits between removing unused files in the ArangoSearch data directory
// (default: 2, to disable use: 0).
CleanupIntervalStep *int64 `json:"cleanupIntervalStep,omitempty"`
// ConsolidationPolicy The consolidation policy to apply for selecting which segments should be merged (default: {}).
ConsolidationPolicy *ArangoSearchConsolidationPolicy `json:"consolidationPolicy,omitempty"`
// WriteBufferIdle Maximum number of writers (segments) cached in the pool (default: 64, use 0 to disable)
WriteBufferIdle *int64 `json:"writebufferIdle,omitempty"`
// WriteBufferActive Maximum number of concurrent active writers (segments) that perform a transaction.
// Other writers (segments) wait till current active writers (segments) finish (default: 0, use 0 to disable)
WriteBufferActive *int64 `json:"writebufferActive,omitempty"`
// WriteBufferSizeMax Maximum memory byte size per writer (segment) before a writer (segment) flush is triggered.
// 0 value turns off this limit for any writer (buffer) and data will be flushed periodically based on the value defined for the flush thread (ArangoDB server startup option).
// 0 value should be used carefully due to high potential memory consumption (default: 33554432, use 0 to disable)
WriteBufferSizeMax *int64 `json:"writebufferSizeMax,omitempty"`
}
// InvertedIndexPrimarySort defines compression and list of fields to be sorted.
type InvertedIndexPrimarySort struct {
Fields []ArangoSearchPrimarySortEntry `json:"fields,omitempty"`
// Compression optional
Compression PrimarySortCompression `json:"compression,omitempty"`
}
// InvertedIndexField contains configuration for indexing of the field
type InvertedIndexField struct {
// Name An attribute path. The . character denotes sub-attributes.
Name string `json:"name"`
// Analyzer indicating the name of an analyzer instance
// Default: the value defined by the top-level analyzer option, or if not set, the default identity Analyzer.
Analyzer string `json:"analyzer,omitempty"`
// IncludeAllFields This option only applies if you use the inverted index in a search-alias Views.
// If set to true, then all sub-attributes of this field are indexed, excluding any sub-attributes that are configured separately by other elements in the fields array (and their sub-attributes). The analyzer and features properties apply to the sub-attributes.
// If set to false, then sub-attributes are ignored. The default value is defined by the top-level includeAllFields option, or false if not set.
IncludeAllFields bool `json:"includeAllFields,omitempty"`
// SearchField This option only applies if you use the inverted index in a search-alias Views.
// You can set the option to true to get the same behavior as with arangosearch Views regarding the indexing of array values for this field. If enabled, both, array and primitive values (strings, numbers, etc.) are accepted. Every element of an array is indexed according to the trackListPositions option.
// If set to false, it depends on the attribute path. If it explicitly expand an array ([*]), then the elements are indexed separately. Otherwise, the array is indexed as a whole, but only geopoint and aql Analyzers accept array inputs. You cannot use an array expansion if searchField is enabled.
// Default: the value defined by the top-level searchField option, or false if not set.
SearchField bool `json:"searchField,omitempty"`
// TrackListPositions This option only applies if you use the inverted index in a search-alias Views.
// If set to true, then track the value position in arrays for array values. For example, when querying a document like { attr: [ "valueX", "valueY", "valueZ" ] }, you need to specify the array element, e.g. doc.attr[1] == "valueY".
// If set to false, all values in an array are treated as equal alternatives. You don’t specify an array element in queries, e.g. doc.attr == "valueY", and all elements are searched for a match.
// Default: the value defined by the top-level trackListPositions option, or false if not set.
TrackListPositions bool `json:"trackListPositions,omitempty"`
// A list of Analyzer features to use for this field. They define what features are enabled for the analyzer
Features []ArangoSearchAnalyzerFeature `json:"features,omitempty"`
// Nested - Index the specified sub-objects that are stored in an array.
// Other than with the fields property, the values get indexed in a way that lets you query for co-occurring values.
// For example, you can search the sub-objects and all the conditions need to be met by a single sub-object instead of across all of them.
// Enterprise-only feature
Nested []InvertedIndexField `json:"nested,omitempty"`
}

@ -0,0 +1,394 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
"encoding/json"
"path"
)
type indexData struct {
ID string `json:"id,omitempty"`
Type string `json:"type"`
Fields []string `json:"fields,omitempty"`
Unique *bool `json:"unique,omitempty"`
Deduplicate *bool `json:"deduplicate,omitempty"`
Sparse *bool `json:"sparse,omitempty"`
GeoJSON *bool `json:"geoJson,omitempty"`
InBackground *bool `json:"inBackground,omitempty"`
Estimates *bool `json:"estimates,omitempty"`
MaxNumCoverCells int `json:"maxNumCoverCells,omitempty"`
MinLength int `json:"minLength,omitempty"`
ExpireAfter int `json:"expireAfter"`
Name string `json:"name,omitempty"`
FieldValueTypes string `json:"fieldValueTypes,omitempty"`
IsNewlyCreated *bool `json:"isNewlyCreated,omitempty"`
SelectivityEstimate float64 `json:"selectivityEstimate,omitempty"`
BestIndexedLevel int `json:"bestIndexedLevel,omitempty"`
WorstIndexedLevel int `json:"worstIndexedLevel,omitempty"`
LegacyPolygons *bool `json:"legacyPolygons,omitempty"`
CacheEnabled *bool `json:"cacheEnabled,omitempty"`
StoredValues []string `json:"storedValues,omitempty"`
ArangoError `json:",inline"`
}
type indexListResponse struct {
Indexes []json.RawMessage `json:"indexes,omitempty"`
ArangoError
}
// Index opens a connection to an existing index within the collection.
// If no index with given name exists, an NotFoundError is returned.
func (c *collection) Index(ctx context.Context, name string) (Index, error) {
req, err := c.conn.NewRequest("GET", path.Join(c.relPath("index"), name))
if err != nil {
return nil, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
var data map[string]interface{}
if err := resp.ParseBody("", &data); err != nil {
return nil, WithStack(err)
}
rawResponse, err := json.Marshal(data)
if err != nil {
return nil, WithStack(err)
}
idx, err := newIndexFromMap(rawResponse, c)
if err != nil {
return nil, WithStack(err)
}
return idx, nil
}
// IndexExists returns true if an index with given name exists within the collection.
func (c *collection) IndexExists(ctx context.Context, name string) (bool, error) {
req, err := c.conn.NewRequest("GET", path.Join(c.relPath("index"), name))
if err != nil {
return false, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return false, WithStack(err)
}
if err := resp.CheckStatus(200); err == nil {
return true, nil
} else if IsNotFound(err) {
return false, nil
} else {
return false, WithStack(err)
}
}
// Indexes returns a list of all indexes in the collection.
func (c *collection) Indexes(ctx context.Context) ([]Index, error) {
req, err := c.conn.NewRequest("GET", path.Join(c.db.relPath(), "_api", "index"))
if err != nil {
return nil, WithStack(err)
}
req.SetQuery("collection", c.name)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
var data indexListResponse
if err := resp.ParseBody("", &data); err != nil {
return nil, WithStack(err)
}
result := make([]Index, 0, len(data.Indexes))
for _, x := range data.Indexes {
idx, err := newIndexFromMap(x, c)
if err != nil {
return nil, WithStack(err)
}
result = append(result, idx)
}
return result, nil
}
// Deprecated: since 3.10 version. Use ArangoSearch view instead.
// EnsureFullTextIndex creates a fulltext index in the collection, if it does not already exist.
//
// Fields is a slice of attribute names. Currently, the slice is limited to exactly one attribute.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
func (c *collection) EnsureFullTextIndex(ctx context.Context, fields []string, options *EnsureFullTextIndexOptions) (Index, bool, error) {
input := indexData{
Type: string(FullTextIndex),
Fields: fields,
}
if options != nil {
input.InBackground = &options.InBackground
input.Name = options.Name
input.MinLength = options.MinLength
input.Estimates = options.Estimates
}
idx, created, err := c.ensureIndex(ctx, input)
if err != nil {
return nil, false, WithStack(err)
}
return idx, created, nil
}
// EnsureGeoIndex creates a hash index in the collection, if it does not already exist.
//
// Fields is a slice with one or two attribute paths. If it is a slice with one attribute path location,
// then a geo-spatial index on all documents is created using location as path to the coordinates.
// The value of the attribute must be a slice with at least two double values. The slice must contain the latitude (first value)
// and the longitude (second value). All documents, which do not have the attribute path or with value that are not suitable, are ignored.
// If it is a slice with two attribute paths latitude and longitude, then a geo-spatial index on all documents is created
// using latitude and longitude as paths the latitude and the longitude. The value of the attribute latitude and of the
// attribute longitude must a double. All documents, which do not have the attribute paths or which values are not suitable, are ignored.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
func (c *collection) EnsureGeoIndex(ctx context.Context, fields []string, options *EnsureGeoIndexOptions) (Index, bool, error) {
input := indexData{
Type: string(GeoIndex),
Fields: fields,
}
if options != nil {
input.InBackground = &options.InBackground
input.Name = options.Name
input.GeoJSON = &options.GeoJSON
input.Estimates = options.Estimates
if options.LegacyPolygons {
input.LegacyPolygons = &options.LegacyPolygons
}
}
idx, created, err := c.ensureIndex(ctx, input)
if err != nil {
return nil, false, WithStack(err)
}
return idx, created, nil
}
// EnsureHashIndex creates a hash index in the collection, if it does not already exist.
// Fields is a slice of attribute paths.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
func (c *collection) EnsureHashIndex(ctx context.Context, fields []string, options *EnsureHashIndexOptions) (Index, bool, error) {
input := indexData{
Type: string(HashIndex),
Fields: fields,
}
off := false
if options != nil {
input.InBackground = &options.InBackground
input.Name = options.Name
input.Unique = &options.Unique
input.Sparse = &options.Sparse
input.Estimates = options.Estimates
if options.NoDeduplicate {
input.Deduplicate = &off
}
}
idx, created, err := c.ensureIndex(ctx, input)
if err != nil {
return nil, false, WithStack(err)
}
return idx, created, nil
}
// EnsurePersistentIndex creates a persistent index in the collection, if it does not already exist.
// Fields is a slice of attribute paths.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
func (c *collection) EnsurePersistentIndex(ctx context.Context, fields []string, options *EnsurePersistentIndexOptions) (Index, bool, error) {
input := indexData{
Type: string(PersistentIndex),
Fields: fields,
}
off := false
on := true
if options != nil {
input.InBackground = &options.InBackground
input.Name = options.Name
input.Unique = &options.Unique
input.Sparse = &options.Sparse
input.Estimates = options.Estimates
if options.NoDeduplicate {
input.Deduplicate = &off
}
if options.CacheEnabled {
input.CacheEnabled = &on
}
if options.StoredValues != nil {
input.StoredValues = options.StoredValues
}
}
idx, created, err := c.ensureIndex(ctx, input)
if err != nil {
return nil, false, WithStack(err)
}
return idx, created, nil
}
// EnsureSkipListIndex creates a skiplist index in the collection, if it does not already exist.
// Fields is a slice of attribute paths.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
func (c *collection) EnsureSkipListIndex(ctx context.Context, fields []string, options *EnsureSkipListIndexOptions) (Index, bool, error) {
input := indexData{
Type: string(SkipListIndex),
Fields: fields,
}
off := false
if options != nil {
input.InBackground = &options.InBackground
input.Name = options.Name
input.Unique = &options.Unique
input.Sparse = &options.Sparse
input.Estimates = options.Estimates
if options.NoDeduplicate {
input.Deduplicate = &off
}
}
idx, created, err := c.ensureIndex(ctx, input)
if err != nil {
return nil, false, WithStack(err)
}
return idx, created, nil
}
// EnsureTTLIndex creates a TLL collection, if it does not already exist.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
func (c *collection) EnsureTTLIndex(ctx context.Context, field string, expireAfter int, options *EnsureTTLIndexOptions) (Index, bool, error) {
input := indexData{
Type: string(TTLIndex),
Fields: []string{field},
ExpireAfter: expireAfter,
}
if options != nil {
input.InBackground = &options.InBackground
input.Name = options.Name
input.Estimates = options.Estimates
}
idx, created, err := c.ensureIndex(ctx, input)
if err != nil {
return nil, false, WithStack(err)
}
return idx, created, nil
}
// EnsureZKDIndex creates a ZKD index in the collection, if it does not already exist.
// Fields is a slice of attribute paths.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
func (c *collection) EnsureZKDIndex(ctx context.Context, fields []string, options *EnsureZKDIndexOptions) (Index, bool, error) {
input := indexData{
Type: string(ZKDIndex),
Fields: fields,
// fieldValueTypes is required and the only allowed value is "double". Future extensions of the index will allow other types.
FieldValueTypes: "double",
}
if options != nil {
input.InBackground = &options.InBackground
input.Name = options.Name
input.Unique = &options.Unique
//input.Sparse = &options.Sparse
}
idx, created, err := c.ensureIndex(ctx, input)
if err != nil {
return nil, false, WithStack(err)
}
return idx, created, nil
}
type invertedIndexData struct {
InvertedIndexOptions
Type string `json:"type"`
ID string `json:"id,omitempty"`
ArangoError `json:",inline"`
}
// EnsureInvertedIndex creates an inverted index in the collection, if it does not already exist.
// Available in ArangoDB 3.10 and later.
func (c *collection) EnsureInvertedIndex(ctx context.Context, options *InvertedIndexOptions) (Index, bool, error) {
req, err := c.conn.NewRequest("POST", path.Join(c.db.relPath(), "_api/index"))
if err != nil {
return nil, false, WithStack(err)
}
if options == nil {
options = &InvertedIndexOptions{}
}
req.SetQuery("collection", c.name)
if _, err := req.SetBody(invertedIndexData{InvertedIndexOptions: *options, Type: string(InvertedIndex)}); err != nil {
return nil, false, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return nil, false, WithStack(err)
}
if err := resp.CheckStatus(200, 201); err != nil {
return nil, false, WithStack(err)
}
created := resp.StatusCode() == 201
var data invertedIndexData
if err := resp.ParseBody("", &data); err != nil {
return nil, false, WithStack(err)
}
idx, err := newInvertedIndex(data, c)
if err != nil {
return nil, false, WithStack(err)
}
return idx, created, nil
}
// ensureIndex creates a persistent index in the collection, if it does not already exist.
// Fields is a slice of attribute paths.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
func (c *collection) ensureIndex(ctx context.Context, options indexData) (Index, bool, error) {
req, err := c.conn.NewRequest("POST", path.Join(c.db.relPath(), "_api/index"))
if err != nil {
return nil, false, WithStack(err)
}
req.SetQuery("collection", c.name)
if _, err := req.SetBody(options); err != nil {
return nil, false, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return nil, false, WithStack(err)
}
if err := resp.CheckStatus(200, 201); err != nil {
return nil, false, WithStack(err)
}
created := resp.StatusCode() == 201
var data indexData
if err := resp.ParseBody("", &data); err != nil {
return nil, false, WithStack(err)
}
idx, err := newIndex(data, c)
if err != nil {
return nil, false, WithStack(err)
}
return idx, created, nil
}

@ -0,0 +1,172 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
"encoding/json"
"errors"
velocypack "github.com/arangodb/go-velocypack"
)
// Connection is a connection to a database server using a specific protocol.
type Connection interface {
// NewRequest creates a new request with given method and path.
NewRequest(method, path string) (Request, error)
// Do performs a given request, returning its response.
Do(ctx context.Context, req Request) (Response, error)
// Unmarshal unmarshals the given raw object into the given result interface.
Unmarshal(data RawObject, result interface{}) error
// Endpoints returns the endpoints used by this connection.
Endpoints() []string
// UpdateEndpoints reconfigures the connection to use the given endpoints.
UpdateEndpoints(endpoints []string) error
// SetAuthentication creates a copy of connection wrapper for given auth parameters.
SetAuthentication(Authentication) (Connection, error)
// Protocols returns all protocols used by this connection.
Protocols() ProtocolSet
}
// Request represents the input to a request on the server.
type Request interface {
// SetQuery sets a single query argument of the request.
// Any existing query argument with the same key is overwritten.
SetQuery(key, value string) Request
// SetBody sets the content of the request.
// The protocol of the connection determines what kinds of marshalling is taking place.
// When multiple bodies are given, they are merged, with fields in the first document prevailing.
SetBody(body ...interface{}) (Request, error)
// SetBodyArray sets the content of the request as an array.
// If the given mergeArray is not nil, its elements are merged with the elements in the body array (mergeArray data overrides bodyArray data).
// The merge is NOT recursive.
// The protocol of the connection determines what kinds of marshalling is taking place.
SetBodyArray(bodyArray interface{}, mergeArray []map[string]interface{}) (Request, error)
// SetBodyImportArray sets the content of the request as an array formatted for importing documents.
// The protocol of the connection determines what kinds of marshalling is taking place.
SetBodyImportArray(bodyArray interface{}) (Request, error)
// SetHeader sets a single header arguments of the request.
// Any existing header argument with the same key is overwritten.
SetHeader(key, value string) Request
// Written returns true as soon as this request has been written completely to the network.
// This does not guarantee that the server has received or processed the request.
Written() bool
// Clone creates a new request containing the same data as this request
Clone() Request
// Path returns the Request path
Path() string
// Method returns the Request method
Method() string
}
type BodyBuilder interface {
// GetBody returns data which are generated by the body builder
GetBody() []byte
// SetBody sets the content of the request.
// The protocol of the connection determines what kinds of marshalling is taking place.
// When multiple bodies are given, they are merged, with fields in the first document prevailing.
SetBody(body ...interface{}) error
// SetBodyArray sets the content of the request as an array.
// If the given mergeArray is not nil, its elements are merged with the elements in the body array (mergeArray data overrides bodyArray data).
// The merge is NOT recursive.
// The protocol of the connection determines what kinds of marshalling is taking place.
SetBodyArray(bodyArray interface{}, mergeArray []map[string]interface{}) error
// SetBodyImportArray sets the content of the request as an array formatted for importing documents.
// The protocol of the connection determines what kinds of marshalling is taking place.
SetBodyImportArray(bodyArray interface{}) error
// GetContentType returns the type of the data in a body
GetContentType() string
// Clone creates new Body builder
Clone() BodyBuilder
}
// Response represents the response from the server on a given request.
type Response interface {
// StatusCode returns an HTTP compatible status code of the response.
StatusCode() int
// Endpoint returns the endpoint that handled the request.
Endpoint() string
// CheckStatus checks if the status of the response equals to one of the given status codes.
// If so, nil is returned.
// If not, an attempt is made to parse an error response in the body and an error is returned.
CheckStatus(validStatusCodes ...int) error
// Header returns the value of a response header with given key.
// If no such header is found, an empty string is returned.
// On nested Response's, this function will always return an empty string.
Header(key string) string
// ParseBody performs protocol specific unmarshalling of the response data into the given result.
// If the given field is non-empty, the contents of that field will be parsed into the given result.
// This can only be used for requests that return a single object.
ParseBody(field string, result interface{}) error
// ParseArrayBody performs protocol specific unmarshalling of the response array data into individual response objects.
// This can only be used for requests that return an array of objects.
ParseArrayBody() ([]Response, error)
}
// RawObject is a raw encoded object.
// Connection implementations must be able to unmarshal *RawObject into Go objects.
type RawObject []byte
// MarshalJSON returns *r as the JSON encoding of r.
func (r *RawObject) MarshalJSON() ([]byte, error) {
return *r, nil
}
// UnmarshalJSON sets *r to a copy of data.
func (r *RawObject) UnmarshalJSON(data []byte) error {
if r == nil {
return errors.New("RawObject: UnmarshalJSON on nil pointer")
}
*r = append((*r)[0:0], data...)
return nil
}
// Ensure RawObject implements json.Marshaler & json.Unmarshaler
var _ json.Marshaler = (*RawObject)(nil)
var _ json.Unmarshaler = (*RawObject)(nil)
// MarshalVPack returns m as the Velocypack encoding of m.
func (r RawObject) MarshalVPack() (velocypack.Slice, error) {
if r == nil {
return velocypack.NullSlice(), nil
}
return velocypack.Slice(r), nil
}
// UnmarshalVPack sets *m to a copy of data.
func (r *RawObject) UnmarshalVPack(data velocypack.Slice) error {
if r == nil {
return errors.New("velocypack.RawSlice: UnmarshalVPack on nil pointer")
}
*r = append((*r)[0:0], data...)
return nil
}
var _ velocypack.Marshaler = (*RawObject)(nil)
var _ velocypack.Unmarshaler = (*RawObject)(nil)

@ -0,0 +1,46 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import "fmt"
// ContentType identifies the type of encoding to use for the data.
type ContentType int
const (
// ContentTypeJSON encodes data as json
ContentTypeJSON ContentType = iota
// ContentTypeVelocypack encodes data as Velocypack
ContentTypeVelocypack
)
func (ct ContentType) String() string {
switch ct {
case ContentTypeJSON:
return "application/json"
case ContentTypeVelocypack:
return "application/x-velocypack"
default:
panic(fmt.Sprintf("Unknown content type %d", int(ct)))
}
}

@ -0,0 +1,543 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
// Author Tomasz Mielech <tomasz@arangodb.com>
//
package driver
import (
"context"
"fmt"
"reflect"
"strconv"
"time"
"github.com/arangodb/go-driver/util"
)
// ContextKey is an internal type used for holding values in a `context.Context`
// do not use!.
type ContextKey string
const (
keyRevision ContextKey = "arangodb-revision"
keyRevisions ContextKey = "arangodb-revisions"
keyReturnNew ContextKey = "arangodb-returnNew"
keyReturnOld ContextKey = "arangodb-returnOld"
keySilent ContextKey = "arangodb-silent"
keyWaitForSync ContextKey = "arangodb-waitForSync"
keyDetails ContextKey = "arangodb-details"
keyKeepNull ContextKey = "arangodb-keepNull"
keyMergeObjects ContextKey = "arangodb-mergeObjects"
keyRawResponse ContextKey = "arangodb-rawResponse"
keyImportDetails ContextKey = "arangodb-importDetails"
keyResponse ContextKey = "arangodb-response"
keyEndpoint ContextKey = "arangodb-endpoint"
keyIsRestore ContextKey = "arangodb-isRestore"
keyIsSystem ContextKey = "arangodb-isSystem"
keyIgnoreRevs ContextKey = "arangodb-ignoreRevs"
keyEnforceReplicationFactor ContextKey = "arangodb-enforceReplicationFactor"
keyConfigured ContextKey = "arangodb-configured"
keyFollowLeaderRedirect ContextKey = "arangodb-followLeaderRedirect"
keyDBServerID ContextKey = "arangodb-dbserverID"
keyBatchID ContextKey = "arangodb-batchID"
keyJobIDResponse ContextKey = "arangodb-jobIDResponse"
keyAllowDirtyReads ContextKey = "arangodb-allowDirtyReads"
keyTransactionID ContextKey = "arangodb-transactionID"
keyOverwriteMode ContextKey = "arangodb-overwriteMode"
keyOverwrite ContextKey = "arangodb-overwrite"
keyUseQueueTimeout ContextKey = "arangodb-use-queue-timeout"
keyMaxQueueTime ContextKey = "arangodb-max-queue-time-seconds"
)
type OverwriteMode string
const (
OverwriteModeIgnore OverwriteMode = "ignore"
OverwriteModeReplace OverwriteMode = "replace"
OverwriteModeUpdate OverwriteMode = "update"
OverwriteModeConflict OverwriteMode = "conflict"
)
// WithRevision is used to configure a context to make document
// functions specify an explicit revision of the document using an `If-Match` condition.
func WithRevision(parent context.Context, revision string) context.Context {
return context.WithValue(contextOrBackground(parent), keyRevision, revision)
}
// WithRevisions is used to configure a context to make multi-document
// functions specify explicit revisions of the documents.
func WithRevisions(parent context.Context, revisions []string) context.Context {
return context.WithValue(contextOrBackground(parent), keyRevisions, revisions)
}
// WithReturnNew is used to configure a context to make create, update & replace document
// functions return the new document into the given result.
func WithReturnNew(parent context.Context, result interface{}) context.Context {
return context.WithValue(contextOrBackground(parent), keyReturnNew, result)
}
// WithReturnOld is used to configure a context to make update & replace document
// functions return the old document into the given result.
func WithReturnOld(parent context.Context, result interface{}) context.Context {
return context.WithValue(contextOrBackground(parent), keyReturnOld, result)
}
// WithDetails is used to configure a context to make Client.Version return additional details.
// You can pass a single (optional) boolean. If that is set to false, you explicitly ask to not provide details.
func WithDetails(parent context.Context, value ...bool) context.Context {
v := true
if len(value) == 1 {
v = value[0]
}
return context.WithValue(contextOrBackground(parent), keyDetails, v)
}
// WithEndpoint is used to configure a context that forces a request to be executed on a specific endpoint.
// If you specify an endpoint like this, failover is disabled.
// If you specify an unknown endpoint, an InvalidArgumentError is returned from requests.
func WithEndpoint(parent context.Context, endpoint string) context.Context {
endpoint = util.FixupEndpointURLScheme(endpoint)
return context.WithValue(contextOrBackground(parent), keyEndpoint, endpoint)
}
// WithKeepNull is used to configure a context to make update functions keep null fields (value==true)
// or remove fields with null values (value==false).
func WithKeepNull(parent context.Context, value bool) context.Context {
return context.WithValue(contextOrBackground(parent), keyKeepNull, value)
}
// WithMergeObjects is used to configure a context to make update functions merge objects present in both
// the existing document and the patch document (value==true) or overwrite objects in the existing document
// with objects found in the patch document (value==false)
func WithMergeObjects(parent context.Context, value bool) context.Context {
return context.WithValue(contextOrBackground(parent), keyMergeObjects, value)
}
// WithSilent is used to configure a context to make functions return an empty result (silent==true),
// instead of a metadata result (silent==false, default).
// You can pass a single (optional) boolean. If that is set to false, you explicitly ask to return metadata result.
func WithSilent(parent context.Context, value ...bool) context.Context {
v := true
if len(value) == 1 {
v = value[0]
}
return context.WithValue(contextOrBackground(parent), keySilent, v)
}
// WithWaitForSync is used to configure a context to make modification
// functions wait until the data has been synced to disk (or not).
// You can pass a single (optional) boolean. If that is set to false, you explicitly do not wait for
// data to be synced to disk.
func WithWaitForSync(parent context.Context, value ...bool) context.Context {
v := true
if len(value) == 1 {
v = value[0]
}
return context.WithValue(contextOrBackground(parent), keyWaitForSync, v)
}
// WithAllowDirtyReads is used in an active failover deployment to allow reads from the follower.
// You can pass a reference to a boolean that will set according to whether a potentially dirty read
// happened or not. nil is allowed.
// This is valid for document reads, aql queries, gharial vertex and edge reads.
// Since 3.10 This feature is available in the Enterprise Edition for cluster deployments as well
func WithAllowDirtyReads(parent context.Context, wasDirtyRead *bool) context.Context {
return context.WithValue(contextOrBackground(parent), keyAllowDirtyReads, wasDirtyRead)
}
// WithArangoQueueTimeout is used to enable Queue timeout on the server side.
// If WithArangoQueueTime is used then its value takes precedence in other case value of ctx.Deadline will be taken
func WithArangoQueueTimeout(parent context.Context, useQueueTimeout bool) context.Context {
return context.WithValue(contextOrBackground(parent), keyUseQueueTimeout, useQueueTimeout)
}
// WithArangoQueueTime defines max queue timeout on the server side.
func WithArangoQueueTime(parent context.Context, duration time.Duration) context.Context {
return context.WithValue(contextOrBackground(parent), keyMaxQueueTime, duration)
}
// WithRawResponse is used to configure a context that will make all functions store the raw response into a
// buffer.
func WithRawResponse(parent context.Context, value *[]byte) context.Context {
return context.WithValue(contextOrBackground(parent), keyRawResponse, value)
}
// WithResponse is used to configure a context that will make all functions store the response into the given value.
func WithResponse(parent context.Context, value *Response) context.Context {
return context.WithValue(contextOrBackground(parent), keyResponse, value)
}
// WithImportDetails is used to configure a context that will make import document requests return
// details about documents that could not be imported.
func WithImportDetails(parent context.Context, value *[]string) context.Context {
return context.WithValue(contextOrBackground(parent), keyImportDetails, value)
}
// WithIsRestore is used to configure a context to make insert functions use the "isRestore=<value>"
// setting.
// Note: This function is intended for internal (replication) use. It is NOT intended to
// be used by normal client. This CAN screw up your database.
func WithIsRestore(parent context.Context, value bool) context.Context {
return context.WithValue(contextOrBackground(parent), keyIsRestore, value)
}
// WithIsSystem is used to configure a context to make insert functions use the "isSystem=<value>"
// setting.
func WithIsSystem(parent context.Context, value bool) context.Context {
return context.WithValue(contextOrBackground(parent), keyIsSystem, value)
}
// WithIgnoreRevisions is used to configure a context to make modification
// functions ignore revisions in the update.
// Do not use in combination with WithRevision or WithRevisions.
func WithIgnoreRevisions(parent context.Context, value ...bool) context.Context {
v := true
if len(value) == 1 {
v = value[0]
}
return context.WithValue(contextOrBackground(parent), keyIgnoreRevs, v)
}
// WithEnforceReplicationFactor is used to configure a context to make adding collections
// fail if the replication factor is too high (default or true) or
// silently accept (false).
func WithEnforceReplicationFactor(parent context.Context, value bool) context.Context {
return context.WithValue(contextOrBackground(parent), keyEnforceReplicationFactor, value)
}
// WithConfigured is used to configure a context to return the configured value of
// a user grant instead of the effective grant.
func WithConfigured(parent context.Context, value ...bool) context.Context {
v := true
if len(value) == 1 {
v = value[0]
}
return context.WithValue(contextOrBackground(parent), keyConfigured, v)
}
// WithFollowLeaderRedirect is used to configure a context to return turn on/off
// following redirection responses from the server when the request is answered by a follower.
// Default behavior is "on".
func WithFollowLeaderRedirect(parent context.Context, value bool) context.Context {
return context.WithValue(contextOrBackground(parent), keyFollowLeaderRedirect, value)
}
// WithDBServerID is used to configure a context that includes an ID of a specific DBServer.
func WithDBServerID(parent context.Context, id string) context.Context {
return context.WithValue(contextOrBackground(parent), keyDBServerID, id)
}
// WithBatchID is used to configure a context that includes an ID of a Batch.
// This is used in replication functions.
func WithBatchID(parent context.Context, id string) context.Context {
return context.WithValue(contextOrBackground(parent), keyBatchID, id)
}
// WithJobIDResponse is used to configure a context that includes a reference to a JobID
// that is filled on a error-free response.
// This is used in cluster functions.
func WithJobIDResponse(parent context.Context, jobID *string) context.Context {
return context.WithValue(contextOrBackground(parent), keyJobIDResponse, jobID)
}
// WithTransactionID is used to bind a request to a specific transaction
func WithTransactionID(parent context.Context, tid TransactionID) context.Context {
return context.WithValue(contextOrBackground(parent), keyTransactionID, tid)
}
// WithOverwriteMode is used to configure a context to instruct how a document should be overwritten.
func WithOverwriteMode(parent context.Context, mode OverwriteMode) context.Context {
return context.WithValue(contextOrBackground(parent), keyOverwriteMode, mode)
}
// WithOverwrite is used to configure a context to instruct if a document should be overwritten.
func WithOverwrite(parent context.Context) context.Context {
return context.WithValue(contextOrBackground(parent), keyOverwrite, true)
}
type contextSettings struct {
Silent bool
WaitForSync bool
ReturnOld interface{}
ReturnNew interface{}
Revision string
Revisions []string
ImportDetails *[]string
IsRestore bool
IsSystem bool
AllowDirtyReads bool
DirtyReadFlag *bool
IgnoreRevs *bool
EnforceReplicationFactor *bool
Configured *bool
FollowLeaderRedirect *bool
DBServerID string
BatchID string
JobIDResponse *string
OverwriteMode OverwriteMode
Overwrite bool
QueueTimeout bool
MaxQueueTime time.Duration
}
// loadContextResponseValue loads generic values from the response and puts it into variables specified
// via context values.
func loadContextResponseValues(cs contextSettings, resp Response) {
// Parse potential dirty read
if cs.DirtyReadFlag != nil {
if dirtyRead := resp.Header("X-Arango-Potential-Dirty-Read"); dirtyRead != "" {
*cs.DirtyReadFlag = true // The documentation does not say anything about the actual value (dirtyRead == "true")
} else {
*cs.DirtyReadFlag = false
}
}
}
// setDirtyReadFlagIfRequired is a helper function that sets the bool reference for allowDirtyReads to the
// specified value, if required and reference is not nil.
func setDirtyReadFlagIfRequired(ctx context.Context, wasDirty bool) {
if v := ctx.Value(keyAllowDirtyReads); v != nil {
if ref, ok := v.(*bool); ok && ref != nil {
*ref = wasDirty
}
}
}
// applyContextSettings returns the settings configured in the context in the given request.
// It then returns information about the applied settings that may be needed later in API implementation functions.
func applyContextSettings(ctx context.Context, req Request) contextSettings {
result := contextSettings{}
if ctx == nil {
return result
}
// Details
if v := ctx.Value(keyDetails); v != nil {
if details, ok := v.(bool); ok {
req.SetQuery("details", strconv.FormatBool(details))
}
}
// KeepNull
if v := ctx.Value(keyKeepNull); v != nil {
if keepNull, ok := v.(bool); ok {
req.SetQuery("keepNull", strconv.FormatBool(keepNull))
}
}
// MergeObjects
if v := ctx.Value(keyMergeObjects); v != nil {
if mergeObjects, ok := v.(bool); ok {
req.SetQuery("mergeObjects", strconv.FormatBool(mergeObjects))
}
}
// Silent
if v := ctx.Value(keySilent); v != nil {
if silent, ok := v.(bool); ok {
req.SetQuery("silent", strconv.FormatBool(silent))
result.Silent = silent
}
}
// WaitForSync
if v := ctx.Value(keyWaitForSync); v != nil {
if waitForSync, ok := v.(bool); ok {
req.SetQuery("waitForSync", strconv.FormatBool(waitForSync))
result.WaitForSync = waitForSync
}
}
// AllowDirtyReads
if v := ctx.Value(keyAllowDirtyReads); v != nil {
req.SetHeader("x-arango-allow-dirty-read", "true")
result.AllowDirtyReads = true
if dirtyReadFlag, ok := v.(*bool); ok {
result.DirtyReadFlag = dirtyReadFlag
}
}
// Enable Queue timeout
if v := ctx.Value(keyUseQueueTimeout); v != nil {
if useQueueTimeout, ok := v.(bool); ok && useQueueTimeout {
result.QueueTimeout = useQueueTimeout
if v := ctx.Value(keyMaxQueueTime); v != nil {
if timeout, ok := v.(time.Duration); ok {
result.MaxQueueTime = timeout
req.SetHeader("x-arango-queue-time-seconds", fmt.Sprint(timeout.Seconds()))
}
} else if deadline, ok := ctx.Deadline(); ok {
timeout := deadline.Sub(time.Now())
req.SetHeader("x-arango-queue-time-seconds", fmt.Sprint(timeout.Seconds()))
}
}
}
// TransactionID
if v := ctx.Value(keyTransactionID); v != nil {
req.SetHeader("x-arango-trx-id", string(v.(TransactionID)))
}
// ReturnOld
if v := ctx.Value(keyReturnOld); v != nil {
req.SetQuery("returnOld", "true")
result.ReturnOld = v
}
// ReturnNew
if v := ctx.Value(keyReturnNew); v != nil {
req.SetQuery("returnNew", "true")
result.ReturnNew = v
}
// If-Match
if v := ctx.Value(keyRevision); v != nil {
if rev, ok := v.(string); ok {
req.SetHeader("If-Match", rev)
result.Revision = rev
}
}
// Revisions
if v := ctx.Value(keyRevisions); v != nil {
if revs, ok := v.([]string); ok {
req.SetQuery("ignoreRevs", "false")
result.Revisions = revs
}
}
// ImportDetails
if v := ctx.Value(keyImportDetails); v != nil {
if details, ok := v.(*[]string); ok {
req.SetQuery("details", "true")
result.ImportDetails = details
}
}
// IsRestore
if v := ctx.Value(keyIsRestore); v != nil {
if isRestore, ok := v.(bool); ok {
req.SetQuery("isRestore", strconv.FormatBool(isRestore))
result.IsRestore = isRestore
}
}
// IsSystem
if v := ctx.Value(keyIsSystem); v != nil {
if isSystem, ok := v.(bool); ok {
req.SetQuery("isSystem", strconv.FormatBool(isSystem))
result.IsSystem = isSystem
}
}
// IgnoreRevs
if v := ctx.Value(keyIgnoreRevs); v != nil {
if ignoreRevs, ok := v.(bool); ok {
req.SetQuery("ignoreRevs", strconv.FormatBool(ignoreRevs))
result.IgnoreRevs = &ignoreRevs
}
}
// EnforeReplicationFactor
if v := ctx.Value(keyEnforceReplicationFactor); v != nil {
if enforceReplicationFactor, ok := v.(bool); ok {
req.SetQuery("enforceReplicationFactor", strconv.FormatBool(enforceReplicationFactor))
result.EnforceReplicationFactor = &enforceReplicationFactor
}
}
// Configured
if v := ctx.Value(keyConfigured); v != nil {
if configured, ok := v.(bool); ok {
req.SetQuery("configured", strconv.FormatBool(configured))
result.Configured = &configured
}
}
// FollowLeaderRedirect
if v := ctx.Value(keyFollowLeaderRedirect); v != nil {
if followLeaderRedirect, ok := v.(bool); ok {
result.FollowLeaderRedirect = &followLeaderRedirect
}
}
// DBServerID
if v := ctx.Value(keyDBServerID); v != nil {
if id, ok := v.(string); ok {
req.SetQuery("DBserver", id)
result.DBServerID = id
}
}
// BatchID
if v := ctx.Value(keyBatchID); v != nil {
if id, ok := v.(string); ok {
req.SetQuery("batchId", id)
result.BatchID = id
}
}
// JobIDResponse
if v := ctx.Value(keyJobIDResponse); v != nil {
if idRef, ok := v.(*string); ok {
result.JobIDResponse = idRef
}
}
// OverwriteMode
if v := ctx.Value(keyOverwriteMode); v != nil {
if mode, ok := v.(OverwriteMode); ok {
req.SetQuery("overwriteMode", string(mode))
result.OverwriteMode = mode
}
}
if v := ctx.Value(keyOverwrite); v != nil {
if overwrite, ok := v.(bool); ok && overwrite {
req.SetQuery("overwrite", "true")
result.Overwrite = true
}
}
return result
}
// contextOrBackground returns the given context if it is not nil.
// Returns context.Background() otherwise.
func contextOrBackground(ctx context.Context) context.Context {
if ctx != nil {
return ctx
}
return context.Background()
}
// withDocumentAt returns a context derived from the given parent context to be used in multi-document options
// that needs a client side "loop" implementation.
// It handle:
// - WithRevisions
// - WithReturnNew
// - WithReturnOld
func withDocumentAt(ctx context.Context, index int) (context.Context, error) {
if ctx == nil {
return nil, nil
}
// Revisions
if v := ctx.Value(keyRevisions); v != nil {
if revs, ok := v.([]string); ok {
if index >= len(revs) {
return nil, WithStack(InvalidArgumentError{Message: "Index out of range: revisions"})
}
ctx = WithRevision(ctx, revs[index])
}
}
// ReturnOld
if v := ctx.Value(keyReturnOld); v != nil {
val := reflect.ValueOf(v)
ctx = WithReturnOld(ctx, val.Index(index).Addr().Interface())
}
// ReturnNew
if v := ctx.Value(keyReturnNew); v != nil {
val := reflect.ValueOf(v)
ctx = WithReturnNew(ctx, val.Index(index).Addr().Interface())
}
return ctx, nil
}

@ -0,0 +1,91 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
"io"
"time"
)
// QueryExtra holds Query extra information
type QueryExtra interface {
// GetStatistics returns Query statistics
GetStatistics() QueryStatistics
// GetProfileRaw returns raw profile information in json
GetProfileRaw() ([]byte, bool, error)
// PlanRaw returns raw plan
GetPlanRaw() ([]byte, bool, error)
}
// Statistics returned with the query cursor
type QueryStatistics interface {
// the total number of data-modification operations successfully executed.
WritesExecuted() int64
// The total number of data-modification operations that were unsuccessful
WritesIgnored() int64
// The total number of documents iterated over when scanning a collection without an index.
ScannedFull() int64
// The total number of documents iterated over when scanning a collection using an index.
ScannedIndex() int64
// the total number of documents that were removed after executing a filter condition in a FilterNode
Filtered() int64
// Returns the numer of results before the last LIMIT in the query was applied.
// A valid return value is only available when the has been created with a context that was
// prepared with `WithFullCount`. Additionally this will also not return a valid value if
// the context was prepared with `WithStream`.
FullCount() int64
// Execution time of the query (wall-clock time). value will be set from the outside
ExecutionTime() time.Duration
}
// Cursor is returned from a query, used to iterate over a list of documents.
// Note that a Cursor must always be closed to avoid holding on to resources in the server while they are no longer needed.
type Cursor interface {
io.Closer
// HasMore returns true if the next call to ReadDocument does not return a NoMoreDocuments error.
HasMore() bool
// ReadDocument reads the next document from the cursor.
// The document data is stored into result, the document meta data is returned.
// If the cursor has no more documents, a NoMoreDocuments error is returned.
// Note: If the query (resulting in this cursor) does not return documents,
// then the returned DocumentMeta will be empty.
ReadDocument(ctx context.Context, result interface{}) (DocumentMeta, error)
// Count returns the total number of result documents available.
// A valid return value is only available when the cursor has been created with a context that was
// prepared with `WithQueryCount` and not with `WithQueryStream`.
Count() int64
// Statistics returns the query execution statistics for this cursor.
// This might not be valid if the cursor has been created with a context that was
// prepared with `WithQueryStream`
Statistics() QueryStatistics
// Extra returns the query extras for this cursor.
Extra() QueryExtra
}

@ -0,0 +1,345 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
"encoding/json"
"path"
"reflect"
"sync"
"sync/atomic"
"time"
)
// newCursor creates a new Cursor implementation.
func newCursor(data cursorData, endpoint string, db *database, allowDirtyReads bool) (Cursor, error) {
if db == nil {
return nil, WithStack(InvalidArgumentError{Message: "db is nil"})
}
return &cursor{
cursorData: data,
endpoint: endpoint,
db: db,
conn: db.conn,
allowDirtyReads: allowDirtyReads,
}, nil
}
type cursor struct {
cursorData
endpoint string
resultIndex int
db *database
conn Connection
closed int32
closeMutex sync.Mutex
allowDirtyReads bool
lastReadWasDirty bool
}
// CursorStats TODO: all these int64 should be changed into uint64
type cursorStats struct {
// The total number of data-modification operations successfully executed.
WritesExecutedInt int64 `json:"writesExecuted,omitempty"`
// The total number of data-modification operations that were unsuccessful
WritesIgnoredInt int64 `json:"writesIgnored,omitempty"`
// The total number of documents iterated over when scanning a collection without an index.
ScannedFullInt int64 `json:"scannedFull,omitempty"`
// The total number of documents iterated over when scanning a collection using an index.
ScannedIndexInt int64 `json:"scannedIndex,omitempty"`
// The total number of documents that were removed after executing a filter condition in a FilterNode
FilteredInt int64 `json:"filtered,omitempty"`
// The total number of documents that matched the search condition if the query's final LIMIT statement were not present.
FullCountInt int64 `json:"fullCount,omitempty"`
// Query execution time (wall-clock time). value will be set from the outside
ExecutionTimeInt float64 `json:"executionTime,omitempty"`
Nodes []cursorPlanNodes `json:"nodes,omitempty"`
HttpRequests int64 `json:"httpRequests,omitempty"`
PeakMemoryUsage int64 `json:"peakMemoryUsage,omitempty"`
// CursorsCreated the total number of cursor objects created during query execution. Cursor objects are created for index lookups.
CursorsCreated uint64 `json:"cursorsCreated,omitempty"`
// CursorsRearmed the total number of times an existing cursor object was repurposed.
// Repurposing an existing cursor object is normally more efficient compared to destroying an existing cursor object
// and creating a new one from scratch.
CursorsRearmed uint64 `json:"cursorsRearmed,omitempty"`
// CacheHits the total number of index entries read from in-memory caches for indexes of type edge or persistent.
// This value will only be non-zero when reading from indexes that have an in-memory cache enabled,
// and when the query allows using the in-memory cache (i.e. using equality lookups on all index attributes).
CacheHits uint64 `json:"cacheHits,omitempty"`
// CacheMisses the total number of cache read attempts for index entries that could not be served from in-memory caches for indexes of type edge or persistent.
// This value will only be non-zero when reading from indexes that have an in-memory cache enabled,
// the query allows using the in-memory cache (i.e. using equality lookups on all index attributes) and the looked up values are not present in the cache.
CacheMisses uint64 `json:"cacheMisses,omitempty"`
}
type cursorPlan struct {
Nodes []cursorPlanNodes `json:"nodes,omitempty"`
Rules []string `json:"rules,omitempty"`
Collections []cursorPlanCollection `json:"collections,omitempty"`
Variables []cursorPlanVariable `json:"variables,omitempty"`
EstimatedCost float64 `json:"estimatedCost,omitempty"`
EstimatedNrItems int `json:"estimatedNrItems,omitempty"`
IsModificationQuery bool `json:"isModificationQuery,omitempty"`
}
type cursorExtra struct {
Stats cursorStats `json:"stats,omitempty"`
Profile cursorProfile `json:"profile,omitempty"`
Plan *cursorPlan `json:"plan,omitempty"`
Warnings []warn `json:"warnings,omitempty"`
}
type warn struct {
Code int `json:"code"`
Message string `json:"message"`
}
func (c cursorExtra) GetStatistics() QueryStatistics {
return c.Stats
}
func (c cursorExtra) GetProfileRaw() ([]byte, bool, error) {
if c.Profile == nil {
return nil, false, nil
}
d, err := json.Marshal(c.Profile)
if err != nil {
return nil, true, err
}
return d, true, nil
}
func (c cursorExtra) GetPlanRaw() ([]byte, bool, error) {
if c.Plan == nil {
return nil, false, nil
}
d, err := json.Marshal(c.Plan)
if err != nil {
return nil, true, err
}
return d, true, nil
}
type cursorPlanVariable struct {
ID int `json:"id"`
Name string `json:"name"`
IsDataFromCollection bool `json:"isDataFromCollection"`
IsFullDocumentFromCollection bool `json:"isFullDocumentFromCollection"`
}
type cursorPlanCollection struct {
Name string `json:"name"`
Type string `json:"type"`
}
type cursorPlanNodes map[string]interface{}
type cursorProfile map[string]interface{}
type cursorData struct {
Key string `json:"_key,omitempty"`
Count int64 `json:"count,omitempty"` // the total number of result documents available (only available if the query was executed with the count attribute set)
ID string `json:"id"` // id of temporary cursor created on the server (optional, see above)
Result []*RawObject `json:"result,omitempty"` // an array of result documents (might be empty if query has no results)
HasMore bool `json:"hasMore,omitempty"` // A boolean indicator whether there are more results available for the cursor on the server
Extra cursorExtra `json:"extra"`
Cached bool `json:"cached,omitempty"`
ArangoError
}
// relPath creates the relative path to this cursor (`_db/<db-name>/_api/cursor`)
func (c *cursor) relPath() string {
return path.Join(c.db.relPath(), "_api", "cursor")
}
// Name returns the name of the collection.
func (c *cursor) HasMore() bool {
return c.resultIndex < len(c.Result) || c.cursorData.HasMore
}
// Count returns the total number of result documents available.
// A valid return value is only available when the cursor has been created with a context that was
// prepare with `WithQueryCount`.
func (c *cursor) Count() int64 {
return c.cursorData.Count
}
// Close deletes the cursor and frees the resources associated with it.
func (c *cursor) Close() error {
if c == nil {
// Avoid panics in the case that someone defer's a close before checking that the cursor is not nil.
return nil
}
if c := atomic.LoadInt32(&c.closed); c != 0 {
return nil
}
c.closeMutex.Lock()
defer c.closeMutex.Unlock()
if c.closed == 0 {
if c.cursorData.ID != "" {
// Force use of initial endpoint
ctx := WithEndpoint(nil, c.endpoint)
req, err := c.conn.NewRequest("DELETE", path.Join(c.relPath(), c.cursorData.ID))
if err != nil {
return WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(202); err != nil {
return WithStack(err)
}
}
atomic.StoreInt32(&c.closed, 1)
}
return nil
}
// ReadDocument reads the next document from the cursor.
// The document data is stored into result, the document meta data is returned.
// If the cursor has no more documents, a NoMoreDocuments error is returned.
func (c *cursor) ReadDocument(ctx context.Context, result interface{}) (DocumentMeta, error) {
// Force use of initial endpoint
ctx = WithEndpoint(ctx, c.endpoint)
if c.resultIndex >= len(c.Result) && c.cursorData.HasMore {
// This is required since we are interested if this was a dirty read
// but we do not want to trash the users bool reference.
var wasDirtyRead bool
fetchctx := ctx
if c.allowDirtyReads {
fetchctx = WithAllowDirtyReads(ctx, &wasDirtyRead)
}
// Fetch next batch
req, err := c.conn.NewRequest("PUT", path.Join(c.relPath(), c.cursorData.ID))
if err != nil {
return DocumentMeta{}, WithStack(err)
}
cs := applyContextSettings(fetchctx, req)
resp, err := c.conn.Do(fetchctx, req)
if err != nil {
return DocumentMeta{}, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return DocumentMeta{}, WithStack(err)
}
loadContextResponseValues(cs, resp)
var data cursorData
if err := resp.ParseBody("", &data); err != nil {
return DocumentMeta{}, WithStack(err)
}
c.cursorData = data
c.resultIndex = 0
c.lastReadWasDirty = wasDirtyRead
}
// ReadDocument should act as if it would actually do a read
// hence update the bool reference
if c.allowDirtyReads {
setDirtyReadFlagIfRequired(ctx, c.lastReadWasDirty)
}
index := c.resultIndex
if index >= len(c.Result) {
// Out of data
return DocumentMeta{}, WithStack(NoMoreDocumentsError{})
}
c.resultIndex++
var meta DocumentMeta
resultPtr := c.Result[index]
if resultPtr == nil {
// Got NULL result
rv := reflect.ValueOf(result)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return DocumentMeta{}, WithStack(&json.InvalidUnmarshalError{Type: reflect.TypeOf(result)})
}
e := rv.Elem()
e.Set(reflect.Zero(e.Type()))
} else {
if err := c.conn.Unmarshal(*resultPtr, &meta); err != nil {
// If a cursor returns something other than a document, this will fail.
// Just ignore it.
}
if err := c.conn.Unmarshal(*resultPtr, result); err != nil {
return DocumentMeta{}, WithStack(err)
}
}
return meta, nil
}
// Return execution statistics for this cursor. This might not
// be valid if the cursor has been created with a context that was
// prepared with `WithStream`
func (c *cursor) Statistics() QueryStatistics {
return c.cursorData.Extra.Stats
}
func (c *cursor) Extra() QueryExtra {
return c.cursorData.Extra
}
// the total number of data-modification operations successfully executed.
func (cs cursorStats) WritesExecuted() int64 {
return cs.WritesExecutedInt
}
// The total number of data-modification operations that were unsuccessful
func (cs cursorStats) WritesIgnored() int64 {
return cs.WritesIgnoredInt
}
// The total number of documents iterated over when scanning a collection without an index.
func (cs cursorStats) ScannedFull() int64 {
return cs.ScannedFullInt
}
// The total number of documents iterated over when scanning a collection using an index.
func (cs cursorStats) ScannedIndex() int64 {
return cs.ScannedIndexInt
}
// the total number of documents that were removed after executing a filter condition in a FilterNode
func (cs cursorStats) Filtered() int64 {
return cs.FilteredInt
}
// Returns the numer of results before the last LIMIT in the query was applied.
// A valid return value is only available when the has been created with a context that was
// prepared with `WithFullCount`. Additionally this will also not return a valid value if
// the context was prepared with `WithStream`.
func (cs cursorStats) FullCount() int64 {
return cs.FullCountInt
}
// query execution time (wall-clock time). value will be set from the outside
func (cs cursorStats) ExecutionTime() time.Duration {
return time.Duration(cs.ExecutionTimeInt * float64(time.Second))
}

@ -0,0 +1,129 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import "context"
// Database provides access to all collections & graphs in a single database.
type Database interface {
// Name returns the name of the database.
Name() string
// Info fetches information about the database.
Info(ctx context.Context) (DatabaseInfo, error)
// EngineInfo returns information about the database engine being used.
// Note: When your cluster has multiple endpoints (cluster), you will get information
// from the server that is currently being used.
// If you want to know exactly which server the information is from, use a client
// with only a single endpoint and avoid automatic synchronization of endpoints.
EngineInfo(ctx context.Context) (EngineInfo, error)
// Remove removes the entire database.
// If the database does not exist, a NotFoundError is returned.
Remove(ctx context.Context) error
// Collection functions
DatabaseCollections
// View functions
DatabaseViews
// Graph functions
DatabaseGraphs
// Pregel functions
DatabasePregels
// Streaming Transactions functions
DatabaseStreamingTransactions
// ArangoSearch Analyzers API
DatabaseArangoSearchAnalyzers
// Query performs an AQL query, returning a cursor used to iterate over the returned documents.
// Note that the returned Cursor must always be closed to avoid holding on to resources in the server while they are no longer needed.
Query(ctx context.Context, query string, bindVars map[string]interface{}) (Cursor, error)
// ValidateQuery validates an AQL query.
// When the query is valid, nil returned, otherwise an error is returned.
// The query is not executed.
ValidateQuery(ctx context.Context, query string) error
// OptimizerRulesForQueries returns the available optimizer rules for AQL queries
// returns an array of objects that contain the name of each available rule and its respective flags.
OptimizerRulesForQueries(ctx context.Context) ([]QueryRule, error)
// Transaction performs a javascript transaction. The result of the transaction function is returned.
Transaction(ctx context.Context, action string, options *TransactionOptions) (interface{}, error)
}
// DatabaseInfo contains information about a database
type DatabaseInfo struct {
// The identifier of the database.
ID string `json:"id,omitempty"`
// The name of the database.
Name string `json:"name,omitempty"`
// The filesystem path of the database.
Path string `json:"path,omitempty"`
// If true then the database is the _system database.
IsSystem bool `json:"isSystem,omitempty"`
// Default replication factor for collections in database
ReplicationFactor int `json:"replicationFactor,omitempty"`
// Default write concern for collections in database
WriteConcern int `json:"writeConcern,omitempty"`
// Default sharding for collections in database
Sharding DatabaseSharding `json:"sharding,omitempty"`
}
// EngineType indicates type of database engine being used.
type EngineType string
const (
EngineTypeMMFiles = EngineType("mmfiles")
EngineTypeRocksDB = EngineType("rocksdb")
)
func (t EngineType) String() string {
return string(t)
}
// EngineInfo contains information about the database engine being used.
type EngineInfo struct {
Type EngineType `json:"name"`
Supports map[string]interface{} `json:"supports,omitempty"`
}
type QueryRule struct {
Name string `json:"name"`
Flags QueryFlags `json:"flags,omitempty"`
}
type QueryFlags struct {
Hidden bool `json:"hidden,omitempty"`
ClusterOnly bool `json:"clusterOnly,omitempty"`
CanBeDisabled bool `json:"canBeDisabled,omitempty"`
CanCreateAdditionalPlans bool `json:"canCreateAdditionalPlans,omitempty"`
DisabledByDefault bool `json:"disabledByDefault,omitempty"`
EnterpriseOnly bool `json:"enterpriseOnly,omitempty"`
}

@ -0,0 +1,61 @@
//
// DISCLAIMER
//
// Copyright 2018 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
//
// Author Lars Maier
//
package driver
import "context"
type ArangoSearchAnalyzer interface {
// Name returns the analyzer name
Name() string
// Type returns the analyzer type
Type() ArangoSearchAnalyzerType
// UniqueName returns the unique name: <database>::<analyzer-name>
UniqueName() string
// Definition returns the analyzer definition
Definition() ArangoSearchAnalyzerDefinition
// Properties returns the analyzer properties
Properties() ArangoSearchAnalyzerProperties
// Database returns the database of this analyzer
Database() Database
// Removes the analyzers
Remove(ctx context.Context, force bool) error
}
type DatabaseArangoSearchAnalyzers interface {
// Ensure ensures that the given analyzer exists. If it does not exist it is created.
// The function returns whether the analyzer already existed or an error.
EnsureAnalyzer(ctx context.Context, analyzer ArangoSearchAnalyzerDefinition) (bool, ArangoSearchAnalyzer, error)
// Get returns the analyzer definition for the given analyzer or returns an error
Analyzer(ctx context.Context, name string) (ArangoSearchAnalyzer, error)
// List returns a list of all analyzers
Analyzers(ctx context.Context) ([]ArangoSearchAnalyzer, error)
}

@ -0,0 +1,184 @@
//
// DISCLAIMER
//
// Copyright 2018 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
//
// Author Lars Maier
//
package driver
import (
"context"
"path"
"strings"
)
type analyzer struct {
definition ArangoSearchAnalyzerDefinition
db *database
}
// Name returns the analyzer name
func (a *analyzer) Name() string {
split := strings.Split(a.definition.Name, "::")
return split[len(split)-1]
}
// UniqueName returns the unique name: <database>::<analyzer-name>
func (a *analyzer) UniqueName() string {
return a.definition.Name
}
// Type returns the analyzer type
func (a *analyzer) Type() ArangoSearchAnalyzerType {
return a.definition.Type
}
// Definition returns the analyzer definition
func (a *analyzer) Definition() ArangoSearchAnalyzerDefinition {
return a.definition
}
// Properties returns the analyzer properties
func (a *analyzer) Properties() ArangoSearchAnalyzerProperties {
return a.definition.Properties
}
// Removes the analyzers
func (a *analyzer) Remove(ctx context.Context, force bool) error {
req, err := a.db.conn.NewRequest("DELETE", path.Join(a.db.relPath(), "_api/analyzer/", a.Name()))
if err != nil {
return WithStack(err)
}
applyContextSettings(ctx, req)
payload := struct {
Force bool `json:"force,omitempty"`
}{
Force: force,
}
req, err = req.SetBody(payload)
if err != nil {
return WithStack(err)
}
resp, err := a.db.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return WithStack(err)
}
var actualDef ArangoSearchAnalyzerDefinition
if err := resp.ParseBody("", &actualDef); err != nil {
return WithStack(err)
}
return nil
}
// Database returns the database of this analyzer
func (a *analyzer) Database() Database {
return a.db
}
// Ensure ensures that the given analyzer exists. If it does not exist it is created.
// The function returns whether the analyzer already existed or an error.
func (d *database) EnsureAnalyzer(ctx context.Context, definition ArangoSearchAnalyzerDefinition) (bool, ArangoSearchAnalyzer, error) {
req, err := d.conn.NewRequest("POST", path.Join(d.relPath(), "_api/analyzer"))
if err != nil {
return false, nil, WithStack(err)
}
applyContextSettings(ctx, req)
req, err = req.SetBody(definition)
if err != nil {
return false, nil, WithStack(err)
}
resp, err := d.conn.Do(ctx, req)
if err != nil {
return false, nil, WithStack(err)
}
if err := resp.CheckStatus(201, 200); err != nil {
return false, nil, WithStack(err)
}
found := resp.StatusCode() == 200
var actualDef ArangoSearchAnalyzerDefinition
if err := resp.ParseBody("", &actualDef); err != nil {
return false, nil, WithStack(err)
}
return found, &analyzer{
db: d,
definition: actualDef,
}, nil
}
// Get returns the analyzer definition for the given analyzer or returns an error
func (d *database) Analyzer(ctx context.Context, name string) (ArangoSearchAnalyzer, error) {
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/analyzer/", name))
if err != nil {
return nil, WithStack(err)
}
applyContextSettings(ctx, req)
resp, err := d.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
var actualDef ArangoSearchAnalyzerDefinition
if err := resp.ParseBody("", &actualDef); err != nil {
return nil, WithStack(err)
}
return &analyzer{
db: d,
definition: actualDef,
}, nil
}
type analyzerListResponse struct {
Analyzer []ArangoSearchAnalyzerDefinition `json:"result,omitempty"`
ArangoError
}
// List returns a list of all analyzers
func (d *database) Analyzers(ctx context.Context) ([]ArangoSearchAnalyzer, error) {
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/analyzer"))
if err != nil {
return nil, WithStack(err)
}
applyContextSettings(ctx, req)
resp, err := d.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
var response analyzerListResponse
if err := resp.ParseBody("", &response); err != nil {
return nil, WithStack(err)
}
result := make([]ArangoSearchAnalyzer, 0, len(response.Analyzer))
for _, a := range response.Analyzer {
result = append(result, &analyzer{
db: d,
definition: a,
})
}
return result, nil
}

@ -0,0 +1,224 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import "context"
// DatabaseCollections provides access to all collections in a single database.
type DatabaseCollections interface {
// Collection opens a connection to an existing collection within the database.
// If no collection with given name exists, an NotFoundError is returned.
Collection(ctx context.Context, name string) (Collection, error)
// CollectionExists returns true if a collection with given name exists within the database.
CollectionExists(ctx context.Context, name string) (bool, error)
// Collections returns a list of all collections in the database.
Collections(ctx context.Context) ([]Collection, error)
// CreateCollection creates a new collection with given name and options, and opens a connection to it.
// If a collection with given name already exists within the database, a DuplicateError is returned.
CreateCollection(ctx context.Context, name string, options *CreateCollectionOptions) (Collection, error)
}
// CreateCollectionOptions contains options that customize the creating of a collection.
type CreateCollectionOptions struct {
// CacheEnabled set cacheEnabled option in collection properties
CacheEnabled *bool `json:"cacheEnabled,omitempty"`
// ComputedValues let configure collections to generate document attributes when documents are created or modified, using an AQL expression
ComputedValues []ComputedValue `json:"computedValues,omitempty"`
// This field is used for internal purposes only. DO NOT USE.
DistributeShardsLike string `json:"distributeShardsLike,omitempty"`
// DoCompact checks if the collection will be compacted (default is true)
DoCompact *bool `json:"doCompact,omitempty"`
// The number of buckets into which indexes using a hash table are split. The default is 16 and this number has to be a power
// of 2 and less than or equal to 1024. For very large collections one should increase this to avoid long pauses when the hash
// table has to be initially built or resized, since buckets are resized individually and can be initially built in parallel.
// For example, 64 might be a sensible value for a collection with 100 000 000 documents.
// Currently, only the edge index respects this value, but other index types might follow in future ArangoDB versions.
// Changes are applied when the collection is loaded the next time.
IndexBuckets int `json:"indexBuckets,omitempty"`
// Available from 3.9 ArangoD version.
InternalValidatorType int `json:"internalValidatorType,omitempty"`
// IsDisjoint set isDisjoint flag for Graph. Required ArangoDB 3.7+
IsDisjoint bool `json:"isDisjoint,omitempty"`
// Set to create a smart edge or vertex collection.
// This requires ArangoDB Enterprise Edition.
IsSmart bool `json:"isSmart,omitempty"`
// If true, create a system collection. In this case collection-name should start with an underscore.
// End users should normally create non-system collections only. API implementors may be required to create system
// collections in very special occasions, but normally a regular collection will do. (The default is false)
IsSystem bool `json:"isSystem,omitempty"`
// If true then the collection data is kept in-memory only and not made persistent.
// Unloading the collection will cause the collection data to be discarded. Stopping or re-starting the server will also
// cause full loss of data in the collection. Setting this option will make the resulting collection be slightly faster
// than regular collections because ArangoDB does not enforce any synchronization to disk and does not calculate any
// CRC checksums for datafiles (as there are no datafiles). This option should therefore be used for cache-type collections only,
// and not for data that cannot be re-created otherwise. (The default is false)
IsVolatile bool `json:"isVolatile,omitempty"`
// The maximal size of a journal or datafile in bytes. The value must be at least 1048576 (1 MiB). (The default is a configuration parameter)
JournalSize int `json:"journalSize,omitempty"`
// Specifies how keys in the collection are created.
KeyOptions *CollectionKeyOptions `json:"keyOptions,omitempty"`
// Deprecated: use 'WriteConcern' instead
MinReplicationFactor int `json:"minReplicationFactor,omitempty"`
// In a cluster, this value determines the number of shards to create for the collection. In a single server setup, this option is meaningless. (default is 1)
NumberOfShards int `json:"numberOfShards,omitempty"`
// ReplicationFactor in a cluster (default is 1), this attribute determines how many copies of each shard are kept on different DBServers.
// The value 1 means that only one copy (no synchronous replication) is kept.
// A value of k means that k-1 replicas are kept. Any two copies reside on different DBServers.
// Replication between them is synchronous, that is, every write operation to the "leader" copy will be replicated to all "follower" replicas,
// before the write operation is reported successful. If a server fails, this is detected automatically
// and one of the servers holding copies take over, usually without an error being reported.
ReplicationFactor int `json:"replicationFactor,omitempty"`
// Schema for collection validation
Schema *CollectionSchemaOptions `json:"schema,omitempty"`
// This attribute specifies the name of the sharding strategy to use for the collection.
// Must be one of ShardingStrategy* values.
ShardingStrategy ShardingStrategy `json:"shardingStrategy,omitempty"`
// In a cluster, this attribute determines which document attributes are used to
// determine the target shard for documents. Documents are sent to shards based on the values of their shard key attributes.
// The values of all shard key attributes in a document are hashed, and the hash value is used to determine the target shard.
// Note: Values of shard key attributes cannot be changed once set. This option is meaningless in a single server setup.
// The default is []string{"_key"}.
ShardKeys []string `json:"shardKeys,omitempty"`
// This field must be set to the attribute that will be used for sharding or smart graphs.
// All vertices are required to have this attribute set. Edges derive the attribute from their connected vertices.
// This requires ArangoDB Enterprise Edition.
SmartGraphAttribute string `json:"smartGraphAttribute,omitempty"`
// SmartJoinAttribute
// In the specific case that the two collections have the same number of shards, the data of the two collections can
// be co-located on the same server for the same shard key values. In this case the extra hop via the coordinator will not be necessary.
// See documentation for smart joins.
// This requires ArangoDB Enterprise Edition.
SmartJoinAttribute string `json:"smartJoinAttribute,omitempty"`
// Available from 3.7 ArangoDB version
SyncByRevision bool `json:"syncByRevision,omitempty"`
// The type of the collection to create. (default is CollectionTypeDocument)
Type CollectionType `json:"type,omitempty"`
// If true then the data is synchronized to disk before returning from a document create, update, replace or removal operation. (default: false)
WaitForSync bool `json:"waitForSync,omitempty"`
// WriteConcern contains how many copies must be available before a collection can be written.
// It is required that 1 <= WriteConcern <= ReplicationFactor.
// Default is 1. Not available for satellite collections.
// Available from 3.6 ArangoDB version.
WriteConcern int `json:"writeConcern,omitempty"`
}
// Init translate deprecated fields into current one for backward compatibility
func (c *CreateCollectionOptions) Init() {
if c == nil {
return
}
c.KeyOptions.Init()
}
// CollectionType is the type of a collection.
type CollectionType int
const (
// CollectionTypeDocument specifies a document collection
CollectionTypeDocument = CollectionType(2)
// CollectionTypeEdge specifies an edges collection
CollectionTypeEdge = CollectionType(3)
)
type ComputedValue struct {
// The name of the target attribute. Can only be a top-level attribute, but you
// may return a nested object. Cannot be `_key`, `_id`, `_rev`, `_from`, `_to`,
// or a shard key attribute.
Name string `json:"name"`
// An AQL `RETURN` operation with an expression that computes the desired value.
Expression string `json:"expression"`
// An array of strings to define on which write operations the value shall be
// computed. The possible values are `"insert"`, `"update"`, and `"replace"`.
// The default is `["insert", "update", "replace"]`.
ComputeOn []ComputeOn `json:"computeOn,omitempty"`
// Whether the computed value shall take precedence over a user-provided or existing attribute.
Overwrite bool `json:"overwrite"`
// Whether to let the write operation fail if the expression produces a warning. The default is false.
FailOnWarning *bool `json:"failOnWarning,omitempty"`
// Whether the result of the expression shall be stored if it evaluates to `null`.
// This can be used to skip the value computation if any pre-conditions are not met.
KeepNull *bool `json:"keepNull,omitempty"`
}
type ComputeOn string
const (
ComputeOnInsert ComputeOn = "insert"
ComputeOnUpdate ComputeOn = "update"
ComputeOnReplace ComputeOn = "replace"
)
// CollectionKeyOptions specifies ways for creating keys of a collection.
type CollectionKeyOptions struct {
// If set to true, then it is allowed to supply own key values in the _key attribute of a document.
// If set to false, then the key generator will solely be responsible for generating keys and supplying own
// key values in the _key attribute of documents is considered an error.
// Deprecated: Use AllowUserKeysPtr instead
AllowUserKeys bool `json:"-"`
// If set to true, then it is allowed to supply own key values in the _key attribute of a document.
// If set to false, then the key generator will solely be responsible for generating keys and supplying own
// key values in the _key attribute of documents is considered an error.
AllowUserKeysPtr *bool `json:"allowUserKeys,omitempty"`
// Specifies the type of the key generator. The currently available generators are traditional and autoincrement.
Type KeyGeneratorType `json:"type,omitempty"`
// increment value for autoincrement key generator. Not used for other key generator types.
Increment int `json:"increment,omitempty"`
// Initial offset value for autoincrement key generator. Not used for other key generator types.
Offset int `json:"offset,omitempty"`
}
// Init translate deprecated fields into current one for backward compatibility
func (c *CollectionKeyOptions) Init() {
if c == nil {
return
}
if c.AllowUserKeysPtr == nil {
if c.AllowUserKeys {
c.AllowUserKeysPtr = &c.AllowUserKeys
}
}
}
// KeyGeneratorType is a type of key generated, used in `CollectionKeyOptions`.
type KeyGeneratorType string
const (
KeyGeneratorTraditional = KeyGeneratorType("traditional")
KeyGeneratorAutoIncrement = KeyGeneratorType("autoincrement")
)
// ShardingStrategy describes the sharding strategy of a collection
type ShardingStrategy string
const (
ShardingStrategyCommunityCompat ShardingStrategy = "community-compat"
ShardingStrategyEnterpriseCompat ShardingStrategy = "enterprise-compat"
ShardingStrategyEnterpriseSmartEdgeCompat ShardingStrategy = "enterprise-smart-edge-compat"
ShardingStrategyHash ShardingStrategy = "hash"
ShardingStrategyEnterpriseHashSmartEdge ShardingStrategy = "enterprise-hash-smart-edge"
)

@ -0,0 +1,190 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
"path"
)
// Collection opens a connection to an existing collection within the database.
// If no collection with given name exists, an NotFoundError is returned.
func (d *database) Collection(ctx context.Context, name string) (Collection, error) {
escapedName := pathEscape(name)
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/collection", escapedName))
if err != nil {
return nil, WithStack(err)
}
resp, err := d.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
coll, err := newCollection(name, d)
if err != nil {
return nil, WithStack(err)
}
return coll, nil
}
// CollectionExists returns true if a collection with given name exists within the database.
func (d *database) CollectionExists(ctx context.Context, name string) (bool, error) {
escapedName := pathEscape(name)
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/collection", escapedName))
if err != nil {
return false, WithStack(err)
}
resp, err := d.conn.Do(ctx, req)
if err != nil {
return false, WithStack(err)
}
if err := resp.CheckStatus(200); err == nil {
return true, nil
} else if IsNotFound(err) {
return false, nil
} else {
return false, WithStack(err)
}
}
type getCollectionResponse struct {
Result []CollectionInfo `json:"result,omitempty"`
}
// Collections returns a list of all collections in the database.
func (d *database) Collections(ctx context.Context) ([]Collection, error) {
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/collection"))
if err != nil {
return nil, WithStack(err)
}
resp, err := d.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
var data getCollectionResponse
if err := resp.ParseBody("", &data); err != nil {
return nil, WithStack(err)
}
result := make([]Collection, 0, len(data.Result))
for _, info := range data.Result {
col, err := newCollection(info.Name, d)
if err != nil {
return nil, WithStack(err)
}
result = append(result, col)
}
return result, nil
}
type createCollectionOptionsInternal struct {
CacheEnabled *bool `json:"cacheEnabled,omitempty"`
ComputedValues []ComputedValue `json:"computedValues,omitempty"`
DistributeShardsLike string `json:"distributeShardsLike,omitempty"`
DoCompact *bool `json:"doCompact,omitempty"`
IndexBuckets int `json:"indexBuckets,omitempty"`
InternalValidatorType int `json:"internalValidatorType,omitempty"`
IsDisjoint bool `json:"isDisjoint,omitempty"`
IsSmart bool `json:"isSmart,omitempty"`
IsSystem bool `json:"isSystem,omitempty"`
IsVolatile bool `json:"isVolatile,omitempty"`
JournalSize int `json:"journalSize,omitempty"`
KeyOptions *CollectionKeyOptions `json:"keyOptions,omitempty"`
// Deprecated: use 'WriteConcern' instead
MinReplicationFactor int `json:"minReplicationFactor,omitempty"`
Name string `json:"name"`
NumberOfShards int `json:"numberOfShards,omitempty"`
ReplicationFactor replicationFactor `json:"replicationFactor,omitempty"`
Schema *CollectionSchemaOptions `json:"schema,omitempty"`
ShardingStrategy ShardingStrategy `json:"shardingStrategy,omitempty"`
ShardKeys []string `json:"shardKeys,omitempty"`
SmartGraphAttribute string `json:"smartGraphAttribute,omitempty"`
SmartJoinAttribute string `json:"smartJoinAttribute,omitempty"`
SyncByRevision bool `json:"syncByRevision,omitempty"`
Type CollectionType `json:"type,omitempty"`
WaitForSync bool `json:"waitForSync,omitempty"`
WriteConcern int `json:"writeConcern,omitempty"`
}
// CreateCollection creates a new collection with given name and options, and opens a connection to it.
// If a collection with given name already exists within the database, a DuplicateError is returned.
func (d *database) CreateCollection(ctx context.Context, name string, options *CreateCollectionOptions) (Collection, error) {
options.Init()
input := createCollectionOptionsInternal{
Name: name,
}
if options != nil {
input.fromExternal(options)
}
req, err := d.conn.NewRequest("POST", path.Join(d.relPath(), "_api/collection"))
if err != nil {
return nil, WithStack(err)
}
if _, err := req.SetBody(input); err != nil {
return nil, WithStack(err)
}
applyContextSettings(ctx, req)
resp, err := d.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
col, err := newCollection(name, d)
if err != nil {
return nil, WithStack(err)
}
return col, nil
}
func (p *createCollectionOptionsInternal) fromExternal(i *CreateCollectionOptions) {
p.CacheEnabled = i.CacheEnabled
p.ComputedValues = i.ComputedValues
p.DistributeShardsLike = i.DistributeShardsLike
p.DoCompact = i.DoCompact
p.IndexBuckets = i.IndexBuckets
p.InternalValidatorType = i.InternalValidatorType
p.IsDisjoint = i.IsDisjoint
p.IsSmart = i.IsSmart
p.IsSystem = i.IsSystem
p.IsVolatile = i.IsVolatile
p.JournalSize = i.JournalSize
p.KeyOptions = i.KeyOptions
p.MinReplicationFactor = i.MinReplicationFactor
p.NumberOfShards = i.NumberOfShards
p.ReplicationFactor = replicationFactor(i.ReplicationFactor)
p.Schema = i.Schema
p.ShardingStrategy = i.ShardingStrategy
p.ShardKeys = i.ShardKeys
p.SmartGraphAttribute = i.SmartGraphAttribute
p.SmartJoinAttribute = i.SmartJoinAttribute
p.SyncByRevision = i.SyncByRevision
p.Type = i.Type
p.WaitForSync = i.WaitForSync
p.WriteConcern = i.WriteConcern
}

@ -0,0 +1,54 @@
//
// DISCLAIMER
//
// Copyright 2020 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
//
// Author Adam Janikowski
//
package driver
import (
"encoding/json"
)
type CollectionSchemaLevel string
const (
CollectionSchemaLevelNone CollectionSchemaLevel = "none"
CollectionSchemaLevelNew CollectionSchemaLevel = "new"
CollectionSchemaLevelModerate CollectionSchemaLevel = "moderate"
CollectionSchemaLevelStrict CollectionSchemaLevel = "strict"
)
type CollectionSchemaOptions struct {
Rule interface{} `json:"rule,omitempty"`
Level CollectionSchemaLevel `json:"level,omitempty"`
Message string `json:"message,omitempty"`
Type string `json:"type,omitempty"`
}
func (d *CollectionSchemaOptions) LoadRule(data []byte) error {
var rule interface{}
if err := json.Unmarshal(data, &rule); err != nil {
return err
}
d.Rule = rule
return nil
}

@ -0,0 +1,93 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import "context"
const (
SatelliteGraph = -100
)
// DatabaseGraphs provides access to all graphs in a single database.
type DatabaseGraphs interface {
// Graph opens a connection to an existing graph within the database.
// If no graph with given name exists, an NotFoundError is returned.
Graph(ctx context.Context, name string) (Graph, error)
// GraphExists returns true if a graph with given name exists within the database.
GraphExists(ctx context.Context, name string) (bool, error)
// Graphs returns a list of all graphs in the database.
Graphs(ctx context.Context) ([]Graph, error)
// CreateGraph creates a new graph with given name and options, and opens a connection to it.
// If a graph with given name already exists within the database, a DuplicateError is returned.
// Deprecated: since ArangoDB 3.9 - please use CreateGraphV2 instead
CreateGraph(ctx context.Context, name string, options *CreateGraphOptions) (Graph, error)
// CreateGraphV2 creates a new graph with given name and options, and opens a connection to it.
// If a graph with given name already exists within the database, a DuplicateError is returned.
CreateGraphV2(ctx context.Context, name string, options *CreateGraphOptions) (Graph, error)
}
// CreateGraphOptions contains options that customize the creating of a graph.
type CreateGraphOptions struct {
// OrphanVertexCollections is an array of additional vertex collections used in the graph.
// These are vertices for which there are no edges linking these vertices with anything.
OrphanVertexCollections []string
// EdgeDefinitions is an array of edge definitions for the graph.
EdgeDefinitions []EdgeDefinition
// IsSmart defines if the created graph should be smart.
// This only has effect in Enterprise Edition.
IsSmart bool
// SmartGraphAttribute is the attribute name that is used to smartly shard the vertices of a graph.
// Every vertex in this Graph has to have this attribute.
// Cannot be modified later.
SmartGraphAttribute string
// NumberOfShards is the number of shards that is used for every collection within this graph.
// Cannot be modified later.
NumberOfShards int
// ReplicationFactor is the number of replication factor that is used for every collection within this graph.
// Cannot be modified later.
ReplicationFactor int
// WriteConcern is the number of min replication factor that is used for every collection within this graph.
// Cannot be modified later.
WriteConcern int
// IsDisjoint set isDisjoint flag for Graph. Required ArangoDB 3.7+
IsDisjoint bool
// Satellites contains an array of collection names that will be used to create SatelliteCollections for a Hybrid (Disjoint) SmartGraph (Enterprise Edition only)
// Requires ArangoDB 3.9+
Satellites []string `json:"satellites,omitempty"`
}
// EdgeDefinition contains all information needed to define a single edge in a graph.
type EdgeDefinition struct {
// The name of the edge collection to be used.
Collection string `json:"collection"`
// To contains the names of one or more vertex collections that can contain target vertices.
To []string `json:"to"`
// From contains the names of one or more vertex collections that can contain source vertices.
From []string `json:"from"`
// Options contains optional parameters
Options CreateEdgeCollectionOptions `json:"options,omitempty"`
}

@ -0,0 +1,271 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
"encoding/json"
"path"
"github.com/pkg/errors"
)
// Graph opens a connection to an existing graph within the database.
// If no graph with given name exists, an NotFoundError is returned.
func (d *database) Graph(ctx context.Context, name string) (Graph, error) {
escapedName := pathEscape(name)
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/gharial", escapedName))
if err != nil {
return nil, WithStack(err)
}
resp, err := d.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
var data getGraphResponse
if err := resp.ParseBody("", &data); err != nil {
return nil, WithStack(err)
}
g, err := newGraph(data.Graph, d)
if err != nil {
return nil, WithStack(err)
}
return g, nil
}
// GraphExists returns true if a graph with given name exists within the database.
func (d *database) GraphExists(ctx context.Context, name string) (bool, error) {
escapedName := pathEscape(name)
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/gharial", escapedName))
if err != nil {
return false, WithStack(err)
}
resp, err := d.conn.Do(ctx, req)
if err != nil {
return false, WithStack(err)
}
if err := resp.CheckStatus(200); err == nil {
return true, nil
} else if IsNotFound(err) {
return false, nil
} else {
return false, WithStack(err)
}
}
type getGraphsResponse struct {
Graphs []graphDefinition `json:"graphs,omitempty"`
ArangoError
}
// Graphs returns a list of all graphs in the database.
func (d *database) Graphs(ctx context.Context) ([]Graph, error) {
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/gharial"))
if err != nil {
return nil, WithStack(err)
}
resp, err := d.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
var data getGraphsResponse
if err := resp.ParseBody("", &data); err != nil {
return nil, WithStack(err)
}
result := make([]Graph, 0, len(data.Graphs))
for _, info := range data.Graphs {
g, err := newGraph(info, d)
if err != nil {
return nil, WithStack(err)
}
result = append(result, g)
}
return result, nil
}
type createGraphOptions struct {
Name string `json:"name"`
OrphanVertexCollections []string `json:"orphanCollections,omitempty"`
EdgeDefinitions []EdgeDefinition `json:"edgeDefinitions,omitempty"`
IsSmart bool `json:"isSmart,omitempty"`
Options *createGraphAdditionalOptions `json:"options,omitempty"`
}
type graphReplicationFactor int
func (g graphReplicationFactor) MarshalJSON() ([]byte, error) {
switch g {
case SatelliteGraph:
return json.Marshal(replicationFactorSatelliteString)
default:
return json.Marshal(int(g))
}
}
func (g *graphReplicationFactor) UnmarshalJSON(data []byte) error {
var d int
if err := json.Unmarshal(data, &d); err == nil {
*g = graphReplicationFactor(d)
return nil
}
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
switch s {
case replicationFactorSatelliteString:
*g = graphReplicationFactor(SatelliteGraph)
return nil
default:
return errors.Errorf("Unsupported type %s", s)
}
}
type createGraphAdditionalOptions struct {
// SmartGraphAttribute is the attribute name that is used to smartly shard the vertices of a graph.
// Every vertex in this Graph has to have this attribute.
// Cannot be modified later.
SmartGraphAttribute string `json:"smartGraphAttribute,omitempty"`
// NumberOfShards is the number of shards that is used for every collection within this graph.
// Cannot be modified later.
NumberOfShards int `json:"numberOfShards,omitempty"`
// ReplicationFactor is the number of replication factor that is used for every collection within this graph.
// Cannot be modified later.
ReplicationFactor graphReplicationFactor `json:"replicationFactor,omitempty"`
// WriteConcern is the number of min replication factor that is used for every collection within this graph.
// Cannot be modified later.
WriteConcern int `json:"writeConcern,omitempty"`
// IsDisjoint set isDisjoint flag for Graph. Required ArangoDB 3.7+
IsDisjoint bool `json:"isDisjoint,omitempty"`
// Satellites contains an array of collection names that will be used to create SatelliteCollections for a Hybrid (Disjoint) SmartGraph (Enterprise Edition only)
// Requires ArangoDB 3.9+
Satellites []string `json:"satellites,omitempty"`
}
// CreateGraph creates a new graph with given name and options, and opens a connection to it.
// If a graph with given name already exists within the database, a DuplicateError is returned.
// Deprecated: since ArangoDB 3.9 - please use CreateGraphV2 instead
func (d *database) CreateGraph(ctx context.Context, name string, options *CreateGraphOptions) (Graph, error) {
input := createGraphOptions{
Name: name,
}
if options != nil {
input.OrphanVertexCollections = options.OrphanVertexCollections
input.EdgeDefinitions = options.EdgeDefinitions
input.IsSmart = options.IsSmart
if options.ReplicationFactor == SatelliteGraph {
input.Options = &createGraphAdditionalOptions{
SmartGraphAttribute: options.SmartGraphAttribute,
ReplicationFactor: graphReplicationFactor(options.ReplicationFactor),
IsDisjoint: options.IsDisjoint,
Satellites: options.Satellites,
}
} else if options.SmartGraphAttribute != "" || options.NumberOfShards != 0 {
input.Options = &createGraphAdditionalOptions{
SmartGraphAttribute: options.SmartGraphAttribute,
NumberOfShards: options.NumberOfShards,
ReplicationFactor: graphReplicationFactor(options.ReplicationFactor),
WriteConcern: options.WriteConcern,
IsDisjoint: options.IsDisjoint,
Satellites: options.Satellites,
}
}
}
req, err := d.conn.NewRequest("POST", path.Join(d.relPath(), "_api/gharial"))
if err != nil {
return nil, WithStack(err)
}
if _, err := req.SetBody(input); err != nil {
return nil, WithStack(err)
}
resp, err := d.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(201, 202); err != nil {
return nil, WithStack(err)
}
var data getGraphResponse
if err := resp.ParseBody("", &data); err != nil {
return nil, WithStack(err)
}
g, err := newGraph(data.Graph, d)
if err != nil {
return nil, WithStack(err)
}
return g, nil
}
// CreateGraphV2 creates a new graph with given name and options, and opens a connection to it.
// If a graph with given name already exists within the database, a DuplicateError is returned.
func (d *database) CreateGraphV2(ctx context.Context, name string, options *CreateGraphOptions) (Graph, error) {
input := createGraphOptions{
Name: name,
}
if options != nil {
input.OrphanVertexCollections = options.OrphanVertexCollections
input.EdgeDefinitions = options.EdgeDefinitions
input.IsSmart = options.IsSmart
input.Options = &createGraphAdditionalOptions{
SmartGraphAttribute: options.SmartGraphAttribute,
NumberOfShards: options.NumberOfShards,
ReplicationFactor: graphReplicationFactor(options.ReplicationFactor),
WriteConcern: options.WriteConcern,
IsDisjoint: options.IsDisjoint,
Satellites: options.Satellites,
}
}
req, err := d.conn.NewRequest("POST", path.Join(d.relPath(), "_api/gharial"))
if err != nil {
return nil, WithStack(err)
}
if _, err := req.SetBody(input); err != nil {
return nil, WithStack(err)
}
resp, err := d.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(201, 202); err != nil {
return nil, WithStack(err)
}
var data getGraphResponse
if err := resp.ParseBody("", &data); err != nil {
return nil, WithStack(err)
}
g, err := newGraph(data.Graph, d)
if err != nil {
return nil, WithStack(err)
}
return g, nil
}

@ -0,0 +1,250 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
"net/http"
"path"
)
// newDatabase creates a new Database implementation.
func newDatabase(name string, conn Connection) (Database, error) {
if name == "" {
return nil, WithStack(InvalidArgumentError{Message: "name is empty"})
}
if conn == nil {
return nil, WithStack(InvalidArgumentError{Message: "conn is nil"})
}
return &database{
name: name,
conn: conn,
}, nil
}
// database implements the Database interface.
type database struct {
name string
conn Connection
}
// relPath creates the relative path to this database (`_db/<name>`)
func (d *database) relPath() string {
escapedName := pathEscape(d.name)
return path.Join("_db", escapedName)
}
// Name returns the name of the database.
func (d *database) Name() string {
return d.name
}
// Info fetches information about the database.
func (d *database) Info(ctx context.Context) (DatabaseInfo, error) {
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/database/current"))
if err != nil {
return DatabaseInfo{}, WithStack(err)
}
applyContextSettings(ctx, req)
resp, err := d.conn.Do(ctx, req)
if err != nil {
return DatabaseInfo{}, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return DatabaseInfo{}, WithStack(err)
}
var data DatabaseInfo
if err := resp.ParseBody("result", &data); err != nil {
return DatabaseInfo{}, WithStack(err)
}
return data, nil
}
// EngineInfo returns information about the database engine being used.
// Note: When your cluster has multiple endpoints (cluster), you will get information
// from the server that is currently being used.
// If you want to know exactly which server the information is from, use a client
// with only a single endpoint and avoid automatic synchronization of endpoints.
func (d *database) EngineInfo(ctx context.Context) (EngineInfo, error) {
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/engine"))
if err != nil {
return EngineInfo{}, WithStack(err)
}
resp, err := d.conn.Do(ctx, req)
if err != nil {
return EngineInfo{}, WithStack(err)
}
if err := resp.CheckStatus(200, 404); err != nil {
return EngineInfo{}, WithStack(err)
}
if resp.StatusCode() == 404 {
// On version 3.1, this endpoint is not yet supported
return EngineInfo{Type: EngineTypeMMFiles}, nil
}
var data EngineInfo
if err := resp.ParseBody("", &data); err != nil {
return EngineInfo{}, WithStack(err)
}
return data, nil
}
// Remove removes the entire database.
// If the database does not exist, a NotFoundError is returned.
func (d *database) Remove(ctx context.Context) error {
req, err := d.conn.NewRequest("DELETE", path.Join("_db/_system/_api/database", pathEscape(d.name)))
if err != nil {
return WithStack(err)
}
resp, err := d.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return WithStack(err)
}
return nil
}
// Query performs an AQL query, returning a cursor used to iterate over the returned documents.
func (d *database) Query(ctx context.Context, query string, bindVars map[string]interface{}) (Cursor, error) {
req, err := d.conn.NewRequest("POST", path.Join(d.relPath(), "_api/cursor"))
if err != nil {
return nil, WithStack(err)
}
input := queryRequest{
Query: query,
BindVars: bindVars,
}
input.applyContextSettings(ctx)
if _, err := req.SetBody(input); err != nil {
return nil, WithStack(err)
}
cs := applyContextSettings(ctx, req)
resp, err := d.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(201); err != nil {
return nil, WithStack(err)
}
var data cursorData
if err := resp.ParseBody("", &data); err != nil {
return nil, WithStack(err)
}
col, err := newCursor(data, resp.Endpoint(), d, cs.AllowDirtyReads)
if err != nil {
return nil, WithStack(err)
}
return col, nil
}
// ValidateQuery validates an AQL query.
// When the query is valid, nil returned, otherwise an error is returned.
// The query is not executed.
func (d *database) ValidateQuery(ctx context.Context, query string) error {
req, err := d.conn.NewRequest("POST", path.Join(d.relPath(), "_api/query"))
if err != nil {
return WithStack(err)
}
input := parseQueryRequest{
Query: query,
}
if _, err := req.SetBody(input); err != nil {
return WithStack(err)
}
resp, err := d.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return WithStack(err)
}
return nil
}
// OptimizerRulesForQueries returns the available optimizer rules for AQL query
// returns an array of objects that contain the name of each available rule and its respective flags.
func (d *database) OptimizerRulesForQueries(ctx context.Context) ([]QueryRule, error) {
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/query/rules"))
if err != nil {
return []QueryRule{}, WithStack(err)
}
resp, err := d.conn.Do(ctx, req)
if err != nil {
return []QueryRule{}, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return []QueryRule{}, WithStack(err)
}
var data []QueryRule
responses, err := resp.ParseArrayBody()
if err != nil {
return []QueryRule{}, WithStack(err)
}
for _, response := range responses {
var rule QueryRule
if err := response.ParseBody("", &rule); err != nil {
return []QueryRule{}, WithStack(err)
}
data = append(data, rule)
}
return data, nil
}
func (d *database) Transaction(ctx context.Context, action string, options *TransactionOptions) (interface{}, error) {
req, err := d.conn.NewRequest("POST", path.Join(d.relPath(), "_api/transaction"))
if err != nil {
return nil, WithStack(err)
}
input := transactionRequest{Action: action}
if options != nil {
input.MaxTransactionSize = options.MaxTransactionSize
input.LockTimeout = options.LockTimeout
input.WaitForSync = options.WaitForSync
input.IntermediateCommitCount = options.IntermediateCommitCount
input.Params = options.Params
input.IntermediateCommitSize = options.IntermediateCommitSize
input.Collections.Read = options.ReadCollections
input.Collections.Write = options.WriteCollections
input.Collections.Exclusive = options.ExclusiveCollections
}
if _, err = req.SetBody(input); err != nil {
return nil, WithStack(err)
}
resp, err := d.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err = resp.CheckStatus(http.StatusOK); err != nil {
return nil, WithStack(err)
}
output := &transactionResponse{}
if err = resp.ParseBody("", output); err != nil {
return nil, WithStack(err)
}
return output.Result, nil
}

@ -0,0 +1,182 @@
//
// DISCLAIMER
//
// Copyright 2022 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"
"time"
)
// DatabasePregels provides access to all Pregel Jobs in a single database.
type DatabasePregels interface {
// StartJob - Start the execution of a Pregel algorithm
StartJob(ctx context.Context, options PregelJobOptions) (string, error)
// GetJob - Get the status of a Pregel execution
GetJob(ctx context.Context, id string) (*PregelJob, error)
// GetJobs - Returns a list of currently running and recently finished Pregel jobs without retrieving their results.
GetJobs(ctx context.Context) ([]*PregelJob, error)
// CancelJob - Cancel an ongoing Pregel execution
CancelJob(ctx context.Context, id string) error
}
type PregelAlgorithm string
const (
PregelAlgorithmPageRank PregelAlgorithm = "pagerank"
PregelAlgorithmSingleSourceShortestPath PregelAlgorithm = "sssp"
PregelAlgorithmConnectedComponents PregelAlgorithm = "connectedcomponents"
PregelAlgorithmWeaklyConnectedComponents PregelAlgorithm = "wcc"
PregelAlgorithmStronglyConnectedComponents PregelAlgorithm = "scc"
PregelAlgorithmHyperlinkInducedTopicSearch PregelAlgorithm = "hits"
PregelAlgorithmEffectiveCloseness PregelAlgorithm = "effectivecloseness"
PregelAlgorithmLineRank PregelAlgorithm = "linerank"
PregelAlgorithmLabelPropagation PregelAlgorithm = "labelpropagation"
PregelAlgorithmSpeakerListenerLabelPropagation PregelAlgorithm = "slpa"
)
type PregelJobOptions struct {
// Name of the algorithm
Algorithm PregelAlgorithm `json:"algorithm"`
// Name of a graph. Either this or the parameters VertexCollections and EdgeCollections are required.
// Please note that there are special sharding requirements for graphs in order to be used with Pregel.
GraphName string `json:"graphName,optional"`
// List of vertex collection names. Please note that there are special sharding requirements for collections in order to be used with Pregel.
VertexCollections []string `json:"vertexCollections,optional"`
// List of edge collection names. Please note that there are special sharding requirements for collections in order to be used with Pregel.
EdgeCollections []string `json:"edgeCollections,optional"`
// General as well as algorithm-specific options.
Params map[string]interface{} `json:"params,optional"`
}
type PregelJobState string
const (
// PregelJobStateNone - The Pregel run did not yet start.
PregelJobStateNone PregelJobState = "none"
// PregelJobStateLoading - The graph is loaded from the database into memory before the execution of the algorithm.
PregelJobStateLoading PregelJobState = "loading"
// PregelJobStateRunning - The algorithm is executing normally.
PregelJobStateRunning PregelJobState = "running"
// PregelJobStateStoring - The algorithm finished, but the results are still being written back into the collections. Occurs only if the store parameter is set to true.
PregelJobStateStoring PregelJobState = "storing"
// PregelJobStateDone - The execution is done. In version 3.7.1 and later, this means that storing is also done.
// In earlier versions, the results may not be written back into the collections yet. This event is announced in the server log (requires at least info log level for the pregel log topic).
PregelJobStateDone PregelJobState = "done"
// PregelJobStateCanceled - The execution was permanently canceled, either by the user or by an error.
PregelJobStateCanceled PregelJobState = "canceled"
// PregelJobStateFatalError - The execution has failed and cannot recover.
PregelJobStateFatalError PregelJobState = "fatal error"
// PregelJobStateInError - The execution is in an error state. This can be caused by DB-Servers being not reachable or being non-responsive.
// The execution might recover later, or switch to "canceled" if it was not able to recover successfully.
PregelJobStateInError PregelJobState = "in error"
// PregelJobStateRecovering - (currently unused): The execution is actively recovering and switches back to running if the recovery is successful.
PregelJobStateRecovering PregelJobState = "recovering"
)
type PregelJob struct {
// The ID of the Pregel job, as a string.
ID string `json:"id"`
// The algorithm used by the job.
Algorithm PregelAlgorithm `json:"algorithm,omitempty"`
// The date and time when the job was created.
Created time.Time `json:"created,omitempty"`
// The date and time when the job results expire.
// The expiration date is only meaningful for jobs that were completed, canceled or resulted in an error.
// Such jobs are cleaned up by the garbage collection when they reach their expiration date/time.
Started time.Time `json:"started,omitempty"`
// The TTL (time to live) value for the job results, specified in seconds. The TTL is used to calculate the expiration date for the job’s results.
TTL uint64 `json:"ttl,omitempty"`
// The state of the execution.
State PregelJobState `json:"state,omitempty"`
// The number of global supersteps executed.
Gss uint64 `json:"gss,omitempty"`
// The total runtime of the execution up to now (if the execution is still ongoing).
TotalRuntime float64 `json:"totalRuntime,omitempty"`
// The startup runtime of the execution. The startup time includes the data loading time and can be substantial.
StartupTime float64 `json:"startupTime,omitempty"`
// The algorithm execution time. Is shown when the computation started.
ComputationTime float64 `json:"computationTime,omitempty"`
// The time for storing the results if the job includes results storage. Is shown when the storing started.
StorageTime float64 `json:"storageTime,omitempty"`
// Computation time of each global super step. Is shown when the computation started.
GSSTimes []float64 `json:"gssTimes,omitempty"`
// This attribute is used by Programmable Pregel Algorithms (air, experimental). The value is only populated once the algorithm has finished.
Reports []map[string]interface{} `json:"reports,omitempty"`
// The total number of vertices processed.
VertexCount uint64 `json:"vertexCount,omitempty"`
// The total number of edges processed.
EdgeCount uint64 `json:"edgeCount,omitempty"`
// UseMemoryMaps
UseMemoryMaps *bool `json:"useMemoryMaps,omitempty"`
// The Pregel run details.
// Available from 3.10 arangod version.
Detail *PregelRunDetails `json:"detail,omitempty"`
}
// PregelRunDetails - The Pregel run details.
// Available from 3.10 arangod version.
type PregelRunDetails struct {
// The aggregated details of the full Pregel run. The values are totals of all the DB-Server.
AggregatedStatus *AggregatedStatus `json:"aggregatedStatus,omitempty"`
// The details of the Pregel for every DB-Server. Each object key is a DB-Server ID, and each value is a nested object similar to the aggregatedStatus attribute.
// In a single server deployment, there is only a single entry with an empty string as key.
WorkerStatus map[string]*AggregatedStatus `json:"workerStatus,omitempty"`
}
// AggregatedStatus The aggregated details of the full Pregel run. The values are totals of all the DB-Server.
type AggregatedStatus struct {
// The time at which the status was measured.
TimeStamp time.Time `json:"timeStamp,omitempty"`
// The status of the in memory graph.
GraphStoreStatus *GraphStoreStatus `json:"graphStoreStatus,omitempty"`
// Information about the global supersteps.
AllGSSStatus *AllGSSStatus `json:"allGssStatus,omitempty"`
}
// GraphStoreStatus The status of the in memory graph.
type GraphStoreStatus struct {
// The number of vertices that are loaded from the database into memory.
VerticesLoaded uint64 `json:"verticesLoaded,omitempty"`
// The number of edges that are loaded from the database into memory.
EdgesLoaded uint64 `json:"edgesLoaded,omitempty"`
// The number of bytes used in-memory for the loaded graph.
MemoryBytesUsed uint64 `json:"memoryBytesUsed,omitempty"`
// The number of vertices that are written back to the database after the Pregel computation finished. It is only set if the store parameter is set to true.
VerticesStored uint64 `json:"verticesStored,omitempty"`
}
// AllGSSStatus Information about the global supersteps.
type AllGSSStatus struct {
// A list of objects with details for each global superstep.
Items []GSSStatus `json:"items,omitempty"`
}
// GSSStatus Information about the global superstep
type GSSStatus struct {
// The number of vertices that have been processed in this step.
VerticesProcessed uint64 `json:"verticesProcessed,omitempty"`
// The number of messages sent in this step.
MessagesSent uint64 `json:"messagesSent,omitempty"`
// The number of messages received in this step.
MessagesReceived uint64 `json:"messagesReceived,omitempty"`
// The number of bytes used in memory for the messages in this step.
MemoryBytesUsedForMessages uint64 `json:"memoryBytesUsedForMessages,omitempty"`
}

@ -0,0 +1,116 @@
//
// DISCLAIMER
//
// Copyright 2022 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"
"path"
"strings"
)
func (d *database) StartJob(ctx context.Context, options PregelJobOptions) (string, error) {
id := ""
req, err := d.conn.NewRequest("POST", path.Join(d.relPath(), "_api/control_pregel"))
if err != nil {
return id, WithStack(err)
}
if _, err := req.SetBody(options); err != nil {
return id, WithStack(err)
}
var rawResponse []byte
ctx = WithRawResponse(ctx, &rawResponse)
resp, err := d.conn.Do(ctx, req)
if err != nil {
return id, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return id, WithStack(err)
}
return strings.Trim(string(rawResponse), "\""), nil
}
func (d *database) GetJob(ctx context.Context, id string) (*PregelJob, error) {
escapedId := pathEscape(id)
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/control_pregel", escapedId))
if err != nil {
return nil, WithStack(err)
}
resp, err := d.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
var data PregelJob
if err := resp.ParseBody("", &data); err != nil {
return nil, WithStack(err)
}
return &data, nil
}
func (d *database) GetJobs(ctx context.Context) ([]*PregelJob, error) {
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/control_pregel"))
if err != nil {
return nil, WithStack(err)
}
resp, err := d.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
var data []*PregelJob
responses, err := resp.ParseArrayBody()
if err != nil {
return nil, WithStack(err)
}
for _, response := range responses {
var job PregelJob
if err := response.ParseBody("", &job); err != nil {
return nil, WithStack(err)
}
data = append(data, &job)
}
return data, nil
}
func (d *database) CancelJob(ctx context.Context, id string) error {
escapedId := pathEscape(id)
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/control_pregel", escapedId))
if err != nil {
return WithStack(err)
}
resp, err := d.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return WithStack(err)
}
return nil
}

@ -0,0 +1,76 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Lars Maier
//
package driver
import (
"context"
"time"
)
// BeginTransactionOptions provides options for BeginTransaction call
type BeginTransactionOptions struct {
WaitForSync bool
AllowImplicit bool
LockTimeout time.Duration
MaxTransactionSize uint64
}
// TransactionCollections is used to specify which collections are accessed by
// a transaction and how
type TransactionCollections struct {
Read []string `json:"read,omitempty"`
Write []string `json:"write,omitempty"`
Exclusive []string `json:"exclusive,omitempty"`
}
// CommitTransactionOptions provides options for CommitTransaction. Currently unused
type CommitTransactionOptions struct{}
// AbortTransactionOptions provides options for CommitTransaction. Currently unused
type AbortTransactionOptions struct{}
// TransactionID identifies a transaction
type TransactionID string
// TransactionStatus describes the status of an transaction
type TransactionStatus string
const (
TransactionRunning TransactionStatus = "running"
TransactionCommitted TransactionStatus = "committed"
TransactionAborted TransactionStatus = "aborted"
)
// TransactionStatusRecord provides insight about the status of transaction
type TransactionStatusRecord struct {
Status TransactionStatus
}
// DatabaseStreamingTransactions provides access to the Streaming Transactions API
type DatabaseStreamingTransactions interface {
BeginTransaction(ctx context.Context, cols TransactionCollections, opts *BeginTransactionOptions) (TransactionID, error)
CommitTransaction(ctx context.Context, tid TransactionID, opts *CommitTransactionOptions) error
AbortTransaction(ctx context.Context, tid TransactionID, opts *AbortTransactionOptions) error
TransactionStatus(ctx context.Context, tid TransactionID) (TransactionStatusRecord, error)
}

@ -0,0 +1,100 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Lars Maier
//
package driver
import (
"context"
"path"
)
type beginTransactionRequest struct {
WaitForSync bool `json:"waitForSync,omitempty"`
AllowImplicit bool `json:"allowImplicit,omitempty"`
LockTimeout float64 `json:"lockTimeout,omitempty"`
MaxTransactionSize uint64 `json:"maxTransactionSize,omitempty"`
Collections TransactionCollections `json:"collections,omitempty"`
}
func (d *database) BeginTransaction(ctx context.Context, cols TransactionCollections, opts *BeginTransactionOptions) (TransactionID, error) {
req, err := d.conn.NewRequest("POST", path.Join(d.relPath(), "_api/transaction/begin"))
if err != nil {
return "", WithStack(err)
}
var reqBody beginTransactionRequest
if opts != nil {
reqBody.WaitForSync = opts.WaitForSync
reqBody.AllowImplicit = opts.AllowImplicit
reqBody.LockTimeout = opts.LockTimeout.Seconds()
}
reqBody.Collections = cols
if _, err := req.SetBody(reqBody); err != nil {
return "", WithStack(err)
}
resp, err := d.conn.Do(ctx, req)
if err != nil {
return "", WithStack(err)
}
if err := resp.CheckStatus(201); err != nil {
return "", WithStack(err)
}
var result struct {
TransactionID TransactionID `json:"id,omitempty"`
}
if err := resp.ParseBody("result", &result); err != nil {
return "", WithStack(err)
}
return result.TransactionID, nil
}
func (d *database) requestForTransaction(ctx context.Context, tid TransactionID, method string) (TransactionStatusRecord, error) {
req, err := d.conn.NewRequest(method, path.Join(d.relPath(), "_api/transaction/", string(tid)))
if err != nil {
return TransactionStatusRecord{}, WithStack(err)
}
resp, err := d.conn.Do(ctx, req)
if err != nil {
return TransactionStatusRecord{}, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return TransactionStatusRecord{}, WithStack(err)
}
var result TransactionStatusRecord
if err := resp.ParseBody("result", &result); err != nil {
return TransactionStatusRecord{}, WithStack(err)
}
return result, nil
}
func (d *database) CommitTransaction(ctx context.Context, tid TransactionID, opts *CommitTransactionOptions) error {
_, err := d.requestForTransaction(ctx, tid, "PUT")
return err
}
func (d *database) AbortTransaction(ctx context.Context, tid TransactionID, opts *AbortTransactionOptions) error {
_, err := d.requestForTransaction(ctx, tid, "DELETE")
return err
}
func (d *database) TransactionStatus(ctx context.Context, tid TransactionID) (TransactionStatusRecord, error) {
return d.requestForTransaction(ctx, tid, "GET")
}

@ -0,0 +1,58 @@
//
// DISCLAIMER
//
// Copyright 2018 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
//
// Author Ewout Prangsma
//
package driver
import "context"
// DatabaseViews provides access to all views in a single database.
// Views are only available in ArangoDB 3.4 and higher.
type DatabaseViews interface {
// View opens a connection to an existing view within the database.
// If no collection with given name exists, an NotFoundError is returned.
View(ctx context.Context, name string) (View, error)
// ViewExists returns true if a view with given name exists within the database.
ViewExists(ctx context.Context, name string) (bool, error)
// Views returns a list of all views in the database.
Views(ctx context.Context) ([]View, error)
// CreateArangoSearchView creates a new view of type ArangoSearch,
// with given name and options, and opens a connection to it.
// If a view with given name already exists within the database, a ConflictError is returned.
CreateArangoSearchView(ctx context.Context, name string, options *ArangoSearchViewProperties) (ArangoSearchView, error)
// CreateArangoSearchAliasView creates ArangoSearch alias view with given name and options, and opens a connection to it.
// If a view with given name already exists within the database, a ConflictError is returned.
CreateArangoSearchAliasView(ctx context.Context, name string, options *ArangoSearchAliasViewProperties) (ArangoSearchViewAlias, error)
}
// ViewType is the type of view.
type ViewType string
const (
// ViewTypeArangoSearch specifies an ArangoSearch view type.
ViewTypeArangoSearch = ViewType("arangosearch")
// ViewTypeArangoSearchAlias specifies an ArangoSearch view type alias.
ViewTypeArangoSearchAlias = ViewType("search-alias")
)

@ -0,0 +1,202 @@
//
// DISCLAIMER
//
// Copyright 2018 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
"path"
)
type viewInfo struct {
Name string `json:"name,omitempty"`
Type ViewType `json:"type,omitempty"`
ArangoID
ArangoError
}
type getViewResponse struct {
Result []viewInfo `json:"result,omitempty"`
ArangoError
}
// View opens a connection to an existing view within the database.
// If no collection with given name exists, an NotFoundError is returned.
func (d *database) View(ctx context.Context, name string) (View, error) {
escapedName := pathEscape(name)
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/view", escapedName))
if err != nil {
return nil, WithStack(err)
}
applyContextSettings(ctx, req)
resp, err := d.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
var data viewInfo
if err := resp.ParseBody("", &data); err != nil {
return nil, WithStack(err)
}
view, err := newView(name, data.Type, d)
if err != nil {
return nil, WithStack(err)
}
return view, nil
}
// ViewExists returns true if a view with given name exists within the database.
func (d *database) ViewExists(ctx context.Context, name string) (bool, error) {
escapedName := pathEscape(name)
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/view", escapedName))
if err != nil {
return false, WithStack(err)
}
applyContextSettings(ctx, req)
resp, err := d.conn.Do(ctx, req)
if err != nil {
return false, WithStack(err)
}
if err := resp.CheckStatus(200); err == nil {
return true, nil
} else if IsNotFound(err) {
return false, nil
} else {
return false, WithStack(err)
}
}
// Views returns a list of all views in the database.
func (d *database) Views(ctx context.Context) ([]View, error) {
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/view"))
if err != nil {
return nil, WithStack(err)
}
applyContextSettings(ctx, req)
resp, err := d.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
var data getViewResponse
if err := resp.ParseBody("", &data); err != nil {
return nil, WithStack(err)
}
result := make([]View, 0, len(data.Result))
for _, info := range data.Result {
view, err := newView(info.Name, info.Type, d)
if err != nil {
return nil, WithStack(err)
}
result = append(result, view)
}
return result, nil
}
// CreateArangoSearchView creates a new view of type ArangoSearch,
// with given name and options, and opens a connection to it.
// If a view with given name already exists within the database, a ConflictError is returned.
func (d *database) CreateArangoSearchView(ctx context.Context, name string, options *ArangoSearchViewProperties) (ArangoSearchView, error) {
input := struct {
Name string `json:"name"`
Type ViewType `json:"type"`
ArangoSearchViewProperties // `json:"properties"`
}{
Name: name,
Type: ViewTypeArangoSearch,
}
if options != nil {
input.ArangoSearchViewProperties = *options
}
req, err := d.conn.NewRequest("POST", path.Join(d.relPath(), "_api/view"))
if err != nil {
return nil, WithStack(err)
}
if _, err := req.SetBody(input); err != nil {
return nil, WithStack(err)
}
applyContextSettings(ctx, req)
resp, err := d.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(201); err != nil {
return nil, WithStack(err)
}
view, err := newView(name, input.Type, d)
if err != nil {
return nil, WithStack(err)
}
result, err := view.ArangoSearchView()
if err != nil {
return nil, WithStack(err)
}
return result, nil
}
// CreateArangoSearchAliasView creates a new view of type search-alias,
// with given name and options, and opens a connection to it.
// If a view with given name already exists within the database, a ConflictError is returned.
func (d *database) CreateArangoSearchAliasView(ctx context.Context, name string, options *ArangoSearchAliasViewProperties) (ArangoSearchViewAlias, error) {
input := struct {
Name string `json:"name"`
Type ViewType `json:"type"`
ArangoSearchAliasViewProperties
}{
Name: name,
Type: ViewTypeArangoSearchAlias,
}
if options != nil {
input.ArangoSearchAliasViewProperties = *options
}
req, err := d.conn.NewRequest("POST", path.Join(d.relPath(), "_api/view"))
if err != nil {
return nil, WithStack(err)
}
if _, err := req.SetBody(input); err != nil {
return nil, WithStack(err)
}
applyContextSettings(ctx, req)
resp, err := d.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(201); err != nil {
return nil, WithStack(err)
}
view, err := newView(name, input.Type, d)
if err != nil {
return nil, WithStack(err)
}
result, err := view.ArangoSearchViewAlias()
if err != nil {
return nil, WithStack(err)
}
return result, nil
}

@ -0,0 +1,43 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
/*
Package driver implements a Go driver for the ArangoDB database.
To get started, create a connection to the database and wrap a client around it.
// Create an HTTP connection to the database
conn, err := http.NewConnection(http.ConnectionConfig{
Endpoints: []string{"http://localhost:8529"},
})
if err != nil {
// Handle error
}
// Create a client
c, err := driver.NewClient(driver.ClientConfig{
Connection: conn,
})
if err != nil {
// Handle error
}
*/
package driver

@ -0,0 +1,31 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
// EdgeDocument is a minimal document for use in edge collection.
// You can use this in your own edge document structures completely use your own.
// If you use your own, make sure to include a `_from` and `_to` field.
type EdgeDocument struct {
From DocumentID `json:"_from,omitempty"`
To DocumentID `json:"_to,omitempty"`
}

@ -0,0 +1,596 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
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)
if cs.ReturnOld != nil {
return DocumentMeta{}, contextSettings{}, WithStack(InvalidArgumentError{Message: "ReturnOld is not supported when removing edges"})
}
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())})
}
}

@ -0,0 +1,187 @@
//
// DISCLAIMER
//
// Copyright 2017-2021 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
//
// Author Ewout Prangsma
// Author Tomasz Mielech
//
package driver
import (
"context"
"path"
)
// newEdgeCollection creates a new EdgeCollection implementation.
func newEdgeCollection(name string, g *graph) (Collection, error) {
if name == "" {
return nil, WithStack(InvalidArgumentError{Message: "name is empty"})
}
if g == nil {
return nil, WithStack(InvalidArgumentError{Message: "g is nil"})
}
return &edgeCollection{
name: name,
g: g,
conn: g.db.conn,
}, nil
}
type edgeCollection struct {
name string
g *graph
conn Connection
}
// relPath creates the relative path to this edge collection (`_db/<db-name>/_api/gharial/<graph-name>/edge/<collection-name>`)
func (c *edgeCollection) relPath() string {
escapedName := pathEscape(c.name)
return path.Join(c.g.relPath(), "edge", escapedName)
}
// Name returns the name of the edge collection.
func (c *edgeCollection) Name() string {
return c.name
}
// Database returns the database containing the collection.
func (c *edgeCollection) Database() Database {
return c.g.db
}
// rawCollection returns a standard document implementation of Collection
// for this edge collection.
func (c *edgeCollection) rawCollection() Collection {
result, _ := newCollection(c.name, c.g.db)
return result
}
// Status fetches the current status of the collection.
func (c *edgeCollection) Status(ctx context.Context) (CollectionStatus, error) {
result, err := c.rawCollection().Status(ctx)
if err != nil {
return CollectionStatus(0), WithStack(err)
}
return result, nil
}
// Checksum returns a checksum for the specified collection
func (c *edgeCollection) Checksum(ctx context.Context, withRevisions bool, withData bool) (CollectionChecksum, error) {
result, err := c.rawCollection().Checksum(ctx, withRevisions, withData)
if err != nil {
return CollectionChecksum{}, WithStack(err)
}
return result, nil
}
// Count fetches the number of document in the collection.
func (c *edgeCollection) Count(ctx context.Context) (int64, error) {
result, err := c.rawCollection().Count(ctx)
if err != nil {
return 0, WithStack(err)
}
return result, nil
}
// Statistics returns the number of documents and additional statistical information about the collection.
func (c *edgeCollection) Statistics(ctx context.Context) (CollectionStatistics, error) {
result, err := c.rawCollection().Statistics(ctx)
if err != nil {
return CollectionStatistics{}, WithStack(err)
}
return result, nil
}
// Revision fetches the revision ID of the collection.
// The revision ID is a server-generated string that clients can use to check whether data
// in a collection has changed since the last revision check.
func (c *edgeCollection) Revision(ctx context.Context) (string, error) {
result, err := c.rawCollection().Revision(ctx)
if err != nil {
return "", WithStack(err)
}
return result, nil
}
// Properties fetches extended information about the collection.
func (c *edgeCollection) Properties(ctx context.Context) (CollectionProperties, error) {
result, err := c.rawCollection().Properties(ctx)
if err != nil {
return CollectionProperties{}, WithStack(err)
}
return result, nil
}
// SetProperties changes properties of the collection.
func (c *edgeCollection) SetProperties(ctx context.Context, options SetCollectionPropertiesOptions) error {
if err := c.rawCollection().SetProperties(ctx, options); err != nil {
return WithStack(err)
}
return nil
}
// Shards fetches shards information of the collection.
func (c *edgeCollection) Shards(ctx context.Context, details bool) (CollectionShards, error) {
result, err := c.rawCollection().Shards(ctx, details)
if err != nil {
return result, WithStack(err)
}
return result, nil
}
// Load the collection into memory.
func (c *edgeCollection) Load(ctx context.Context) error {
if err := c.rawCollection().Load(ctx); err != nil {
return WithStack(err)
}
return nil
}
// UnLoad the collection from memory.
func (c *edgeCollection) Unload(ctx context.Context) error {
if err := c.rawCollection().Unload(ctx); err != nil {
return WithStack(err)
}
return nil
}
// Remove removes the entire collection.
// If the collection does not exist, a NotFoundError is returned.
func (c *edgeCollection) Remove(ctx context.Context) error {
req, err := c.conn.NewRequest("DELETE", c.relPath())
if err != nil {
return WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(201, 202); err != nil {
return WithStack(err)
}
return nil
}
// Truncate removes all documents from the collection, but leaves the indexes intact.
func (c *edgeCollection) Truncate(ctx context.Context) error {
if err := c.rawCollection().Truncate(ctx); err != nil {
return WithStack(err)
}
return nil
}

@ -0,0 +1,148 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import "context"
// Index opens a connection to an existing index within the collection.
// If no index with given name exists, an NotFoundError is returned.
func (c *edgeCollection) Index(ctx context.Context, name string) (Index, error) {
result, err := c.rawCollection().Index(ctx, name)
if err != nil {
return nil, WithStack(err)
}
return result, nil
}
// IndexExists returns true if an index with given name exists within the collection.
func (c *edgeCollection) IndexExists(ctx context.Context, name string) (bool, error) {
result, err := c.rawCollection().IndexExists(ctx, name)
if err != nil {
return false, WithStack(err)
}
return result, nil
}
// Indexes returns a list of all indexes in the collection.
func (c *edgeCollection) Indexes(ctx context.Context) ([]Index, error) {
result, err := c.rawCollection().Indexes(ctx)
if err != nil {
return nil, WithStack(err)
}
return result, nil
}
// Deprecated: since 3.10 version. Use ArangoSearch view instead.
// EnsureFullTextIndex creates a fulltext index in the collection, if it does not already exist.
//
// Fields is a slice of attribute names. Currently, the slice is limited to exactly one attribute.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
func (c *edgeCollection) EnsureFullTextIndex(ctx context.Context, fields []string, options *EnsureFullTextIndexOptions) (Index, bool, error) {
result, created, err := c.rawCollection().EnsureFullTextIndex(ctx, fields, options)
if err != nil {
return nil, false, WithStack(err)
}
return result, created, nil
}
// EnsureGeoIndex creates a hash index in the collection, if it does not already exist.
//
// Fields is a slice with one or two attribute paths. If it is a slice with one attribute path location,
// then a geo-spatial index on all documents is created using location as path to the coordinates.
// The value of the attribute must be a slice with at least two double values. The slice must contain the latitude (first value)
// and the longitude (second value). All documents, which do not have the attribute path or with value that are not suitable, are ignored.
// If it is a slice with two attribute paths latitude and longitude, then a geo-spatial index on all documents is created
// using latitude and longitude as paths the latitude and the longitude. The value of the attribute latitude and of the
// attribute longitude must a double. All documents, which do not have the attribute paths or which values are not suitable, are ignored.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
func (c *edgeCollection) EnsureGeoIndex(ctx context.Context, fields []string, options *EnsureGeoIndexOptions) (Index, bool, error) {
result, created, err := c.rawCollection().EnsureGeoIndex(ctx, fields, options)
if err != nil {
return nil, false, WithStack(err)
}
return result, created, nil
}
// EnsureHashIndex creates a hash index in the collection, if it does not already exist.
// Fields is a slice of attribute paths.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
func (c *edgeCollection) EnsureHashIndex(ctx context.Context, fields []string, options *EnsureHashIndexOptions) (Index, bool, error) {
result, created, err := c.rawCollection().EnsureHashIndex(ctx, fields, options)
if err != nil {
return nil, false, WithStack(err)
}
return result, created, nil
}
// EnsurePersistentIndex creates a persistent index in the collection, if it does not already exist.
// Fields is a slice of attribute paths.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
func (c *edgeCollection) EnsurePersistentIndex(ctx context.Context, fields []string, options *EnsurePersistentIndexOptions) (Index, bool, error) {
result, created, err := c.rawCollection().EnsurePersistentIndex(ctx, fields, options)
if err != nil {
return nil, false, WithStack(err)
}
return result, created, nil
}
// EnsureSkipListIndex creates a skiplist index in the collection, if it does not already exist.
// Fields is a slice of attribute paths.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
func (c *edgeCollection) EnsureSkipListIndex(ctx context.Context, fields []string, options *EnsureSkipListIndexOptions) (Index, bool, error) {
result, created, err := c.rawCollection().EnsureSkipListIndex(ctx, fields, options)
if err != nil {
return nil, false, WithStack(err)
}
return result, created, nil
}
// EnsureTTLIndex creates a TLL collection, if it does not already exist.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
func (c *edgeCollection) EnsureTTLIndex(ctx context.Context, field string, expireAfter int, options *EnsureTTLIndexOptions) (Index, bool, error) {
result, created, err := c.rawCollection().EnsureTTLIndex(ctx, field, expireAfter, options)
if err != nil {
return nil, false, WithStack(err)
}
return result, created, nil
}
// EnsureZKDIndex creates a ZKD index in the collection, if it does not already exist.
// Fields is a slice of attribute paths.
// The index is returned, together with a boolean indicating if the index was newly created (true) or pre-existing (false).
func (c *edgeCollection) EnsureZKDIndex(ctx context.Context, fields []string, options *EnsureZKDIndexOptions) (Index, bool, error) {
result, created, err := c.rawCollection().EnsureZKDIndex(ctx, fields, options)
if err != nil {
return nil, false, WithStack(err)
}
return result, created, nil
}
// EnsureInvertedIndex creates an inverted index in the collection, if it does not already exist.
// Available in ArangoDB 3.10 and later.
func (c *edgeCollection) EnsureInvertedIndex(ctx context.Context, options *InvertedIndexOptions) (Index, bool, error) {
result, created, err := c.rawCollection().EnsureInvertedIndex(ctx, options)
if err != nil {
return nil, false, WithStack(err)
}
return result, created, nil
}

@ -0,0 +1,39 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
//go:build go1.8
// +build go1.8
package driver
import "net/url"
// pathEscape the given value for use in a URL path.
func pathEscape(s string) string {
return url.PathEscape(s)
}
// pathUnescape unescapes the given value for use in a URL path.
func pathUnescape(s string) string {
r, _ := url.PathUnescape(s)
return r
}

@ -0,0 +1,39 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
//go:build !go1.8
// +build !go1.8
package driver
import "net/url"
// Escape the given value for use in a URL path.
func pathEscape(s string) string {
return url.QueryEscape(s)
}
// pathUnescape unescapes the given value for use in a URL path.
func pathUnescape(s string) string {
r, _ := url.QueryUnescape(s)
return r
}

@ -0,0 +1,316 @@
//
// DISCLAIMER
//
// Copyright 2017-2021 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
"fmt"
"net"
"net/http"
"net/url"
"os"
)
const (
// general errors
ErrNotImplemented = 9
ErrForbidden = 11
ErrDisabled = 36
// HTTP error status codes
ErrHttpForbidden = 403
ErrHttpInternal = 501
// Internal ArangoDB storage errors
ErrArangoReadOnly = 1004
// External ArangoDB storage errors
ErrArangoCorruptedDatafile = 1100
ErrArangoIllegalParameterFile = 1101
ErrArangoCorruptedCollection = 1102
ErrArangoFileSystemFull = 1104
ErrArangoDataDirLocked = 1107
// General ArangoDB storage errors
ErrArangoConflict = 1200
ErrArangoDocumentNotFound = 1202
ErrArangoDataSourceNotFound = 1203
ErrArangoUniqueConstraintViolated = 1210
ErrArangoDatabaseNameInvalid = 1229
// ArangoDB cluster errors
ErrClusterLeadershipChallengeOngoing = 1495
ErrClusterNotLeader = 1496
// User management errors
ErrUserDuplicate = 1702
)
// ArangoError is a Go error with arangodb specific error information.
type ArangoError struct {
HasError bool `json:"error"`
Code int `json:"code"`
ErrorNum int `json:"errorNum"`
ErrorMessage string `json:"errorMessage"`
}
// Error returns the error message of an ArangoError.
func (ae ArangoError) Error() string {
if ae.ErrorMessage != "" {
return ae.ErrorMessage
}
return fmt.Sprintf("ArangoError: Code %d, ErrorNum %d", ae.Code, ae.ErrorNum)
}
// Timeout returns true when the given error is a timeout error.
func (ae ArangoError) Timeout() bool {
return ae.HasError && (ae.Code == http.StatusRequestTimeout || ae.Code == http.StatusGatewayTimeout)
}
// Temporary returns true when the given error is a temporary error.
func (ae ArangoError) Temporary() bool {
return ae.HasError && ae.Code == http.StatusServiceUnavailable
}
// newArangoError creates a new ArangoError with given values.
func newArangoError(code, errorNum int, errorMessage string) error {
return ArangoError{
HasError: true,
Code: code,
ErrorNum: errorNum,
ErrorMessage: errorMessage,
}
}
// IsArangoError returns true when the given error is an ArangoError.
func IsArangoError(err error) bool {
ae, ok := Cause(err).(ArangoError)
return ok && ae.HasError
}
// AsArangoError returns true when the given error is an ArangoError together with an object.
func AsArangoError(err error) (ArangoError, bool) {
ae, ok := Cause(err).(ArangoError)
if ok {
return ae, true
} else {
return ArangoError{}, false
}
}
// IsArangoErrorWithCode returns true when the given error is an ArangoError and its Code field is equal to the given code.
func IsArangoErrorWithCode(err error, code int) bool {
ae, ok := Cause(err).(ArangoError)
return ok && ae.Code == code
}
// IsArangoErrorWithErrorNum returns true when the given error is an ArangoError and its ErrorNum field is equal to one of the given numbers.
func IsArangoErrorWithErrorNum(err error, errorNum ...int) bool {
ae, ok := Cause(err).(ArangoError)
if !ok {
return false
}
for _, x := range errorNum {
if ae.ErrorNum == x {
return true
}
}
return false
}
// IsInvalidRequest returns true if the given error is an ArangoError with code 400, indicating an invalid request.
func IsInvalidRequest(err error) bool {
return IsArangoErrorWithCode(err, http.StatusBadRequest)
}
// IsUnauthorized returns true if the given error is an ArangoError with code 401, indicating an unauthorized request.
func IsUnauthorized(err error) bool {
return IsArangoErrorWithCode(err, http.StatusUnauthorized)
}
// IsForbidden returns true if the given error is an ArangoError with code 403, indicating a forbidden request.
func IsForbidden(err error) bool {
return IsArangoErrorWithCode(err, http.StatusForbidden)
}
// Deprecated: Use IsNotFoundGeneral instead.
// For ErrArangoDocumentNotFound error there is a chance that we get a different HTTP code if the API requires an existing document as input, which is not found.
//
// IsNotFound returns true if the given error is an ArangoError with code 404, indicating a object not found.
func IsNotFound(err error) bool {
return IsArangoErrorWithCode(err, http.StatusNotFound) ||
IsArangoErrorWithErrorNum(err, ErrArangoDocumentNotFound, ErrArangoDataSourceNotFound)
}
// IsNotFoundGeneral returns true if the given error is an ArangoError with code 404, indicating an object is not found.
func IsNotFoundGeneral(err error) bool {
return IsArangoErrorWithCode(err, http.StatusNotFound)
}
// IsDataSourceOrDocumentNotFound returns true if the given error is an Arango storage error, indicating an object is not found.
func IsDataSourceOrDocumentNotFound(err error) bool {
return IsArangoErrorWithCode(err, http.StatusNotFound) &&
IsArangoErrorWithErrorNum(err, ErrArangoDocumentNotFound, ErrArangoDataSourceNotFound)
}
// IsExternalStorageError returns true if ArangoDB is having an error with accessing or writing to storage.
func IsExternalStorageError(err error) bool {
return IsArangoErrorWithErrorNum(
err,
ErrArangoCorruptedDatafile,
ErrArangoIllegalParameterFile,
ErrArangoCorruptedCollection,
ErrArangoFileSystemFull,
ErrArangoDataDirLocked,
)
}
// IsConflict returns true if the given error is an ArangoError with code 409, indicating a conflict.
func IsConflict(err error) bool {
return IsArangoErrorWithCode(err, http.StatusConflict) || IsArangoErrorWithErrorNum(err, ErrUserDuplicate)
}
// IsPreconditionFailed returns true if the given error is an ArangoError with code 412, indicating a failed precondition.
func IsPreconditionFailed(err error) bool {
return IsArangoErrorWithCode(err, http.StatusPreconditionFailed) ||
IsArangoErrorWithErrorNum(err, ErrArangoConflict, ErrArangoUniqueConstraintViolated)
}
// IsNoLeader returns true if the given error is an ArangoError with code 503 error number 1496.
func IsNoLeader(err error) bool {
return IsArangoErrorWithCode(err, http.StatusServiceUnavailable) && IsArangoErrorWithErrorNum(err, ErrClusterNotLeader)
}
// IsNoLeaderOrOngoing return true if the given error is an ArangoError with code 503 and error number 1496 or 1495
func IsNoLeaderOrOngoing(err error) bool {
return IsArangoErrorWithCode(err, http.StatusServiceUnavailable) &&
IsArangoErrorWithErrorNum(err, ErrClusterLeadershipChallengeOngoing, ErrClusterNotLeader)
}
// InvalidArgumentError is returned when a go function argument is invalid.
type InvalidArgumentError struct {
Message string
}
// Error implements the error interface for InvalidArgumentError.
func (e InvalidArgumentError) Error() string {
return e.Message
}
// IsInvalidArgument returns true if the given error is an InvalidArgumentError.
func IsInvalidArgument(err error) bool {
_, ok := Cause(err).(InvalidArgumentError)
return ok
}
// NoMoreDocumentsError is returned by Cursor's, when an attempt is made to read documents when there are no more.
type NoMoreDocumentsError struct{}
// Error implements the error interface for NoMoreDocumentsError.
func (e NoMoreDocumentsError) Error() string {
return "no more documents"
}
// IsNoMoreDocuments returns true if the given error is an NoMoreDocumentsError.
func IsNoMoreDocuments(err error) bool {
_, ok := Cause(err).(NoMoreDocumentsError)
return ok
}
// A ResponseError is returned when a request was completely written to a server, but
// the server did not respond, or some kind of network error occurred during the response.
type ResponseError struct {
Err error
}
// Error returns the Error() result of the underlying error.
func (e *ResponseError) Error() string {
return e.Err.Error()
}
// IsResponse returns true if the given error is (or is caused by) a ResponseError.
func IsResponse(err error) bool {
return isCausedBy(err, func(e error) bool { _, ok := e.(*ResponseError); return ok })
}
// IsCanceled returns true if the given error is the result on a cancelled context.
func IsCanceled(err error) bool {
return isCausedBy(err, func(e error) bool { return e == context.Canceled })
}
// IsTimeout returns true if the given error is the result on a deadline that has been exceeded.
func IsTimeout(err error) bool {
return isCausedBy(err, func(e error) bool { return e == context.DeadlineExceeded })
}
// isCausedBy returns true if the given error returns true on the given predicate,
// unwrapping various standard library error wrappers.
func isCausedBy(err error, p func(error) bool) bool {
if p(err) {
return true
}
err = Cause(err)
for {
if p(err) {
return true
} else if err == nil {
return false
}
if xerr, ok := err.(*ResponseError); ok {
err = xerr.Err
} else if xerr, ok := err.(*url.Error); ok {
err = xerr.Err
} else if xerr, ok := err.(*net.OpError); ok {
err = xerr.Err
} else if xerr, ok := err.(*os.SyscallError); ok {
err = xerr.Err
} else {
return false
}
}
}
var (
// WithStack is called on every return of an error to add stacktrace information to the error.
// When setting this function, also set the Cause function.
// The interface of this function is compatible with functions in github.com/pkg/errors.
WithStack = func(err error) error { return err }
// Cause is used to get the root cause of the given error.
// The interface of this function is compatible with functions in github.com/pkg/errors.
Cause = func(err error) error { return err }
)
// ErrorSlice is a slice of errors
type ErrorSlice []error
// FirstNonNil returns the first error in the slice that is not nil.
// If all errors in the slice are nil, nil is returned.
func (l ErrorSlice) FirstNonNil() error {
for _, e := range l {
if e != nil {
return e
}
}
return nil
}

@ -0,0 +1,85 @@
//
// DISCLAIMER
//
// Copyright 2020 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
//
// Author Tomasz Mielech
//
package driver
import (
"context"
"net/http"
"os"
"strconv"
)
// InstallFoxxService installs a new service at a given mount path.
func (c *client) InstallFoxxService(ctx context.Context, zipFile string, options FoxxCreateOptions) error {
req, err := c.conn.NewRequest("POST", "_api/foxx")
if err != nil {
return WithStack(err)
}
req.SetHeader("Content-Type", "application/zip")
req.SetQuery("mount", options.Mount)
bytes, err := os.ReadFile(zipFile)
if err != nil {
return WithStack(err)
}
_, err = req.SetBody(bytes)
if err != nil {
return WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(http.StatusCreated); err != nil {
return WithStack(err)
}
return nil
}
// UninstallFoxxService uninstalls service at a given mount path.
func (c *client) UninstallFoxxService(ctx context.Context, options FoxxDeleteOptions) error {
req, err := c.conn.NewRequest("DELETE", "_api/foxx/service")
if err != nil {
return WithStack(err)
}
req.SetQuery("mount", options.Mount)
req.SetQuery("teardown", strconv.FormatBool(options.Teardown))
resp, err := c.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(http.StatusNoContent); err != nil {
return WithStack(err)
}
return nil
}

@ -0,0 +1,80 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import "context"
// Graph provides access to all edge & vertex collections of a single graph in a database.
type Graph interface {
// Name returns the name of the graph.
Name() string
// Remove removes the entire graph.
// If the graph does not exist, a NotFoundError is returned.
Remove(ctx context.Context) error
// IsSmart returns true of smart is smart. In case of Community Edition it is always false
IsSmart() bool
// IsSatellite returns true of smart is satellite. In case of Community Edition it is always false
IsSatellite() bool
// IsDisjoint return information if graph have isDisjoint flag set to true
IsDisjoint() bool
// GraphEdgeCollections Edge collection functions
GraphEdgeCollections
// GraphVertexCollections Vertex collection functions
GraphVertexCollections
// ID returns the id of the graph.
ID() string
// Key returns the key of the graph.
Key() DocumentID
// Rev returns the revision of the graph.
Rev() string
// EdgeDefinitions returns the edge definitions of the graph.
EdgeDefinitions() []EdgeDefinition
// SmartGraphAttribute returns the attributes of a smart graph if there are any.
SmartGraphAttribute() string
// MinReplicationFactor returns the minimum replication factor for the graph.
MinReplicationFactor() int
// NumberOfShards returns the number of shards for the graph.
NumberOfShards() int
// OrphanCollections returns the orphan collcetions of the graph.
OrphanCollections() []string
// ReplicationFactor returns the current replication factor.
ReplicationFactor() int
// WriteConcern returns the write concern setting of the graph.
WriteConcern() int
}

@ -0,0 +1,67 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import "context"
// GraphEdgeCollections provides access to all edge collections of a single graph in a database.
type GraphEdgeCollections interface {
// EdgeCollection opens a connection to an existing edge-collection within the graph.
// If no edge-collection with given name exists, an NotFoundError is returned.
// Note: When calling Remove on the returned Collection, the collection is removed from the graph. Not from the database.
EdgeCollection(ctx context.Context, name string) (Collection, VertexConstraints, error)
// EdgeCollectionExists returns true if an edge-collection with given name exists within the graph.
EdgeCollectionExists(ctx context.Context, name string) (bool, error)
// EdgeCollections returns all edge collections of this graph
// Note: When calling Remove on any of the returned Collection's, the collection is removed from the graph. Not from the database.
EdgeCollections(ctx context.Context) ([]Collection, []VertexConstraints, error)
// CreateEdgeCollection creates an edge collection in the graph.
// collection: The name of the edge collection to be used.
// constraints.From: contains the names of one or more vertex collections that can contain source vertices.
// constraints.To: contains the names of one or more edge collections that can contain target vertices.
CreateEdgeCollection(ctx context.Context, collection string, constraints VertexConstraints) (Collection, error)
// CreateEdgeCollectionWithOptions creates an edge collection in the graph with additional options
CreateEdgeCollectionWithOptions(ctx context.Context, collection string, constraints VertexConstraints, options CreateEdgeCollectionOptions) (Collection, error)
// SetVertexConstraints modifies the vertex constraints of an existing edge collection in the graph.
SetVertexConstraints(ctx context.Context, collection string, constraints VertexConstraints) error
}
// VertexConstraints limit the vertex collection you can use in an edge.
type VertexConstraints struct {
// From contains names of vertex collection that are allowed to be used in the From part of an edge.
From []string
// To contains names of vertex collection that are allowed to be used in the To part of an edge.
To []string
}
// CreateEdgeCollectionOptions contains optional parameters for creating a new edge collection
type CreateEdgeCollectionOptions struct {
// Satellites contains an array of collection names that will be used to create SatelliteCollections for a Hybrid (Disjoint) SmartGraph (Enterprise Edition only)
// Requires ArangoDB 3.9+
Satellites []string `json:"satellites,omitempty"`
}

@ -0,0 +1,239 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
"path"
)
type graphDefinition struct {
Name string `json:"name"`
IsSmart bool `json:"isSmart"`
IsSatellite bool `json:"isSatellite"`
IsDisjoint bool `json:"isDisjoint,omitempty"`
EdgeDefinitions []EdgeDefinition `json:"edgeDefinitions,omitempty"`
NumberOfShards int `json:"numberOfShards,omitempty"`
OrphanCollections []string `json:"orphanCollections,omitempty"`
// Deprecated: use 'WriteConcern' instead.
MinReplicationFactor int `json:"minReplicationFactor,omitempty"`
WriteConcern int `json:"writeConcern,omitempty"`
// ReplicationFactor is the number of replication factor that is used for every collection within this graph.
// Cannot be modified later.
ReplicationFactor graphReplicationFactor `json:"replicationFactor,omitempty"`
// This field must be set to the attribute that will be used for sharding or smart graphs.
// All vertices are required to have this attribute set. Edges derive the attribute from their connected vertices.
// This requires ArangoDB Enterprise Edition.
SmartGraphAttribute string `json:"smartGraphAttribute,omitempty"`
Initial *string `json:"initial,omitempty"`
InitialCid int `json:"initialCid,omitempty"`
ID string `json:"_id"`
Key DocumentID `json:"_key"`
Rev string `json:"_rev"`
}
type getGraphResponse struct {
Graph graphDefinition `json:"graph"`
ArangoError
}
// EdgeCollection opens a connection to an existing edge-collection within the graph.
// If no edge-collection with given name exists, an NotFoundError is returned.
func (g *graph) EdgeCollection(ctx context.Context, name string) (Collection, VertexConstraints, error) {
req, err := g.conn.NewRequest("GET", g.relPath())
if err != nil {
return nil, VertexConstraints{}, WithStack(err)
}
resp, err := g.conn.Do(ctx, req)
if err != nil {
return nil, VertexConstraints{}, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, VertexConstraints{}, WithStack(err)
}
var data getGraphResponse
if err := resp.ParseBody("", &data); err != nil {
return nil, VertexConstraints{}, WithStack(err)
}
for _, n := range data.Graph.EdgeDefinitions {
if n.Collection == name {
ec, err := newEdgeCollection(name, g)
if err != nil {
return nil, VertexConstraints{}, WithStack(err)
}
constraints := VertexConstraints{
From: n.From,
To: n.To,
}
return ec, constraints, nil
}
}
return nil, VertexConstraints{}, WithStack(newArangoError(404, 0, "not found"))
}
// EdgeCollectionExists returns true if an edge-collection with given name exists within the graph.
func (g *graph) EdgeCollectionExists(ctx context.Context, name string) (bool, error) {
req, err := g.conn.NewRequest("GET", g.relPath())
if err != nil {
return false, WithStack(err)
}
resp, err := g.conn.Do(ctx, req)
if err != nil {
return false, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return false, WithStack(err)
}
var data getGraphResponse
if err := resp.ParseBody("", &data); err != nil {
return false, WithStack(err)
}
for _, n := range data.Graph.EdgeDefinitions {
if n.Collection == name {
return true, nil
}
}
return false, nil
}
// EdgeCollections returns all edge collections of this graph
func (g *graph) EdgeCollections(ctx context.Context) ([]Collection, []VertexConstraints, error) {
req, err := g.conn.NewRequest("GET", g.relPath())
if err != nil {
return nil, nil, WithStack(err)
}
resp, err := g.conn.Do(ctx, req)
if err != nil {
return nil, nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, nil, WithStack(err)
}
var data getGraphResponse
if err := resp.ParseBody("", &data); err != nil {
return nil, nil, WithStack(err)
}
result := make([]Collection, 0, len(data.Graph.EdgeDefinitions))
constraints := make([]VertexConstraints, 0, len(data.Graph.EdgeDefinitions))
for _, n := range data.Graph.EdgeDefinitions {
ec, err := newEdgeCollection(n.Collection, g)
if err != nil {
return nil, nil, WithStack(err)
}
result = append(result, ec)
constraints = append(constraints, VertexConstraints{
From: n.From,
To: n.To,
})
}
return result, constraints, nil
}
// collection: The name of the edge collection to be used.
// from: contains the names of one or more vertex collections that can contain source vertices.
// to: contains the names of one or more edge collections that can contain target vertices.
func (g *graph) CreateEdgeCollection(ctx context.Context, collection string, constraints VertexConstraints) (Collection, error) {
req, err := g.conn.NewRequest("POST", path.Join(g.relPath(), "edge"))
if err != nil {
return nil, WithStack(err)
}
input := EdgeDefinition{
Collection: collection,
From: constraints.From,
To: constraints.To,
}
if _, err := req.SetBody(input); err != nil {
return nil, WithStack(err)
}
resp, err := g.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(201, 202); err != nil {
return nil, WithStack(err)
}
ec, err := newEdgeCollection(collection, g)
if err != nil {
return nil, WithStack(err)
}
return ec, nil
}
// CreateEdgeCollectionWithOptions creates an edge collection in the graph with additional options
func (g *graph) CreateEdgeCollectionWithOptions(ctx context.Context, collection string, constraints VertexConstraints, options CreateEdgeCollectionOptions) (Collection, error) {
req, err := g.conn.NewRequest("POST", path.Join(g.relPath(), "edge"))
if err != nil {
return nil, WithStack(err)
}
input := EdgeDefinition{
Collection: collection,
From: constraints.From,
To: constraints.To,
Options: options,
}
if _, err := req.SetBody(input); err != nil {
return nil, WithStack(err)
}
resp, err := g.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(201, 202); err != nil {
return nil, WithStack(err)
}
ec, err := newEdgeCollection(collection, g)
if err != nil {
return nil, WithStack(err)
}
return ec, nil
}
// SetVertexConstraints modifies the vertex constraints of an existing edge collection in the graph.
func (g *graph) SetVertexConstraints(ctx context.Context, collection string, constraints VertexConstraints) error {
req, err := g.conn.NewRequest("PUT", path.Join(g.relPath(), "edge", collection))
if err != nil {
return WithStack(err)
}
input := EdgeDefinition{
Collection: collection,
From: constraints.From,
To: constraints.To,
}
if _, err := req.SetBody(input); err != nil {
return WithStack(err)
}
resp, err := g.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(201, 202); err != nil {
return WithStack(err)
}
return nil
}

@ -0,0 +1,139 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
"path"
)
// newGraph creates a new Graph implementation.
func newGraph(input graphDefinition, db *database) (Graph, error) {
if input.Name == "" {
return nil, WithStack(InvalidArgumentError{Message: "name is empty"})
}
if db == nil {
return nil, WithStack(InvalidArgumentError{Message: "db is nil"})
}
return &graph{
input: input,
db: db,
conn: db.conn,
}, nil
}
type graph struct {
input graphDefinition
db *database
conn Connection
}
func (g *graph) IsSmart() bool {
return g.input.IsSmart
}
func (g *graph) IsDisjoint() bool {
return g.input.IsDisjoint
}
func (g *graph) IsSatellite() bool {
return g.input.IsSatellite
}
// relPath creates the relative path to this graph (`_db/<db-name>/_api/gharial/<graph-name>`)
func (g *graph) relPath() string {
escapedName := pathEscape(g.Name())
return path.Join(g.db.relPath(), "_api", "gharial", escapedName)
}
// Name returns the name of the graph.
func (g *graph) Name() string {
return g.input.Name
}
// ID returns the id of the graph.
func (g *graph) ID() string {
return g.input.ID
}
// Key returns the key of the graph.
func (g *graph) Key() DocumentID {
return g.input.Key
}
// Key returns the key of the graph.
func (g *graph) Rev() string {
return g.input.Rev
}
// EdgeDefinitions returns the edge definitions of the graph.
func (g *graph) EdgeDefinitions() []EdgeDefinition {
return g.input.EdgeDefinitions
}
// IsSmart returns the isSmart setting of the graph.
func (g *graph) SmartGraphAttribute() string {
return g.input.SmartGraphAttribute
}
// MinReplicationFactor returns the minimum replication factor for the graph.
func (g *graph) MinReplicationFactor() int {
return g.input.MinReplicationFactor
}
// NumberOfShards returns the number of shards for the graph.
func (g *graph) NumberOfShards() int {
return g.input.NumberOfShards
}
// OrphanCollections returns the orphan collcetions of the graph.
func (g *graph) OrphanCollections() []string {
return g.input.OrphanCollections
}
// ReplicationFactor returns the current replication factor.
func (g *graph) ReplicationFactor() int {
return int(g.input.ReplicationFactor)
}
// WriteConcern returns the write concern setting of the graph.
func (g *graph) WriteConcern() int {
return g.input.WriteConcern
}
// Remove removes the entire graph.
// If the graph does not exist, a NotFoundError is returned.
func (g *graph) Remove(ctx context.Context) error {
req, err := g.conn.NewRequest("DELETE", g.relPath())
if err != nil {
return WithStack(err)
}
resp, err := g.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(201, 202); err != nil {
return WithStack(err)
}
return nil
}

@ -0,0 +1,54 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import "context"
// GraphVertexCollections provides access to all vertex collections of a single graph in a database.
type GraphVertexCollections interface {
// VertexCollection opens a connection to an existing vertex-collection within the graph.
// If no vertex-collection with given name exists, an NotFoundError is returned.
// Note: When calling Remove on the returned Collection, the collection is removed from the graph. Not from the database.
VertexCollection(ctx context.Context, name string) (Collection, error)
// VertexCollectionExists returns true if an vertex-collection with given name exists within the graph.
VertexCollectionExists(ctx context.Context, name string) (bool, error)
// VertexCollections returns all vertex collections of this graph
// Note: When calling Remove on any of the returned Collection's, the collection is removed from the graph. Not from the database.
VertexCollections(ctx context.Context) ([]Collection, error)
// CreateVertexCollection creates a vertex collection in the graph.
// collection: The name of the vertex collection to be used.
CreateVertexCollection(ctx context.Context, collection string) (Collection, error)
// CreateVertexCollectionWithOptions creates a vertex collection in the graph
CreateVertexCollectionWithOptions(ctx context.Context, collection string, options CreateVertexCollectionOptions) (Collection, error)
}
// CreateVertexCollectionOptions contains optional parameters for creating a new vertex collection
type CreateVertexCollectionOptions struct {
// Satellites contains an array of collection names that will be used to create SatelliteCollections for a Hybrid (Disjoint) SmartGraph (Enterprise Edition only)
// Requires ArangoDB 3.9+
Satellites []string `json:"satellites,omitempty"`
}

@ -0,0 +1,176 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
"path"
)
type listVertexCollectionResponse struct {
Collections []string `json:"collections,omitempty"`
ArangoError
}
// VertexCollection opens a connection to an existing edge-collection within the graph.
// If no edge-collection with given name exists, an NotFoundError is returned.
func (g *graph) VertexCollection(ctx context.Context, name string) (Collection, error) {
req, err := g.conn.NewRequest("GET", path.Join(g.relPath(), "vertex"))
if err != nil {
return nil, WithStack(err)
}
resp, err := g.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
var data listVertexCollectionResponse
if err := resp.ParseBody("", &data); err != nil {
return nil, WithStack(err)
}
for _, n := range data.Collections {
if n == name {
ec, err := newVertexCollection(name, g)
if err != nil {
return nil, WithStack(err)
}
return ec, nil
}
}
return nil, WithStack(newArangoError(404, 0, "not found"))
}
// VertexCollectionExists returns true if an edge-collection with given name exists within the graph.
func (g *graph) VertexCollectionExists(ctx context.Context, name string) (bool, error) {
req, err := g.conn.NewRequest("GET", path.Join(g.relPath(), "vertex"))
if err != nil {
return false, WithStack(err)
}
resp, err := g.conn.Do(ctx, req)
if err != nil {
return false, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return false, WithStack(err)
}
var data listVertexCollectionResponse
if err := resp.ParseBody("", &data); err != nil {
return false, WithStack(err)
}
for _, n := range data.Collections {
if n == name {
return true, nil
}
}
return false, nil
}
// VertexCollections returns all edge collections of this graph
func (g *graph) VertexCollections(ctx context.Context) ([]Collection, error) {
req, err := g.conn.NewRequest("GET", path.Join(g.relPath(), "vertex"))
if err != nil {
return nil, WithStack(err)
}
resp, err := g.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
var data listVertexCollectionResponse
if err := resp.ParseBody("", &data); err != nil {
return nil, WithStack(err)
}
result := make([]Collection, 0, len(data.Collections))
for _, name := range data.Collections {
ec, err := newVertexCollection(name, g)
if err != nil {
return nil, WithStack(err)
}
result = append(result, ec)
}
return result, nil
}
// collection: The name of the edge collection to be used.
// from: contains the names of one or more vertex collections that can contain source vertices.
// to: contains the names of one or more edge collections that can contain target vertices.
func (g *graph) CreateVertexCollection(ctx context.Context, collection string) (Collection, error) {
req, err := g.conn.NewRequest("POST", path.Join(g.relPath(), "vertex"))
if err != nil {
return nil, WithStack(err)
}
input := struct {
Collection string `json:"collection,omitempty"`
}{
Collection: collection,
}
if _, err := req.SetBody(input); err != nil {
return nil, WithStack(err)
}
resp, err := g.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(201, 202); err != nil {
return nil, WithStack(err)
}
ec, err := newVertexCollection(collection, g)
if err != nil {
return nil, WithStack(err)
}
return ec, nil
}
// CreateVertexCollectionWithOptions creates a vertex collection in the graph
func (g *graph) CreateVertexCollectionWithOptions(ctx context.Context, collection string, options CreateVertexCollectionOptions) (Collection, error) {
req, err := g.conn.NewRequest("POST", path.Join(g.relPath(), "vertex"))
if err != nil {
return nil, WithStack(err)
}
input := struct {
Collection string `json:"collection,omitempty"`
Options CreateVertexCollectionOptions `json:"options,omitempty"`
}{
Collection: collection,
Options: options,
}
if _, err := req.SetBody(input); err != nil {
return nil, WithStack(err)
}
resp, err := g.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(201, 202); err != nil {
return nil, WithStack(err)
}
ec, err := newVertexCollection(collection, g)
if err != nil {
return nil, WithStack(err)
}
return ec, nil
}

@ -0,0 +1,279 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package http
import (
"context"
"encoding/base64"
"errors"
"fmt"
"sync"
"sync/atomic"
driver "github.com/arangodb/go-driver"
)
// ErrAuthenticationNotChanged is returned when authentication is not changed.
var ErrAuthenticationNotChanged = errors.New("authentication not changed")
// Authentication implements a kind of authentication.
type httpAuthentication interface {
// Prepare is called before the first request of the given connection is made.
Prepare(ctx context.Context, conn driver.Connection) error
// Configure is called for every request made on a connection.
Configure(req driver.Request) error
}
// IsAuthenticationTheSame checks whether two authentications are the same.
func IsAuthenticationTheSame(auth1, auth2 driver.Authentication) bool {
if auth1 == nil && auth2 == nil {
return true
}
if auth1 == nil || auth2 == nil {
return false
}
if auth1.Type() != auth2.Type() {
return false
}
if auth1.Type() == driver.AuthenticationTypeRaw {
if auth1.Get("value") != auth2.Get("value") {
return false
}
} else {
if auth1.Get("username") != auth2.Get("username") ||
auth1.Get("password") != auth2.Get("password") {
return false
}
}
return true
}
// newBasicAuthentication creates an authentication implementation based on the given username & password.
func newBasicAuthentication(userName, password string) httpAuthentication {
auth := fmt.Sprintf("%s:%s", userName, password)
encoded := base64.StdEncoding.EncodeToString([]byte(auth))
return &basicAuthentication{
authorizationValue: "Basic " + encoded,
}
}
// newJWTAuthentication creates a JWT token authentication implementation based on the given username & password.
func newJWTAuthentication(userName, password string) httpAuthentication {
return &jwtAuthentication{
userName: userName,
password: password,
}
}
// newRawAuthentication creates a Raw authentication implementation based on the given value.
func newRawAuthentication(value string) httpAuthentication {
return &basicAuthentication{
authorizationValue: value,
}
}
// basicAuthentication implements HTTP Basic authentication.
type basicAuthentication struct {
authorizationValue string
}
// Prepare is called before the first request of the given connection is made.
func (a *basicAuthentication) Prepare(ctx context.Context, conn driver.Connection) error {
// No need to do anything here
return nil
}
// Configure is called for every request made on a connection.
func (a *basicAuthentication) Configure(req driver.Request) error {
req.SetHeader("Authorization", a.authorizationValue)
return nil
}
// jwtAuthentication implements JWT token authentication.
type jwtAuthentication struct {
userName string
password string
token string
}
type jwtOpenRequest struct {
UserName string `json:"username"`
Password string `json:"password"`
}
type jwtOpenResponse struct {
Token string `json:"jwt"`
MustChangePassword bool `json:"must_change_password,omitempty"`
}
// Prepare is called before the first request of the given connection is made.
func (a *jwtAuthentication) Prepare(ctx context.Context, conn driver.Connection) error {
// Prepare request
r, err := conn.NewRequest("POST", "/_open/auth")
if err != nil {
return driver.WithStack(err)
}
r.SetBody(jwtOpenRequest{
UserName: a.userName,
Password: a.password,
})
// Perform request
resp, err := conn.Do(ctx, r)
if err != nil {
return driver.WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return driver.WithStack(err)
}
// Parse response
var data jwtOpenResponse
if err := resp.ParseBody("", &data); err != nil {
return driver.WithStack(err)
}
// Store token
a.token = data.Token
// Ok
return nil
}
// Configure is called for every request made on a connection.
func (a *jwtAuthentication) Configure(req driver.Request) error {
req.SetHeader("Authorization", "bearer "+a.token)
return nil
}
// newAuthenticatedConnection creates a Connection that applies the given connection on the given underlying connection.
func newAuthenticatedConnection(conn driver.Connection, auth httpAuthentication) (driver.Connection, error) {
if conn == nil {
return nil, driver.WithStack(driver.InvalidArgumentError{Message: "conn is nil"})
}
if auth == nil {
return nil, driver.WithStack(driver.InvalidArgumentError{Message: "auth is nil"})
}
return &authenticatedConnection{
conn: conn,
auth: auth,
}, nil
}
// authenticatedConnection implements authentication behavior for connections.
type authenticatedConnection struct {
conn driver.Connection // Un-authenticated connection
auth httpAuthentication
prepareMutex sync.Mutex
prepared int32
}
// NewRequest creates a new request with given method and path.
func (c *authenticatedConnection) NewRequest(method, path string) (driver.Request, error) {
r, err := c.conn.NewRequest(method, path)
if err != nil {
return nil, driver.WithStack(err)
}
return r, nil
}
// Do performs a given request, returning its response.
func (c *authenticatedConnection) Do(ctx context.Context, req driver.Request) (driver.Response, error) {
if atomic.LoadInt32(&c.prepared) == 0 {
// Probably we're not yet prepared
if err := c.prepare(ctx); err != nil {
// Authentication failed
return nil, driver.WithStack(err)
}
}
// Configure the request for authentication.
if err := c.auth.Configure(req); err != nil {
// Failed to configure request for authentication
return nil, driver.WithStack(err)
}
// Do the authenticated request
resp, err := c.conn.Do(ctx, req)
if err != nil {
return nil, driver.WithStack(err)
}
return resp, nil
}
// Unmarshal unmarshals the given raw object into the given result interface.
func (c *authenticatedConnection) Unmarshal(data driver.RawObject, result interface{}) error {
if err := c.conn.Unmarshal(data, result); err != nil {
return driver.WithStack(err)
}
return nil
}
// Endpoints returns the endpoints used by this connection.
func (c *authenticatedConnection) Endpoints() []string {
return c.conn.Endpoints()
}
// UpdateEndpoints reconfigures the connection to use the given endpoints.
func (c *authenticatedConnection) UpdateEndpoints(endpoints []string) error {
if err := c.conn.UpdateEndpoints(endpoints); err != nil {
return driver.WithStack(err)
}
return nil
}
// SetAuthentication creates a copy of connection wrapper for given auth parameters.
func (c *authenticatedConnection) SetAuthentication(auth driver.Authentication) (driver.Connection, error) {
result, err := c.conn.SetAuthentication(auth)
if err != nil {
return nil, driver.WithStack(err)
}
return result, nil
}
// Protocols returns all protocols used by this connection.
func (c *authenticatedConnection) Protocols() driver.ProtocolSet {
return c.conn.Protocols()
}
// prepare calls Authentication.Prepare if needed.
func (c *authenticatedConnection) prepare(ctx context.Context) error {
c.prepareMutex.Lock()
defer c.prepareMutex.Unlock()
if c.prepared == 0 {
// We need to prepare first
if err := c.auth.Prepare(ctx, c.conn); err != nil {
// Authentication failed
return driver.WithStack(err)
}
// We're now prepared
atomic.StoreInt32(&c.prepared, 1)
} else {
// We're already prepared, do nothing
}
return nil
}

@ -0,0 +1,497 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package http
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/http/httptrace"
"net/url"
"strings"
"sync"
"time"
velocypack "github.com/arangodb/go-velocypack"
driver "github.com/arangodb/go-driver"
"github.com/arangodb/go-driver/cluster"
"github.com/arangodb/go-driver/util"
)
const (
DefaultMaxIdleConnsPerHost = 64
DefaultConnLimit = 32
keyRawResponse driver.ContextKey = "arangodb-rawResponse"
keyResponse driver.ContextKey = "arangodb-response"
)
// ConnectionConfig provides all configuration options for a HTTP connection.
type ConnectionConfig struct {
// Endpoints holds 1 or more URL's used to connect to the database.
// In case of a connection to an ArangoDB cluster, you must provide the URL's of all coordinators.
Endpoints []string
// TLSConfig holds settings used to configure a TLS (HTTPS) connection.
// This is only used for endpoints using the HTTPS scheme.
TLSConfig *tls.Config
// Transport allows the use of a custom round tripper.
// If Transport is not of type `*http.Transport`, the `TLSConfig` property is not used.
// Otherwise a `TLSConfig` property other than `nil` will overwrite the `TLSClientConfig`
// property of `Transport`.
//
// When using a custom `http.Transport`, make sure to set the `MaxIdleConnsPerHost` field at least as
// high as the maximum number of concurrent requests you will make to your database.
// A lower number will cause the golang runtime to create additional connections and close them
// directly after use, resulting in a large number of connections in `TIME_WAIT` state.
// When this value is not set, the driver will set it to 64 automatically.
Transport http.RoundTripper
// DontFollowRedirect; if set, redirect will not be followed, response from the initial request will be returned without an error
// DontFollowRedirect takes precendance over FailOnRedirect.
DontFollowRedirect bool
// FailOnRedirect; if set, redirect will not be followed, instead the status code is returned as error
FailOnRedirect bool
// Cluster configuration settings
cluster.ConnectionConfig
// ContentType specified type of content encoding to use.
ContentType driver.ContentType
// ConnLimit is the upper limit to the number of connections to a single server.
// The default is 32 (DefaultConnLimit).
// Set this value to -1 if you do not want any upper limit.
ConnLimit int
}
// NewConnection creates a new HTTP connection based on the given configuration settings.
func NewConnection(config ConnectionConfig) (driver.Connection, error) {
c, err := cluster.NewConnection(config.ConnectionConfig, func(endpoint string) (driver.Connection, error) {
conn, err := newHTTPConnection(endpoint, config)
if err != nil {
return nil, driver.WithStack(err)
}
return conn, nil
}, config.Endpoints)
if err != nil {
return nil, driver.WithStack(err)
}
return c, nil
}
// newHTTPConnection creates a new HTTP connection for a single endpoint and the remainder of the given configuration settings.
func newHTTPConnection(endpoint string, config ConnectionConfig) (driver.Connection, error) {
if config.ConnLimit == 0 {
config.ConnLimit = DefaultConnLimit
}
endpoint = util.FixupEndpointURLScheme(endpoint)
u, err := url.Parse(endpoint)
if err != nil {
return nil, driver.WithStack(err)
}
var httpTransport *http.Transport
if config.Transport != nil {
httpTransport, _ = config.Transport.(*http.Transport)
} else {
httpTransport = &http.Transport{
// Copy default values from http.DefaultTransport
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
config.Transport = httpTransport
}
if httpTransport != nil {
if httpTransport.MaxIdleConnsPerHost == 0 {
// Raise the default number of idle connections per host since in a database application
// it is very likely that you want more than 2 concurrent connections to a host.
// We raise it to avoid the extra concurrent connections being closed directly
// after use, resulting in a lot of connection in `TIME_WAIT` state.
httpTransport.MaxIdleConnsPerHost = DefaultMaxIdleConnsPerHost
}
defaultMaxIdleConns := 3 * DefaultMaxIdleConnsPerHost
if httpTransport.MaxIdleConns > 0 && httpTransport.MaxIdleConns < defaultMaxIdleConns {
// For a cluster scenario we assume the use of 3 coordinators (don't know the exact number here)
// and derive the maximum total number of idle connections from that.
httpTransport.MaxIdleConns = defaultMaxIdleConns
}
if config.TLSConfig != nil {
httpTransport.TLSClientConfig = config.TLSConfig
}
}
httpClient := &http.Client{
Transport: config.Transport,
}
if config.DontFollowRedirect {
httpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse // Do not wrap, standard library will not understand
}
} else if config.FailOnRedirect {
httpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return driver.ArangoError{
HasError: true,
Code: http.StatusFound,
ErrorNum: 0,
ErrorMessage: "Redirect not allowed",
}
}
}
var connPool chan int
if config.ConnLimit > 0 {
connPool = make(chan int, config.ConnLimit)
// Fill with available tokens
for i := 0; i < config.ConnLimit; i++ {
connPool <- i
}
}
c := &httpConnection{
endpoint: *u,
contentType: config.ContentType,
client: httpClient,
connPool: connPool,
}
return c, nil
}
// httpConnection implements an HTTP + JSON connection to an arangodb server.
type httpConnection struct {
endpoint url.URL
contentType driver.ContentType
client *http.Client
connPool chan int
}
// String returns the endpoint as string
func (c *httpConnection) String() string {
return c.endpoint.String()
}
// NewRequest creates a new request with given method and path.
func (c *httpConnection) NewRequest(method, path string) (driver.Request, error) {
switch method {
case "GET", "POST", "DELETE", "HEAD", "PATCH", "PUT", "OPTIONS":
// Ok
default:
return nil, driver.WithStack(driver.InvalidArgumentError{Message: fmt.Sprintf("Invalid method '%s'", method)})
}
ct := c.contentType
if ct != driver.ContentTypeJSON && strings.Contains(path, "_api/gharial") {
// Currently (3.1.18) calls to this API do not work well with vpack.
ct = driver.ContentTypeJSON
}
r := &httpRequest{
method: method,
path: path,
}
switch ct {
case driver.ContentTypeJSON:
r.bodyBuilder = NewJsonBodyBuilder()
return r, nil
case driver.ContentTypeVelocypack:
r.bodyBuilder = NewVelocyPackBodyBuilder()
r.velocyPack = true
return r, nil
default:
return nil, driver.WithStack(fmt.Errorf("Unsupported content type %d", int(c.contentType)))
}
}
// Do performs a given request, returning its response.
func (c *httpConnection) Do(ctx context.Context, req driver.Request) (driver.Response, error) {
request, ok := req.(*httpRequest)
if !ok {
return nil, driver.WithStack(driver.InvalidArgumentError{Message: "request is not a httpRequest type"})
}
r, err := request.createHTTPRequest(c.endpoint)
rctx := ctx
if rctx == nil {
rctx = context.Background()
}
rctx = httptrace.WithClientTrace(rctx, &httptrace.ClientTrace{
WroteRequest: func(info httptrace.WroteRequestInfo) {
request.WroteRequest(info)
},
})
r = r.WithContext(rctx)
if err != nil {
return nil, driver.WithStack(err)
}
// Block on too many concurrent connections
if c.connPool != nil {
select {
case t := <-c.connPool:
// Ok, we're allowed to continue
defer func() {
// Give back token
c.connPool <- t
}()
case <-rctx.Done():
// Context cancelled or expired
return nil, driver.WithStack(rctx.Err())
}
}
resp, err := c.client.Do(r)
if err != nil {
return nil, driver.WithStack(err)
}
var rawResponse *[]byte
useRawResponse := false
if ctx != nil {
if v := ctx.Value(keyRawResponse); v != nil {
useRawResponse = true
if buf, ok := v.(*[]byte); ok {
rawResponse = buf
}
}
}
// Read response body
body, err := readBody(resp)
if err != nil {
return nil, driver.WithStack(err)
}
if rawResponse != nil {
*rawResponse = body
}
ct := resp.Header.Get("Content-Type")
var httpResp driver.Response
switch strings.Split(ct, ";")[0] {
case "application/json", "application/x-arango-dump":
httpResp = &httpJSONResponse{resp: resp, rawResponse: body}
case "application/x-velocypack":
httpResp = &httpVPackResponse{resp: resp, rawResponse: body}
default:
if resp.StatusCode == http.StatusUnauthorized {
// When unauthorized the server sometimes return a `text/plain` response.
return nil, driver.WithStack(driver.ArangoError{
HasError: true,
Code: resp.StatusCode,
ErrorMessage: string(body),
})
}
// Handle empty 'text/plain' body as empty JSON object
if len(body) == 0 {
body = []byte("{}")
if rawResponse != nil {
*rawResponse = body
}
httpResp = &httpJSONResponse{resp: resp, rawResponse: body}
} else if useRawResponse {
httpResp = &httpJSONResponse{resp: resp, rawResponse: body}
} else {
return nil, driver.WithStack(fmt.Errorf("Unsupported content type '%s' with status %d and content '%s'", ct, resp.StatusCode, string(body)))
}
}
if ctx != nil {
if v := ctx.Value(keyResponse); v != nil {
if respPtr, ok := v.(*driver.Response); ok {
*respPtr = httpResp
}
}
}
return httpResp, nil
}
// readBody reads the body of the given response into a byte slice.
func readBody(resp *http.Response) ([]byte, error) {
defer resp.Body.Close()
contentLength := resp.ContentLength
if contentLength < 0 {
// Don't know the content length, do it the slowest way
result, err := io.ReadAll(resp.Body)
if err != nil {
return nil, driver.WithStack(err)
}
return result, nil
}
buf := &bytes.Buffer{}
if int64(int(contentLength)) == contentLength {
// contentLength is an int64. If we can safely cast to int, use Grow.
buf.Grow(int(contentLength))
}
if _, err := buf.ReadFrom(resp.Body); err != nil {
return nil, driver.WithStack(err)
}
return buf.Bytes(), nil
}
// Unmarshal unmarshals the given raw object into the given result interface.
func (c *httpConnection) Unmarshal(data driver.RawObject, result interface{}) error {
ct := c.contentType
if ct == driver.ContentTypeVelocypack && len(data) >= 2 {
// Poor mans auto detection of json
l := len(data)
if (data[0] == '{' && data[l-1] == '}') || (data[0] == '[' && data[l-1] == ']') {
ct = driver.ContentTypeJSON
}
}
switch ct {
case driver.ContentTypeJSON:
if err := json.Unmarshal(data, result); err != nil {
return driver.WithStack(err)
}
case driver.ContentTypeVelocypack:
//panic(velocypack.Slice(data))
if err := velocypack.Unmarshal(velocypack.Slice(data), result); err != nil {
return driver.WithStack(err)
}
default:
return driver.WithStack(fmt.Errorf("Unsupported content type %d", int(c.contentType)))
}
return nil
}
// Endpoints returns the endpoints used by this connection.
func (c *httpConnection) Endpoints() []string {
return []string{c.endpoint.String()}
}
// UpdateEndpoints reconfigures the connection to use the given endpoints.
func (c *httpConnection) UpdateEndpoints(endpoints []string) error {
// Do nothing here.
// The real updating is done in cluster Connection.
return nil
}
// SetAuthentication creates a copy of connection wrapper for given auth parameters.
func (c *httpConnection) SetAuthentication(auth driver.Authentication) (driver.Connection, error) {
var httpAuth httpAuthentication
switch auth.Type() {
case driver.AuthenticationTypeBasic:
userName := auth.Get("username")
password := auth.Get("password")
httpAuth = newBasicAuthentication(userName, password)
case driver.AuthenticationTypeJWT:
userName := auth.Get("username")
password := auth.Get("password")
httpAuth = newJWTAuthentication(userName, password)
case driver.AuthenticationTypeRaw:
value := auth.Get("value")
httpAuth = newRawAuthentication(value)
default:
return nil, driver.WithStack(fmt.Errorf("Unsupported authentication type %d", int(auth.Type())))
}
result, err := newAuthenticatedConnection(c, httpAuth)
if err != nil {
return nil, driver.WithStack(err)
}
return result, nil
}
// Protocols returns all protocols used by this connection.
func (c *httpConnection) Protocols() driver.ProtocolSet {
return driver.ProtocolSet{driver.ProtocolHTTP}
}
// RequestRepeater creates possibility to send the request many times.
type RequestRepeater interface {
Repeat(conn driver.Connection, resp driver.Response, err error) bool
}
// RepeatConnection is responsible for sending request until request repeater gives up.
type RepeatConnection struct {
mutex sync.Mutex
auth driver.Authentication
conn driver.Connection
repeat RequestRepeater
}
func NewRepeatConnection(conn driver.Connection, repeat RequestRepeater) driver.Connection {
return &RepeatConnection{
conn: conn,
repeat: repeat,
}
}
// NewRequest creates a new request with given method and path.
func (h *RepeatConnection) NewRequest(method, path string) (driver.Request, error) {
return h.conn.NewRequest(method, path)
}
// Do performs a given request, returning its response. Repeats requests until repeat function gives up.
func (h *RepeatConnection) Do(ctx context.Context, req driver.Request) (driver.Response, error) {
for {
resp, err := h.conn.Do(ctx, req.Clone())
if !h.repeat.Repeat(h, resp, err) {
return resp, err
}
}
}
// Unmarshal unmarshals the given raw object into the given result interface.
func (h *RepeatConnection) Unmarshal(data driver.RawObject, result interface{}) error {
return h.conn.Unmarshal(data, result)
}
// Endpoints returns the endpoints used by this connection.
func (h *RepeatConnection) Endpoints() []string {
return h.conn.Endpoints()
}
// UpdateEndpoints reconfigures the connection to use the given endpoints.
func (h *RepeatConnection) UpdateEndpoints(endpoints []string) error {
return h.conn.UpdateEndpoints(endpoints)
}
// SetAuthentication configure the authentication used for this connection.
// Returns ErrAuthenticationNotChanged when the authentication is not changed.
func (h *RepeatConnection) SetAuthentication(authentication driver.Authentication) (driver.Connection, error) {
h.mutex.Lock()
defer h.mutex.Unlock()
if IsAuthenticationTheSame(h.auth, authentication) {
return h, ErrAuthenticationNotChanged
}
newConn, err := h.conn.SetAuthentication(authentication)
if err != nil {
return nil, driver.WithStack(err)
}
h.conn = newConn
h.auth = authentication
return h, nil
}
// Protocols returns all protocols used by this connection.
func (h *RepeatConnection) Protocols() driver.ProtocolSet {
return h.conn.Protocols()
}

@ -0,0 +1,111 @@
//
// DISCLAIMER
//
// Copyright 2020 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 http
import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
"github.com/arangodb/go-velocypack"
"github.com/arangodb/go-driver"
)
type connectionDebugWrapper struct {
driver.Connection
ct driver.ContentType
}
func NewConnectionDebugWrapper(conn driver.Connection, ct driver.ContentType) driver.Connection {
return &connectionDebugWrapper{conn, ct}
}
func (c *connectionDebugWrapper) Do(ctx context.Context, req driver.Request) (driver.Response, error) {
if c.ct == driver.ContentTypeJSON {
resp, err := c.Connection.Do(ctx, req)
if err != nil {
return resp, err
}
httpResponse, ok := resp.(*httpJSONResponse)
if !ok {
panic("can not cast response to the httpJSONResponse type!")
}
return &responseDebugWrapper{httpResponse}, err
}
return c.Connection.Do(ctx, req)
}
func (c *connectionDebugWrapper) Unmarshal(data driver.RawObject, result interface{}) error {
ct := c.ct
if ct == driver.ContentTypeVelocypack && len(data) >= 2 {
// Poor mans auto detection of json
l := len(data)
if (data[0] == '{' && data[l-1] == '}') || (data[0] == '[' && data[l-1] == ']') {
ct = driver.ContentTypeJSON
}
}
switch ct {
case driver.ContentTypeJSON:
decoder := json.NewDecoder(strings.NewReader(string(data)))
decoder.DisallowUnknownFields()
if err := decoder.Decode(result); err != nil {
return driver.WithStack(err)
}
if err := json.Unmarshal(data, result); err != nil {
fmt.Printf("Struct: %s \n", reflect.TypeOf(result).String())
fmt.Printf("Response: %s \n\n", string(data))
return driver.WithStack(errors.New(fmt.Sprintf("Struct: %s, Error: %s", reflect.TypeOf(result).String(), err.Error())))
}
case driver.ContentTypeVelocypack:
if err := velocypack.Unmarshal(velocypack.Slice(data), result); err != nil {
return driver.WithStack(err)
}
default:
return driver.WithStack(fmt.Errorf("unsupported content type %d", int(c.ct)))
}
return nil
}
type responseDebugWrapper struct {
*httpJSONResponse
}
func (r *responseDebugWrapper) ParseBody(field string, result interface{}) error {
if field == "" {
decoder := json.NewDecoder(strings.NewReader(string(r.httpJSONResponse.rawResponse)))
decoder.DisallowUnknownFields()
if err := decoder.Decode(result); err != nil {
fmt.Printf("Struct: %s \n", reflect.TypeOf(result).String())
fmt.Printf("Response: %s \n\n", string(r.httpJSONResponse.rawResponse))
return driver.WithStack(errors.New(fmt.Sprintf("Struct: %s, Error: %s", reflect.TypeOf(result).String(), err.Error())))
}
}
return r.httpJSONResponse.ParseBody(field, result)
}

@ -0,0 +1,68 @@
//
// DISCLAIMER
//
// Copyright 2018 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
//
// Author Ewout Prangsma
//
/*
Package http implements driver.Connection using an HTTP connection.
This connection uses HTTP or HTTPS to connect to the ArangoDB database and
encodes its content as JSON or Velocypack, depending on the value
of the `ContentType` fields in the `http.ConnectionConfig`.
# Creating an Insecure Connection
To create an HTTP connection, use code like this.
// Create an HTTP connection to the database
conn, err := http.NewConnection(http.ConnectionConfig{
Endpoints: []string{"http://localhost:8529"},
})
if err != nil {
// Handle error
}
The resulting connection is used to create a client which you will use
for normal database requests.
// Create a client
c, err := driver.NewClient(driver.ClientConfig{
Connection: conn,
})
if err != nil {
// Handle error
}
# Creating a Secure Connection
To create a secure HTTPS connection, use code like this.
// Create an HTTPS connection to the database
conn, err := http.NewConnection(http.ConnectionConfig{
Endpoints: []string{"https://localhost:8529"},
TLSConfig: &tls.Config{
InsecureSkipVerify: trueWhenUsingNonPublicCertificates,
},
})
if err != nil {
// Handle error
}
*/
package http

@ -0,0 +1,84 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package http
import (
"encoding/json"
driver "github.com/arangodb/go-driver"
)
// mergeObject is a helper used to merge 2 objects into JSON.
type mergeObject struct {
Object interface{}
Merge interface{}
}
func (m mergeObject) MarshalJSON() ([]byte, error) {
m1, err := toMap(m.Object)
if err != nil {
return nil, driver.WithStack(err)
}
m2, err := toMap(m.Merge)
if err != nil {
return nil, driver.WithStack(err)
}
var merged map[string]interface{}
// If m1 an empty object?
if len(m1) == 0 {
merged = m2
} else if len(m2) == 0 {
merged = m1
} else {
// Merge
merged = make(map[string]interface{})
for k, v := range m1 {
merged[k] = v
}
for k, v := range m2 {
merged[k] = v
}
}
// Marshal merged map
data, err := json.Marshal(merged)
if err != nil {
return nil, driver.WithStack(err)
}
return data, nil
}
// toMap converts the given object to a map (using JSON marshal/unmarshal when needed)
func toMap(object interface{}) (map[string]interface{}, error) {
if m, ok := object.(map[string]interface{}); ok {
return m, nil
}
data, err := json.Marshal(object)
if err != nil {
return nil, driver.WithStack(err)
}
var m map[string]interface{}
if err := json.Unmarshal(data, &m); err != nil {
return nil, driver.WithStack(err)
}
return m, nil
}

@ -0,0 +1,369 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package http
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptrace"
"net/url"
"reflect"
"strconv"
"strings"
driver "github.com/arangodb/go-driver"
)
// httpRequest implements driver.Request using standard golang http requests.
type httpRequest struct {
method string
path string
q url.Values
hdr map[string]string
written bool
bodyBuilder driver.BodyBuilder
velocyPack bool
}
// Path returns the Request path
func (r *httpRequest) Path() string {
return r.path
}
// Method returns the Request method
func (r *httpRequest) Method() string {
return r.method
}
// Clone creates a new request containing the same data as this request
func (r *httpRequest) Clone() driver.Request {
clone := *r
clone.q = url.Values{}
for k, v := range r.q {
for _, x := range v {
clone.q.Add(k, x)
}
}
if clone.hdr != nil {
clone.hdr = make(map[string]string)
for k, v := range r.hdr {
clone.hdr[k] = v
}
}
clone.bodyBuilder = r.bodyBuilder.Clone()
return &clone
}
// SetQuery sets a single query argument of the request.
// Any existing query argument with the same key is overwritten.
func (r *httpRequest) SetQuery(key, value string) driver.Request {
if r.q == nil {
r.q = url.Values{}
}
r.q.Set(key, value)
return r
}
// SetBody sets the content of the request.
// The protocol of the connection determines what kinds of marshalling is taking place.
func (r *httpRequest) SetBody(body ...interface{}) (driver.Request, error) {
return r, r.bodyBuilder.SetBody(body...)
}
// SetBodyArray sets the content of the request as an array.
// If the given mergeArray is not nil, its elements are merged with the elements in the body array (mergeArray data overrides bodyArray data).
// The protocol of the connection determines what kinds of marshalling is taking place.
func (r *httpRequest) SetBodyArray(bodyArray interface{}, mergeArray []map[string]interface{}) (driver.Request, error) {
return r, r.bodyBuilder.SetBodyArray(bodyArray, mergeArray)
}
// SetBodyImportArray sets the content of the request as an array formatted for importing documents.
// The protocol of the connection determines what kinds of marshalling is taking place.
func (r *httpRequest) SetBodyImportArray(bodyArray interface{}) (driver.Request, error) {
err := r.bodyBuilder.SetBodyImportArray(bodyArray)
if err == nil {
if r.velocyPack {
r.SetQuery("type", "list")
}
}
return r, err
}
func isNil(v reflect.Value) bool {
switch v.Kind() {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Slice:
return v.IsNil()
default:
return false
}
}
// SetHeader sets a single header arguments of the request.
// Any existing header argument with the same key is overwritten.
func (r *httpRequest) SetHeader(key, value string) driver.Request {
if r.hdr == nil {
r.hdr = make(map[string]string)
}
if strings.EqualFold(key, "Content-Type") {
switch strings.ToLower(value) {
case "application/octet-stream":
case "application/zip":
r.bodyBuilder = NewBinaryBodyBuilder(strings.ToLower(value))
}
}
r.hdr[key] = value
return r
}
// Written returns true as soon as this request has been written completely to the network.
// This does not guarantee that the server has received or processed the request.
func (r *httpRequest) Written() bool {
return r.written
}
// WroteRequest implements the WroteRequest function of an httptrace.
// It sets written to true.
func (r *httpRequest) WroteRequest(httptrace.WroteRequestInfo) {
r.written = true
}
// createHTTPRequest creates a golang http.Request based on the configured arguments.
func (r *httpRequest) createHTTPRequest(endpoint url.URL) (*http.Request, error) {
r.written = false
u := endpoint
u.Path = ""
url := u.String()
if !strings.HasSuffix(url, "/") {
url = url + "/"
}
p := r.path
if strings.HasPrefix(p, "/") {
p = p[1:]
}
url = url + p
if r.q != nil {
q := r.q.Encode()
if len(q) > 0 {
url = url + "?" + q
}
}
var bodyReader io.Reader
body := r.bodyBuilder.GetBody()
if body != nil {
bodyReader = bytes.NewReader(body)
}
req, err := http.NewRequest(r.method, url, bodyReader)
if err != nil {
return nil, driver.WithStack(err)
}
if r.hdr != nil {
for k, v := range r.hdr {
req.Header.Set(k, v)
}
}
if r.velocyPack {
req.Header.Set("Accept", "application/x-velocypack")
}
if body != nil {
req.Header.Set("Content-Length", strconv.Itoa(len(body)))
req.Header.Set("Content-Type", r.bodyBuilder.GetContentType())
}
return req, nil
}
type jsonBody struct {
body []byte
}
func NewJsonBodyBuilder() *jsonBody {
return &jsonBody{}
}
// SetBody sets the content of the request.
// The protocol of the connection determines what kinds of marshalling is taking place.
func (b *jsonBody) SetBody(body ...interface{}) error {
switch len(body) {
case 0:
return driver.WithStack(fmt.Errorf("Must provide at least 1 body"))
case 1:
if data, err := json.Marshal(body[0]); err != nil {
return driver.WithStack(err)
} else {
b.body = data
}
return nil
case 2:
mo := mergeObject{Object: body[1], Merge: body[0]}
if data, err := json.Marshal(mo); err != nil {
return driver.WithStack(err)
} else {
b.body = data
}
return nil
default:
return driver.WithStack(fmt.Errorf("Must provide at most 2 bodies"))
}
}
// SetBodyArray sets the content of the request as an array.
// If the given mergeArray is not nil, its elements are merged with the elements in the body array (mergeArray data overrides bodyArray data).
// The protocol of the connection determines what kinds of marshalling is taking place.
func (b *jsonBody) SetBodyArray(bodyArray interface{}, mergeArray []map[string]interface{}) error {
bodyArrayVal := reflect.ValueOf(bodyArray)
switch bodyArrayVal.Kind() {
case reflect.Array, reflect.Slice:
// OK
default:
return driver.WithStack(driver.InvalidArgumentError{Message: fmt.Sprintf("bodyArray must be slice, got %s", bodyArrayVal.Kind())})
}
if mergeArray == nil {
// Simple case; just marshal bodyArray directly.
if data, err := json.Marshal(bodyArray); err != nil {
return driver.WithStack(err)
} else {
b.body = data
}
return nil
}
// Complex case, mergeArray is not nil
elementCount := bodyArrayVal.Len()
mergeObjects := make([]mergeObject, elementCount)
for i := 0; i < elementCount; i++ {
mergeObjects[i] = mergeObject{
Object: bodyArrayVal.Index(i).Interface(),
Merge: mergeArray[i],
}
}
// Now marshal merged array
if data, err := json.Marshal(mergeObjects); err != nil {
return driver.WithStack(err)
} else {
b.body = data
}
return nil
}
// SetBodyImportArray sets the content of the request as an array formatted for importing documents.
// The protocol of the connection determines what kinds of marshalling is taking place.
func (b *jsonBody) SetBodyImportArray(bodyArray interface{}) error {
bodyArrayVal := reflect.ValueOf(bodyArray)
switch bodyArrayVal.Kind() {
case reflect.Array, reflect.Slice:
// OK
default:
return driver.WithStack(driver.InvalidArgumentError{Message: fmt.Sprintf("bodyArray must be slice, got %s", bodyArrayVal.Kind())})
}
// Render elements
elementCount := bodyArrayVal.Len()
buf := &bytes.Buffer{}
encoder := json.NewEncoder(buf)
for i := 0; i < elementCount; i++ {
entryVal := bodyArrayVal.Index(i)
if isNil(entryVal) {
buf.WriteString("\n")
} else {
if err := encoder.Encode(entryVal.Interface()); err != nil {
return driver.WithStack(err)
}
}
}
b.body = buf.Bytes()
return nil
}
func (b *jsonBody) GetBody() []byte {
return b.body
}
func (b *jsonBody) GetContentType() string {
return "application/json"
}
func (b *jsonBody) Clone() driver.BodyBuilder {
return &jsonBody{
body: b.GetBody(),
}
}
type binaryBody struct {
body []byte
contentType string
}
func NewBinaryBodyBuilder(contentType string) *binaryBody {
b := binaryBody{
contentType: contentType,
}
return &b
}
// SetBody sets the content of the request.
// The protocol of the connection determines what kinds of marshalling is taking place.
func (b *binaryBody) SetBody(body ...interface{}) error {
if len(body) == 0 {
return driver.WithStack(fmt.Errorf("must provide at least 1 body"))
}
if data, ok := body[0].([]byte); ok {
b.body = data
return nil
}
return driver.WithStack(fmt.Errorf("must provide body as a []byte type"))
}
func (b *binaryBody) SetBodyArray(_ interface{}, _ []map[string]interface{}) error {
return nil
}
func (b *binaryBody) SetBodyImportArray(_ interface{}) error {
return nil
}
func (b *binaryBody) GetBody() []byte {
return b.body
}
func (b *binaryBody) GetContentType() string {
return b.contentType
}
func (b *binaryBody) Clone() driver.BodyBuilder {
return &binaryBody{
body: b.GetBody(),
contentType: b.GetContentType(),
}
}

@ -0,0 +1,144 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package http
import (
"bytes"
"fmt"
"reflect"
velocypack "github.com/arangodb/go-velocypack"
driver "github.com/arangodb/go-driver"
)
type velocyPackBody struct {
body []byte
}
func NewVelocyPackBodyBuilder() *velocyPackBody {
return &velocyPackBody{}
}
// SetBody sets the content of the request.
// The protocol of the connection determines what kinds of marshalling is taking place.
func (b *velocyPackBody) SetBody(body ...interface{}) error {
switch len(body) {
case 0:
return driver.WithStack(fmt.Errorf("Must provide at least 1 body"))
case 1:
if data, err := velocypack.Marshal(body[0]); err != nil {
return driver.WithStack(err)
} else {
b.body = data
}
return nil
case 2:
mo := mergeObject{Object: body[1], Merge: body[0]}
if data, err := velocypack.Marshal(mo); err != nil {
return driver.WithStack(err)
} else {
b.body = data
}
return nil
default:
return driver.WithStack(fmt.Errorf("Must provide at most 2 bodies"))
}
return nil
}
// SetBodyArray sets the content of the request as an array.
// If the given mergeArray is not nil, its elements are merged with the elements in the body array (mergeArray data overrides bodyArray data).
// The protocol of the connection determines what kinds of marshalling is taking place.
func (b *velocyPackBody) SetBodyArray(bodyArray interface{}, mergeArray []map[string]interface{}) error {
bodyArrayVal := reflect.ValueOf(bodyArray)
switch bodyArrayVal.Kind() {
case reflect.Array, reflect.Slice:
// OK
default:
return driver.WithStack(driver.InvalidArgumentError{Message: fmt.Sprintf("bodyArray must be slice, got %s", bodyArrayVal.Kind())})
}
if mergeArray == nil {
// Simple case; just marshal bodyArray directly.
if data, err := velocypack.Marshal(bodyArray); err != nil {
return driver.WithStack(err)
} else {
b.body = data
}
return nil
}
// Complex case, mergeArray is not nil
elementCount := bodyArrayVal.Len()
mergeObjects := make([]mergeObject, elementCount)
for i := 0; i < elementCount; i++ {
mergeObjects[i] = mergeObject{
Object: bodyArrayVal.Index(i).Interface(),
Merge: mergeArray[i],
}
}
// Now marshal merged array
if data, err := velocypack.Marshal(mergeObjects); err != nil {
return driver.WithStack(err)
} else {
b.body = data
}
return nil
}
// SetBodyImportArray sets the content of the request as an array formatted for importing documents.
// The protocol of the connection determines what kinds of marshalling is taking place.
func (b *velocyPackBody) SetBodyImportArray(bodyArray interface{}) error {
bodyArrayVal := reflect.ValueOf(bodyArray)
switch bodyArrayVal.Kind() {
case reflect.Array, reflect.Slice:
// OK
default:
return driver.WithStack(driver.InvalidArgumentError{Message: fmt.Sprintf("bodyArray must be slice, got %s", bodyArrayVal.Kind())})
}
// Render elements
buf := &bytes.Buffer{}
encoder := velocypack.NewEncoder(buf)
if err := encoder.Encode(bodyArray); err != nil {
return driver.WithStack(err)
}
b.body = buf.Bytes()
return nil
}
func (b *velocyPackBody) GetBody() []byte {
return b.body
}
func (b *velocyPackBody) GetContentType() string {
return "application/x-velocypack"
}
func (b *velocyPackBody) Clone() driver.BodyBuilder {
return &velocyPackBody{
body: b.GetBody(),
}
}

@ -0,0 +1,206 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package http
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"strings"
driver "github.com/arangodb/go-driver"
)
// httpJSONResponse implements driver.Response for standard golang JSON encoded http responses.
type httpJSONResponse struct {
resp *http.Response
rawResponse []byte
bodyObject map[string]*json.RawMessage
bodyArray []map[string]*json.RawMessage
}
// StatusCode returns an HTTP compatible status code of the response.
func (r *httpJSONResponse) StatusCode() int {
return r.resp.StatusCode
}
// Endpoint returns the endpoint that handled the request.
func (r *httpJSONResponse) Endpoint() string {
u := *r.resp.Request.URL
u.Path = ""
u.RawQuery = ""
return u.String()
}
// CheckStatus checks if the status of the response equals to one of the given status codes.
// If so, nil is returned.
// If not, an attempt is made to parse an error response in the body and an error is returned.
func (r *httpJSONResponse) CheckStatus(validStatusCodes ...int) error {
for _, x := range validStatusCodes {
if x == r.resp.StatusCode {
// Found valid status code
return nil
}
}
// Invalid status code, try to parse arango error response.
var aerr driver.ArangoError
if err := r.ParseBody("", &aerr); err == nil && aerr.HasError {
// Found correct arango error.
return aerr
}
// We do not have a valid error code, so we can only create one based on the HTTP status code.
return driver.ArangoError{
HasError: true,
Code: r.resp.StatusCode,
ErrorMessage: fmt.Sprintf("Unexpected status code %d", r.resp.StatusCode),
}
}
// Header returns the value of a response header with given key.
// If no such header is found, an empty string is returned.
func (r *httpJSONResponse) Header(key string) string {
return r.resp.Header.Get(key)
}
// ParseBody performs protocol specific unmarshalling of the response data into the given result.
// If the given field is non-empty, the contents of that field will be parsed into the given result.
func (r *httpJSONResponse) ParseBody(field string, result interface{}) error {
if r.bodyObject == nil {
bodyMap := make(map[string]*json.RawMessage)
if err := json.Unmarshal(r.rawResponse, &bodyMap); err != nil {
return driver.WithStack(err)
}
r.bodyObject = bodyMap
}
if result != nil {
if err := parseBody(r.bodyObject, field, result); err != nil {
return driver.WithStack(err)
}
}
return nil
}
// ParseArrayBody performs protocol specific unmarshalling of the response array data into individual response objects.
// This can only be used for requests that return an array of objects.
func (r *httpJSONResponse) ParseArrayBody() ([]driver.Response, error) {
if r.bodyArray == nil {
var bodyArray []map[string]*json.RawMessage
if err := json.Unmarshal(r.rawResponse, &bodyArray); err != nil {
return nil, driver.WithStack(err)
}
r.bodyArray = bodyArray
}
resps := make([]driver.Response, len(r.bodyArray))
for i, x := range r.bodyArray {
resps[i] = &httpJSONResponseElement{bodyObject: x}
}
return resps, nil
}
func parseBody(bodyObject map[string]*json.RawMessage, field string, result interface{}) error {
if field != "" {
// Unmarshal only a specific field
raw, ok := bodyObject[field]
if !ok || raw == nil {
// Field not found, silently ignored
return nil
}
// Unmarshal field
if err := json.Unmarshal(*raw, result); err != nil {
return driver.WithStack(err)
}
return nil
}
// Unmarshal entire body
rv := reflect.ValueOf(result)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return &json.InvalidUnmarshalError{Type: reflect.TypeOf(result)}
}
objValue := rv.Elem()
switch objValue.Kind() {
case reflect.Struct:
if err := decodeObjectFields(objValue, bodyObject); err != nil {
return driver.WithStack(err)
}
case reflect.Map:
if err := decodeMapFields(objValue, bodyObject); err != nil {
return driver.WithStack(err)
}
default:
return &json.InvalidUnmarshalError{Type: reflect.TypeOf(result)}
}
return nil
}
// decodeObjectFields decodes fields from the given body into a objValue of kind struct.
func decodeObjectFields(objValue reflect.Value, body map[string]*json.RawMessage) error {
objValueType := objValue.Type()
for i := 0; i != objValue.NumField(); i++ {
f := objValueType.Field(i)
if f.Anonymous && f.Type.Kind() == reflect.Struct {
// Recurse into fields of anonymous field
if err := decodeObjectFields(objValue.Field(i), body); err != nil {
return driver.WithStack(err)
}
} else {
// Decode individual field
jsonName := strings.Split(f.Tag.Get("json"), ",")[0]
if jsonName == "" {
jsonName = f.Name
} else if jsonName == "-" {
continue
}
raw, ok := body[jsonName]
if ok && raw != nil {
field := objValue.Field(i)
if err := json.Unmarshal(*raw, field.Addr().Interface()); err != nil {
return driver.WithStack(err)
}
}
}
}
return nil
}
// decodeMapFields decodes fields from the given body into a mapValue of kind map.
func decodeMapFields(val reflect.Value, body map[string]*json.RawMessage) error {
mapVal := val
if mapVal.IsNil() {
valType := val.Type()
mapType := reflect.MapOf(valType.Key(), valType.Elem())
mapVal = reflect.MakeMap(mapType)
}
for jsonName, raw := range body {
var value interface{}
if raw != nil {
if err := json.Unmarshal(*raw, &value); err != nil {
return driver.WithStack(err)
}
}
mapVal.SetMapIndex(reflect.ValueOf(jsonName), reflect.ValueOf(value))
}
val.Set(mapVal)
return nil
}

@ -0,0 +1,113 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package http
import (
"encoding/json"
"fmt"
driver "github.com/arangodb/go-driver"
)
// httpJSONResponseElement implements driver.Response for an entry of an array response.
type httpJSONResponseElement struct {
statusCode *int
bodyObject map[string]*json.RawMessage
}
// StatusCode returns an HTTP compatible status code of the response.
func (r *httpJSONResponseElement) StatusCode() int {
if r.statusCode == nil {
statusCode := 200
// Look for "error" field
if errorFieldJSON, found := r.bodyObject["error"]; found {
var hasError bool
if err := json.Unmarshal(*errorFieldJSON, &hasError); err == nil && hasError {
// We have an error, look for code field
statusCode = 500
if codeFieldJSON, found := r.bodyObject["code"]; found {
var code int
if err := json.Unmarshal(*codeFieldJSON, &code); err == nil {
statusCode = code
}
}
}
}
r.statusCode = &statusCode
}
return *r.statusCode
}
// Endpoint returns the endpoint that handled the request.
func (r *httpJSONResponseElement) Endpoint() string {
return ""
}
// CheckStatus checks if the status of the response equals to one of the given status codes.
// If so, nil is returned.
// If not, an attempt is made to parse an error response in the body and an error is returned.
func (r *httpJSONResponseElement) CheckStatus(validStatusCodes ...int) error {
statusCode := r.StatusCode()
for _, x := range validStatusCodes {
if x == statusCode {
// Found valid status code
return nil
}
}
// Invalid status code, try to parse arango error response.
var aerr driver.ArangoError
if err := r.ParseBody("", &aerr); err == nil && aerr.HasError {
// Found correct arango error.
return aerr
}
// We do not have a valid error code, so we can only create one based on the HTTP status code.
return driver.ArangoError{
HasError: true,
Code: statusCode,
ErrorMessage: fmt.Sprintf("Unexpected status code %d", statusCode),
}
}
// Header returns the value of a response header with given key.
// If no such header is found, an empty string is returned.
func (r *httpJSONResponseElement) Header(key string) string {
return ""
}
// ParseBody performs protocol specific unmarshalling of the response data into the given result.
// If the given field is non-empty, the contents of that field will be parsed into the given result.
func (r *httpJSONResponseElement) ParseBody(field string, result interface{}) error {
if result != nil {
if err := parseBody(r.bodyObject, field, result); err != nil {
return driver.WithStack(err)
}
}
return nil
}
// ParseArrayBody performs protocol specific unmarshalling of the response array data into individual response objects.
// This can only be used for requests that return an array of objects.
func (r *httpJSONResponseElement) ParseArrayBody() ([]driver.Response, error) {
return nil, driver.WithStack(driver.InvalidArgumentError{Message: "ParseArrayBody not allowed"})
}

@ -0,0 +1,151 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package http
import (
"fmt"
"net/http"
velocypack "github.com/arangodb/go-velocypack"
driver "github.com/arangodb/go-driver"
)
// httpVPackResponse implements driver.Response for standard golang Velocypack encoded http responses.
type httpVPackResponse struct {
resp *http.Response
rawResponse []byte
slice velocypack.Slice
bodyArray []driver.Response
}
// StatusCode returns an HTTP compatible status code of the response.
func (r *httpVPackResponse) StatusCode() int {
return r.resp.StatusCode
}
// Endpoint returns the endpoint that handled the request.
func (r *httpVPackResponse) Endpoint() string {
u := *r.resp.Request.URL
u.Path = ""
u.RawQuery = ""
return u.String()
}
// CheckStatus checks if the status of the response equals to one of the given status codes.
// If so, nil is returned.
// If not, an attempt is made to parse an error response in the body and an error is returned.
func (r *httpVPackResponse) CheckStatus(validStatusCodes ...int) error {
for _, x := range validStatusCodes {
if x == r.resp.StatusCode {
// Found valid status code
return nil
}
}
// Invalid status code, try to parse arango error response.
var aerr driver.ArangoError
if err := r.ParseBody("", &aerr); err == nil && aerr.HasError {
// Found correct arango error.
return aerr
}
// We do not have a valid error code, so we can only create one based on the HTTP status code.
return driver.ArangoError{
HasError: true,
Code: r.resp.StatusCode,
ErrorMessage: fmt.Sprintf("Unexpected status code %d", r.resp.StatusCode),
}
}
// Header returns the value of a response header with given key.
// If no such header is found, an empty string is returned.
func (r *httpVPackResponse) Header(key string) string {
return r.resp.Header.Get(key)
}
// ParseBody performs protocol specific unmarshalling of the response data into the given result.
// If the given field is non-empty, the contents of that field will be parsed into the given result.
func (r *httpVPackResponse) ParseBody(field string, result interface{}) error {
slice, err := r.getSlice()
if err != nil {
return driver.WithStack(err)
}
if field != "" {
var err error
slice, err = slice.Get(field)
if err != nil {
return driver.WithStack(err)
}
if slice.IsNone() {
// Field not found
return nil
}
}
if result != nil {
if err := velocypack.Unmarshal(slice, result); err != nil {
return driver.WithStack(err)
}
}
return nil
}
// ParseArrayBody performs protocol specific unmarshalling of the response array data into individual response objects.
// This can only be used for requests that return an array of objects.
func (r *httpVPackResponse) ParseArrayBody() ([]driver.Response, error) {
if r.bodyArray == nil {
slice, err := r.getSlice()
if err != nil {
return nil, driver.WithStack(err)
}
l, err := slice.Length()
if err != nil {
return nil, driver.WithStack(err)
}
bodyArray := make([]driver.Response, 0, l)
it, err := velocypack.NewArrayIterator(slice)
if err != nil {
return nil, driver.WithStack(err)
}
for it.IsValid() {
v, err := it.Value()
if err != nil {
return nil, driver.WithStack(err)
}
bodyArray = append(bodyArray, &httpVPackResponseElement{slice: v})
it.Next()
}
r.bodyArray = bodyArray
}
return r.bodyArray, nil
}
// getSlice reads the slice from the response if needed.
func (r *httpVPackResponse) getSlice() (velocypack.Slice, error) {
if r.slice == nil {
r.slice = velocypack.Slice(r.rawResponse)
//fmt.Println(r.slice)
}
return r.slice, nil
}

@ -0,0 +1,124 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package http
import (
"fmt"
"github.com/arangodb/go-velocypack"
"github.com/arangodb/go-driver"
)
// httpVPackResponseElement implements driver.Response for an entry of an array response.
type httpVPackResponseElement struct {
statusCode *int
slice velocypack.Slice
}
// StatusCode returns an HTTP compatible status code of the response.
func (r *httpVPackResponseElement) StatusCode() int {
if r.statusCode == nil {
statusCode := 200
// Look for "error" field
if errorFieldSlice, _ := r.slice.Get("error"); !errorFieldSlice.IsNone() {
if hasError, err := errorFieldSlice.GetBool(); err == nil && hasError {
// We have an error, look for code field
statusCode = 500
if codeFieldSlice, _ := r.slice.Get("code"); !codeFieldSlice.IsNone() {
if code, err := codeFieldSlice.GetInt(); err == nil {
statusCode = int(code)
}
}
}
}
r.statusCode = &statusCode
}
return *r.statusCode
}
// Endpoint returns the endpoint that handled the request.
func (r *httpVPackResponseElement) Endpoint() string {
return ""
}
// CheckStatus checks if the status of the response equals to one of the given status codes.
// If so, nil is returned.
// If not, an attempt is made to parse an error response in the body and an error is returned.
func (r *httpVPackResponseElement) CheckStatus(validStatusCodes ...int) error {
statusCode := r.StatusCode()
for _, x := range validStatusCodes {
if x == statusCode {
// Found valid status code
return nil
}
}
// Invalid status code, try to parse arango error response.
var aerr driver.ArangoError
if err := r.ParseBody("", &aerr); err == nil && aerr.HasError {
// Found correct arango error.
return aerr
}
// We do not have a valid error code, so we can only create one based on the HTTP status code.
return driver.ArangoError{
HasError: true,
Code: statusCode,
ErrorMessage: fmt.Sprintf("Unexpected status code %d", statusCode),
}
}
// Header returns the value of a response header with given key.
// If no such header is found, an empty string is returned.
func (r *httpVPackResponseElement) Header(key string) string {
return ""
}
// ParseBody performs protocol specific unmarshalling of the response data into the given result.
// If the given field is non-empty, the contents of that field will be parsed into the given result.
func (r *httpVPackResponseElement) ParseBody(field string, result interface{}) error {
slice := r.slice
if field != "" {
var err error
slice, err = slice.Get(field)
if err != nil {
return driver.WithStack(err)
}
if slice.IsNone() {
// Field not found
return nil
}
}
if result != nil {
if err := velocypack.Unmarshal(slice, result); err != nil {
return driver.WithStack(err)
}
}
return nil
}
// ParseArrayBody performs protocol specific unmarshalling of the response array data into individual response objects.
// This can only be used for requests that return an array of objects.
func (r *httpVPackResponseElement) ParseArrayBody() ([]driver.Response, error) {
return nil, driver.WithStack(driver.InvalidArgumentError{Message: "ParseArrayBody not allowed"})
}

@ -0,0 +1,98 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import (
"fmt"
"strings"
)
// ArangoID is a generic Arango ID struct representation
type ArangoID struct {
ID string `json:"id,omitempty"`
GloballyUniqueId string `json:"globallyUniqueId,omitempty"`
}
// DocumentID references a document in a collection.
// Format: collection/_key
type DocumentID string
// String returns a string representation of the document ID.
func (id DocumentID) String() string {
return string(id)
}
// Validate validates the given id.
func (id DocumentID) Validate() error {
if id == "" {
return WithStack(fmt.Errorf("DocumentID is empty"))
}
parts := strings.Split(string(id), "/")
if len(parts) != 2 {
return WithStack(fmt.Errorf("Expected 'collection/key', got '%s'", string(id)))
}
if parts[0] == "" {
return WithStack(fmt.Errorf("Collection part of '%s' is empty", string(id)))
}
if parts[1] == "" {
return WithStack(fmt.Errorf("Key part of '%s' is empty", string(id)))
}
return nil
}
// ValidateOrEmpty validates the given id unless it is empty.
// In case of empty, nil is returned.
func (id DocumentID) ValidateOrEmpty() error {
if id == "" {
return nil
}
if err := id.Validate(); err != nil {
return WithStack(err)
}
return nil
}
// IsEmpty returns true if the given ID is empty, false otherwise.
func (id DocumentID) IsEmpty() bool {
return id == ""
}
// Collection returns the collection part of the ID.
func (id DocumentID) Collection() string {
parts := strings.Split(string(id), "/")
return pathUnescape(parts[0])
}
// Key returns the key part of the ID.
func (id DocumentID) Key() string {
parts := strings.Split(string(id), "/")
if len(parts) == 2 {
return pathUnescape(parts[1])
}
return ""
}
// NewDocumentID creates a new document ID from the given collection, key pair.
func NewDocumentID(collection, key string) DocumentID {
return DocumentID(pathEscape(collection) + "/" + pathEscape(key))
}

@ -0,0 +1,102 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import "context"
// IndexType represents a index type as string
type IndexType string
// Symbolic constants for index types
const (
PrimaryIndex = IndexType("primary")
FullTextIndex = IndexType("fulltext") // Deprecated: since 3.10 version. Use ArangoSearch view instead.
HashIndex = IndexType("hash")
SkipListIndex = IndexType("skiplist")
PersistentIndex = IndexType("persistent")
GeoIndex = IndexType("geo")
EdgeIndex = IndexType("edge")
TTLIndex = IndexType("ttl")
ZKDIndex = IndexType("zkd")
InvertedIndex = IndexType("inverted")
)
// Index provides access to a single index in a single collection.
type Index interface {
// Name returns the collection specific ID of the index. This value should be used for all functions
// the require a index _name_.
Name() string
// ID returns the ID of the index. Effectively this is `<collection-name>/<index.Name()>`.
ID() string
// UserName returns the user provided name of the index or empty string if non is provided. This _name_
// is used in query to provide hints for the optimizer about preferred indexes.
UserName() string
// Type returns the type of the index
Type() IndexType
// Remove removes the entire index.
// If the index does not exist, a NotFoundError is returned.
Remove(ctx context.Context) error
// Fields returns a list of attributes of this index.
Fields() []string
// Unique returns if this index is unique.
Unique() bool
// Deduplicate returns deduplicate setting of this index.
Deduplicate() bool
// Sparse returns if this is a sparse index or not.
Sparse() bool
// GeoJSON returns if geo json was set for this index or not.
GeoJSON() bool
// InBackground if true will not hold an exclusive collection lock for the entire index creation period (rocksdb only).
InBackground() bool
// Estimates determines if the to-be-created index should maintain selectivity estimates or not.
Estimates() bool
// MinLength returns min length for this index if set.
MinLength() int
// ExpireAfter returns an expire after for this index if set.
ExpireAfter() int
// LegacyPolygons determines if the index uses legacy polygons or not - GeoIndex only
LegacyPolygons() bool
// CacheEnabled returns if the index is enabled for caching or not - PersistentIndex only
CacheEnabled() bool
// StoredValues returns a list of stored values for this index - PersistentIndex only
StoredValues() []string
// InvertedIndexOptions returns the inverted index options for this index - InvertedIndex only
InvertedIndexOptions() InvertedIndexOptions
}

@ -0,0 +1,287 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
"encoding/json"
"path"
"strings"
)
// indexStringToType converts a string representation of an index to IndexType
func indexStringToType(indexTypeString string) (IndexType, error) {
switch indexTypeString {
case string(FullTextIndex):
return FullTextIndex, nil
case string(HashIndex):
return HashIndex, nil
case string(SkipListIndex):
return SkipListIndex, nil
case string(PrimaryIndex):
return PrimaryIndex, nil
case string(PersistentIndex):
return PersistentIndex, nil
case string(GeoIndex), "geo1", "geo2":
return GeoIndex, nil
case string(EdgeIndex):
return EdgeIndex, nil
case string(TTLIndex):
return TTLIndex, nil
case string(ZKDIndex):
return ZKDIndex, nil
case string(InvertedIndex):
return InvertedIndex, nil
default:
return "", WithStack(InvalidArgumentError{Message: "unknown index type"})
}
}
// newIndex creates a new Index implementation.
func newIndex(data indexData, col *collection) (Index, error) {
if data.ID == "" {
return nil, WithStack(InvalidArgumentError{Message: "id is empty"})
}
parts := strings.Split(data.ID, "/")
if len(parts) != 2 {
return nil, WithStack(InvalidArgumentError{Message: "id must be `collection/name`"})
}
if col == nil {
return nil, WithStack(InvalidArgumentError{Message: "col is nil"})
}
indexType, err := indexStringToType(data.Type)
if err != nil {
return nil, WithStack(err)
}
return &index{
indexData: data,
indexType: indexType,
col: col,
db: col.db,
conn: col.conn,
}, nil
}
// newIndex creates a new Index implementation.
func newInvertedIndex(data invertedIndexData, col *collection) (Index, error) {
if data.ID == "" {
return nil, WithStack(InvalidArgumentError{Message: "id is empty"})
}
parts := strings.Split(data.ID, "/")
if len(parts) != 2 {
return nil, WithStack(InvalidArgumentError{Message: "id must be `collection/name`"})
}
if col == nil {
return nil, WithStack(InvalidArgumentError{Message: "col is nil"})
}
indexType, err := indexStringToType(data.Type)
if err != nil {
return nil, WithStack(err)
}
dataIndex := indexData{
ID: data.ID,
Type: data.Type,
InBackground: &data.InvertedIndexOptions.InBackground,
IsNewlyCreated: &data.InvertedIndexOptions.IsNewlyCreated,
Name: data.InvertedIndexOptions.Name,
ArangoError: data.ArangoError,
}
return &index{
indexData: dataIndex,
invertedDataIndex: data,
indexType: indexType,
col: col,
db: col.db,
conn: col.conn,
}, nil
}
// newIndexFrom map returns Index implementation based on index type extracted from rawData
func newIndexFromMap(rawData json.RawMessage, col *collection) (Index, error) {
type generalIndexData struct {
Type string `json:"type"`
}
var gen generalIndexData
err := json.Unmarshal(rawData, &gen)
if err != nil {
return nil, WithStack(err)
}
if IndexType(gen.Type) == InvertedIndex {
var idxData invertedIndexData
err = json.Unmarshal(rawData, &idxData)
if err != nil {
return nil, WithStack(err)
}
return newInvertedIndex(idxData, col)
}
var idxData indexData
err = json.Unmarshal(rawData, &idxData)
if err != nil {
return nil, WithStack(err)
}
return newIndex(idxData, col)
}
type index struct {
indexData
invertedDataIndex invertedIndexData
indexType IndexType
db *database
col *collection
conn Connection
}
// relPath creates the relative path to this index (`_db/<db-name>/_api/index`)
func (i *index) relPath() string {
return path.Join(i.db.relPath(), "_api", "index")
}
// Name returns the name of the index.
func (i *index) Name() string {
parts := strings.Split(i.indexData.ID, "/")
return parts[1]
}
// ID returns the ID of the index.
func (i *index) ID() string {
return i.indexData.ID
}
// UserName returns the user provided name of the index or empty string if non is provided.
func (i *index) UserName() string {
return i.indexData.Name
}
// Type returns the type of the index
func (i *index) Type() IndexType {
return i.indexType
}
// Fields returns a list of attributes of this index.
func (i *index) Fields() []string {
return i.indexData.Fields
}
// Unique returns if this index is unique.
func (i *index) Unique() bool {
if i.indexData.Unique == nil {
return false
}
return *i.indexData.Unique
}
// Deduplicate returns deduplicate setting of this index.
func (i *index) Deduplicate() bool {
if i.indexData.Deduplicate == nil {
return false
}
return *i.indexData.Deduplicate
}
// Sparse returns if this is a sparse index or not.
func (i *index) Sparse() bool {
if i.indexData.Sparse == nil {
return false
}
return *i.indexData.Sparse
}
// GeoJSON returns if geo json was set for this index or not.
func (i *index) GeoJSON() bool {
if i.indexData.GeoJSON == nil {
return false
}
return *i.indexData.GeoJSON
}
// InBackground if true will not hold an exclusive collection lock for the entire index creation period (rocksdb only).
func (i *index) InBackground() bool {
if i.indexData.InBackground == nil {
return false
}
return *i.indexData.InBackground
}
// Estimates determines if the to-be-created index should maintain selectivity estimates or not.
func (i *index) Estimates() bool {
if i.indexData.Estimates == nil {
return false
}
return *i.indexData.Estimates
}
// MinLength returns min length for this index if set.
func (i *index) MinLength() int {
return i.indexData.MinLength
}
// ExpireAfter returns an expire after for this index if set.
func (i *index) ExpireAfter() int {
return i.indexData.ExpireAfter
}
// LegacyPolygons determines if the index uses legacy polygons or not - GeoIndex only
func (i *index) LegacyPolygons() bool {
if i.indexData.LegacyPolygons == nil {
return false
}
return *i.indexData.LegacyPolygons
}
// CacheEnabled returns if the index is enabled for caching or not - PersistentIndex only
func (i *index) CacheEnabled() bool {
if i.indexData.CacheEnabled == nil {
return false
}
return *i.indexData.CacheEnabled
}
// StoredValues returns a list of stored values for this index - PersistentIndex only
func (i *index) StoredValues() []string {
return i.indexData.StoredValues
}
// InvertedIndexOptions returns the inverted index options for this index - InvertedIndex only
func (i *index) InvertedIndexOptions() InvertedIndexOptions {
return i.invertedDataIndex.InvertedIndexOptions
}
// Remove removes the entire index.
// If the index does not exist, a NotFoundError is returned.
func (i *index) Remove(ctx context.Context) error {
req, err := i.conn.NewRequest("DELETE", path.Join(i.relPath(), i.indexData.ID))
if err != nil {
return WithStack(err)
}
resp, err := i.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return WithStack(err)
}
return nil
}

@ -0,0 +1,69 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
// DocumentMeta contains all meta data used to identifier a document.
type DocumentMeta struct {
Key string `json:"_key,omitempty"`
ID DocumentID `json:"_id,omitempty"`
Rev string `json:"_rev,omitempty"`
OldRev string `json:"_oldRev,omitempty"`
}
// validateKey returns an error if the given key is empty otherwise invalid.
func validateKey(key string) error {
if key == "" {
return WithStack(InvalidArgumentError{Message: "key is empty"})
}
return nil
}
// DocumentMetaSlice is a slice of DocumentMeta elements
type DocumentMetaSlice []DocumentMeta
// Keys returns the keys of all elements.
func (l DocumentMetaSlice) Keys() []string {
keys := make([]string, len(l))
for i, m := range l {
keys[i] = m.Key
}
return keys
}
// Revs returns the revisions of all elements.
func (l DocumentMetaSlice) Revs() []string {
revs := make([]string, len(l))
for i, m := range l {
revs[i] = m.Rev
}
return revs
}
// IDs returns the ID's of all elements.
func (l DocumentMetaSlice) IDs() []DocumentID {
ids := make([]DocumentID, len(l))
for i, m := range l {
ids[i] = m.ID
}
return ids
}

@ -0,0 +1,56 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
type Protocol int
const (
ProtocolHTTP Protocol = iota
ProtocolVST1_0
ProtocolVST1_1
)
// ProtocolSet is a set of protocols.
type ProtocolSet []Protocol
// Contains returns true if the given protocol is contained in the given set, false otherwise.
func (ps ProtocolSet) Contains(p Protocol) bool {
for _, x := range ps {
if x == p {
return true
}
}
return false
}
// ContainsAny returns true if any of the given protocols is contained in the given set, false otherwise.
func (ps ProtocolSet) ContainsAny(p ...Protocol) bool {
for _, x := range ps {
for _, y := range p {
if x == y {
return true
}
}
}
return false
}

@ -0,0 +1,297 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
"time"
)
const (
keyQueryCount = "arangodb-query-count"
keyQueryBatchSize = "arangodb-query-batchSize"
keyQueryCache = "arangodb-query-cache"
keyQueryMemoryLimit = "arangodb-query-memoryLimit"
keyQueryForceOneShardAttributeValue = "arangodb-query-forceOneShardAttributeValue"
keyQueryTTL = "arangodb-query-ttl"
keyQueryOptSatSyncWait = "arangodb-query-opt-satSyncWait"
keyQueryOptFullCount = "arangodb-query-opt-fullCount"
keyQueryOptStream = "arangodb-query-opt-stream"
keyQueryOptProfile = "arangodb-query-opt-profile"
keyQueryOptMaxRuntime = "arangodb-query-opt-maxRuntime"
keyQueryShardIds = "arangodb-query-opt-shardIds"
keyFillBlockCache = "arangodb-query-opt-fillBlockCache"
)
// WithQueryCount is used to configure a context that will set the Count of a query request,
// If value is not given it defaults to true.
func WithQueryCount(parent context.Context, value ...bool) context.Context {
v := true
if len(value) > 0 {
v = value[0]
}
return context.WithValue(contextOrBackground(parent), keyQueryCount, v)
}
// WithQueryBatchSize is used to configure a context that will set the BatchSize of a query request,
func WithQueryBatchSize(parent context.Context, value int) context.Context {
return context.WithValue(contextOrBackground(parent), keyQueryBatchSize, value)
}
// WithQuerySharIds is used to configure a context that will set the ShardIds of a query request,
func WithQueryShardIds(parent context.Context, value []string) context.Context {
return context.WithValue(contextOrBackground(parent), keyQueryShardIds, value)
}
// WithQueryCache is used to configure a context that will set the Cache of a query request,
// If value is not given it defaults to true.
func WithQueryCache(parent context.Context, value ...bool) context.Context {
v := true
if len(value) > 0 {
v = value[0]
}
return context.WithValue(contextOrBackground(parent), keyQueryCache, v)
}
// WithQueryMemoryLimit is used to configure a context that will set the MemoryList of a query request,
func WithQueryMemoryLimit(parent context.Context, value int64) context.Context {
return context.WithValue(contextOrBackground(parent), keyQueryMemoryLimit, value)
}
// WithQueryForceOneShardAttributeValue is used to configure a context that will set the ForceOneShardAttributeValue of a query request,
func WithQueryForceOneShardAttributeValue(parent context.Context, value string) context.Context {
return context.WithValue(contextOrBackground(parent), keyQueryForceOneShardAttributeValue, value)
}
// WithQueryTTL is used to configure a context that will set the TTL of a query request,
func WithQueryTTL(parent context.Context, value time.Duration) context.Context {
return context.WithValue(contextOrBackground(parent), keyQueryTTL, value)
}
// WithQuerySatelliteSyncWait sets the satelliteSyncWait query value on the query cursor request
func WithQuerySatelliteSyncWait(parent context.Context, value time.Duration) context.Context {
return context.WithValue(contextOrBackground(parent), keyQueryOptSatSyncWait, value)
}
// WithQueryFullCount is used to configure whether the query returns the full count of results
// before the last LIMIT statement
func WithQueryFullCount(parent context.Context, value ...bool) context.Context {
v := true
if len(value) > 0 {
v = value[0]
}
return context.WithValue(contextOrBackground(parent), keyQueryOptFullCount, v)
}
// WithQueryStream is used to configure whether this becomes a stream query.
// A stream query is not executed right away, but continually evaluated
// when the client is requesting more results. Should the cursor expire
// the query transaction is canceled. This means for writing queries clients
// have to read the query-cursor until the HasMore() method returns false.
func WithQueryStream(parent context.Context, value ...bool) context.Context {
v := true
if len(value) > 0 {
v = value[0]
}
return context.WithValue(contextOrBackground(parent), keyQueryOptStream, v)
}
// WithQueryProfile is used to configure whether Query should be profiled.
func WithQueryProfile(parent context.Context, value ...int) context.Context {
v := 1
if len(value) > 0 {
v = value[0]
}
if v < 0 {
v = 0
} else if v > 2 {
v = 2
}
return context.WithValue(contextOrBackground(parent), keyQueryOptProfile, v)
}
func WithQueryMaxRuntime(parent context.Context, value ...float64) context.Context {
v := 0.0
if len(value) > 0 {
v = value[0]
}
return context.WithValue(contextOrBackground(parent), keyQueryOptMaxRuntime, v)
}
// WithQueryFillBlockCache if is set to true or not specified, this will make the query store the data it reads via the RocksDB storage engine in the RocksDB block cache.
// This is usually the desired behavior. The option can be set to false for queries that are known to either read a lot of data which would thrash the block cache,
// or for queries that read data which are known to be outside of the hot set. By setting the option to false, data read by the query will not make it into
// the RocksDB block cache if not already in there, thus leaving more room for the actual hot set.
func WithQueryFillBlockCache(parent context.Context, value ...bool) context.Context {
v := true
if len(value) > 0 {
v = value[0]
}
return context.WithValue(contextOrBackground(parent), keyFillBlockCache, v)
}
type queryRequest struct {
// indicates whether the number of documents in the result set should be returned in the "count" attribute of the result.
// Calculating the "count" attribute might have a performance impact for some queries in the future so this option is
// turned off by default, and "count" is only returned when requested.
Count bool `json:"count,omitempty"`
// maximum number of result documents to be transferred from the server to the client in one roundtrip.
// If this attribute is not set, a server-controlled default value will be used. A batchSize value of 0 is disallowed.
BatchSize int `json:"batchSize,omitempty"`
// flag to determine whether the AQL query cache shall be used. If set to false, then any query cache lookup
// will be skipped for the query. If set to true, it will lead to the query cache being checked for the query
// if the query cache mode is either on or demand.
Cache bool `json:"cache,omitempty"`
// the maximum number of memory (measured in bytes) that the query is allowed to use. If set, then the query will fail
// with error "resource limit exceeded" in case it allocates too much memory. A value of 0 indicates that there is no memory limit.
MemoryLimit int64 `json:"memoryLimit,omitempty"`
// The time-to-live for the cursor (in seconds). The cursor will be removed on the server automatically after the specified
// amount of time. This is useful to ensure garbage collection of cursors that are not fully fetched by clients.
// If not set, a server-defined value will be used.
TTL float64 `json:"ttl,omitempty"`
// contains the query string to be executed
Query string `json:"query"`
// key/value pairs representing the bind parameters.
BindVars map[string]interface{} `json:"bindVars,omitempty"`
Options struct {
// ShardId query option
ShardIds []string `json:"shardIds,omitempty"`
// Profile If set to true or 1, then the additional query profiling information will be returned in the sub-attribute profile of the extra return attribute,
// if the query result is not served from the query cache. Set to 2 the query will include execution stats per query plan node in
// sub-attribute stats.nodes of the extra return attribute. Additionally the query plan is returned in the sub-attribute extra.plan.
Profile int `json:"profile,omitempty"`
// A list of to-be-included or to-be-excluded optimizer rules can be put into this attribute, telling the optimizer to include or exclude specific rules.
// To disable a rule, prefix its name with a -, to enable a rule, prefix it with a +. There is also a pseudo-rule all, which will match all optimizer rules.
OptimizerRules string `json:"optimizer.rules,omitempty"`
// This Enterprise Edition parameter allows to configure how long a DBServer will have time to bring the satellite collections
// involved in the query into sync. The default value is 60.0 (seconds). When the max time has been reached the query will be stopped.
SatelliteSyncWait float64 `json:"satelliteSyncWait,omitempty"`
// if set to true and the query contains a LIMIT clause, then the result will have an extra attribute with the sub-attributes
// stats and fullCount, { ... , "extra": { "stats": { "fullCount": 123 } } }. The fullCount attribute will contain the number
// of documents in the result before the last LIMIT in the query was applied. It can be used to count the number of documents
// that match certain filter criteria, but only return a subset of them, in one go. It is thus similar to MySQL's SQL_CALC_FOUND_ROWS hint.
// Note that setting the option will disable a few LIMIT optimizations and may lead to more documents being processed, and
// thus make queries run longer. Note that the fullCount attribute will only be present in the result if the query has a LIMIT clause
// and the LIMIT clause is actually used in the query.
FullCount bool `json:"fullCount,omitempty"`
// Limits the maximum number of plans that are created by the AQL query optimizer.
MaxPlans int `json:"maxPlans,omitempty"`
// Specify true and the query will be executed in a streaming fashion. The query result is not stored on
// the server, but calculated on the fly. Beware: long-running queries will need to hold the collection
// locks for as long as the query cursor exists. When set to false a query will be executed right away in
// its entirety.
Stream bool `json:"stream,omitempty"`
// MaxRuntime specify the timeout which can be used to kill a query on the server after the specified
// amount in time. The timeout value is specified in seconds. A value of 0 means no timeout will be enforced.
MaxRuntime float64 `json:"maxRuntime,omitempty"`
// ForceOneShardAttributeValue This query option can be used in complex queries in case the query optimizer cannot
// automatically detect that the query can be limited to only a single server (e.g. in a disjoint smart graph case).
ForceOneShardAttributeValue *string `json:"forceOneShardAttributeValue,omitempty"`
// FillBlockCache if is set to true or not specified, this will make the query store the data it reads via the RocksDB storage engine in the RocksDB block cache.
// This is usually the desired behavior. The option can be set to false for queries that are known to either read a lot of data which would thrash the block cache,
// or for queries that read data which are known to be outside of the hot set. By setting the option to false, data read by the query will not make it into
// the RocksDB block cache if not already in there, thus leaving more room for the actual hot set.
FillBlockCache bool `json:"fillBlockCache,omitempty"`
} `json:"options,omitempty"`
}
// applyContextSettings fills fields in the queryRequest from the given context.
func (q *queryRequest) applyContextSettings(ctx context.Context) {
if ctx == nil {
return
}
if rawValue := ctx.Value(keyQueryCount); rawValue != nil {
if value, ok := rawValue.(bool); ok {
q.Count = value
}
}
if rawValue := ctx.Value(keyQueryBatchSize); rawValue != nil {
if value, ok := rawValue.(int); ok {
q.BatchSize = value
}
}
if rawValue := ctx.Value(keyQueryShardIds); rawValue != nil {
if value, ok := rawValue.([]string); ok {
q.Options.ShardIds = value
}
}
if rawValue := ctx.Value(keyQueryCache); rawValue != nil {
if value, ok := rawValue.(bool); ok {
q.Cache = value
}
}
if rawValue := ctx.Value(keyQueryMemoryLimit); rawValue != nil {
if value, ok := rawValue.(int64); ok {
q.MemoryLimit = value
}
}
if rawValue := ctx.Value(keyQueryForceOneShardAttributeValue); rawValue != nil {
if value, ok := rawValue.(string); ok {
q.Options.ForceOneShardAttributeValue = &value
}
}
if rawValue := ctx.Value(keyQueryTTL); rawValue != nil {
if value, ok := rawValue.(time.Duration); ok {
q.TTL = value.Seconds()
}
}
if rawValue := ctx.Value(keyQueryOptSatSyncWait); rawValue != nil {
if value, ok := rawValue.(time.Duration); ok {
q.Options.SatelliteSyncWait = value.Seconds()
}
}
if rawValue := ctx.Value(keyQueryOptFullCount); rawValue != nil {
if value, ok := rawValue.(bool); ok {
q.Options.FullCount = value
}
}
if rawValue := ctx.Value(keyQueryOptStream); rawValue != nil {
if value, ok := rawValue.(bool); ok {
q.Options.Stream = value
}
}
if rawValue := ctx.Value(keyQueryOptProfile); rawValue != nil {
if _, ok := rawValue.(bool); ok {
q.Options.Profile = 1
} else if value, ok := rawValue.(int); ok {
q.Options.Profile = value
}
}
if rawValue := ctx.Value(keyQueryOptMaxRuntime); rawValue != nil {
if value, ok := rawValue.(float64); ok {
q.Options.MaxRuntime = value
}
}
if rawValue := ctx.Value(keyFillBlockCache); rawValue != nil {
if value, ok := rawValue.(bool); ok {
q.Options.FillBlockCache = value
}
}
}
type parseQueryRequest struct {
// contains the query string to be executed
Query string `json:"query"`
}

@ -0,0 +1,68 @@
//
// DISCLAIMER
//
// Copyright 2018 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
//
// Author Ewout Prangsma
//
package driver
import (
"context"
"time"
)
// Tick is represent a place in either the Write-Ahead Log,
// journals and datafiles value reported by the server
type Tick string
// Batch represents state on the server used during
// certain replication operations to keep state required
// by the client (such as Write-Ahead Log, inventory and data-files)
type Batch interface {
// id of this batch
BatchID() string
// LastTick reported by the server for this batch
LastTick() Tick
// Extend the lifetime of an existing batch on the server
Extend(ctx context.Context, ttl time.Duration) error
// DeleteBatch deletes an existing batch on the server
Delete(ctx context.Context) error
}
// Replication provides access to replication related operations.
type Replication interface {
// CreateBatch creates a "batch" to prevent removal of state required for replication
CreateBatch(ctx context.Context, db Database, serverID int64, ttl time.Duration) (Batch, error)
// Get the inventory of the server containing all collections (with entire details) of a database.
// When this function is called on a coordinator is a cluster, an ID of a DBServer must be provided
// using a context that is prepare with `WithDBServerID`.
DatabaseInventory(ctx context.Context, db Database) (DatabaseInventory, error)
// GetRevisionTree retrieves the Revision tree (Merkel tree) associated with the collection.
GetRevisionTree(ctx context.Context, db Database, batchId, collection string) (RevisionTree, error)
// GetRevisionsByRanges retrieves the revision IDs of documents within requested ranges.
GetRevisionsByRanges(ctx context.Context, db Database, batchId, collection string, minMaxRevision []RevisionMinMax,
resume RevisionUInt64) (RevisionRanges, error)
// GetRevisionDocuments retrieves documents by revision.
GetRevisionDocuments(ctx context.Context, db Database, batchId, collection string,
revisions Revisions) ([]map[string]interface{}, error)
}

@ -0,0 +1,165 @@
//
// DISCLAIMER
//
// Copyright 2018-2021 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
//
// Author Ewout Prangsma
// Author Tomasz Mielech
//
package driver
import (
"context"
"errors"
"path"
"strconv"
"sync/atomic"
"time"
)
// Content of the create batch resp
type batchMetadata struct {
// ID of the batch
ID string `json:"id"`
// Last Tick reported by the server
LastTickInt Tick `json:"lastTick,omitempty"`
cl *client
serverID int64
database string
closed int32
}
// ErrBatchClosed occurs when there is an attempt closing or prolonging closed batch
var ErrBatchClosed = errors.New("Batch already closed")
// CreateBatch creates a "batch" to prevent WAL file removal and to take a snapshot
func (c *client) CreateBatch(ctx context.Context, db Database, serverID int64, ttl time.Duration) (Batch, error) {
req, err := c.conn.NewRequest("POST", path.Join("_db", db.Name(), "_api/replication/batch"))
if err != nil {
return nil, WithStack(err)
}
req = req.SetQuery("serverId", strconv.FormatInt(serverID, 10))
params := struct {
TTL float64 `json:"ttl"`
}{TTL: ttl.Seconds()} // just use a default ttl value
req, err = req.SetBody(params)
if err != nil {
return nil, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
var batch batchMetadata
if err := resp.ParseBody("", &batch); err != nil {
return nil, WithStack(err)
}
batch.cl = c
batch.serverID = serverID
batch.database = db.Name()
return &batch, nil
}
// Get the inventory of a server containing all collections (with entire details) of a database.
func (c *client) DatabaseInventory(ctx context.Context, db Database) (DatabaseInventory, error) {
req, err := c.conn.NewRequest("GET", path.Join("_db", db.Name(), "_api/replication/inventory"))
if err != nil {
return DatabaseInventory{}, WithStack(err)
}
applyContextSettings(ctx, req)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return DatabaseInventory{}, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return DatabaseInventory{}, WithStack(err)
}
var result DatabaseInventory
if err := resp.ParseBody("", &result); err != nil {
return DatabaseInventory{}, WithStack(err)
}
return result, nil
}
// BatchID reported by the server
// The receiver is pointer because this struct contains the field `closed` and it can not be copied
// because race detector will complain.
func (b *batchMetadata) BatchID() string {
return b.ID
}
// LastTick reported by the server for this batch
// The receiver is pointer because this struct contains the field `closed` and it can not be copied
// because race detector will complain.
func (b *batchMetadata) LastTick() Tick {
return b.LastTickInt
}
// Extend the lifetime of an existing batch on the server
func (b *batchMetadata) Extend(ctx context.Context, ttl time.Duration) error {
if !atomic.CompareAndSwapInt32(&b.closed, 0, 0) {
return WithStack(ErrBatchClosed)
}
req, err := b.cl.conn.NewRequest("PUT", path.Join("_db", b.database, "_api/replication/batch", b.ID))
if err != nil {
return WithStack(err)
}
req = req.SetQuery("serverId", strconv.FormatInt(b.serverID, 10))
input := struct {
TTL int64 `json:"ttl"`
}{
TTL: int64(ttl.Seconds()),
}
req, err = req.SetBody(input)
if err != nil {
return WithStack(err)
}
resp, err := b.cl.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(204); err != nil {
return WithStack(err)
}
return nil
}
// Delete an existing dump batch
func (b *batchMetadata) Delete(ctx context.Context) error {
if !atomic.CompareAndSwapInt32(&b.closed, 0, 1) {
return WithStack(ErrBatchClosed)
}
req, err := b.cl.conn.NewRequest("DELETE", path.Join("_db", b.database, "_api/replication/batch", b.ID))
if err != nil {
return WithStack(err)
}
resp, err := b.cl.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(204); err != nil {
return WithStack(err)
}
return nil
}

@ -0,0 +1,273 @@
//
// DISCLAIMER
//
// Copyright 2020 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
//
// Author Adam Janikowski
//
package driver
import (
"context"
"path"
"github.com/arangodb/go-velocypack"
)
// RevisionUInt64 is representation of '_rev' string value as an uint64 number
type RevisionUInt64 uint64
// RevisionMinMax is an array of two Revisions which create range of them
type RevisionMinMax [2]RevisionUInt64
// Revisions is a slice of Revisions
type Revisions []RevisionUInt64
type RevisionRanges struct {
Ranges []Revisions `json:"ranges"`
Resume RevisionUInt64 `json:"resume,string" velocypack:"resume"`
}
// RevisionTreeNode is a leaf in Merkle tree with hashed Revisions and with count of documents in the leaf
type RevisionTreeNode struct {
Hash uint64 `json:"hash"`
Count uint64 `json:"count,int"`
}
// RevisionTree is a list of Revisions in a Merkle tree
type RevisionTree struct {
Version int `json:"version"`
MaxDepth int `json:"maxDepth"`
RangeMin RevisionUInt64 `json:"rangeMin,string" velocypack:"rangeMin"`
RangeMax RevisionUInt64 `json:"rangeMax,string" velocypack:"rangeMax"`
InitialRangeMin RevisionUInt64 `json:"initialRangeMin,string" velocypack:"initialRangeMin"`
Count uint64 `json:"count,int"`
Hash uint64 `json:"hash"`
Nodes []RevisionTreeNode `json:"nodes"`
}
var (
revisionEncodingTable = [64]byte{'-', '_', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9'}
revisionDecodingTable = [256]byte{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 32 - 47 (here is the '-' on 45 place)
54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 0, 0, 0, 0, 0, 0, // 48 - 63
0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, // 64 - 79
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 0, 0, 0, 0, 1, // 80 - 95
0, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, // 96 - 111
43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 0, 0, 0, 0, 0, // 112 - 127
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 128 - 143
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 144 - 159
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 160 - 175
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 176 - 191
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 192 - 207
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 208 - 223
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 224 - 239
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 240 - 255
}
)
func decodeRevision(revision []byte) RevisionUInt64 {
var t RevisionUInt64
for _, s := range revision {
t = t*64 + RevisionUInt64(revisionDecodingTable[s])
}
return t
}
func encodeRevision(revision RevisionUInt64) []byte {
if revision == 0 {
return []byte{}
}
var result [12]byte
index := cap(result)
for revision > 0 {
index--
result[index] = revisionEncodingTable[uint8(revision&0x3f)]
revision >>= 6
}
return result[index:]
}
// UnmarshalJSON parses string revision document into RevisionUInt64 number
func (n *RevisionUInt64) UnmarshalJSON(revision []byte) (err error) {
length := len(revision)
if length > 2 {
*n = decodeRevision(revision[1 : length-1])
} else {
// it can be only empty json string ""
*n = 0
}
return nil
}
// MarshalJSON converts RevisionUInt64 into string revision
func (n *RevisionUInt64) MarshalJSON() ([]byte, error) {
if *n == 0 {
return []byte{'"', '"'}, nil // return an empty string
}
value := make([]byte, 0, 16)
r := encodeRevision(*n)
value = append(value, '"')
value = append(value, r...)
value = append(value, '"')
return value, nil
}
// UnmarshalVPack parses string revision document into RevisionUInt64 number
func (n *RevisionUInt64) UnmarshalVPack(slice velocypack.Slice) error {
source, err := slice.GetString()
if err != nil {
return err
}
*n = decodeRevision([]byte(source))
return nil
}
// MarshalVPack converts RevisionUInt64 into string revision
func (n *RevisionUInt64) MarshalVPack() (velocypack.Slice, error) {
var b velocypack.Builder
value := velocypack.NewStringValue(string(encodeRevision(*n)))
if err := b.AddValue(value); err != nil {
return nil, err
}
return b.Slice()
}
// GetRevisionTree retrieves the Revision tree (Merkel tree) associated with the collection.
func (c *client) GetRevisionTree(ctx context.Context, db Database, batchId, collection string) (RevisionTree, error) {
req, err := c.conn.NewRequest("GET", path.Join("_db", db.Name(), "_api/replication/revisions/tree"))
if err != nil {
return RevisionTree{}, WithStack(err)
}
req = req.SetQuery("batchId", batchId)
req = req.SetQuery("collection", collection)
resp, err := c.conn.Do(ctx, req)
if err != nil {
return RevisionTree{}, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return RevisionTree{}, WithStack(err)
}
var tree RevisionTree
if err := resp.ParseBody("", &tree); err != nil {
return RevisionTree{}, WithStack(err)
}
return tree, nil
}
// GetRevisionsByRanges retrieves the revision IDs of documents within requested ranges.
func (c *client) GetRevisionsByRanges(ctx context.Context, db Database, batchId, collection string,
minMaxRevision []RevisionMinMax, resume RevisionUInt64) (RevisionRanges, error) {
req, err := c.conn.NewRequest("PUT", path.Join("_db", db.Name(), "_api/replication/revisions/ranges"))
if err != nil {
return RevisionRanges{}, WithStack(err)
}
req = req.SetQuery("batchId", batchId)
req = req.SetQuery("collection", collection)
if resume > 0 {
req = req.SetQuery("resume", string(encodeRevision(resume)))
}
req, err = req.SetBodyArray(minMaxRevision, nil)
if err != nil {
return RevisionRanges{}, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return RevisionRanges{}, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return RevisionRanges{}, WithStack(err)
}
var ranges RevisionRanges
if err := resp.ParseBody("", &ranges); err != nil {
return RevisionRanges{}, WithStack(err)
}
return ranges, nil
}
// GetRevisionDocuments retrieves documents by revision.
func (c *client) GetRevisionDocuments(ctx context.Context, db Database, batchId, collection string,
revisions Revisions) ([]map[string]interface{}, error) {
req, err := c.conn.NewRequest("PUT", path.Join("_db", db.Name(), "_api/replication/revisions/documents"))
if err != nil {
return nil, WithStack(err)
}
req = req.SetQuery("batchId", batchId)
req = req.SetQuery("collection", collection)
req, err = req.SetBody(revisions)
if err != nil {
return nil, WithStack(err)
}
resp, err := c.conn.Do(ctx, req)
if err != nil {
return nil, WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return nil, WithStack(err)
}
arrayResponse, err := resp.ParseArrayBody()
if err != nil {
return nil, WithStack(err)
}
documents := make([]map[string]interface{}, 0, len(arrayResponse))
for _, a := range arrayResponse {
document := map[string]interface{}{}
if err = a.ParseBody("", &document); err != nil {
return nil, WithStack(err)
}
documents = append(documents, document)
}
return documents, nil
}

@ -0,0 +1,80 @@
//
// DISCLAIMER
//
// Copyright 2020 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
//
// Author Adam Janikowski
//
package driver
// TransactionOptions contains options that customize the transaction.
type TransactionOptions struct {
// Transaction size limit in bytes. Honored by the RocksDB storage engine only.
MaxTransactionSize int
// An optional numeric value that can be used to set a timeout for waiting on collection
// locks. If not specified, a default value will be used.
// Setting lockTimeout to 0 will make ArangoDB not time out waiting for a lock.
LockTimeout *int
// An optional boolean flag that, if set, will force the transaction to write
// all data to disk before returning.
WaitForSync bool
// Maximum number of operations after which an intermediate commit is performed
// automatically. Honored by the RocksDB storage engine only.
IntermediateCommitCount *int
// Optional arguments passed to action.
Params []interface{}
// Maximum total size of operations after which an intermediate commit is
// performed automatically. Honored by the RocksDB storage engine only.
IntermediateCommitSize *int
// ReadCollections Collections that the transaction reads from.
ReadCollections []string
// WriteCollections Collections that the transaction writes to.
WriteCollections []string
// ExclusiveCollections Collections that the transaction write exclusively to.
ExclusiveCollections []string
}
type transactionRequest struct {
MaxTransactionSize int `json:"maxTransactionSize"`
LockTimeout *int `json:"lockTimeout,omitempty"`
WaitForSync bool `json:"waitForSync"`
IntermediateCommitCount *int `json:"intermediateCommitCount,omitempty"`
Params []interface{} `json:"params"`
IntermediateCommitSize *int `json:"intermediateCommitSize,omitempty"`
Action string `json:"action"`
Collections transactionCollectionsRequest `json:"collections"`
}
type transactionCollectionsRequest struct {
Read []string `json:"read,omitempty"`
Write []string `json:"write,omitempty"`
Exclusive []string `json:"exclusive,omitempty"`
}
type transactionResponse struct {
ArangoError
Result interface{} `json:"result"`
}

@ -0,0 +1,128 @@
//
// DISCLAIMER
//
// Copyright 2017 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
//
// Author Ewout Prangsma
//
package driver
import "context"
// User provides access to a single user of a single server / cluster of servers.
type User interface {
// Name returns the name of the user.
Name() string
// Is this an active user?
IsActive() bool
// Is a password change for this user needed?
IsPasswordChangeNeeded() bool
// Get extra information about this user that was passed during its creation/update/replacement
Extra(result interface{}) error
// Remove removes the user.
// If the user does not exist, a NotFoundError is returned.
Remove(ctx context.Context) error
// Update updates individual properties of the user.
// If the user does not exist, a NotFoundError is returned.
Update(ctx context.Context, options UserOptions) error
// Replace replaces all properties of the user.
// If the user does not exist, a NotFoundError is returned.
Replace(ctx context.Context, options UserOptions) error
// AccessibleDatabases returns a list of all databases that can be accessed (read/write or read-only) by this user.
AccessibleDatabases(ctx context.Context) ([]Database, error)
// SetDatabaseAccess sets the access this user has to the given database.
// Pass a `nil` database to set the default access this user has to any new database.
// This function requires ArangoDB 3.2 and up for access value `GrantReadOnly`.
SetDatabaseAccess(ctx context.Context, db Database, access Grant) error
// GetDatabaseAccess gets the access rights for this user to the given database.
// Pass a `nil` database to get the default access this user has to any new database.
// This function requires ArangoDB 3.2 and up.
// By default this function returns the "effective" grant.
// To return the "configured" grant, pass a context configured with `WithConfigured`.
// This distinction is only relevant in ArangoDB 3.3 in the context of a readonly database.
GetDatabaseAccess(ctx context.Context, db Database) (Grant, error)
// RemoveDatabaseAccess removes the access this user has to the given database.
// As a result the users access falls back to its default access.
// If you remove default access (db==`nil`) for a user (and there are no specific access
// rules for a database), the user's access falls back to no-access.
// Pass a `nil` database to set the default access this user has to any new database.
// This function requires ArangoDB 3.2 and up.
RemoveDatabaseAccess(ctx context.Context, db Database) error
// SetCollectionAccess sets the access this user has to a collection.
// If you pass a `Collection`, it will set access for that collection.
// If you pass a `Database`, it will set the default collection access for that database.
// If you pass `nil`, it will set the default collection access for the default database.
// This function requires ArangoDB 3.2 and up.
SetCollectionAccess(ctx context.Context, col AccessTarget, access Grant) error
// GetCollectionAccess gets the access rights for this user to the given collection.
// If you pass a `Collection`, it will get access for that collection.
// If you pass a `Database`, it will get the default collection access for that database.
// If you pass `nil`, it will get the default collection access for the default database.
// By default this function returns the "effective" grant.
// To return the "configured" grant, pass a context configured with `WithConfigured`.
// This distinction is only relevant in ArangoDB 3.3 in the context of a readonly database.
GetCollectionAccess(ctx context.Context, col AccessTarget) (Grant, error)
// RemoveCollectionAccess removes the access this user has to a collection.
// If you pass a `Collection`, it will removes access for that collection.
// If you pass a `Database`, it will removes the default collection access for that database.
// If you pass `nil`, it will removes the default collection access for the default database.
// This function requires ArangoDB 3.2 and up.
RemoveCollectionAccess(ctx context.Context, col AccessTarget) error
// GrantReadWriteAccess grants this user read/write access to the given database.
//
// Deprecated: use GrantDatabaseReadWriteAccess instead.
GrantReadWriteAccess(ctx context.Context, db Database) error
// RevokeAccess revokes this user access to the given database.
//
// Deprecated: use `SetDatabaseAccess(ctx, db, GrantNone)` instead.
RevokeAccess(ctx context.Context, db Database) error
}
// Grant specifies access rights for an object
type Grant string
const (
// GrantReadWrite indicates read/write access to an object
GrantReadWrite Grant = "rw"
// GrantReadOnly indicates read-only access to an object
GrantReadOnly Grant = "ro"
// GrantNone indicates no access to an object
GrantNone Grant = "none"
)
// AccessTarget is implemented by Database & Collection and it used to
// get/set/remove collection permissions.
type AccessTarget interface {
// Name returns the name of the database/collection.
Name() string
}

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save