Module: RestRead
- Included in:
- ActiveOrient::OrientDB
- Defined in:
- lib/rest/read.rb
Instance Method Summary collapse
-
#get_class_properties(o_class) ⇒ Object
Returns a JSON of the property of a class.
-
#get_classes(*attributes) ⇒ Object
Returns an Array with (unmodified) Class-attribute-hash-Elements.
-
#get_databases ⇒ Object
Returns an Array with available Database-Names as Elements.
-
#get_record(rid) ⇒ Object
(also: #get_document)
Retrieves a Record from the Database .
-
#get_records(raw: false, query: nil, **args) ⇒ Object
(also: #get_documents)
Retrieves Records from a query.
- #print_class_properties(o_class) ⇒ Object
Instance Method Details
#get_class_properties(o_class) ⇒ Object
Returns a JSON of the property of a class
ORD.create_vertex_class a:
ORD.get_class_properties A
=> {"name"=>"a", "superClass"=>"V", "superClasses"=>["V"], "alias"=>nil, "abstract"=>false, "strictmode"=>false, "clusters"=>[65, 66, 67, 68], "defaultCluster"=>65, "clusterSelection"=>"round-robin", "records"=>3}
55 56 57 58 59 60 61 62 |
# File 'lib/rest/read.rb', line 55 def get_class_properties o_class ActiveOrient.db_pool.checkout do | conn | JSON.parse(conn["/class/#{ActiveOrient.database}/#{classname(o_class)}"].get) end rescue => e logger.error e. nil end |
#get_classes(*attributes) ⇒ Object
Returns an Array with (unmodified) Class-attribute-hash-Elements
»get_classes ‘name’, ‘superClass’« returns
[ {"name"=>"E", "superClass"=>""},
{"name"=>"OFunction", "superClass"=>""},
{"name"=>"ORole", "superClass"=>"OIdentity"}
(...) ]
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rest/read.rb', line 25 def get_classes *attributes begin response = ActiveOrient.db_pool.checkout do | conn | conn["/database/#{ActiveOrient.database}"].get end if response.code == 200 classes = JSON.parse(response.body)['classes'] unless attributes.empty? classes.map{|y| y.select{|v,_| attributes.include?(v)}} else classes end else [] end rescue Exception => e logger.progname = 'RestRead#GetClasses' logger.error{e.} end end |
#get_databases ⇒ Object
Returns an Array with available Database-Names as Elements
ORD.get_databases
=> ["temp", "GratefulDeadConcerts", (...)]
9 10 11 12 13 14 |
# File 'lib/rest/read.rb', line 9 def get_databases ActiveOrient.db_pool.checkout do | conn | JSON.parse(conn["/listDatabases"].get.body)['databases'] end end |
#get_record(rid) ⇒ Object Also known as: get_document
Retrieves a Record from the Database
The argument can either be a rid “#x:y” or a link “x:y”.
(to be specific: it must provide the methods rid? and to_orient, the latter must return the rid: “#[a}:b”.)
If no Record is found, nil is returned
The rid-cache is not used or updated
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/rest/read.rb', line 92 def get_record rid begin logger.progname = 'RestRead#GetRecord' if rid.rid? response = ActiveOrient.db_pool.checkout do | conn | conn["/document/#{ActiveOrient.database}/#{rid.to_orient[1..-1]}"].get end raw_data = JSON.parse(response.body) # ActiveOrient::Model.use_or_allocate( raw_data['@rid'] ) do the_object= ActiveOrient::Model.orientdb_class(name: raw_data['@class']).new raw_data ActiveOrient::Base.store_rid( the_object ) # update cache else logger.error { "Wrong parameter #{rid.inspect}. " } nil end rescue RestClient::InternalServerError => e if e.http_body.split(':').last =~ /was not found|does not exist in database/ nil else logger.error { "Something went wrong" } logger.error { e.http_body.inspect } raise end rescue RestClient::ResourceNotFound => e logger.error { "RID: #{rid} ---> No Record present " } ActiveOrient::Model.remove_rid rid # remove rid from cache nil rescue NoMethodError => e logger.fatal { "---------------- Serious Trouble ----------------" } logger.fatal { "GetRecord raw-data: #{raw_data}" } logger.error { "GetRecord could not allocate Model-Instance" } logger.error { "is a model file required but missing?" } raise rescue Exception => e logger.error { "Something went wrong" } logger.error { "RID: #{rid} - #{e.}" } raise end end |
#get_records(raw: false, query: nil, **args) ⇒ Object Also known as: get_documents
Retrieves Records from a query
If raw is specified, the JSON-Array is returned, e.g.
{"@type"=>"d", "@rid"=>"#15:1", "@version"=>1, "@class"=>"DocumebntKlasse10", "con_id"=>343, "symbol"=>"EWTZ"}
Otherwise ActiveModel-Instances are created and returned. In this case cached data are used in favour and its not checked if the database contents have changed.
145 146 147 148 149 150 151 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 |
# File 'lib/rest/read.rb', line 145 def get_records raw: false, query: nil, **args query = OrientSupport::OrientQuery.new(args) if query.nil? begin logger.progname = 'RestRead#GetRecords' response = ActiveOrient.db_pool.checkout do | conn | url = "/query/#{ActiveOrient.database}/sql/" + query.compose(destination: :rest) + "/#{query.get_limit}" conn[URI.encode(url)].get end JSON.parse(response.body)['result'].map do |record| if raw record # query returns an anonymus class: Use the provided Block or the Dummy-Model MyQuery elsif record['@class'].blank? block_given? ? yield.new(record) : ActiveOrient::Model.orientdb_class(name: 'query' ).new( record ) else the_object = ActiveOrient::Model.orientdb_class(name: record['@class']).new record ActiveOrient::Base.store_rid( the_object ) # update cache # end end end # returns an array of updated objects rescue RestClient::BadRequest => e #puts e.inspect logger.error { "-"*30 } logger.error { "REST_READ#GET_RECORDS.URL ---> Wrong Query" } logger.error { query.compose( destination: :rest).to_s } logger.error { "Fired Statement: #{url.to_s} " } response="" rescue RestClient::InternalServerError => e response = JSON.parse(e.response)['errors'].pop logger.error{ "Interbak Server ERROR" } logger.error{response['content'].split(':').last} rescue URI::InvalidURIError => e logger.error{"Invalid URI detected"} logger.error query.to_s logger.info{"Trying batch processing"} response = execute{ query.to_s} logger.info{"Success: to avoid this delay use ActiveOrient::Model#query_database instead"} response end end |
#print_class_properties(o_class) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rest/read.rb', line 65 def print_class_properties o_class puts "Detected Properties for class #{classname(o_class)}" rp = get_class_properties o_class n = rp['name'] if rp['properties'].nil? puts "No property available" else puts rp['properties'].map{|x| "\t"+[n+'.'+x['name'], x['type'],x['linkedClass']].compact.join("\t-> ")}.join("\n") end rescue NoMethodError puts "Class #{o_class} not present in database" end |