class ActiveOrient::OrientDB

OrientDB points to an OrientDB-Database. The communication is based on the OrientDB-REST-API.

Its usually initialised through ActiveOrient::Init.connect

Public Class Methods

new(database: nil, preallocate: true, model_dir: nil, **defaults) click to toggle source

OrientDB is conventionally initialized.

The first call initialises database-name and -classes, server-adress and user-credentials.

Subsequent initialisations are made to initialise namespaced database classes, ie.

ORD =  ActiveOrient.init.connect  database: 'temp'
                                                                                                                        server:  'localhost',
                                                                                                                        port:    2480,
                                                                                                                        user:   root, 
                                                                                                                        password: root 
module HC; end
ActiveOrient::Init.define_namespace { HC }
ActiveOrient::OrientDB.new  preallocate: true
# File lib/rest/rest.rb, line 59
    def initialize database: nil, preallocate: true, model_dir: nil, **defaults
      ActiveOrient.database ||= database || 'temp'
      ActiveOrient.database_classes ||=   Hash.new

                        ActiveOrient.default_server ||= { :server => defaults[:server] || 'localhost' ,
                                                                                                                                                                :port   => defaults[:port] ||= 2480,
                                                                                                                                                                :user   => defaults[:user].to_s ,
                                                                                                                                                                :password => defaults[:password].to_s }
                        # setup connection pool
                        # database-settings: client.channel.maxPool = 100
                        ActiveOrient.db_pool ||= Pond.new( :maximum_size => 25, :timeout => 50) {  get_resource }
#                       ActiveOrient.db_pool.collection = :stack
      connect() 
      database_classes # initialize @classes-array and ActiveOrient.database_classes
                        ActiveOrient::Base.logger =  logger
      ActiveOrient::Model.orientdb = self 
      ActiveOrient::Model.db = self 
      ActiveOrient::Model.keep_models_without_file ||= nil
      preallocate_classes( model_dir )  if preallocate
                        Thread.abort_on_exception = true
    end

Public Instance Methods

connect() click to toggle source

Used to connect to the database

# File lib/rest/rest.rb, line 93
def connect 
        first_tentative = true
        begin
                database =  ActiveOrient.database
                logger.progname = 'OrientDB#Connect'
                r = ActiveOrient.db_pool.checkout do | conn |
                        r = conn["/connect/#{database}"].get
                end
                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_resource() click to toggle source

thread safe method to allocate a resource

# File lib/rest/rest.rb, line 82
def get_resource
                    logger.debug {"ALLOCATING NEW RESOURCE --> #{ ActiveOrient.db_pool.size }" }
  login = [ActiveOrient.default_server[:user] , ActiveOrient.default_server[:password]]
  server_adress = "http://#{ActiveOrient.default_server[:server]}:#{ActiveOrient.default_server[:port]}"
                     RestClient::Resource.new(server_adress, *login)
end