Class CouchbaseDocument

java.lang.Object
org.springframework.data.couchbase.core.mapping.CouchbaseDocument
All Implemented Interfaces:
CouchbaseStorable

public class CouchbaseDocument extends Object implements CouchbaseStorable
A 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 Details

    • DEFAULT_EXPIRATION_TIME

      public static final int DEFAULT_EXPIRATION_TIME
      Defnes the default expiration time for the document.
      See Also:
  • Constructor Details

    • CouchbaseDocument

      public CouchbaseDocument()
      Creates a completely empty CouchbaseDocument.
    • CouchbaseDocument

      public CouchbaseDocument(Object id)
      Creates a empty CouchbaseDocument, and set the ID immediately.
      Parameters:
      id - the document ID.
    • CouchbaseDocument

      public CouchbaseDocument(Object id, int expiration)
      Creates a empty CouchbaseDocument with ID and expiration time.
      Parameters:
      id - the document ID.
      expiration - the expiration time of the document.
  • Method Details

    • put

      public final CouchbaseDocument put(String key, Object value)
      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

      public final Object get(String key)
      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

      public final TreeMap<String,Object> 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

      public final boolean containsKey(String key)
      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

      public final boolean containsValue(Object value)
      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

      public Map<String,Object> 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

      public CouchbaseDocument setContent(Map<String,Object> content)
      Allows to set the full payload as a map.
      Parameters:
      content - the payload to set
      Returns:
      this document for chaining purposes.
    • setContent

      public CouchbaseDocument setContent(com.couchbase.client.java.json.JsonObject payload)
      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

      public CouchbaseDocument setExpiration(int expiration)
      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

      public Object getId()
      Returns the ID of the document.
      Returns:
      the ID of the document.
    • setId

      public CouchbaseDocument setId(Object id)
      Sets the unique ID of the document per bucket.
      Parameters:
      id - the ID of the document.
      Returns:
      the CouchbaseDocument for chaining.
    • toString

      public String toString()
      A string representation of expiration, id and payload.
      Overrides:
      toString in class Object
      Returns:
      the string representation of the object.