Class: Arango::Graph

Inherits:
Object
  • Object
show all
Includes:
Database_Return, Helper_Error, Helper_Return
Defined in:
lib/Graph.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Database_Return

#database=

Methods included from Helper_Return

#return_delete, #return_directly?, #return_element

Methods included from Helper_Error

#satisfy_category?, #satisfy_class?, #warning_deprecated

Constructor Details

#initialize(name:, database:, edgeDefinitions: [], orphanCollections: [], body: {}, numberOfShards: nil, isSmart: nil, smartGraphAtttribute: nil, replicationFactor: nil, cache_name: nil) ⇒ Graph

Returns a new instance of Graph.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/Graph.rb', line 30

def initialize(name:, database:, edgeDefinitions: [],
  orphanCollections: [], body: {}, numberOfShards: nil, isSmart: nil,
  smartGraphAtttribute: nil, replicationFactor: nil, cache_name: nil)
  assign_database(database)
  unless cache_name.nil?
    @cache_name = cache_name
    @server.cache.save(:graph, cache_name, self)
  end
  body[:_key]    ||= name
  body[:_id]     ||= "_graphs/#{name}"
  body[:isSmart] ||= isSmart
  body[:edgeDefinitions]     ||= edgeDefinitions
  body[:orphanCollections]   ||= orphanCollections
  body[:numberOfShards]      ||= numberOfShards
  body[:replicationFactor]   ||= replicationFactor
  body[:smartGraphAttribute] ||= smartGraphAttribute
  assign_attributes(body)
end

Instance Attribute Details

#bodyObject

DEFINE ===



51
52
53
# File 'lib/Graph.rb', line 51

def body
  @body
end

#cache_nameObject (readonly)

DEFINE ===



51
52
53
# File 'lib/Graph.rb', line 51

def cache_name
  @cache_name
end

#databaseObject (readonly)

DEFINE ===



51
52
53
# File 'lib/Graph.rb', line 51

def database
  @database
end

#idObject (readonly)

DEFINE ===



51
52
53
# File 'lib/Graph.rb', line 51

def id
  @id
end

#isSmartObject (readonly)

DEFINE ===



51
52
53
# File 'lib/Graph.rb', line 51

def isSmart
  @isSmart
end

#nameObject Also known as: key

DEFINE ===



51
52
53
# File 'lib/Graph.rb', line 51

def name
  @name
end

#numberOfShardsObject

Returns the value of attribute numberOfShards.



52
53
54
# File 'lib/Graph.rb', line 52

def numberOfShards
  @numberOfShards
end

#replicationFactorObject

Returns the value of attribute replicationFactor.



52
53
54
# File 'lib/Graph.rb', line 52

def replicationFactor
  @replicationFactor
end

#revObject (readonly)

DEFINE ===



51
52
53
# File 'lib/Graph.rb', line 51

def rev
  @rev
end

#serverObject (readonly)

DEFINE ===



51
52
53
# File 'lib/Graph.rb', line 51

def server
  @server
end

#smartGraphAttributeObject

Returns the value of attribute smartGraphAttribute.



52
53
54
# File 'lib/Graph.rb', line 52

def smartGraphAttribute
  @smartGraphAttribute
end

Class Method Details

.new(*args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/Graph.rb', line 9

def self.new(*args)
  hash = args[0]
  super unless hash.is_a?(Hash)
  database = hash[:database]
  if database.is_a?(Arango::Database) && database.server.active_cache
    cache_name = "#{database.name}/#{hash[:name]}"
    cached = database.server.cache.cache.dig(:graph, cache_name)
    if cached.nil?
      hash[:cache_name] = cache_name
      return super
    else
      body = hash[:body] || {}
      [:isSmart, :edgeDefinitions, :orphanCollections, :numberOfShards,
        :replicationFactor, :smartGraphAttribute].each{|k| body[k] ||= hash[k]}
      cached.assign_attributes(body)
      return cached
    end
  end
  super
end

Instance Method Details

#addEdgeDefinition(collection:, from:, to:) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/Graph.rb', line 289

def addEdgeDefinition(collection:, from:, to:)
  satisfy_class?(collection, [String, Arango::Collection])
  satisfy_class?(from, [String, Arango::Collection], true)
  satisfy_class?(to, [String, Arango::Collection], true)
  from = [from] unless from.is_a?(Array)
  to = [to] unless to.is_a?(Array)
  body = {}
  body[:collection] = collection.is_a?(String) ? collection : collection.name
  body[:from] = from.map{|f| f.is_a?(String) ? f : f.name }
  body[:to] = to.map{|t| t.is_a?(String) ? t : t.name }
  result = request("POST", "edge", body: body, key: :graph)
  return_element(result)
end

#addVertexCollection(collection:) ⇒ Object



264
265
266
267
268
269
270
# File 'lib/Graph.rb', line 264

def addVertexCollection(collection:)
  satisfy_class?(collection, [String, Arango::Collection])
  collection = collection.is_a?(String) ? collection : collection.name
  body = { "collection": collection }
  result = request("POST", "vertex", body: body, key: :graph)
  return_element(result)
end

#create(isSmart: @isSmart, smartGraphAttribute: @smartGraphAttribute, numberOfShards: @numberOfShards) ⇒ Object

POST ===



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/Graph.rb', line 226

def create(isSmart: @isSmart, smartGraphAttribute: @smartGraphAttribute,
  numberOfShards: @numberOfShards)
  body = {
    "name": @name,
    "edgeDefinitions":   edgeDefinitionsRaw,
    "orphanCollections": orphanCollectionsRaw,
    "isSmart": isSmart,
    "options": {
      "smartGraphAttribute": smartGraphAttribute,
      "numberOfShards": numberOfShards
    }
  }
  body[:options].delete_if{|k,v| v.nil?}
  body.delete(:options) if body[:options].empty?
  result = @database.request("POST", "_api/gharial", body: body, key: :graph)
  return_element(result)
end

#destroy(dropCollections: nil) ⇒ Object

DELETE ===



246
247
248
249
250
251
# File 'lib/Graph.rb', line 246

def destroy(dropCollections: nil)
  query = { "dropCollections": dropCollections }
  result = @database.request("DELETE", "_api/gharial/#{@name}", query: query,
    key: :removed)
  return_delete(result)
end

#edgeDefinitions(raw = false) ⇒ Object



102
103
104
105
# File 'lib/Graph.rb', line 102

def edgeDefinitions(raw=false)
  return edgeDefinitionsRaw if raw
  return @edgeDefinitions
end

#edgeDefinitions=(edgeDefinitions) ⇒ Object Also known as: assign_edgeDefinitions



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/Graph.rb', line 107

def edgeDefinitions=(edgeDefinitions)
  @edgeDefinitions = []
  edgeDefinitions ||= []
  edgeDefinitions = [edgeDefinitions] unless edgeDefinitions.is_a?(Array)
  edgeDefinitions.each do |edgeDefinition|
    hash = {}
    hash[:collection] = return_collection(edgeDefinition[:collection], :edge)
    edgeDefinition[:from] ||= []
    edgeDefinition[:to]   ||= []
    hash[:from] = edgeDefinition[:from].map{|t| return_collection(t)}
    hash[:to]   = edgeDefinition[:to].map{|t| return_collection(t)}
    setup_orphaCollection_after_adding_edge_definitions(hash)
    @edgeDefinitions << hash
  end
end

#getEdgeCollectionsObject

EDGE COLLECTION ===



282
283
284
285
286
287
# File 'lib/Graph.rb', line 282

def getEdgeCollections
  result = request("GET", "edge", key: :collections)
  return result if @database.server.async != false
  return result if return_directly?(result)
  result.map{|r| Arango::Collection.new(database: @database, name: r, type: :edge)}
end

#getVertexCollectionsObject Also known as: vertexCollections

VERTEX COLLECTION ===



255
256
257
258
259
260
261
# File 'lib/Graph.rb', line 255

def getVertexCollections
  result = request("GET", "vertex", key: :collections)
  return result if return_directly?(result)
  result.map do |x|
    Arango::Collection.new(name: x, database: @database, graph: self)
  end
end

#orphanCollections(raw = false) ⇒ Object



137
138
139
140
# File 'lib/Graph.rb', line 137

def orphanCollections(raw=false)
  return orphanCollectionsRaw if raw
  return @orphanCollections
end

#orphanCollections=(orphanCollections) ⇒ Object Also known as: assign_orphanCollections



124
125
126
127
128
# File 'lib/Graph.rb', line 124

def orphanCollections=(orphanCollections)
  orphanCollections ||= []
  orphanCollections = [orphanCollections] unless orphanCollections.is_a?(Array)
  @orphanCollections = orphanCollections.map{|oc| add_orphan_collection(oc)}
end

#removeEdgeDefinition(collection:, dropCollection: nil) ⇒ Object



317
318
319
320
321
322
323
# File 'lib/Graph.rb', line 317

def removeEdgeDefinition(collection:, dropCollection: nil)
  satisfy_class?(collection, [String, Arango::Collection])
  query = {"dropCollection": dropCollection}
  collection = collection.is_a?(String) ? collection : collection.name
  result = request("DELETE", "edge/#{collection}", query: query, key: :graph)
  return_element(result)
end

#removeVertexCollection(collection:, dropCollection: nil) ⇒ Object



272
273
274
275
276
277
278
# File 'lib/Graph.rb', line 272

def removeVertexCollection(collection:, dropCollection: nil)
  query = {"dropCollection": dropCollection}
  satisfy_class?(collection, [String, Arango::Collection])
  collection = collection.is_a?(String) ? collection : collection.name
  result = request("DELETE", "vertex/#{collection}", query: query, key: :graph)
  return_element(result)
end

#replaceEdgeDefinition(collection:, from:, to:) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/Graph.rb', line 303

def replaceEdgeDefinition(collection:, from:, to:)
  satisfy_class?(collection, [String, Arango::Collection])
  satisfy_class?(from, [String, Arango::Collection], true)
  satisfy_class?(to, [String, Arango::Collection], true)
  from = [from] unless from.is_a?(Array)
  to = [to] unless to.is_a?(Array)
  body = {}
  body[:collection] = collection.is_a?(String) ? collection : collection.name
  body[:from] = from.map{|f| f.is_a?(String) ? f : f.name }
  body[:to] = to.map{|t| t.is_a?(String) ? t : t.name }
  result = request("PUT", "edge/#{body[:collection]}", body: body, key: :graph)
  return_element(result)
end

#request(action, url, body: {}, headers: {}, query: {}, key: nil, return_direct_result: false, skip_to_json: false) ⇒ Object

REQUEST ===



192
193
194
195
196
197
# File 'lib/Graph.rb', line 192

def request(action, url, body: {}, headers: {}, query: {}, key: nil, return_direct_result: false, skip_to_json: false)
  url = "_api/gharial/#{@name}/#{url}"
  @database.request(action, url, body: body, headers: headers,
    query: query, key: key, return_direct_result: return_direct_result,
    skip_to_json: skip_to_json)
end

#retrieveObject

GET ===



219
220
221
222
# File 'lib/Graph.rb', line 219

def retrieve
  result = @database.request("GET", "_api/gharial/#{@name}", key: :graph)
  return_element(result)
end

#return_collection(collection, type = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/Graph.rb', line 79

def return_collection(collection, type=nil)
  satisfy_class?(collection, [Arango::Collection, String])
  case collection
  when Arango::Collection
    return collection
  when String
    return Arango::Collection.new(name: collection,
      database: @database, type: type, graph: self)
  end
end

#to_hObject

TO HASH ===



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/Graph.rb', line 201

def to_h
  {
    "name": @name,
    "id": @id,
    "rev": @rev,
    "isSmart": @isSmart,
    "numberOfShards": @numberOfShards,
    "replicationFactor": @replicationFactor,
    "smartGraphAttribute": @smartGraphAttribute,
    "edgeDefinitions": edgeDefinitionsRaw,
    "orphanCollections": orphanCollectionsRaw,
    "cache_name": @cache_name,
    "database": @database.name
  }.delete_if{|k,v| v.nil?}
end