arangorb 1.4.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/ArangoRB.gemspec +20 -18
  3. data/Gemfile +3 -0
  4. data/README.md +1079 -908
  5. data/lib/AQL.rb +155 -0
  6. data/lib/Batch.rb +97 -0
  7. data/lib/Cache.rb +71 -0
  8. data/lib/Collection.rb +852 -0
  9. data/lib/Database.rb +417 -0
  10. data/lib/Document.rb +346 -0
  11. data/lib/Edge.rb +104 -0
  12. data/lib/Error.rb +125 -0
  13. data/lib/Foxx.rb +277 -0
  14. data/lib/Graph.rb +325 -0
  15. data/lib/Index.rb +126 -0
  16. data/lib/Replication.rb +235 -0
  17. data/lib/Request.rb +143 -0
  18. data/lib/Server.rb +466 -0
  19. data/lib/Task.rb +120 -0
  20. data/lib/Transaction.rb +115 -0
  21. data/lib/Traversal.rb +224 -0
  22. data/lib/User.rb +197 -0
  23. data/lib/Vertex.rb +127 -0
  24. data/lib/View.rb +151 -0
  25. data/lib/arangorb.rb +23 -15
  26. data/lib/helpers/Error.rb +28 -0
  27. data/lib/helpers/Return.rb +53 -0
  28. metadata +64 -45
  29. data/lib/ArangoRB_AQL.rb +0 -181
  30. data/lib/ArangoRB_Cache.rb +0 -174
  31. data/lib/ArangoRB_Col.rb +0 -526
  32. data/lib/ArangoRB_DB.rb +0 -363
  33. data/lib/ArangoRB_Doc.rb +0 -319
  34. data/lib/ArangoRB_Edg.rb +0 -184
  35. data/lib/ArangoRB_Gra.rb +0 -201
  36. data/lib/ArangoRB_Index.rb +0 -135
  37. data/lib/ArangoRB_Replication.rb +0 -261
  38. data/lib/ArangoRB_Ser.rb +0 -446
  39. data/lib/ArangoRB_Task.rb +0 -129
  40. data/lib/ArangoRB_Tra.rb +0 -169
  41. data/lib/ArangoRB_Tran.rb +0 -68
  42. data/lib/ArangoRB_User.rb +0 -157
  43. data/lib/ArangoRB_Ver.rb +0 -162
  44. data/spec/arangoRB_helper.rb +0 -4
  45. data/spec/arangoRestart_helper.rb +0 -14
  46. data/spec/arangorb-1.3.0.gem +0 -0
  47. data/spec/lib/0.1.0/arangoAQL_helper.rb +0 -64
  48. data/spec/lib/0.1.0/arangoC_helper.rb +0 -170
  49. data/spec/lib/0.1.0/arangoDB_helper.rb +0 -119
  50. data/spec/lib/0.1.0/arangoDoc_helper.rb +0 -79
  51. data/spec/lib/0.1.0/arangoE_helper.rb +0 -50
  52. data/spec/lib/0.1.0/arangoG_helper.rb +0 -78
  53. data/spec/lib/0.1.0/arangoS_helper.rb +0 -37
  54. data/spec/lib/0.1.0/arangoT_helper.rb +0 -48
  55. data/spec/lib/0.1.0/arangoV_helper.rb +0 -65
  56. data/spec/lib/1.0.0/arangoC_helper.rb +0 -73
  57. data/spec/lib/1.0.0/arangoDB_helper.rb +0 -48
  58. data/spec/lib/1.0.0/arangoI_helper.rb +0 -43
  59. data/spec/lib/1.0.0/arangoS_helper.rb +0 -192
  60. data/spec/lib/1.0.0/arangoTa_helper.rb +0 -49
  61. data/spec/lib/1.0.0/arangoTr_helper.rb +0 -15
  62. data/spec/lib/1.0.0/arangoU_helper.rb +0 -72
  63. data/spec/lib/1.1.0/arangoRB_helper.rb +0 -144
  64. data/spec/lib/1.1.0/arangoRB_walks_helper.rb +0 -19
  65. data/spec/lib/1.2.0/arangoCache_helper.rb +0 -66
  66. data/spec/lib/1.3.0/arangoHash_helper.rb +0 -30
  67. data/spec/lib/arangoRB_0.1.0_helper.rb +0 -9
  68. data/spec/lib/arangoRB_1.0.0_helper.rb +0 -6
  69. data/spec/lib/arangoRB_1.1.0_helper.rb +0 -2
  70. data/spec/lib/arangoRB_1.2.0_helper.rb +0 -2
  71. data/spec/spec_helper.rb +0 -42
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e7898e5b31c9c45cc10d0deccb84c18309f85e04
4
- data.tar.gz: a3d414487bd4fdaba84bc5f3c1c72e23011942cf
3
+ metadata.gz: 4f10d19137d9ad901edf1f2a821b71e7f7da9f34
4
+ data.tar.gz: 3745c2e90ed3e1496a95d2ad0bf7ea28b7fba953
5
5
  SHA512:
6
- metadata.gz: 0042e4fc89867ce6a3255bbe4a14f8d61a2ff7e2a1a91ed9f0f668da4823587e41be4823c643064dbf67855c23ffe3bdd5e38db28fe4c8a916e7ff8035dbf7ee
7
- data.tar.gz: 848cb9a73042f29651be327028cad40329478d17dbc64a09401fb11b48b9252d20501b8986d6aef8db780087c63482df1cb8df882ac58359131b34617b7f0825
6
+ metadata.gz: cebecdfe93d39841f2447f96407c027901b0d087b8208cafd203608afd6f743b8fe800b9289f4ec13c4555cf8db83593e778777a453e9b252d9a929690d18f05
7
+ data.tar.gz: ade043fc9e624d7255e169c577648b36f0a2bbdcc5cf1b17f9f3b3564ecedaeb1fde7660a396967b7f6940626e3b068688a01038ee0d48dae4b66568ae947205
data/ArangoRB.gemspec CHANGED
@@ -1,18 +1,20 @@
1
- # lib = File.expand_path('../lib', __FILE__)
2
- # $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
- require "rake"
4
-
5
- Gem::Specification.new do |s|
6
- s.name = 'arangorb'
7
- s.version = '1.4.1'
8
- s.authors = ['Stefano Martin']
9
- s.email = ['stefano@seluxit.com']
10
- s.homepage = 'https://github.com/StefanoMartin/ArangoRB'
11
- s.license = 'MIT'
12
- s.summary = 'A simple ruby client for ArangoDB'
13
- s.description = "Ruby driver for ArangoDB's HTTP API"
14
- s.platform = Gem::Platform::RUBY
15
- s.require_paths = ['lib']
16
- s.files = FileList['lib/*', 'spec/**/*', 'ArangoRB.gemspec', 'Gemfile', 'LICENSE', 'README.md'].to_a
17
- s.add_dependency 'httparty', '~> 0.14', '>= 0.14.0'
18
- end
1
+ # lib = File.expand_path('../lib', __FILE__)
2
+ # $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "rake"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'arangorb'
7
+ s.version = '2.0.0'
8
+ s.authors = ['Stefano Martin']
9
+ s.email = ['stefano@seluxit.com']
10
+ s.homepage = 'https://github.com/StefanoMartin/ArangoRB'
11
+ s.license = 'MIT'
12
+ s.summary = 'A simple ruby client for ArangoDB'
13
+ s.description = "Ruby driver for ArangoDB's HTTP API"
14
+ s.platform = Gem::Platform::RUBY
15
+ s.require_paths = ['lib']
16
+ s.files = FileList['lib/**/*', 'ArangoRB.gemspec', 'Gemfile', 'LICENSE', 'README.md'].to_a
17
+ s.add_dependency 'httparty', '~> 0.14', '>= 0.14.0'
18
+ s.add_dependency 'oj', '~> 3.6.11', '>= 3.6.11'
19
+ s.add_dependency 'connection_pool', '~> 2.2.2', '>= 2.2.2'
20
+ end
data/Gemfile CHANGED
@@ -1,8 +1,11 @@
1
1
  source "https://rubygems.org"
2
2
 
3
3
  gem "httparty", "~>0.14.0"
4
+ gem "oj", "~>3.6.11"
5
+ gem "connection_pool", "~>2.2.2"
4
6
 
5
7
  group :development, :test do
6
8
  gem "rspec", "~>3.5"
7
9
  gem "awesome_print", "~>1.7"
10
+ gem "pry-byebug"
8
11
  end
data/README.md CHANGED
@@ -1,908 +1,1079 @@
1
- ArangoRB [![Gem Version](https://badge.fury.io/rb/arangorb.svg)](https://badge.fury.io/rb/arangorb)
2
- ===============================
3
-
4
- [ArangoDatabase](https://www.arangodb.com/) is a native multi-model database with flexible data models for document, graphs, and key-values.
5
- ArangoRB is a Gem to use ArangoDatabase with Ruby. ArangoRB is based on the [HTTP API of ArangoDB](https://docs.arangodb.com/3.0/HTTP/index.html).
6
-
7
- ArangoRB 0.1.0 - 1.3.0 have been tested with ArangoDB 3.0 with Ruby 2.3.1</br>
8
- ArangoRB 1.4.0 has been tested with ArangoDB 3.1 with Ruby 2.3.3</br>
9
- It requires the gem "HTTParty"</br>
10
-
11
- To install ArangoRB: `gem install arangorb`
12
-
13
- To use it in your application: `require "arangorb"`
14
-
15
- This Gem was created from [Seluxit ApS](http://seluxit.com/).
16
-
17
- ## Classes used
18
-
19
- ArangoRB has the following classes.
20
-
21
- * [ArangoServer](#ArangoServer): to manage the global variables for the management of the database
22
- * [ArangoDatabase](#ArangoDatabase): to manage Databases
23
- * [ArangoCollection](#ArangoCollection): to manage Collections
24
- * [ArangoDocument](#ArangoDocument): to manage Documents
25
- * [ArangoVertex](#ArangoVertex): to manage Vertexes
26
- * [ArangoEdge](#ArangoEdge): to manage Edges
27
- * [ArangoGraph](#ArangoGraph): to manage Graphs
28
- * [ArangoTraversal](#ArangoTraversal): to manage Traversal operations
29
- * [ArangoAQL](#arangoaql): to manage AQL instances
30
- * [ArangoUser](#ArangoUser): to manage Users
31
- * [ArangoIndex](#ArangoIndex): to manage Indexes
32
- * [ArangoTask](#ArangoTask): to manage Tasks
33
- * [ArangoTransaction](#ArangoTransaction): to manage Transactions
34
- * [ArangoCache](#ArangoCache): to manage a cache on your Computer
35
- * [ArangoReplication](#ArangoReplication): to manage Replications
36
-
37
- NB: All the instances of these classes (with the exception of ArangoCache) can be transformed in Hash with the command to_h.
38
-
39
- <a name="ArangoServer"></a>
40
- ## ArangoServer
41
-
42
- ArangoServer is used to manage the default Database, Collection, User and Graph.
43
- It is used to give your login credentials and it is the mandatory step to start your database.
44
- Further it helps to manage the Server connected with ArangoDB.
45
-
46
- To setup your credentials use:
47
-
48
- ``` ruby
49
- ArangoServer.default_server user: "MyUsername", password: "MyPassword", server: "localhost", port: "8529"
50
- ```
51
-
52
- If not declared, the default values are user: "root", password: "", server: "localhost", port: "8529".
53
-
54
- ### Default variables
55
-
56
- Databases, Graphs and Collections used in your program can be defined each time. But often it is convenient to have some default one.
57
- If this is the case, the user can use ArangoServer to define some default values.
58
-
59
- ``` ruby
60
- ArangoServer.database = "MyDatabase" # Setup default Database
61
- ArangoServer.graph = "MyGraph" # Setup default Graph
62
- ArangoServer.collection = "MyCollection" # Setup default Collection
63
- ArangoServer.user = "MyUser" # Setup default User
64
- ```
65
-
66
- By default the default database is "\_system".
67
-
68
- ### Verbose
69
-
70
- For Debugging reasons the user can receive the original JSON file from the database by setting verbose on true (false by default).
71
-
72
- ``` ruby
73
- ArangoServer.verbose = true
74
- ```
75
-
76
- Remember that verbose is only for testing reason: to work efficiently verbose should be false.
77
-
78
- ### Information
79
-
80
- Basic informations can be retrieved with these command.
81
-
82
- ``` ruby
83
- ArangoServer.database # Check name default Database
84
- ArangoServer.graph # Check name default Graph
85
- ArangoServer.collection # Check name default Collection
86
- ArangoServer.user # Check name default User
87
- ArangoServer.address # Check address used to connect with the server
88
- ArangoServer.username # Check name used to connect with the server
89
- ArangoServer.verbose # Check if verbose is true or false
90
- ArangoServer.async # Check the status of async
91
- ArangoServer.request # Check the default request sent to the server
92
- ```
93
-
94
- To retrieve lists
95
-
96
- ``` ruby
97
- ArangoServer.endpoints # Lists of endpoints used
98
- ArangoServer.users # Lists of available users
99
- ArangoServer.databases # Lists of available databases
100
- ArangoServer.tasks # Lists of available tasks
101
- ```
102
-
103
- To monitoring the server you can use the following commands
104
-
105
- ``` ruby
106
- ArangoServer.log # Return log files
107
- ArangoServer.reload # Reloads the routing information from the collection routing.
108
- ArangoServer.statistics # Returns the statistics information
109
- ArangoServer.statistics description: true # Fetch descriptive info of statistics
110
- ArangoServer.role # Get to know whether this server is a Coordinator or DB-Server
111
- ArangoServer.serverId # Fetch this servers uniq identifier
112
- ```
113
-
114
- ### Manage Async
115
-
116
- With ArangoServer you can manage Async results.
117
-
118
- ``` ruby
119
- ArangoServer.async = false # default
120
- ArangoServer.async = true # fire and forget
121
- ArangoServer.async = "store" # fire and store
122
- ```
123
-
124
- If Async is "store", then the commands of ArangoRB will return the id of the Async requests.
125
- ArangoServer provides different methods to manage these Async requests.
126
-
127
- ``` ruby
128
- ArangoServer.pendingAsync # Querying the status of a pending job
129
- ArangoServer.fetchAsync id: id # Fetches a job result and removes it from the queue
130
- ArangoServer.retrieveAsync id: id # Returns the status of a specific job
131
- ArangoServer.retrieveAsync type: type # Returns the ids of job results with a specific status
132
- # Type can be "done" or "pending"
133
- ArangoServer.retrieveDoneAsync # Equivalent to ArangoServer.retrieveAsync type: "done"
134
- ArangoServer.retrievePendingAsync # Equivalent to ArangoServer.retrieveAsync type: "pending"
135
- ArangoServer.cancelAsync id: id # Cancels an async job
136
- ArangoServer.destroyAsync id: id # Deletes an async job result
137
- ArangoServer.destroyAsync type: type # Deletes async jobs with a specific status
138
- # Type can be "all" or "expired"
139
- ArangoServer.destroyAllAsync # Equivalent to ArangoServer.destroyAsync type: "all"
140
- ArangoServer.destroyExpiredAsync # Equivalent to ArangoServer.destroyAsync type: "expired"
141
- ```
142
-
143
- ### Batch
144
-
145
- ArangoDB offers a batch request API that clients can use to send multiple operations in one batch to ArangoDB. To manage it you can use the following way.
146
-
147
- ``` ruby
148
- queries = [
149
- {
150
- "type": "POST",
151
- "address": "/_db/MyDatabase/_api/collection",
152
- "body": {"name": "newCOLLECTION"},
153
- "id": "1"
154
- },
155
- {
156
- "type": "GET",
157
- "address": "/_api/database",
158
- "id": "2"
159
- }
160
- ]
161
- ArangoServer.batch queries: queries
162
- ```
163
-
164
- To manage these batch, ArangoServer offers the following functions:
165
-
166
- ``` ruby
167
- ArangoServer.createDumpBatch ttl: 10 # Create a new dump batch with 10 second time-to-live (return id of the dumpBatch)
168
- ArangoServer.prolongDumpBatch id: idDumpBatch, ttl: 20 # Prolong the life of a batch for 20 seconds
169
- ArangoServer.destroyDumpBatch id: idDumpBatch # Delete a selected batch
170
- ```
171
-
172
- ### Miscellaneous
173
-
174
- ``` ruby
175
- ArangoServer.version # Returns the server version number
176
- ArangoServer.flushWAL # Flushes the write-ahead log
177
- ArangoServer.propertyWAL # Retrieves the configuration of the write-ahead log
178
- ArangoServer.changePropertyWAL # Configures the write-ahead log
179
- ArangoServer.transactions # Returns information about the currently running transactions
180
- ArangoServer.time # Get the current time of the system
181
- ArangoServer.echo # Return current request
182
- ArangoServer.databaseVersion # Return the required version of the database
183
- ArangoServer.sleep duration: 10 # Sleep for a specified amount of seconds
184
- ArangoServer.shutdown # Initiate shutdown sequence
185
- ArangoServer.restart # Restart ArangoDB (requires administration access)
186
- ```
187
-
188
- UNTESTED
189
-
190
- ``` ruby
191
- ArangoServer.test body: body # Runs tests on server
192
- ArangoServer.execute body: body # Execute a script on the server.
193
- ```
194
-
195
- ### Sharding (UNTESTED)
196
-
197
- ArangoDB permits the sharding of the database. Although these methods has not been tested with ArangoRB.
198
-
199
- ``` ruby
200
- ArangoServer.cluster # Default cluster
201
- ArangoServer.getCluster body: body, cluster: cluster # Retrieve cluster
202
- ArangoServer.executeCluster body: body, cluster: cluster # Execute cluster
203
- ArangoServer.executeClusterPut body: body, cluster: cluster # Execute cluster
204
- ArangoServer.executeClusterHead body: body, cluster: cluster # Execute cluster
205
- ArangoServer.destroyCluster cluster: cluster # Delete cluster
206
- ArangoServer.updateCluster body: body, cluster: cluster # Update cluster
207
- ArangoServer.checkPort port: port # Allows to check whether a given port is usable
208
- ArangoServer.server # Returns the id of a server in a cluster.
209
- ArangoServer.clusterStatistics dbserver: dbserver # Allows to query the statistics of a DBserver in the cluster
210
- ```
211
-
212
- <a name="ArangoDatabase"></a>
213
- ## ArangoDatabase
214
-
215
- ArangoDatabase is used to manage your Database. You can create an instance in the following way:
216
-
217
- ``` ruby
218
- myDatabase = ArangoDatabase.new database: "MyDatabase"
219
- ```
220
-
221
- Alternatively, you can use the default values provided by ArangoServer:
222
-
223
- ``` ruby
224
- ArangoServer.database = "MyDatabase"
225
- myDatabase = ArangoDatabase.new
226
- ```
227
-
228
- ### Create and Destroy a Database
229
-
230
- ``` ruby
231
- myDatabase.create # Create a new Database
232
- myDatabase.destroy # Delete the selected Database
233
- ```
234
-
235
- ### Retrieve information
236
-
237
- ``` ruby
238
- ArangoDatabase.info # Obtain general info about the databases
239
- ArangoDatabase.databases # Obtain an Array with the available databases
240
- myDatabase.collections # Obtain an Array with the available collections in the selected Database
241
- myDatabase.graphs # Obtain an Array with the available graphs in the selected Database
242
- myDatabase.functions # Obtain an Array with the available functions in the selected Database
243
- ```
244
-
245
- It is possible to access its Collections and Graphs by their ids.
246
-
247
- ``` ruby
248
- myDatabase["MyCollection"].retrieve # where "MyCollection" is the id of the collection
249
- myDatabase.collection("MyCollection").retrieve
250
- myDatabase.graph("MyGraph").retrieve
251
- ```
252
-
253
-
254
- ### Query
255
-
256
- ArangoDatabase instances can manage the queries used in their Database.
257
-
258
- ``` ruby
259
- myDatabase.propertiesQuery; # See property of the Query
260
- myDatabase.currentQuery; # Check the current Query
261
- myDatabase.slowQuery; # Find slow queries
262
- myDatabase.stopSlowQuery; # Stop slow queries
263
- myDatabase.kill query: myQuery # Kill a Query
264
- myDatabase.changePropertiesQuery maxSlowQueries: 65 # Change properties of a Query
265
- ```
266
-
267
- ### Cache
268
-
269
- To manage the cache ArangoDatabase provides the following three functions.
270
-
271
- ``` ruby
272
- myDatabase.clearCache # Clear Cache
273
- myDatabase.propertyCache # Check properties Cache
274
- myDatabase.changePropertyCache maxResults: 30 # Change properties Cache
275
- ```
276
-
277
- ### AQL Functions
278
-
279
- ``` ruby
280
- myDatabase.createFunction code: "function(){return 1+1;}", name: "myFunction" # Create a new Function
281
- myDatabase.deleteFunction name: "myFunction" # Delete a function
282
- myDatabase.functions # Retrieve a list of the available functions
283
- ```
284
-
285
- ### User
286
-
287
- You can manage the right of a user to access the database.
288
-
289
- ``` ruby
290
- myDatabase.grant user: myUser # Grant access to database
291
- myDatabase.revoke user: myUser # Revoke access to database
292
- ```
293
-
294
- <a name="ArangoCollection"></a>
295
- ## ArangoCollection
296
-
297
- ArangoDatabase is used to manage your Collections. You can create an ArangoCollection instance in one of the following way:
298
-
299
- ``` ruby
300
- myCollection = ArangoCollection.new database: "MyDatabase", collection: "MyCollection"
301
- myCollection = ArangoCollection.new collection: "MyCollection" # Using the default database
302
- myCollection = ArangoCollection.new # Using the default database and collection
303
- ```
304
-
305
- A Collection can be of two types: "Document" and "Edge". If you want to specify it, uses:
306
-
307
- ``` ruby
308
- myCollectionA = ArangoCollection.new collection: "MyCollectionA", type: "Document"
309
- myCollectionB = ArangoCollection.new collection: "MyCollectionB", type: "Edge"
310
- ```
311
-
312
- ### Create a Collection
313
-
314
- `myCollection.create`
315
-
316
- If not specified the default type of the Collection is "Document".
317
- To create an Edge Collection you can use one of the next three options:
318
-
319
- ``` ruby
320
- myCollection.create_edge_collection
321
- myCollection.create type: "Edge"
322
- myCollectionB = ArangoCollection.new collection: "MyCollectionB", type: "Edge"; myCollectionB.create
323
- ```
324
-
325
- ### Destroy or Truncate a Collections
326
-
327
- Destroy will delete the selected Collection from the Database.
328
-
329
- `myCollection.destroy`
330
-
331
- Truncate will delete all the Documents inside the selected Collection.
332
-
333
- `myCollection.truncate`
334
-
335
- ### Retrieve information
336
-
337
- It is possible to access its Documents and its database.
338
-
339
- ``` ruby
340
- myCollection["MyDocument"]
341
- myDatabase.document("MyDocument")
342
- myDatabase.database
343
- ```
344
-
345
- For other information
346
-
347
- ``` ruby
348
- myCollection.retrieve # Retrieve the selected Collection
349
- myCollection.indexes # Return a list of all used Indexes in the Collection
350
- myCollection.data # Returns the whole content of one collection
351
- myCollection.properties # Properties of the Collection
352
- myCollection.count # Number of Documents in the Collection
353
- myCollection.stats # Statistics of the Collection
354
- myCollection.revision # Return collection revision id
355
- myCollection.checksum # Return checksum for the Collection
356
- ```
357
-
358
- To retrieve all the documents of a Collection you can use:
359
-
360
- ``` ruby
361
- myCollection.documents
362
- myCollection.allDocuments
363
- ```
364
-
365
- These two functions are similar except for the fact that you can assign different variables.
366
-
367
- `myCollection.documents type: "path"`
368
-
369
- Type can be "path", "id" or "key" in relation what we wish to have. If not specified we will receive an array of ArangoDocument instances.
370
-
371
- `myCollection.allDocuments skip: 3, limit: 100, batchSize: 10`
372
-
373
- It means that we skip the first three Documents, we can retrieve the next 100 Documents but we return only the first ten.
374
-
375
- To retrieve specific Document you can use:
376
-
377
- ``` ruby
378
- myCollection.documentsMatch match: {"value" => 4} # All Documents of the Collection with value equal to 4
379
- myCollection.documentMatch match: {"value" => 4} # The first Document of the Collection with value equal to 4
380
- myCollection.documentByKeys keys: ["4546", "4646"] # Documents of the Collection with the keys in the Array
381
- myCollection.random # A random Document of the Collection
382
- ```
383
-
384
- ### Modify the Collection
385
-
386
- ``` ruby
387
- myCollection.load # Load the Collection
388
- myCollection.unload # Unload the Collection
389
- myCollection.change waitForSync: true # Change a property of the Collection
390
- myCollection.rename "myCollectionC" # Rename the Collection
391
- myCollection.rotate # Rotate journal of a collection
392
- ```
393
-
394
- ### Import and Export Documents
395
-
396
- For the standard way to import one or more Documents (or Edges) we refer to the [dedicated ArangoDocument section](#create_doc).
397
- However it is possible to import a huge quantity of documents in a Collection with only one requests with the command import.
398
-
399
- <strong>Import one Document with Array</strong>
400
- I import one document with the following structure {"value": "uno", "num": 1, "name": "ONE"}.
401
- ``` ruby
402
- attributes = ["value", "num", "name"]
403
- values = ["uno",1,"ONE"]
404
- myCollection.import attributes: attributes, values: values
405
- ```
406
-
407
- <strong>Import more Documents with Array</strong>
408
- I import three Documents with the following structure {"value": "uno", "num": 1, "name": "ONE"}, {"value": "due", "num": 2, "name": "TWO"}, {"value": "tre", "num": 3, "name": "THREE"}.
409
- ``` ruby
410
- attributes = ["value", "num", "name"]
411
- values = [["uno",1,"ONE"],["due",2,"TWO"],["tre",3,"THREE"]]
412
- myCollection.import attributes: attributes, values: values
413
- ```
414
-
415
- <strong>Import more Documents with JSON</strong>
416
- I import two Documents with the following structure {"value": "uno", "num": 1, "name": "ONE"}, {"value": "due", "num": 2, "name": "TWO"}.
417
- ``` ruby
418
- body = [{"value": "uno", "num": 1, "name": "ONE"}, {"value": "due", "num": 2, "name": "DUE"}]
419
- myCollection.importJSON body: body
420
- ```
421
-
422
- As it is possible to import files, it is possible to export all the Document of a Collection with the following command.
423
- ``` ruby
424
- myCollection.export
425
- ```
426
-
427
- Alternatively it is possible to retrieve all the Documents in a Collection gradually.
428
-
429
- ``` ruby
430
- myCollection.export batchSize: 3 # First three Documents of the Collection
431
- myCollection.exportNext # Next three Documents
432
- myCollection.exportNext # Next three Documents
433
- ```
434
-
435
- ### Other operations
436
-
437
- ``` ruby
438
- myCollection.removeByKeys keys: ["4546", "4646"] # Documents of the Collection with the keys in the Array will be removed
439
- myCollection.removeMatch match: {"value" => 4} # All Documents of the Collection with value equal to 4 will be removed
440
- myCollection.replaceMatch match: {"value" => 4}, newValue: {"value" => 6} # All Documents of the Collection with value equal to 4 will be replaced with the new Value
441
- myCollection.updateMatch match: {"value" => 4}, newValue: {"value" => 6} # All Documents of the Collection with value equal to 4 will be updated with the new Value
442
- ```
443
-
444
- <a name="ArangoDocument"></a>
445
- ## ArangoDocument
446
-
447
- An Arango Document is an element of a Collection. Edges are documents with "\_from" and "\_to" in their body.
448
- You can create an ArangoCollection instance in one of the following way:
449
-
450
- ``` ruby
451
- myDocument = ArangoDocument.new database: "MyDatabase", collection: "MyCollection", key: "myKey"
452
- myDocument = ArangoDocument.new collection: "MyCollection", key: "myKey" # Using default Database
453
- myDocument = ArangoDocument.new key: "myKey" # Using default Collection and Database
454
- myDocument = ArangoDocument.new # Using default Collection and Database and I don't want to define a key for my Instance
455
- ```
456
-
457
- In the case you want to define a Edge, it is convenient to introduce the parameters "from" and "to" in the instance.
458
-
459
- ``` ruby
460
- myEdge = ArangoDocument.new from: myDocA, to: myDocB
461
- ```
462
-
463
- where myDocA and myDocB are the IDs of two Documents or are two ArangoDocument instances.
464
- During the instance, it is possible to define a Body for the Document.
465
-
466
- ``` ruby
467
- myDocument = ArangoDocument.new body: {"value" => 17}
468
- ```
469
-
470
- <a name="create_doc"></a>
471
- ### Create one or more Documents
472
-
473
- ArangoRB provides several way to create a single Document.
474
-
475
- ``` ruby
476
- myDocument.create
477
- myCollection.create_document document: myDocument # myDocument is an ArangoDocument instance or a Hash
478
- ArangoDocument.create body: {"value" => 17}, collection: myDocument
479
- ```
480
-
481
- Or more Documents.
482
-
483
- ``` ruby
484
- myCollection.create_document document: [myDocumentA, myDocumentB, {"value" => 17}] # Array of ArangoDocument instances and Hashes
485
- ArangoDocument.create body: [myDocumentA, {"value" => 18}, {"value" => 3}], collection: myDocument # Array of ArangoDocument instances and Hash
486
- ```
487
-
488
- ### Create one or more Edges
489
-
490
- ArangoRB has different way to create one or multiple edges. Here some example:
491
-
492
- ``` ruby
493
- myEdge = ArangoDocument.new from: myDocA, to: myDocB; myEdge.create
494
- myEdge.create_edge from: myDocA, to: myDocB # myDocA and myDocB are ArangoDocument ids or ArangoDocument instances
495
- myEdgeCollection.create_edge document: myEdge, from: myDocA, to: myDocB
496
- ArangoDocument.create_edge body: {"value" => 17}, from: myDocA, to: myDocB, collection: myEdgeCollection
497
- ```
498
-
499
- Further we have the possibility to create different combination of Edges in only one line of code
500
-
501
- One-to-one with one Edge class
502
-
503
- * [myDocA] --(myEdge)--> [myDocB]
504
-
505
- ``` ruby
506
- myEdgeCollection.create_edge document: myEdge, from: myDocA, to: myDocB
507
- ```
508
-
509
- One-to-more with one Edge class (and More-to-one with one Edge class)
510
-
511
- * [myDocA] --(myEdge)--> [myDocB]
512
- * [myDocA] --(myEdge)--> [myDocC]
513
-
514
- ``` ruby
515
- myEdgeCollection.create_edge document: myEdge, from: myDocA, to: [myDocB, myDocC]
516
- ```
517
-
518
- More-to-More with one Edge class
519
-
520
- * [myDocA] --(myEdge)--> [myDocC]
521
- * [myDocB] --(myEdge)--> [myDocC]
522
- * [myDocA] --(myEdge)--> [myDocD]
523
- * [myDocB] --(myEdge)--> [myDocD]
524
-
525
- ``` ruby
526
- myEdgeCollection.create_edge document: myEdge, from: [myDocA, myDocB], to: [myDocC, myDocD]
527
- ```
528
-
529
- More-to-More with more Edge classes
530
-
531
- * [myDocA] --(myEdge)--> [myDocC]
532
- * [myDocB] --(myEdge)--> [myDocC]
533
- * [myDocA] --(myEdge)--> [myDocD]
534
- * [myDocB] --(myEdge)--> [myDocD]
535
- * [myDocA] --(myEdge2)--> [myDocC]
536
- * [myDocB] --(myEdge2)--> [myDocC]
537
- * [myDocA] --(myEdge2)--> [myDocD]
538
- * [myDocB] --(myEdge2)--> [myDocD]
539
-
540
- ``` ruby
541
- myEdgeCollection.create_edge document: [myEdge, myEdge2], from: [myDocA, myDocB], to: [myDocC, myDocD]
542
- ```
543
-
544
- ### Destroy a Document
545
-
546
- ``` ruby
547
- myDocument.destroy
548
- ```
549
-
550
- ### Retrieve information
551
-
552
- ``` ruby
553
- myDocument.retrieve # Retrieve Document
554
- myDocument.collection # Retrieve Collection of the Document
555
- myDocument.database # Retrieve Database of the Document
556
- myDocument.retrieve_edges collection: myEdgeCollection # Retrieve all myEdgeCollection edges connected with the Document
557
- myDocument.any(myEdgeCollection) # Retrieve all myEdgeCollection edges connected with the Document
558
- myDocument.in(myEdgeCollection) # Retrieve all myEdgeCollection edges coming in the Document
559
- myDocument.out(myEdgeCollection) # Retrieve all myEdgeCollection edges going out the Document
560
- myEdge.from # Retrieve the document at the begin of the edge
561
- myEdge.to # Retrieve the document at the end of the edge
562
- ```
563
-
564
- #### Example: how to navigate the edges
565
-
566
- Think for example that we have the following schema:
567
- * A --[class: a, name: aa]--> B
568
- * A --[class: a, name: bb]--> C
569
- * A --[class: b, name: cc]--> D
570
- * B --[class: a, name: dd]--> E
571
-
572
- Then we have:
573
-
574
- * A.retrieve is A
575
- * A.retrieve_edges(collection: a) is [aa, bb]
576
- * B.any(a) is [aa, dd]
577
- * B.in(a) is [aa]
578
- * B.out(a) is [dd]
579
- * aa.from is A
580
- * aa.to is B
581
-
582
- We can even do some combinations: for example A.out(a)[0].to.out(a)[0].to is E since:
583
- * A.out(a) is [aa]
584
- * A.out(a)[0] is aa
585
- * A.out(a)[0].to is B
586
- * A.out(a)[0].to.out(a) is [dd]
587
- * A.out(a)[0].to.out(a)[0] is dd
588
- * A.out(a)[0].to.out(a)[0].to is E
589
-
590
- ### Modify
591
-
592
- ``` ruby
593
- myDocument.update body: {"value" => 3} # We update or add a value
594
- myDocument.replace body: {"value" => 3} # We replace a value
595
- ```
596
-
597
- <a name="ArangoGraph"></a>
598
- ## ArangoGraph
599
-
600
- ArangoGraph are used to manage Graphs. You can create an ArangoGraph instance in one of the following way.
601
-
602
- ``` ruby
603
- myGraph = ArangoGraph.new database: "MyDatabase", graph: "MyGraph"
604
- myGraph = ArangoGraph.new graph: "MyGraph" # By using the default Database
605
- myGraph = ArangoGraph.new # By using the defauly Database and Graph
606
- ```
607
-
608
- ### Create, Retrieve and Destroy a Graph
609
-
610
- ``` ruby
611
- myGraph.create # create a new Graph
612
- myGraph.retrieve # retrieve the Graph
613
- myGraph.database # retrieve database of the Graph
614
- myGraph.destroy # destroy the Graph
615
- ```
616
-
617
- ### Manage Vertex Collections
618
-
619
- ``` ruby
620
- myGraph.vertexCollections # Retrieve all the vertexCollections of the Graph
621
- myGraph.addVertexCollection collection: "myCollection" # Add a Vertex Collection to our Graph
622
- myGraph.removeVertexCollection collection: "myCollection" # Remove a Vertex Collection to our Graph
623
- ```
624
-
625
- ### Manage Edge Collections
626
-
627
- ``` ruby
628
- myGraph.edgeCollections # Retrieve all the edgeCollections of the Graph
629
- myGraph.addEdgeCollections collection: "myEdgeCollection", from: "myCollectionA", to: "myCollectionB" # Add an Edge Collection to our Graph
630
- myGraph.replaceEdgeCollections collection: "myEdgeCollection", from: "myCollectionA", to: "myCollectionB" # Replace an Edge Collection to our Graph
631
- myGraph.removeEdgeCollections collection: "myEdgeCollection" # Remove an Edge Collection to our Graph
632
- ```
633
-
634
- <a name="ArangoVertex"></a><a name="ArangoEdge"></a>
635
- ## ArangoVertex and ArangoEdge
636
-
637
- Both these two classes inherit the class ArangoDocument.
638
- These two classes have been created since ArangoDatabase offers, in connection of the chosen graph, different HTTP requests to manage Vertexes and Edges. We recommend the reader to read carefully the section on [ArangoDocument instances](#ArangoDocument) before to start to use ArangoVertex and ArangoEdge instances.
639
-
640
- ### ArangoVertex methods
641
-
642
- ArangoVertex inherit all the methods of ArangoDocument class. The following one works similar to the one of ArangoDocument Class but use different HTTP requests. For this reason the performance could be different.
643
- To use ArangoVertex, the Collection of the Vertex needs to be added either to the VertexCollections or to the EdgeCollections of the chosen Graph.
644
-
645
- ``` ruby
646
- myVertex = ArangoVertex.new key: "newVertex", body: {"value" => 3}, collection: "myCollection", graph: "myGraph", database: "myDatabase" # create a new instance
647
- myVertex.create # create a new Document in the Graph
648
- myVertex.retrieve # retrieve a Document
649
- myVertex.graph # Retrieve Graph of the Document
650
- myVertex.replace body: {"value" => 6} # replace the Document
651
- myVertex.update body: {"value" => 6} # update the Document
652
- myVertex.destroy # delete the Document
653
- ```
654
-
655
- ### ArangoEdge methods
656
-
657
- ArangoEdge inherit all the methods of ArangoDocument class. The following one works similar to the one of ArangoDocument Class but use a different HTTP request. For this reason the performance could be different.
658
- To use ArangoEdge, the Collection of the Edge needs to be added to the EdgeCollections of the chosen Graph.
659
-
660
- ``` ruby
661
- myEdge = ArangoEdge.new key: "newVertex", body: {"value" => 3}, from: myArangoDocument, to: myArangoDocument, collection: "myCollection", graph: "myGraph", database: "myDatabase" # create a new instance
662
- myEdge.create # create a new Document of type Edge in the Graph
663
- myEdge.retrieve # retrieve a Document
664
- myEdge.graph # Retrieve Graph of the Document
665
- myEdge.replace body: {"value" => 6} # replace the Document
666
- myEdge.update body: {"value" => 6} # update the Document
667
- myEdge.destroy # delete the Document
668
- ```
669
-
670
- <a name="ArangoTraversal"></a>
671
- ## ArangoTraversal
672
-
673
- ArangoTraversal is used to administrate the traversals.
674
- ArangoTraversal needs to know the vertex from where the traversal starts, the direction the traversal is going and either the Graph or the EdgeCollection we want to analize.
675
-
676
- ``` ruby
677
- myTraversal = ArangoTraversal.new # create new ArangoTraversal
678
- myTraversal.vertex = myVertex # define starting Vertex
679
- myTraversal.graph = myGraph # define used Graph
680
- myTraversal.edgeCollection = myEdgeCollection # define used Edge
681
- myTraversal.in # Direction is in
682
- myTraversal.out # Direction is out
683
- myTraversal.any # Direction is in and out
684
- myTraversal.min = 1 # Define how minimum deep we want to go with the traversal
685
- myTraversal.max = 3 # Define how maximum deep we want to go with the traversal
686
- ```
687
-
688
- After the traversal is setup, you can execute it:
689
-
690
- ``` ruby
691
- myTraversal.execute
692
- ```
693
-
694
- <a name="arangoaql"></a>
695
- ## ArangoAQL - ArangoDatabase Query Language
696
-
697
- ArangoAQL is used to manage the ArangoDB query language. To instantiate a query
698
-
699
- ``` ruby
700
- myQuery = ArangoAQL.new query: "FOR v,e,p IN 1..6 ANY 'Year/2016' GRAPH 'MyGraph' FILTER p.vertices[1].num == 6 && p.vertices[2].num == 22 && p.vertices[6]._key == '424028e5-e429-4885-b50b-007867208c71' RETURN [p.vertices[4].value, p.vertices[5].data]"
701
- ```
702
-
703
- To execute it use:
704
- ``` ruby
705
- myQuery.execute
706
- ```
707
-
708
- If the query is too big, you can divide the fetching in pieces, for example:
709
- ``` ruby
710
- myQuery.size = 10
711
- myQuery.execute # First 10 documents
712
- myQuery.next # Next 10 documents
713
- myQuery.next # Next 10 documents
714
- ```
715
-
716
- ### Check property query
717
-
718
- ``` ruby
719
- myQuery.explain # Show data query
720
- myQuery.parse # Parse query
721
- myQuery.properties; # Check properties
722
- myQuery.current; # Retrieve current Query
723
- myQuery.slow; # Retrieve slow Queries
724
- myQuery.changeProperties maxSlowQueries: 65 # Change Properties
725
- ```
726
-
727
- ### Delete query
728
-
729
- ``` ruby
730
- myQuery.stopSlow; # Stop Slow query
731
- myQuery.kill; # Kill Query
732
- ```
733
-
734
- <a name="ArangoUser"></a>
735
- ## ArangoUser
736
-
737
- ArangoUser manages the users.
738
- To initialize an user:
739
-
740
- ``` ruby
741
- myUser = ArangoUser.new user: "MyUser", password: "password"
742
- ```
743
-
744
- ### User management
745
-
746
- ``` ruby
747
- myUser.retrieve # Retrieve User
748
- myUser["MyDatabase"] # Retrieve database if the user can access it
749
- myUser.create # Create a new User
750
- myUser.replace active: false # Replace User
751
- myUser.update active: false # Update User
752
- myUser.destroy # Delete User
753
- ```
754
-
755
- ### Database management
756
-
757
- ``` ruby
758
- myUser.databases # Check permission Databases
759
- myUser.grant database: "MyDatabase" # Grant access to a database
760
- myUser.revoke database: "MyDatabase" # Revoke access to a database
761
- ```
762
-
763
- <a name="ArangoIndex"></a>
764
- ## ArangoIndex
765
-
766
- ArangoIndex manages the indexes.
767
- To initialize an index:
768
-
769
- ``` ruby
770
- myIndex = ArangoIndex.new fields: "num", unique: false, id: "myIndex"
771
- ```
772
-
773
- ### Index management
774
-
775
- ``` ruby
776
- myIndex.retrieve # Retrieve Index
777
- myIndex.create # Create a new Index
778
- ArangoIndex.indexes collection: "MyCollection" # List indexes
779
- myIndex.destroy # Delete Index
780
- ```
781
-
782
- Alternatively, you can create an Index of a Collection directly from its Collection.
783
-
784
- ``` ruby
785
- myCollection.createIndex unique: false, fields: "num", type: "hash"
786
- ```
787
-
788
- <a name="ArangoTransaction"></a>
789
- ## ArangoTransaction
790
-
791
- Transactions are managed by ArangoTransaction. This class has only initialization and execution.
792
-
793
- ``` ruby
794
- myArangoTransaction = ArangoTransaction.new action: "function(){ var db = require('@arangodb').db; db.MyCollection.save({}); return db.MyCollection.count(); }", write: myCollection # Or read
795
- myArangoTransaction.execute # Return the result of the execution
796
- ```
797
-
798
- <a name="ArangoTask"></a>
799
- ## ArangoTask
800
-
801
- Tasks are managed by ArangoTask.
802
-
803
- ``` ruby
804
- myArangoTask = ArangoTask.new id: "mytaskid", name: "MyTaskID", command: "(function(params) { require('@arangodb').print(params); })(params)", params: {"foo" => "bar", "bar" => "foo"}, period: 2 # Initialize a Task Instance that will be alive for 2 seconds
805
- myArangoTask.create # Create a new Task
806
- ArangoTask.tasks # Retrieve a list of active tasks
807
- myArangoTask.retrieve # Retrieve a Task
808
- myArangoTask.destroy # Delete a Task
809
- ```
810
-
811
- <a name="ArangoCache"></a>
812
- ## ArangoCache
813
-
814
- ArangoCache helps you to manage your request to your Database by creating a cache.
815
-
816
- ``` ruby
817
- myQuery = ArangoAQL.new query: "FOR v,e,p IN 1..6 ANY 'Year/2016' GRAPH 'MyGraph' FILTER p.vertices[1].num == 6 && p.vertices[2].num == 22 && p.vertices[6]._key == '424028e5-e429-4885-b50b-007867208c71' RETURN [p.vertices[4].value, p.vertices[5].data]"
818
- myQuery.execute # Heavy computation
819
- ArangoCache.cache data: myQuery # Cache these heavy query
820
- ArangoCache.uncache data: myQuery # Retrieve cached ArangoAQL with same query request
821
- ArangoCache.clear data: myQuery # Free the cache from these three documents
822
- ArangoCache.clear type: "AQL" # Delete cache from AQL requests
823
- ArangoCache.clear # Clear completely all the cache
824
- ```
825
-
826
- Alternatively we can save, retrieve and delete multiple values
827
-
828
- ``` ruby
829
- myQuery = ArangoAQL.new query: "FOR v,e,p IN 1..6 ANY 'Year/2016' GRAPH 'MyGraph' FILTER p.vertices[1].num == 6 && p.vertices[2].num == 22 && p.vertices[6]._key == '424028e5-e429-4885-b50b-007867208c71' RETURN [p.vertices[4].value, p.vertices[5].data]"
830
- myQuery2 = ArangoAQL.new query: "FOR u IN Hour FILTER u._key == "2016-10-04T23" RETURN u"
831
- myQuery.execute # Heavy computation
832
- myQuery2.execute
833
- ArangoCache.cache data: [myQuery, myQuery2] # Cache these heavy query
834
- ArangoCache.uncache data: [myQuery, myQuery2] # Retrieve cached ArangoAQL
835
- ArangoCache.clear data: [myQuery, myQuery2] # Free the cache from these request
836
- ```
837
-
838
- If we need we can save with personalized ID.
839
-
840
- ``` ruby
841
- ArangoCache.cache id: ["myFirstQuery", "mySecondQuery"] data: [myQuery, myQuery2] # Cache these heavy query
842
- ArangoCache.uncache type: "AQL", id: ["myFirstQuery", "mySecondQuery"] # Retrieve cached ArangoAQL
843
- ArangoCache.clear type: "AQL", id: ["myFirstQuery", "mySecondQuery"] # Free the cache from these request
844
- ```
845
-
846
- The type and the quantity that you can save in the cache are the following: Database: 1, Collection: 20, Document: 200, Graph: 1, Vertex: 50, Edge: 100, Index: 20, AQL: 100, User: 50, Task: 20, Traversal: 20, Transaction: 20, Other: 100. For "Other" we mean all the values that are not included in the other categories.
847
-
848
- To modify these limitations you can use the following command:
849
- ``` ruby
850
- ArangoCache.max type: "Document", val: 100 # Change limits Document
851
- ```
852
- NB: If you insert a max value higher than the quantity of elements in the Cache, then the first elements in excess will be removed from the Cache.
853
-
854
- If the limit of the Cache for one type is reached, then the first element cached of that type will be deleted from the Cache.
855
-
856
- <a name="ArangoReplication"></a>
857
- ## Replication
858
-
859
- Replication is useful to create back up copy of your database or to have a master-slave relationship between two databases.
860
-
861
- Remember: the used database is the one where the data will be written (the slave) and the remote database will be the master one.
862
-
863
- Use with caution since the data in the slave database will be deleted.
864
-
865
- To setup our Slave Server and Master Database use a similar command.
866
-
867
- ``` ruby
868
- ArangoServer.default_server user: "root", password: "tretretre", server: "172.17.8.101", port: "8529" # Our Slave Server
869
- myReplication = ArangoReplication.new endpoint: "tcp://10.10.1.97:8529", username: "root", password: "", database: "year" # Our Master Database
870
- ```
871
-
872
- Than to do a simple syncronization uses;
873
-
874
- ``` ruby
875
- myReplication.sync
876
- ```
877
-
878
- To retrieve some information ArangoRB provides the following methods:
879
-
880
- ``` ruby
881
- myDatabase = ArangoDatabase.new database: "year"
882
- myCollection = myDatabase["MyCollection"]
883
- myDatabase.inventory # Fetch Collection data
884
- myCollection.dump # Fetch all the data in one class from one tick to another
885
- myReplication.logger # Returns the current state of the server's replication logger
886
- myReplication.loggerFollow # Returns data from the server's replication log.
887
- myReplication.firstTick # Return the first available tick value from the server
888
- myReplication.rangeTick # Returns the currently available ranges of tick values for all currently available WAL logfiles.
889
- myReplication.serverId # Returns the servers id.
890
- ```
891
-
892
- ### Relation Master-Slave
893
-
894
- To enslave a Server in relation to another Database use the following command:
895
-
896
- ``` ruby
897
- myReplication.enslave
898
- ```
899
-
900
- To manage the Configuration of a Master-Slave Replication you can use the following commands:
901
-
902
- ``` ruby
903
- myReplication.configurationReplication # check the Configuration of the Replication
904
- myReplication.stateReplication # check the status of the Replication
905
- myReplication.stopReplication # stop the Replication
906
- myReplication.modifyReplication # modify the Configuration of the Replication (you can modify only a stopped Replication)
907
- myReplication.startReplication # restart the replication
908
- ```
1
+ ArangoRB [![Gem Version](https://badge.fury.io/rb/arangorb.svg)](https://badge.fury.io/rb/arangorb)
2
+ ===============================
3
+
4
+ [ArangoDatabase](https://www.arangodb.com/) is a native multi-model database with flexible data models for document, graphs, and key-values.
5
+ ArangoRB is a Gem to use ArangoDatabase with Ruby. ArangoRB is based on the [HTTP API of ArangoDB](https://docs.arangodb.com/3.0/HTTP/index.html).
6
+
7
+ ArangoRB 0.1.0 - 1.3.0 have been tested with ArangoDB 3.0 with Ruby 2.3.1</br>
8
+ ArangoRB 1.4.0 has been tested with ArangoDB 3.1 with Ruby 2.3.3</br>
9
+ ArangoRB 2.0.0 has been tested with ArangoDB 3.4 with Ruby 2.3.3</br>
10
+
11
+ It requires the gems "HTTParty", "Oj" and "connection_pool"</br>
12
+
13
+ To install ArangoRB: `gem install arangorb`
14
+
15
+ To use it in your application: `require "arangorb"`
16
+
17
+ ## Differences between version 1.4 and 2.0
18
+
19
+ ArangoRB 1.4 and ArangoRB 2.0 are not compatible.
20
+ The new version provide different improvements.
21
+ * Now all the instances are in a module Arango. This means that previous classes like Arango::Server will become Arango::Server.
22
+ * Arango::Server is now an instance. This means that ArangoRB 2.0 will permits to use different servers.
23
+ * ArangoRB has implemented connection_pool
24
+ * ArangoRB has now a better implementation of Cache
25
+ * The returned values are returned with symbols as keys
26
+ * Oj is used for parsing instead of JSON
27
+ * Add supports for View and Foxx. The least is untested.
28
+ * Better hierarchy handling between the different instances. For example now myDocument.database will return the database instance that includes the document.
29
+ * Better handling of replication (untested)
30
+ * Clean in general the code (for future easier maintance)
31
+ * Now errors are handled by Arango::Error
32
+
33
+ ## Support
34
+
35
+ This is a project managed by one single person. ArangoDB is a wonderful project that it is hard to cover completely alone. For this reason all your supports is more than welcome.
36
+ Any pull request, issue, suggestions and ideas are more than welcome. Do not be shy to contact me, create issues on Github or pushing changes.
37
+
38
+ Here something the community can help on:
39
+ * Add test and correct bugs for replication, clustering, foxx and other.
40
+ * Improve AQL instance to implement in Rails.
41
+ * Improve Documentation.
42
+
43
+ If you like this project, please star it. It will remind me that my work has been useful for somebody.
44
+
45
+ ## Classes used
46
+
47
+ ArangoRB has the two type of classes.
48
+
49
+ Classes relative to ArangoDB elements:
50
+ * [Arango::Server](#ArangoServer): to manage a Server
51
+ * [Arango::Database](#ArangoDatabase): to manage a Database
52
+ * [Arango::Collection](#ArangoCollection): to manage a Collection
53
+ * [Arango::Document](#ArangoDocument): to manage a Document
54
+ * [Arango::Vertex](#ArangoVertex): to manage a Vertex
55
+ * [Arango::Edge](#ArangoEdge): to manage an Edge
56
+ * [Arango::Graph](#ArangoGraph): to manage a Graph
57
+ * [Arango::Traversal](#ArangoTraversal): to manage a Traversal operation
58
+ * [Arango::AQL](#arangoaql): to manage an AQL instances
59
+ * [Arango::User](#ArangoUser): to manage an User
60
+ * [Arango::Index](#ArangoIndex): to manage an Index
61
+ * [Arango::Task](#ArangoTask): to manage a Task
62
+ * [Arango::Transaction](#ArangoTransaction): to manage a Transaction
63
+ * [Arango::Replication](#ArangoReplication): to manage a Replication
64
+ * [Arango::Batch](#ArangoBatch): to manage a Batch of multiple requests
65
+ * [Arango::Foxx](#ArangoFoxx): to manage a Foxx instance
66
+ * [Arango::View](#ArangoView): to manage a View instance
67
+
68
+ Classes relative to the Gem ArangoRB
69
+ * [Arango::Cache](#ArangoCache): to manage internal Cache
70
+ * [Arango::Error](#ArangoError): to handle ArangoRB errors
71
+
72
+ All the instances of these classes can be transformed in Hash with the method to_h.
73
+
74
+ Many methods can have multiple attributes not mentioned in this documentation. Keep in mind that if an attribute is defined in the ArangoDB documentation, then it is easy that it is an attribute for the relative method too.
75
+ Please refer to the ArangoDB documentation, the rubydoc documentation or open an issue for this missing attributes.
76
+
77
+ <a name="ArangoServer"></a>
78
+ ## Arango::Server
79
+
80
+ Arango::Server is used to manage a single server by managing the connection with ArangoDB.
81
+ You can provide your login credentials and it is a mandatory step to start your database.
82
+
83
+ To setup a server use the following way:
84
+
85
+ ``` ruby
86
+ server = Arango::Server.new username: "MyUsername", password: "MyPassword",
87
+ server: "localhost", port: "8529"
88
+ server.username = "MyOtherUsername" # Default "root"
89
+ server.password = "other_password"
90
+ server.server = "127.0.0.1" # Default "localhost"
91
+ server.port = "8765" # Default "8529"
92
+ ```
93
+
94
+ Password is a mandatory field.
95
+
96
+ ### Returning results
97
+
98
+ ArangoRB try always to store the information obtained from ArangoDB in an instance.
99
+ If you need to receive the output, you can return it with:
100
+
101
+ ``` ruby
102
+ server.return_output = true # Default false
103
+ ```
104
+
105
+ ### Verbose and warnings
106
+
107
+ For Debugging reasons the user can print out the request and response to ArangoDB by setting verbose to true.
108
+
109
+ ``` ruby
110
+ server.verbose = true # Default false
111
+ ```
112
+
113
+ Remember that verbose is only for testing reason: to work efficiently verbose should be false.
114
+
115
+ Some deprecated methods will return a warning. To silence these warnings use:
116
+
117
+ ``` ruby
118
+ server.warning = false # Default true
119
+ ```
120
+
121
+ ### Connection Pool
122
+
123
+ ArangoRB supports connection pool, to activate it you can setup pool to true during the initialization or the proceedings. To do so use:
124
+
125
+ ``` ruby
126
+ server = Arango::Server.new username: "MyUsername", password: "MyPassword",
127
+ server: "localhost", port: "8529", pool: true, size: 5, timeout: 5
128
+ server.pool = true # Defult false
129
+ server.size = 7 # Default 5
130
+ server.timeout = 10 # Default 5
131
+ server.restartPool # Restart pool with new size and timeout
132
+ ```
133
+
134
+ NB: ConnectionPool is not heavily tested.
135
+
136
+ ### Cache
137
+
138
+ Often ArangoRB returns multiple time the same object (for example for lists or for retrieving). It can happens thus that a document instance updated somewhere in your code, it is not update somewhere even if you are refering the same ArangoDB document.
139
+ To avoid this, ArangoRB provides its own cache.
140
+
141
+ You can activate the ArangoRB cache by using:
142
+
143
+ ``` ruby
144
+ server.active_cache = true # Default false
145
+ ```
146
+
147
+ If active_cache is true, then a previous document or collection instance will be stored in the ArangoRB cache. In case a new instance of the same document is created, then a new instance will NOT be created but, instead, the old one will be returned.
148
+
149
+ With an example:
150
+
151
+ ``` ruby
152
+ server.active_cache = false
153
+ a = Arango::Document.new name: "test", collection: my_collection
154
+ b = Arango::Document.new name: "test", collection: my_collection
155
+ # In this case a and b are two different instances
156
+ ```
157
+
158
+ ``` ruby
159
+ server.active_cache = true
160
+ a = Arango::Document.new name: "test", collection: my_collection
161
+ b = Arango::Document.new name: "test", collection: my_collection
162
+ # In this case a and b are the same instance
163
+ ```
164
+
165
+ Note that if you set server.active_cache to false, then the stored Cache will be emptied.
166
+ For more information about the cache, look at the section about [Arango::Cache](#ArangoCache).
167
+
168
+ ### Information
169
+
170
+ Basic information can be retrieved with these command.
171
+
172
+ ``` ruby
173
+ server.to_h # Return an hash of the instances
174
+ server.endpoint # Check address used to connect with the server
175
+ server.username # Check name used to connect with the server
176
+ server.verbose # Check if verbose is true or false
177
+ server.async # Check the status of async
178
+ ```
179
+
180
+ To retrieve lists
181
+
182
+ ``` ruby
183
+ server.databases # Lists of available databases
184
+ server.endpoints # Lists of endpoints used
185
+ server.users # Lists of available users
186
+ server.tasks # Lists of available tasks
187
+ ```
188
+
189
+ To monitoring the server you can use the following commands
190
+
191
+ ``` ruby
192
+ server.log # Return log files
193
+ server.loglevel
194
+ server.updateLoglevel body: body
195
+ server.available? # Reloads the routing information from the collection routing.
196
+ server.reload # Reloads the routing information from the collection routing.
197
+ server.statistics # Returns the statistics information
198
+ server.statisticsDescription # Fetch descriptive info of statistics
199
+ server.status # Status of the server
200
+ server.role # Get to know whether this server is a Coordinator or DB-Server
201
+ server.serverData # Get server data
202
+ server.mode # Get server mode
203
+ server.updateMode(mode: "default") # Change mode of the server
204
+ ```
205
+
206
+ ### Manage Async
207
+
208
+ With Arango::Server you can manage Async results. It can be useful to use async for heavy requests (like the one in example/year.rb).
209
+
210
+ ``` ruby
211
+ server.async = false # default
212
+ server.async = true # fire and forget
213
+ server.async = :store # fire and store
214
+ ```
215
+
216
+ If Async is "store", then the commands of ArangoRB will return the id of the Async requests.
217
+ Arango::Server provides different methods to manage these Async requests.
218
+
219
+ ``` ruby
220
+ server.fetchAsync id: id # Fetches a job result and removes it from the queue
221
+ server.cancelAsync id: id # Cancels an async job
222
+ server.destroyAsync id: id # Deletes an async job result
223
+ server.retrieveAsync id: id # Returns the status of a specific job
224
+ server.retrieveAsyncByType type: type # Returns the ids of job results with a specific
225
+ # status. Type can be "done" or "pending"
226
+ server.retrieveDoneAsync # Equivalent to server.retrieveAsync type: "done"
227
+ server.retrievePendingAsync # Equivalent to server.retrieveAsync type: "pending"
228
+ server.destroyAsync type: type # Deletes async jobs with a specific status
229
+ # Type can be "all" or "expired"
230
+ server.destroyAllAsync # Equivalent to server.destroyAsync type: "all"
231
+ server.destroyExpiredAsync # Equivalent to server.destroyAsync type: "expired"
232
+ ```
233
+
234
+ ### Miscellaneous
235
+
236
+ ``` ruby
237
+ server.version # Returns the server version number
238
+ server.engine # Returns the server engine
239
+ server.flushWAL # Flushes the write-ahead log
240
+ server.propertyWAL # Retrieves the configuration of the write-ahead log
241
+ server.changePropertyWAL # Configures the write-ahead log
242
+ server.transactions # Returns information about the currently running transactions
243
+ server.time # Get the current time of the system
244
+ server.echo # Return current request
245
+ server.databaseVersion # Return the required version of the database
246
+ server.shutdown # Initiate shutdown sequence
247
+ ```
248
+
249
+ UNTESTED
250
+
251
+ ``` ruby
252
+ server.test body: body # Runs tests on server
253
+ server.execute body: body # Execute a script on the server.
254
+ ```
255
+
256
+ ### Cluster (UNTESTED)
257
+
258
+ ArangoDB permits the sharding of the database. Although these methods has not been tested with ArangoRB.
259
+
260
+ ``` ruby
261
+ server.clusterHealth port: port # Allows to check whether a given port is usable
262
+ server.serverId # Returns the id of a server in a cluster.
263
+ server.clusterStatistics dbserver: dbserver # Allows to query the statistics of a
264
+ # DBserver in the cluster
265
+ ```
266
+
267
+ <a name="ArangoDatabase"></a>
268
+ ## Arango::Database
269
+
270
+ Arango::Database is used to manage a Database. You can create an instance in one of the following ways:
271
+
272
+ ``` ruby
273
+ myDatabase = server.database name: "MyDatabase"
274
+ myDatabase = server["MyDatabase"]
275
+ myDatabase = Arango::Database.new database: "MyDatabase", server: server
276
+ ```
277
+
278
+ ### Main methods
279
+
280
+ ``` ruby
281
+ myDatabase.create # Create a new Database
282
+ myDatabase.retrieve # Retrieve database
283
+ myDatabase.destroy # Delete the selected Database
284
+ ```
285
+
286
+ ### Retrieve information
287
+
288
+ ``` ruby
289
+ server.databases # Obtain an Array with the available databases in the server
290
+ myDatabase.to_h # Hash of the instance
291
+ myDatabase.info # Obtain general info about the databases
292
+ myDatabase.sever # Return the server connected with the database
293
+ myDatabase.collections # Obtain an Array with the available collections in the selected Database
294
+ myDatabase.graphs # Obtain an Array with the available graphs in the selected Database
295
+ myDatabase.aqlFunctions # Obtain an Array with the available functions in the selected Database
296
+ myDatabase.foxxes # Return all the foxx available in the database
297
+ myDatabase.views # Return all the views available in the database
298
+ myDatabase.tasks # Return all the tasks available in the database
299
+ ```
300
+
301
+ <a name="arangoaql"></a>
302
+ ## Arango::AQL
303
+
304
+ An AQL instance can be created by using one of the following way:
305
+
306
+ ``` ruby
307
+ query = "FOR v,e,p IN 1..6 ANY 'Year/2016' GRAPH 'MyGraph' FILTER p.vertices[1].num == 6 && p.vertices[2].num == 22 && p.vertices[6]._key == '424028e5-e429-4885-b50b-007867208c71' RETURN [p.vertices[4].value, p.vertices[5].data]"
308
+ myQuery = myDatabase.aql query: query
309
+ myQuery = ArangoAQL.new database: myDatabase, query: query
310
+ ```
311
+
312
+ To execute it use:
313
+
314
+ ``` ruby
315
+ myQuery.execute
316
+ ```
317
+
318
+ If the query is too big, you can divide the fetching in pieces, for example:
319
+
320
+ ``` ruby
321
+ myQuery.size = 10
322
+ myQuery.execute # First 10 documents
323
+ myQuery.next # Next 10 documents
324
+ myQuery.next # Next 10 documents
325
+ ```
326
+
327
+ Other useful methods are the following
328
+
329
+ ``` ruby
330
+ myQuery.destroy # Destroy cursor to retrieve documents
331
+ myQuery.kill # Kill query request (if requires too much time)
332
+ myQuery.explain # Show data query
333
+ myQuery.parse # Parse query
334
+ ```
335
+
336
+ ### Query Properties
337
+
338
+ It is possible to handle generic properties of query by Arango::Database.
339
+
340
+ ``` ruby
341
+ myQuery.to_h # Return an hash version of the instance
342
+ myQuery.properties # Check Query properties
343
+ myQuery.current # Retrieve current running Query
344
+ myQuery.changeProperties maxSlowQueries: 65 # Change Properties
345
+ myQuery.slow # Retrieve slow Queries
346
+ myQuery.stopSlowQueries # Stop slow Queries
347
+ ```
348
+
349
+ The cache of the query can handle in the following way:
350
+
351
+ ``` ruby
352
+ myDatabase.retrieveQueryCache # Retrieve Query Cache
353
+ myDatabase.clearQueryCache # Clear Query Cache
354
+ myDatabase.propertyQueryCache # Check properties Cache
355
+ myDatabase.changePropertyQueryCache maxResults: 30 # Change properties Cache
356
+ ```
357
+
358
+ ### AQL Functions
359
+
360
+ AQL queries can be potentiate by providing javascript function as supports.
361
+
362
+ ``` ruby
363
+ myDatabase.createAqlFunction code: "function(){return 1+1;}", name: "myFunction" # Create a new AQL Function
364
+ myDatabase.deleteFunction name: "myFunction" # Delete an AQL function
365
+ myDatabase.aqlFunctions # Retrieve a list of the available aql functions
366
+ ```
367
+
368
+ <a name="ArangoCollection"></a>
369
+ ## Arango::Collection
370
+
371
+ Arango::Collection is used to manage your Collections. You can create an Arango::Collection instance in one of the following way:
372
+
373
+ ``` ruby
374
+ myCollection = myDatabase.collection name: "MyCollection"
375
+ myCollection = myDatabase["MyCollection"]
376
+ myCollection = Arango::Collection.new database: myDatabase, collection: "MyCollection"
377
+ ```
378
+
379
+ A Collection can be of two types: "Document" and "Edge". If you want to specify it, uses:
380
+
381
+ ``` ruby
382
+ myCollectionA = ArangoCollection.new collection: "MyCollectionA", type: :document # Default
383
+ myEdgeCollection = ArangoCollection.new collection: "MyCollectionB", type: :edge
384
+ ```
385
+
386
+ ### Main methods
387
+
388
+ ``` ruby
389
+ myCollection.create # Create collection
390
+ myCollection.destroy # Delete collection from database
391
+ myCollection.truncate # Delete all the Documents inside the selected Collection
392
+ myCollection.retrieve # Retrieve the selected Collection
393
+ ```
394
+
395
+ ### Info methods
396
+
397
+ ``` ruby
398
+ myCollection.database # Return database of the collection
399
+ myCollection.server # Returm server of the collection
400
+ myCollection.indexes # Return a list of all used Indexes in the Collection
401
+ myCollection.rotate # Rotate the collection
402
+ myCollection.data # Returns the whole content of one collection
403
+ myCollection.properties # Properties of the Collection
404
+ myCollection.count # Number of Documents in the Collection
405
+ myCollection.stats # Statistics of the Collection
406
+ myCollection.revision # Return collection revision id
407
+ myCollection.checksum # Return checksum for the Collection
408
+ ```
409
+
410
+ ### Modify the Collection
411
+
412
+ ``` ruby
413
+ myCollection.load # Load the collection (preparing for retrieving documents)
414
+ myCollection.unload # Unload the collection (if you stop to work with it)
415
+ myCollection.loadIndexesIntoMemory # Load indexes in memory
416
+ myCollection.change(waitForSync: true) # Change some properties
417
+ myCollection.rename(newName: "myCollection2") # Change name (NB: This is not Arango::Cache compatible)
418
+ myCollection.rotate # Rotate journal of a collection
419
+ ```
420
+
421
+ ### Handle documents
422
+
423
+ To retrieve all the documents of a Collection you can use:
424
+
425
+ ``` ruby
426
+ myCollection.documents
427
+ myCollection.allDocuments
428
+ ```
429
+
430
+ These two functions are similar except for the fact that you can assign different variables.
431
+
432
+ ``` ruby
433
+ myCollection.documents type: "path"
434
+ myCollection.next # Retrieve other documents if the first request is not finished
435
+ ```
436
+
437
+ Type can be "path", "id" or "key" in relation what we wish to have. If not specified ArangoRB will return an array of Arango::Document instances.
438
+
439
+ ``` ruby
440
+ myCollection.allDocuments skip: 3, limit: 100, batchSize: 10
441
+ ```
442
+
443
+ It means that we skip the first three Documents, we can retrieve the next 100 Documents but we return only the first ten.
444
+
445
+ To retrieve specific Document you can use:
446
+
447
+ ``` ruby
448
+ myCollection.documentsMatch match: {"value": 4} # All Documents of the Collection with value equal to 4
449
+ myCollection.documentMatch match: {"value": 4} # The first Document of the Collection with value equal to 4
450
+ myCollection.documentByKeys keys: ["4546", "4646"] # Documents of the Collection with the keys in the Array
451
+ myCollection.documentByName names: ["4546", "4646"] # Documents of the Collection with the name in the Array
452
+ myCollection.random # A random Document of the Collection
453
+ ```
454
+
455
+ #### Modifying multiple documents
456
+
457
+ From a collection is it possible to create, remove and modify multiple documents.
458
+
459
+ ``` ruby
460
+ myCollection.createDocuments document: [myDocumentA, myDocumentB, {"value": 17}] # Array of Arango::Document instances and Hashes
461
+ myCollection.removeByKeys keys: ["4546", "4646"] # Documents of the Collection with the keys in the Array will be removed
462
+ myCollection.removeMatch match: {"value": 4} # All Documents of the Collection with value equal to 4 will be removed
463
+ myCollection.replaceMatch match: {"value": 4}, newValue: {"value": 6} # All Documents of the Collection with value equal to 4 will be replaced with the new Value
464
+ myCollection.updateMatch match: {"value": 4}, newValue: {"value": 6} # All Documents of the Collection with value equal to 4 will be updated with the new Value
465
+ ```
466
+
467
+ #### Create multiple Edges at once
468
+
469
+ We have the possibility to create different combination of Edges in only one line of code.
470
+
471
+ One-to-one with one Edge class
472
+
473
+ * [myDocA] --(myEdge)--> [myDocB]
474
+
475
+ ``` ruby
476
+ myEdgeCollection.createEdges document: myEdge, from: myDocA, to: myDocB
477
+ ```
478
+
479
+ One-to-more with one Edge class (and More-to-one with one Edge class)
480
+
481
+ * [myDocA] --(myEdge)--> [myDocB]
482
+ * [myDocA] --(myEdge)--> [myDocC]
483
+
484
+ ``` ruby
485
+ myEdgeCollection.createEdges document: myEdge, from: myDocA, to: [myDocB, myDocC]
486
+ ```
487
+
488
+ More-to-More with one Edge class
489
+
490
+ * [myDocA] --(myEdge)--> [myDocC]
491
+ * [myDocB] --(myEdge)--> [myDocC]
492
+ * [myDocA] --(myEdge)--> [myDocD]
493
+ * [myDocB] --(myEdge)--> [myDocD]
494
+
495
+ ``` ruby
496
+ myEdgeCollection.createEdges document: myEdge, from: [myDocA, myDocB], to: [myDocC, myDocD]
497
+ ```
498
+
499
+ More-to-More with more Edge classes
500
+
501
+ * [myDocA] --(myEdge)--> [myDocC]
502
+ * [myDocB] --(myEdge)--> [myDocC]
503
+ * [myDocA] --(myEdge)--> [myDocD]
504
+ * [myDocB] --(myEdge)--> [myDocD]
505
+ * [myDocA] --(myEdge2)--> [myDocC]
506
+ * [myDocB] --(myEdge2)--> [myDocC]
507
+ * [myDocA] --(myEdge2)--> [myDocD]
508
+ * [myDocB] --(myEdge2)--> [myDocD]
509
+
510
+ ``` ruby
511
+ myEdgeCollection.createEdges document: [myEdge, myEdge2], from: [myDocA, myDocB], to: [myDocC, myDocD]
512
+ ```
513
+
514
+
515
+ ### Import and Export Documents
516
+
517
+ Another way to create multiple documents in a Collection with only one request is by using the method import.
518
+
519
+ #### Import one Document with Array
520
+
521
+ We can import one document with the following structure {"value": "uno", "num": 1, "name": "ONE"}.
522
+
523
+ ``` ruby
524
+ attributes = ["value", "num", "name"]
525
+ values = ["uno",1,"ONE"]
526
+ myCollection.import attributes: attributes, values: values
527
+ ```
528
+
529
+ #### Import more Documents with Array
530
+
531
+ We can import three Documents with the following structure {"value": "uno", "num": 1, "name": "ONE"}, {"value": "due", "num": 2, "name": "TWO"}, {"value": "tre", "num": 3, "name": "THREE"}.
532
+
533
+ ``` ruby
534
+ attributes = ["value", "num", "name"]
535
+ values = [["uno",1,"ONE"],["due",2,"TWO"],["tre",3,"THREE"]]
536
+ myCollection.import attributes: attributes, values: values
537
+ ```
538
+
539
+ #### Import more Documents with JSON
540
+
541
+ I import two Documents with the following structure {"value": "uno", "num": 1, "name": "ONE"}, {"value": "due", "num": 2, "name": "TWO"}.
542
+
543
+ ``` ruby
544
+ body = [{"value": "uno", "num": 1, "name": "ONE"}, {"value": "due", "num": 2, "name": "DUE"}]
545
+ myCollection.importJSON body: body
546
+ ```
547
+
548
+ #### Export files
549
+
550
+ As it is possible to import files, it is possible to export all the Document of a Collection with the following command.
551
+
552
+ ``` ruby
553
+ myCollection.export
554
+ ```
555
+
556
+ Alternatively it is possible to retrieve all the Documents in a Collection gradually.
557
+
558
+ ``` ruby
559
+ myCollection.export batchSize: 3 # First three Documents of the Collection
560
+ myCollection.exportNext # Next three Documents
561
+ myCollection.exportNext # Next three Documents
562
+ ```
563
+
564
+ <a name="ArangoDocument"></a>
565
+ ## Arango::Document
566
+
567
+ An Arango::Document is an element of a Arango::Collection. Edges are Arango documents with "\_from" and "\_to" in their body.
568
+ You can create an ArangoCollection instance in one of the following way:
569
+
570
+ ``` ruby
571
+ myDocument = myCollection.document name: "MyDocument"
572
+ myDocument = myCollection["MyDocument"]
573
+ myDocument = Arango::Document.new collection: myCollection, name: "MyDocument"
574
+ ```
575
+
576
+ In the case you want to define a Edge, it is convenient to introduce the parameters "from" and "to" in the instance.
577
+
578
+ ``` ruby
579
+ myEdge = Arango::Document.new name: "MyEdge", from: myDocA, to: myDocB
580
+ ```
581
+
582
+ where myDocA and myDocB are the IDs of two Documents or are two Arango::Document instances.
583
+
584
+ During the creation of an Arango::Document instance, it is possible to define the body for the Document.
585
+
586
+ ``` ruby
587
+ myDocument = Arango::Document.new body: {"value": 17}, name: "MyDocument"
588
+ ```
589
+
590
+ ### Main methods
591
+
592
+ ArangoRB provides several way to create a single Document.
593
+
594
+ ``` ruby
595
+ myDocument.create # Create a new document
596
+ myDocument.retrieve # Retrieve Document
597
+ myDocument.update body: {"value": 3} # We update or add a value
598
+ myDocument.replace body: {"value": 3} # We replace a value
599
+ myDocument.destroy # Destroy document
600
+ ```
601
+
602
+ ### Retrieve information
603
+
604
+ ``` ruby
605
+ myDocument.collection # Retrieve Collection of the Document
606
+ myDocument.database # Retrieve Database of the Document
607
+ myDocument.server # Retrieve Server of the Document
608
+ myDocument.edges collection: myEdgeCollection # Retrieve all myEdgeCollection edges connected with the Document
609
+ myDocument.any(myEdgeCollection) # Retrieve all myEdgeCollection edges connected with the Document
610
+ myDocument.in(myEdgeCollection) # Retrieve all myEdgeCollection edges coming in the Document
611
+ myDocument.out(myEdgeCollection) # Retrieve all myEdgeCollection edges going out the Document
612
+ myEdge.from # Retrieve the document at the begin of the edge
613
+ myEdge.to # Retrieve the document at the end of the edge
614
+ ```
615
+
616
+ #### Example: how to navigate the edges
617
+
618
+ It is possible to navigate between edges and vertexes of a graph database.
619
+
620
+ Think for example that we have the following schema:
621
+ * A --[class: a, name: aa]--> B
622
+ * A --[class: a, name: bb]--> C
623
+ * A --[class: b, name: cc]--> D
624
+ * B --[class: a, name: dd]--> E
625
+
626
+ Then we have:
627
+
628
+ * A.retrieve is A
629
+ * A.edges(collection: a) is [aa, bb]
630
+ * B.in(a) is [aa]
631
+ * B.out(a) is [dd]
632
+ * B.any(a) is [aa, dd] (aa in entry, dd in exit)
633
+ * aa.from is A
634
+ * aa.to is B
635
+
636
+ We can even do some combinations: for example A.out(a)[0].to.out(a)[0].to is E since:
637
+ * A.out(a) is [aa]
638
+ * A.out(a)[0] is aa
639
+ * A.out(a)[0].to is B
640
+ * A.out(a)[0].to.out(a) is [dd]
641
+ * A.out(a)[0].to.out(a)[0] is dd
642
+ * A.out(a)[0].to.out(a)[0].to is E
643
+
644
+ <a name="ArangoGraph"></a>
645
+ ## Arango::Graph
646
+
647
+ ArangoGraph are used to manage Graphs. You can create an Arango::Graph instance in one of the following way.
648
+
649
+ ``` ruby
650
+ myGraph = myDatabase.graph name: "MyGraph"
651
+ myGraph = Arango::Graph.new name: "MyGraph", database: myDatabase
652
+ ```
653
+
654
+ ### Main methods
655
+
656
+ ``` ruby
657
+ myGraph.create # create a new Graph
658
+ myGraph.retrieve # retrieve the Graph
659
+ myGraph.database # retrieve database of the Graph
660
+ myGraph.destroy # destroy the Graph
661
+ ```
662
+
663
+ ### Manage Vertex Collections
664
+
665
+ ``` ruby
666
+ myGraph.getVertexCollections # Retrieve all the vertexCollections of the Graph
667
+ myGraph.addVertexCollection collection: "myCollection" # Add a Vertex Collection to our Graph
668
+ myGraph.removeVertexCollection collection: "myCollection" # Remove a Vertex Collection to our Graph
669
+ ```
670
+
671
+ ### Manage Edge Collections
672
+
673
+ ``` ruby
674
+ myGraph.edgeCollections # Retrieve all the edgeCollections of the Graph
675
+ myGraph.addEdgeCollections collection: "myEdgeCollection", from: "myCollectionA", to: "myCollectionB" # Add an Edge Collection to our Graph
676
+ myGraph.replaceEdgeCollections collection: "myEdgeCollection", from: "myCollectionA", to: "myCollectionB" # Replace an Edge Collection to our Graph
677
+ myGraph.removeEdgeCollections collection: "myEdgeCollection" # Remove an Edge Collection to our Graph
678
+ ```
679
+
680
+ <a name="ArangoVertex"></a><a name="ArangoEdge"></a>
681
+ ## Arango::Vertex and Arango::Edge
682
+
683
+ Both these two classes inherit the class Arango::Document.
684
+ These two classes have been created since Arango::Database offers, in connection of the chosen graph, different HTTP requests to manage Vertexes and Edges. We recommend the reader to read carefully the section on [Arango::Document instances](#ArangoDocument) before to start to use Arango::Vertex and Arango::Edge instances.
685
+
686
+ ### Arango::Vertex methods
687
+
688
+ Arango::Vertex inherit all the methods of Arango::Document class. The following one works similar to the one of Arango::Document Class but use different HTTP requests. For this reason the performance could be different.
689
+ To use Arango::Vertex, the Collection of the Vertex needs to be added either to the VertexCollections or to the EdgeCollections of the chosen Graph. And the Collection needs to have assigned the graph.
690
+
691
+ ``` ruby
692
+ myCollection.graph = myGraph # Be sure that the collection is assigned to a Graph
693
+ myVertex = myCollection.vertex name: "newVertex", body: {"value": 3} # If graph is not assigned, an Arango::Document will be created
694
+ myVertex = Arango::Vertex.new name: "newVertex", body: {"value": 3},
695
+ collection: myCollection # create a new instance
696
+ ```
697
+
698
+ ``` ruby
699
+ myVertex.create # create a new Document in the Graph
700
+ myVertex.retrieve # retrieve a Document
701
+ myVertex.graph # retrieve Graph of the Document
702
+ myVertex.replace body: {"value": 6} # replace the Document
703
+ myVertex.update body: {"value": 6} # update the Document
704
+ myVertex.destroy # delete the Document
705
+ ```
706
+
707
+ ### Arango::Edge methods
708
+
709
+ Arango::Edge inherit all the methods of Arango::Document class. The following one works similar to the one of Arango::Document Class but use a different HTTP request. For this reason the performance could be different.
710
+ To use Arango::Edge, the Collection of the Edge needs to be added to the EdgeCollections of the chosen Graph.
711
+
712
+ ``` ruby
713
+ myEdgeCollection.graph = myGraph
714
+ myEdge = myCollection.edge name: "newEdge", body: {"value": 3}, from: myArangoDocument1, to: myArangoDocument2 # If graph is not assigned, an Arango::Document will be created
715
+ ```
716
+
717
+ ``` ruby
718
+ myEdge.create # create a new Document of type Edge in the Graph
719
+ myEdge.retrieve # retrieve a Document
720
+ myEdge.graph # Retrieve Graph of the Document
721
+ myEdge.replace body: {"value": 6} # replace the Document
722
+ myEdge.update body: {"value": 6} # update the Document
723
+ myEdge.destroy # delete the Document
724
+ ```
725
+
726
+ <a name="ArangoTraversal"></a>
727
+ ## Arango::Traversal
728
+
729
+ Arango::Traversal is used to administrate the traversals.
730
+ Arango::Traversal needs to know the vertex from where the traversal starts, the direction the traversal is going and either the Graph or the EdgeCollection we want to analyze.
731
+
732
+
733
+ ``` ruby
734
+ myTraversal = myVertex.traversal # Start traversal from myVertex
735
+ myTraversal = myDocument.traversal # Start traversal from myDocument
736
+ myTraversal = ArangoTraversal.new database: myDatabase, startVertex: myVertex # create instance
737
+ ```
738
+
739
+ ``` ruby
740
+ myTraversal.vertex = myVertex # define starting Vertex
741
+ myTraversal.edgeCollection = myEdgeCollection # define used Edge
742
+ myTraversal.in # Direction is in
743
+ myTraversal.out # Direction is out
744
+ myTraversal.any # Direction is in and out
745
+ myTraversal.min = 1 # Define how minimum deep we want to go with the traversal
746
+ myTraversal.max = 3 # Define how maximum deep we want to go with the traversal
747
+ ```
748
+
749
+ After the traversal is setup, you can execute it:
750
+
751
+ ``` ruby
752
+ myTraversal.execute
753
+ ```
754
+
755
+ <a name="ArangoUser"></a>
756
+ ## Arango::User
757
+
758
+ Arango::User manages the users.
759
+ To initialize an user:
760
+
761
+ ``` ruby
762
+ myUser = server.user name: "MyUser", password: "password"
763
+ myUser = Arango::User.new user: "MyUser", password: "password", server: server
764
+ ```
765
+
766
+ ### User management
767
+
768
+ ``` ruby
769
+ myUser.retrieve # Retrieve User
770
+ myUser["MyDatabase"] # Retrieve database if the user can access it
771
+ myUser.database("MyDatabase") # Retrieve database if the user can access it
772
+ myUser.create # Create a new User
773
+ myUser.replace active: false # Replace User
774
+ myUser.update active: false # Update User
775
+ myUser.destroy # Delete User
776
+ ```
777
+
778
+ ### Database management
779
+
780
+ ``` ruby
781
+ myUser.databases # List of databases can access
782
+ myUser.addDatabaseAccess grant: "rw", database: myDatabase
783
+ myUser.grant database: myDatabase # Grant access to a database
784
+ myUser.revoke database: myDatabase # Revoke access to a database
785
+ myUser.databaseAccess database: myDatabase # Check permission level of an User
786
+ myUser.addCollectionAccess grant: "rw", database: myDatabase, collection: myCollection
787
+ myUser.revokeCollectionAccess database: myDatabase, collection: myCollection
788
+ myUser.collectionAccess database: myDatabase, collection: myCollection
789
+ ```
790
+
791
+ You can manage the user permissions from the database or from the collection too.
792
+
793
+ ``` ruby
794
+ myDatabase.check_user(myUser)
795
+ myDatabase.addUserAccess(grant: "rw", user: myUser)
796
+ myDatabase.revokeUserAccess(user: myUser)
797
+ myDatabase.userAccess(user: myUser)
798
+ myCollection.check_user(myUser)
799
+ myCollection.addUserAccess(grant: "rw", user: myUser)
800
+ myCollection.revokeUserAccess(user: myUser)
801
+ myCollection.userAccess(user: myUser)
802
+ ```
803
+
804
+ <a name="ArangoIndex"></a>
805
+ ## Arango::Index
806
+
807
+ Arango::Index manages the indexes.
808
+ To initialize an index:
809
+
810
+ ``` ruby
811
+ myIndex = myCollection.index fields: "num", unique: false, id: "myIndex"
812
+ myIndex = Arango::Index.new fields: "num", unique: false, id: "myIndex", collection: myCollection
813
+ ```
814
+
815
+ ### Index management
816
+
817
+ ``` ruby
818
+ myIndex.retrieve # Retrieve Index
819
+ myIndex.create # Create a new Index
820
+ myIndex.destroy # Delete Index
821
+ ```
822
+
823
+ <a name="ArangoTransaction"></a>
824
+ ## Arango::Transaction
825
+
826
+ Transactions are managed by Arango::Transaction.
827
+
828
+ ``` ruby
829
+ action = "function(){ var db = require('@arangodb').db; db.MyCollection.save({}); return db.MyCollection.count(); }"
830
+ myTransaction = myDatabase.transaction action: action
831
+ myTransaction = Arango::Transaction.new action: action, database: myDatabase
832
+ ```
833
+
834
+ ``` ruby
835
+ myTransaction.addWrite(myCollection) # Add write collection
836
+ myTransaction.addRead(myCollection) # Add read collection
837
+ myArangoTransaction.execute # Return the result of the execution
838
+ ```
839
+
840
+ <a name="ArangoTask"></a>
841
+ ## Arango::Task
842
+
843
+ Tasks are managed by ArangoTask.
844
+
845
+ ``` ruby
846
+ command = "(function(params) { require('@arangodb').print(params); })(params)"
847
+ myTask = myDatabase.task id: "mytaskid", name: "MyTaskID", command: command, params: {"foo": "bar", "bar": "foo"}, period: 2
848
+ myTask = Arango::Task.new id: "mytaskid", name: "MyTaskID", command: command, params: {"foo": "bar", "bar": "foo"}, period: 2, database: myDatabase
849
+ ```
850
+
851
+ ``` ruby
852
+ myArangoTask.create # Create a new Task
853
+ myArangoTask.retrieve # Retrieve a Task
854
+ myArangoTask.destroy # Delete a Task
855
+ ```
856
+
857
+ <a name="ArangoReplication"></a>
858
+ ## Arango::Replication
859
+
860
+ Replication is useful to create back up copy of your database or to have a master-slave relationship between two databases.
861
+
862
+ Remember: the used database is the one where the data will be written (the slave) and the remote database will be the master one.
863
+
864
+ Use with caution since the data in the slave database will be deleted.
865
+
866
+ To setup our Slave Server and Master Database use a similar command.
867
+
868
+ ``` ruby
869
+ myReplication = slaveDatabase.replication(master: masterDatabase)
870
+ myReplication = masterDatabase.replication_as_master(slave: slaveDatabase)
871
+ myReplication = Arango::Replication.new master: masterDatabase, slave: slaveDatabase
872
+ ```
873
+
874
+ Than to do a simple syncronization uses;
875
+
876
+ ``` ruby
877
+ myReplication.sync # Sync master - slave database
878
+ ```
879
+
880
+ To retrieve other information ArangoRB provides the following methods:
881
+
882
+ ``` ruby
883
+ myReplication.logger # Returns the current state of the server's replication logger
884
+ myReplication.loggerFollow # Returns data from the server's replication log.
885
+ myReplication.firstTick # Return the first available tick value from the server
886
+ myReplication.rangeTick # Returns the currently available ranges of tick values for all currently available WAL logfiles.
887
+ myReplication.serverId # Returns the servers id.
888
+ ```
889
+
890
+ ### Relation Master-Slave
891
+
892
+ To enslave a Server in relation to another Database use the following command:
893
+
894
+ ``` ruby
895
+ myReplication.enslave
896
+ ```
897
+
898
+ To manage the Configuration of a Master-Slave Replication you can use the following commands:
899
+
900
+ ``` ruby
901
+ myReplication.start # Start replication
902
+ myReplication.configuration # check the Configuration of the Replication
903
+ myReplication.state # check the status of the Replication
904
+ myReplication.stop # stop the Replication
905
+ myReplication.modify # modify the Configuration of the Replication (you can modify only a stopped Replication)
906
+ ```
907
+
908
+ ## Arango::Batch
909
+
910
+ To create a batch request, you can use ArangoRB::Batch object. This permit to do multiple requests with one single call to the API.
911
+
912
+ To create a batch use one of the following way:
913
+
914
+ ``` ruby
915
+ batch = server.batch
916
+ batch = Arango::Batch.new(server: server)
917
+ ```
918
+
919
+ To add a queries to the batch request you can use the brutal way:
920
+
921
+ ``` ruby
922
+ batch.queries = [
923
+ {
924
+ "type": "POST",
925
+ "address": "/_db/MyDatabase/_api/collection",
926
+ "body": {"name": "newCOLLECTION"},
927
+ "id": "1"
928
+ },
929
+ {
930
+ "type": "GET",
931
+ "address": "/_api/database",
932
+ "id": "2"
933
+ }
934
+ ]
935
+ ```
936
+
937
+ Or the Ruby way (the id will be handled by the system, if not specified):
938
+
939
+ ``` ruby
940
+ batch = server.batch
941
+ batch.addQuery(method: "POST", address: "/_db/MyDatabase/_api/collection",
942
+ body: {"name": "newCOLLECTION"})
943
+ batch.addQuery(method: "GET", address: "/_api/database")
944
+ ```
945
+
946
+ In both the cases the queries will be stored in an hash with key the id of the query and as value the query.
947
+ This query can be handled with the following methods:
948
+
949
+ ``` ruby
950
+ batch.to_h # Retrieve an hash version of the instance
951
+ batch.modifyQuery(id: "1", method: "GET", address: "/_db/MyDatabase/_api/collection/newCOLLECTION") # Modify the Query with id "1"
952
+ batch.removeQuery(id: "1") # Remove query
953
+ ```
954
+
955
+ To execute the query use:
956
+
957
+ ``` ruby
958
+ batch.execute
959
+ ```
960
+
961
+ To manage how the server handle the batch, Arango::Server offers the following functions:
962
+
963
+ ``` ruby
964
+ server.createDumpBatch ttl: 10 # Create a new dump batch with 10 second time-to-live (return id of the dumpBatch)
965
+ server.prolongDumpBatch id: idDumpBatch, ttl: 20 # Prolong the life of a batch for 20 seconds
966
+ server.destroyDumpBatch id: idDumpBatch # Delete a selected batch
967
+ ```
968
+
969
+ <a name="ArangoView"></a>
970
+ ## Arango::View
971
+
972
+ Views are managed by Arango::View.
973
+
974
+ ``` ruby
975
+ myView = myDatabase.view name: "MyView"
976
+ myView = Arango::View.new name: "MyView", database: myDatabase
977
+ ```
978
+
979
+ ``` ruby
980
+ myView.addLink(collection: myCollection) # Add a collection to the link
981
+ myView.retrieve # Retrieve view
982
+ myView.create # Create a view
983
+ myView.replaceProperties # Replace properties
984
+ myView.updateProperties # Update properties
985
+ myView.rename name: "MyView2" # Change name
986
+ myView.properties # Check properties
987
+ myView.destroy # Delete view
988
+ ```
989
+
990
+ <a name="ArangoFoxx"></a>
991
+ ## Arango::Foxx
992
+
993
+ Views are managed by Arango::Foxx. This instance of Foxx is untested.
994
+
995
+ ``` ruby
996
+ myFoxx = myDatabase.foxx mount: address
997
+ myFoxx = Arango::Foxx.new mount: address, database: myDatabase
998
+ ```
999
+
1000
+ ``` ruby
1001
+ myFoxx.retrieve # Retrieve foxx
1002
+ myView.create # Create a foxx
1003
+ myFoxx.update # Update foxx
1004
+ myFoxx.replace # Replace foxx
1005
+ myFoxx.destroy # Delete foxx
1006
+ ```
1007
+
1008
+ Other methods
1009
+
1010
+ ``` ruby
1011
+ myFoxx.retrieveConfiguration
1012
+ myView.updateConfiguration body: body
1013
+ myFoxx.replaceConfiguration body: body
1014
+ myFoxx.retrieveDependencies
1015
+ myFoxx.updateDependencies body: body
1016
+ myFoxx.replaceDependencies body: body
1017
+ myFoxx.scripts
1018
+ myFoxx.run_script name: "test"
1019
+ myFoxx.tests
1020
+ myFoxx.enableDevelopment
1021
+ myFoxx.disableDevelopment
1022
+ myFoxx.readme
1023
+ myFoxx.swagger
1024
+ myFoxx.download path: path
1025
+ myFoxx.commit body: body
1026
+ ```
1027
+
1028
+ <a name="ArangoCache"></a>
1029
+ ## Arango::Cache
1030
+
1031
+ Arango::Cache is to manage ArangoRB Cache (NOT ArangoDB cache) and it helps you to manage your documents around your code. In this way if you update an Arango::Document in your code, it will updated everywhere.
1032
+ An Arango::Cache instance is created together with the initialization of a server instance with active_cache true.
1033
+
1034
+ ``` ruby
1035
+ cache = server.cache # Arango::Cache instance
1036
+ cache.max # Max number of variable stored in the cache
1037
+ cache.updateMax(type: :document, val: 400) # Update how many documents store in the cache
1038
+ cache.clear # Clear cache
1039
+ cache.to_h # Hash of the cache
1040
+ ```
1041
+
1042
+ NB: If you insert a max value higher than the quantity of elements in the Cache, then the first elements in excess will be removed from the Cache.
1043
+
1044
+ If the limit of the Cache for one type is reached, then the first element cached of that type will be deleted from the Cache.
1045
+
1046
+
1047
+ <a name="ArangoError"></a>
1048
+ ## Arango::Error
1049
+
1050
+ Error from ArangoRB are managed by Arango::Error.
1051
+ Each ArangoRB error is provided of a code and a message. The list of code can be find in the file lib/Error.rb
1052
+
1053
+ ``` ruby
1054
+ begin
1055
+ Arango::Collection.new name: "Test", database: not_a_database
1056
+ rescue Arango::Error => e
1057
+ e.message # Message of the error
1058
+ e.code # ArangoRB code, each ArangoRB errors provides a list of errors
1059
+ e.data # More information about the error
1060
+ e.to_h # Hash version of the error
1061
+ end
1062
+ ```
1063
+
1064
+ Arango::Error has a children class called Arango::ErrorDB to handle Errors coming from wrong HTTP requests.
1065
+
1066
+ ``` ruby
1067
+ begin
1068
+ Arango::Collection.new(name: "DuplicateCollection", database: myDatabase).create
1069
+ rescue Arango::Error => e
1070
+ e.message # Message of the error
1071
+ e.code # HTTP error code
1072
+ e.data # More information about the error
1073
+ e.errorNum # ArangoDB errorNum code
1074
+ e.action # Type of request done to ArangoDB (POST, GET, ...)
1075
+ e.url # URL requested to ArangoDB
1076
+ e.request # Request made to ArangoDB
1077
+ e.to_h # Hash version of the error
1078
+ end
1079
+ ```