Search in sources :

Example 26 with IndexDescriptor

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

the class IntegrityValidator method validateSchemaRule.

void validateSchemaRule(SchemaRule schemaRule) throws TransactionFailureException {
    Preconditions.checkState(indexValidator != null, "No index validator installed");
    KernelVersion currentVersion = neoStores.getMetaDataStore().kernelVersion();
    if (currentVersion.isLessThan(VERSION_IN_WHICH_TOKEN_INDEXES_ARE_INTRODUCED)) {
        if (schemaRule instanceof IndexDescriptor) {
            IndexDescriptor index = (IndexDescriptor) schemaRule;
            if (index.isTokenIndex() || isBtreeRelationshipPropertyIndex(index)) {
                throw new TransactionFailureException(Status.General.UpgradeRequired, "Index operation on index '%s' not allowed. " + "Required kernel version for this transaction is %s, but actual version was %s.", index, VERSION_IN_WHICH_TOKEN_INDEXES_ARE_INTRODUCED.name(), currentVersion.name());
            }
        }
    }
    if (schemaRule instanceof ConstraintDescriptor) {
        ConstraintDescriptor constraint = (ConstraintDescriptor) schemaRule;
        if (constraint.isIndexBackedConstraint()) {
            long ownedIndex = constraint.asIndexBackedConstraint().ownedIndexId();
            try {
                indexValidator.validateIndex(ownedIndex);
            } catch (KernelException e) {
                // and have recovery performed. It's the safest bet to avoid loosing data.
                throw new TransactionFailureException(Status.Transaction.TransactionValidationFailed, e, "Index validation of " + schemaRule + " failed, specifically for its owned index " + ownedIndex, e);
            }
        }
    }
}
Also used : KernelVersion(org.neo4j.kernel.KernelVersion) ConstraintViolationTransactionFailureException(org.neo4j.internal.kernel.api.exceptions.ConstraintViolationTransactionFailureException) TransactionFailureException(org.neo4j.internal.kernel.api.exceptions.TransactionFailureException) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) KernelException(org.neo4j.exceptions.KernelException)

Example 27 with IndexDescriptor

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

the class FulltextIndexConsistencyCheckIT method mustDiscoverNodeInStoreMissingFromIndex.

@Test
void mustDiscoverNodeInStoreMissingFromIndex() throws Exception {
    GraphDatabaseService db = createDatabase();
    try (Transaction tx = db.beginTx()) {
        tx.execute(format(NODE_CREATE, "nodes", asStrList("Label"), asStrList("prop"))).close();
        tx.commit();
    }
    IndexDescriptor indexDescriptor;
    long nodeId;
    try (Transaction tx = db.beginTx()) {
        tx.schema().awaitIndexesOnline(2, TimeUnit.MINUTES);
        indexDescriptor = getFulltextIndexDescriptor(tx.schema().getIndexes());
        Node node = tx.createNode(Label.label("Label"));
        node.setProperty("prop", "value");
        nodeId = node.getId();
        tx.commit();
    }
    IndexingService indexes = getIndexingService(db);
    IndexProxy indexProxy = indexes.getIndexProxy(indexDescriptor);
    try (IndexUpdater updater = indexProxy.newUpdater(IndexUpdateMode.ONLINE, NULL)) {
        updater.process(IndexEntryUpdate.remove(nodeId, indexDescriptor, Values.stringValue("value")));
    }
    managementService.shutdown();
    ConsistencyCheckService.Result result = checkConsistency();
    assertFalse(result.isSuccessful());
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) Node(org.neo4j.graphdb.Node) IndexProxy(org.neo4j.kernel.impl.api.index.IndexProxy) ConsistencyCheckService(org.neo4j.consistency.ConsistencyCheckService) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) IndexUpdater(org.neo4j.kernel.api.index.IndexUpdater) Test(org.junit.jupiter.api.Test)

Example 28 with IndexDescriptor

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

the class StartOldDbOnCurrentVersionAndCreateFusionIndexIT method verifyExpectedProvider.

private static void verifyExpectedProvider(GraphDatabaseAPI db, Label label, IndexProviderDescriptor expectedDescriptor) throws TransactionFailureException {
    try (InternalTransaction tx = (InternalTransaction) db.beginTx();
        KernelTransaction kernelTransaction = tx.kernelTransaction()) {
        TokenRead tokenRead = kernelTransaction.tokenRead();
        SchemaRead schemaRead = kernelTransaction.schemaRead();
        int labelId = tokenRead.nodeLabel(label.name());
        int key1Id = tokenRead.propertyKey(KEY1);
        int key2Id = tokenRead.propertyKey(KEY2);
        IndexDescriptor index = single(schemaRead.index(SchemaDescriptor.forLabel(labelId, key1Id)));
        assertIndexHasExpectedProvider(expectedDescriptor, index);
        index = single(schemaRead.index(SchemaDescriptor.forLabel(labelId, key1Id, key2Id)));
        assertIndexHasExpectedProvider(expectedDescriptor, index);
        tx.commit();
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) TokenRead(org.neo4j.internal.kernel.api.TokenRead)

Example 29 with IndexDescriptor

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

the class ConstraintIndexConcurrencyTest method shouldNotAllowConcurrentViolationOfConstraint.

@Test
void shouldNotAllowConcurrentViolationOfConstraint() throws Exception {
    // Given
    Label label = label("Foo");
    String propertyKey = "bar";
    String conflictingValue = "baz";
    String constraintName = "MyConstraint";
    // a constraint
    try (Transaction tx = db.beginTx()) {
        tx.schema().constraintFor(label).assertPropertyIsUnique(propertyKey).withName(constraintName).create();
        tx.commit();
    }
    // When
    try (Transaction tx = db.beginTx()) {
        KernelTransaction ktx = ((InternalTransaction) tx).kernelTransaction();
        int labelId = ktx.tokenRead().nodeLabel(label.name());
        int propertyKeyId = ktx.tokenRead().propertyKey(propertyKey);
        Read read = ktx.dataRead();
        try (NodeValueIndexCursor cursor = ktx.cursors().allocateNodeValueIndexCursor(ktx.cursorContext(), ktx.memoryTracker())) {
            IndexDescriptor index = ktx.schemaRead().indexGetForName(constraintName);
            IndexReadSession indexSession = ktx.dataRead().indexReadSession(index);
            read.nodeIndexSeek(indexSession, cursor, unconstrained(), PropertyIndexQuery.exact(propertyKeyId, "The value is irrelevant, we just want to perform some sort of lookup against this " + "index"));
        }
        // then let another thread come in and create a node
        threads.execute(db -> {
            try (Transaction transaction = db.beginTx()) {
                transaction.createNode(label).setProperty(propertyKey, conflictingValue);
                transaction.commit();
            }
            return null;
        }, db).get();
        // before we create a node with the same property ourselves - using the same statement that we have
        // already used for lookup against that very same index
        long node = ktx.dataWrite().nodeCreate();
        ktx.dataWrite().nodeAddLabel(node, labelId);
        var e = assertThrows(UniquePropertyValueValidationException.class, () -> ktx.dataWrite().nodeSetProperty(node, propertyKeyId, Values.of(conflictingValue)));
        assertEquals(ConstraintDescriptorFactory.uniqueForLabel(labelId, propertyKeyId), e.constraint());
        IndexEntryConflictException conflict = Iterators.single(e.conflicts().iterator());
        assertEquals(Values.stringValue(conflictingValue), conflict.getSinglePropertyValue());
        tx.commit();
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Label(org.neo4j.graphdb.Label) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) ConstraintDescriptorFactory(org.neo4j.internal.schema.constraints.ConstraintDescriptorFactory) Values(org.neo4j.values.storable.Values) ImpermanentDbmsExtension(org.neo4j.test.extension.ImpermanentDbmsExtension) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Inject(org.neo4j.test.extension.Inject) ThreadingExtension(org.neo4j.test.rule.concurrent.ThreadingExtension) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Transaction(org.neo4j.graphdb.Transaction) NodeValueIndexCursor(org.neo4j.internal.kernel.api.NodeValueIndexCursor) Read(org.neo4j.internal.kernel.api.Read) Iterators(org.neo4j.internal.helpers.collection.Iterators) Label.label(org.neo4j.graphdb.Label.label) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Test(org.junit.jupiter.api.Test) IndexQueryConstraints.unconstrained(org.neo4j.internal.kernel.api.IndexQueryConstraints.unconstrained) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) UniquePropertyValueValidationException(org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException) ThreadingRule(org.neo4j.test.rule.concurrent.ThreadingRule) PropertyIndexQuery(org.neo4j.internal.kernel.api.PropertyIndexQuery) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NodeValueIndexCursor(org.neo4j.internal.kernel.api.NodeValueIndexCursor) Label(org.neo4j.graphdb.Label) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession) Read(org.neo4j.internal.kernel.api.Read) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) IndexEntryConflictException(org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException) Test(org.junit.jupiter.api.Test)

Example 30 with IndexDescriptor

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

the class IndexConfigMigrationIT method getIndexConfig.

private static Map<String, Value> getIndexConfig(GraphDatabaseAPI db, Transaction tx, String indexName) throws IndexNotFoundKernelException {
    IndexingService indexingService = getIndexingService(db);
    IndexDescriptor indexReference = schemaRead(tx).indexGetForName(indexName);
    IndexProxy indexProxy = indexingService.getIndexProxy(indexReference);
    return indexProxy.indexConfig();
}
Also used : IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) IndexProxy(org.neo4j.kernel.impl.api.index.IndexProxy) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor)

Aggregations

IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)404 Test (org.junit.jupiter.api.Test)231 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)81 Value (org.neo4j.values.storable.Value)43 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)33 ArrayList (java.util.ArrayList)31 Transaction (org.neo4j.graphdb.Transaction)31 SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)31 IndexPrototype (org.neo4j.internal.schema.IndexPrototype)30 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)30 TokenRead (org.neo4j.internal.kernel.api.TokenRead)29 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)26 SchemaRead (org.neo4j.internal.kernel.api.SchemaRead)26 IndexUpdater (org.neo4j.kernel.api.index.IndexUpdater)25 IndexProxy (org.neo4j.kernel.impl.api.index.IndexProxy)25 IndexReadSession (org.neo4j.internal.kernel.api.IndexReadSession)23 IndexNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException)23 LabelSchemaDescriptor (org.neo4j.internal.schema.LabelSchemaDescriptor)23 IndexProviderDescriptor (org.neo4j.internal.schema.IndexProviderDescriptor)22 KernelException (org.neo4j.exceptions.KernelException)20