Class CouchbaseDocument
- All Implemented Interfaces:
CouchbaseStorable
CouchbaseDocument
is an abstract representation of a document stored inside Couchbase Server.
It acts like a TreeMap
, but only allows those types to be written that are supported by the underlying
storage format, which is currently JSON. Note that JSON conversion is not happening here, but performed at a
different stage based on the payload stored in the CouchbaseDocument
.
In addition to the actual content, meta data is also stored. This especially refers to the document ID and its expiration time. Note that this information is not mandatory, since documents can be nested and therefore only the topmost document most likely has an ID.
- Author:
- Michael Nitschinger
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Defnes the default expiration time for the document. -
Constructor Summary
ConstructorDescriptionCreates a completely emptyCouchbaseDocument
.Creates a emptyCouchbaseDocument
, and set the ID immediately.CouchbaseDocument
(Object id, int expiration) Creates a emptyCouchbaseDocument
with ID and expiration time. -
Method Summary
Modifier and TypeMethodDescriptionfinal boolean
containsKey
(String key) Returns true if it contains a payload for the specified key.final boolean
containsValue
(Object value) Returns true if it contains the given value.export()
Returns the current payload, including all recursive elements.final Object
Potentially get a value from the payload with the given key.Returns the underlying payload.int
Returns the expiration time of the document.getId()
Returns the ID of the document.final CouchbaseDocument
Store a value with the given key for later retreival.setContent
(com.couchbase.client.java.json.JsonObject payload) Allows to set the full payload as a json object for convenience.setContent
(Map<String, Object> content) Allows to set the full payload as a map.setExpiration
(int expiration) Set the expiration time of the document.Sets the unique ID of the document per bucket.final int
size()
Returns the size of the attributes in this document (not nested).final int
size
(boolean recursive) Retruns the size of the attributes in this and recursive documents.toString()
A string representation of expiration, id and payload.
-
Field Details
-
DEFAULT_EXPIRATION_TIME
public static final int DEFAULT_EXPIRATION_TIMEDefnes the default expiration time for the document.- See Also:
-
-
Constructor Details
-
CouchbaseDocument
public CouchbaseDocument()Creates a completely emptyCouchbaseDocument
. -
CouchbaseDocument
Creates a emptyCouchbaseDocument
, and set the ID immediately.- Parameters:
id
- the document ID.
-
CouchbaseDocument
Creates a emptyCouchbaseDocument
with ID and expiration time.- Parameters:
id
- the document ID.expiration
- the expiration time of the document.
-
-
Method Details
-
put
Store a value with the given key for later retreival.- Parameters:
key
- the key of the attribute.value
- the actual content to be stored.- Returns:
- the
CouchbaseDocument
for chaining.
-
get
Potentially get a value from the payload with the given key.- Parameters:
key
- the key of the attribute.- Returns:
- the value to which the specified key is mapped, or null if does not contain a mapping for the key.
-
export
Returns the current payload, including all recursive elements.It either returns the raw results or makes sure that the recusrive elements are also exported properly.
- Returns:
-
containsKey
Returns true if it contains a payload for the specified key.- Parameters:
key
- the key of the attribute.- Returns:
- true if it contains a payload for the specified key.
-
containsValue
Returns true if it contains the given value.- Parameters:
value
- the value to check for.- Returns:
- true if it contains the specified value.
-
size
public final int size()Returns the size of the attributes in this document (not nested).- Returns:
- the size of the attributes in this document (not nested).
-
size
public final int size(boolean recursive) Retruns the size of the attributes in this and recursive documents.- Parameters:
recursive
- wheter nested attributes should be taken into account.- Returns:
- the size of the attributes in this and recursive documents.
-
getContent
Returns the underlying payload.Note that unlike
export()
, the nested objects are not converted, so the "raw" map is returned.- Returns:
- the underlying payload.
-
setContent
Allows to set the full payload as a map.- Parameters:
content
- the payload to set- Returns:
- this document for chaining purposes.
-
setContent
Allows to set the full payload as a json object for convenience.- Parameters:
payload
- the payload to set- Returns:
- this document for chaining purposes.
-
getExpiration
public int getExpiration()Returns the expiration time of the document.If the expiration time is 0, then the document will be persisted until deleted manually ("forever").
- Returns:
- the expiration time of the document.
-
setExpiration
Set the expiration time of the document.If the expiration time is 0, then the document will be persisted until deleted manually ("forever").
Expiration should be expressed as seconds if <= 30 days (30 x 24 x 60 x 60 seconds), or as an expiry date (UTC, UNIX time ie. seconds form the Epoch) if > 30 days.
- Parameters:
expiration
-- Returns:
- the
CouchbaseDocument
for chaining.
-
getId
Returns the ID of the document.- Returns:
- the ID of the document.
-
setId
Sets the unique ID of the document per bucket.- Parameters:
id
- the ID of the document.- Returns:
- the
CouchbaseDocument
for chaining.
-
toString
A string representation of expiration, id and payload.
-