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()));
}
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();
}
}
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();
}
}
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);
}
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);
}
Aggregations