Class: Riagent::Persistence::RiakDTSetStrategy

Inherits:
RiakKVStrategy show all
Defined in:
lib/riagent/persistence/riak_dt_set_strategy.rb

Instance Attribute Summary collapse

Attributes inherited from RiakKVStrategy

#bucket

Attributes inherited from PersistenceStrategy

#client, #collection_name, #model_class

Instance Method Summary collapse

Methods inherited from RiakKVStrategy

#allows_query?, #client, #find, #from_riak_object, #new_riak_object, #update

Methods inherited from PersistenceStrategy

#initialize

Constructor Details

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

Instance Attribute Details

#key_list_setRiak::Crdt::Set

Return the Crdt Set object that keeps track of keys in this collection

Returns:

  • (Riak::Crdt::Set)


77
78
79
80
# File 'lib/riagent/persistence/riak_dt_set_strategy.rb', line 77

def key_list_set 
  # Note: Assumes that the Bucket Type for Sets is the default 'sets'
  @key_list_set || Riak::Crdt::Set.new(self.key_lists_bucket, self.key_list_set_name)
end

Instance Method Details

#add_key(key) ⇒ Object

Adds a key to the collection key list (usually done as part of an insert) Added as a standalone method for ease of testing

Parameters:

  • key (String)

    Key to be added to list



32
33
34
# File 'lib/riagent/persistence/riak_dt_set_strategy.rb', line 32

def add_key(key)
  self.key_list_set.add(key)
end

#all(results_limit) ⇒ Array<Riagent::ActiveDocument>

Return all the documents in the collection. Uses Riak 2.0 CRDT Set Data type to keep track of collection key list

Parameters:

  • results_limit (Integer)

    Number of results returned (currently ignored)

Returns:



40
41
42
43
44
45
# File 'lib/riagent/persistence/riak_dt_set_strategy.rb', line 40

def all(results_limit)
  keys = self.all_keys
  # TODO: Trim keys to results_limit
  all_docs_hash = self.bucket.get_many(keys)
  all_docs_hash.values
end

#all_keysArray<String>

Return all keys in the collection

Returns:

  • (Array<String>)

    List of all keys in the collection



49
50
51
# File 'lib/riagent/persistence/riak_dt_set_strategy.rb', line 49

def all_keys
  self.key_list_set.members.to_a
end

#delete_key(key) ⇒ Object

Deletes a key from the key list (usually called by remove()).



54
55
56
57
# File 'lib/riagent/persistence/riak_dt_set_strategy.rb', line 54

def delete_key(key)
  key_list_set = self.key_list_set.reload
  key_list_set.remove(key)
end

#delete_key_listObject

Clears the key list set



60
61
62
63
# File 'lib/riagent/persistence/riak_dt_set_strategy.rb', line 60

def delete_key_list
  # Perform a Riak DELETE operation (using the bucket type interface)
  self.key_lists_bucket.delete self.key_list_set_name, type: 'sets'
end

#insert(document) ⇒ String

Insert a document into the collection. Also inserts the document’s key into the key list set.

Parameters:

Returns:

  • (String)

    Document key



69
70
71
72
73
# File 'lib/riagent/persistence/riak_dt_set_strategy.rb', line 69

def insert(document)
  doc_key = super
  self.add_key(doc_key)
  doc_key
end

#key_list_set_nameString

Return the key name of the set that keeps track of keys in this collection

Returns:

  • (String)


84
85
86
# File 'lib/riagent/persistence/riak_dt_set_strategy.rb', line 84

def key_list_set_name
  '_rg_keys_' + self.collection_name()
end

#key_lists_bucketRiak::Bucket

Return the bucket in which the Riagent collection key lists are kept

Returns:

  • (Riak::Bucket)


90
91
92
# File 'lib/riagent/persistence/riak_dt_set_strategy.rb', line 90

def key_lists_bucket
  self.client.bucket('_rg_key_lists')
end

#remove(document) ⇒ Object



94
95
96
97
98
# File 'lib/riagent/persistence/riak_dt_set_strategy.rb', line 94

def remove(document)
  doc_key = document.key
  super
  self.delete_key(doc_key)
end