class Orientdb4r::Client
Constants
- SERVER_VERSION_PATTERN
Regexp to validate format of provided version.
Attributes
type of connection library [:restclient, :excon]
connection parameters
object implementing a LB strategy
type of load balancing [:sequence, :round_robin]
nodes responsible for communication with a server
connection parameters
proxy for remote communication
connection parameters
Public Class Methods
Constructor.
# File lib/orientdb4r/client.rb, line 27 def initialize @nodes = [] @connected = false end
Public Instance Methods
Checks existence of a given class.
# File lib/orientdb4r/client.rb, line 206 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
Executes a command against the database.
# File lib/orientdb4r/client.rb, line 143 def command(sql) raise NotImplementedError, 'this should be overridden by concrete client' end
Connects client to the server.
# File lib/orientdb4r/client.rb, line 37 def connect options raise NotImplementedError, 'this should be overridden by concrete client' end
Gets flag whenever the client is connected or not.
# File lib/orientdb4r/client.rb, line 51 def connected? @connected end
Creates a new class in the schema.
# File lib/orientdb4r/client.rb, line 152 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
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 74 def create_database(options) raise NotImplementedError, 'this should be overridden by concrete client' end
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 275 def create_document(doc) raise NotImplementedError, 'this should be overridden by concrete client' end
Creates a new property in the schema. You need to create the class before.
# File lib/orientdb4r/client.rb, line 241 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
Checks existence of a given database. Client
has not to be connected to see databases suitable to connect.
# File lib/orientdb4r/client.rb, line 90 def database_exists?(options) rslt = true begin get_database options rescue OrientdbError rslt = false end rslt end
Drops a database. Requires additional authentication to the server.
# File lib/orientdb4r/client.rb, line 104 def delete_database(options) raise NotImplementedError, 'this should be overridden by concrete client' end
Deletes an existing document.
# File lib/orientdb4r/client.rb, line 296 def delete_document(rid) raise NotImplementedError, 'this should be overridden by concrete client' end
Disconnects client from the server.
# File lib/orientdb4r/client.rb, line 44 def disconnect raise NotImplementedError, 'this should be overridden by concrete client' end
Removes a class from the schema.
# File lib/orientdb4r/client.rb, line 220 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
Exports a gzip file that contains the database JSON export. Returns name of stored file.
# File lib/orientdb4r/client.rb, line 121 def export(options) raise NotImplementedError, 'this should be overridden by concrete client' end
Gets informations about requested class.
# File lib/orientdb4r/client.rb, line 199 def get_class(name) raise NotImplementedError, 'this should be overridden by concrete client' end
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 82 def get_database(options) raise NotImplementedError, 'this should be overridden by concrete client' end
Retrieves a document by given ID.
# File lib/orientdb4r/client.rb, line 282 def get_document(rid) raise NotImplementedError, 'this should be overridden by concrete client' end
Imports a database from an uploaded JSON text file.
# File lib/orientdb4r/client.rb, line 128 def import(options) raise NotImplementedError, 'this should be overridden by concrete client' end
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 113 def list_databases(options) raise NotImplementedError, 'this should be overridden by concrete client' end
Executes a query against the database.
# File lib/orientdb4r/client.rb, line 136 def query(sql, options) raise NotImplementedError, 'this should be overridden by concrete client' end
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 60 def server(options={}) raise NotImplementedError, 'this should be overridden by concrete client' end
Updates an existing document.
# File lib/orientdb4r/client.rb, line 289 def update_document(doc) raise NotImplementedError, 'this should be overridden by concrete client' end
Protected Instance Methods
Asserts if the client is connected and raises an error if not.
# File lib/orientdb4r/client.rb, line 338 def assert_connected raise ConnectionError, 'not connected' unless @connected end
Calls the server with a specific task. Returns a response according to communication channel (e.g. HTTP response).
# File lib/orientdb4r/client.rb, line 306 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 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
Around advice to meassure and print the method time.
# File lib/orientdb4r/client.rb, line 345 def time_around(&block) start = Time.now rslt = block.call Orientdb4r::logger.debug \ "#{aop_context[:class].name}##{aop_context[:method]}: elapsed time = #{Time.now - start} [s]" rslt end