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
Public Instance Methods
@return [Boolean] Does this persistence strategy support querying?
# File lib/riagent/persistence/riak_kv_strategy.rb, line 33 def allows_query? false end
@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
@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
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
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
@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
@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
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
@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