class Riagent::Persistence::RiakKVStrategy

General Riak Key/Value persistence strategy. Read and Write documents as Riak objects. The listing/indexing method will depend on subclass

Attributes

bucket[W]

Public Instance Methods

allows_query?() click to toggle source

@return [Boolean] Does this persistence strategy support querying?

# File lib/riagent/persistence/riak_kv_strategy.rb, line 33
def allows_query?
  false
end
bucket() click to toggle source

@return [Riak::Bucket] Riak bucket associated with this model/collection

# File lib/riagent/persistence/riak_kv_strategy.rb, line 38
def bucket
  @bucket ||= self.client.bucket(self.collection_name)
end
client() click to toggle source

@return [Riak::Client] Riak client (lazy-initialized, cached in current Thread)

# File lib/riagent/persistence/riak_kv_strategy.rb, line 43
def client
  @client ||= Riagent.riak_client  # See lib/configuration.rb
end
find(key) click to toggle source

Fetch a document by key. @param [String] key @return [ActiveDocument|nil]

# File lib/riagent/persistence/riak_kv_strategy.rb, line 50
def find(key)
  begin
    result = self.bucket.get(key)
  rescue Riak::FailedRequest => fr
    if fr.not_found?
      result = nil
    else
      raise fr
    end
  end
  self.from_riak_object(result)
end
from_riak_object(riak_object, persisted=true) click to toggle source

Converts from a Riak::RObject instance to an instance of ActiveDocument @param [Riak::RObject] riak_object @param [Boolean] persisted Mark the document as persisted/not new? @return [ActiveDocument|nil] ActiveDocument instance, or nil if the Riak Object is nil

# File lib/riagent/persistence/riak_kv_strategy.rb, line 67
def from_riak_object(riak_object, persisted=true)
  return nil if riak_object.nil?
  active_doc_instance = self.model_class.from_json(riak_object.raw_data, riak_object.key)
  if persisted
    active_doc_instance.persist!  # Mark as persisted / not new
  end
  active_doc_instance.source_object = riak_object
  active_doc_instance
end
insert(document) click to toggle source

@param [Riagent::ActiveDocument] document Document to be inserted @return [String] Document key

# File lib/riagent/persistence/riak_kv_strategy.rb, line 79
def insert(document)
  if document.key.present?
    # Attempt to fetch existing object, just in case
    riak_object = self.bucket.get_or_new(document.key)
  else
    riak_object = self.new_riak_object()
  end
  riak_object.raw_data = document.to_json_document
  riak_object = riak_object.store
  document.source_object = riak_object  # store the riak object in document
  document.key = riak_object.key
end
new_riak_object(key=nil) click to toggle source

@param [String|nil] Optional key @return [Riak::RObject] New Riak object instance for this model/collection

# File lib/riagent/persistence/riak_kv_strategy.rb, line 94
def new_riak_object(key=nil)
  Riak::RObject.new(self.bucket, key).tap do |obj|
    obj.content_type = "application/json"
  end
end
remove(document) click to toggle source

Deletes the riak object that stores the document @param [Riagent::ActiveDocument] document Document to be deleted

# File lib/riagent/persistence/riak_kv_strategy.rb, line 102
def remove(document)
  self.new_riak_object(document.key).delete
  document.source_object = nil
end
update(document) click to toggle source

@param [Riagent::ActiveDocument] document Document to be updated @return [Integer] Document key

# File lib/riagent/persistence/riak_kv_strategy.rb, line 109
def update(document)
  self.insert(document)
end