class Orchestrate::Application::Record
Class for accessing collections belonging to an Orchestrate.io application.
-
public class methods for collection requests
-
public instance methods for key/value requests
PUT and DELETE requests return Orchestrate::API::Response.
GET requests return Result or SearchResult. The result data is returned as Document objects.
Attributes
The unique primary_key that identifies a set of key/value data stored in an orchestrate.io collection.
Public Class Methods
Performs application-level initialization functions.
# File rails/lib/orchestrate_application/record.rb, line 35 def initialize(params={}) name = init_collection_name params[:define_collection_name] @@_collection[self.class.name] ||= Schema.instance.get_collection(name) @id = params[:id] if params[:id] @__ref_value__ = '"*"' # @event_types = params[:event_types] ? params[:event_types] : [] # @relation_kinds = params[:graphs] ? params[:graphs] : [] @@_cache[self.class.name] ||= SimpleCacheRequest.new(ocollection) @@cache_store ||= SimpleCacheStore.instance end
Delete the specified collection.
# File rails/lib/orchestrate_application/record.rb, line 97 def self.orchio_delete(collection) response = client.send_request( :delete, { collection: collection, path: "?force=true" } ) # SchemaCollection.delete(collection) if response.header.code == 204 orchio_status response, 204 end
Delete key from collection.
# File rails/lib/orchestrate_application/record.rb, line 106 def self.orchio_delete_key(collection, key) new(collection: collection, id: key).orchio_delete end
Lists the collection contents. The results are based the options. Returns Result object.
# File rails/lib/orchestrate_application/record.rb, line 78 def self.orchio_list(collection, path=nil) response = client.send_request :get, { collection: collection, path: path } Result.new( status: orchio_status(response, 200), response: sanitize(response), count: response.body.count, :next => response.body.next, results: response.body.results.map { |result| Document.new( result['value'].merge(id: result['path']['key']), Metadata.new( collection: result['path']['collection'], key: result['path']['key'], ref: result['path']['ref'], ) )}) end
Searches the collection; encodes any whitespace contained in the query string. Returns SearchResult object where the results attribute contains an array of Document objects.
# File rails/lib/orchestrate_application/record.rb, line 54 def self.orchio_search(collection, query_str) response = client.send_request( :get, { collection: collection, path: "?query=#{query_str.gsub(/\s/, '%20')}" }) SearchResult.new( status: orchio_status(response, 200), response: sanitize(response), count: response.body.count, total_count: response.body.total_count, results: response.body.results.map { |result| Document.new( result['value'].merge(id: result['path']['key']), Metadata.new( collection: result['path']['collection'], key: result['path']['key'], ref: result['path']['ref'], score: result['score'] ))}) end
Private Class Methods
Calls client instance method.
# File rails/lib/orchestrate_application/record.rb, line 455 def self.client new.orchestrate_client end
Removes result data from the response body for a successful GET request This is done to avoid redundancy, since the result data is already included in the Result object.
# File rails/lib/orchestrate_application/record.rb, line 462 def self.sanitize(response) if response.header.code.to_i == 200 Response.new do |r| r.success = response.success? r.header = response.header r.body = ResponseBody.new nil end else response end end
Public Instance Methods
Returns the client handle.
# File rails/lib/orchestrate_application/record.rb, line 334 def orchestrate_client client end
Returns the collection name for the current instance.
# File rails/lib/orchestrate_application/record.rb, line 317 def orchestrate_collection_name ocollection end
Returns the primary_key for the current instance.
# File rails/lib/orchestrate_application/record.rb, line 329 def orchestrate_primary_key id end
Returns the ref value for the current instance. The ref value is an immutable value assigned to each version of primary_key data in an orchestrate.io collection.
# File rails/lib/orchestrate_application/record.rb, line 324 def orchestrate_ref_value __ref_value__ end
Deletes the primary_key and data associated with this instance from the collection.
# File rails/lib/orchestrate_application/record.rb, line 155 def orchio_delete response = client.send_request :delete, inst_args orchio_status response, 204 end
Delete a graph/relation from the collection.
# File rails/lib/orchestrate_application/record.rb, line 301 def orchio_delete_graph(kind, to_collection, to_key) response = client.send_request( :delete, inst_args( kind: kind, to_collection: to_collection, to_key: to_key, path: "?purge=true" )) orchio_status response, 204 end
Fetches the data associated with this id from the collection.
# File rails/lib/orchestrate_application/record.rb, line 114 def orchio_get if cache.enabled? and response = cache_request.get(id) if response.header.status == :cache doc = response.body.document end else response = client.send_request :get, inst_args if response.nil? doc = Document.new( response.body.to_hash, Metadata.new( :collection => ocollection, :key => @id, :ref => response.header.etag )) end Result.new( status: orchio_status(response, 200), response: response, results: [ doc ] ) end
Gets the key/value pair by its 'ref' value.
# File rails/lib/orchestrate_application/record.rb, line 171 def orchio_get_by_ref(ref) response = client.send_request :get, inst_args(ref: ref) Result.new( status: orchio_status(response, 200), response: response, results: [ Document.new( response.body.to_hash, Metadata.new( :collection => ocollection, :key => @id, :ref => response.header.etag ))] ) end
Gets all events of specified type, within the timestamp parameters, where timestamp = { :start => start, :end => end }.
# File rails/lib/orchestrate_application/record.rb, line 204 def orchio_get_events(event_type, timestamp={}) # add_event_type event_type if cache.enabled? and response = cache_request.get_events(id, event_type) if response.header.status == :cache docs = response.body.documents count = docs.length end else response = client.send_request( :get, inst_args(event_type: event_type, timestamp: timestamp) ) docs = response.body.results.map { |result| Document.new result['value'].merge( key: @id, etype: event_type, timestamp: result['timestamp'] ), Metadata.new( collection: ocollection, key: @id, ref: funkify("#{event_type}#{result['timestamp']}"), etype: event_type, ) } cache.save_event(id, event_type) if cache.enabled? and count == 0 end Result.new( status: orchio_status(response, 200), response: response, count: response.body.count, results: docs ) end
Gets the graph for the specified kind of relation.
# File rails/lib/orchestrate_application/record.rb, line 251 def orchio_get_graph(kind) # add_relation_kind kind if cache.enabled? and response = cache_request.get_graph(id, kind) if response.header.status == :cache docs = response.body.documents count = docs.length end else response = client.send_request :get, inst_args(kind: kind) docs = response.body.results.map { |result| Document.new( result['value'].merge(id: result['path']['key']), Metadata.new( :collection => result['path']['collection'], :key => result['path']['key'], :ref => result['path']['ref'], :kind => kind, :from_collection => ocollection, :from_key => @id, ))} cache.save_graph(id, kind) if cache.enabled? and count == 0 end Result.new( status: orchio_status(response, 200), response: response, count: response.body.count, results: docs ) end
Deletes the primary_key and purges all of its immutable data from the collection.
# File rails/lib/orchestrate_application/record.rb, line 162 def orchio_purge response = client.send_request :delete, inst_args(path: "?purge=true") orchio_status response, 204 end
Updates the collection with the data associated with this instance.
# File rails/lib/orchestrate_application/record.rb, line 137 def orchio_put(jdoc) response = client.send_request :put, inst_args(json: jdoc) if cache.enabled? simple_cache.save( Document.new( response.body.to_hash, Metadata.new( :collection => ocollection, :key => @id, :ref => response.header.etag ))) end set_ref_value response orchio_status response, 201 end
# File rails/lib/orchestrate_application/record.rb, line 239 def orchio_put_event(event_type, timestamp=nil, document) # add_event_type event_type response = client.send_request(:put, inst_args( event_type: event_type, timestamp: timestamp, json: document )) orchio_status response, 204 end
Add a graph/relation to the collection. Store the to_key's 'ref' value if caching is enabled.
# File rails/lib/orchestrate_application/record.rb, line 283 def orchio_put_graph(kind, to_collection, to_key) # add_relation_kind kind if cache.enabled? ref = simple_cache.get_ref to_collection, to_key simple_cache.get_cc(ocollection).save_graph Metadata.new( { from_key: @id, kind: kind, ref: ref } ) end response = client.send_request( :put, inst_args(kind: kind, to_collection: to_collection, to_key: to_key) ) orchio_status response, 204 end
Updates the key/value if the send'ref' value matches the 'ref' value for the latest version in the collection.
# File rails/lib/orchestrate_application/record.rb, line 188 def orchio_put_if_match(document, ref) response = client.send_request :put, inst_args(json: document, ref: ref) set_ref_value response orchio_status response, 201 end
Updates the key/value if the key does not already exist in the collection.
# File rails/lib/orchestrate_application/record.rb, line 195 def orchio_put_if_none_match(document) orchio_put_if_match(document, '"*"') end
Private Instance Methods
Returns handle to the SimpleCacheCollection instance for this class.
# File rails/lib/orchestrate_application/record.rb, line 437 def cache @@_cache[self.class.name] end
Calls cache. Explain! Used for clarity? JMC
# File rails/lib/orchestrate_application/record.rb, line 442 def cache_request cache end
Returns the client handle for requests to the orchestrate.io api.
# File rails/lib/orchestrate_application/record.rb, line 406 def client @@client ||= Orchestrate::Application::Connect.client end
Returns a unique identifer for the specified event.
# File rails/lib/orchestrate_application/record.rb, line 430 def funkify(etype_and_timestamp) funky = etype_and_timestamp.reverse.concat("#{ocollection}#{id}") funkier = funky.split('').shuffle(random: funky.length).join.delete('_') funkiest = funkier.bytes.join.to_i % 0xffffffffff end
Returns the collection name for this instance.
This method is called during initializtion with the value of :define_collection_name from the params hash. If this value is nil or blank, it is expected that the collection name can be derived from the class name as shown:
-
class: Film => 'films'
-
class: FilmClassic => 'film_classics'
Any collection names that do not follow this convention must be specified by adding the :define_collection_name key to the params hash in the model class definition.
class Film < Orchestrate::Application::Record def initialize(params={}) params[:define_collection_name] = "Classic_Film_Collection" super(params) end end
# File rails/lib/orchestrate_application/record.rb, line 362 def init_collection_name(collection_name) (collection_name.blank?) ? File.basename(self.class.name.tableize) : collection_name end
Returns hash that merges additional arguments with the ever-present collection and key args.
# File rails/lib/orchestrate_application/record.rb, line 417 def inst_args(args={}) args.merge(collection: ocollection, key: id) end
Returns the collection name associated with the current instance's class.
# File rails/lib/orchestrate_application/record.rb, line 411 def ocollection @@_collection[self.class.name].name end
Updates the ref table collection with key/value data consisting of the current instance's collection, key, timestamp and ref values , using the ref value as the primary key. When the ref table feature is enabled, the ref table is updated after each sucessful put_key request.
# File rails/lib/orchestrate_application/record.rb, line 383 def orchio_update_ref_table(timestamp) return if ocollection == RefTable.get_collection_name if RefTable.enabled? primary_key = __ref_value__.gsub(/"/, '') doc = { xcollection: ocollection, xkey: id, xref: primary_key, timestamp: timestamp }.to_json RefTable.new(:id => primary_key).orchio_put doc end end
After a successful PUT request, updates the current instance's ref value (also referred to as the etag) and calls orchio_update_ref_table.
# File rails/lib/orchestrate_application/record.rb, line 371 def set_ref_value(response) unless response.header.code != 201 || response.header.etag.blank? @__ref_value__ = response.header.etag # orchio_update_ref_table response.header.timestamp end end
Returns handle to the SimpleCacheStore singleton instance.
# File rails/lib/orchestrate_application/record.rb, line 447 def simple_cache @@cache_store end