class Orientdb4r::Client

Constants

SERVER_VERSION_PATTERN

Regexp to validate format of provided version.

Attributes

connection_library[R]

type of connection library [:restclient, :excon]

database[R]

connection parameters

lb_strategy[R]

object implementing a LB strategy

load_balancing[R]

type of load balancing [:sequence, :round_robin]

nodes[R]

nodes responsible for communication with a server

password[R]

connection parameters

proxy[R]

proxy for remote communication

user[R]

connection parameters

Public Class Methods

new() click to toggle source

Constructor.

# File lib/orientdb4r/client.rb, line 29
def initialize
  @nodes = []
  @connected = false
end

Public Instance Methods

class_exists?(name) click to toggle source

Checks existence of a given class.

# File lib/orientdb4r/client.rb, line 208
def class_exists?(name)
  rslt = true
  begin
    get_class name
  rescue OrientdbError => e
    raise e if e.is_a? ConnectionError and e.message == 'not connected' # workaround for AOP2 (unable to decorate already existing methods)
    rslt = false
  end
  rslt
end
command(sql) click to toggle source

Executes a command against the database.

# File lib/orientdb4r/client.rb, line 145
def command(sql)
  raise NotImplementedError, 'this should be overridden by concrete client'
end
connect(options) click to toggle source

Connects client to the server.

# File lib/orientdb4r/client.rb, line 39
def connect options
  raise NotImplementedError, 'this should be overridden by concrete client'
end
connected?() click to toggle source

Gets flag whenever the client is connected or not.

# File lib/orientdb4r/client.rb, line 53
def connected?
  @connected
end
create_class(name, options={}) { |proxy| ... } click to toggle source

Creates a new class in the schema.

# File lib/orientdb4r/client.rb, line 154
def create_class(name, options={})
  raise ArgumentError, "class name is blank" if blank?(name)
  opt_pattern = {
      :extends => :optional, :cluster => :optional, :force => false, :abstract => false,
      :properties => :optional
  }
  verify_options(options, opt_pattern)

  sql = "CREATE CLASS #{name}"
  sql << " EXTENDS #{options[:extends]}" if options.include? :extends
  sql << " CLUSTER #{options[:cluster]}" if options.include? :cluster
  sql << ' ABSTRACT' if options.include?(:abstract)

  drop_class name if options[:force]

  command sql

  # properties given?
  if options.include? :properties
    props = options[:properties]
    raise ArgumentError, 'properties have to be an array' unless props.is_a? Array

    props.each do |prop|
      raise ArgumentError, 'property definition has to be a hash' unless prop.is_a? Hash
      prop_name = prop.delete :property
      prop_type = prop.delete :type
      create_property(name, prop_name, prop_type, prop)
    end
  end

  if block_given?
    proxy = Orientdb4r::Utils::Proxy.new(self, name)
    def proxy.property(property, type, options={})
      self.target.send :create_property, self.context, property, type, options
    end
    def proxy.link(property, type, linked_class, options={})
      raise ArgumentError, "type has to be a linked-type, given=#{type}" unless type.to_s.start_with? 'link'
      options[:linked_class] = linked_class
      self.target.send :create_property, self.context, property, type, options
    end
    yield proxy
  end
end
create_database(options) click to toggle source

Creates a new database. You can provide an additional authentication to the server with 'database.create' resource or the current one will be used. *options

*storage - 'memory' (by default) or 'local'
*type - 'document' (by default) or 'graph'
# File lib/orientdb4r/client.rb, line 76
def create_database(options)
  raise NotImplementedError, 'this should be overridden by concrete client'
end
create_document(doc) click to toggle source

Create a new document. Returns the Record-id assigned for OrientDB version <= 1.3.x and the whole new document for version >= 1.4.x (see groups.google.com/forum/?fromgroups=#!topic/orient-database/UJGAXYpHDmo for more info).

# File lib/orientdb4r/client.rb, line 277
def create_document(doc)
  raise NotImplementedError, 'this should be overridden by concrete client'
end
create_property(clazz, property, type, options={}) click to toggle source

Creates a new property in the schema. You need to create the class before.

# File lib/orientdb4r/client.rb, line 243
def create_property(clazz, property, type, options={})
  raise ArgumentError, "class name is blank" if blank?(clazz)
  raise ArgumentError, "property name is blank" if blank?(property)
  opt_pattern = {
    :mandatory => :optional , :notnull => :optional, :min => :optional, :max => :optional,
    :readonly =>  :optional, :linked_class => :optional
  }
  verify_options(options, opt_pattern)

  cmd = "CREATE PROPERTY #{clazz}.#{property} #{type.to_s}"
  # link?
  if [:link, :linklist, :linkset, :linkmap].include? type.to_s.downcase.to_sym
    raise ArgumentError, "defined linked-type, but not linked-class" unless options.include? :linked_class
    cmd << " #{options[:linked_class]}"
  end
  command cmd

  # ALTER PROPERTY ...
  options.delete :linked_class # it's not option for ALTER
  unless options.empty?
    options.each do |k,v|
      command "ALTER PROPERTY #{clazz}.#{property} #{k.to_s.upcase} #{v}"
    end
  end
end
database_exists?(options) click to toggle source

Checks existence of a given database. Client has not to be connected to see databases suitable to connect.

# File lib/orientdb4r/client.rb, line 92
def database_exists?(options)
  rslt = true
  begin
    get_database options
  rescue OrientdbError
    rslt = false
  end
  rslt
end
delete_database(options) click to toggle source

Drops a database. Requires additional authentication to the server.

# File lib/orientdb4r/client.rb, line 106
def delete_database(options)
  raise NotImplementedError, 'this should be overridden by concrete client'
end
delete_document(rid) click to toggle source

Deletes an existing document.

# File lib/orientdb4r/client.rb, line 298
def delete_document(rid)
  raise NotImplementedError, 'this should be overridden by concrete client'
end
disconnect() click to toggle source

Disconnects client from the server.

# File lib/orientdb4r/client.rb, line 46
def disconnect
  raise NotImplementedError, 'this should be overridden by concrete client'
end
drop_class(name, options={}) click to toggle source

Removes a class from the schema.

# File lib/orientdb4r/client.rb, line 222
def drop_class(name, options={})
  raise ArgumentError, 'class name is blank' if blank?(name)

  # :mode=>:strict forbids to drop a class that is a super class for other one
  opt_pattern = { :mode => :nil }
  verify_options(options, opt_pattern)
  if :strict == options[:mode]
    response = get_database
    children = response['classes'].select { |i| i['superClass'] == name }
    unless children.empty?
      raise OrientdbError, "class is super-class, cannot be deleted, name=#{name}"
    end
  end

  command "DROP CLASS #{name}"
end
export(options) click to toggle source

Exports a gzip file that contains the database JSON export. Returns name of stored file.

# File lib/orientdb4r/client.rb, line 123
def export(options)
  raise NotImplementedError, 'this should be overridden by concrete client'
end
get_class(name) click to toggle source

Gets informations about requested class.

# File lib/orientdb4r/client.rb, line 201
def get_class(name)
  raise NotImplementedError, 'this should be overridden by concrete client'
end
get_database(options) click to toggle source

Retrieves all the information about a database. Client has not to be connected to see databases suitable to connect.

# File lib/orientdb4r/client.rb, line 84
def get_database(options)
  raise NotImplementedError, 'this should be overridden by concrete client'
end
get_document(rid) click to toggle source

Retrieves a document by given ID.

# File lib/orientdb4r/client.rb, line 284
def get_document(rid)
  raise NotImplementedError, 'this should be overridden by concrete client'
end
import(options) click to toggle source

Imports a database from an uploaded JSON text file.

# File lib/orientdb4r/client.rb, line 130
def import(options)
  raise NotImplementedError, 'this should be overridden by concrete client'
end
list_databases(options) click to toggle source

Retrieves the available databases. That is protected by the resource “server.listDatabases” that by default is assigned to the guest (anonymous) user in orientdb-server-config.xml.

# File lib/orientdb4r/client.rb, line 115
def list_databases(options)
  raise NotImplementedError, 'this should be overridden by concrete client'
end
query(sql, options) click to toggle source

Executes a query against the database.

# File lib/orientdb4r/client.rb, line 138
def query(sql, options)
  raise NotImplementedError, 'this should be overridden by concrete client'
end
server(options={}) click to toggle source

Retrieve information about the connected OrientDB Server. Enables additional authentication to the server with an account that can access the 'server.info' resource.

# File lib/orientdb4r/client.rb, line 62
def server(options={})
  raise NotImplementedError, 'this should be overridden by concrete client'
end
update_document(doc) click to toggle source

Updates an existing document.

# File lib/orientdb4r/client.rb, line 291
def update_document(doc)
  raise NotImplementedError, 'this should be overridden by concrete client'
end

Protected Instance Methods

assert_connected() click to toggle source

Asserts if the client is connected and raises an error if not.

# File lib/orientdb4r/client.rb, line 343
def assert_connected
  raise ConnectionError, 'not connected' unless @connected
end
call_server(options) click to toggle source

Calls the server with a specific task. Returns a response according to communication channel (e.g. HTTP response).

# File lib/orientdb4r/client.rb, line 308
def call_server(options)
  lb_all_bad_msg = 'all nodes failed to communicate with server!'
  response = nil

  # credentials if not defined explicitly
  options[:user] = user unless options.include? :user
  options[:password] = password unless options.include? :password
  debug_string = options[:uri]
  if debug_string
    query_log("Orientdb4r::Client#call_server", URI.decode(debug_string))
  end
  idx = lb_strategy.node_index
  raise OrientdbError, lb_all_bad_msg if idx.nil? # no good node found

  begin
    node = @nodes[idx]
    begin
      response = node.request options
      lb_strategy.good_one idx
      return response

    rescue NodeError => e
      Orientdb4r::logger.error "node error, index=#{idx}, msg=#{e.message}, #{node}"
      node.cleanup
      lb_strategy.bad_one idx
      idx = lb_strategy.node_index
    end
  end until idx.nil? and response.nil? # both 'nil' <= we tried all nodes and all with problem

  raise OrientdbError, lb_all_bad_msg
end
query_log(context, message) click to toggle source
# File lib/orientdb4r/client.rb, line 357
def query_log(context, message)
  Orientdb4r::logger.debug \
    "  \033[01;33m#{context}:\033[0m #{message}"
end
time_around(&block) click to toggle source

Around advice to meassure and print the method time.

# File lib/orientdb4r/client.rb, line 350
def time_around(&block)
  start = Time.now
  rslt = block.call
  query_log("#{aop_context[:class].name}##{aop_context[:method]}", "elapsed time = #{Time.now - start} [s]")
  rslt
end