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 Java Client Library methods support the use of an explicit offset value that will be used to reduce the stored value in the database.
| API Call | client.decr(key, offset) | ||
| Asynchronous | no | ||
| 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. | ||
| Returns | long (
Numeric value
) | ||
| Arguments | |||
String key | Document ID used to identify the value | ||
int offset | Integer offset value to increment/decrement (default 1) | ||
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:
client.decr("dlcounter",5);The return value is the updated value of the specified key.
| API Call | client.decr(key, offset, default) | ||
| Asynchronous | no | ||
| 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. | ||
| Returns | long (
Numeric value
) | ||
| Arguments | |||
String key | Document ID used to identify the value | ||
int offset | Integer offset value to increment/decrement (default 1) | ||
int default | Default value to increment/decrement if key does not exist | ||
The second form of the decr() method will
decrement the given key by the specified offset
value if the key already exists, or set the key to the specified
default value if the key does not exist. This
can be used in situations where you are recording a counter value
but do not know whether the key exists at the point of storage.
For example, if the key dlcounter does not
exist, the following fragment will return 1000:
long newcount = client.decr("dlcount",1,1000); System.out.printf("Updated counter is %d\n",newcount);
A subsequent identical call will return the value 999 as the key
dlcount already exists.
| API Call | client.decr(key, offset, default, expiry) | ||
| Asynchronous | no | ||
| 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. | ||
| Returns | long (
Numeric value
) | ||
| Arguments | |||
String key | Document ID used to identify the value | ||
int offset | Integer offset value to increment/decrement (default 1) | ||
int default | Default value to increment/decrement if key does not exist | ||
int expiry | Expiry time for key. Values larger than 30*24*60*60 seconds (30 days) are interpreted as absolute times (from the epoch). | ||
The third form of the decr() method the
decrement operation, with a default value and with the addition of
setting an expiry time on the stored value. For example, to
decrement a counter, using a default of 1000 if the value does not
exist, and an expiry of 1 hour (3600 seconds):
long newcount = client.decr("dlcount",1,1000,3600);
For information on expiry values, see Section 4.4, “Expiry Values”.
| API Call | client.asyncDecr(key, offset) | ||
| Asynchronous | yes | ||
| 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. | ||
| Returns | long (
Numeric value
) | ||
| Arguments | |||
String key | Document ID used to identify the value | ||
int offset | Integer offset value to increment/decrement (default 1) | ||
The asynchronous form of the asyncDecr()
method enables you to decrement a value without waiting for a
response. This can be useful in situations where you do not need
to know the updated value or merely want to perform a decrement
and continue processing.
For example, to asynchronously decrement a given key:
OperationFuture<Long> decrOp = client.asyncDecr("samplekey",1,1000,24000);