Class: Riak::Crdt::Set

Inherits:
GrowOnlySet show all
Includes:
Util::String
Defined in:
lib/riak/crdt/set.rb

Overview

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.

Defined Under Namespace

Classes: BatchSet

Instance Attribute Summary

Attributes inherited from Base

#bucket, #bucket_type, #key

Instance Method Summary collapse

Methods included from Util::String

equal_bytes?

Methods inherited from GrowOnlySet

#empty?, #include?, #members, #pretty_print

Methods inherited from Base

#==, #context?, #dirty?, #inspect_name, #pretty_print, #pretty_print_cycle, #reload

Methods included from Util::Translation

#i18n_scope, #t

Constructor Details

#initialize(bucket, key, bucket_type = nil, options = {}) ⇒ Set

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

Parameters:

  • bucket (Bucket)

    the Bucket for this set

  • key (String, nil)

    The name of the set. A nil key makes Riak assign a key.

  • bucket_type (String) (defaults to: nil)

    The optional bucket type for this set.

  • options (Hash) (defaults to: {})


39
40
41
# File 'lib/riak/crdt/set.rb', line 39

def initialize(bucket, key, bucket_type = nil, options = {})
  super(bucket, key, bucket_type || :set, options)
end

Instance Method Details

#add(element, options = {}) ⇒ Object

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

Parameters:

  • element (String)

    the element to add to the set

  • options (Hash) (defaults to: {})


73
74
75
# File 'lib/riak/crdt/set.rb', line 73

def add(element, options = {})
  operate operation(:add, element), options
end

#batch {|batcher| ... } ⇒ Object

Yields:

  • (batcher)


54
55
56
57
58
59
60
# File 'lib/riak/crdt/set.rb', line 54

def batch
  batcher = BatchSet.new self

  yield batcher

  operate batcher.operations
end

#remove(element, options = {}) ⇒ Object Also known as: delete

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

Parameters:

  • element (String)

    to remove from the set

  • options (Hash) (defaults to: {})

Raises:



81
82
83
84
# File 'lib/riak/crdt/set.rb', line 81

def remove(element, options = {})
  raise CrdtError::SetRemovalWithoutContextError unless context?
  operate operation(:remove, element), options
end

#to_aArray

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

Returns:

  • (Array)

    array of set members



65
66
67
# File 'lib/riak/crdt/set.rb', line 65

def to_a
  super.to_a
end

#vivify(value) {|batch_set| ... } ⇒ Object

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

Yield Parameters:

  • batch_set (BatchSet)

    collects set operations



48
49
50
51
52
# File 'lib/riak/crdt/set.rb', line 48

def vivify(value)
  value.each(&:freeze)
  @members = ::Set.new(value)
  @members.freeze
end