Class: Couchbase::Datastructures::CouchbaseMap

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

Overview

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

Note that as such, a CouchbaseMap is restricted to the types that JSON array can contain.

Instance Method Summary collapse

Constructor Details

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

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

Parameters:

  • id (String)

    the id of the document to back the map.

  • collection (Collection)

    the Couchbase collection through which to interact with the document.

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

    customization of the datastructure



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

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

Instance Method Details

#[](key) ⇒ Object?

Returns a value from the map for the given key.

If the key cannot be found, nil will be returned.

Parameters:

  • key (String)

Returns:

  • (Object, nil)

    value, associated with the key or nil



147
148
149
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 147

def [](key)
  fetch(key, nil)
end

#[]=(key, value) ⇒ void

This method returns an undefined value.

Associate the value given by value with the key given by key.

Parameters:

  • key (String)
  • value (Object)


173
174
175
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 173

def []=(key, value)
  store(key, value)
end

#clear(parent_span: nil) ⇒ Object

Removes all elements from the map



90
91
92
93
94
95
96
97
98
99
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 90

def clear(parent_span: nil)
  @observability.record_operation(Observability::OP_MAP_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(key, parent_span: nil) ⇒ Object

Deletes the key-value pair from the map.

Parameters:

  • key (String)

Returns:

  • void



182
183
184
185
186
187
188
189
190
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 182

def delete(key, parent_span: nil)
  @observability.record_operation(Observability::OP_MAP_DELETE, parent_span, self) do |obs_handler|
    @collection.mutate_in(@id, [
                            MutateInSpec.remove(key),
                          ], Options::MutateIn.new(parent_span: obs_handler.op_span))
  rescue Error::DocumentNotFound, Error::PathNotFound
    nil
  end
end

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

Calls the given block once for each element in the map, 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_map.rb', line 48

def each(parent_span: nil, &)
  if block_given?
    current =
      @observability.record_operation(Observability::OP_MAP_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 map is empty

Returns:

  • (Boolean)

    returns true if map is empty



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

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

#fetch(key) ⇒ Object #fetch(key, default) ⇒ Object #fetch(key, &block) ⇒ Object

Returns a value from the map for the given key.

If the key cannot be found, there are several options:

  • with no other arguments, it will raise a KeyError exception
  • if default is given, then that will be returned
  • if the optional code block is specified, then that will be run and its result returned

Overloads:

  • #fetch(key) ⇒ Object

    Gets the value, associated with the key, or raise KeyError if key could not be found

    Parameters:

    • key (String)

      key

  • #fetch(key, default) ⇒ Object

    Gets the value, associated with the key, or return default if key could not be found

    Parameters:

    • key (String)

      key

  • #fetch(key, &block) ⇒ Object

    Gets the value, associated with the key, or invoke specified block and propagate its return value if key could not be found

    Parameters:

    • key (String)

      key

    Yield Returns:

    • (Object)

      the default value to return in case the key could not be found

Returns:

  • (Object)


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

def fetch(key, *rest, parent_span: nil)
  @observability.record_operation(Observability::OP_MAP_FETCH, 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.get(key),
                                   ], options)
    result.content(0)
  rescue Error::DocumentNotFound, Error::PathNotFound
    return yield if block_given?
    return rest.first unless rest.empty?

    raise KeyError, "key not found: #{key}"
  end
end

#key?(key, parent_span: nil) ⇒ Boolean Also known as: member?, include?

Returns true if the given key is present

Parameters:

  • key (String)

Returns:

  • (Boolean)


196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 196

def key?(key, parent_span: nil)
  @observability.record_operation(Observability::OP_MAP_KEY_EXISTS, 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.exists(key),
                                   ], options)
    result.exists?(0)
  rescue Error::DocumentNotFound, Error::PathNotFound
    false
  end
end

#keysArray

Returns a new array populated with the keys from the map.

Returns:

  • (Array)


215
216
217
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 215

def keys
  map { |key, _value| key }
end

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

Returns the number of elements in the map.

Returns:

  • (Integer)

    returns the number of elements in the map.



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

def length(parent_span: nil)
  @observability.record_operation(Observability::OP_MAP_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

#store(key, value, parent_span: nil) ⇒ void

This method returns an undefined value.

Associate the value given by value with the key given by key.

Parameters:

  • key (String)
  • value (Object)


157
158
159
160
161
162
163
164
165
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 157

def store(key, value, parent_span: nil)
  @observability.record_operation(Observability::OP_MAP_STORE, parent_span, self) do |obs_handler|
    options = @options.mutate_in_options.clone
    options.parent_span = obs_handler.op_span
    @collection.mutate_in(@id, [
                            MutateInSpec.upsert(key, value),
                          ], options)
  end
end

#valuesArray

Returns a new array populated with the values from the map.

Returns:

  • (Array)


222
223
224
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 222

def values
  map { |_key, value| value }
end