Data Structures Reference
You can use complex data structures such as dictionaries and lists in Couchbase.
Data structures in Couchbase are similar in concept to data structures in the Java Collections Framework:
See our Data Model pages.
Here is a list of common operations:
|
Add a key to the map.
|
||
|
Remove a key from a map.
|
||
|
Get an item from a map.
If the key is not found, a |
||
|
Add an item to the end of a list.
|
||
|
Add an item to the beginning of a list.
|
||
|
Remove a value from a list.
|
||
|
Set an element at a specific index in the list.
|
||
|
Get an item from a list by its index.
If the index is out of range, a |
||
|
Add an item to a set, if the item does not yet exist in the set.
Note that a set is just a list.
You can retrieve the entire set by simply using a full-document
|
||
|
Check if a value exists in the set.
|
||
|
Remove an item from a set, if it exists.
An exception is not thrown if the item does not exist.
You can determine if an item existed or not by the return value.
If the item did not exist beforehand,
|
||
|
Add an item to the beginning of the queue.
Note that a queue is just a list.
You can retrieve items from the middle of the queue by using |
||
|
Remove an item from the end of the queue and return it.
If the queue is empty, then |
||
|
These methods get the length of the data structure. For maps, this is the number of key-value pairs inside the map. For lists, queues, and sets, this is the number of elements in the structure.
|
Note that there are only two basic types: map and list. Types such as queue and set are merely derivatives of list.
Data Structures and Key-Value APIs
Data structures can be accessed using key-value APIs as well. In fact, the data structure API is actually a client-side wrapper around the key-value and sub-document API. Most of the data structure APIs wrap the sub-document API directly.
| Because the data structure API is just a wrapper around the various key-value APIs, you are free to switch between them in your code. |
Collections Framework Integration
In addition to the Bucket level methods for working with data structures, the Java SDK provides implementations of the Map, List, Set, and Queue interfaces from the Java Collections Framework.
Instead of maintaining in-memory storage, these implementations are backed by JSON documents stored in Couchbase Server.
The implementations are thread-safe and suitable for concurrent use.
The Map, List, and Queue implementations may contain values of the following types:
-
String
-
Integer
-
Long
-
Double
-
Boolean
-
BigInteger
-
BigDecimal
-
JsonObject
-
JsonArray
The Set implementation may contain values of all of the above types except JsonObject and JsonArray.
CouchbaseMap
The CouchbaseMap<V> class implements Map<String, V>. It allows null values, but does not allow null keys.
Example usage:
Unresolved include directive in modules/ref/pages/data-structures.adoc - include::example$DataStructuresExample.java[]
CouchbaseArrayList
The CouchbaseArrayList<V> class implements List<V>. It allows null values.
Example usage:
Unresolved include directive in modules/ref/pages/data-structures.adoc - include::example$DataStructuresExample.java[]