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
# 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
end

Instance Method Details

#clearObject

Removes all elements from the queue



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

def clear
  @collection.remove(@id, @options.remove_options)
  nil
rescue Error::DocumentNotFound
  nil
end

#each {|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:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 47

def each(&)
  if block_given?
    begin
      result = @collection.get(@id, @options.get_options)
      current = result.content
      @cas = result.cas
    rescue Error::DocumentNotFound
      current = []
      @cas = 0
    end
    current.each(&)
    self
  else
    enum_for(:each)
  end
end

#empty?Boolean

Returns true if queue is empty

Returns:

  • (Boolean)

    returns true if queue is empty



77
78
79
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 77

def empty?
  size.zero?
end

#lengthInteger Also known as: size

Returns the number of elements in the queue.

Returns:

  • (Integer)

    returns the number of elements in the queue.



65
66
67
68
69
70
71
72
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 65

def length
  result = @collection.lookup_in(@id, [
                                   LookupInSpec.count(""),
                                 ], @options.lookup_in_options)
  result.content(0)
rescue Error::DocumentNotFound
  0
end

#popObject? Also known as: deq, shift

Retrieves object from the queue

Returns:

  • (Object, nil)

    queue entry or nil



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 110

def pop
  result = @collection.lookup_in(@id, [
                                   LookupInSpec.get("[-1]"),
                                 ], @options.lookup_in_options)
  obj = result.exists?(0) ? result.content(0) : nil
  options = Collection::MutateInOptions.new
  options.cas = result.cas
  @collection.mutate_in(@id, [
                          MutateInSpec.remove("[-1]"),
                        ], options)
  obj
rescue Error::CasMismatch
  retry
rescue Error::DocumentNotFound, Error::PathNotFound
  nil
end

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

Adds the given value to the queue

Parameters:

  • obj (Object)

Returns:



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

def push(obj)
  begin
    @collection.mutate_in(@id, [
                            MutateInSpec.array_prepend("", [obj]),
                          ], @options.mutate_in_options)
  rescue Error::PathExists
    # ignore
  end
  self
end