Class: Couchbase::Datastructures::CouchbaseQueue

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

Overview

A CouchbaseQueue is implements FIFO queue with 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::CouchbaseQueue.new) ⇒ CouchbaseQueue

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

Parameters:

  • id (String)

    the id of the document to back the queue.

  • collection (Collection)

    the collection through which to interact with the document.

  • options (Options::CouchbaseList) (defaults to: Options::CouchbaseQueue.new)

    customization of the datastructure



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

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

Instance Method Details

#clear(parent_span: nil) ⇒ Object

Removes all elements from the queue



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

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

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

Calls the given block once for each element in the queue, 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
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 48

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

Returns:

  • (Boolean)

    returns true if queue is empty



84
85
86
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 84

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 queue.

Returns:

  • (Integer)

    returns the number of elements in the queue.



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

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

#pop(parent_span: nil) ⇒ Object? Also known as: deq, shift

Retrieves object from the queue

Returns:

  • (Object, nil)

    queue entry or nil



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 123

def pop(parent_span: nil)
  @observability.record_operation(Observability::OP_QUEUE_POP, parent_span, self) do |obs_handler|
    obj, cas = begin
      options = @options.lookup_in_options.clone
      options.parent_span = obs_handler.op_span
      result = @collection.lookup_in(@id, [
                                       LookupInSpec.get("[-1]"),
                                     ], @options.lookup_in_options)
      [
        result.exists?(0) ? result.content(0) : nil,
        result.cas,
      ]
    end
    begin
      options = Options::MutateIn.new(
        parent_span: obs_handler.op_span,
        cas: cas,
      )
      @collection.mutate_in(@id, [
                              MutateInSpec.remove("[-1]"),
                            ], options)
    end
    obj
  rescue Error::CasMismatch
    retry
  rescue Error::DocumentNotFound, Error::PathNotFound
    nil
  end
end

#push(obj, parent_span: nil) ⇒ CouchbaseQueue Also known as: enq, <<

Adds the given value to the queue

Parameters:

  • obj (Object)

Returns:



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 104

def push(obj, parent_span: nil)
  @observability.record_operation(Observability::OP_QUEUE_PUSH, 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_prepend("", [obj]),
                          ], options)
  rescue Error::PathExists
    # ignore
  end
  self
end