Class: Riak::Crdt::Set

Inherits:
Base 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 Base

#==, #context?, #dirty?, #inspect_name, #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: {})


25
26
27
# File 'lib/riak/crdt/set.rb', line 25

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: {})


86
87
88
# File 'lib/riak/crdt/set.rb', line 86

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

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

Yields:

  • (batcher)


40
41
42
43
44
45
46
# File 'lib/riak/crdt/set.rb', line 40

def batch
  batcher = BatchSet.new self

  yield batcher

  operate batcher.operations
end

#empty?Boolean

Check to see if this structure has any members.

Returns:

  • (Boolean)

    if the structure is empty



70
71
72
# File 'lib/riak/crdt/set.rb', line 70

def empty?
  members.empty?
end

#include?(candidate) ⇒ Boolean

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

Parameters:

  • candidate (String)

    string to check for inclusion in this structure

Returns:

  • (Boolean)

    if the structure includes



78
79
80
# File 'lib/riak/crdt/set.rb', line 78

def include?(candidate)
  members.any? { |m| equal_bytes?(m, candidate) }
end

#members::Set Also known as: value

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

Returns:



53
54
55
56
# File 'lib/riak/crdt/set.rb', line 53

def members
  reload if dirty?
  @members
end

#pretty_print(pp) ⇒ Object



101
102
103
104
105
106
# File 'lib/riak/crdt/set.rb', line 101

def pretty_print(pp)
  super pp do
    pp.comma_breakable
    pp.pp to_a
  end
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:



94
95
96
97
# File 'lib/riak/crdt/set.rb', line 94

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



63
64
65
# File 'lib/riak/crdt/set.rb', line 63

def to_a
  members.to_a
end