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
Create a set instance. The bucket type is determined by the first of these sources:
-
The ‘bucket_type`
String
argument -
A {BucketTyped::Bucket} as the ‘bucket` argument
-
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]
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 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
# File lib/riak/crdt/set.rb, line 37 def batch batcher = BatchSet.new self yield batcher operate batcher.operations end
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
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
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
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 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
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
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
# 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