Class: ActiveOrient::OrientDB

Inherits:
Object
  • Object
show all
Includes:
ClassUtils, DatabaseUtils, OrientDbPrivate, OrientSupport::Support, RestChange, RestCreate, RestDelete, RestOperations, RestRead
Defined in:
lib/rest/rest.rb

Overview

OrientDB performs queries to a OrientDB-Database The communication is based on the ActiveOrient-API. The OrientDB-Server is specified in config/connect.yml A Sample:

:orientdb:
  :server: localhost
    :port: 2480
  :database: working-database
  :admin:
    :user: admin-user
    :pass: admin-password

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RestDelete

#delete_class, #delete_database, #delete_property, #delete_record, #delete_records

Methods included from RestOperations

#call_function, #count, #execute

Methods included from RestChange

#alter_property, #change_database, #patch_record, #update, #update_records

Methods included from RestCreate

#create_classes, #create_database, #create_index, #create_multiple_records, #create_properties, #create_property, #create_record, #upsert

Methods included from RestRead

#get_class_properties, #get_classes, #get_databases, #get_record, #get_records, #print_class_properties

Methods included from ClassUtils

#allocate_classes_in_ruby, #classname, #create_class, #create_edge, #delete_edge, #delete_vertex, #update_or_create_records

Methods included from DatabaseUtils

#class_hierarchy, #create_edge_class, #create_vertex_class, #database_classes, #fv, #fx, #get_db_superclass, #preallocate_classes, #system_classes

Methods included from OrientSupport::Support

#compose_where, #generate_sql_list

Constructor Details

#initialize(database: nil, connect: true, preallocate: true, model_dir: nil) ⇒ OrientDB

Contructor: OrientDB is conventionally initialized.

Thus several instances pointing to the same or different databases can coexist

A simple
 xyz =  ActiveOrient::OrientDB.new
uses the database specified in the yaml-file »config/connect.yml« and connects
 xyz = ActiveOrient::OrientDB.new database: my_fency_database
accesses the database »my_fency_database«. The database is created if its not existing.

*USECASE*
 xyz =  ActiveOrient::Model.orientdb = ActiveOrient::OrientDB.new
initialises the Database-Connection and publishes the Instance to any ActiveOrient::Model-Object


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rest/rest.rb', line 56

def initialize database: nil, connect: true, preallocate: true, model_dir: nil
  self.logger = Logger.new('/dev/stdout') unless logger.present?
#  self.default_server = {
#    :server => 'localhost',
#    :port => 2480,
#    :protocol => 'http',
#    :user => 'root',
#    :password => 'root',
#    :database => 'temp'
#  }.merge default_server.presence || {}
#  @res = get_resource
  ActiveOrient.database = database if database.present?
  @res = get_resource
  connect() if connect
  database_classes # initialize @classes-array
  ActiveOrient::Model.orientdb = self 
  ActiveOrient::Model.db = self 
  ActiveOrient::Model.keep_models_without_file ||= nil
  preallocate_classes(model_dir)  if preallocate

end

Instance Attribute Details

#databaseObject (readonly)

Used to read the working database



37
38
39
# File 'lib/rest/rest.rb', line 37

def database
  @database
end

Instance Method Details

#connectObject

Used to connect to the database



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rest/rest.rb', line 86

def connect
  first_tentative = true
  begin
	database =  ActiveOrient.database
    logger.progname = 'OrientDB#Connect'
    r = @res["/connect/#{database}"].get
    if r.code == 204
     logger.info{"Connected to database #{database}"}
     true
   else
     logger.error{"Connection to database #{database} could NOT be established"}
     nil
   end
  rescue RestClient::Unauthorized => e
    if first_tentative
     logger.info{"Database #{database} NOT present --> creating"}
     first_tentative = false
     create_database database: database
     retry
    else
     Kernel.exit
    end
  end
end

#get_resourceObject



78
79
80
81
82
# File 'lib/rest/rest.rb', line 78

def get_resource
   = [ActiveOrient.default_server[:user].to_s , ActiveOrient.default_server[:password].to_s]
  server_adress = "http://#{ActiveOrient.default_server[:server]}:#{ActiveOrient.default_server[:port]}"
  RestClient::Resource.new(server_adress, *)
end