class Orchestrate::Application::Record

Class for accessing collections belonging to an Orchestrate.io application.

PUT and DELETE requests return Orchestrate::API::Response.

GET requests return Result or SearchResult. The result data is returned as Document objects.

Attributes

id[R]

The unique primary_key that identifies a set of key/value data stored in an orchestrate.io collection.

Public Class Methods

new(params={}) click to toggle source

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
orchio_delete(collection) click to toggle source

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
orchio_delete_key(collection, key) click to toggle source

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
orchio_list(collection, path=nil) click to toggle source

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

Private Class Methods

client() click to toggle source

Calls client instance method.

# File rails/lib/orchestrate_application/record.rb, line 455
def self.client
  new.orchestrate_client
end
sanitize(response) click to toggle source

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

orchestrate_client() click to toggle source

Returns the client handle.

# File rails/lib/orchestrate_application/record.rb, line 334
def orchestrate_client
  client
end
orchestrate_collection_name() click to toggle source

Returns the collection name for the current instance.

# File rails/lib/orchestrate_application/record.rb, line 317
def orchestrate_collection_name
  ocollection
end
orchestrate_primary_key() click to toggle source

Returns the primary_key for the current instance.

# File rails/lib/orchestrate_application/record.rb, line 329
def orchestrate_primary_key
  id
end
orchestrate_ref_value() click to toggle source

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
orchio_delete() click to toggle source

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
orchio_delete_graph(kind, to_collection, to_key) click to toggle source

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
orchio_get() click to toggle source

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
orchio_get_by_ref(ref) click to toggle source

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
orchio_get_events(event_type, timestamp={}) click to toggle source

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
orchio_get_graph(kind) click to toggle source

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
orchio_purge() click to toggle source

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
orchio_put(jdoc) click to toggle source

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
orchio_put_event(event_type, timestamp=nil, document) click to toggle source
# 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
orchio_put_graph(kind, to_collection, to_key) click to toggle source

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
orchio_put_if_match(document, ref) click to toggle source

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
orchio_put_if_none_match(document) click to toggle source

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

cache() click to toggle source

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
cache_request() click to toggle source

Calls cache. Explain! Used for clarity? JMC

# File rails/lib/orchestrate_application/record.rb, line 442
def cache_request
  cache
end
client() click to toggle source

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
funkify(etype_and_timestamp) click to toggle source

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
init_collection_name(collection_name) click to toggle source

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
inst_args(args={}) click to toggle source

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
ocollection() click to toggle source

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
orchio_update_ref_table(timestamp) click to toggle source

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
set_ref_value(response) click to toggle source

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
simple_cache() click to toggle source

Returns handle to the SimpleCacheStore singleton instance.

# File rails/lib/orchestrate_application/record.rb, line 447
def simple_cache
  @@cache_store
end