arangorb 1.4.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ArangoRB.gemspec +20 -18
- data/Gemfile +3 -0
- data/README.md +1079 -908
- data/lib/AQL.rb +155 -0
- data/lib/Batch.rb +97 -0
- data/lib/Cache.rb +71 -0
- data/lib/Collection.rb +852 -0
- data/lib/Database.rb +417 -0
- data/lib/Document.rb +346 -0
- data/lib/Edge.rb +104 -0
- data/lib/Error.rb +125 -0
- data/lib/Foxx.rb +277 -0
- data/lib/Graph.rb +325 -0
- data/lib/Index.rb +126 -0
- data/lib/Replication.rb +235 -0
- data/lib/Request.rb +143 -0
- data/lib/Server.rb +466 -0
- data/lib/Task.rb +120 -0
- data/lib/Transaction.rb +115 -0
- data/lib/Traversal.rb +224 -0
- data/lib/User.rb +197 -0
- data/lib/Vertex.rb +127 -0
- data/lib/View.rb +151 -0
- data/lib/arangorb.rb +23 -15
- data/lib/helpers/Error.rb +28 -0
- data/lib/helpers/Return.rb +53 -0
- metadata +64 -45
- data/lib/ArangoRB_AQL.rb +0 -181
- data/lib/ArangoRB_Cache.rb +0 -174
- data/lib/ArangoRB_Col.rb +0 -526
- data/lib/ArangoRB_DB.rb +0 -363
- data/lib/ArangoRB_Doc.rb +0 -319
- data/lib/ArangoRB_Edg.rb +0 -184
- data/lib/ArangoRB_Gra.rb +0 -201
- data/lib/ArangoRB_Index.rb +0 -135
- data/lib/ArangoRB_Replication.rb +0 -261
- data/lib/ArangoRB_Ser.rb +0 -446
- data/lib/ArangoRB_Task.rb +0 -129
- data/lib/ArangoRB_Tra.rb +0 -169
- data/lib/ArangoRB_Tran.rb +0 -68
- data/lib/ArangoRB_User.rb +0 -157
- data/lib/ArangoRB_Ver.rb +0 -162
- data/spec/arangoRB_helper.rb +0 -4
- data/spec/arangoRestart_helper.rb +0 -14
- data/spec/arangorb-1.3.0.gem +0 -0
- data/spec/lib/0.1.0/arangoAQL_helper.rb +0 -64
- data/spec/lib/0.1.0/arangoC_helper.rb +0 -170
- data/spec/lib/0.1.0/arangoDB_helper.rb +0 -119
- data/spec/lib/0.1.0/arangoDoc_helper.rb +0 -79
- data/spec/lib/0.1.0/arangoE_helper.rb +0 -50
- data/spec/lib/0.1.0/arangoG_helper.rb +0 -78
- data/spec/lib/0.1.0/arangoS_helper.rb +0 -37
- data/spec/lib/0.1.0/arangoT_helper.rb +0 -48
- data/spec/lib/0.1.0/arangoV_helper.rb +0 -65
- data/spec/lib/1.0.0/arangoC_helper.rb +0 -73
- data/spec/lib/1.0.0/arangoDB_helper.rb +0 -48
- data/spec/lib/1.0.0/arangoI_helper.rb +0 -43
- data/spec/lib/1.0.0/arangoS_helper.rb +0 -192
- data/spec/lib/1.0.0/arangoTa_helper.rb +0 -49
- data/spec/lib/1.0.0/arangoTr_helper.rb +0 -15
- data/spec/lib/1.0.0/arangoU_helper.rb +0 -72
- data/spec/lib/1.1.0/arangoRB_helper.rb +0 -144
- data/spec/lib/1.1.0/arangoRB_walks_helper.rb +0 -19
- data/spec/lib/1.2.0/arangoCache_helper.rb +0 -66
- data/spec/lib/1.3.0/arangoHash_helper.rb +0 -30
- data/spec/lib/arangoRB_0.1.0_helper.rb +0 -9
- data/spec/lib/arangoRB_1.0.0_helper.rb +0 -6
- data/spec/lib/arangoRB_1.1.0_helper.rb +0 -2
- data/spec/lib/arangoRB_1.2.0_helper.rb +0 -2
- data/spec/spec_helper.rb +0 -42
data/lib/Database.rb
ADDED
@@ -0,0 +1,417 @@
|
|
1
|
+
# === DATABASE ===
|
2
|
+
|
3
|
+
module Arango
|
4
|
+
class Database
|
5
|
+
include Arango::Helper_Error
|
6
|
+
include Arango::Helper_Return
|
7
|
+
include Arango::Server_Return
|
8
|
+
|
9
|
+
def self.new(*args)
|
10
|
+
hash = args[0]
|
11
|
+
super unless hash.is_a?(Hash)
|
12
|
+
server = hash[:server]
|
13
|
+
if server.is_a?(Arango::Server) && server.active_cache
|
14
|
+
cache_name = hash[:name]
|
15
|
+
cached = server.cache.cache.dig(:database, cache_name)
|
16
|
+
if cached.nil?
|
17
|
+
hash[:cache_name] = cache_name
|
18
|
+
return super
|
19
|
+
else
|
20
|
+
return cached
|
21
|
+
end
|
22
|
+
end
|
23
|
+
super
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize(name:, server:, cache_name: nil)
|
27
|
+
assign_server(server)
|
28
|
+
unless cache_name.nil?
|
29
|
+
@cache_name = cache_name
|
30
|
+
@server.cache.save(:database, cache_name, self)
|
31
|
+
end
|
32
|
+
@name = name
|
33
|
+
@server = server
|
34
|
+
@isSystem = nil
|
35
|
+
@path = nil
|
36
|
+
@id = nil
|
37
|
+
end
|
38
|
+
|
39
|
+
# === DEFINE ===
|
40
|
+
|
41
|
+
attr_reader :isSystem, :path, :id, :server, :cache_name
|
42
|
+
attr_accessor :name
|
43
|
+
|
44
|
+
# === TO HASH ===
|
45
|
+
|
46
|
+
def to_h
|
47
|
+
{
|
48
|
+
"name": @name,
|
49
|
+
"isSystem": @isSystem,
|
50
|
+
"path": @path,
|
51
|
+
"id": @id,
|
52
|
+
"cache_name": @cache_name,
|
53
|
+
"server": @server.base_uri
|
54
|
+
}.delete_if{|k,v| v.nil?}
|
55
|
+
end
|
56
|
+
|
57
|
+
# === REQUEST ===
|
58
|
+
|
59
|
+
def request(action, url, body: {}, headers: {},
|
60
|
+
query: {}, key: nil, return_direct_result: false,
|
61
|
+
skip_to_json: false, keepNull: false)
|
62
|
+
url = "_db/#{@name}/#{url}"
|
63
|
+
@server.request(action, url, body: body,
|
64
|
+
headers: headers, query: query, key: key,
|
65
|
+
return_direct_result: return_direct_result,
|
66
|
+
skip_to_json: skip_to_json, keepNull: keepNull)
|
67
|
+
end
|
68
|
+
|
69
|
+
# === GET ===
|
70
|
+
|
71
|
+
def assign_attributes(result)
|
72
|
+
return unless result.is_a?(Hash)
|
73
|
+
@name = result[:name]
|
74
|
+
@isSystem = result[:isSystem]
|
75
|
+
@path = result[:path]
|
76
|
+
@id = result[:id]
|
77
|
+
if @server.active_cache && @cache_name.nil?
|
78
|
+
@cache_name = result[:name]
|
79
|
+
@server.cache.save(:database, @cache_name, self)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def retrieve
|
84
|
+
result = request("GET", "_api/database/current", key: :result)
|
85
|
+
assign_attributes(result)
|
86
|
+
return return_directly?(result) ? result : self
|
87
|
+
end
|
88
|
+
alias current retrieve
|
89
|
+
|
90
|
+
# === POST ===
|
91
|
+
|
92
|
+
def create(name: @name, users: nil)
|
93
|
+
body = {
|
94
|
+
"name": name,
|
95
|
+
"users": users
|
96
|
+
}
|
97
|
+
result = @server.request("POST", "_api/database", body: body, key: :result)
|
98
|
+
return return_directly?(result) ? result : self
|
99
|
+
end
|
100
|
+
|
101
|
+
# == DELETE ==
|
102
|
+
|
103
|
+
def destroy
|
104
|
+
@server.request("DELETE", "_api/database/#{@name}", key: :result)
|
105
|
+
end
|
106
|
+
|
107
|
+
# == COLLECTION ==
|
108
|
+
|
109
|
+
def [](name)
|
110
|
+
Arango::Collection.new(name: name, database: self)
|
111
|
+
end
|
112
|
+
|
113
|
+
def collection(name:, body: {}, type: :document)
|
114
|
+
Arango::Collection.new(name: name, database: self, body: body, type: type)
|
115
|
+
end
|
116
|
+
|
117
|
+
def collections(excludeSystem: true)
|
118
|
+
query = { "excludeSystem": excludeSystem }
|
119
|
+
result = request("GET", "_api/collection", query: query)
|
120
|
+
return result if return_directly?(result)
|
121
|
+
result[:result].map do |x|
|
122
|
+
Arango::Collection.new(database: self, name: x[:name], body: x )
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
# == GRAPH ==
|
127
|
+
|
128
|
+
def graphs
|
129
|
+
result = request("GET", "_api/gharial")
|
130
|
+
return result if return_directly?(result)
|
131
|
+
result[:graphs].map do |graph|
|
132
|
+
Arango::Graph.new(database: self, name: graph[:_key], body: graph)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def graph(name:, edgeDefinitions: [], orphanCollections: [],
|
137
|
+
body: {})
|
138
|
+
Arango::Graph.new(name: name, database: self,
|
139
|
+
edgeDefinitions: edgeDefinitions,
|
140
|
+
orphanCollections: orphanCollections, body: body)
|
141
|
+
end
|
142
|
+
|
143
|
+
# == QUERY ==
|
144
|
+
|
145
|
+
def queryProperties
|
146
|
+
request("GET", "_api/query/properties")
|
147
|
+
end
|
148
|
+
|
149
|
+
def changeQueryProperties(slowQueryThreshold: nil, enabled: nil, maxSlowQueries: nil,
|
150
|
+
trackSlowQueries: nil, maxQueryStringLength: nil, trackBindVars: nil)
|
151
|
+
body = {
|
152
|
+
"slowQueryThreshold": slowQueryThreshold,
|
153
|
+
"enabled": enabled,
|
154
|
+
"maxSlowQueries": maxSlowQueries,
|
155
|
+
"trackSlowQueries": trackSlowQueries,
|
156
|
+
"maxQueryStringLength": maxQueryStringLength,
|
157
|
+
"trackBindVars": trackBindVars
|
158
|
+
}
|
159
|
+
request("PUT", "_api/query/properties", body: body)
|
160
|
+
end
|
161
|
+
|
162
|
+
def currentQuery
|
163
|
+
request("GET", "_api/query/current")
|
164
|
+
end
|
165
|
+
|
166
|
+
def slowQueries
|
167
|
+
request("GET", "_api/query/slow")
|
168
|
+
end
|
169
|
+
|
170
|
+
def stopSlowQueries
|
171
|
+
result = request("DELETE", "_api/query/slow")
|
172
|
+
return return_delete(result)
|
173
|
+
end
|
174
|
+
|
175
|
+
# === QUERY CACHE ===
|
176
|
+
|
177
|
+
def clearQueryCache
|
178
|
+
result = request("DELETE", "_api/query-cache")
|
179
|
+
return return_delete(result)
|
180
|
+
end
|
181
|
+
|
182
|
+
def retrieveQueryCache
|
183
|
+
request("GET", "_api/query-cache/entries")
|
184
|
+
end
|
185
|
+
|
186
|
+
def propertyQueryCache
|
187
|
+
request("GET", "_api/query-cache/properties")
|
188
|
+
end
|
189
|
+
|
190
|
+
def changePropertyQueryCache(mode:, maxResults: nil)
|
191
|
+
satisfy_category?(mode, ["off", "on", "demand"])
|
192
|
+
body = { "mode": mode, "maxResults": maxResults }
|
193
|
+
database.request("PUT", "_api/query-cache/properties", body: body)
|
194
|
+
end
|
195
|
+
|
196
|
+
# === AQL ===
|
197
|
+
|
198
|
+
def aql(query:, count: nil, batchSize: nil, cache: nil, memoryLimit: nil,
|
199
|
+
ttl: nil, bindVars: nil, failOnWarning: nil, profile: nil,
|
200
|
+
maxTransactionSize: nil, skipInaccessibleCollections: nil,
|
201
|
+
maxWarningCount: nil, intermediateCommitCount: nil,
|
202
|
+
satelliteSyncWait: nil, fullCount: nil, intermediateCommitSize: nil,
|
203
|
+
optimizer_rules: nil, maxPlans: nil)
|
204
|
+
Arango::AQL.new(query: query, database: self, count: count,
|
205
|
+
batchSize: batchSize, cache: cache, memoryLimit: memoryLimit, ttl: ttl,
|
206
|
+
bindVars: bindVars, failOnWarning: failOnWarning, profile: profile,
|
207
|
+
maxTransactionSize: maxTransactionSize,
|
208
|
+
skipInaccessibleCollections: skipInaccessibleCollections,
|
209
|
+
maxWarningCount: maxWarningCount,
|
210
|
+
intermediateCommitCount: intermediateCommitCount,
|
211
|
+
satelliteSyncWait: satelliteSyncWait, fullCount: fullCount,
|
212
|
+
intermediateCommitSize: intermediateCommitSize,
|
213
|
+
optimizer_rules: optimizer_rules, maxPlans: maxPlans)
|
214
|
+
end
|
215
|
+
|
216
|
+
# === AQL FUNCTION ===
|
217
|
+
|
218
|
+
def aqlFunctions(namespace: nil)
|
219
|
+
request("GET", "_api/aqlfunction", query: {"namespace": namespace}, key: :result)
|
220
|
+
end
|
221
|
+
|
222
|
+
def createAqlFunction(code:, name:, isDeterministic: nil)
|
223
|
+
body = { "code": code, "name": name, "isDeterministic": isDeterministic }
|
224
|
+
request("POST", "_api/aqlfunction", body: body)
|
225
|
+
end
|
226
|
+
|
227
|
+
def deleteAqlFunction(name:)
|
228
|
+
result = request("DELETE", "_api/aqlfunction/#{name}")
|
229
|
+
return return_delete(result)
|
230
|
+
end
|
231
|
+
|
232
|
+
# === REPLICATION ===
|
233
|
+
|
234
|
+
def inventory(includeSystem: nil, global: nil, batchId:)
|
235
|
+
query = {
|
236
|
+
"includeSystem": includeSystem,
|
237
|
+
"global": global,
|
238
|
+
"batchId": batchId
|
239
|
+
}
|
240
|
+
request("GET", "_api/replication/inventory", query: query)
|
241
|
+
end
|
242
|
+
|
243
|
+
def clusterInventory(includeSystem: nil)
|
244
|
+
query = { "includeSystem": includeSystem }
|
245
|
+
request("GET", "_api/replication/clusterInventory", query: query)
|
246
|
+
end
|
247
|
+
|
248
|
+
def logger
|
249
|
+
request("GET", "_api/replication/logger-state")
|
250
|
+
end
|
251
|
+
|
252
|
+
def loggerFollow(from: nil, to: nil, chunkSize: nil, includeSystem: nil)
|
253
|
+
query = {
|
254
|
+
"from": from,
|
255
|
+
"to": to,
|
256
|
+
"chunkSize": chunkSize,
|
257
|
+
"includeSystem": includeSystem
|
258
|
+
}
|
259
|
+
request("GET", "_api/replication/logger-follow", query: query)
|
260
|
+
end
|
261
|
+
|
262
|
+
def loggerFirstTick
|
263
|
+
request("GET", "_api/replication/logger-first-tick", key: :firstTick)
|
264
|
+
end
|
265
|
+
|
266
|
+
def loggerRangeTick
|
267
|
+
request("GET", "_api/replication/logger-tick-ranges")
|
268
|
+
end
|
269
|
+
|
270
|
+
def serverId
|
271
|
+
request("GET", "_api/replication/server-id", key: :serverId)
|
272
|
+
end
|
273
|
+
|
274
|
+
def range
|
275
|
+
request("GET", "_api/wal/range")
|
276
|
+
end
|
277
|
+
|
278
|
+
def lastTick
|
279
|
+
request("GET", "_api/wal/lastTick")
|
280
|
+
end
|
281
|
+
|
282
|
+
def tail(from: nil, to: nil, global: nil, chunkSize: nil,
|
283
|
+
serverID: nil, barrierID: nil)
|
284
|
+
query = {
|
285
|
+
from: from,
|
286
|
+
to: to,
|
287
|
+
global: global,
|
288
|
+
chunkSize: chunkSize,
|
289
|
+
serverID: serverID,
|
290
|
+
barrierID: barrierID
|
291
|
+
}
|
292
|
+
request("GET", "_api/wal/tail", query: query)
|
293
|
+
end
|
294
|
+
|
295
|
+
def replication(master:, includeSystem: true,
|
296
|
+
initialSyncMaxWaitTime: nil, incremental: nil,
|
297
|
+
restrictCollections: nil, connectTimeout: nil,
|
298
|
+
autoResync: nil, idleMinWaitTime: nil, requestTimeout: nil,
|
299
|
+
requireFromPresent: nil, idleMaxWaitTime: nil, restrictType: nil,
|
300
|
+
maxConnectRetries: nil, adaptivePolling: nil,
|
301
|
+
connectionRetryWaitTime: nil, autoResyncRetries: nil, chunkSize: nil,
|
302
|
+
verbose: nil)
|
303
|
+
Arango::Replication.new(slave: self, master: master, includeSystem: includeSystem,
|
304
|
+
initialSyncMaxWaitTime: initialSyncMaxWaitTime, incremental: incremental,
|
305
|
+
restrictCollections: restrictCollections, connectTimeout: connectTimeout,
|
306
|
+
autoResync: autoResync, idleMinWaitTime: idleMinWaitTime,
|
307
|
+
requestTimeout: requestTimeout, requireFromPresent: requireFromPresent, idleMaxWaitTime: idleMaxWaitTime, restrictType: restrictType,
|
308
|
+
maxConnectRetries: maxConnectRetries, adaptivePolling: adaptivePolling,
|
309
|
+
connectionRetryWaitTime: connectionRetryWaitTime,
|
310
|
+
autoResyncRetries: autoResyncRetries, chunkSize: chunkSize,
|
311
|
+
verbose: verbose)
|
312
|
+
end
|
313
|
+
|
314
|
+
def replication_as_master(slave:, includeSystem: true,
|
315
|
+
initialSyncMaxWaitTime: nil, incremental: nil,
|
316
|
+
restrictCollections: nil, connectTimeout: nil,
|
317
|
+
autoResync: nil, idleMinWaitTime: nil, requestTimeout: nil,
|
318
|
+
requireFromPresent: nil, idleMaxWaitTime: nil, restrictType: nil,
|
319
|
+
maxConnectRetries: nil, adaptivePolling: nil,
|
320
|
+
connectionRetryWaitTime: nil, autoResyncRetries: nil, chunkSize: nil,
|
321
|
+
verbose: nil)
|
322
|
+
Arango::Replication.new(master: self, slave: slave, includeSystem: includeSystem,
|
323
|
+
initialSyncMaxWaitTime: initialSyncMaxWaitTime, incremental: incremental,
|
324
|
+
restrictCollections: restrictCollections, connectTimeout: connectTimeout,
|
325
|
+
autoResync: autoResync, idleMinWaitTime: idleMinWaitTime,
|
326
|
+
requestTimeout: requestTimeout, requireFromPresent: requireFromPresent, idleMaxWaitTime: idleMaxWaitTime, restrictType: restrictType,
|
327
|
+
maxConnectRetries: maxConnectRetries, adaptivePolling: adaptivePolling,
|
328
|
+
connectionRetryWaitTime: connectionRetryWaitTime,
|
329
|
+
autoResyncRetries: autoResyncRetries, chunkSize: chunkSize,
|
330
|
+
verbose: verbose)
|
331
|
+
end
|
332
|
+
|
333
|
+
# === FOXX ===
|
334
|
+
|
335
|
+
def foxx(body: {}, mount:, development: nil, legacy: nil, provides: nil,
|
336
|
+
name: nil, version: nil, type: "application/json", setup: nil,
|
337
|
+
teardown: nil)
|
338
|
+
Arango::Foxx.new(database: self, body: body, mount: mount,
|
339
|
+
development: development, legacy: legacy, provides: provides,
|
340
|
+
name: name, version: version, type: type, setup: setup,
|
341
|
+
teardown: teardown)
|
342
|
+
end
|
343
|
+
|
344
|
+
def foxxes
|
345
|
+
result = request("GET", "_api/foxx")
|
346
|
+
return result if return_directly?(result)
|
347
|
+
result.map do |fox|
|
348
|
+
Arango::Foxx.new(database: self, mount: fox[:mount], body: fox)
|
349
|
+
end
|
350
|
+
end
|
351
|
+
|
352
|
+
# === USER ACCESS ===
|
353
|
+
|
354
|
+
def check_user(user)
|
355
|
+
user = Arango::User.new(user: user) if user.is_a?(String)
|
356
|
+
return user
|
357
|
+
end
|
358
|
+
private :check_user
|
359
|
+
|
360
|
+
def addUserAccess(grant:, user:)
|
361
|
+
user = check_user(user)
|
362
|
+
user.addDatabaseAccess(grant: grant, database: @name)
|
363
|
+
end
|
364
|
+
|
365
|
+
def revokeUserAccess(user:)
|
366
|
+
user = check_user(user)
|
367
|
+
user.revokeDatabaseAccess(database: @name)
|
368
|
+
end
|
369
|
+
|
370
|
+
def userAccess(user:)
|
371
|
+
user = check_user(user)
|
372
|
+
user.databaseAccess(database: @name)
|
373
|
+
end
|
374
|
+
|
375
|
+
# === VIEW ===
|
376
|
+
|
377
|
+
def views
|
378
|
+
result = request("GET", "_api/view", key: :result)
|
379
|
+
return result if return_directly?(result)
|
380
|
+
result.map do |view|
|
381
|
+
Arango::View.new(database: self, id: view[:id], name: view[:name], type: view[:type])
|
382
|
+
end
|
383
|
+
end
|
384
|
+
|
385
|
+
def view(name:)
|
386
|
+
Arango::View.new(database: self, name: name)
|
387
|
+
end
|
388
|
+
|
389
|
+
# === TASK ===
|
390
|
+
|
391
|
+
def task(id: nil, name: nil, type: nil, period: nil, command: nil, params: nil, created: nil, body: {})
|
392
|
+
Arango::Task.new(id: id, name: name, type: type, period: period, command: command,
|
393
|
+
params: params, created: created, body: body, database: self)
|
394
|
+
end
|
395
|
+
|
396
|
+
def tasks
|
397
|
+
result = request("GET", "_api/tasks")
|
398
|
+
return result if return_directly?(result)
|
399
|
+
result.delete_if{|k| k[:database] != @name}
|
400
|
+
result.map do |task|
|
401
|
+
Arango::Task.new(body: task, database: self)
|
402
|
+
end
|
403
|
+
end
|
404
|
+
|
405
|
+
# === TRANSACTION ===
|
406
|
+
|
407
|
+
def transaction(action:, write: [], read: [], params: nil,
|
408
|
+
maxTransactionSize: nil, lockTimeout: nil, waitForSync: nil,
|
409
|
+
intermediateCommitCount: nil, intermedateCommitSize: nil)
|
410
|
+
Arango::Transaction.new(database: self, action: action, write: write,
|
411
|
+
read: read, params: params, maxTransactionSize: maxTransactionSize,
|
412
|
+
lockTimeout: lockTimeout, waitForSync: waitForSync,
|
413
|
+
intermediateCommitCount: intermediateCommitCount,
|
414
|
+
intermedateCommitSize: intermedateCommitSize)
|
415
|
+
end
|
416
|
+
end
|
417
|
+
end
|
data/lib/Document.rb
ADDED
@@ -0,0 +1,346 @@
|
|
1
|
+
# ==== DOCUMENT ====
|
2
|
+
|
3
|
+
module Arango
|
4
|
+
class Document
|
5
|
+
include Arango::Helper_Error
|
6
|
+
include Arango::Helper_Return
|
7
|
+
include Arango::Collection_Return
|
8
|
+
|
9
|
+
def self.new(*args)
|
10
|
+
hash = args[0]
|
11
|
+
super unless hash.is_a?(Hash)
|
12
|
+
collection = hash[:collection]
|
13
|
+
if collection.is_a?(Arango::Collection) &&
|
14
|
+
collection.database.server.active_cache && !hash[:name].nil?
|
15
|
+
cache_name = "#{collection.database.name}/#{collection.name}/#{hash[:name]}"
|
16
|
+
cached = collection.database.server.cache.cache.dig(:document, cache_name)
|
17
|
+
if cached.nil?
|
18
|
+
hash[:cache_name] = cache_name
|
19
|
+
return super
|
20
|
+
else
|
21
|
+
body = hash[:body] || {}
|
22
|
+
[:rev, :from, :to].each{|k| body[:"_#{k}"] ||= hash[k]}
|
23
|
+
body[:"_key"] ||= hash[:name]
|
24
|
+
cached.assign_attributes(body)
|
25
|
+
return cached
|
26
|
+
end
|
27
|
+
end
|
28
|
+
super
|
29
|
+
end
|
30
|
+
|
31
|
+
def initialize(name: nil, collection:, body: {}, rev: nil, from: nil,
|
32
|
+
to: nil, cache_name: nil)
|
33
|
+
assign_collection(collection)
|
34
|
+
unless cache_name.nil?
|
35
|
+
@cache_name = cache_name
|
36
|
+
@server.cache.save(:document, cache_name, self)
|
37
|
+
end
|
38
|
+
body[:_key] ||= name
|
39
|
+
body[:_rev] ||= rev
|
40
|
+
body[:_to] ||= to
|
41
|
+
body[:_from] ||= from
|
42
|
+
body[:_id] ||= "#{@collection.name}/#{name}" unless name.nil?
|
43
|
+
assign_attributes(body)
|
44
|
+
end
|
45
|
+
|
46
|
+
def name
|
47
|
+
return @body[:_key]
|
48
|
+
end
|
49
|
+
alias key name
|
50
|
+
|
51
|
+
def rev
|
52
|
+
return @body[:_rev]
|
53
|
+
end
|
54
|
+
|
55
|
+
def id
|
56
|
+
return @body[:_id]
|
57
|
+
end
|
58
|
+
|
59
|
+
def name=(att)
|
60
|
+
assign_attributes({_key: att})
|
61
|
+
end
|
62
|
+
alias key= name=
|
63
|
+
|
64
|
+
def rev=(att)
|
65
|
+
assign_attributes({_rev: att})
|
66
|
+
end
|
67
|
+
|
68
|
+
def id=(att)
|
69
|
+
assign_attributes({_id: id})
|
70
|
+
end
|
71
|
+
|
72
|
+
def from=(att)
|
73
|
+
att = att.id if att.is_a?(Arango::Document)
|
74
|
+
assign_attributes({_from: att})
|
75
|
+
end
|
76
|
+
|
77
|
+
def to=(att)
|
78
|
+
att = att.id if att.is_a?(Arango::Document)
|
79
|
+
assign_attributes({_to: att})
|
80
|
+
end
|
81
|
+
|
82
|
+
# === DEFINE ==
|
83
|
+
|
84
|
+
attr_reader :collection, :graph, :database, :server, :body, :cache_name
|
85
|
+
|
86
|
+
def body=(result)
|
87
|
+
result.delete_if{|k,v| v.nil?}
|
88
|
+
@body ||= {}
|
89
|
+
# binding.pry if @body[:_key] == "Second_Key"
|
90
|
+
hash = {
|
91
|
+
"_key": @body[:_key],
|
92
|
+
"_id": @body[:_id],
|
93
|
+
"_rev": @body[:_rev],
|
94
|
+
"_from": @body[:_from],
|
95
|
+
"_to": @body[:_to]
|
96
|
+
}
|
97
|
+
@body = hash.merge(result)
|
98
|
+
if @body[:_id].nil? && !@body[:_key].nil?
|
99
|
+
@body[:_id] = "#{@collection.name}/#{@body[:_key]}"
|
100
|
+
end
|
101
|
+
set_up_from_or_to("from", result[:_from])
|
102
|
+
set_up_from_or_to("to", result[:_to])
|
103
|
+
if @server.active_cache && @cache_name.nil? && !@body[:_id].nil?
|
104
|
+
@cache_name = "#{@database.name}/#{@body[:_id]}"
|
105
|
+
@server.cache.save(:document, @cache_name, self)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
alias assign_attributes body=
|
109
|
+
|
110
|
+
# === TO HASH ===
|
111
|
+
|
112
|
+
def to_h
|
113
|
+
{
|
114
|
+
"name": @body[:_key],
|
115
|
+
"id": @body[:_id],
|
116
|
+
"rev": @body[:_rev],
|
117
|
+
"from": @body[:_from],
|
118
|
+
"to": @body[:_to],
|
119
|
+
"body": @body,
|
120
|
+
"cache_name": @cache_name,
|
121
|
+
"collection": @collection.name,
|
122
|
+
"graph": @graph&.name
|
123
|
+
}.delete_if{|k,v| v.nil?}
|
124
|
+
end
|
125
|
+
|
126
|
+
def set_up_from_or_to(attrs, var)
|
127
|
+
case var
|
128
|
+
when NilClass
|
129
|
+
@body[:"_#{attrs}"] = nil
|
130
|
+
when String
|
131
|
+
unless var.include?("/")
|
132
|
+
raise Arango::Error.new err: :attribute_is_not_valid, data:
|
133
|
+
{"attribute": attrs, "wrong_value": var}
|
134
|
+
end
|
135
|
+
@body[:"_#{attrs}"] = var
|
136
|
+
when Arango::Document
|
137
|
+
@body[:"_#{attrs}"] = var.id
|
138
|
+
@from = var if attrs == "from"
|
139
|
+
@to = var if attrs == "to"
|
140
|
+
else
|
141
|
+
raise Arango::Error.new err: :attribute_is_not_valid, data:
|
142
|
+
{"attribute": attrs, "wrong_value": var}
|
143
|
+
end
|
144
|
+
end
|
145
|
+
private :set_up_from_or_to
|
146
|
+
|
147
|
+
def from(string: false)
|
148
|
+
return @body[:_from] if string
|
149
|
+
@from ||= retrieve_instance_from_and_to(@body[:_from])
|
150
|
+
return @from
|
151
|
+
end
|
152
|
+
|
153
|
+
def to(string: false)
|
154
|
+
return @body[:_to] if string
|
155
|
+
@to ||= retrieve_instance_from_and_to(@body[:_to])
|
156
|
+
return @to
|
157
|
+
end
|
158
|
+
|
159
|
+
def retrieve_instance_from_and_to(var)
|
160
|
+
case var
|
161
|
+
when NilClass
|
162
|
+
return nil
|
163
|
+
when String
|
164
|
+
collection_name, document_name = var.split("/")
|
165
|
+
collection = Arango::Collection.new name: collection_name, database: @database
|
166
|
+
if @graph.nil?
|
167
|
+
return Arango::Document.new(name: document_name, collection: collection)
|
168
|
+
else
|
169
|
+
collection.graph = @graph
|
170
|
+
return Arango::Vertex.new(name: document_name, collection: collection)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
private :retrieve_instance_from_and_to
|
175
|
+
|
176
|
+
# == GET ==
|
177
|
+
|
178
|
+
def retrieve(if_none_match: false, if_match: false)
|
179
|
+
headers = {}
|
180
|
+
headers[:"If-None-Match"] = @body[:_rev] if if_none_match
|
181
|
+
headers[:"If-Match"] = @body[:_rev] if if_match
|
182
|
+
result = @database.request("GET", "_api/document/#{@body[:_id]}", headers: headers)
|
183
|
+
return_element(result)
|
184
|
+
end
|
185
|
+
|
186
|
+
# == HEAD ==
|
187
|
+
|
188
|
+
def head(if_none_match: false, if_match: false)
|
189
|
+
headers = {}
|
190
|
+
headers[:"If-None-Match"] = @body[:_rev] if if_none_match
|
191
|
+
headers[:"If-Match"] = @body[:_rev] if if_match
|
192
|
+
@database.request("HEAD", "_api/document/#{@body[:_id]}", headers: headers)
|
193
|
+
end
|
194
|
+
|
195
|
+
# == POST ==
|
196
|
+
|
197
|
+
def create(body: {}, waitForSync: nil, returnNew: nil, silent: nil)
|
198
|
+
body = @body.merge(body)
|
199
|
+
query = {
|
200
|
+
"waitForSync": waitForSync,
|
201
|
+
"returnNew": returnNew,
|
202
|
+
"silent": silent
|
203
|
+
}
|
204
|
+
result = @database.request("POST", "_api/document/#{@collection.name}", body: body,
|
205
|
+
query: query)
|
206
|
+
return result if @server.async != false || silent
|
207
|
+
body2 = result.clone
|
208
|
+
if returnNew
|
209
|
+
body2.delete(:new)
|
210
|
+
body2 = body2.merge(result[:new])
|
211
|
+
end
|
212
|
+
body = body.merge(body2)
|
213
|
+
assign_attributes(body)
|
214
|
+
return return_directly?(result) ? result : self
|
215
|
+
end
|
216
|
+
|
217
|
+
# == PUT ==
|
218
|
+
|
219
|
+
def replace(body: {}, waitForSync: nil, ignoreRevs: nil, returnOld: nil,
|
220
|
+
returnNew: nil, silent: nil, if_match: false)
|
221
|
+
query = {
|
222
|
+
"waitForSync": waitForSync,
|
223
|
+
"returnNew": returnNew,
|
224
|
+
"returnOld": returnOld,
|
225
|
+
"ignoreRevs": ignoreRevs,
|
226
|
+
"silent": silent
|
227
|
+
}
|
228
|
+
headers = {}
|
229
|
+
headers[:"If-Match"] = @body[:_rev] if if_match
|
230
|
+
result = @database.request("PUT", "_api/document/#{@body[:_id]}", body: body,
|
231
|
+
query: query, headers: headers)
|
232
|
+
return result if @server.async != false || silent
|
233
|
+
body2 = result.clone
|
234
|
+
if returnNew
|
235
|
+
body2.delete(:new)
|
236
|
+
body2 = body2.merge(result[:new])
|
237
|
+
end
|
238
|
+
body = body.merge(body2)
|
239
|
+
assign_attributes(body)
|
240
|
+
return return_directly?(result) ? result : self
|
241
|
+
end
|
242
|
+
|
243
|
+
def update(body: {}, waitForSync: nil, ignoreRevs: nil,
|
244
|
+
returnOld: nil, returnNew: nil, keepNull: nil,
|
245
|
+
mergeObjects: nil, silent: nil, if_match: false)
|
246
|
+
query = {
|
247
|
+
"waitForSync": waitForSync,
|
248
|
+
"returnNew": returnNew,
|
249
|
+
"returnOld": returnOld,
|
250
|
+
"ignoreRevs": ignoreRevs,
|
251
|
+
"keepNull": keepNull,
|
252
|
+
"mergeObjects": mergeObjects,
|
253
|
+
"silent": silent
|
254
|
+
}
|
255
|
+
headers = {}
|
256
|
+
headers[:"If-Match"] = @body[:_rev] if if_match
|
257
|
+
result = @database.request("PATCH", "_api/document/#{@body[:_id]}", body: body,
|
258
|
+
query: query, headers: headers, keepNull: keepNull)
|
259
|
+
return result if @server.async != false || silent
|
260
|
+
body2 = result.clone
|
261
|
+
if returnNew
|
262
|
+
body2.delete(:new)
|
263
|
+
body2 = body2.merge(result[:new])
|
264
|
+
end
|
265
|
+
body = body.merge(body2)
|
266
|
+
if mergeObjects
|
267
|
+
@body = @body.merge(body)
|
268
|
+
else
|
269
|
+
body.each{|key, value| @body[key] = value}
|
270
|
+
end
|
271
|
+
assign_attributes(@body)
|
272
|
+
return return_directly?(result) ? result : self
|
273
|
+
end
|
274
|
+
|
275
|
+
# === DELETE ===
|
276
|
+
|
277
|
+
def destroy(waitForSync: nil, silent: nil, returnOld: nil, if_match: false)
|
278
|
+
query = {
|
279
|
+
"waitForSync": waitForSync,
|
280
|
+
"returnOld": returnOld,
|
281
|
+
"silent": silent
|
282
|
+
}
|
283
|
+
headers = {}
|
284
|
+
headers[:"If-Match"] = @body[:_rev] if if_match
|
285
|
+
result = @database.request("DELETE", "_api/document/#{@body[:_id]}", query: query,
|
286
|
+
headers: headers)
|
287
|
+
return result if @server.async != false || silent
|
288
|
+
body2 = result.clone
|
289
|
+
if returnOld
|
290
|
+
body2.delete(:old)
|
291
|
+
body2 = body2.merge(result[:old])
|
292
|
+
else
|
293
|
+
body2 = body2.merge(@body)
|
294
|
+
end
|
295
|
+
return_element(body2)
|
296
|
+
return true
|
297
|
+
end
|
298
|
+
|
299
|
+
# === EDGE ===
|
300
|
+
|
301
|
+
def edges(collection:, direction: nil)
|
302
|
+
satisfy_class?(collection, [Arango::Collection, String])
|
303
|
+
collection = collection.is_a?(Arango::Collection) ? collection.name : collection
|
304
|
+
query = {
|
305
|
+
"vertex": @body[:_id],
|
306
|
+
"direction": direction
|
307
|
+
}
|
308
|
+
result = @database.request("GET", "_api/edges/#{collection}", query: query)
|
309
|
+
return result if return_directly?(result)
|
310
|
+
result[:edges].map do |edge|
|
311
|
+
collection_name, key = edge[:_id].split("/")
|
312
|
+
collection = Arango::Collection.new(name: collection_name,
|
313
|
+
database: @database, type: :edge)
|
314
|
+
Arango::Document.new(name: key, body: edge, collection: collection)
|
315
|
+
end
|
316
|
+
end
|
317
|
+
|
318
|
+
def any(collection)
|
319
|
+
edges(collection: collection)
|
320
|
+
end
|
321
|
+
|
322
|
+
def out(collection)
|
323
|
+
edges(collection: collection, direction: "out")
|
324
|
+
end
|
325
|
+
|
326
|
+
def in(collection)
|
327
|
+
edges(collection: collection, direction: "in")
|
328
|
+
end
|
329
|
+
|
330
|
+
# === TRAVERSAL ===
|
331
|
+
|
332
|
+
def traversal(body: {}, sort: nil, direction: nil, minDepth: nil,
|
333
|
+
visitor: nil, itemOrder: nil, strategy: nil,
|
334
|
+
filter: nil, init: nil, maxIterations: nil, maxDepth: nil,
|
335
|
+
uniqueness: nil, order: nil, expander: nil,
|
336
|
+
edgeCollection: nil)
|
337
|
+
Arango::Traversal.new(body: body,
|
338
|
+
sort: sort, direction: direction, minDepth: minDepth,
|
339
|
+
vertex: self, visitor: visitor,itemOrder: itemOrder,
|
340
|
+
strategy: strategy, filter: filter, init: init,
|
341
|
+
maxIterations: maxIterations, maxDepth: maxDepth,
|
342
|
+
uniqueness: uniqueness, order: order,
|
343
|
+
expander: expander, edgeCollection: edgeCollection)
|
344
|
+
end
|
345
|
+
end
|
346
|
+
end
|