Class: Riagent::Persistence::RiakKVStrategy
- Inherits:
-
PersistenceStrategy
- Object
- PersistenceStrategy
- Riagent::Persistence::RiakKVStrategy
- Defined in:
- lib/riagent/persistence/riak_kv_strategy.rb
Overview
General Riak Key/Value persistence strategy. Read and Write documents as Riak objects. The listing/indexing method will depend on subclass
Direct Known Subclasses
Instance Attribute Summary collapse
-
#bucket ⇒ Riak::Bucket
Riak bucket associated with this model/collection.
Attributes inherited from PersistenceStrategy
#collection_name, #model_class
Instance Method Summary collapse
-
#allows_query? ⇒ Boolean
Does this persistence strategy support querying?.
-
#client ⇒ Riak::Client
Riak client (lazy-initialized, cached in current Thread).
-
#find(key) ⇒ ActiveDocument|nil
Fetch a document by key.
-
#from_riak_object(riak_object, persisted = true) ⇒ ActiveDocument|nil
Converts from a Riak::RObject instance to an instance of ActiveDocument.
-
#insert(document) ⇒ String
Document key.
-
#new_riak_object(key = nil) ⇒ Riak::RObject
New Riak object instance for this model/collection.
-
#remove(document) ⇒ Object
Deletes the riak object that stores the document.
-
#update(document) ⇒ Integer
Document key.
Methods inherited from PersistenceStrategy
Constructor Details
This class inherits a constructor from Riagent::Persistence::PersistenceStrategy
Instance Attribute Details
#bucket ⇒ Riak::Bucket
Returns Riak bucket associated with this model/collection.
38 39 40 |
# File 'lib/riagent/persistence/riak_kv_strategy.rb', line 38 def bucket @bucket ||= self.client.bucket(self.collection_name) end |
Instance Method Details
#allows_query? ⇒ Boolean
Returns Does this persistence strategy support querying?.
33 34 35 |
# File 'lib/riagent/persistence/riak_kv_strategy.rb', line 33 def allows_query? false end |
#client ⇒ Riak::Client
Returns Riak client (lazy-initialized, cached in current Thread).
43 44 45 |
# File 'lib/riagent/persistence/riak_kv_strategy.rb', line 43 def client @client ||= Riagent.riak_client # See lib/configuration.rb end |
#find(key) ⇒ ActiveDocument|nil
Fetch a document by key.
50 51 52 53 54 55 56 57 58 59 60 61 |
# 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) ⇒ ActiveDocument|nil
Converts from a Riak::RObject instance to an instance of ActiveDocument
67 68 69 70 71 72 73 74 75 |
# 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) ⇒ String
Returns Document key.
79 80 81 82 83 84 85 86 87 88 89 90 |
# 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) ⇒ Riak::RObject
Returns New Riak object instance for this model/collection.
94 95 96 97 98 |
# 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) ⇒ Object
Deletes the riak object that stores the document
102 103 104 105 |
# 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) ⇒ Integer
Returns Document key.
109 110 111 |
# File 'lib/riagent/persistence/riak_kv_strategy.rb', line 109 def update(document) self.insert(document) end |