Class UniqueIndex<A,O>
- java.lang.Object
-
- com.googlecode.cqengine.index.support.AbstractAttributeIndex<A,O>
-
- com.googlecode.cqengine.index.unique.UniqueIndex<A,O>
-
- All Implemented Interfaces:
ModificationListener<O>
,AttributeIndex<A,O>
,Index<O>
,OnHeapTypeIndex
public class UniqueIndex<A,O> extends AbstractAttributeIndex<A,O> implements OnHeapTypeIndex
An index backed by aConcurrentHashMap
, which can be more efficient thanHashIndex
when used with (and only with) attributes which uniquely identify objects (primary key-type attributes). This type of index does not store a set of objects matching each attribute value, but instead stores only a single object for each value. This results in faster query performance, and often lower memory usage, but has some trade-offs. This index will throw an exception if a duplicate object is detected for an existing attribute value. That condition means however that inconsistencies might already have arisen between this and other indexes as a result of the application's misuse of this index. Trade-offs:UniqueIndex
versusHashIndex
-
UniqueIndex
will always use less memory than a non-quantizedHashIndex
-
UniqueIndex
will not necessarily use less memory than a quantizedHashIndex
, i.e. configured with aQuantizer
-
In all cases,
UniqueIndex
will answer queries faster than aHashIndex
-
It is important that
UniqueIndex
only be used with attributes which uniquely identify objects -
A
UniqueIndex
on a primary key-type attribute might not be compatible with the MVCC algorithm implemented byTransactionalIndexedCollection
.-
However, as an alternative option to reduce memory overhead in those situations see:
HashIndex.onSemiUniqueAttribute(Attribute)
-
However, as an alternative option to reduce memory overhead in those situations see:
- Author:
- Kinz Liu, Niall Gallagher
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
UniqueIndex.DefaultIndexMapFactory<A,O>
Creates an index map using default settings.static class
UniqueIndex.UniqueConstraintViolatedException
-
Field Summary
Fields Modifier and Type Field Description protected static int
INDEX_RETRIEVAL_COST
protected ConcurrentMap<A,O>
indexMap
protected Factory<ConcurrentMap<A,O>>
indexMapFactory
-
Fields inherited from class com.googlecode.cqengine.index.support.AbstractAttributeIndex
attribute, supportedQueries
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
UniqueIndex(Factory<ConcurrentMap<A,O>> indexMapFactory, Attribute<O,A> attribute)
Package-private constructor, used by static factory methods.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
addAll(ObjectSet<O> objectSet, QueryOptions queryOptions)
Notifies the listener that the specified objects are being added to the collection, and so it can take action and update its internal data structures.void
clear(QueryOptions queryOptions)
Notifies the listener that all objects have been removed from the collection, and so it can take action and update its internal data structures.void
destroy(QueryOptions queryOptions)
This is a no-op for this type of index.Index<O>
getEffectiveIndex()
Returns the effective index, which Persistence objects will use to determine the identity of the index making persistence requests.void
init(ObjectStore<O> objectStore, QueryOptions queryOptions)
Notifies the listener that the given ObjectStore has just been created.boolean
isMutable()
Indicates if objects can be added to or removed from the index after the index has been built.boolean
isQuantized()
Indicates if the index is quantized, using aQuantizer
.static <A,O>
UniqueIndex<A,O>onAttribute(Attribute<O,A> attribute)
Creates a newUniqueIndex
on the specified attribute.static <A,O>
UniqueIndex<A,O>onAttribute(Factory<ConcurrentMap<A,O>> indexMapFactory, Attribute<O,A> attribute)
Creates a newUniqueIndex
on the specified attribute.boolean
removeAll(ObjectSet<O> objectSet, QueryOptions queryOptions)
Notifies the listener that the specified objects are being removed from the collection, and so it can take action and update its internal data structures.ResultSet<O>
retrieve(Query<O> query, QueryOptions queryOptions)
Returns aResultSet
which when iterated will return objects from the index matching the query supplied.protected ResultSet<O>
retrieveEqual(Equal<O,A> equal, QueryOptions queryOptions, ConcurrentMap<A,O> indexMap)
protected ResultSet<O>
retrieveIn(In<O,A> in, QueryOptions queryOptions, ConcurrentMap<A,O> indexMap)
boolean
supportsQuery(Query<O> query, QueryOptions queryOptions)
Indicates if the index can perform retrievals for the type of query supplied.-
Methods inherited from class com.googlecode.cqengine.index.support.AbstractAttributeIndex
equals, getAttribute, hashCode
-
-
-
-
Field Detail
-
INDEX_RETRIEVAL_COST
protected static final int INDEX_RETRIEVAL_COST
- See Also:
- Constant Field Values
-
indexMapFactory
protected final Factory<ConcurrentMap<A,O>> indexMapFactory
-
indexMap
protected final ConcurrentMap<A,O> indexMap
-
-
Constructor Detail
-
UniqueIndex
protected UniqueIndex(Factory<ConcurrentMap<A,O>> indexMapFactory, Attribute<O,A> attribute)
Package-private constructor, used by static factory methods. Creates a new UniqueIndex initialized to index the supplied attribute.- Parameters:
indexMapFactory
- A factory used to create the main map-based data structure used by the indexattribute
- The attribute on which the index will be built
-
-
Method Detail
-
supportsQuery
public boolean supportsQuery(Query<O> query, QueryOptions queryOptions)
Description copied from class:AbstractAttributeIndex
Indicates if the index can perform retrievals for the type of query supplied.- Specified by:
supportsQuery
in interfaceIndex<A>
- Overrides:
supportsQuery
in classAbstractAttributeIndex<A,O>
- Parameters:
query
- A query to checkqueryOptions
- Optional parameters for the query- Returns:
- True if the index can perform retrievals for the type of query supplied, false if it does not support this type of query
-
isMutable
public boolean isMutable()
Indicates if objects can be added to or removed from the index after the index has been built. This index is mutable.
-
isQuantized
public boolean isQuantized()
Description copied from interface:Index
Indicates if the index is quantized, using aQuantizer
.- Specified by:
isQuantized
in interfaceIndex<A>
- Returns:
- True if the index is quantized, false if not.
-
getEffectiveIndex
public Index<O> getEffectiveIndex()
Description copied from interface:Index
Returns the effective index, which Persistence objects will use to determine the identity of the index making persistence requests. Most Index implementations will typically return a reference to themselves ('this'). However in advanced cases when one index delegates to another, the implementation of the wrapping index will create a subclass the delegate index, and override this method in the delegate index so that when the delegate index interacts with the persistence, it will identify itself as the outer or "effective" index.- Specified by:
getEffectiveIndex
in interfaceIndex<A>
- Returns:
- The effective index, in the case that this index is wrapped by another index.
-
retrieve
public ResultSet<O> retrieve(Query<O> query, QueryOptions queryOptions)
Description copied from interface:Index
Returns aResultSet
which when iterated will return objects from the index matching the query supplied. UsuallyResultSet
s are lazy which means that they don't actually do any work, or encapsulate or materialize matching objects, but rather they encapsulate logic to fetch matching objects from the index on-the-fly as the application iterates through theResultSet
.- Specified by:
retrieve
in interfaceIndex<A>
- Parameters:
query
- An object which specifies some restriction on an attribute of an objectqueryOptions
- Optional parameters for the query- Returns:
- A set of objects with attributes matching the restriction imposed by the query
- See Also:
Index.supportsQuery(Query, QueryOptions)
-
retrieveIn
protected ResultSet<O> retrieveIn(In<O,A> in, QueryOptions queryOptions, ConcurrentMap<A,O> indexMap)
-
retrieveEqual
protected ResultSet<O> retrieveEqual(Equal<O,A> equal, QueryOptions queryOptions, ConcurrentMap<A,O> indexMap)
-
addAll
public boolean addAll(ObjectSet<O> objectSet, QueryOptions queryOptions)
Notifies the listener that the specified objects are being added to the collection, and so it can take action and update its internal data structures.- Specified by:
addAll
in interfaceModificationListener<A>
- Parameters:
objectSet
- The objects being addedqueryOptions
- Optional parameters for the update
-
removeAll
public boolean removeAll(ObjectSet<O> objectSet, QueryOptions queryOptions)
Notifies the listener that the specified objects are being removed from the collection, and so it can take action and update its internal data structures.- Specified by:
removeAll
in interfaceModificationListener<A>
- Parameters:
objectSet
- The objects being removedqueryOptions
- Optional parameters for the update
-
init
public void init(ObjectStore<O> objectStore, QueryOptions queryOptions)
Notifies the listener that the given ObjectStore has just been created.- Specified by:
init
in interfaceModificationListener<A>
- Parameters:
objectStore
- The ObjectStore which persists objectsqueryOptions
- Optional parameters for the update
-
destroy
public void destroy(QueryOptions queryOptions)
This is a no-op for this type of index.- Specified by:
destroy
in interfaceModificationListener<A>
- Parameters:
queryOptions
- Optional parameters for the update
-
clear
public void clear(QueryOptions queryOptions)
Notifies the listener that all objects have been removed from the collection, and so it can take action and update its internal data structures.- Specified by:
clear
in interfaceModificationListener<A>
- Parameters:
queryOptions
- Optional parameters for the update
-
onAttribute
public static <A,O> UniqueIndex<A,O> onAttribute(Attribute<O,A> attribute)
Creates a newUniqueIndex
on the specified attribute.- Type Parameters:
O
- The type of the object containing the attribute- Parameters:
attribute
- The attribute on which the index will be built- Returns:
- A
UniqueIndex
on this attribute
-
onAttribute
public static <A,O> UniqueIndex<A,O> onAttribute(Factory<ConcurrentMap<A,O>> indexMapFactory, Attribute<O,A> attribute)
Creates a newUniqueIndex
on the specified attribute.- Type Parameters:
O
- The type of the object containing the attribute- Parameters:
indexMapFactory
- A factory used to create the main map-based data structure used by the indexattribute
- The attribute on which the index will be built- Returns:
- A
UniqueIndex
on this attribute
-
-