Class: Riagent::Persistence::RiakKVStrategy

Inherits:
PersistenceStrategy show all
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

RiakDTSetStrategy, RiakNoIndexStrategy

Instance Attribute Summary collapse

Attributes inherited from PersistenceStrategy

#collection_name, #model_class

Instance Method Summary collapse

Methods inherited from PersistenceStrategy

#initialize

Constructor Details

This class inherits a constructor from Riagent::Persistence::PersistenceStrategy

Instance Attribute Details

#bucketRiak::Bucket

Returns Riak bucket associated with this model/collection.

Returns:

  • (Riak::Bucket)

    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?.

Returns:

  • (Boolean)

    Does this persistence strategy support querying?



33
34
35
# File 'lib/riagent/persistence/riak_kv_strategy.rb', line 33

def allows_query?
  false
end

#clientRiak::Client

Returns Riak client (lazy-initialized, cached in current Thread).

Returns:

  • (Riak::Client)

    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.

Parameters:

  • key (String)

Returns:



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

Parameters:

  • riak_object (Riak::RObject)
  • persisted (Boolean) (defaults to: true)

    Mark the document as persisted/not new?

Returns:

  • (ActiveDocument|nil)

    ActiveDocument instance, or nil if the Riak Object is nil



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.

Parameters:

Returns:

  • (String)

    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.

Parameters:

  • Optional (String|nil)

    key

Returns:

  • (Riak::RObject)

    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

Parameters:



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.

Parameters:

Returns:

  • (Integer)

    Document key



109
110
111
# File 'lib/riagent/persistence/riak_kv_strategy.rb', line 109

def update(document)
  self.insert(document)
end