Search in sources :

Example 6 with PropertyTypes

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);
    }
}
Also used : PropertyTypes(jetbrains.exodus.entitystore.tables.PropertyTypes) Store(jetbrains.exodus.env.Store)

Example 7 with PropertyTypes

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();
    }
}
Also used : PropertyTypes(jetbrains.exodus.entitystore.tables.PropertyTypes) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) ByteIterable(jetbrains.exodus.ByteIterable) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) Environment(jetbrains.exodus.env.Environment) SingleColumnTable(jetbrains.exodus.entitystore.tables.SingleColumnTable) IndexField(jetbrains.exodus.query.metadata.IndexField)

Aggregations

PropertyTypes (jetbrains.exodus.entitystore.tables.PropertyTypes)7 ComparablePair (jetbrains.exodus.entitystore.custom.ComparablePair)4 ComparablePairBinding (jetbrains.exodus.entitystore.custom.ComparablePairBinding)4 ArrayByteIterable (jetbrains.exodus.ArrayByteIterable)1 ByteIterable (jetbrains.exodus.ByteIterable)1 TestFor (jetbrains.exodus.TestFor)1 SingleColumnTable (jetbrains.exodus.entitystore.tables.SingleColumnTable)1 Environment (jetbrains.exodus.env.Environment)1 Store (jetbrains.exodus.env.Store)1 IndexField (jetbrains.exodus.query.metadata.IndexField)1