Search:

Search all manuals
Search this manual
Manual
Couchbaseクライアントライブラリ: Java 1.0
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
7 更新操作
Chapter Sections
Chapters

7.6. インクリメント操作

インクリメントメソッドは特定の格納された整数値を増やすことができます。これらはデクリメント操作と同様の基準で動作します。値が整数値にパースできる場合、それを更新します。更新操作はサーバー上で発生し、プロトコルレベルで提供されています。これは二段階の取得およびセット操作を簡略化しています。

API Callclient.incr(key, offset)
Asynchronousno
Description Increment the value of an existing numeric key. Couchbase Server stores numbers as unsigned numbers, therefore if you try to increment an existing negative number, it will cause an integer overflow and return a non-logical numeric result. If a key does not exist, this method will initialize it with the zero or a specified value.
Returnslong ( Numeric value )
Arguments 
String key Document ID used to identify the value
int offset Integer offset value to increment/decrement (default 1)

incr() メソッドの一つ目の形式では、サーバ側の整数を増加させるための、キーとオフセット(増分)値を受け取ります。例えばサーバの整数、dlcounter を 5 インクリメントします:

client.incr("dlcounter",5);

指定したキーの更新後の値が戻り値となります。

API Callclient.incr(key, offset, default)
Asynchronousno
Description Increment the value of an existing numeric key. Couchbase Server stores numbers as unsigned numbers, therefore if you try to increment an existing negative number, it will cause an integer overflow and return a non-logical numeric result. If a key does not exist, this method will initialize it with the zero or a specified value.
Returnslong ( 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

incr() メソッドの二つ目の形式ではデータベース内に値がまだ存在しない場合に利用されるデフォルト値の利用をサポートしています。キーが存在する場合は、デフォルト値は無視され、指定したオフセット値で値がインクリメントされます。これは、カウンタ値を記録している状況で、キーが保存されているか分からない場合に利用できます。

例えば、dlcounter キーが存在しない場合、次のコードは1000を返します:

long newcount =
    client.incr("dlcount",1,1000);

System.out.printf("Updated counter is %d\n",newcount);

後続の同様の呼び出しでは、dlcount が既に存在(1000)し、1インクリメントされるため、1001が返ります。

API Callclient.incr(key, offset, default, expiry)
Asynchronousno
Description Increment the value of an existing numeric key. Couchbase Server stores numbers as unsigned numbers, therefore if you try to increment an existing negative number, it will cause an integer overflow and return a non-logical numeric result. If a key does not exist, this method will initialize it with the zero or a specified value.
Returnslong ( 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).

incr() メソッドの三つ目の形式は、キーが存在しない場合のデフォルト値に加え、指定キーの有効期限の設定を可能とします。

例えば、値がない場合のデフォルト値を1000、有効期限を1時間(3600秒)でカウンタをインクリメントします:

long newcount =
    client.incr("dlcount",1,1000,3600);

有効期限付きの値については次を参照してください。「有効期限」

API Callclient.asyncIncr(key, offset)
Asynchronousyes
Description Increment the value of an existing numeric key. Couchbase Server stores numbers as unsigned numbers, therefore if you try to increment an existing negative number, it will cause an integer overflow and return a non-logical numeric result. If a key does not exist, this method will initialize it with the zero or a specified value.
ReturnsFuture<Long> ( Asynchronous request value, as Long )
Arguments 
String key Document ID used to identify the value
int offset Integer offset value to increment/decrement (default 1)

asyncIncr() メソッドはキーに対応する値の非同期のインクリメントをサポートします。非同期のインクリメントはインクリメント操作の戻り値をすぐに利用しない場合に便利です。

OperationFuture<Long> incrOp =
    client.asyncIncr("samplekey",1,1000,24000);