Is there a way to make a Entity property unique when storing using PersistentEntityStore
?
Such than any put
operation with a duplicate property value would translate to a rollback or should not commit. Is this possible?
Is there a way to make a Entity property unique when storing using PersistentEntityStore
?
Such than any put
operation with a duplicate property value would translate to a rollback or should not commit. Is this possible?
There is no specific way to declare such an index. If PersistentEntityStore#executeInTransaction()
or PersistentEntityStore#computeInTransaction()
is used to define a transaction, then one can check if a property is unique directly in the lambda:
entityStore.executeInTransaction(txn -> {
// ...
if (!txn.find("EntityType", "propertyName", propValue).isEmpty()) {
throw new ExodusException("Unique property violation");
}
entity.setProperty("propertyName", propValue);
// ...
});
This way of setting properties can be extracted, for example, to a Kotlin extension function:
fun StoreTransaction.setProperty(entity: Entity, entityType: String, propName: String, propValue: Comparable<*>) {
if(!find(entityType, propName, propValue).isEmpty) {
throw ExodusException("Unique property violation: $propName = $propValue")
}
entity.setProperty(propName, propValue)
}