Class: Blodsband::Riak::Sset

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

Overview

A concurrent set. Named Sset to avoid colliding with ruby standard Set.

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

#delete(v) ⇒ Object

Parameters:

  • v (Object)

    a value to remove from this set.



58
59
60
61
62
63
64
65
66
67
# File 'lib/blodsband/riak/sset.rb', line 58

def delete(v)
  element_key = bucket.get(key_for(v))
  if element_key.nil?
    nil
  else
    element = Element.find(self, element_key)
    element.delete
    element.value
  end
end

#include?(v) ⇒ true, false

Returns whether this set includes v.

Parameters:

  • v (Object)

    a value to check if this set includes.

Returns:

  • (true, false)

    whether this set includes v.



16
17
18
# File 'lib/blodsband/riak/sset.rb', line 16

def include?(v)
  !bucket.get(key_for(v)).nil?
end

#intersect(sset) ⇒ Set<Object>

Returns a ruby Set containing the intersection of this Blodsband::Riak::Sset and the argument.

Parameters:

Returns:

  • (Set<Object>)

    a ruby Set containing the intersection of this Blodsband::Riak::Sset and the argument.



47
48
49
50
51
52
53
# File 'lib/blodsband/riak/sset.rb', line 47

def intersect(sset)
  if size > sset.size
    retain(sset.to_a)
  else
    sset.retain(to_a)
  end
end

#retain(values) ⇒ Set<Object>

Returns the values that are included in this set.

Parameters:

  • values (Array<Object>)

    some values to check if they are included in this set.

Returns:

  • (Set<Object>)

    the values that are included in this set.



25
26
27
28
29
30
31
32
33
# File 'lib/blodsband/riak/sset.rb', line 25

def retain(values)
  map = values.inject({}) do |sum, value|
    sum.merge(key_for(value) => value)
  end
  included = bucket.has_many?(map.keys)
  Set.new(map.reject do |k, v|
            !included.include?(k)
          end.values)
end

#to_setSet<Object>

Returns this Blodsband::Riak::Sset as a ruby Set.

Returns:



38
39
40
# File 'lib/blodsband/riak/sset.rb', line 38

def to_set
  Set.new(to_a)
end