Unique Index
Unique constraints can be defined for an attribute or attributes of a space class. This will ensure that only one instance of the space class exists in the space with the specific attribute value. These indexes are applicable to all types of index; Equal, Ordered, Equal-and-Ordered, Compound, and Collection indexes.
The uniqueness is enforced per partition and not over the whole cluster.
Operation
When the system encounters a unique constraint violation in one of the index-changing api calls (write/update/change) a UniqueConstraintViolationException is thrown.
The operation which caused the violation is rolled back with the following effects:
Operation | Action |
---|---|
write | the entry is removed |
update | the original value is restored |
change | the original value is restored |
If the operation(write or update) is performed under a transaction, the unique value check is done when the operation is performed (eager mode) and not when the transaction is committed.
API
A unique attribute is added to the @SpaceIndex
annotation. Unique = true will designate a unique constraint.
Example:
@SpaceClass
public class Person
{
@SpaceIndex(type=SpaceIndexType.EQUAL, unique = true)
private String lastName;
@SpaceIndex(type=SpaceIndexType.EQUAL)
private String firstName;
@SpaceIndex(type=SpaceIndexType.ORDERED)
private Integer age;
.
.
<gigaspaces-mapping>
<class name="com.gigaspaces.examples.Person" persist="false" replicate="false" fifo="false" >
<property name="lastName">
<index type="EQUAL" unique="true"/>
</property>
<property name="firstName">
<index type="EQUAL"/>
</property>
<property name="age">
<index type="ORDERED"/>
</property>
</class>
</gigaspaces-mapping>
Limitations
- Supported only for ALL_IN_CACHE cache policy, not supported for LRU Last Recently Used. This is a common caching strategy. It defines the policy to evict elements from the cache to make room for new elements when the cache is full, meaning it discards the least recently used items first. and other evictable cache policies
- Not supported for local-cache/local-view since its only per-partition enforcement
- Currently not supported for dynamic (on-the-fly) indexes.