Class: Couchbase::BinaryCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/couchbase/binary_collection.rb,
lib/couchbase/binary_collection_options.rb

Overview

Allows to perform certain operations on non-JSON documents.

Defined Under Namespace

Classes: CounterResult

Constant Summary collapse

AppendOptions =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

TODO: deprecate in 3.1

::Couchbase::Options::Append
PrependOptions =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

TODO: deprecate in 3.1

::Couchbase::Options::Prepend
IncrementOptions =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

TODO: deprecate in 3.1

::Couchbase::Options::Increment
DecrementOptions =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

TODO: deprecate in 3.1

::Couchbase::Options::Decrement

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ BinaryCollection

Returns a new instance of BinaryCollection.

Parameters:



26
27
28
29
# File 'lib/couchbase/binary_collection.rb', line 26

def initialize(collection)
  @collection = collection
  @backend = collection.instance_variable_get(:@backend)
end

Instance Method Details

#append(id, content, options = Options::Append::DEFAULT) ⇒ Collection::MutationResult

Appends binary content to the document

Examples:

Append “bar” to the content of the existing document

collection.upsert("mydoc", "foo")
collection.binary.append("mydoc", "bar", Options::Append(timeout: 3_000))
collection.get("mydoc", Options::Get(transcoder: nil)).content #=> "foobar"

Parameters:

  • id (String)

    the document id which is used to uniquely identify it

  • content (String)

    the binary content to append to the document

  • options (Options::Append) (defaults to: Options::Append::DEFAULT)

    custom options to customize the request

Returns:



43
44
45
46
47
48
49
50
# File 'lib/couchbase/binary_collection.rb', line 43

def append(id, content, options = Options::Append::DEFAULT)
  resp = @backend.document_append(@collection.bucket_name, @collection.scope_name, @collection.name,
                                  id, content, options.to_backend)
  Collection::MutationResult.new do |res|
    res.cas = resp[:cas]
    res.mutation_token = @collection.send(:extract_mutation_token, resp)
  end
end

#decrement(id, options = Options::Decrement::DEFAULT) ⇒ CounterResult

Decrements the counter document by one of the number defined in the options

Examples:

Decrement value by 2, and initialize to 100 if it does not exist

res = collection.binary.decrement("raw_counter", Options::Decrement(delta: 2, initial: 100))
res.value #=> 100
res = collection.binary.decrement("raw_counter", Options::Decrement(delta: 2, initial: 100))
res.value #=> 98

Parameters:

  • id (String)

    the document id which is used to uniquely identify it

  • options (Options::Decrement) (defaults to: Options::Decrement::DEFAULT)

    custom options to customize the request

Returns:



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

def decrement(id, options = Options::Decrement::DEFAULT)
  resp = @backend.document_decrement(@collection.bucket_name, @collection.scope_name, @collection.name, id,
                                     options.to_backend)
  CounterResult.new do |res|
    res.cas = resp[:cas]
    res.content = resp[:content]
    res.mutation_token = @collection.send(:extract_mutation_token, resp)
  end
end

#increment(id, options = Options::Increment::DEFAULT) ⇒ CounterResult

Increments the counter document by one of the number defined in the options

Examples:

Increment value by 10, and initialize to 0 if it does not exist

res = collection.binary.increment("raw_counter", Options::Increment(delta: 10, initial: 0))
res.content #=> 0
res = collection.binary.increment("raw_counter", Options::Increment(delta: 10, initial: 0))
res.content #=> 10

Parameters:

  • id (String)

    the document id which is used to uniquely identify it

  • options (Options::Increment) (defaults to: Options::Increment::DEFAULT)

    custom options to customize the request

Returns:



85
86
87
88
89
90
91
92
93
# File 'lib/couchbase/binary_collection.rb', line 85

def increment(id, options = Options::Increment::DEFAULT)
  resp = @backend.document_increment(@collection.bucket_name, @collection.scope_name, @collection.name, id,
                                     options.to_backend)
  CounterResult.new do |res|
    res.cas = resp[:cas]
    res.content = resp[:content]
    res.mutation_token = @collection.send(:extract_mutation_token, resp)
  end
end

#prepend(id, content, options = Options::Prepend::DEFAULT) ⇒ Collection::MutationResult

Prepends binary content to the document

Examples:

Prepend “bar” to the content of the existing document

collection.upsert("mydoc", "foo")
collection.binary.prepend("mydoc", "bar", Options::Prepend(timeout: 3_000))
collection.get("mydoc", Options::Get(transcoder: nil)).content #=> "barfoo"

Parameters:

  • id (String)

    the document id which is used to uniquely identify it

  • content (String)

    the binary content to prepend to the document

  • options (Options::Prepend) (defaults to: Options::Prepend::DEFAULT)

    custom options to customize the request

Returns:



64
65
66
67
68
69
70
71
# File 'lib/couchbase/binary_collection.rb', line 64

def prepend(id, content, options = Options::Prepend::DEFAULT)
  resp = @backend.document_prepend(@collection.bucket_name, @collection.scope_name, @collection.name,
                                   id, content, options.to_backend)
  Collection::MutationResult.new do |res|
    res.cas = resp[:cas]
    res.mutation_token = @collection.send(:extract_mutation_token, resp)
  end
end