Interface ObjectRepository<T>

  • Type Parameters:
    T - the type of the object to store.
    All Superinterfaces:
    org.dizitart.no2.common.meta.AttributesAware, AutoCloseable, EventAware, PersistentCollection<T>

    public interface ObjectRepository<T>
    extends PersistentCollection<T>
    Represents a type-safe persistent java object collection. An object repository is backed by a NitriteCollection, where all objects are converted into a Document and saved into the database.

    An object repository is observable like its underlying NitriteCollection.

    Create a repository
     
     // create/open a database
     Nitrite db = Nitrite.builder()
          .openOrCreate("user", "password");
    
     // create an object repository
     ObjectRepository<Employee> employeeStore = db.getRepository(Employee.class);
    
     // insert an object
     Employee emp = new Employee();
     emp.setName("John Doe");
     employeeStore.insert(emp);
     
     
    Since:
    1.0
    Author:
    Anindya Chatterjee.
    See Also:
    EventAware, Document, NitriteId, CollectionEventListener, EventBus, NitriteCollection
    • Method Detail

      • insert

        default WriteResult insert​(T object,
                                   T... others)
        Inserts object into this repository. If the object contains a value marked with Id, then the value will be used as a unique key to identify the object in the repository.

        If any of the value is already indexed in the repository, then after insertion the index will also be updated.

        NOTE: This operations will notify all CollectionEventListener instances registered to this collection with change type EventType.Insert.

        Parameters:
        object - the object to insert
        others - other objects to insert in a batch.
        Returns:
        the result of the write operation.
        Throws:
        ValidationException - if object is null.
        InvalidIdException - if the id value contains null value.
        InvalidIdException - if the id value contains non comparable type, i.e. type that does not implement Comparable.
        InvalidIdException - if the id contains value which is not of the same java type as of other objects' id in the collection.
        UniqueConstraintException - if the value of id value clashes with the id of another object in the collection.
        UniqueConstraintException - if a value of the object is indexed, and it violates the unique constraint in the collection(if any).
        See Also:
        NitriteId, WriteResult
      • update

        default WriteResult update​(Filter filter,
                                   T update)
        Updates object in the repository. If the filter does not find any object in the collection, then the update object will be inserted.

        If the filter is null, it will update all objects in the collection.

        CAUTION: If the update object has a non null value in the id value, this value will be removed before update.

        NOTE: This operations will notify all CollectionEventListener instances registered to this collection with change type EventType.Update.

        Parameters:
        filter - the filter to apply to select objects from the collection.
        update - the modifications to apply.
        Returns:
        the result of the update operation.
        Throws:
        ValidationException - if the update object is null.
      • update

        WriteResult update​(Filter filter,
                           T update,
                           UpdateOptions updateOptions)
        Updates object in the repository. Update operation can be customized with the help of updateOptions.

        If the filter is null, it will update all objects in the collection unless justOnce is set to true in updateOptions.

        CAUTION: If the update object has a non null value in the id value, this value will be removed before update.

        NOTE: This operations will notify all CollectionEventListener instances registered to this collection with change type EventType.Update or EventType.Insert.

        Parameters:
        filter - the filter to apply to select objects from the collection.
        update - the modifications to apply.
        updateOptions - various options for update operation.
        Returns:
        the result of the update operation.
        Throws:
        ValidationException - if the update object is null.
        ValidationException - if updateOptions is null.
      • update

        default WriteResult update​(Filter filter,
                                   Document update)
        Updates object in the repository by setting the field specified in document.

        If the filter is null, it will update all objects in the collection.

        CAUTION: The update document should not contain _id field.

        NOTE: This operations will notify all CollectionEventListener instances registered to this collection with change type EventType.Update.

        Parameters:
        filter - the filter to apply to select objects from the collection.
        update - the modifications to apply.
        Returns:
        the result of the update operation.
        Throws:
        ValidationException - if the update object is null.
      • update

        WriteResult update​(Filter filter,
                           Document update,
                           boolean justOnce)
        Updates object in the repository by setting the field specified in document. Update operation can either update the first matching object or all matching objects depending on the value of justOnce.

        If the filter is null, it will update all objects in the collection unless justOnce is set to true.

        CAUTION: The update document should not contain _id field.

        NOTE: This operations will notify all CollectionEventListener instances registered to this collection with change type EventType.Update.

        Parameters:
        filter - the filter to apply to select objects from the collection.
        update - the modifications to apply.
        justOnce - indicates if update should be applied on first matching object or all.
        Returns:
        the result of the update operation.
        Throws:
        ValidationException - if the update object is null.
      • remove

        default WriteResult remove​(Filter filter)
        Removes matching elements from the collection.

        If the filter is null, it will remove all objects from the collection.

        NOTE: This operations will notify all CollectionEventListener instances registered to this collection with change type EventType.Remove.

        Parameters:
        filter - the filter to apply to select elements from collection.
        Returns:
        the result of the remove operation.
      • remove

        WriteResult remove​(Filter filter,
                           boolean justOne)
        Removes object from the collection. Remove operation can be customized by removeOptions.

        If the filter is null, it will remove all objects in the collection unless justOnce is set to true in removeOptions.

        NOTE: This operations will notify all CollectionEventListener instances registered to this collection with change type EventType.Remove.

        Parameters:
        filter - the filter to apply to select objects from collection.
        justOne - indicates if only one element will be removed or all of them.
        Returns:
        the result of the remove operation.
      • find

        default Cursor<T> find()
        Returns a cursor to all objects in the collection.
        Returns:
        a cursor to all objects in the collection.
      • find

        default Cursor<T> find​(Filter filter)
        Applies a filter on the collection and returns a cursor to the selected objects.

        See Filter for all available filters.

        NOTE: If there is an index on the value specified in the filter, this operation will take advantage of the index.

        Parameters:
        filter - the filter to apply to select objects from collection.
        Returns:
        a cursor to all selected objects.
        See Also:
        Filter, Cursor.project(Class)
      • find

        default Cursor<T> find​(FindOptions findOptions)
        Returns a customized cursor to all objects in the collection.
        Parameters:
        findOptions - specifies pagination, sort options for the cursor.
        Returns:
        a cursor to all selected objects.
      • find

        Cursor<T> find​(Filter filter,
                       FindOptions findOptions)
        Applies a filter on the collection and returns a customized cursor to the selected objects.

        NOTE: If there is an index on the value specified in the filter, this operation will take advantage of the index.

        Parameters:
        filter - the filter to apply to select objects from collection.
        findOptions - specifies pagination, sort options for the cursor.
        Returns:
        a cursor to all selected objects.
      • getById

        <I> T getById​(I id)
        Gets a single element from the repository by its id. If no element is found, it will return null. The object must have a field annotated with Id, otherwise this call will throw InvalidIdException.
        Type Parameters:
        I - the type parameter
        Parameters:
        id - the id value
        Returns:
        the unique object associated with the id.
        Throws:
        ValidationException - if id is null.
        InvalidIdException - if the id value is null, or the type is not compatible.
        NotIdentifiableException - if the object has no field marked with Id.
      • getType

        Class<T> getType()
        Returns the type associated with the ObjectRepository.
        Returns:
        type of the object.