Search in sources :

Example 41 with RelationshipType

use of org.neo4j.graphdb.RelationshipType in project neo4j by neo4j.

the class TestTransactionEvents method shouldAllowToStringOnCreatedRelationshipInAfterCommit.

@Test
void shouldAllowToStringOnCreatedRelationshipInAfterCommit() {
    // GIVEN
    Relationship relationship;
    Node startNode;
    Node endNode;
    RelationshipType type = MyRelTypes.TEST;
    try (Transaction tx = db.beginTx()) {
        startNode = tx.createNode();
        endNode = tx.createNode();
        relationship = startNode.createRelationshipTo(endNode, type);
        tx.commit();
    }
    // WHEN
    AtomicReference<String> deletedToString = new AtomicReference<>();
    dbms.registerTransactionEventListener(DEFAULT_DATABASE_NAME, new TransactionEventListenerAdapter<>() {

        @Override
        public void afterCommit(TransactionData data, Object state, GraphDatabaseService databaseService) {
            for (Relationship relationship : data.deletedRelationships()) {
                deletedToString.set(relationship.toString());
            }
        }
    });
    try (Transaction tx = db.beginTx()) {
        tx.getRelationshipById(relationship.getId()).delete();
        tx.commit();
    }
    // THEN
    assertNotNull(deletedToString.get());
    assertThat(deletedToString.get()).contains(type.name());
    assertThat(deletedToString.get()).contains(format("(%d)", startNode.getId()));
    assertThat(deletedToString.get()).contains(format("(%d)", endNode.getId()));
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) RelationshipType(org.neo4j.graphdb.RelationshipType) AtomicReference(java.util.concurrent.atomic.AtomicReference) TransactionData(org.neo4j.graphdb.event.TransactionData) Test(org.junit.jupiter.api.Test)

Example 42 with RelationshipType

use of org.neo4j.graphdb.RelationshipType in project neo4j by neo4j.

the class TransactionEventsIT method registerUnregisterWithConcurrentTransactions.

@Test
void registerUnregisterWithConcurrentTransactions() throws Exception {
    ExecutorService executor = Executors.newFixedThreadPool(2);
    try {
        AtomicInteger runningCounter = new AtomicInteger();
        AtomicInteger doneCounter = new AtomicInteger();
        BinaryLatch startLatch = new BinaryLatch();
        RelationshipType relationshipType = RelationshipType.withName("REL");
        CountingTransactionEventListener[] handlers = new CountingTransactionEventListener[20];
        for (int i = 0; i < handlers.length; i++) {
            handlers[i] = new CountingTransactionEventListener();
        }
        long relNodeId;
        try (Transaction tx = db.beginTx()) {
            relNodeId = tx.createNode().getId();
            tx.commit();
        }
        Future<?> nodeCreator = executor.submit(() -> {
            try {
                runningCounter.incrementAndGet();
                startLatch.await();
                for (int i = 0; i < 2_000; i++) {
                    try (Transaction tx = db.beginTx()) {
                        tx.createNode();
                        if (ThreadLocalRandom.current().nextBoolean()) {
                            tx.commit();
                        }
                    }
                }
            } finally {
                doneCounter.incrementAndGet();
            }
        });
        Future<?> relationshipCreator = executor.submit(() -> {
            try {
                runningCounter.incrementAndGet();
                startLatch.await();
                for (int i = 0; i < 1_000; i++) {
                    try (Transaction tx = db.beginTx()) {
                        Node relNode = tx.getNodeById(relNodeId);
                        relNode.createRelationshipTo(relNode, relationshipType);
                        if (ThreadLocalRandom.current().nextBoolean()) {
                            tx.commit();
                        }
                    }
                }
            } finally {
                doneCounter.incrementAndGet();
            }
        });
        while (runningCounter.get() < 2) {
            Thread.yield();
        }
        int i = 0;
        dbms.registerTransactionEventListener(DEFAULT_DATABASE_NAME, handlers[i]);
        CountingTransactionEventListener currentlyRegistered = handlers[i];
        i++;
        startLatch.release();
        while (doneCounter.get() < 2) {
            dbms.registerTransactionEventListener(DEFAULT_DATABASE_NAME, handlers[i]);
            i++;
            if (i == handlers.length) {
                i = 0;
            }
            dbms.unregisterTransactionEventListener(DEFAULT_DATABASE_NAME, currentlyRegistered);
            currentlyRegistered = handlers[i];
        }
        nodeCreator.get();
        relationshipCreator.get();
        for (CountingTransactionEventListener handler : handlers) {
            assertEquals(0, handler.get());
        }
    } finally {
        executor.shutdown();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Node(org.neo4j.graphdb.Node) ExecutorService(java.util.concurrent.ExecutorService) RelationshipType(org.neo4j.graphdb.RelationshipType) BinaryLatch(org.neo4j.util.concurrent.BinaryLatch) Test(org.junit.jupiter.api.Test)

Example 43 with RelationshipType

use of org.neo4j.graphdb.RelationshipType in project neo4j by neo4j.

the class DetachDeleteIT method verifyDetachDeleteRacingWithRelationCreateWithoutThrowing.

private void verifyDetachDeleteRacingWithRelationCreateWithoutThrowing(long nodeId, int iterPerRelType) throws Exception {
    CountDownLatch latch = new CountDownLatch(5);
    List<Callable<Object>> tasks = new ArrayList<>();
    for (int i = 1; i < 10; i++) {
        RelationshipType type = RelationshipType.withName("R" + i);
        Callable<Object> callable = () -> {
            latch.countDown();
            latch.await();
            try (Transaction tx = db.beginTx()) {
                Node node = tx.getNodeById(nodeId);
                node.createRelationshipTo(tx.createNode(), type);
                tx.commit();
            } catch (NotFoundException | DeadlockDetectedException ignore) {
            // The DETACH DELETE got ahead of us. It's fine.
            }
            return null;
        };
        for (int j = 0; j < iterPerRelType; j++) {
            tasks.add(callable);
        }
    }
    tasks.add(() -> {
        latch.countDown();
        latch.await();
        var retry = true;
        while (retry) {
            try (Transaction tx = db.beginTx()) {
                Write write = getWrite(tx);
                write.nodeDetachDelete(nodeId);
                tx.commit();
                retry = false;
            } catch (DeadlockDetectedException de) {
                retry = false;
            }
        }
        return null;
    });
    Collections.shuffle(tasks);
    List<Future<Object>> futures = executor.invokeAll(tasks);
    for (Future<Object> future : futures) {
        future.get();
    }
}
Also used : Write(org.neo4j.internal.kernel.api.Write) Node(org.neo4j.graphdb.Node) DeadlockDetectedException(org.neo4j.kernel.DeadlockDetectedException) ArrayList(java.util.ArrayList) RelationshipType(org.neo4j.graphdb.RelationshipType) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Future(java.util.concurrent.Future)

Example 44 with RelationshipType

use of org.neo4j.graphdb.RelationshipType in project neo4j by neo4j.

the class QueryExecutionLocksIT method takeRelationshipTypeLockForQueryWithIndexUsages.

@Test
void takeRelationshipTypeLockForQueryWithIndexUsages() throws Exception {
    RelationshipType relType = RelationshipType.withName("REL");
    String propertyKey = "name";
    createRelationshipIndex(relType, propertyKey);
    try (Transaction transaction = db.beginTx()) {
        Node node1 = transaction.createNode();
        Node node2 = transaction.createNode();
        node1.createRelationshipTo(node2, relType).setProperty(propertyKey, "v");
        transaction.commit();
    }
    String query = "MATCH ()-[r:" + relType.name() + "]-() where r." + propertyKey + " = \"v\" RETURN r ";
    List<LockOperationRecord> lockOperationRecords = traceQueryLocks(query);
    assertThat(lockOperationRecords).as("Observed list of lock operations is: " + lockOperationRecords).hasSize(1);
    LockOperationRecord operationRecord = lockOperationRecords.get(0);
    assertTrue(operationRecord.acquisition);
    assertFalse(operationRecord.exclusive);
    assertEquals(ResourceTypes.RELATIONSHIP_TYPE, operationRecord.resourceType);
}
Also used : InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Node(org.neo4j.graphdb.Node) RelationshipType(org.neo4j.graphdb.RelationshipType) Test(org.junit.jupiter.api.Test)

Example 45 with RelationshipType

use of org.neo4j.graphdb.RelationshipType in project neo4j by neo4j.

the class QueryExecutionLocksIT method takeRelationshipTypeLockForQueryWithSeekIndexUsages.

@Test
void takeRelationshipTypeLockForQueryWithSeekIndexUsages() throws Exception {
    RelationshipType relType = RelationshipType.withName("REL");
    String propertyKey = "name";
    createRelationshipIndex(relType, propertyKey);
    Relationship rel;
    try (Transaction transaction = db.beginTx()) {
        Node node1 = transaction.createNode();
        Node node2 = transaction.createNode();
        rel = node1.createRelationshipTo(node2, relType);
        rel.setProperty(propertyKey, "v");
        transaction.commit();
    }
    String query = "MATCH ()-[r:REL]->() WHERE r.name = 'v' RETURN r.prop";
    List<LockOperationRecord> lockOperationRecords = traceQueryLocks(query);
    assertThat(lockOperationRecords).as("Observed list of lock operations is: " + lockOperationRecords).hasSize(1);
    LockOperationRecord operationRecord = lockOperationRecords.get(0);
    assertTrue(operationRecord.acquisition);
    assertFalse(operationRecord.exclusive);
    assertEquals(ResourceTypes.RELATIONSHIP_TYPE, operationRecord.resourceType);
}
Also used : InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) RelationshipType(org.neo4j.graphdb.RelationshipType) Test(org.junit.jupiter.api.Test)

Aggregations

RelationshipType (org.neo4j.graphdb.RelationshipType)147 Node (org.neo4j.graphdb.Node)84 Relationship (org.neo4j.graphdb.Relationship)59 Transaction (org.neo4j.graphdb.Transaction)52 Test (org.junit.Test)35 Test (org.junit.jupiter.api.Test)32 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)20 Direction (org.neo4j.graphdb.Direction)19 Label (org.neo4j.graphdb.Label)18 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)15 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)15 ArrayList (java.util.ArrayList)13 NotFoundException (org.neo4j.graphdb.NotFoundException)10 Traverser (org.neo4j.graphdb.Traverser)10 HashSet (java.util.HashSet)9 RelationshipRecord (org.neo4j.kernel.impl.nioneo.store.RelationshipRecord)7 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)7 Collection (java.util.Collection)6 HashMap (java.util.HashMap)6 LinkedList (java.util.LinkedList)6