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 aNitriteCollection
, where all objects are converted into aDocument
and saved into the database.An object repository is observable like its underlying
Create a repositoryNitriteCollection
.// 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 Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default Cursor<T>
find()
Returns a cursor to all objects in the collection.default Cursor<T>
find(FindOptions findOptions)
Returns a customized cursor to all objects in the collection.default Cursor<T>
find(Filter filter)
Applies a filter on the collection and returns a cursor to the selected objects.Cursor<T>
find(Filter filter, FindOptions findOptions)
Applies a filter on the collection and returns a customized cursor to the selected objects.<I> T
getById(I id)
Gets a single element from the repository by its id.NitriteCollection
getDocumentCollection()
Returns the underlyingNitriteCollection
instance.Class<T>
getType()
Returns the type associated with theObjectRepository
.default WriteResult
insert(T object, T... others)
Inserts object into this repository.default WriteResult
remove(Filter filter)
Removes matching elements from the collection.WriteResult
remove(Filter filter, boolean justOne)
Removes object from the collection.default WriteResult
update(Filter filter, Document update)
Updates object in the repository by setting the field specified indocument
.WriteResult
update(Filter filter, Document update, boolean justOnce)
Updates object in the repository by setting the field specified indocument
.default WriteResult
update(Filter filter, T update)
Updates object in the repository.WriteResult
update(Filter filter, T update, UpdateOptions updateOptions)
Updates object in the repository.-
Methods inherited from interface org.dizitart.no2.common.meta.AttributesAware
getAttributes, setAttributes
-
Methods inherited from interface org.dizitart.no2.collection.events.EventAware
subscribe, unsubscribe
-
Methods inherited from interface org.dizitart.no2.common.PersistentCollection
addProcessor, clear, close, createIndex, createIndex, drop, dropAllIndices, dropIndex, getStore, hasIndex, insert, isDropped, isIndexing, isOpen, listIndices, rebuildIndex, remove, size, update, update
-
-
-
-
Method Detail
-
insert
default WriteResult insert(T object, T... others)
Inserts object into this repository. If the object contains a value marked withId
, 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 typeEventType.Insert
.- Parameters:
object
- the object to insertothers
- other objects to insert in a batch.- Returns:
- the result of the write operation.
- Throws:
ValidationException
- ifobject
isnull
.InvalidIdException
- if the id value containsnull
value.InvalidIdException
- if the id value contains non comparable type, i.e. type that does not implementComparable
.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 theupdate
object will be inserted.If the
filter
isnull
, it will update all objects in the collection.CAUTION: If the
update
object has a nonnull
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 typeEventType.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 theupdate
object isnull
.
-
update
WriteResult update(Filter filter, T update, UpdateOptions updateOptions)
Updates object in the repository. Update operation can be customized with the help ofupdateOptions
.If the
filter
isnull
, it will update all objects in the collection unlessjustOnce
is set totrue
inupdateOptions
.CAUTION: If the
update
object has a nonnull
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 typeEventType.Update
orEventType.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 theupdate
object isnull
.ValidationException
- ifupdateOptions
isnull
.
-
update
default WriteResult update(Filter filter, Document update)
Updates object in the repository by setting the field specified indocument
.If the
filter
isnull
, 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 typeEventType.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 theupdate
object isnull
.
-
update
WriteResult update(Filter filter, Document update, boolean justOnce)
Updates object in the repository by setting the field specified indocument
. Update operation can either update the first matching object or all matching objects depending on the value ofjustOnce
.If the
filter
isnull
, it will update all objects in the collection unlessjustOnce
is set totrue
.CAUTION: The
update
document should not contain_id
field.NOTE: This operations will notify all
CollectionEventListener
instances registered to this collection with change typeEventType.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 theupdate
object isnull
.
-
remove
default WriteResult remove(Filter filter)
Removes matching elements from the collection.If the
filter
isnull
, it will remove all objects from the collection.NOTE: This operations will notify all
CollectionEventListener
instances registered to this collection with change typeEventType.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 byremoveOptions
.If the
filter
isnull
, it will remove all objects in the collection unlessjustOnce
is set totrue
inremoveOptions
.NOTE: This operations will notify all
CollectionEventListener
instances registered to this collection with change typeEventType.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 returnnull
. The object must have a field annotated withId
, otherwise this call will throwInvalidIdException
.- Type Parameters:
I
- the type parameter- Parameters:
id
- the id value- Returns:
- the unique object associated with the id.
- Throws:
ValidationException
- ifid
isnull
.InvalidIdException
- if the id value isnull
, or the type is not compatible.NotIdentifiableException
- if the object has no field marked withId
.
-
getType
Class<T> getType()
Returns the type associated with theObjectRepository
.- Returns:
- type of the object.
-
getDocumentCollection
NitriteCollection getDocumentCollection()
Returns the underlyingNitriteCollection
instance.- Returns:
- the underlying document collection.
-
-