Class: Orientdb4r::Client

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/orientdb4r/client.rb

Direct Known Subclasses

BinClient, RestClient

Constant Summary collapse

SERVER_VERSION_PATTERN =

# Regexp to validate format of provided version.

/^\d+\.\d+\.\d+[-SNAPHOT]*$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#blank?, #compare_versions, #random_string, #verify_and_sanitize_options, #verify_options

Constructor Details

#initializeClient

Constructor.



27
28
29
30
# File 'lib/orientdb4r/client.rb', line 27

def initialize
  @nodes = []
  @connected = false
end

Instance Attribute Details

#connection_libraryObject (readonly)

type of connection library [:restclient, :excon]



12
13
14
# File 'lib/orientdb4r/client.rb', line 12

def connection_library
  @connection_library
end

#databaseObject (readonly)

connection parameters



10
11
12
# File 'lib/orientdb4r/client.rb', line 10

def database
  @database
end

#lb_strategyObject (readonly)

object implementing a LB strategy



23
24
25
# File 'lib/orientdb4r/client.rb', line 23

def lb_strategy
  @lb_strategy
end

#load_balancingObject (readonly)

type of load balancing [:sequence, :round_robin]



14
15
16
# File 'lib/orientdb4r/client.rb', line 14

def load_balancing
  @load_balancing
end

#nodesObject (readonly)

nodes responsible for communication with a server



21
22
23
# File 'lib/orientdb4r/client.rb', line 21

def nodes
  @nodes
end

#passwordObject (readonly)

connection parameters



10
11
12
# File 'lib/orientdb4r/client.rb', line 10

def password
  @password
end

#proxyObject (readonly)

proxy for remote communication



16
17
18
# File 'lib/orientdb4r/client.rb', line 16

def proxy
  @proxy
end

#userObject (readonly)

connection parameters



10
11
12
# File 'lib/orientdb4r/client.rb', line 10

def user
  @user
end

Instance Method Details

#class_exists?(name) ⇒ Boolean

Checks existence of a given class.

Returns:

  • (Boolean)


206
207
208
209
210
211
212
213
214
215
# 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

#command(sql) ⇒ Object

Executes a command against the database.

Raises:

  • (NotImplementedError)


143
144
145
# File 'lib/orientdb4r/client.rb', line 143

def command(sql)
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#connect(options) ⇒ Object

Connects client to the server.

Raises:

  • (NotImplementedError)


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

def connect options
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#connected?Boolean

Gets flag whenever the client is connected or not.

Returns:

  • (Boolean)


51
52
53
# File 'lib/orientdb4r/client.rb', line 51

def connected?
  @connected
end

#create_class(name, options = {}) ⇒ Object

Creates a new class in the schema.

Raises:

  • (ArgumentError)


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# 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

#create_database(options) ⇒ Object

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'

Raises:

  • (NotImplementedError)


74
75
76
# File 'lib/orientdb4r/client.rb', line 74

def create_database(options)
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#create_document(doc) ⇒ Object

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).

Raises:

  • (NotImplementedError)


275
276
277
# File 'lib/orientdb4r/client.rb', line 275

def create_document(doc)
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#create_property(clazz, property, type, options = {}) ⇒ Object

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

Raises:

  • (ArgumentError)


241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# 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

#database_exists?(options) ⇒ Boolean

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

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
98
# File 'lib/orientdb4r/client.rb', line 90

def database_exists?(options)
  rslt = true
  begin
    get_database options
  rescue OrientdbError
    rslt = false
  end
  rslt
end

#delete_database(options) ⇒ Object

Drops a database. Requires additional authentication to the server.

Raises:

  • (NotImplementedError)


104
105
106
# File 'lib/orientdb4r/client.rb', line 104

def delete_database(options)
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#delete_document(rid) ⇒ Object

Deletes an existing document.

Raises:

  • (NotImplementedError)


296
297
298
# File 'lib/orientdb4r/client.rb', line 296

def delete_document(rid)
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#disconnectObject

Disconnects client from the server.

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/orientdb4r/client.rb', line 44

def disconnect
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#drop_class(name, options = {}) ⇒ Object

Removes a class from the schema.

Raises:

  • (ArgumentError)


220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# 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

#export(options) ⇒ Object

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

Raises:

  • (NotImplementedError)


121
122
123
# File 'lib/orientdb4r/client.rb', line 121

def export(options)
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#get_class(name) ⇒ Object

Gets informations about requested class.

Raises:

  • (NotImplementedError)


199
200
201
# File 'lib/orientdb4r/client.rb', line 199

def get_class(name)
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#get_database(options) ⇒ Object

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

Raises:

  • (NotImplementedError)


82
83
84
# File 'lib/orientdb4r/client.rb', line 82

def get_database(options)
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#get_document(rid) ⇒ Object

Retrieves a document by given ID.

Raises:

  • (NotImplementedError)


282
283
284
# File 'lib/orientdb4r/client.rb', line 282

def get_document(rid)
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#import(options) ⇒ Object

Imports a database from an uploaded JSON text file.

Raises:

  • (NotImplementedError)


128
129
130
# File 'lib/orientdb4r/client.rb', line 128

def import(options)
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#list_databases(options) ⇒ Object

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.

Raises:

  • (NotImplementedError)


113
114
115
# File 'lib/orientdb4r/client.rb', line 113

def list_databases(options)
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#query(sql, options) ⇒ Object

Executes a query against the database.

Raises:

  • (NotImplementedError)


136
137
138
# File 'lib/orientdb4r/client.rb', line 136

def query(sql, options)
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#server(options = {}) ⇒ Object

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

Raises:

  • (NotImplementedError)


60
61
62
# File 'lib/orientdb4r/client.rb', line 60

def server(options={})
  raise NotImplementedError, 'this should be overridden by concrete client'
end

#update_document(doc) ⇒ Object

Updates an existing document.

Raises:

  • (NotImplementedError)


289
290
291
# File 'lib/orientdb4r/client.rb', line 289

def update_document(doc)
  raise NotImplementedError, 'this should be overridden by concrete client'
end