active-orient 0.4 → 0.80
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/.graphs.txt.swp +0 -0
- data/Gemfile +9 -5
- data/Guardfile +12 -4
- data/README.md +70 -281
- data/VERSION +1 -1
- data/active-orient.gemspec +9 -7
- data/bin/active-orient-0.6.gem +0 -0
- data/bin/active-orient-console +97 -0
- data/changelog.md +60 -0
- data/config/boot.rb +70 -17
- data/config/config.yml +10 -0
- data/config/connect.yml +11 -6
- data/examples/books.rb +154 -65
- data/examples/streets.rb +89 -85
- data/graphs.txt +70 -0
- data/lib/active-orient.rb +78 -6
- data/lib/base.rb +266 -168
- data/lib/base_properties.rb +76 -65
- data/lib/class_utils.rb +187 -0
- data/lib/database_utils.rb +99 -0
- data/lib/init.rb +80 -0
- data/lib/java-api.rb +442 -0
- data/lib/jdbc.rb +211 -0
- data/lib/model/custom.rb +29 -0
- data/lib/model/e.rb +6 -0
- data/lib/model/edge.rb +114 -0
- data/lib/model/model.rb +134 -0
- data/lib/model/the_class.rb +657 -0
- data/lib/model/the_record.rb +313 -0
- data/lib/model/vertex.rb +371 -0
- data/lib/orientdb_private.rb +48 -0
- data/lib/other.rb +423 -0
- data/lib/railtie.rb +68 -0
- data/lib/rest/change.rb +150 -0
- data/lib/rest/create.rb +287 -0
- data/lib/rest/delete.rb +150 -0
- data/lib/rest/operations.rb +222 -0
- data/lib/rest/read.rb +189 -0
- data/lib/rest/rest.rb +120 -0
- data/lib/rest_disabled.rb +24 -0
- data/lib/support/conversions.rb +42 -0
- data/lib/support/default_formatter.rb +7 -0
- data/lib/support/errors.rb +41 -0
- data/lib/support/logging.rb +38 -0
- data/lib/support/orient.rb +305 -0
- data/lib/support/orientquery.rb +647 -0
- data/lib/support/query.rb +92 -0
- data/rails.md +154 -0
- data/rails/activeorient.rb +32 -0
- data/rails/config.yml +10 -0
- data/rails/connect.yml +17 -0
- metadata +89 -30
- data/lib/model.rb +0 -461
- data/lib/orient.rb +0 -98
- data/lib/query.rb +0 -88
- data/lib/rest.rb +0 -1036
- data/lib/support.rb +0 -347
- data/test.rb +0 -4
- data/usecase.md +0 -91
data/lib/model.rb
DELETED
@@ -1,461 +0,0 @@
|
|
1
|
-
class String
|
2
|
-
def to_or
|
3
|
-
"'#{self}'"
|
4
|
-
end
|
5
|
-
end
|
6
|
-
|
7
|
-
class Numeric
|
8
|
-
def to_or
|
9
|
-
"#{self.to_s}"
|
10
|
-
end
|
11
|
-
end
|
12
|
-
module ActiveOrient
|
13
|
-
|
14
|
-
#require 'base'
|
15
|
-
#require 'base_properties'
|
16
|
-
|
17
|
-
class Model < ActiveOrient::Base
|
18
|
-
include BaseProperties
|
19
|
-
|
20
|
-
mattr_accessor :orientdb
|
21
|
-
mattr_accessor :logger
|
22
|
-
=begin
|
23
|
-
orientdb_class is used to instantiate a ActiveOrient:Model:{class} by providing its name
|
24
|
-
todo: implement object-inherence
|
25
|
-
=end
|
26
|
-
def self.orientdb_class name:
|
27
|
-
klass = Class.new( self )
|
28
|
-
name = name.to_s.camelize
|
29
|
-
if self.send :const_defined?, name
|
30
|
-
retrieved_class = self.send :const_get, name
|
31
|
-
else
|
32
|
-
new_class = self.send :const_set , name , klass
|
33
|
-
new_class.orientdb = orientdb
|
34
|
-
new_class # return_value
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
=begin
|
39
|
-
ActiveOrient::Model.autoload_object "#00:00"
|
40
|
-
either retrieves the object from the rid_store or loads it from the DB
|
41
|
-
|
42
|
-
the rid_store is updated!
|
43
|
-
|
44
|
-
to_do: fetch for version in the db and load the object if a change is detected
|
45
|
-
=end
|
46
|
-
def self.autoload_object link
|
47
|
-
# puts "autoload_object #{link}"
|
48
|
-
link_cluster_and_record = link[1,link.size].split(':').map &:to_i
|
49
|
-
@@rid_store[link_cluster_and_record].presence || orientdb.get_document( link )
|
50
|
-
end
|
51
|
-
|
52
|
-
|
53
|
-
def self.superClass
|
54
|
-
orientdb.get_classes( 'name', 'superClass').detect{|x| x["name"].downcase == new.class.to_s.downcase.split(':')[-1].to_s
|
55
|
-
}['superClass']
|
56
|
-
end
|
57
|
-
|
58
|
-
def to_orient
|
59
|
-
link
|
60
|
-
end
|
61
|
-
def from_orient
|
62
|
-
self
|
63
|
-
end
|
64
|
-
=begin
|
65
|
-
Returns just the name of the Class
|
66
|
-
=end
|
67
|
-
def classname
|
68
|
-
self.class.to_s.split(':')[-1]
|
69
|
-
end
|
70
|
-
|
71
|
-
=begin
|
72
|
-
If a Rest::Model-Object is included in a HashWidhtIndifferentAccess-Object, only the link is stored
|
73
|
-
=end
|
74
|
-
def nested_under_indifferent_access # :nodoc:
|
75
|
-
link
|
76
|
-
end
|
77
|
-
|
78
|
-
# hard-coded orientdb-columns
|
79
|
-
# prop :cluster, :version, :record, :fieldtypes
|
80
|
-
|
81
|
-
# def default_attributes
|
82
|
-
# super.merge cluster: 0
|
83
|
-
# super.merge version: 0
|
84
|
-
# super.merge record: 0
|
85
|
-
# end
|
86
|
-
def riid # :nodoc:
|
87
|
-
[ @metadata[ :cluster ] , @metadata[ :record ] ]
|
88
|
-
end
|
89
|
-
=begin
|
90
|
-
rid is used in the where-part of sql-queries
|
91
|
-
=end
|
92
|
-
def rid
|
93
|
-
if @metadata.has_key?( 'cluster')
|
94
|
-
"#{@metadata[ :cluster ]}:#{@metadata[ :record ]}"
|
95
|
-
else
|
96
|
-
"0:0"
|
97
|
-
end
|
98
|
-
end
|
99
|
-
=begin
|
100
|
-
link is used in any sql-commands
|
101
|
-
eg . update #link set ...
|
102
|
-
=end
|
103
|
-
def link
|
104
|
-
"##{rid}"
|
105
|
-
end
|
106
|
-
|
107
|
-
|
108
|
-
# def method_missing method, *args, &b
|
109
|
-
# property= orientdb.get_class_properties( classname )['properties'].detect{|x| x.has_value? method.to_s }
|
110
|
-
# puts "method_missing::property"
|
111
|
-
# puts property.inspect
|
112
|
-
# if property.present?
|
113
|
-
# if property['type'] == 'LINKSET'
|
114
|
-
# attributes[method] = OrientSupport::Array.new( self )
|
115
|
-
# else
|
116
|
-
# attributes[method] = ''
|
117
|
-
# end
|
118
|
-
# else
|
119
|
-
# raise NoMethodError
|
120
|
-
# end
|
121
|
-
# end
|
122
|
-
=begin
|
123
|
-
Queries the database and fetches the count of datasets
|
124
|
-
|
125
|
-
Any parameter that qualifies a database-query is suppoerted
|
126
|
-
(see method get_documents)
|
127
|
-
=end
|
128
|
-
def self.count **args
|
129
|
-
orientdb.count_documents from: self , **args
|
130
|
-
end
|
131
|
-
=begin
|
132
|
-
Creates a new Instance of the Class with the applied attributes
|
133
|
-
and returns the freshly instantiated Object
|
134
|
-
=end
|
135
|
-
|
136
|
-
def self.create properties = {}
|
137
|
-
orientdb.create_or_update_document self, set: properties
|
138
|
-
end
|
139
|
-
|
140
|
-
def self.update_or_create set: {}, where:{} ,**args, &b
|
141
|
-
orientdb.update_or_create_documents self , set: set, where: where , **args , &b
|
142
|
-
|
143
|
-
end
|
144
|
-
|
145
|
-
# historic method
|
146
|
-
def self.new_document attributes: {} # :nodoc:
|
147
|
-
orientdb.create_or_update_document self, set: attributes
|
148
|
-
end
|
149
|
-
=begin
|
150
|
-
Create a Property in the Schema of the Class
|
151
|
-
:call-seq:
|
152
|
-
self.create_property( field (required) , type: 'string', linked_class: nil, index: nil) do
|
153
|
-
index
|
154
|
-
end
|
155
|
-
=end
|
156
|
-
|
157
|
-
def self.get_properties
|
158
|
-
object = orientdb.get_class_properties self
|
159
|
-
{:properties => object['properties'] , :indexes => object['indexes'] }
|
160
|
-
end
|
161
|
-
|
162
|
-
def self.create_property field, **keyword_arguments, &b
|
163
|
-
orientdb.create_property self, field, **keyword_arguments, &b
|
164
|
-
end
|
165
|
-
|
166
|
-
def self.create_properties argument_hash, &b
|
167
|
-
orientdb.create_properties self, argument_hash, &b
|
168
|
-
end
|
169
|
-
|
170
|
-
def self.create_link name, class_name
|
171
|
-
orientdb.create_property self, name, type: 'link', linked_class: class_name
|
172
|
-
end
|
173
|
-
def self.create_linkset name, class_name
|
174
|
-
orientdb.create_property self, name, type: 'linkset', linked_class: class_name
|
175
|
-
end
|
176
|
-
=begin
|
177
|
-
Only if the Class inherents from »E«
|
178
|
-
Instantiate a new Edge betwen two Vertices
|
179
|
-
|
180
|
-
Parameter: unique: (true) In case of an existing Edge just update its Properties.
|
181
|
-
The parameters »from« and »to« can take a list of model-records. Then subsequent edges are created.
|
182
|
-
:call-seq:
|
183
|
-
self.create_edge from: , to: , unique: false, attributes:{}
|
184
|
-
=end
|
185
|
-
def self.create_edge **keyword_arguments
|
186
|
-
o=orientdb.nexus_edge self, **keyword_arguments
|
187
|
-
[:from,:to].each{|y| keyword_arguments[y].is_a?(Array) ? keyword_arguments[y].each( &:reload! ): keyword_arguments[y].reload! }
|
188
|
-
o # return_value
|
189
|
-
end
|
190
|
-
|
191
|
-
def self.query_database query, set_from: true
|
192
|
-
query.from self if set_from && query.is_a?( OrientSupport::OrientQuery ) && query.from.nil?
|
193
|
-
sql_cmd = -> (command) { { type: "cmd", language: "sql", command: command } }
|
194
|
-
orientdb.execute do
|
195
|
-
[ sql_cmd[ query.to_s ] ]
|
196
|
-
end
|
197
|
-
end
|
198
|
-
def query q
|
199
|
-
a= ActiveOrient::Query.new
|
200
|
-
a.queries << q
|
201
|
-
a.execute_queries
|
202
|
-
end
|
203
|
-
=begin
|
204
|
-
Parameter projection:
|
205
|
-
|
206
|
-
»select« is a method of enumeration, we use »projection:« to specify anything between »select« and »from«
|
207
|
-
in the query-string.
|
208
|
-
|
209
|
-
projection: a_string --> inserts the sting as it appears
|
210
|
-
an OrientSupport::OrientQuery-Object --> performs a sub-query and uses the result for further querying though the given parameters.
|
211
|
-
|
212
|
-
[ a, b, c ] --> "a , b , c " ( inserts a comma-separated list )
|
213
|
-
|
214
|
-
{ a: b, "sum(x) "=> f } --> "a as b, sum(x) as f" (renames properties and uses functions )
|
215
|
-
Parameter distinct:
|
216
|
-
|
217
|
-
Performs a Query like » select distinct( property ) [ as property ] from ...«
|
218
|
-
|
219
|
-
distinct: :property --> the result is mapped to the property »distinct«.
|
220
|
-
|
221
|
-
[ :property ] --> the result replaces the property
|
222
|
-
|
223
|
-
{ property: :some_name} --> the result is mapped to ModelInstance.some_name
|
224
|
-
|
225
|
-
Parameter Order
|
226
|
-
|
227
|
-
Sorts the result-set.
|
228
|
-
|
229
|
-
If new properties are introduced via select:, distinct: etc
|
230
|
-
|
231
|
-
Sorting takes place on these properties
|
232
|
-
|
233
|
-
order: :property
|
234
|
-
{ property: asc, property: desc }
|
235
|
-
[property, property, .. ] ( orderdirection is 'asc' )
|
236
|
-
|
237
|
-
|
238
|
-
Further supported Parameter:
|
239
|
-
group_by
|
240
|
-
skip
|
241
|
-
limit
|
242
|
-
unwind
|
243
|
-
|
244
|
-
see orientdb- documentation (https://orientdb.com/docs/last/SQL-Query.html)
|
245
|
-
|
246
|
-
Parameter query:
|
247
|
-
Instead of providing the parameter, the OrientSupport::OrientQuery can build and
|
248
|
-
tested before the method-call. The OrientQuery-Object can be provided with the query-parameter
|
249
|
-
i.e.
|
250
|
-
q= OrientSupport::OrientQuery.new
|
251
|
-
TestModel = r.open_class 'test_model'
|
252
|
-
q.from TestModel
|
253
|
-
q.where { name: 'Thomas' }
|
254
|
-
|
255
|
-
count= TestModel.count query:q
|
256
|
-
q.limit 10
|
257
|
-
0.step(count,10) do |x|
|
258
|
-
q.skip = x
|
259
|
-
puts TestModel.get_documents( query: q ).map{|x| x.adress }.join('\t')
|
260
|
-
end
|
261
|
-
prints a Table with 10 columns.
|
262
|
-
=end
|
263
|
-
|
264
|
-
def self.get_documents **args , &b
|
265
|
-
orientdb.get_documents from: self, **args, &b
|
266
|
-
|
267
|
-
end
|
268
|
-
=begin
|
269
|
-
Performs a query on the Class and returns an Array of ActiveOrient:Model-Records.
|
270
|
-
|
271
|
-
Example:
|
272
|
-
Log = r.open_class 'Log'
|
273
|
-
Log.where priority: 'high'
|
274
|
-
--> submited database-request: query/hc_database/sql/select from Log where priority = 'high'/-1
|
275
|
-
=> [ #<ActiveOrient::Model::Log:0x0000000480f7d8 @metadata={ ... }, ... ]
|
276
|
-
|
277
|
-
|
278
|
-
=end
|
279
|
-
def self.where attributes = {}
|
280
|
-
q = OrientSupport::OrientQuery.new where: attributes
|
281
|
-
query_database q
|
282
|
-
end
|
283
|
-
=begin
|
284
|
-
|
285
|
-
removes the Model-Instance from the database
|
286
|
-
|
287
|
-
returns true (successfully deleted) or false ( obj not deleted)
|
288
|
-
=end
|
289
|
-
def delete
|
290
|
-
|
291
|
-
r= if is_edge?
|
292
|
-
# returns the count of deleted edges
|
293
|
-
orientdb.delete_edge rid
|
294
|
-
else
|
295
|
-
orientdb.delete_document rid
|
296
|
-
end
|
297
|
-
ActiveOrient::Base.remove_riid self if r # removes the obj from the rid_store
|
298
|
-
r # true or false
|
299
|
-
end
|
300
|
-
=begin
|
301
|
-
An Edge is defined
|
302
|
-
* when inherented from the superclass »E» (formal definition)
|
303
|
-
* if it has an in- and an out property
|
304
|
-
|
305
|
-
Actually we just check the second term as we trust the constuctor to work properly
|
306
|
-
=end
|
307
|
-
def is_edge?
|
308
|
-
attributes.keys.include?( 'in') && attributes.keys.include?('out')
|
309
|
-
end
|
310
|
-
# get enables loading of datasets if a link is followed
|
311
|
-
# model_class.all.first.link.get
|
312
|
-
def self.get rid
|
313
|
-
orientdb.get_document rid
|
314
|
-
end
|
315
|
-
def self.all
|
316
|
-
orientdb.get_documents from: self
|
317
|
-
end
|
318
|
-
def self.first where: {}
|
319
|
-
orientdb.get_documents( from: self, where: where, limit: 1).pop
|
320
|
-
end
|
321
|
-
|
322
|
-
def self.last where: {}
|
323
|
-
# debug:: orientdb.get_documents( self, order: { "@rid" => 'desc' }, limit: 1 ){ |x| puts x }.pop
|
324
|
-
orientdb.get_documents( from: self, where: where, order: { "@rid" => 'desc' }, limit: 1 ).pop
|
325
|
-
end
|
326
|
-
=begin
|
327
|
-
Convient update of the dataset by calling sql-patch
|
328
|
-
The attributes are saved to the database.
|
329
|
-
With the optional :set argument ad-hoc attributes can be defined
|
330
|
-
obj = ActiveOrient::Model::Contracts.first
|
331
|
-
obj.name = 'new_name'
|
332
|
-
obj.update set: { yesterdays_event: 35 }
|
333
|
-
=end
|
334
|
-
def update set: {}
|
335
|
-
attributes.merge!( set ) if set.present?
|
336
|
-
result= orientdb.patch_document(rid) do
|
337
|
-
attributes.merge( { '@version' => @metadata[ :version ], '@class' => @metadata[ :class ] } )
|
338
|
-
end
|
339
|
-
# returns a new instance of ActiveOrient::Model
|
340
|
-
reload! ActiveOrient::Model.orientdb_class(name: classname).new( JSON.parse( result ))
|
341
|
-
# instantiate object, update rid_store and reassign to self
|
342
|
-
|
343
|
-
end
|
344
|
-
=begin
|
345
|
-
Overwrite the attributes with Database-Contents (or attributes provided by the updated_dataset.model-instance)
|
346
|
-
=end
|
347
|
-
def reload! updated_dataset=nil
|
348
|
-
updated_dataset = orientdb.get_document( link) if updated_dataset.nil?
|
349
|
-
@metadata[:version]= updated_dataset.version
|
350
|
-
attributes = updated_dataset.attributes
|
351
|
-
self # return_value (otherwise only the attributes would be returned)
|
352
|
-
end
|
353
|
-
|
354
|
-
def remove_item_from_property array, item=nil
|
355
|
-
logger.progname = 'ActiveOrient::Model#RemoveItemFromProperty'
|
356
|
-
execute_array = Array.new
|
357
|
-
return unless attributes.has_key? array
|
358
|
-
remove_execute_array = -> (it) do
|
359
|
-
case it
|
360
|
-
when ActiveOrient::Model
|
361
|
-
execute_array << {type: "cmd", language: "sql", command: "update #{link} remove #{array} = #{it.link}"}
|
362
|
-
when String
|
363
|
-
execute_array << {type: "cmd", language: "sql", command: "update #{link} remove #{array} = '#{it}'"}
|
364
|
-
when Numeric
|
365
|
-
execute_array << {type: "cmd", language: "sql", command: "update #{link} remove #{array} = #{it}"}
|
366
|
-
else
|
367
|
-
logger.error { "Only Basic Formats supported . Cannot Serialize #{it.class} this way" }
|
368
|
-
logger.error { "Try to load the array from the DB, modify it and update the hole record" }
|
369
|
-
end
|
370
|
-
end
|
371
|
-
|
372
|
-
if block_given?
|
373
|
-
items = yield
|
374
|
-
items.each{|x| remove_execute_array[x]; self.attributes[array].delete( x ) }
|
375
|
-
elsif item.present?
|
376
|
-
remove_execute_array[item]
|
377
|
-
a= attributes; a.delete item
|
378
|
-
self.attributes[array].delete( item )
|
379
|
-
end
|
380
|
-
orientdb.execute do
|
381
|
-
execute_array
|
382
|
-
end
|
383
|
-
reload!
|
384
|
-
|
385
|
-
rescue RestClient::InternalServerError => e
|
386
|
-
logger.error " Could not remove item in #{array} "
|
387
|
-
logger.error e.inspect
|
388
|
-
end
|
389
|
-
|
390
|
-
=begin
|
391
|
-
Convient method for populating embedded- or linkset-properties
|
392
|
-
|
393
|
-
In both cases an array/ a collection is stored in the database.
|
394
|
-
|
395
|
-
its called via
|
396
|
-
model.add_item_to_property( linkset- or embedded property, Object_to_be_linked_to )
|
397
|
-
or
|
398
|
-
mode.add_items_to_property( linkset- or embedded property ) do
|
399
|
-
Array_of_Objects_to_be_linked_to
|
400
|
-
(actually, the objects must inherent from ActiveOrient::Model, Numeric, String)
|
401
|
-
end
|
402
|
-
|
403
|
-
to_do: use "<<" to add the item to the property
|
404
|
-
=end
|
405
|
-
def add_item_to_property array, item=nil
|
406
|
-
logger.progname = 'ActiveOrient::Model#AddItem2Property'
|
407
|
-
execute_array = Array.new
|
408
|
-
self.attributes[array] = Array.new unless attributes[array].present?
|
409
|
-
add_2_execute_array = -> (it) do
|
410
|
-
case it
|
411
|
-
when ActiveOrient::Model
|
412
|
-
execute_array << {type: "cmd", language: "sql", command: "update #{link} add #{array} = #{it.link}"}
|
413
|
-
when String
|
414
|
-
execute_array << {type: "cmd", language: "sql", command: "update #{link} add #{array} = '#{it}'"}
|
415
|
-
when Numeric
|
416
|
-
execute_array << {type: "cmd", language: "sql", command: "update #{link} add #{array} = #{it}"}
|
417
|
-
else
|
418
|
-
logger.error { "Only Basic Formats supported . Cannot Serialize #{it.class} this way" }
|
419
|
-
logger.error { "Try to load the array from the DB, modify it and update the hole record" }
|
420
|
-
end
|
421
|
-
end
|
422
|
-
|
423
|
-
if block_given?
|
424
|
-
items = yield
|
425
|
-
items.each{|x| add_2_execute_array[x]; self.attributes[array] << x }
|
426
|
-
elsif item.present?
|
427
|
-
add_2_execute_array[item]
|
428
|
-
self.attributes[array] << item
|
429
|
-
end
|
430
|
-
orientdb.execute do
|
431
|
-
execute_array
|
432
|
-
end
|
433
|
-
reload!
|
434
|
-
|
435
|
-
rescue RestClient::InternalServerError => e
|
436
|
-
logger.error " Duplicate found in #{array} "
|
437
|
-
logger.error e.inspect
|
438
|
-
end
|
439
|
-
|
440
|
-
alias add_items_to_property add_item_to_property
|
441
|
-
## historical aliases
|
442
|
-
alias update_linkset add_item_to_property
|
443
|
-
alias update_embedded add_item_to_property
|
444
|
-
=begin
|
445
|
-
Convient method for updating a linkset-property
|
446
|
-
its called via
|
447
|
-
model.update_linkset( linkset-property, Object_to_be_linked_to )
|
448
|
-
or
|
449
|
-
mode.update_linkset( linkset-property ) do
|
450
|
-
Array_of_Objects_to_be_linked_to
|
451
|
-
end
|
452
|
-
=end
|
453
|
-
|
454
|
-
#private
|
455
|
-
def version
|
456
|
-
@metadata[ :version ]
|
457
|
-
end
|
458
|
-
|
459
|
-
end # class
|
460
|
-
|
461
|
-
end # module
|