class Riak::Crdt::Set

A distributed set containing strings, using the Riak 2 Data Types feature.

Uses the Ruby standard library ‘::Set` frequently, so the full class names will be used frequently.

Public Class Methods

new(bucket, key, bucket_type = nil, options = {}) click to toggle source

Create a set instance. The bucket type is determined by the first of these sources:

  1. The ‘bucket_type` String argument

  2. A {BucketTyped::Bucket} as the ‘bucket` argument

  3. The ‘Crdt::Base::DEFAULT_BUCKET_TYPES` entry

@param bucket [Bucket] the {Riak::Bucket} for this set @param [String, nil] key The name of the set. A nil key makes

Riak assign a key.

@param [String] bucket_type The optional bucket type for this set. @param options [Hash]

Calls superclass method Riak::Crdt::Base::new
# File lib/riak/crdt/set.rb, line 22
def initialize(bucket, key, bucket_type = nil, options = {})
  super(bucket, key, bucket_type || :set, options)
end

Public Instance Methods

add(element, options = {}) click to toggle source

Add a {String} to the {Riak::Crdt::Set}

@param [String] element the element to add to the set @param [Hash] options

# File lib/riak/crdt/set.rb, line 83
def add(element, options = {})
  operate operation(:add, element), options
end
batch() { |batcher| ... } click to toggle source
# File lib/riak/crdt/set.rb, line 37
def batch
  batcher = BatchSet.new self

  yield batcher

  operate batcher.operations
end
delete(element, options = {})
Alias for: remove
empty?() click to toggle source

Check to see if this structure has any members.

@return [Boolean] if the structure is empty

# File lib/riak/crdt/set.rb, line 67
def empty?
  members.empty?
end
include?(candidate) click to toggle source

Check to see if a given string is present in this data structure.

@param [String] candidate string to check for inclusion in this structure @return [Boolean] if the structure includes

# File lib/riak/crdt/set.rb, line 75
def include?(candidate)
  members.include?(candidate)
end
members() click to toggle source

Gets the current set members from Riak if necessary, and return the stdlib ‘::Set` of them.

@return [::Set] a Ruby standard library {::Set} of the members

of this {Riak::Crdt::Set}
# File lib/riak/crdt/set.rb, line 50
def members
  reload if dirty?
  @members
end
Also aliased as: value
pretty_print(pp) click to toggle source
Calls superclass method Riak::Crdt::Base#pretty_print
# File lib/riak/crdt/set.rb, line 98
def pretty_print(pp)
  super pp do
    pp.comma_breakable
    pp.pp to_a
  end
end
remove(element, options = {}) click to toggle source

Remove a {String} from the {Riak::Crdt::Set}

@param [String] element to remove from the set @param [Hash] options

# File lib/riak/crdt/set.rb, line 91
def remove(element, options = {})
  raise CrdtError::SetRemovalWithoutContextError unless context?
  operate operation(:remove, element), options
end
Also aliased as: delete
to_a() click to toggle source

Cast this {Riak::Crdt::Set} to a Ruby {Array}.

@return [Array] array of set members

# File lib/riak/crdt/set.rb, line 60
def to_a
  members.to_a
end
value()
Alias for: members
vivify(value) click to toggle source

Yields a ‘BatchSet` to proxy multiple set operations into a single Riak update. The `BatchSet` has the same methods as this {Riak::Crdt::Set}.

@yieldparam batch_set [BatchSet] collects set operations

# File lib/riak/crdt/set.rb, line 31
def vivify(value)
  value.each(&:freeze)
  @members = ::Set.new(value)
  @members.freeze
end

Private Instance Methods

operation(direction, element) click to toggle source
# File lib/riak/crdt/set.rb, line 110
def operation(direction, element)
  Operation::Update.new.tap do |op|
    op.type = :set
    op.value = { direction => element }
  end
end