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 29 def initialize @nodes = [] @connected = false end
Public Instance Methods
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
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
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
Gets flag whenever the client is connected or not.
# File lib/orientdb4r/client.rb, line 53 def connected? @connected end
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
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 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
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
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
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
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
Disconnects client from the server.
# File lib/orientdb4r/client.rb, line 46 def disconnect raise NotImplementedError, 'this should be overridden by concrete client' end
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
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
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
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
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
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
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
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
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
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
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
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
# File lib/orientdb4r/client.rb, line 357 def query_log(context, message) Orientdb4r::logger.debug \ " \033[01;33m#{context}:\033[0m #{message}" end
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