Interface Nitrite

  • All Superinterfaces:
    AutoCloseable

    public interface Nitrite
    extends AutoCloseable
    Nitrite is a lightweight, embedded, and self-contained Java NoSQL database. It provides an easy-to-use API to store and retrieve data. Nitrite stores data in the form of documents and supports indexing on fields within the documents to provide efficient search capabilities. Nitrite supports transactions, and provides a simple and efficient way to persist data.

    Nitrite is thread-safe and can be used in a multi-threaded environment without any issues. Nitrite is designed to be embedded within the application and does not require any external setup or installation.

    Since:
    1.0
    Author:
    Anindya Chatterjee
    See Also:
    NitriteBuilder, NitriteCollection, ObjectRepository, EntityDecorator
    • Method Detail

      • commit

        void commit()
        Commits the unsaved changes. For file based store, it saves the changes to disk if there are any unsaved changes.

        No need to call it after every change, if auto-commit is not disabled while opening the db. However, it may still be called to flush all changes to disk.

      • getCollection

        NitriteCollection getCollection​(String name)
        Opens a named collection from the store. If the collection does not exist it will be created automatically and returned. If a collection is already opened, it is returned as is. Returned collection is thread-safe for concurrent use.

        The name cannot contain below reserved strings:

        • Constants.INTERNAL_NAME_SEPARATOR
        • Constants.USER_MAP
        • Constants.INDEX_META_PREFIX
        • Constants.INDEX_PREFIX
        • Constants.OBJECT_STORE_NAME_SEPARATOR
        Parameters:
        name - the name of the collection
        Returns:
        the collection
        See Also:
        NitriteCollection
      • getRepository

        <T> ObjectRepository<T> getRepository​(Class<T> type)
        Opens a type-safe object repository from the store. If the repository does not exist it will be created automatically and returned. If a repository is already opened, it is returned as is.

        The returned repository is thread-safe for concurrent use.

        Type Parameters:
        T - the type parameter
        Parameters:
        type - the type of the object
        Returns:
        the repository containing objects of type Nitrite.
        See Also:
        ObjectRepository
      • getRepository

        <T> ObjectRepository<T> getRepository​(Class<T> type,
                                              String key)
        Opens a type-safe object repository with a key identifier from the store. If the repository does not exist it will be created automatically and returned. If a repository is already opened, it is returned as is.

        The returned repository is thread-safe for concurrent use.

        Type Parameters:
        T - the type parameter.
        Parameters:
        type - the type of the object.
        key - the key, which will be appended to the repositories name.
        Returns:
        the repository containing objects of type Nitrite.
        See Also:
        ObjectRepository
      • getRepository

        <T> ObjectRepository<T> getRepository​(EntityDecorator<T> entityDecorator)
        Opens a type-safe object repository using a EntityDecorator. If the repository does not exist it will be created automatically and returned. If a repository is already opened, it is returned as is.

        The returned repository is thread-safe for concurrent use.

        Type Parameters:
        T - the type parameter
        Parameters:
        entityDecorator - the entityDecorator
        Returns:
        the repository
      • getRepository

        <T> ObjectRepository<T> getRepository​(EntityDecorator<T> entityDecorator,
                                              String key)
        Opens a type-safe object repository using a EntityDecorator and a key identifier from the store. If the repository does not exist it will be created automatically and returned. If a repository is already opened, it is returned as is.

        The returned repository is thread-safe for concurrent use.

        Type Parameters:
        T - the type parameter
        Parameters:
        entityDecorator - the entityDecorator
        key - the key
        Returns:
        the repository
      • destroyCollection

        void destroyCollection​(String name)
        Destroys a NitriteCollection without opening it first.
        Parameters:
        name - the name of the collection
      • destroyRepository

        <T> void destroyRepository​(Class<T> type)
        Destroys an ObjectRepository without opening it first.
        Type Parameters:
        T - the type parameter
        Parameters:
        type - the type
      • destroyRepository

        <T> void destroyRepository​(Class<T> type,
                                   String key)
        Destroys a keyed-ObjectRepository without opening it first.
        Type Parameters:
        T - the type parameter
        Parameters:
        type - the type
        key - the key
      • destroyRepository

        <T> void destroyRepository​(EntityDecorator<T> type)
        Destroys an ObjectRepository without opening it first.
        Type Parameters:
        T - the type parameter
        Parameters:
        type - the type
      • destroyRepository

        <T> void destroyRepository​(EntityDecorator<T> type,
                                   String key)
        Destroys a keyed-ObjectRepository without opening it first.
        Type Parameters:
        T - the type parameter
        Parameters:
        type - the type
        key - the key
      • listCollectionNames

        Set<String> listCollectionNames()
        Gets the set of all NitriteCollections' names in the database.
        Returns:
        a set of all collection names in the database
      • listRepositories

        Set<String> listRepositories()
        Gets the set of all fully qualified class names corresponding to all ObjectRepositorys in the database.
        Returns:
        a set of all the repository names in the Nitrite database.
      • listKeyedRepositories

        Map<String,​Set<String>> listKeyedRepositories()
        Gets the map of all key to the fully qualified class names corresponding to all keyed-ObjectRepositorys in the store.
        Returns:
        a map of all keyed-repositories keyed by their names
      • hasUnsavedChanges

        boolean hasUnsavedChanges()
        Checks if there are any unsaved changes in the Nitrite database.
        Returns:
        true if there are unsaved changes, false otherwise.
      • isClosed

        boolean isClosed()
        Checks if the Nitrite database instance is closed.
        Returns:
        true if the Nitrite database instance is closed; false otherwise.
      • getStore

        NitriteStore<?> getStore()
        Returns the NitriteStore instance associated with this Nitrite database.
        Returns:
        the NitriteStore instance associated with this Nitrite database.
      • getDatabaseMetaData

        org.dizitart.no2.store.StoreMetaData getDatabaseMetaData()
        Returns the metadata of the database store.
        Returns:
        the metadata of the database store.
      • createSession

        Session createSession()
        Creates a new session for the Nitrite database. A session is a lightweight container that holds transactions. Multiple sessions can be created for a single Nitrite database instance.
        Returns:
        a new session for the Nitrite database.
      • close

        void close()
        Closes the database.
        Specified by:
        close in interface AutoCloseable
      • hasCollection

        default boolean hasCollection​(String name)
        Checks if a collection with the given name exists in the database.
        Parameters:
        name - the name of the collection to check
        Returns:
        true if a collection with the given name exists, false otherwise
      • hasRepository

        default <T> boolean hasRepository​(Class<T> type)
        Checks if a repository of the specified type exists in the database.
        Type Parameters:
        T - the type of the repository
        Parameters:
        type - the type of the repository to check for
        Returns:
        true if a repository of the specified type exists, false otherwise
      • hasRepository

        default <T> boolean hasRepository​(Class<T> type,
                                          String key)
        Checks if a repository of the specified type and the given key exists in the database.
        Type Parameters:
        T - the type of the entity
        Parameters:
        type - the entity type of the repository
        key - the key of the repository
        Returns:
        true if a repository with the given key exists for the specified entity type; false otherwise
      • hasRepository

        default <T> boolean hasRepository​(EntityDecorator<T> entityDecorator)
        Checks if a repository of the specified type described by the EntityDecorator exists in the database.
        Type Parameters:
        T - the type parameter
        Parameters:
        entityDecorator - entityDecorator
        Returns:
        true if the repository exists; false otherwise.
      • hasRepository

        default <T> boolean hasRepository​(EntityDecorator<T> entityDecorator,
                                          String key)
        Checks if a keyed-repository of the specified type described by the EntityDecorator exists in the database.
        Type Parameters:
        T - the type parameter.
        Parameters:
        entityDecorator - entityDecorator.
        key - the key, which will be appended to the repositories name.
        Returns:
        true if the repository exists; false otherwise.
      • validateCollectionName

        default void validateCollectionName​(String name)
        Validates the given collection name.
        Parameters:
        name - the name of the collection to validate
        Throws:
        ValidationException - if the name is null, empty, or contains any reserved names
      • checkOpened

        default void checkOpened()
        Checks if the Nitrite database is opened or not. Throws a NitriteIOException if the database is closed.