use of org.neo4j.graphdb.Relationship in project neo4j by neo4j.
the class PartialTransactionFailureIT method concurrentlyCommittingTransactionsMustNotRotateOutLoggedCommandsOfFailingTransaction.
@Test
public void concurrentlyCommittingTransactionsMustNotRotateOutLoggedCommandsOfFailingTransaction() throws Exception {
final ClassGuardedAdversary adversary = new ClassGuardedAdversary(new CountingAdversary(1, false), Command.RelationshipCommand.class);
adversary.disable();
File storeDir = dir.graphDbDir();
final Map<String, String> params = stringMap(GraphDatabaseSettings.pagecache_memory.name(), "8m");
final EmbeddedGraphDatabase db = new TestEmbeddedGraphDatabase(storeDir, params) {
@Override
protected void create(File storeDir, Map<String, String> params, GraphDatabaseFacadeFactory.Dependencies dependencies) {
new GraphDatabaseFacadeFactory(DatabaseInfo.COMMUNITY, CommunityEditionModule::new) {
@Override
protected PlatformModule createPlatform(File storeDir, Config config, Dependencies dependencies, GraphDatabaseFacade graphDatabaseFacade) {
return new PlatformModule(storeDir, config, databaseInfo, dependencies, graphDatabaseFacade) {
@Override
protected FileSystemAbstraction createFileSystemAbstraction() {
return new AdversarialFileSystemAbstraction(adversary);
}
};
}
}.initFacade(storeDir, params, dependencies, this);
}
};
Node a, b, c, d;
try (Transaction tx = db.beginTx()) {
a = db.createNode();
b = db.createNode();
c = db.createNode();
d = db.createNode();
tx.success();
}
adversary.enable();
CountDownLatch latch = new CountDownLatch(1);
Thread t1 = new Thread(createRelationship(db, a, b, latch), "T1");
Thread t2 = new Thread(createRelationship(db, c, d, latch), "T2");
t1.start();
t2.start();
// Wait for both threads to get going
t1.join(10);
t2.join(10);
latch.countDown();
// Wait for the transactions to finish
t1.join(25000);
t2.join(25000);
db.shutdown();
// We should observe the store in a consistent state
EmbeddedGraphDatabase db2 = new TestEmbeddedGraphDatabase(storeDir, params);
try (Transaction tx = db2.beginTx()) {
Node x = db2.getNodeById(a.getId());
Node y = db2.getNodeById(b.getId());
Node z = db2.getNodeById(c.getId());
Node w = db2.getNodeById(d.getId());
Iterator<Relationship> itrRelX = x.getRelationships().iterator();
Iterator<Relationship> itrRelY = y.getRelationships().iterator();
Iterator<Relationship> itrRelZ = z.getRelationships().iterator();
Iterator<Relationship> itrRelW = w.getRelationships().iterator();
if (itrRelX.hasNext() != itrRelY.hasNext()) {
fail("Node x and y have inconsistent relationship counts");
} else if (itrRelX.hasNext()) {
Relationship rel = itrRelX.next();
assertEquals(rel, itrRelY.next());
assertFalse(itrRelX.hasNext());
assertFalse(itrRelY.hasNext());
}
if (itrRelZ.hasNext() != itrRelW.hasNext()) {
fail("Node z and w have inconsistent relationship counts");
} else if (itrRelZ.hasNext()) {
Relationship rel = itrRelZ.next();
assertEquals(rel, itrRelW.next());
assertFalse(itrRelZ.hasNext());
assertFalse(itrRelW.hasNext());
}
} finally {
db2.shutdown();
}
}
use of org.neo4j.graphdb.Relationship in project neo4j by neo4j.
the class Rmnode method exec.
@Override
protected Continuation exec(AppCommandParser parser, Session session, Output out) throws ShellException, RemoteException {
NodeOrRelationship node = null;
if (parser.arguments().isEmpty()) {
node = getCurrent(session);
} else {
long id = parseInt(parser.arguments().get(0));
try {
node = wrap(getNodeById(id));
} catch (NotFoundException e) {
throw new ShellException("No node " + id + " found");
}
}
if (!node.isNode()) {
out.println("Please select a node to delete");
return Continuation.INPUT_COMPLETE;
}
boolean forceDeletion = parser.options().containsKey("f");
if (forceDeletion) {
for (Relationship relationship : node.asNode().getRelationships()) {
out.println("Relationship " + getDisplayName(getServer(), session, relationship, true, false) + " deleted");
relationship.delete();
}
}
if (node.asNode().hasRelationship()) {
throw new ShellException(getDisplayName(getServer(), session, node.asNode(), false) + " cannot be deleted because it still has relationships. Use -f to force deletion of its relationships");
}
node.asNode().delete();
return Continuation.INPUT_COMPLETE;
}
use of org.neo4j.graphdb.Relationship in project neo4j by neo4j.
the class Rmrel method exec.
@Override
protected Continuation exec(AppCommandParser parser, Session session, Output out) throws ShellException, RemoteException {
assertCurrentIsNode(session);
if (parser.arguments().isEmpty()) {
throw new ShellException("Must supply relationship id to delete as the first argument");
}
Node currentNode = this.getCurrent(session).asNode();
Relationship rel = findRel(currentNode, Long.parseLong(parser.arguments().get(0)));
rel.delete();
Node otherNode = rel.getOtherNode(currentNode);
boolean deleteOtherNodeIfEmpty = parser.options().containsKey("d");
if (deleteOtherNodeIfEmpty && !otherNode.hasRelationship()) {
out.println("Also deleted " + getDisplayName(getServer(), session, otherNode, false) + " due to it not having any relationships left");
otherNode.delete();
}
return Continuation.INPUT_COMPLETE;
}
use of org.neo4j.graphdb.Relationship in project neo4j by neo4j.
the class Ls method displayRelationships.
private void displayRelationships(AppCommandParser parser, NodeOrRelationship thing, Session session, Output out, boolean verbose, boolean quiet, Map<String, Object> filterMap, boolean caseInsensitiveFilters, boolean looseFilters, boolean brief) throws ShellException, RemoteException {
boolean sortByType = parser.options().containsKey("s");
Node node = thing.asNode();
Iterable<Relationship> relationships = getRelationships(node, filterMap, caseInsensitiveFilters, looseFilters, sortByType | brief);
if (brief) {
Iterator<Relationship> iterator = relationships.iterator();
if (!iterator.hasNext()) {
return;
}
Relationship sampleRelationship = iterator.next();
RelationshipType lastType = sampleRelationship.getType();
int currentCounter = 1;
while (iterator.hasNext()) {
Relationship rel = iterator.next();
if (!rel.isType(lastType)) {
displayBriefRelationships(thing, session, out, sampleRelationship, currentCounter);
sampleRelationship = rel;
lastType = sampleRelationship.getType();
currentCounter = 1;
} else {
currentCounter++;
}
}
displayBriefRelationships(thing, session, out, sampleRelationship, currentCounter);
} else {
Iterator<Relationship> iterator = relationships.iterator();
if (parser.options().containsKey("m")) {
iterator = wrapInLimitingIterator(parser, iterator, filterMap, caseInsensitiveFilters, looseFilters);
}
while (iterator.hasNext()) {
Relationship rel = iterator.next();
StringBuffer buf = new StringBuffer(getDisplayName(getServer(), session, thing, true));
String relDisplay = quiet ? "" : getDisplayName(getServer(), session, rel, verbose, true);
buf.append(withArrows(rel, relDisplay, thing.asNode()));
buf.append(getDisplayName(getServer(), session, rel.getOtherNode(node), true));
out.println(buf);
}
}
}
use of org.neo4j.graphdb.Relationship in project neo4j by neo4j.
the class AbstractShellIT method createRelationshipChain.
protected Relationship[] createRelationshipChain(RelationshipType type, int length) {
try (Transaction transaction = db.beginTx()) {
Relationship[] relationshipChain = createRelationshipChain(db.createNode(), type, length);
transaction.success();
return relationshipChain;
}
}
Aggregations