use of jetbrains.exodus.entitystore.tables.PropertyTypes in project xodus by JetBrains.
the class UniqueKeyIndicesEngine method insertUniqueKey.
public void insertUniqueKey(@NotNull final PersistentStoreTransaction txn, @NotNull final Index index, @NotNull final List<Comparable> propValues, @NotNull final Entity entity) {
final PropertyTypes propertyTypes = persistentStore.getPropertyTypes();
final int propCount = index.getFields().size();
if (propCount != propValues.size()) {
throw new IllegalArgumentException("Number of fields differs from the number of property values");
}
final Store indexTable = getUniqueKeyIndex(txn, index);
if (!indexTable.add(txn.getEnvironmentTransaction(), propertyTypes.dataArrayToEntry(propValues.toArray(new Comparable[propCount])), LongBinding.longToCompressedEntry(entity.getId().getLocalId()))) {
throw new InsertConstraintException("Failed to insert unique key (already exists). Index: " + index);
}
}
use of jetbrains.exodus.entitystore.tables.PropertyTypes in project xodus by JetBrains.
the class UniqueKeyIndicesEngine method createUniqueKeyIndex.
private void createUniqueKeyIndex(@NotNull final PersistentStoreTransaction txn, @NotNull final PersistentStoreTransaction snapshot, @NotNull final Index index) {
if (logger.isDebugEnabled()) {
logger.debug("Create index [" + index + ']');
}
final Environment environment = persistentStore.getEnvironment();
final PersistentEntityStoreConfig config = persistentStore.getConfig();
final PropertyTypes propertyTypes = persistentStore.getPropertyTypes();
final List<IndexField> fields = index.getFields();
final int propCount = fields.size();
if (propCount == 0) {
throw new EntityStoreException("Can't create unique key index on empty list of keys.");
}
SingleColumnTable indexTable = null;
Comparable[] props = new Comparable[propCount];
for (final String entityType : getEntityTypesToIndex(index)) {
int i = 0;
for (final Entity entity : snapshot.getAll(entityType)) {
for (int j = 0; j < propCount; ++j) {
final IndexField field = fields.get(j);
if (field.isProperty()) {
if ((props[j] = persistentStore.getProperty(txn, (PersistentEntity) entity, field.getName())) == null) {
throw new EntityStoreException("Can't create unique key index with null property value: " + entityType + '.' + field.getName());
}
} else {
if ((props[j] = entity.getLink(field.getName())) == null) {
throw new EntityStoreException("Can't create unique key index with null link: " + entityType + '.' + field.getName());
}
}
}
if (indexTable == null) {
final String uniqueKeyIndexName = getUniqueKeyIndexName(index);
indexTable = new SingleColumnTable(txn, uniqueKeyIndexName, environment.storeExists(uniqueKeyIndexName, txn.getEnvironmentTransaction()) ? StoreConfig.USE_EXISTING : StoreConfig.WITHOUT_DUPLICATES_WITH_PREFIXING);
}
ArrayByteIterable propsEntry = propertyTypes.dataArrayToEntry(props);
if (!indexTable.getDatabase().add(txn.getEnvironmentTransaction(), propsEntry, LongBinding.longToCompressedEntry(entity.getId().getLocalId()))) {
ByteIterable oldEntityIdEntry = indexTable.getDatabase().get(txn.getEnvironmentTransaction(), propsEntry);
long oldEntityId = LongBinding.compressedEntryToLong(oldEntityIdEntry);
throw new EntityStoreException("Failed to insert unique key (already exists), index: " + index + ", values = " + Arrays.toString(props) + ", new entity = " + entity + ", old entity id = " + oldEntityId + ", index owner entity type = " + index.getOwnerEntityType());
}
if (++i % 100 == 0) {
txn.flush();
}
}
txn.flush();
}
}
Aggregations