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

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ BinaryCollection

Returns a new instance of BinaryCollection.

Parameters:



24
25
26
27
# File 'lib/couchbase/binary_collection.rb', line 24

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

Instance Method Details

#append(id, content, options = Options::Append.new) ⇒ 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.new)

    custom options to customize the request

Returns:



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

def append(id, content, options = Options::Append.new)
  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.new) ⇒ 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.new)

    custom options to customize the request

Returns:



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

def decrement(id, options = Options::Decrement.new)
  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.new) ⇒ 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.new)

    custom options to customize the request

Returns:



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

def increment(id, options = Options::Increment.new)
  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.new) ⇒ 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.new)

    custom options to customize the request

Returns:



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

def prepend(id, content, options = Options::Prepend.new)
  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