There are several Couchbase SDK APIs which are considered
'convenience' methods in that they provide commonly used
functionality in a single method call. They tend to be less
resource intensive processes that can be used in place of a
series of get()/set() calls that you
would otherwise have to perform to achieve the same result.
Typically these convenience methods enable you to perform an
operation in single request to Couchbase Server, instead of
having to do two requests. The following is a summary of
recommended alternative calls:
Multi-Get/Bulk-Get: When you want to retrieve multiple items
and have all of the keys, then performing a multi-get
retrieves all the keys in a single request as opposed to a
request per key. It is therefore faster and less resource
intensive than performing individual, sequential
get() calls. The following
demonstrates a multi-get in Ruby:
keys = ["foo", "bar","baz"] // alternate method signatures for multi-get conn.get(keys) conn.get(["foo", "bar", "baz"]) conn.get("foo", "bar", "baz")
Each key we provide in the array will be sent in a single request, and Couchbase Server will return a single response with all existing keys. Consult the API documentation for your chosen SDK to find out more about a specify method call for multi-gets.
Increment/Decrement: These are two other convenience methods
which enable you to perform an update without having to call
a get() and
set(). Typically if you want to
increment or decrement an integer, you would need to 1)
retrieve it with a request to Couchbase, 2) add an amount to
ithe value if it exists, or set it to an initial value
otherwise and 3) then store the value Couchbase Server. If a
key is not found, Couchbase Server will store the initial
value, but not increment or decrement it as part of the
operation. With increment and decrement methods, you can
perform all three steps in a single method call, as we do in
this Ruby SDK example:
client.increment("score", :delta => 1, :initial => 100);
In this example in we provide a key, and also two other
parameters: one is an initial value, the later is the
increment amount. Most Couchbase SDKs follow a similar
signature. The first parameter is the key you want to
increment or decrement, the second parameter is an initial
value if the value does not already exist, and the third
parameter is the amount that Couchbase Server will
increment/decrement the existing value. In a single server
request and response, increment and decrement methods
provide you the convenience of establishing a key-document
if it does not exist, and provide the ability to
increment/decrement. Over thousands or millions of
documents, this approach will improve application
performance compared to using
get()/set() to perform the
functional equivalent.
Prepend/Append: These two methods provide the functional equivalent of: 1) retrieving a key from Couchbase Server with a request, 2) adding binary content to the document, and then 3) making a second request to Couchbase Server to store the updated value. With prepend and append, you can perform these three steps in a single request to Couchbase Server. The following illustrates this in Python. To see the full example in Python, including encoding and decoding the data, see Maintaining a Set:
def modify(cb, indexName, op, keys): encoded = encodeSet(keys, op) try: cb.append(indexName, encoded) except KeyError: # If we can't append, and we're adding to the set, # we are trying to create the index, so do that. if op == '+': cb.add(indexName, encoded) def add(mc, indexName, *keys): """Add the given keys to the given set.""" modify(cb, indexName, '+', keys) def remove(cb, indexName, *keys): """Remove the given keys from the given set.""" modify(cb, indexName, '-', keys)
This example can be used to manage a set of keys, such as
'a', 'b', 'c' and can indicate that given
keys are including or not included in a set by using
append. For instance, given a set
'a', 'b', 'c', if you update the set to
read +a +b +c -b this actually represents
{a, c}. We have method
modify() which will take a
Couchbase client object, a named set, an operator, and keys.
The modify() tries to append the
new key with the operator into the named set, and since
append fails if the set does not exist,
modify can add the new set.
Compared to using a separate get()
call, appending the string to the start of the document,
then saving the document back to Couchbase with another
request, we have accomplished it in a single call/request.
Once again you improve application performance if you
substitute get()/set() sequences,
with a single append or prepend; this is particular so if
you are performing this on thousands or millions of
documents.
Append()/Prepend() can add raw
serialized data to existing data for a key. The Couchbase
Server treats an existing value as a binary stream and
concatenates the new content to either beginning or end.
Non-linear, hierarchical formats in the database will merely
have the new information added at the start or end. There will
be no logic which adds the information to a certain place in a
stored document structure or object.
Therefore, if you have a serialized object in Couchbase Server
and then append or prepend, the existing content in the
serialized object will not be extended. For instance, if you
append() an integer to an Array
stored in Couchbase, this will result in the record containing
a serialized array, and then the serialized integer.