Search in sources :

Example 46 with Statement

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

the class PropertyIT method shouldListAllPropertyKeys.

@Test
public void shouldListAllPropertyKeys() throws Exception {
    // given
    dbWithNoCache();
    Statement statement = statementInNewTransaction(AnonymousContext.writeToken());
    int prop1 = statement.tokenWriteOperations().propertyKeyGetOrCreateForName("prop1");
    int prop2 = statement.tokenWriteOperations().propertyKeyGetOrCreateForName("prop2");
    // when
    Iterator<Token> propIdsBeforeCommit = statement.readOperations().propertyKeyGetAllTokens();
    // then
    assertThat(asCollection(propIdsBeforeCommit), hasItems(new Token("prop1", prop1), new Token("prop2", prop2)));
    // when
    commit();
    ReadOperations readOperations = readOperationsInNewTransaction();
    Iterator<Token> propIdsAfterCommit = readOperations.propertyKeyGetAllTokens();
    // then
    assertThat(asCollection(propIdsAfterCommit), hasItems(new Token("prop1", prop1), new Token("prop2", prop2)));
}
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 47 with Statement

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

the class PropertyIT method shouldRemoveSetNodePropertyAcrossTransactions.

@Test
public void shouldRemoveSetNodePropertyAcrossTransactions() throws Exception {
    // GIVEN
    int propertyKeyId;
    long nodeId;
    {
        Statement statement = statementInNewTransaction(AnonymousContext.writeToken());
        nodeId = statement.dataWriteOperations().nodeCreate();
        propertyKeyId = statement.tokenWriteOperations().propertyKeyGetOrCreateForName("clown");
        statement.dataWriteOperations().nodeSetProperty(nodeId, stringProperty(propertyKeyId, "bozo"));
        commit();
    }
    {
        Statement statement = statementInNewTransaction(AnonymousContext.writeToken());
        // WHEN
        Object previous = statement.dataWriteOperations().nodeRemoveProperty(nodeId, propertyKeyId).value();
        // THEN
        assertEquals("bozo", previous);
        assertThat(statement.readOperations().nodeGetProperty(nodeId, propertyKeyId), nullValue());
        // WHEN
        commit();
    }
    // THEN
    ReadOperations readOperations = readOperationsInNewTransaction();
    assertThat(readOperations.nodeGetProperty(nodeId, propertyKeyId), nullValue());
}
Also used : ReadOperations(org.neo4j.kernel.api.ReadOperations) Statement(org.neo4j.kernel.api.Statement) Test(org.junit.Test)

Example 48 with Statement

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

the class KernelIT method transactionStateShouldRemovePreviouslyAddedLabel.

@Test
public void transactionStateShouldRemovePreviouslyAddedLabel() throws Exception {
    Transaction tx = db.beginTx();
    Statement statement = statementContextSupplier.get();
    // WHEN
    Node node = db.createNode();
    int labelId1 = statement.tokenWriteOperations().labelGetOrCreateForName("labello1");
    int labelId2 = statement.tokenWriteOperations().labelGetOrCreateForName("labello2");
    statement.dataWriteOperations().nodeAddLabel(node.getId(), labelId1);
    statement.dataWriteOperations().nodeAddLabel(node.getId(), labelId2);
    statement.dataWriteOperations().nodeRemoveLabel(node.getId(), labelId2);
    statement.close();
    tx.success();
    tx.close();
    // THEN
    tx = db.beginTx();
    statement = statementContextSupplier.get();
    assertEquals(PrimitiveIntCollections.asSet(new int[] { labelId1 }), PrimitiveIntCollections.asSet(statement.readOperations().nodeGetLabels(node.getId())));
    tx.close();
}
Also used : Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Statement(org.neo4j.kernel.api.Statement) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Example 49 with Statement

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

the class KernelIT method txReturnsCorrectIdWhenReadOnly.

@Test
public void txReturnsCorrectIdWhenReadOnly() throws Exception {
    executeDummyTxs(db, 42);
    KernelTransaction tx = kernel.newTransaction(KernelTransaction.Type.implicit, AUTH_DISABLED);
    try (Statement statement = tx.acquireStatement();
        Cursor<NodeItem> cursor = statement.readOperations().nodeCursorById(1)) {
    }
    tx.success();
    assertEquals(KernelTransaction.READ_ONLY, tx.closeTransaction());
    assertFalse(tx.isOpen());
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NodeItem(org.neo4j.storageengine.api.NodeItem) Statement(org.neo4j.kernel.api.Statement) Test(org.junit.Test)

Example 50 with Statement

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

the class KernelIT method shouldNotBeAbleToCommitIfFailedTransactionContext.

@Test
public void shouldNotBeAbleToCommitIfFailedTransactionContext() throws Exception {
    // WHEN
    Node node = null;
    int labelId = -1;
    TransactionFailureException expectedException = null;
    try (Transaction transaction = db.beginTx()) {
        Statement statement = statementContextSupplier.get();
        node = db.createNode();
        labelId = statement.tokenWriteOperations().labelGetOrCreateForName("labello");
        statement.dataWriteOperations().nodeAddLabel(node.getId(), labelId);
        statement.close();
        transaction.failure();
        transaction.success();
    } catch (TransactionFailureException e) {
        expectedException = e;
    } finally {
        Assert.assertNotNull("Should have failed", expectedException);
    }
    // THEN
    try (Transaction tx = db.beginTx()) {
        Statement statement = statementContextSupplier.get();
        try {
            statement.readOperations().nodeHasLabel(node.getId(), labelId);
            fail("should have thrown exception");
        } catch (EntityNotFoundException e) {
        // Yay!
        }
    }
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Statement(org.neo4j.kernel.api.Statement) Node(org.neo4j.graphdb.Node) EntityNotFoundException(org.neo4j.kernel.api.exceptions.EntityNotFoundException) Test(org.junit.Test)

Aggregations

Statement (org.neo4j.kernel.api.Statement)158 Test (org.junit.Test)76 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)56 Transaction (org.neo4j.graphdb.Transaction)44 ReadOperations (org.neo4j.kernel.api.ReadOperations)40 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)30 EntityNotFoundException (org.neo4j.kernel.api.exceptions.EntityNotFoundException)23 NotFoundException (org.neo4j.graphdb.NotFoundException)21 Node (org.neo4j.graphdb.Node)20 ThreadToStatementContextBridge (org.neo4j.kernel.impl.core.ThreadToStatementContextBridge)19 PropertyNotFoundException (org.neo4j.kernel.api.exceptions.PropertyNotFoundException)13 KeyReadOperations (org.neo4j.kernel.impl.api.operations.KeyReadOperations)11 ArrayList (java.util.ArrayList)10 DependencyResolver (org.neo4j.graphdb.DependencyResolver)10 IndexNotFoundKernelException (org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException)10 Label (org.neo4j.graphdb.Label)9 KernelAPI (org.neo4j.kernel.api.KernelAPI)9 ProcedureException (org.neo4j.kernel.api.exceptions.ProcedureException)9 InvalidTransactionTypeKernelException (org.neo4j.kernel.api.exceptions.InvalidTransactionTypeKernelException)8 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)8