orientdb4r 0.2.4 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -72,12 +72,15 @@ see Wiki page for more sample at https://github.com/veny/orientdb4r/wiki
72
72
  Tested on
73
73
  * Ruby 1.9.3
74
74
  * OrientDB 1.0.0
75
+ * OrientDB 1.1.0-SNAPSHOT r5913+
75
76
 
76
77
  == TESTS
78
+ In order to run the tests you only need to:
77
79
 
78
- rake test
80
+ cd /path/to/repository
81
+ rake test
79
82
 
80
- Start an instance of OrientDB (on localhost with default port and 'admin/admin' account) before running.
83
+ Start an instance of OrientDB (on localhost with default web port 2480 and 'admin/admin' account) before running.
81
84
 
82
85
  == AUTHOR
83
86
 
@@ -16,6 +16,7 @@ module Orientdb4r
16
16
  @connected = false
17
17
  end
18
18
 
19
+
19
20
  # --------------------------------------------------------------- CONNECTION
20
21
 
21
22
  ###
@@ -47,6 +48,7 @@ module Orientdb4r
47
48
  raise NotImplementedError, 'this should be overridden by concrete client'
48
49
  end
49
50
 
51
+
50
52
  # ----------------------------------------------------------------- DATABASE
51
53
 
52
54
  ###
@@ -59,11 +61,25 @@ module Orientdb4r
59
61
 
60
62
 
61
63
  ###
62
- # Gets informations about requested class.
63
- def get_class(name)
64
+ # Retrieves all the information about a database.
65
+ def get_database(name)
64
66
  raise NotImplementedError, 'this should be overridden by concrete client'
65
67
  end
66
68
 
69
+
70
+ ###
71
+ # Checks existence of a given database.
72
+ def database_exists?(name)
73
+ rslt = true
74
+ begin
75
+ get_database name
76
+ rescue NotFoundError
77
+ rslt = false
78
+ end
79
+ rslt
80
+ end
81
+
82
+
67
83
  # ---------------------------------------------------------------------- SQL
68
84
 
69
85
  ###
@@ -79,6 +95,7 @@ module Orientdb4r
79
95
  raise NotImplementedError, 'this should be overridden by concrete client'
80
96
  end
81
97
 
98
+
82
99
  # -------------------------------------------------------------------- CLASS
83
100
 
84
101
  ###
@@ -111,6 +128,13 @@ module Orientdb4r
111
128
  end
112
129
 
113
130
 
131
+ ###
132
+ # Gets informations about requested class.
133
+ def get_class(name)
134
+ raise NotImplementedError, 'this should be overridden by concrete client'
135
+ end
136
+
137
+
114
138
  ###
115
139
  # Removes a class from the schema.
116
140
  def drop_class(name, options={})
@@ -6,7 +6,7 @@ module Orientdb4r
6
6
  # Name of cookie that represents a session.
7
7
  SESSION_COOKIE_NAME = 'OSESSIONID'
8
8
 
9
- before [:create_database, :get_class, :query, :command], :assert_connected
9
+ before [:create_database, :get_database, :get_class, :query, :command], :assert_connected
10
10
  around [:query, :command], :time_around
11
11
 
12
12
  attr_reader :host, :port, :ssl, :user, :password, :database, :session_id
@@ -22,6 +22,8 @@ module Orientdb4r
22
22
  end
23
23
 
24
24
 
25
+ # --------------------------------------------------------------- CONNECTION
26
+
25
27
  def connect(options) #:nodoc:
26
28
  options_pattern = { :database => :mandatory, :user => :mandatory, :password => :mandatory }
27
29
  verify_and_sanitize_options(options, options_pattern)
@@ -89,6 +91,26 @@ module Orientdb4r
89
91
  end
90
92
 
91
93
 
94
+ def server(options={}) #:nodoc:
95
+ # 'server' does NOT use the RestClient Resource to construct the HTTP request
96
+
97
+ options_pattern = { :user => :optional, :password => :optional }
98
+ verify_options(options, options_pattern)
99
+
100
+ u = options.include?(:user) ? options[:user] : user
101
+ p = options.include?(:password) ? options[:password] : password
102
+ resource = ::RestClient::Resource.new(url, :user => u, :password => p)
103
+ begin
104
+ response = resource['server'].get
105
+ rescue
106
+ raise OrientdbError
107
+ end
108
+ process_response(response)
109
+ end
110
+
111
+
112
+ # ----------------------------------------------------------------- DATABASE
113
+
92
114
  def create_database(options) #:nodoc:
93
115
  options_pattern = {
94
116
  :database => :mandatory, :type => 'memory',
@@ -108,6 +130,46 @@ module Orientdb4r
108
130
  end
109
131
 
110
132
 
133
+ def get_database(name) #:nodoc:
134
+ begin
135
+ response = @resource["database/#{name}"].get
136
+ rescue
137
+ raise NotFoundError
138
+ end
139
+ process_response(response)
140
+ end
141
+
142
+
143
+ # ---------------------------------------------------------------------- SQL
144
+
145
+ def query(sql) #:nodoc:
146
+ raise ArgumentError, 'query is blank' if blank? sql
147
+
148
+ response = @resource["query/#{@database}/sql/#{CGI::escape(sql)}"].get
149
+ entries = process_response(response)
150
+ rslt = entries['result']
151
+ # mixin all document entries (they have '@class' attribute)
152
+ rslt.each { |doc| doc.extend Orientdb4r::DocumentMetadata unless doc['@class'].nil? }
153
+ rslt
154
+ end
155
+
156
+
157
+ def command(sql) #:nodoc:
158
+ raise ArgumentError, 'command is blank' if blank? sql
159
+ begin
160
+ #puts "REQ command/#{@database}/sql/#{CGI::escape(sql)}"
161
+ response = @resource["command/#{@database}/sql/#{CGI::escape(sql)}"].post ''
162
+ rslt = process_response(response)
163
+ rslt
164
+ #puts "RESP #{response.body}"
165
+ rescue
166
+ raise OrientdbError
167
+ end
168
+ end
169
+
170
+
171
+ # -------------------------------------------------------------------- CLASS
172
+
111
173
  def get_class(name) #:nodoc:
112
174
  raise ArgumentError, "class name is blank" if blank?(name)
113
175
 
@@ -144,50 +206,6 @@ module Orientdb4r
144
206
  end
145
207
 
146
208
 
147
- def query(sql) #:nodoc:
148
- raise ArgumentError, 'query is blank' if blank? sql
149
-
150
- response = @resource["query/#{@database}/sql/#{CGI::escape(sql)}"].get
151
- entries = process_response(response)
152
- rslt = entries['result']
153
- # mixin all document entries (they have '@class' attribute)
154
- rslt.each { |doc| doc.extend Orientdb4r::DocumentMetadata unless doc['@class'].nil? }
155
- rslt
156
- end
157
-
158
-
159
- def command(sql) #:nodoc:
160
- raise ArgumentError, 'command is blank' if blank? sql
161
- begin
162
- #puts "REQ #{sql}"
163
- response = @resource["command/#{@database}/sql/#{CGI::escape(sql)}"].post ''
164
- rslt = process_response(response)
165
- rslt
166
- puts "RESP #{response.body}"
167
- rescue
168
- raise OrientdbError
169
- end
170
- end
171
-
172
-
173
- def server(options={}) #:nodoc:
174
- # 'server' does NOT use the RestClient Resource to construct the HTTP request
175
-
176
- options_pattern = { :user => :optional, :password => :optional }
177
- verify_options(options, options_pattern)
178
-
179
- u = options.include?(:user) ? options[:user] : user
180
- p = options.include?(:password) ? options[:password] : password
181
- resource = ::RestClient::Resource.new(url, :user => u, :password => p)
182
- begin
183
- response = resource['server'].get
184
- rescue
185
- raise OrientdbError
186
- end
187
- process_response(response)
188
- end
189
-
190
-
191
209
  # ----------------------------------------------------------------- DOCUMENT
192
210
 
193
211
  def create_document(doc)
@@ -82,16 +82,22 @@ module Orientdb4r
82
82
  class DataGenerator
83
83
 
84
84
  def initialize
85
- @worlds = IO.readlines('/usr/share/dict/words')
86
- 0.upto(@worlds.size - 1) do |i|
87
- word = @worlds[i]
85
+ @words = IO.readlines('/usr/share/dict/words')
86
+ 0.upto(@words.size - 1) do |i|
87
+ word = @words[i]
88
88
  word.strip!
89
89
  idx = word.index("'")
90
90
  word = word[0..(idx - 1)] unless idx.nil?
91
- @worlds[i] = word
91
+ @words[i] = word
92
92
  end
93
- @worlds.uniq
94
- Orientdb4r::logger.info "DataGenerator: #{@worlds.size} words"
93
+ @words.uniq
94
+ Orientdb4r::logger.info "DataGenerator: #{@words.size} words"
95
+ end
96
+
97
+ ###
98
+ # Gets a random word.
99
+ def word
100
+ @words[rand(@words.size)]
95
101
  end
96
102
 
97
103
  end # DataGenerator
@@ -2,6 +2,7 @@ module Orientdb4r
2
2
 
3
3
  # Version history.
4
4
  VERSION_HISTORY = [
5
+ ['0.2.5', '2012-07-01', "Added 'get_database' into database CRUD"],
5
6
  # v-- https://groups.google.com/forum/?fromgroups#!topic/orient-database/5MAMCvFavTc
6
7
  ['0.2.4', '2012-06-26', "Added session management"],
7
8
  ['0.2.3', '2012-06-24', "Documents received by a query are kind of Orientdb4r::DocumentMetadata"],
data/test/graphs.rb ADDED
@@ -0,0 +1,114 @@
1
+ require 'orientdb4r'
2
+
3
+ DB = 'temp'
4
+
5
+ c = Orientdb4r.client
6
+ c.connect :database => DB, :user => 'admin', :password => 'admin'
7
+ dg = Orientdb4r::Utils::DataGenerator.new
8
+
9
+
10
+ def clear(conn)
11
+ conn.drop_class 'Community'
12
+ puts "droped class: Community"
13
+ conn.drop_class 'OrgUnit'
14
+ puts "droped class: OrgUnit"
15
+ conn.drop_class 'User'
16
+ puts "droped class: User"
17
+ end
18
+
19
+ if ARGV.include?('--clear')
20
+ clear(c)
21
+ end
22
+
23
+ if ARGV.include?('--schema')
24
+ clear(c)
25
+ # OrgUnit
26
+ c.create_class 'OrgUnit' do |c|
27
+ c.property 'name', :string, :mandatory => true
28
+ c.property 'network', :boolean
29
+ c.link 'descendants', :linkset, 'OrgUnit'
30
+ end
31
+ puts "created class: OrgUnit"
32
+ # Community
33
+ c.create_class 'Community' do |c|
34
+ c.property 'name', :string, :mandatory => true
35
+ end
36
+ puts "created class: Community"
37
+ # User
38
+ c.create_class 'User' do |c|
39
+ c.property 'username', :string, :mandatory => true
40
+ c.property 'name', :string, :mandatory => true
41
+ c.link 'unit', :link, 'OrgUnit', :mandatory => true
42
+ c.link 'communities', :linkset, 'Community'
43
+ end
44
+ puts "created class: User"
45
+ end
46
+
47
+ if ARGV.include?('--data')
48
+
49
+ # Organisational Units
50
+ c. command 'DELETE FROM OrgUnit'
51
+ units = []
52
+
53
+ comp_rids = []
54
+ auto_rids = []
55
+ auto_rids << c.create_document({ '@class' => 'OrgUnit', :name => 'Sales', :network => true })
56
+ auto_rids << c.create_document({ '@class' => 'OrgUnit', :name => 'Marketing', :network => true })
57
+ auto_rids << c.create_document({ '@class' => 'OrgUnit', :name => 'Design', :network => true })
58
+ comp_rids << c.create_document({ '@class' => 'OrgUnit', :name => 'Automotive', :network => true, :descendants => auto_rids })
59
+ #
60
+ research_rids = []
61
+ research_rids << c.create_document({ '@class' => 'OrgUnit', :name => 'Scientist', :network => false })
62
+ research_rids << c.create_document({ '@class' => 'OrgUnit', :name => 'Spies', :network => false })
63
+ comp_rids << c.create_document({ '@class' => 'OrgUnit', :name => 'Research', :network => false, :descendants => research_rids })
64
+ #
65
+ infra_rids = []
66
+ recruit_rid = c.create_document({ '@class' => 'OrgUnit', :name => 'Recruitment', :network => false })
67
+ infra_rids << c.create_document({ '@class' => 'OrgUnit', :name => 'Human Resources', :network => false, :descendants => [recruit_rid] })
68
+ infra_rids << c.create_document({ '@class' => 'OrgUnit', :name => 'Accounting', :network => false })
69
+ comp_rids << c.create_document({ '@class' => 'OrgUnit', :name => 'Company Infrastructure', :network => false, :descendants => infra_rids })
70
+ #
71
+ units << c.create_document({ '@class' => 'OrgUnit', :name => 'Big Company', :network => false, :descendants => comp_rids })
72
+ units << auto_rids << research_rids << infra_rids << comp_rids
73
+ units.flatten!.uniq!
74
+ puts "created units: #{units}"
75
+
76
+ # Communities
77
+ c. command 'DELETE FROM Community'
78
+ communities = []
79
+ communities << c.create_document({ '@class' => 'Community', :name => 'Pianists' })
80
+ communities << c.create_document({ '@class' => 'Community', :name => 'Violinists' })
81
+ communities << c.create_document({ '@class' => 'Community', :name => 'Dog fanciers' })
82
+ communities << c.create_document({ '@class' => 'Community', :name => 'Cat fanciers' })
83
+ communities << c.create_document({ '@class' => 'Community', :name => 'Soccer fans' })
84
+ communities << c.create_document({ '@class' => 'Community', :name => 'Ski fans' })
85
+ communities << c.create_document({ '@class' => 'Community', :name => 'Basket fans' })
86
+ communities << c.create_document({ '@class' => 'Community', :name => 'Gourmets' })
87
+ communities << c.create_document({ '@class' => 'Community', :name => 'Scifi reader' })
88
+ communities << c.create_document({ '@class' => 'Community', :name => 'Comic reader' })
89
+ puts "created communities: #{communities}"
90
+
91
+ # Users
92
+ c. command 'DELETE FROM User'
93
+ 1.upto(1000) do
94
+ first_name = dg.word.capitalize
95
+ surname = dg.word.capitalize
96
+ # random distribution of Units (1) & Communities (0..3)
97
+ unit = units[rand(units.size)]
98
+ comms = []
99
+ 0.upto(rand(4)) { |i| comms << communities[rand(communities.size)] if i > 0 }
100
+
101
+ c.create_document({ '@class' => 'User', \
102
+ :username => "#{first_name.downcase}.#{surname.downcase}", \
103
+ :name => "#{first_name.capitalize} #{surname.capitalize}", \
104
+ :unit => unit, \
105
+ :communities => comms })
106
+ end
107
+
108
+ end
109
+
110
+ # FIND REFERENCES #6:5 [OrgUnit] # get parent
111
+
112
+ # TRAVERSE descendants FROM #6:10 WHERE $depth < 2 # get first level of descendants
113
+ # SELECT FROM (TRAVERSE descendants FROM #6:10 WHERE $depth < 2) WHERE $depth > 0 # eliminate root of query
114
+ # SELECT FROM orgunit WHERE any() traverse(0,10) (name = 'Recruitment') # get ancestors
@@ -75,6 +75,7 @@ class TestDatabase < Test::Unit::TestCase
75
75
  # Temporary disabled because of unknown way how to drop a new created datatabse.
76
76
  def xtest_create_database
77
77
  @client.create_database :database => 'UniT', :user => 'root', :password => 'root'
78
+ assert_nothing_thrown do @client.get_database 'UniT'; end
78
79
  # creating an existing DB
79
80
  assert_raise Orientdb4r::OrientdbError do
80
81
  @client.create_database :database => 'UniT', :user => 'root', :password => 'root'
@@ -91,6 +92,26 @@ class TestDatabase < Test::Unit::TestCase
91
92
  end
92
93
 
93
94
 
95
+ ###
96
+ # GET DATABASE
97
+ def test_get_database
98
+ @client.connect :database => 'temp', :user => 'admin', :password => 'admin'
99
+ assert_nothing_thrown do @client.get_database 'temp'; end
100
+
101
+ rslt = @client.get_database 'temp'
102
+ assert_not_nil rslt
103
+ assert_instance_of Hash, rslt
104
+ assert rslt.include? 'classes'
105
+ assert @client.database_exists?('temp')
106
+
107
+ # bad databases
108
+ assert_raise Orientdb4r::NotFoundError do @client.get_database 'UnknownDB'; end
109
+ assert_raise Orientdb4r::NotFoundError do @client.get_database 'temp/admin'; end
110
+ assert !@client.database_exists?('UnknownDB')
111
+ assert !@client.database_exists?('temp/admin')
112
+ end
113
+
114
+
94
115
  ###
95
116
  # SERVER info
96
117
  # Temporary disabled because of dependency to password of 'root' account
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: orientdb4r
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-26 00:00:00.000000000 Z
12
+ date: 2012-07-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
@@ -48,6 +48,7 @@ files:
48
48
  - lib/orientdb4r/utils.rb
49
49
  - lib/orientdb4r/version.rb
50
50
  - orientdb4r.gemspec
51
+ - test/graphs.rb
51
52
  - test/readme_sample.rb
52
53
  - test/test_database.rb
53
54
  - test/test_ddo.rb
@@ -80,6 +81,7 @@ signing_key:
80
81
  specification_version: 3
81
82
  summary: Ruby binding for Orient DB.
82
83
  test_files:
84
+ - test/graphs.rb
83
85
  - test/readme_sample.rb
84
86
  - test/test_database.rb
85
87
  - test/test_ddo.rb