Class: Blodsband::Riak::Map
Overview
A concurrent map.
Instance Attribute Summary
Attributes inherited from List
Instance Method Summary collapse
-
#[](key) ⇒ Object
The value in this map for the given key.
-
#[]=(key, value) ⇒ Object
The inserted value.
-
#delete(key) ⇒ Object
The value for the key, or nil.
-
#include?(k) ⇒ true, false
Whether this map contains the argument.
-
#intersect(map) ⇒ Hash<Object, Object>
The intersection of this map with the argument.
-
#retain(keys) ⇒ Hash<Object, Object>
The argument keys that exist in this map and their values.
Methods inherited from List
#append, #append_and_get_element, #append_element_after, #delete_element, #each, #empty?, #exists?, #first, #initialize, #last, #pop, #prepend, #prepend_element_before, #reload, #save, #shift, #size, #stack_each, #to_a, #to_s, #validate
Constructor Details
This class inherits a constructor from Blodsband::Riak::List
Instance Method Details
#[](key) ⇒ Object
Returns the value in this map for the given key.
16 17 18 19 20 21 22 23 24 |
# File 'lib/blodsband/riak/map.rb', line 16 def [](key) element_key = bucket.get(key_for(key)) if element_key.nil? nil else map_key, value = Element.find(self, element_key).value value end end |
#[]=(key, value) ⇒ Object
Returns the inserted value.
95 96 97 98 99 100 101 102 103 |
# File 'lib/blodsband/riak/map.rb', line 95 def []=(key, value) element_key = bucket.get(key_for(key)) if element_key.nil? append([key, value]) else Element.find(self, element_key).value = [key, value] end value end |
#delete(key) ⇒ Object
Returns the value for the key, or nil.
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/blodsband/riak/map.rb', line 78 def delete(key) element_key = bucket.get(key_for(key)) if element_key.nil? nil else element = Element.find(self, element_key) element.delete element.value.last end end |
#include?(k) ⇒ true, false
Returns whether this map contains the argument.
69 70 71 |
# File 'lib/blodsband/riak/map.rb', line 69 def include?(k) !bucket.get(key_for(k)).nil? end |
#intersect(map) ⇒ Hash<Object, Object>
Returns the intersection of this map with the argument.
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/blodsband/riak/map.rb', line 52 def intersect(map) if size > map.size retain(map.to_a.collect do |k,v| k end) else map.retain(to_a.collect do |k,v| k end) end end |
#retain(keys) ⇒ Hash<Object, Object>
Returns the argument keys that exist in this map and their values.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/blodsband/riak/map.rb', line 31 def retain(keys) bucket.has_many?(keys.collect do |map_key| key_for(map_key) end).collect do |bucket_key| bucket.aget(bucket_key) end.collect do |f| f.get end.collect do |element_key| bucket.aget(element_key) end.collect do |f| f.get end.inject({}) do |sum, element_value| sum.merge(element_value.first => element_value.last) end end |