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/AQL.rb ADDED
@@ -0,0 +1,155 @@
1
+ # === AQL ===
2
+
3
+ module Arango
4
+ class AQL
5
+ include Arango::Helper_Error
6
+ include Arango::Helper_Return
7
+ include Arango::Database_Return
8
+
9
+ def initialize(query:, database:, count: nil, batchSize: nil, cache: nil,
10
+ memoryLimit: nil, ttl: nil, bindVars: nil, failOnWarning: nil,
11
+ profile: nil, maxTransactionSize: nil, skipInaccessibleCollections: nil,
12
+ maxWarningCount: nil, intermediateCommitCount: nil,
13
+ satelliteSyncWait: nil, fullCount: nil, intermediateCommitSize: nil,
14
+ optimizer_rules: nil, maxPlans: nil)
15
+ satisfy_class?(query, [String])
16
+ @query = query
17
+ assign_database(database)
18
+
19
+ @count = count
20
+ @batchSize = batchSize
21
+ @cache = cache
22
+ @memoryLimit = memoryLimit
23
+ @ttl = ttl
24
+ @bindVars = bindVars
25
+
26
+ @quantity = nil
27
+ @hasMore = false
28
+ @id = ""
29
+ @result = []
30
+ @options = {}
31
+ # DEFINE
32
+ ["failOnWarning", "profile", "maxTransactionSize",
33
+ "skipInaccessibleCollections", "maxWarningCount", "intermediateCommitCount",
34
+ "satelliteSyncWait", "fullCount", "intermediateCommitSize",
35
+ "optimizer_rules", "maxPlans"].each do |param_name|
36
+ param = eval(param_name)
37
+ set_option(param, param_name)
38
+ define_singleton_method("#{param_name}=") do |value|
39
+ set_option(value, param_name)
40
+ end
41
+ end
42
+ end
43
+
44
+ # === DEFINE ===
45
+
46
+ attr_accessor :count, :query, :batchSize, :ttl, :cache, :options, :bindVars, :quantity
47
+ attr_reader :hasMore, :id, :result, :idCache, :failOnWarning, :profile,
48
+ :maxTransactionSize, :skipInaccessibleCollections, :maxWarningCount,
49
+ :intermediateCommitCount, :satelliteSyncWait, :fullCount, :server, :cached, :extra,
50
+ :intermediateCommitSize, :optimizer_rules, :maxPlans, :database
51
+ alias size batchSize
52
+ alias size= batchSize=
53
+
54
+ def set_option(attrs, name)
55
+ @options ||= {}
56
+ instance_variable_set("@#{name}", attrs)
57
+ unless attrs
58
+ name = "optimizer.rules" if name == "optimizer_rules"
59
+ @options[name] = attrs
60
+ end
61
+ @options.delete_if{|k,v| v.nil?}
62
+ @options = nil if @options.empty?
63
+ end
64
+ private :set_option
65
+
66
+ # === TO HASH ===
67
+
68
+ def to_h
69
+ {
70
+ "query": @query,
71
+ "result": @result,
72
+ "count": @count,
73
+ "quantity": @quantity,
74
+ "ttl": @ttl,
75
+ "cache": @cache,
76
+ "batchSize": @batchSize,
77
+ "bindVars": @bindVars,
78
+ "options": @options,
79
+ "idCache": @idCache,
80
+ "memoryLimit": @memoryLimit,
81
+ "database": @database.name
82
+ }.delete_if{|k,v| v.nil?}
83
+ end
84
+
85
+ # === REQUEST ===
86
+
87
+ def return_aql(result)
88
+ return result if @server.async != false
89
+ @extra = result[:extra]
90
+ @cached = result[:cached]
91
+ @quantity = result[:count]
92
+ @hasMore = result[:hasMore]
93
+ @id = result[:id]
94
+ if(result[:result][0].nil? || !result[:result][0].is_a?(Hash) || !result[:result][0].key?(:_key))
95
+ @result = result[:result]
96
+ else
97
+ @result = result[:result].map do |x|
98
+ collection = Arango::Collection.new(name: x[:_id].split("/")[0], database: @database)
99
+ Arango::Document.new(name: x[:_key], collection: collection, body: x)
100
+ end
101
+ end
102
+ return return_directly?(result) ? result: self
103
+ end
104
+ private :return_aql
105
+
106
+ # === EXECUTE QUERY ===
107
+
108
+ def execute
109
+ body = {
110
+ "query": @query,
111
+ "count": @count,
112
+ "batchSize": @batchSize,
113
+ "ttl": @ttl,
114
+ "cache": @cache,
115
+ "options": @options,
116
+ "bindVars": @bindVars,
117
+ "memoryLimit": @memoryLimit
118
+ }
119
+ result = @database.request("POST", "_api/cursor", body: body)
120
+ return_aql(result)
121
+ end
122
+
123
+ def next
124
+ if @hasMore
125
+ result = @database.request("PUT", "_api/cursor/#{@id}")
126
+ return_aql(result)
127
+ else
128
+ raise Arango::Error.new err::no_other_aql_next, data: {"hasMore": false}
129
+ end
130
+ end
131
+
132
+ def destroy
133
+ @database.request("DELETE", "_api/cursor/#{@id}")
134
+ end
135
+
136
+ def kill
137
+ @database.request("DELETE", "_api/query/#{@id}")
138
+ end
139
+
140
+ # === PROPERTY QUERY ===
141
+
142
+ def explain
143
+ body = {
144
+ "query": @query,
145
+ "options": @options,
146
+ "bindVars": @bindVars
147
+ }
148
+ @database.request("POST", "_api/explain", body: body)
149
+ end
150
+
151
+ def parse
152
+ @database.request("POST", "_api/query", body: {"query": @query})
153
+ end
154
+ end
155
+ end
data/lib/Batch.rb ADDED
@@ -0,0 +1,97 @@
1
+ module Arango
2
+ class Batch
3
+ include Arango::Helper_Error
4
+ include Arango::Helper_Return
5
+ include Arango::Server_Return
6
+
7
+ def initialize(server:, boundary: "XboundaryX", queries: [])
8
+ @id = 1
9
+ assign_server(server)
10
+ assign_queries(queries)
11
+ @headers = {
12
+ "Content-Type": "multipart/form-data",
13
+ "boundary": boundary
14
+ }
15
+ @boundary = boundary
16
+ end
17
+
18
+ # === DEFINE ===
19
+
20
+ attr_reader :server, :boundary, :queries
21
+
22
+ def boundary=(boundary)
23
+ @boundary = boundary
24
+ @headers[:boundary] = boundary
25
+ end
26
+
27
+ def queries=(queries)
28
+ queries = [queries] unless queries.is_a?(Array)
29
+ @queries = {}
30
+ queries.each do |query|
31
+ begin
32
+ query.keys.each do |key|
33
+ query[(key.to_sym rescue key) || key] = query.delete(key)
34
+ end
35
+ rescue
36
+ raise Arango::Error.new(err: :batch_query_not_valid,
37
+ data: {wrong_query: query})
38
+ end
39
+ satisfy_class?(query, [Hash])
40
+ if query[:id].nil?
41
+ query[:id] = @id.to_s
42
+ @id += 1
43
+ end
44
+ @queries[query[:id]] = query
45
+ end
46
+ return @queries
47
+ end
48
+ alias assign_queries queries=
49
+
50
+ # === TO HASH ===
51
+
52
+ def to_h
53
+ {
54
+ "boundary": @boundary,
55
+ "queries": @queries,
56
+ "database": @database.name
57
+ }.delete_if{|k,v| v.nil?}
58
+ end
59
+
60
+ # === QUERY ===
61
+
62
+ def addQuery(id: @id, method:, address:, body: nil)
63
+ id = id.to_s
64
+ @queries[id] = {
65
+ "id": id,
66
+ "method": method,
67
+ "address": address,
68
+ "body": body
69
+ }.delete_if{|k,v| v.nil?}
70
+ @id += 1
71
+ return @queries
72
+ end
73
+ alias modifyQuery addQuery
74
+
75
+ def removeQuery(id:)
76
+ @queries.delete(id)
77
+ return @queries
78
+ end
79
+
80
+ # === EXECUTE ===
81
+
82
+ def execute
83
+ body = ""
84
+ @queries.each do |id, query|
85
+ body += "--#{@boundary}\n"
86
+ body += "Content-Type: application/x-arango-batchpart\n"
87
+ body += "Content-Id: #{query[:id]}\n\n"
88
+ body += "#{query[:method]} "
89
+ body += "#{query[:address]} HTTP/1.1\n"
90
+ body += "\n#{Oj.dump(query[:body])}\n" unless query[:body].nil?
91
+ end
92
+ body += "--#{@boundary}--\n" if @queries.length > 0
93
+ @server.request("POST", "_api/batch", body: body, skip_to_json: true,
94
+ headers: @headers, skip_parsing: true)
95
+ end
96
+ end
97
+ end
data/lib/Cache.rb ADDED
@@ -0,0 +1,71 @@
1
+ module Arango
2
+ class Cache
3
+ def initialize
4
+ @max = {
5
+ database: 10,
6
+ collection: 20,
7
+ document: 200,
8
+ graph: 10,
9
+ index: 20,
10
+ user: 20,
11
+ task: 20,
12
+ view: 20,
13
+ foxx: 20
14
+ }
15
+
16
+ @cache = {
17
+ database: {},
18
+ collection: {},
19
+ document: {},
20
+ graph: {},
21
+ index: {},
22
+ aql: {},
23
+ user: {},
24
+ task: {},
25
+ view: {},
26
+ foxx: {}
27
+ }
28
+ end
29
+
30
+ attr_reader :cache, :max
31
+
32
+ def to_h
33
+ hash = {
34
+ "max": @max,
35
+ "cache": {}
36
+ }
37
+ @cache.each do |key, hash2|
38
+ next if hash2.empty?
39
+ hash[:cache][key] = hash2.keys
40
+ end
41
+ hash
42
+ end
43
+
44
+ def save(type, id, obj)
45
+ while @cache[type].length >= @max[type]
46
+ @cache[type].shift
47
+ end
48
+ @cache[type][id] = obj
49
+ end
50
+
51
+ def destroy(type, id)
52
+ @cache[type].delete(id)
53
+ end
54
+
55
+ def clear
56
+ @cache.each_key{|k| @cache[k] = {}}
57
+ end
58
+
59
+ def updateMax(type:, val:)
60
+ type = type.to_sym rescue type = :error
61
+ unless @max.has_key?(type.to_sym)
62
+ ArangoDB::Error.new :element_in_cache_does_not_exist,
63
+ {wrong_attribute: :type, wrong_value: type}
64
+ end
65
+ while @cache[type].length > val
66
+ @cache[type].shift
67
+ end
68
+ @max[type] = val
69
+ end
70
+ end
71
+ end