Search in sources :

Example 46 with Relationship

use of org.neo4j.graphdb.Relationship in project graphdb by neo4j-attic.

the class TestBatchInsert method testGraphDbServiceGetRelationships.

@Test
public void testGraphDbServiceGetRelationships() {
    BatchInserter batchInserter = newBatchInserter();
    GraphDatabaseService graphDb = batchInserter.getGraphDbService();
    Node startNode = graphDb.createNode();
    for (int i = 0; i < 5; i++) {
        Node endNode = graphDb.createNode();
        startNode.createRelationshipTo(endNode, relTypeArray[i]);
    }
    try {
        startNode.createRelationshipTo(startNode, relTypeArray[0]);
        fail("Could create relationship with same start and end node");
    } catch (IllegalArgumentException e) {
    // ok good
    }
    for (int i = 0; i < 5; i++) {
        assertTrue(startNode.getSingleRelationship(relTypeArray[i], Direction.OUTGOING) != null);
    }
    for (int i = 0; i < 5; i++) {
        Iterator<Relationship> relItr = startNode.getRelationships(relTypeArray[i], Direction.OUTGOING).iterator();
        relItr.next();
        assertTrue(!relItr.hasNext());
    }
    for (int i = 0; i < 5; i++) {
        Iterator<Relationship> relItr = startNode.getRelationships(relTypeArray[i]).iterator();
        relItr.next();
        assertTrue(!relItr.hasNext());
    }
    graphDb.shutdown();
}
Also used : BatchInserter(org.neo4j.kernel.impl.batchinsert.BatchInserter) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) SimpleRelationship(org.neo4j.kernel.impl.batchinsert.SimpleRelationship) Test(org.junit.Test)

Example 47 with Relationship

use of org.neo4j.graphdb.Relationship in project graphdb by neo4j-attic.

the class TestBigStore method createAndVerifyGraphStartingWithId.

private void createAndVerifyGraphStartingWithId(long startId, int requiredHeapMb) throws Exception {
    assumeTrue(machineIsOkToRunThisTest(testName.getMethodName(), requiredHeapMb));
    /*
         * Will create a layout like this:
         * 
         * (refNode) --> (node) --> (highNode)
         *           ...
         *           ...
         *           
         * Each node/relationship will have a bunch of different properties on them.
         */
    setHighIds(startId - 1000);
    byte[] bytes = new byte[45];
    bytes[2] = 5;
    bytes[10] = 42;
    Map<String, Object> properties = map("number", 11, "short string", "test", "long string", "This is a long value, long enough", "array", bytes);
    Transaction tx = db.beginTx();
    int count = 10000;
    for (int i = 0; i < count; i++) {
        Node node = db.createNode();
        setProperties(node, properties);
        Relationship rel = db.getReferenceNode().createRelationshipTo(node, this);
        setProperties(rel, properties);
        Node highNode = db.createNode();
        node.createRelationshipTo(highNode, OTHER_TYPE);
        setProperties(highNode, properties);
        if (i % 100 == 0 && i > 0) {
            tx.success();
            tx.finish();
            tx = db.beginTx();
        }
    }
    tx.success();
    tx.finish();
    db.shutdown();
    db = new EmbeddedGraphDatabase(PATH);
    // Verify the data
    int verified = 0;
    for (Relationship rel : db.getReferenceNode().getRelationships(Direction.OUTGOING)) {
        Node node = rel.getEndNode();
        assertProperties(properties, node);
        assertProperties(properties, rel);
        Node highNode = node.getSingleRelationship(OTHER_TYPE, Direction.OUTGOING).getEndNode();
        assertProperties(properties, highNode);
        verified++;
    }
    assertEquals(count, verified);
}
Also used : EmbeddedGraphDatabase(org.neo4j.kernel.EmbeddedGraphDatabase) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship)

Example 48 with Relationship

use of org.neo4j.graphdb.Relationship in project graphdb by neo4j-attic.

the class TestNeo4j method testIdUsageInfo.

//    private static enum RelTypes implements RelationshipType
//    {
//        ONE_MORE_RELATIONSHIP;
//    }
// TODO: fix this testcase
@Test
public void testIdUsageInfo() {
    GraphDbModule graphDbModule = ((EmbeddedGraphDatabase) getGraphDb()).getConfig().getGraphDbModule();
    NodeManager nm = graphDbModule.getNodeManager();
    long nodeCount = nm.getNumberOfIdsInUse(Node.class);
    long relCount = nm.getNumberOfIdsInUse(Relationship.class);
    if (nodeCount > nm.getHighestPossibleIdInUse(Node.class)) {
    // fail( "Node count greater than highest id " + nodeCount );
    }
    if (relCount > nm.getHighestPossibleIdInUse(Relationship.class)) {
    // fail( "Rel count greater than highest id " + relCount );
    }
    // assertTrue( nodeCount <= nm.getHighestPossibleIdInUse( Node.class )
    // );
    // assertTrue( relCount <= nm.getHighestPossibleIdInUse(
    // Relationship.class ) );
    Node n1 = nm.createNode();
    Node n2 = nm.createNode();
    Relationship r1 = n1.createRelationshipTo(n2, MyRelTypes.TEST);
    // assertEquals( nodeCount + 2, nm.getNumberOfIdsInUse( Node.class ) );
    // assertEquals( relCount + 1, nm.getNumberOfIdsInUse(
    // Relationship.class ) );
    r1.delete();
    n1.delete();
    n2.delete();
    // must commit for ids to be reused
    try {
        getTransaction().success();
        getTransaction().finish();
    } catch (Exception e) {
        fail("" + e);
    }
    // assertEquals( nodeCount, nm.getNumberOfIdsInUse( Node.class ) );
    // assertEquals( relCount, nm.getNumberOfIdsInUse( Relationship.class )
    // );
    setTransaction(getGraphDb().beginTx());
}
Also used : NodeManager(org.neo4j.kernel.impl.core.NodeManager) GraphDbModule(org.neo4j.kernel.impl.core.GraphDbModule) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 49 with Relationship

use of org.neo4j.graphdb.Relationship in project graphdb by neo4j-attic.

the class TestNeo4jApiExceptions method testNotFoundException.

@Test
public void testNotFoundException() {
    Node node1 = getGraphDb().createNode();
    Node node2 = getGraphDb().createNode();
    Relationship rel = node1.createRelationshipTo(node2, MyRelTypes.TEST);
    long nodeId = node1.getId();
    long relId = rel.getId();
    rel.delete();
    node2.delete();
    node1.delete();
    newTransaction();
    try {
        getGraphDb().getNodeById(nodeId);
        fail("Get node by id on deleted node should throw exception");
    } catch (NotFoundException e) {
    // good
    }
    try {
        getGraphDb().getRelationshipById(relId);
        fail("Get relationship by id on deleted node should " + "throw exception");
    } catch (NotFoundException e) {
    // good
    }
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) NotFoundException(org.neo4j.graphdb.NotFoundException) Test(org.junit.Test)

Example 50 with Relationship

use of org.neo4j.graphdb.Relationship in project graphdb by neo4j-attic.

the class TestNeo4jApiExceptions method testNotInTransactionException.

@Test
public void testNotInTransactionException() {
    Node node1 = getGraphDb().createNode();
    node1.setProperty("test", 1);
    Node node2 = getGraphDb().createNode();
    Node node3 = getGraphDb().createNode();
    Relationship rel = node1.createRelationshipTo(node2, MyRelTypes.TEST);
    rel.setProperty("test", 11);
    commit();
    try {
        getGraphDb().createNode();
        fail("Create node with no transaction should throw exception");
    } catch (NotInTransactionException e) {
    // good
    }
    try {
        node1.createRelationshipTo(node2, MyRelTypes.TEST);
        fail("Create relationship with no transaction should " + "throw exception");
    } catch (NotInTransactionException e) {
    // good
    }
    try {
        node1.setProperty("test", 2);
        fail("Set property with no transaction should throw exception");
    } catch (NotInTransactionException e) {
    // good
    }
    try {
        rel.setProperty("test", 22);
        fail("Set property with no transaction should throw exception");
    } catch (NotInTransactionException e) {
    // good
    }
    try {
        node3.delete();
        fail("Delete node with no transaction should " + "throw exception");
    } catch (NotInTransactionException e) {
    // good
    }
    try {
        rel.delete();
        fail("Delete relationship with no transaction should " + "throw exception");
    } catch (NotInTransactionException e) {
    // good
    }
    newTransaction();
    assertEquals(node1.getProperty("test"), 1);
    assertEquals(rel.getProperty("test"), 11);
    assertEquals(rel, node1.getSingleRelationship(MyRelTypes.TEST, Direction.OUTGOING));
    node1.delete();
    node2.delete();
    rel.delete();
    node3.delete();
}
Also used : NotInTransactionException(org.neo4j.graphdb.NotInTransactionException) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Aggregations

Relationship (org.neo4j.graphdb.Relationship)490 Node (org.neo4j.graphdb.Node)370 Test (org.junit.Test)250 Transaction (org.neo4j.graphdb.Transaction)138 LinkedList (java.util.LinkedList)48 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)48 RelationshipType (org.neo4j.graphdb.RelationshipType)38 NotFoundException (org.neo4j.graphdb.NotFoundException)35 Path (org.neo4j.graphdb.Path)29 HashMap (java.util.HashMap)27 Direction (org.neo4j.graphdb.Direction)26 HashSet (java.util.HashSet)24 ArrayList (java.util.ArrayList)18 RelationshipIndex (org.neo4j.graphdb.index.RelationshipIndex)18 Map (java.util.Map)14 WeightedPath (org.neo4j.graphalgo.WeightedPath)14 EmbeddedGraphDatabase (org.neo4j.kernel.EmbeddedGraphDatabase)14 List (java.util.List)13 PropertyContainer (org.neo4j.graphdb.PropertyContainer)12 Graph (org.neo4j.test.GraphDescription.Graph)11