Search in sources :

Example 51 with SchemaDescriptor

use of org.neo4j.internal.schema.SchemaDescriptor in project neo4j by neo4j.

the class SchemaRuleException method describe.

public static String describe(SchemaDescriptorSupplier schemaThing) {
    SchemaDescriptor schema = schemaThing.schema();
    String tagType;
    switch(schema.entityType()) {
        case NODE:
            tagType = "label";
            break;
        case RELATIONSHIP:
            tagType = "relationship type";
            break;
        default:
            throw new AssertionError("Unknown entity type: " + schema.entityType());
    }
    if (schemaThing instanceof ConstraintDescriptor) {
        ConstraintDescriptor constraint = (ConstraintDescriptor) schemaThing;
        switch(constraint.type()) {
            case UNIQUE:
                return tagType + " uniqueness constraint";
            case EXISTS:
                return tagType + " property existence constraint";
            case UNIQUE_EXISTS:
                return schema.entityType().name().toLowerCase() + " key constraint";
            default:
                throw new AssertionError("Unknown constraint type: " + constraint.type());
        }
    } else {
        IndexDescriptor index = (IndexDescriptor) schemaThing;
        IndexType indexType = index.getIndexType();
        if (indexType != IndexType.BTREE) {
            String indexTypeName = indexType.name().toLowerCase();
            return indexTypeName + " " + tagType + " index";
        } else {
            return tagType + " index";
        }
    }
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) IndexType(org.neo4j.internal.schema.IndexType)

Example 52 with SchemaDescriptor

use of org.neo4j.internal.schema.SchemaDescriptor in project neo4j by neo4j.

the class Operations method nodeKeyConstraintCreate.

@Override
public ConstraintDescriptor nodeKeyConstraintCreate(IndexPrototype prototype) throws KernelException {
    SchemaDescriptor schema = prototype.schema();
    exclusiveSchemaLock(schema);
    ktx.assertOpen();
    prototype = ensureIndexPrototypeHasIndexProvider(prototype);
    NodeKeyConstraintDescriptor constraint = ConstraintDescriptorFactory.nodeKeyForSchema(schema);
    try {
        // Check data integrity
        assertValidDescriptor(schema, SchemaKernelException.OperationContext.CONSTRAINT_CREATION);
        if (prototype.getName().isEmpty()) {
            constraint = ensureConstraintHasName(constraint);
            prototype = prototype.withName(constraint.getName());
        } else {
            constraint = constraint.withName(prototype.getName().get());
        }
        exclusiveSchemaNameLock(constraint.getName());
        assertNoBlockingSchemaRulesExists(constraint);
    } catch (SchemaKernelException e) {
        exclusiveSchemaUnlock(schema);
        throw e;
    }
    // enforce constraints
    enforceNodeKeyConstraint(schema);
    // create constraint
    indexBackedConstraintCreate(constraint, prototype);
    return constraint;
}
Also used : RelationTypeSchemaDescriptor(org.neo4j.internal.schema.RelationTypeSchemaDescriptor) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) SchemaKernelException(org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException)

Example 53 with SchemaDescriptor

use of org.neo4j.internal.schema.SchemaDescriptor in project neo4j by neo4j.

the class Read method releaseSharedSchemaLock.

<T extends SchemaDescriptorSupplier> void releaseSharedSchemaLock(T schemaLike) {
    SchemaDescriptor schema = schemaLike.schema();
    long[] lockingKeys = schema.lockingKeys();
    ktx.lockClient().releaseShared(schema.keyType(), lockingKeys);
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor)

Example 54 with SchemaDescriptor

use of org.neo4j.internal.schema.SchemaDescriptor in project neo4j by neo4j.

the class FulltextIndexProviderTest method createIndex.

private IndexDescriptor createIndex(int[] entityTokens, int[] propertyIds, String analyzer, EntityType entityType, boolean eventuallyConsistent) throws KernelException {
    IndexDescriptor fulltext;
    try (KernelTransactionImplementation transaction = getKernelTransaction()) {
        SchemaDescriptor schema = SchemaDescriptor.fulltext(entityType, entityTokens, propertyIds);
        IndexConfig config = IndexConfig.with(FulltextIndexSettingsKeys.ANALYZER, Values.stringValue(analyzer)).withIfAbsent(FulltextIndexSettingsKeys.EVENTUALLY_CONSISTENT, Values.of(eventuallyConsistent));
        IndexPrototype prototype = IndexPrototype.forSchema(schema, DESCRIPTOR).withIndexType(IndexType.FULLTEXT).withName(NAME).withIndexConfig(config);
        fulltext = transaction.schemaWrite().indexCreate(prototype);
        transaction.success();
    }
    return fulltext;
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) IndexConfig(org.neo4j.internal.schema.IndexConfig) KernelTransactionImplementation(org.neo4j.kernel.impl.api.KernelTransactionImplementation) IndexPrototype(org.neo4j.internal.schema.IndexPrototype) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor)

Example 55 with SchemaDescriptor

use of org.neo4j.internal.schema.SchemaDescriptor in project neo4j by neo4j.

the class FulltextIndexProviderTest method indexWithUnknownAnalyzerWillBeMarkedAsFailedOnStartup.

@Test
void indexWithUnknownAnalyzerWillBeMarkedAsFailedOnStartup() throws Exception {
    // Create a full-text index.
    long indexId;
    try (KernelTransactionImplementation transaction = getKernelTransaction()) {
        int[] propertyIds = { propIdHa };
        SchemaDescriptor schema = SchemaDescriptor.fulltext(EntityType.NODE, new int[] { labelIdHa }, propertyIds);
        IndexPrototype prototype = IndexPrototype.forSchema(schema).withIndexType(FULLTEXT).withName(NAME);
        SchemaWrite schemaWrite = transaction.schemaWrite();
        IndexDescriptor index = schemaWrite.indexCreate(prototype);
        indexId = index.getId();
        transaction.success();
    }
    // Modify the full-text index such that it has an analyzer configured that does not exist.
    controller.restartDbms(builder -> {
        var cacheTracer = NULL;
        FileSystemAbstraction fs = builder.getFileSystem();
        DatabaseLayout databaseLayout = Neo4jLayout.of(builder.getHomeDirectory()).databaseLayout(DEFAULT_DATABASE_NAME);
        DefaultIdGeneratorFactory idGenFactory = new DefaultIdGeneratorFactory(fs, RecoveryCleanupWorkCollector.ignore(), databaseLayout.getDatabaseName());
        try (JobScheduler scheduler = JobSchedulerFactory.createInitialisedScheduler();
            PageCache pageCache = StandalonePageCacheFactory.createPageCache(fs, scheduler, cacheTracer)) {
            StoreFactory factory = new StoreFactory(databaseLayout, Config.defaults(), idGenFactory, pageCache, fs, NullLogProvider.getInstance(), cacheTracer, writable());
            var cursorContext = CursorContext.NULL;
            try (NeoStores neoStores = factory.openAllNeoStores(false)) {
                TokenHolders tokens = StoreTokens.readOnlyTokenHolders(neoStores, CursorContext.NULL);
                SchemaStore schemaStore = neoStores.getSchemaStore();
                SchemaStorage storage = new SchemaStorage(schemaStore, tokens, () -> KernelVersion.LATEST);
                IndexDescriptor index = (IndexDescriptor) storage.loadSingleSchemaRule(indexId, CursorContext.NULL);
                Map<String, Value> indexConfigMap = new HashMap<>(index.getIndexConfig().asMap());
                for (Map.Entry<String, Value> entry : indexConfigMap.entrySet()) {
                    if (entry.getKey().contains("analyzer")) {
                        // This analyzer does not exist!
                        entry.setValue(Values.stringValue("bla-bla-lyzer"));
                    }
                }
                index = index.withIndexConfig(IndexConfig.with(indexConfigMap));
                storage.writeSchemaRule(index, cursorContext, INSTANCE);
                schemaStore.flush(cursorContext);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return builder;
    });
    // Verify that the index comes up in a failed state.
    try (Transaction tx = db.beginTx()) {
        IndexDefinition index = tx.schema().getIndexByName(NAME);
        Schema.IndexState indexState = tx.schema().getIndexState(index);
        assertThat(indexState).isEqualTo(Schema.IndexState.FAILED);
        String indexFailure = tx.schema().getIndexFailure(index);
        assertThat(indexFailure).contains("bla-bla-lyzer");
    }
    // Verify that the failed index can be dropped.
    try (Transaction tx = db.beginTx()) {
        tx.schema().getIndexByName(NAME).drop();
        assertThrows(IllegalArgumentException.class, () -> tx.schema().getIndexByName(NAME));
        tx.commit();
    }
    try (Transaction tx = db.beginTx()) {
        assertThrows(IllegalArgumentException.class, () -> tx.schema().getIndexByName(NAME));
    }
    controller.restartDbms();
    try (Transaction tx = db.beginTx()) {
        assertThrows(IllegalArgumentException.class, () -> tx.schema().getIndexByName(NAME));
    }
}
Also used : FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) HashMap(java.util.HashMap) Schema(org.neo4j.graphdb.schema.Schema) IndexPrototype(org.neo4j.internal.schema.IndexPrototype) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) StoreFactory(org.neo4j.kernel.impl.store.StoreFactory) SchemaStorage(org.neo4j.internal.recordstorage.SchemaStorage) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) KernelTransactionImplementation(org.neo4j.kernel.impl.api.KernelTransactionImplementation) DatabaseLayout(org.neo4j.io.layout.DatabaseLayout) TokenHolders(org.neo4j.token.TokenHolders) PageCache(org.neo4j.io.pagecache.PageCache) JobScheduler(org.neo4j.scheduler.JobScheduler) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) SchemaWrite(org.neo4j.internal.kernel.api.SchemaWrite) SchemaStore(org.neo4j.kernel.impl.store.SchemaStore) DefaultIdGeneratorFactory(org.neo4j.internal.id.DefaultIdGeneratorFactory) IndexNotApplicableKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotApplicableKernelException) KernelException(org.neo4j.exceptions.KernelException) TransactionFailureException(org.neo4j.internal.kernel.api.exceptions.TransactionFailureException) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NeoStores(org.neo4j.kernel.impl.store.NeoStores) Value(org.neo4j.values.storable.Value) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Aggregations

SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)58 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)27 LabelSchemaDescriptor (org.neo4j.internal.schema.LabelSchemaDescriptor)23 Test (org.junit.jupiter.api.Test)18 IndexPrototype (org.neo4j.internal.schema.IndexPrototype)18 RelationTypeSchemaDescriptor (org.neo4j.internal.schema.RelationTypeSchemaDescriptor)18 Value (org.neo4j.values.storable.Value)11 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)10 ArrayList (java.util.ArrayList)6 KernelTransactionImplementation (org.neo4j.kernel.impl.api.KernelTransactionImplementation)6 IndexNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException)5 IndexConfig (org.neo4j.internal.schema.IndexConfig)5 MemoryTracker (org.neo4j.memory.MemoryTracker)5 KernelException (org.neo4j.exceptions.KernelException)4 UnspecifiedKernelException (org.neo4j.exceptions.UnspecifiedKernelException)4 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)4 IndexNotApplicableKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotApplicableKernelException)4 SchemaKernelException (org.neo4j.internal.kernel.api.exceptions.schema.SchemaKernelException)4 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)4 IndexProviderDescriptor (org.neo4j.internal.schema.IndexProviderDescriptor)4