Interface PersistentCollection<T>
-
- Type Parameters:
T
- the type of data stored in the collection.
- All Superinterfaces:
org.dizitart.no2.common.meta.AttributesAware
,AutoCloseable
,EventAware
- All Known Subinterfaces:
NitriteCollection
,ObjectRepository<T>
public interface PersistentCollection<T> extends EventAware, org.dizitart.no2.common.meta.AttributesAware, AutoCloseable
A persistent collection interface that provides methods to manage and manipulate data in a Nitrite database.- Since:
- 1.0
- Author:
- Anindya Chatterjee.
- See Also:
NitriteCollection
,ObjectRepository
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description void
addProcessor(Processor processor)
Adds a data processor to this collection.void
clear()
Removes all element from the collection.void
close()
Closes thisPersistentCollection
.default void
createIndex(String... fields)
Creates a unique index on thefields
, if not already exists.void
createIndex(IndexOptions indexOptions, String... fields)
Creates an index on thefields
, if not already exists.void
drop()
Drops the collection and all of its indices.void
dropAllIndices()
Drops all indices from the collection.void
dropIndex(String... fields)
Drops the index on thefields
.NitriteStore<?>
getStore()
Returns theNitriteStore
instance for this collection.boolean
hasIndex(String... fields)
Checks if thefields
is already indexed or not.WriteResult
insert(T[] elements)
Inserts elements into this collection.boolean
isDropped()
Returnstrue
if the collection is dropped; otherwise,false
.boolean
isIndexing(String... fields)
Checks if indexing operation is currently ongoing for thefields
.boolean
isOpen()
Returnstrue
if the collection is open; otherwise,false
.Collection<IndexDescriptor>
listIndices()
Gets a set of all indices in the collection.void
rebuildIndex(String... fields)
Rebuilds index on thefields
if it exists.WriteResult
remove(T element)
Deletes theelement
from the collection.long
size()
Returns the size of thePersistentCollection
.default WriteResult
update(T element)
Updates theelement
in the collection.WriteResult
update(T element, boolean insertIfAbsent)
Updateselement
in the collection.-
Methods inherited from interface org.dizitart.no2.common.meta.AttributesAware
getAttributes, setAttributes
-
Methods inherited from interface org.dizitart.no2.collection.events.EventAware
subscribe, unsubscribe
-
-
-
-
Method Detail
-
addProcessor
void addProcessor(Processor processor)
Adds a data processor to this collection.- Parameters:
processor
- the processor
-
createIndex
default void createIndex(String... fields)
Creates a unique index on thefields
, if not already exists.- Parameters:
fields
- the fields to be indexed.- Throws:
IndexingException
- if an index already exists on the field.
-
createIndex
void createIndex(IndexOptions indexOptions, String... fields)
Creates an index on thefields
, if not already exists. IfindexOptions
isnull
, it will use default options.The default indexing option is -
indexOptions.setIndexType(IndexType.Unique);
NOTE:
- _id field is always indexed.
- Full-text indexing is not supported on _id value.
- Indexing on non-comparable value is not supported.
- Parameters:
indexOptions
- index options.fields
- the fields to be indexed.- Throws:
IndexingException
- if an index already exists on the field.- See Also:
IndexOptions
,IndexType
-
rebuildIndex
void rebuildIndex(String... fields)
Rebuilds index on thefields
if it exists.- Parameters:
fields
- the fields to be indexed.- Throws:
IndexingException
- if thefield
is not indexed.
-
listIndices
Collection<IndexDescriptor> listIndices()
Gets a set of all indices in the collection.- Returns:
- a set of all indices.
- See Also:
IndexDescriptor
-
hasIndex
boolean hasIndex(String... fields)
Checks if thefields
is already indexed or not.- Parameters:
fields
- the fields to check.- Returns:
true
if thefield
is indexed; otherwise,false
.
-
isIndexing
boolean isIndexing(String... fields)
Checks if indexing operation is currently ongoing for thefields
.- Parameters:
fields
- the fields to check.- Returns:
true
if indexing is currently running; otherwise,false
.
-
dropIndex
void dropIndex(String... fields)
Drops the index on thefields
.- Parameters:
fields
- the index on thefields
to drop.- Throws:
IndexingException
- if indexing is currently running on thefields
.IndexingException
- if thefields
are not indexed.
-
dropAllIndices
void dropAllIndices()
Drops all indices from the collection.- Throws:
IndexingException
- if indexing is running on any value.
-
insert
WriteResult insert(T[] elements)
Inserts elements into this collection. If the element has an _id field, then the value will be used as an unique key to identify the element in the collection. If the element does not have any _id field, then nitrite will generate a newNitriteId
and will add it to the _id field.If any of the field is already indexed in the collection, 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:
elements
- an array of element for batch insertion.- Returns:
- the result of the write operation.
- Throws:
ValidationException
- if elements is null.InvalidIdException
- if the _id field's value containsnull
.InvalidIdException
- if the _id field's value contains non comparable type, i.e. type that does not implementComparable
.InvalidIdException
- if the _id field contains value which is not of the same java type as of other element's _id field value in the collection.UniqueConstraintException
- if the value of _id field clashes with the _id field of another element in the repository.UniqueConstraintException
- if a value of the element is indexed and it violates the unique constraint in the collection(if any).- See Also:
NitriteId
,WriteResult
-
update
default WriteResult update(T element)
Updates theelement
in the collection. Specifiedelement
must have an id.NOTE: This operations will notify all
CollectionEventListener
instances registered to this collection with change typeEventType.Update
.- Parameters:
element
- the element to update.- Returns:
- the result of the update operation.
- Throws:
ValidationException
- if the element isnull
.NotIdentifiableException
- if the element does not have any id.
-
update
WriteResult update(T element, boolean insertIfAbsent)
Updateselement
in the collection. Specifiedelement
must have an id. If theelement
is not found in the collection, it will be inserted only ifinsertIfAbsent
is set totrue
.NOTE: This operations will notify all
CollectionEventListener
instances registered to this collection with change typeEventType.Update
orEventType.Insert
.- Parameters:
element
- the element to update.insertIfAbsent
- if set totrue
,element
will be inserted if not found.- Returns:
- the result of the update operation.
- Throws:
ValidationException
- if theelement
isnull
.NotIdentifiableException
- if theelement
does not have any id field.
-
remove
WriteResult remove(T element)
Deletes theelement
from the collection. Theelement
must have an id.NOTE: This operations will notify all
CollectionEventListener
instances registered to this collection with change typeEventType.Remove
.- Parameters:
element
- the element- Returns:
- the result of the remove operation.
- Throws:
NotIdentifiableException
- if theelement
does not have any id field.
-
clear
void clear()
Removes all element from the collection.
-
drop
void drop()
Drops the collection and all of its indices.Any further access to a dropped collection would result into an exception.
-
isDropped
boolean isDropped()
Returnstrue
if the collection is dropped; otherwise,false
.- Returns:
- a boolean value indicating if the collection has been dropped or not.
-
isOpen
boolean isOpen()
Returnstrue
if the collection is open; otherwise,false
.- Returns:
- a boolean value indicating if the collection has been closed or not.
-
size
long size()
Returns the size of thePersistentCollection
.- Returns:
- the size.
-
close
void close()
Closes thisPersistentCollection
.- Specified by:
close
in interfaceAutoCloseable
-
getStore
NitriteStore<?> getStore()
Returns theNitriteStore
instance for this collection.- Returns:
- the
NitriteStore
instance.
-
-