Search in sources :

Example 11 with ReadOperations

use of org.neo4j.kernel.api.ReadOperations in project neo4j by neo4j.

the class IndexCRUDIT method addingALabelToPreExistingNodeShouldGetIndexed.

@Test
public void addingALabelToPreExistingNodeShouldGetIndexed() throws Exception {
    // GIVEN
    String indexProperty = "indexProperty";
    GatheringIndexWriter writer = newWriter();
    createIndex(db, myLabel, indexProperty);
    // WHEN
    String otherProperty = "otherProperty";
    int value = 12;
    int otherValue = 17;
    Node node = createNode(map(indexProperty, value, otherProperty, otherValue));
    // THEN
    assertThat(writer.updatesCommitted.size(), equalTo(0));
    // AND WHEN
    try (Transaction tx = db.beginTx()) {
        node.addLabel(myLabel);
        tx.success();
    }
    // THEN
    try (Transaction tx = db.beginTx()) {
        ReadOperations readOperations = ctxSupplier.get().readOperations();
        int propertyKey1 = readOperations.propertyKeyGetForName(indexProperty);
        int label = readOperations.labelGetForName(myLabel.name());
        LabelSchemaDescriptor descriptor = SchemaDescriptorFactory.forLabel(label, propertyKey1);
        assertThat(writer.updatesCommitted, equalTo(asSet(IndexEntryUpdate.add(node.getId(), descriptor, value))));
        tx.success();
    }
}
Also used : ReadOperations(org.neo4j.kernel.api.ReadOperations) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) LabelSchemaDescriptor(org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor) Test(org.junit.Test)

Example 12 with ReadOperations

use of org.neo4j.kernel.api.ReadOperations in project neo4j by neo4j.

the class GraphDatabaseFacade method nodesByLabelAndProperty.

private ResourceIterator<Node> nodesByLabelAndProperty(Label myLabel, String key, Object value) {
    Statement statement = spi.currentStatement();
    ReadOperations readOps = statement.readOperations();
    int propertyId = readOps.propertyKeyGetForName(key);
    int labelId = readOps.labelGetForName(myLabel.name());
    if (propertyId == NO_SUCH_PROPERTY_KEY || labelId == NO_SUCH_LABEL) {
        statement.close();
        return emptyIterator();
    }
    NewIndexDescriptor descriptor = findAnyIndexByLabelAndProperty(readOps, propertyId, labelId);
    try {
        if (null != descriptor) {
            // Ha! We found an index - let's use it to find matching nodes
            IndexQuery.ExactPredicate query = IndexQuery.exact(descriptor.schema().getPropertyId(), value);
            return map2nodes(readOps.indexQuery(descriptor, query), statement);
        }
    } catch (KernelException e) {
    // weird at this point but ignore and fallback to a label scan
    }
    return getNodesByLabelAndPropertyWithoutIndex(propertyId, value, statement, labelId);
}
Also used : ReadOperations(org.neo4j.kernel.api.ReadOperations) KeyReadOperations(org.neo4j.kernel.impl.api.operations.KeyReadOperations) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) IndexQuery(org.neo4j.kernel.api.schema_new.IndexQuery) Statement(org.neo4j.kernel.api.Statement) IndexNotFoundKernelException(org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException) InvalidTransactionTypeKernelException(org.neo4j.kernel.api.exceptions.InvalidTransactionTypeKernelException) KernelException(org.neo4j.kernel.api.exceptions.KernelException) SchemaKernelException(org.neo4j.kernel.api.exceptions.schema.SchemaKernelException)

Example 13 with ReadOperations

use of org.neo4j.kernel.api.ReadOperations in project neo4j by neo4j.

the class LabelIT method shouldListAllLabels.

@Test
public void shouldListAllLabels() throws Exception {
    // given
    Statement statement = statementInNewTransaction(AnonymousContext.writeToken());
    int label1Id = statement.tokenWriteOperations().labelGetOrCreateForName("label1");
    int label2Id = statement.tokenWriteOperations().labelGetOrCreateForName("label2");
    // when
    Iterator<Token> labelIdsBeforeCommit = statement.readOperations().labelsGetAllTokens();
    // then
    assertThat(asCollection(labelIdsBeforeCommit), hasItems(new Token("label1", label1Id), new Token("label2", label2Id)));
    // when
    commit();
    ReadOperations readOperations = readOperationsInNewTransaction();
    Iterator<Token> labelIdsAfterCommit = readOperations.labelsGetAllTokens();
    // then
    assertThat(asCollection(labelIdsAfterCommit), hasItems(new Token("label1", label1Id), new Token("label2", label2Id)));
}
Also used : ReadOperations(org.neo4j.kernel.api.ReadOperations) Statement(org.neo4j.kernel.api.Statement) Token(org.neo4j.storageengine.api.Token) Test(org.junit.Test)

Example 14 with ReadOperations

use of org.neo4j.kernel.api.ReadOperations in project neo4j by neo4j.

the class NodeGetUniqueFromIndexSeekIT method shouldNotCompositeFindNonMatchingNode.

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

Example 15 with ReadOperations

use of org.neo4j.kernel.api.ReadOperations in project neo4j by neo4j.

the class PropertyIT method nodeHasNotPropertyIfUnset.

@Test
public void nodeHasNotPropertyIfUnset() throws Exception {
    // GIVEN
    Statement statement = statementInNewTransaction(AnonymousContext.writeToken());
    long nodeId = statement.dataWriteOperations().nodeCreate();
    // WHEN
    int propertyKeyId = statement.tokenWriteOperations().propertyKeyGetOrCreateForName("clown");
    // THEN
    assertThat(statement.readOperations().nodeHasProperty(nodeId, propertyKeyId), is(false));
    assertThat(statement.readOperations().nodeGetProperty(nodeId, propertyKeyId), nullValue());
    // WHEN
    commit();
    ReadOperations readOperations = readOperationsInNewTransaction();
    // THEN
    assertThat(readOperations.nodeHasProperty(nodeId, propertyKeyId), is(false));
    assertThat(readOperations.nodeGetProperty(nodeId, propertyKeyId), nullValue());
}
Also used : ReadOperations(org.neo4j.kernel.api.ReadOperations) Statement(org.neo4j.kernel.api.Statement) Test(org.junit.Test)

Aggregations

ReadOperations (org.neo4j.kernel.api.ReadOperations)73 Test (org.junit.Test)52 Statement (org.neo4j.kernel.api.Statement)37 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)22 SchemaWriteOperations (org.neo4j.kernel.api.SchemaWriteOperations)9 KeyReadOperations (org.neo4j.kernel.impl.api.operations.KeyReadOperations)8 IndexNotFoundKernelException (org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException)7 NotFoundException (org.neo4j.graphdb.NotFoundException)5 Transaction (org.neo4j.graphdb.Transaction)5 HashMap (java.util.HashMap)4 PrimitiveLongSet (org.neo4j.collection.primitive.PrimitiveLongSet)4 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)4 DataWriteOperations (org.neo4j.kernel.api.DataWriteOperations)4 KernelIntegrationTest (org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)4 ArrayList (java.util.ArrayList)3 Set (java.util.Set)3 PrimitiveLongIterator (org.neo4j.collection.primitive.PrimitiveLongIterator)3 Label (org.neo4j.graphdb.Label)3 RelationshipType (org.neo4j.graphdb.RelationshipType)3 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)3