Search:

Search all manuals
Search this manual
Manual
Couchbase Client Library Ruby 1.0
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
7 Ruby — Update Operations
Chapter Sections
Chapters

7.3. Decrement Methods

The decrement methods reduce the value of a given key if the corresponding value can be parsed to an integer value. These operations are provided at a protocol level to eliminate the need to get, update, and reset a simple integer value in the database. All the Ruby Client Library methods support the use of an explicit offset value that will be used to reduce the stored value in the database.

API Callobject.decrement(key [, offset ] [, ruby-incr-decr-options ])
Asynchronousno
Description Decrement the value of an existing numeric key. The Couchbase Server stores numbers as unsigned values. Therefore the lowest you can decrement is to zero.
Returnsfixnum ( Value for a given key. A fixed number )
Arguments 
string key Document ID used to identify the value
offset Integer offset value to increment/decrement (default 1)
hash ruby-incr-decr-options Hash of options containing key/value pairs
 Structure definition:
 :create (boolean) :create  
  Default is false. If set to true, it will initialize the key with zero value and zero flags (use :initial option to set another initial value). Note: this will not increment or decrement the missing value once it is initialized.
 :initial (fixnum) :create  
  Default is 0. Can be an integer (up to 64 bits) for missing key initialization. This option automatically implies the :create option is true, regardless of the setting.
 :ttl (int) :ttl (Expiration)  
  Time for document to exist in server before it is automatically destroyed. This option symbol is :ttl and the value can be any number representing seconds.
 :extended (boolean) :extended  
  Default is false. If set to true, the operation will return an array, [value, cas], otherwise it returns just the value.

The first form of the decr method accepts the keyname and offset value to be used when reducing the server-side integer. For example, to decrement the server integer dlcounter by 5:

couchbase.set("counter", 10)
couchbase.decr("counter", 5)
couchbase.get("counter") #returns 5

The following demonstrates different options available when using decrement:

#decrement key by one (default)

c.decr("foo")

#decrement by 50

c.decr("foo", 50)

#decrement key or initialize with zero

c.decr("foo", :create => true)

#decrement key or initialize with 3

c.decr("foo", 50, :initial => 3)

#decrement key and get CAS value

val, cas = c.decr("foo", :extended => true)

#decrementing signed number

c.set("foo", -100)
c.decrement("foo", 100500)   #=> 0

#decrementing zero
c.set("foo", 0)
c.decrement("foo", 100500)   #=> 0

#asynchronous use of decrement
c.run do
      c.decr("foo") do |ret|
            ret.operation   #=> :decrement
            ret.success?    #=> true
            ret.key         #=> "foo"
            ret.value
            ret.cas
      end
end