Search in sources :

Example 41 with TokenRead

use of org.neo4j.internal.kernel.api.TokenRead in project neo4j by neo4j.

the class IndexCRUDIT method addingANodeWithPropertyShouldGetIndexed.

@Test
void addingANodeWithPropertyShouldGetIndexed() throws Exception {
    // Given
    String indexProperty = "indexProperty";
    GatheringIndexWriter writer = newWriter();
    createIndex(db, myLabel, indexProperty);
    // When
    int value1 = 12;
    String otherProperty = "otherProperty";
    int otherValue = 17;
    Node node = createNode(map(indexProperty, value1, otherProperty, otherValue), myLabel);
    // Then, for now, this should trigger two NodePropertyUpdates
    try (Transaction tx = db.beginTx()) {
        KernelTransaction ktx = ((InternalTransaction) tx).kernelTransaction();
        TokenRead tokenRead = ktx.tokenRead();
        int propertyKey1 = tokenRead.propertyKey(indexProperty);
        int label = tokenRead.nodeLabel(myLabel.name());
        LabelSchemaDescriptor descriptor = SchemaDescriptor.forLabel(label, propertyKey1);
        assertThat(writer.updatesCommitted).isEqualTo(asSet(IndexEntryUpdate.add(node.getId(), descriptor, Values.of(value1))));
        tx.commit();
    }
// We get two updates because we both add a label and a property to be indexed
// in the same transaction, in the future, we should optimize this down to
// one NodePropertyUpdate.
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Node(org.neo4j.graphdb.Node) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) TokenRead(org.neo4j.internal.kernel.api.TokenRead) Test(org.junit.jupiter.api.Test)

Example 42 with TokenRead

use of org.neo4j.internal.kernel.api.TokenRead in project neo4j by neo4j.

the class UniquenessConstraintValidationIT method addingUniqueNodeWithUnrelatedValueShouldNotAffectLookup.

@Test
void addingUniqueNodeWithUnrelatedValueShouldNotAffectLookup() throws Exception {
    // given
    ConstraintDescriptor constraint = createConstraint("Person", "id");
    long ourNode;
    {
        KernelTransaction transaction = newTransaction(AnonymousContext.writeToken());
        ourNode = createLabeledNode(transaction, "Person", "id", 1);
        commit();
    }
    KernelTransaction transaction = newTransaction(AnonymousContext.writeToken());
    TokenRead tokenRead = transaction.tokenRead();
    int propId = tokenRead.propertyKey("id");
    IndexDescriptor idx = transaction.schemaRead().indexGetForName(constraint.getName());
    // when
    createLabeledNode(transaction, "Person", "id", 2);
    // then I should find the original node
    try (NodeValueIndexCursor cursor = transaction.cursors().allocateNodeValueIndexCursor(transaction.cursorContext(), transaction.memoryTracker())) {
        assertThat(transaction.dataRead().lockingNodeUniqueIndexSeek(idx, cursor, exact(propId, Values.of(1)))).isEqualTo(ourNode);
    }
    commit();
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NodeValueIndexCursor(org.neo4j.internal.kernel.api.NodeValueIndexCursor) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) TokenRead(org.neo4j.internal.kernel.api.TokenRead) Test(org.junit.jupiter.api.Test)

Example 43 with TokenRead

use of org.neo4j.internal.kernel.api.TokenRead in project neo4j by neo4j.

the class UniquenessConstraintValidationIT method unrelatedNodesWithSamePropertyShouldNotInterfereWithUniquenessCheck.

@Test
void unrelatedNodesWithSamePropertyShouldNotInterfereWithUniquenessCheck() throws Exception {
    // given
    ConstraintDescriptor constraint = createConstraint("Person", "id");
    long ourNode;
    {
        KernelTransaction transaction = newTransaction(AnonymousContext.writeToken());
        ourNode = createLabeledNode(transaction, "Person", "id", 1);
        createLabeledNode(transaction, "Item", "id", 2);
        commit();
    }
    KernelTransaction transaction = newTransaction(AnonymousContext.writeToken());
    TokenRead tokenRead = transaction.tokenRead();
    int propId = tokenRead.propertyKey("id");
    IndexDescriptor idx = transaction.schemaRead().indexGetForName(constraint.getName());
    // when
    createLabeledNode(transaction, "Item", "id", 2);
    // then I should find the original node
    try (NodeValueIndexCursor cursor = transaction.cursors().allocateNodeValueIndexCursor(transaction.cursorContext(), transaction.memoryTracker())) {
        assertThat(transaction.dataRead().lockingNodeUniqueIndexSeek(idx, cursor, exact(propId, Values.of(1)))).isEqualTo(ourNode);
    }
    commit();
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NodeValueIndexCursor(org.neo4j.internal.kernel.api.NodeValueIndexCursor) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) TokenRead(org.neo4j.internal.kernel.api.TokenRead) Test(org.junit.jupiter.api.Test)

Example 44 with TokenRead

use of org.neo4j.internal.kernel.api.TokenRead in project neo4j by neo4j.

the class SchemaStatementProcedureTest method schemaStatementsMustIncludeOnlineIndexes.

@Test
void schemaStatementsMustIncludeOnlineIndexes() throws IndexNotFoundKernelException, ProcedureException {
    IndexDescriptor index = someIndex();
    InternalIndexState indexState = InternalIndexState.ONLINE;
    SchemaReadCore schemaReadCore = getSchemaReadCore(index, indexState);
    TokenRead tokenRead = mock(TokenRead.class);
    Collection<BuiltInProcedures.SchemaStatementResult> result = createSchemaStatementResults(schemaReadCore, tokenRead);
    assertEquals(1, result.size());
}
Also used : InternalIndexState(org.neo4j.internal.kernel.api.InternalIndexState) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) SchemaReadCore(org.neo4j.internal.kernel.api.SchemaReadCore) TokenRead(org.neo4j.internal.kernel.api.TokenRead) Test(org.junit.jupiter.api.Test)

Example 45 with TokenRead

use of org.neo4j.internal.kernel.api.TokenRead in project neo4j by neo4j.

the class SchemaStatementProcedureTest method schemaStatementsShouldHandleConstraintWithBackticks.

@Test
void schemaStatementsShouldHandleConstraintWithBackticks() throws IndexNotFoundKernelException, ProcedureException, LabelNotFoundKernelException, PropertyKeyIdNotFoundKernelException {
    ConstraintDescriptor constraint = ConstraintDescriptorFactory.existsForSchema(forLabel(1, 1)).withName(NAME_WITH_BACKTICKS);
    SchemaReadCore schemaReadCore = getSchemaReadCore(constraint);
    TokenRead tokenRead = mock(TokenRead.class);
    when(tokenRead.nodeLabelName(1)).thenReturn(LABEL_WITH_BACKTICKS);
    when(tokenRead.propertyKeyName(1)).thenReturn(PROPERTY_KEY_WITH_BACKTICKS);
    Collection<BuiltInProcedures.SchemaStatementResult> result = createSchemaStatementResults(schemaReadCore, tokenRead);
    Iterator<BuiltInProcedures.SchemaStatementResult> iter = result.iterator();
    assertTrue(iter.hasNext());
    BuiltInProcedures.SchemaStatementResult next = iter.next();
    assertEquals(NAME_WITH_BACKTICKS, next.name);
    assertEquals(format("CREATE CONSTRAINT %s ON (a:%s) ASSERT (a.%s) IS NOT NULL", ESCAPED_NAME_WITH_BACKTICKS, ESCAPED_LABEL_WITH_BACKTICKS, ESCAPED_PROPERTY_KEY_WITH_BACKTICKS), next.createStatement);
    assertEquals(format("DROP CONSTRAINT %s", ESCAPED_NAME_WITH_BACKTICKS), next.dropStatement);
    assertEquals(NAME_WITH_BACKTICKS, next.name);
    assertFalse(iter.hasNext());
}
Also used : ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) SchemaReadCore(org.neo4j.internal.kernel.api.SchemaReadCore) TokenRead(org.neo4j.internal.kernel.api.TokenRead) Test(org.junit.jupiter.api.Test)

Aggregations

TokenRead (org.neo4j.internal.kernel.api.TokenRead)59 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)42 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)27 Test (org.junit.jupiter.api.Test)19 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)18 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)13 Transaction (org.neo4j.graphdb.Transaction)13 SchemaReadCore (org.neo4j.internal.kernel.api.SchemaReadCore)13 Label (org.neo4j.graphdb.Label)12 SchemaRead (org.neo4j.internal.kernel.api.SchemaRead)11 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)11 InternalIndexState (org.neo4j.internal.kernel.api.InternalIndexState)10 RelationshipType (org.neo4j.graphdb.RelationshipType)7 PropertyKeyIdNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException)6 Arrays (java.util.Arrays)5 List (java.util.List)5 Map (java.util.Map)5 Collectors (java.util.stream.Collectors)5 Stream (java.util.stream.Stream)5