Interface Document

  • All Superinterfaces:
    Cloneable, Iterable<Pair<String,​Object>>, Serializable

    public interface Document
    extends Iterable<Pair<String,​Object>>, Cloneable, Serializable
    Represents a document in Nitrite database.

    Nitrite document are composed of key-value pairs. A key is always a String and value can be anything including null.

    Nitrite document supports nested documents as well. The key of a nested document is a String separated by NitriteConfig.getFieldSeparator(). By default, Nitrite uses `.` as field separator. This can be changed by setting NitriteConfig.fieldSeparator(String).

    For example, if a document has a nested document { "a" : { "b" : 1 } }, then the value of inside the nested document can be retrieved by calling get(String) with key a.b.

    Below fields are reserved and cannot be used as key in a document.

    • _id: The unique identifier of the document. If not provided, Nitrite will generate a unique NitriteId for the document during insertion.
    • _revision: The revision number of the document.
    • _source: The source of the document.
    • _modified: The last modified time of the document in milliseconds since epoch.
    Since:
    1.0
    Author:
    Anindya Chatterjee
    • Method Detail

      • createDocument

        static Document createDocument()
        Creates an empty document.
        Returns:
        the document
      • createDocument

        static Document createDocument​(String key,
                                       Object value)
        Creates a new document initialized with the given key/value pair.
        Parameters:
        key - the key
        value - the value
        Returns:
        the document
      • createDocument

        static Document createDocument​(Map<String,​Object> documentMap)
        Creates a new document initialized with the given map.
        Parameters:
        documentMap - the map
        Returns:
        the document
      • put

        Document put​(String key,
                     Object value)
        Associates the specified value with the specified key in this document.

        NOTE: An embedded field is also supported.

        Parameters:
        key - the key
        value - the value
        Returns:
        the document
      • get

        Object get​(String key)
        Returns the value to which the specified key is associated with, or null if this document contains no mapping for the key.
        Parameters:
        key - the key
        Returns:
        the object
      • get

        <T> T get​(String key,
                  Class<T> type)
        Returns the value of type <T> to which the specified key is associated, or null if this document contains no mapping for the key.

        NOTE: This method may not work for fields containing primitive types. Use get(String) instead.

        Type Parameters:
        T - the type parameter
        Parameters:
        key - the key
        type - the type
        Returns:
        the value
      • getId

        NitriteId getId()
        Return the nitrite id associated with this document.
        Returns:
        the nitrite id
      • getFields

        Set<String> getFields()
        Retrieves all fields (top level and embedded) associated with this document.
        Returns:
        the fields
      • hasId

        boolean hasId()
        Checks if this document has a nitrite id.
        Returns:
        the boolean
      • remove

        void remove​(String key)
        Removes the key and its value from the document.
        Parameters:
        key - the key
      • clone

        Document clone()
        Creates and returns a copy of this document.
        Returns:
        document
      • size

        int size()
        Returns the number of entries in the document.
        Returns:
        the int
      • merge

        Document merge​(Document update)
        Merges a document in this document.
        Parameters:
        update - the update
        Returns:
        the document
      • containsKey

        boolean containsKey​(String key)
        Checks if a top level key exists in the document.
        Parameters:
        key - the key
        Returns:
        the boolean
      • containsField

        boolean containsField​(String field)
        Checks if a top level field or embedded field exists in the document.
        Parameters:
        field - the field
        Returns:
        the boolean
      • getRevision

        default Integer getRevision()
        Gets the document revision number.
        Returns:
        the revision
      • getSource

        default String getSource()
        Gets the source of this document.
        Returns:
        the source
      • getLastModifiedSinceEpoch

        default Long getLastModifiedSinceEpoch()
        Gets last modified time of this document since epoch.
        Returns:
        the last modified since epoch