use of org.neo4j.graphdb.RelationshipType in project neo4j by neo4j.
the class DatabaseMetadataService method generateJsonRepresentation.
private String generateJsonRepresentation(Iterable<RelationshipType> relationshipTypes) {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (RelationshipType rt : relationshipTypes) {
sb.append("\"");
sb.append(rt.name());
sb.append("\",");
}
sb.append("]");
return sb.toString().replaceAll(",]", "]");
}
use of org.neo4j.graphdb.RelationshipType in project neo4j by neo4j.
the class DatabaseMetadataService method getRelationshipTypes.
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getRelationshipTypes(@QueryParam("in_use") @DefaultValue("true") boolean inUse) {
try {
GraphDatabaseAPI db = database.getGraph();
Iterable<RelationshipType> relationshipTypes = inUse ? db.getAllRelationshipTypesInUse() : db.getAllRelationshipTypes();
return Response.ok().type(MediaType.APPLICATION_JSON).entity(generateJsonRepresentation(relationshipTypes)).build();
} finally {
representationWriteHandler.onRepresentationFinal();
}
}
use of org.neo4j.graphdb.RelationshipType in project neo4j by neo4j.
the class TestTransactionEvents method shouldProvideTheCorrectRelationshipData.
@Test
public void shouldProvideTheCorrectRelationshipData() {
GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
// create a rel type so the next type id is non zero
try (Transaction tx = db.beginTx()) {
db.createNode().createRelationshipTo(db.createNode(), withName("TYPE"));
}
RelationshipType livesIn = withName("LIVES_IN");
long relId;
try (Transaction tx = db.beginTx()) {
Node person = db.createNode(label("Person"));
Node city = db.createNode(label("City"));
Relationship rel = person.createRelationshipTo(city, livesIn);
rel.setProperty("since", 2009);
relId = rel.getId();
tx.success();
}
final Set<String> changedRelationships = new HashSet<>();
db.registerTransactionEventHandler(new TransactionEventHandler.Adapter<Void>() {
@Override
public Void beforeCommit(TransactionData data) throws Exception {
for (PropertyEntry<Relationship> entry : data.assignedRelationshipProperties()) {
changedRelationships.add(entry.entity().getType().name());
}
return null;
}
});
try (Transaction tx = db.beginTx()) {
Relationship rel = db.getRelationshipById(relId);
rel.setProperty("since", 2010);
tx.success();
}
assertEquals(1, changedRelationships.size());
assertTrue(livesIn + " not in " + changedRelationships.toString(), changedRelationships.contains(livesIn.name()));
}
use of org.neo4j.graphdb.RelationshipType in project neo4j by neo4j.
the class TestRelationship method makeSureLazyLoadingRelationshipsWorksEvenIfOtherIteratorAlsoLoadsInTheSameIteration.
@Test
public void makeSureLazyLoadingRelationshipsWorksEvenIfOtherIteratorAlsoLoadsInTheSameIteration() {
int numEdges = 100;
/* create 256 nodes */
GraphDatabaseService graphDB = getGraphDb();
Node[] nodes = new Node[256];
for (int num_nodes = 0; num_nodes < nodes.length; num_nodes += 1) {
nodes[num_nodes] = graphDB.createNode();
}
newTransaction();
/* create random outgoing relationships from node 5 */
Node hub = nodes[4];
int nextID = 7;
RelationshipType outtie = withName("outtie");
RelationshipType innie = withName("innie");
for (int k = 0; k < numEdges; k++) {
Node neighbor = nodes[nextID];
nextID += 7;
nextID &= 255;
if (nextID == 0) {
nextID = 1;
}
hub.createRelationshipTo(neighbor, outtie);
}
newTransaction();
/* create random incoming relationships to node 5 */
for (int k = 0; k < numEdges; k += 1) {
Node neighbor = nodes[nextID];
nextID += 7;
nextID &= 255;
if (nextID == 0) {
nextID = 1;
}
neighbor.createRelationshipTo(hub, innie);
}
commit();
newTransaction();
hub = graphDB.getNodeById(hub.getId());
int count = 0;
for (Relationship ignore : hub.getRelationships()) {
count += Iterables.count(hub.getRelationships());
}
assertEquals(40000, count);
count = 0;
for (@SuppressWarnings("unused") Relationship r1 : hub.getRelationships()) {
count += Iterables.count(hub.getRelationships());
}
assertEquals(40000, count);
commit();
}
use of org.neo4j.graphdb.RelationshipType in project neo4j by neo4j.
the class TestNeo4j method testBasicNodeRelationships.
@Test
public void testBasicNodeRelationships() {
Node firstNode;
Node secondNode;
Relationship rel;
// Create nodes and a relationship between them
firstNode = getGraphDb().createNode();
assertNotNull("Failure creating first node", firstNode);
secondNode = getGraphDb().createNode();
assertNotNull("Failure creating second node", secondNode);
rel = firstNode.createRelationshipTo(secondNode, MyRelTypes.TEST);
assertNotNull("Relationship is null", rel);
RelationshipType relType = rel.getType();
assertNotNull("Relationship's type is is null", relType);
// Verify that the node reports that it has a relationship of
// the type we created above
assertTrue(firstNode.getRelationships(relType).iterator().hasNext());
assertTrue(secondNode.getRelationships(relType).iterator().hasNext());
Iterable<Relationship> allRels;
// Verify that both nodes return the relationship we created above
allRels = firstNode.getRelationships();
assertTrue(this.objectExistsInIterable(rel, allRels));
allRels = firstNode.getRelationships(relType);
assertTrue(this.objectExistsInIterable(rel, allRels));
allRels = secondNode.getRelationships();
assertTrue(this.objectExistsInIterable(rel, allRels));
allRels = secondNode.getRelationships(relType);
assertTrue(this.objectExistsInIterable(rel, allRels));
// Verify that the relationship reports that it is associated with
// firstNode and secondNode
Node[] relNodes = rel.getNodes();
assertEquals("A relationship should always be connected to exactly " + "two nodes", relNodes.length, 2);
assertTrue("Relationship says that it isn't connected to firstNode", this.objectExistsInArray(firstNode, relNodes));
assertTrue("Relationship says that it isn't connected to secondNode", this.objectExistsInArray(secondNode, relNodes));
assertTrue("The other node should be secondNode but it isn't", rel.getOtherNode(firstNode).equals(secondNode));
assertTrue("The other node should be firstNode but it isn't", rel.getOtherNode(secondNode).equals(firstNode));
rel.delete();
secondNode.delete();
firstNode.delete();
}
Aggregations