Class: Blodsband::Riak::Map

Inherits:
List
  • Object
show all
Defined in:
lib/blodsband/riak/map.rb

Overview

A concurrent map.

Instance Attribute Summary

Attributes inherited from List

#bucket, #key

Instance Method Summary collapse

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.

Parameters:

  • key (Object)

    a key to the map.

Returns:

  • (Object)

    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.

Parameters:

  • key (Object)

    a key to insert.

  • value (Object)

    a value to insert.

Returns:

  • (Object)

    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.

Parameters:

  • k (Object)

    a key to this map to delete.

Returns:

  • (Object)

    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.

Parameters:

  • k (Object)

    a key to this map.

Returns:

  • (true, false)

    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.

Parameters:

Returns:

  • (Hash<Object, Object>)

    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.

Parameters:

  • keys (Array<Object>)

    some keys to check if they exist in this map.

Returns:

  • (Hash<Object, Object>)

    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