arangorb3 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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