Search:

Search all manuals
Search this manual
Manual
Couchbase Client Library: Java 1.1
Community Wiki and Resources
Download Client Library
JavaDoc
Couchbase Developer Guide 2.0
Couchbase Server Manual 2.0
Java Client Library
SDK Forum
Wiki: Java Client Library
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
1.3 Working with Documents
Chapter Sections
Chapters

1.3.1. Creating and Updating Documents

Couchbase Server provides a set of commands to store documents. The commands are very similar to each other and differ only in their meaning on the server-side. These are:

The SDK provides overloaded methods for these operations, but to start out here are the simplest forms:

String key = "mykey";
int timeout = 0;
String doc = "something";
OperationFuture<Boolean> setResult = client.set(key, timeout, doc);
OperationFuture<Boolean> addResult = client.add(key, timeout, doc);
OperationFuture<Boolean> replaceResult = client.replace(key, timeout, doc);

Couchbase Server takes performance very seriously, so all these operations are non-blocking (asynchronous). They return immediately and are handled in a different thread behind the scenes. If you want to explicitly wait until the operation has finished, you can use the get() method on the returned future. If you do so, your calling thread will wait until the operation has been fulfilled by the server and is marked as done. If something goes wrong, the return value of the future will be false and you can use the future.getStatus().getMessage() method to see why it failed.

The second param on all these mutation operations is a timeout. When set to 0, the document will be stored forever (or until deleted manually). If set to (for example) 10, the document will be deleted automatically after 10 seconds. If you want to use Couchbase Server as a session store, this comes in very handy (amongst lots of other use cases).