Search in sources :

Example 81 with NewIndexDescriptor

use of org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor in project neo4j by neo4j.

the class UniquenessConstraintValidationIT method addingUniqueNodeWithUnrelatedValueShouldNotAffectLookup.

@Test
public void addingUniqueNodeWithUnrelatedValueShouldNotAffectLookup() throws Exception {
    // given
    createConstraint("Person", "id");
    long ourNode;
    {
        Statement statement = statementInNewTransaction(AnonymousContext.writeToken());
        ourNode = createLabeledNode(statement, "Person", "id", 1);
        commit();
    }
    Statement statement = statementInNewTransaction(AnonymousContext.writeToken());
    ReadOperations readOps = statement.readOperations();
    int person = readOps.labelGetForName("Person");
    int propId = readOps.propertyKeyGetForName("id");
    NewIndexDescriptor idx = readOps.uniqueIndexGetForLabelAndPropertyKey(new NodePropertyDescriptor(person, propId));
    // when
    createLabeledNode(statement, "Person", "id", 2);
    // then I should find the original node
    assertThat(readOps.nodeGetFromUniqueIndexSeek(idx, exact(propId, 1)), equalTo(ourNode));
}
Also used : ReadOperations(org.neo4j.kernel.api.ReadOperations) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) NodePropertyDescriptor(org.neo4j.kernel.api.schema.NodePropertyDescriptor) Statement(org.neo4j.kernel.api.Statement) Test(org.junit.Test)

Example 82 with NewIndexDescriptor

use of org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor in project neo4j by neo4j.

the class UniquenessConstraintValidationIT method unrelatedNodesWithSamePropertyShouldNotInterfereWithUniquenessCheck.

@Test
public void unrelatedNodesWithSamePropertyShouldNotInterfereWithUniquenessCheck() throws Exception {
    // given
    createConstraint("Person", "id");
    long ourNode;
    {
        Statement statement = statementInNewTransaction(AnonymousContext.writeToken());
        ourNode = createLabeledNode(statement, "Person", "id", 1);
        createLabeledNode(statement, "Item", "id", 2);
        commit();
    }
    Statement statement = statementInNewTransaction(AnonymousContext.writeToken());
    ReadOperations readOps = statement.readOperations();
    int person = readOps.labelGetForName("Person");
    int propId = readOps.propertyKeyGetForName("id");
    NewIndexDescriptor idx = readOps.uniqueIndexGetForLabelAndPropertyKey(new NodePropertyDescriptor(person, propId));
    // when
    createLabeledNode(statement, "Item", "id", 2);
    // then I should find the original node
    assertThat(readOps.nodeGetFromUniqueIndexSeek(idx, exact(propId, 1)), equalTo(ourNode));
}
Also used : ReadOperations(org.neo4j.kernel.api.ReadOperations) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) NodePropertyDescriptor(org.neo4j.kernel.api.schema.NodePropertyDescriptor) Statement(org.neo4j.kernel.api.Statement) Test(org.junit.Test)

Example 83 with NewIndexDescriptor

use of org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor in project neo4j by neo4j.

the class KernelIT method schemaStateShouldBeEvictedOnIndexDropped.

@Test
public void schemaStateShouldBeEvictedOnIndexDropped() throws Exception {
    // GIVEN
    NewIndexDescriptor idx = createIndex(statementInNewTransaction(SecurityContext.AUTH_DISABLED));
    commit();
    try (Transaction tx = db.beginTx()) {
        db.schema().awaitIndexOnline(db.schema().getIndexes().iterator().next(), 20, SECONDS);
        getOrCreateSchemaState("my key", "some state");
        tx.success();
    }
    // WHEN
    schemaWriteOperationsInNewTransaction().indexDrop(idx);
    commit();
    // THEN
    assertFalse(schemaStateContains("my key"));
}
Also used : NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Test(org.junit.Test)

Example 84 with NewIndexDescriptor

use of org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor in project neo4j by neo4j.

the class NodeGetUniqueFromIndexSeekIT method shouldCompositeFindMatchingNode.

@Test
public void shouldCompositeFindMatchingNode() throws Exception {
    // given
    NewIndexDescriptor index = createUniquenessConstraint(labelId, propertyId1, propertyId2);
    String value1 = "value1";
    String value2 = "value2";
    long nodeId = createNodeWithValues(value1, value2);
    // when looking for it
    ReadOperations readOperations = readOperationsInNewTransaction();
    long foundId = readOperations.nodeGetFromUniqueIndexSeek(index, exact(propertyId1, value1), exact(propertyId2, value2));
    commit();
    // then
    assertTrue("Created node was not found", nodeId == foundId);
}
Also used : ReadOperations(org.neo4j.kernel.api.ReadOperations) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) Test(org.junit.Test)

Example 85 with NewIndexDescriptor

use of org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor in project neo4j by neo4j.

the class NodeGetUniqueFromIndexSeekIT method shouldBlockUniqueIndexSeekFromCompetingTransaction.

@Test(timeout = 1000)
public void shouldBlockUniqueIndexSeekFromCompetingTransaction() throws Exception {
    // This is the interleaving that we are trying to verify works correctly:
    // ----------------------------------------------------------------------
    // Thread1 (main)        : Thread2
    // create unique node    :
    // lookup(node)          :
    // open start latch ----->
    //    |                  : lookup(node)
    // wait for T2 to block  :      |
    //                       :    *block*
    // commit --------------->   *unblock*
    // wait for T2 end latch :      |
    //                       : finish transaction
    //                       : open end latch
    // *unblock* <-------------‘
    // assert that we complete before timeout
    final DoubleLatch latch = new DoubleLatch();
    final NewIndexDescriptor index = createUniquenessConstraint(labelId, propertyId1);
    final String value = "value";
    DataWriteOperations dataStatement = dataWriteOperationsInNewTransaction();
    long nodeId = dataStatement.nodeCreate();
    dataStatement.nodeAddLabel(nodeId, labelId);
    // This adds the node to the unique index and should take an index write lock
    dataStatement.nodeSetProperty(nodeId, Property.stringProperty(propertyId1, value));
    Runnable runnableForThread2 = () -> {
        latch.waitForAllToStart();
        try (Transaction tx = db.beginTx()) {
            try (Statement statement1 = statementContextSupplier.get()) {
                statement1.readOperations().nodeGetFromUniqueIndexSeek(index, exact(propertyId1, value));
            }
            tx.success();
        } catch (IndexNotFoundKernelException | IndexNotApplicableKernelException | IndexBrokenKernelException e) {
            throw new RuntimeException(e);
        } finally {
            latch.finish();
        }
    };
    Thread thread2 = new Thread(runnableForThread2, "Transaction Thread 2");
    thread2.start();
    latch.startAndWaitForAllToStart();
    //noinspection UnusedLabel
    spinUntilBlocking: for (; ; ) {
        if (thread2.getState() == Thread.State.TIMED_WAITING || thread2.getState() == Thread.State.WAITING) {
            break;
        }
        Thread.yield();
    }
    commit();
    latch.waitForAllToFinish();
}
Also used : NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) Transaction(org.neo4j.graphdb.Transaction) DataWriteOperations(org.neo4j.kernel.api.DataWriteOperations) Statement(org.neo4j.kernel.api.Statement) DoubleLatch(org.neo4j.test.DoubleLatch) Test(org.junit.Test)

Aggregations

NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)99 Test (org.junit.Test)55 Statement (org.neo4j.kernel.api.Statement)24 ReadOperations (org.neo4j.kernel.api.ReadOperations)17 IndexNotFoundKernelException (org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException)10 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)9 SchemaIndexProvider (org.neo4j.kernel.api.index.SchemaIndexProvider)9 InternalIndexState (org.neo4j.kernel.api.index.InternalIndexState)7 Transaction (org.neo4j.graphdb.Transaction)6 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)5 IndexEntryConflictException (org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException)5 SchemaRuleNotFoundException (org.neo4j.kernel.api.exceptions.schema.SchemaRuleNotFoundException)5 LabelSchemaDescriptor (org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 PrimitiveLongSet (org.neo4j.collection.primitive.PrimitiveLongSet)4 Label (org.neo4j.graphdb.Label)4 NotFoundException (org.neo4j.graphdb.NotFoundException)4 KernelException (org.neo4j.kernel.api.exceptions.KernelException)4 NodePropertyDescriptor (org.neo4j.kernel.api.schema.NodePropertyDescriptor)4