Class: Couchbase::Datastructures::CouchbaseSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/couchbase/datastructures/couchbase_set.rb

Overview

A CouchbaseSet is implements Enumerable interface and backed by Collection document (more specifically a JSON array).

Note: sets are restricted to containing primitive types only due to server-side comparison restrictions.

Instance Method Summary collapse

Constructor Details

#initialize(id, collection, options = Options::CouchbaseSet.new) ⇒ CouchbaseSet

Create a new List, backed by the document identified by id in collection.

Parameters:

  • id (String)

    the id of the document to back the set.

  • collection (Collection)

    the collection through which to interact with the document.

  • options (Options::CouchbaseSet) (defaults to: Options::CouchbaseSet.new)

    customization of the datastructure



35
36
37
38
39
40
41
# File 'lib/couchbase/datastructures/couchbase_set.rb', line 35

def initialize(id, collection, options = Options::CouchbaseSet.new)
  @id = id
  @collection = collection
  @options = options
  @cas = 0
  @observability = @collection.instance_variable_get(:@observability)
end

Instance Method Details

#add(obj, parent_span: nil) ⇒ CouchbaseSet

Adds the given value to the set

Parameters:

  • obj (Object)

Returns:



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/couchbase/datastructures/couchbase_set.rb', line 93

def add(obj, parent_span: nil)
  @observability.record_operation(Observability::OP_SET_ADD, parent_span, self) do |obs_handler|
    options = @options.mutate_in_options.clone
    options.parent_span = obs_handler.op_span
    @collection.mutate_in(@id, [
                            MutateInSpec.array_add_unique("", obj),
                          ], options)
  rescue Error::PathExists
    # ignore
  end
  self
end

#clear(parent_span: nil) ⇒ Object

Removes all elements from the set



107
108
109
110
111
112
113
114
115
116
# File 'lib/couchbase/datastructures/couchbase_set.rb', line 107

def clear(parent_span: nil)
  @observability.record_operation(Observability::OP_SET_CLEAR, parent_span, self) do |obs_handler|
    options = @options.remove_options.clone
    options.parent_span = obs_handler.op_span
    @collection.remove(@id, options)
    nil
  rescue Error::DocumentNotFound
    nil
  end
end

#delete(obj, parent_span: nil) ⇒ Boolean

Deletes the given object from the set.

Returns:

  • (Boolean)

    true if the value has been removed



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/couchbase/datastructures/couchbase_set.rb', line 121

def delete(obj, parent_span: nil)
  @observability.record_operation(Observability::OP_SET_DELETE, parent_span, self) do |obs_handler|
    result = @collection.get(@id, Options::Get.new(parent_span: obs_handler.op_span))
    idx = result.content.index(obj)
    return false unless idx

    options = Options::MutateIn.new(parent_span: obs_handler.op_span)
    options.cas = result.cas
    @collection.mutate_in(@id, [
                            MutateInSpec.remove("[#{idx}]"),
                          ], options)
    true
  rescue Error::CasMismatch
    retry
  rescue Error::DocumentNotFound
    false
  end
end

#each(parent_span: nil) {|item| ... } ⇒ CouchbaseSet, Enumerable

Calls the given block once for each element in the set, passing that element as a parameter.

Yield Parameters:

  • item (Object)

Returns:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/couchbase/datastructures/couchbase_set.rb', line 48

def each(parent_span: nil, &)
  if block_given?
    current =
      @observability.record_operation(Observability::OP_SET_EACH, parent_span, self) do |obs_handler|
        options = @options.get_options.clone
        options.parent_span = obs_handler.op_span
        result = @collection.get(@id, options)
        @cas = result.cas
        result.content
      rescue Error::DocumentNotFound
        @cas = 0
        []
      end
    current.each(&)
    self
  else
    enum_for(:each, parent_span: parent_span)
  end
end

#empty?(parent_span: nil) ⇒ Boolean

Returns true if set is empty

Returns:

  • (Boolean)

    returns true if set is empty



85
86
87
# File 'lib/couchbase/datastructures/couchbase_set.rb', line 85

def empty?(parent_span: nil)
  size(parent_span: parent_span).zero?
end

#length(parent_span: nil) ⇒ Integer Also known as: size

Returns the number of elements in the set.

Returns:

  • (Integer)

    returns the number of elements in the set.



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/couchbase/datastructures/couchbase_set.rb', line 69

def length(parent_span: nil)
  @observability.record_operation(Observability::OP_SET_LENGTH, parent_span, self) do |obs_handler|
    options = @options.lookup_in_options.clone
    options.parent_span = obs_handler.op_span
    result = @collection.lookup_in(@id, [
                                     LookupInSpec.count(""),
                                   ], options)
    result.content(0)
  rescue Error::DocumentNotFound
    0
  end
end