Search in sources :

Example 56 with Relationship

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

the class TestNeo4jCacheAndPersistence method testTxCacheLoadIsolation.

@Test
public void testTxCacheLoadIsolation() throws Exception {
    Node node = getGraphDb().createNode();
    node.setProperty("someproptest", "testing");
    Node node1 = getGraphDb().createNode();
    node1.setProperty("someotherproptest", 2);
    commit();
    EmbeddedGraphDatabase graphDb = (EmbeddedGraphDatabase) getGraphDb();
    TransactionManager txManager = graphDb.getConfig().getTxModule().getTxManager();
    NodeManager nodeManager = graphDb.getConfig().getGraphDbModule().getNodeManager();
    txManager.begin();
    node.setProperty("someotherproptest", "testing2");
    Relationship rel = node.createRelationshipTo(node1, MyRelTypes.TEST);
    javax.transaction.Transaction txA = txManager.suspend();
    txManager.begin();
    assertEquals("testing", node.getProperty("someproptest"));
    assertTrue(!node.hasProperty("someotherproptest"));
    assertTrue(!node.hasRelationship());
    nodeManager.clearCache();
    assertEquals("testing", node.getProperty("someproptest"));
    assertTrue(!node.hasProperty("someotherproptest"));
    javax.transaction.Transaction txB = txManager.suspend();
    txManager.resume(txA);
    assertEquals("testing", node.getProperty("someproptest"));
    assertTrue(node.hasProperty("someotherproptest"));
    assertTrue(node.hasRelationship());
    nodeManager.clearCache();
    assertEquals("testing", node.getProperty("someproptest"));
    assertTrue(node.hasProperty("someotherproptest"));
    assertTrue(node.hasRelationship());
    txManager.suspend();
    txManager.resume(txB);
    assertEquals("testing", node.getProperty("someproptest"));
    assertTrue(!node.hasProperty("someotherproptest"));
    assertTrue(!node.hasRelationship());
    txManager.rollback();
    txManager.resume(txA);
    node.delete();
    node1.delete();
    rel.delete();
    txManager.commit();
    newTransaction();
}
Also used : EmbeddedGraphDatabase(org.neo4j.kernel.EmbeddedGraphDatabase) TransactionManager(javax.transaction.TransactionManager) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 57 with Relationship

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

the class TestNeo4jConstrains method testIllegalPropertyType.

@Test
public void testIllegalPropertyType() {
    Logger log = Logger.getLogger(NodeManager.class.getName());
    Level level = log.getLevel();
    log.setLevel(Level.OFF);
    try {
        Node node1 = getGraphDb().createNode();
        try {
            node1.setProperty(key, new Object());
            fail("Shouldn't validate");
        } catch (Exception e) {
        // good
        }
        try {
            Transaction tx = getTransaction();
            tx.success();
            tx.finish();
            fail("Shouldn't validate");
        } catch (Exception e) {
        }
        // good
        setTransaction(getGraphDb().beginTx());
        try {
            getGraphDb().getNodeById(node1.getId());
            fail("Node should not exist, previous tx didn't rollback");
        } catch (NotFoundException e) {
        // good
        }
        node1 = getGraphDb().createNode();
        Node node2 = getGraphDb().createNode();
        Relationship rel = node1.createRelationshipTo(node2, MyRelTypes.TEST);
        try {
            rel.setProperty(key, new Object());
            fail("Shouldn't validate");
        } catch (Exception e) {
        // good
        }
        try {
            Transaction tx = getTransaction();
            tx.success();
            tx.finish();
            fail("Shouldn't validate");
        } catch (Exception e) {
        }
        // good
        setTransaction(getGraphDb().beginTx());
        try {
            getGraphDb().getNodeById(node1.getId());
            fail("Node should not exist, previous tx didn't rollback");
        } catch (NotFoundException e) {
        // good
        }
        try {
            getGraphDb().getNodeById(node2.getId());
            fail("Node should not exist, previous tx didn't rollback");
        } catch (NotFoundException e) {
        // good
        }
    } finally {
        log.setLevel(level);
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) NotFoundException(org.neo4j.graphdb.NotFoundException) Level(java.util.logging.Level) Logger(java.util.logging.Logger) NotFoundException(org.neo4j.graphdb.NotFoundException) Test(org.junit.Test)

Example 58 with Relationship

use of org.neo4j.graphdb.Relationship in project neo4j-mobile-android by neo4j-contrib.

the class TraversalBranchImpl method next.

public TraversalBranch next() {
    while (relationships.hasNext()) {
        Relationship relationship = relationships.next();
        if (relationship.equals(howIGotHere)) {
            continue;
        }
        expandedCount++;
        Node node = relationship.getOtherNode(source);
        TraversalBranch next = new TraversalBranchImpl(traverser, this, depth + 1, node, traverser.description.expander, relationship);
        if (traverser.okToProceed(next)) {
            next.initialize();
            return next;
        }
    }
    return null;
}
Also used : Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch)

Example 59 with Relationship

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

the class TestMigrateToDenseNodeSupport method migrateDbWithDenseNodes.

@Test
public void migrateDbWithDenseNodes() throws Exception {
    // migrate
    new TestGraphDatabaseFactory().newEmbeddedDatabaseBuilder(dir).setConfig(allow_store_upgrade, "true").newGraphDatabase().shutdown();
    // check consistency
    assertConsistentStore(dir);
    // open again to do extra checks
    GraphDatabaseService db = new TestGraphDatabaseFactory().newEmbeddedDatabaseBuilder(dir).newGraphDatabase();
    try (Transaction tx = db.beginTx()) {
        ResourceIterator<Node> allNodesWithLabel = db.findNodes(referenceNode);
        Node refNode = Iterators.single(allNodesWithLabel);
        int sparseCount = 0;
        for (Relationship relationship : refNode.getRelationships(Types.SPARSE, OUTGOING)) {
            verifySparseNode(db, relationship.getEndNode());
            sparseCount++;
        }
        int denseCount = 0;
        for (Relationship relationship : refNode.getRelationships(Types.DENSE, OUTGOING)) {
            verifyDenseNode(db, relationship.getEndNode());
            denseCount++;
        }
        assertEquals(10, sparseCount);
        assertEquals(10, denseCount);
        tx.success();
    }
    db.shutdown();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) Test(org.junit.Test)

Example 60 with Relationship

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

the class ExecutionResultSerializerTest method shouldProduceResultStreamWithGraphEntries.

@Test
public void shouldProduceResultStreamWithGraphEntries() throws Exception {
    // given
    Node[] node = { node(0, properties(property("name", "node0")), "Node"), node(1, properties(property("name", "node1"))), node(2, properties(property("name", "node2")), "This", "That"), node(3, properties(property("name", "node3")), "Other") };
    Relationship[] rel = { relationship(0, node[0], "KNOWS", node[1], property("name", "rel0")), relationship(1, node[2], "LOVES", node[3], property("name", "rel1")) };
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ExecutionResultSerializer serializer = getSerializerWith(output);
    // when
    serializer.statementResult(mockExecutionResult(map("node", node[0], "rel", rel[0]), map("node", node[2], "rel", rel[1])), false, ResultDataContent.row, ResultDataContent.graph);
    serializer.finish();
    // then
    String result = output.toString(UTF_8.name());
    // Nodes and relationships form sets, so we cannot test for a fixed string, since we don't know the order.
    String node0 = "{\"id\":\"0\",\"labels\":[\"Node\"],\"properties\":{\"name\":\"node0\"}}";
    String node1 = "{\"id\":\"1\",\"labels\":[],\"properties\":{\"name\":\"node1\"}}";
    String node2 = "{\"id\":\"2\",\"labels\":[\"This\",\"That\"],\"properties\":{\"name\":\"node2\"}}";
    String node3 = "{\"id\":\"3\",\"labels\":[\"Other\"],\"properties\":{\"name\":\"node3\"}}";
    String rel0 = "\"relationships\":[{\"id\":\"0\",\"type\":\"KNOWS\",\"startNode\":\"0\",\"endNode\":\"1\",\"properties\":{\"name\":\"rel0\"}}]}";
    String rel1 = "\"relationships\":[{\"id\":\"1\",\"type\":\"LOVES\",\"startNode\":\"2\",\"endNode\":\"3\",\"properties\":{\"name\":\"rel1\"}}]}";
    String row0 = "{\"row\":[{\"name\":\"node0\"},{\"name\":\"rel0\"}],\"meta\":[{\"id\":0,\"type\":\"node\",\"deleted\":false},{\"id\":0,\"type\":\"relationship\",\"deleted\":false}],\"graph\":{\"nodes\":[";
    String row1 = "{\"row\":[{\"name\":\"node2\"},{\"name\":\"rel1\"}],\"meta\":[{\"id\":2,\"type\":\"node\",\"deleted\":false},{\"id\":1,\"type\":\"relationship\",\"deleted\":false}],\"graph\":{\"nodes\":[";
    int n0 = result.indexOf(node0);
    int n1 = result.indexOf(node1);
    int n2 = result.indexOf(node2);
    int n3 = result.indexOf(node3);
    int r0 = result.indexOf(rel0);
    int r1 = result.indexOf(rel1);
    int _0 = result.indexOf(row0);
    int _1 = result.indexOf(row1);
    assertTrue("result should contain row0", _0 > 0);
    assertTrue("result should contain row1 after row0", _1 > _0);
    assertTrue("result should contain node0 after row0", n0 > _0);
    assertTrue("result should contain node1 after row0", n1 > _0);
    assertTrue("result should contain node2 after row1", n2 > _1);
    assertTrue("result should contain node3 after row1", n3 > _1);
    assertTrue("result should contain rel0 after node0 and node1", r0 > n0 && r0 > n1);
    assertTrue("result should contain rel1 after node2 and node3", r1 > n2 && r1 > n3);
}
Also used : JsonNode(org.codehaus.jackson.JsonNode) JsonHelper.jsonNode(org.neo4j.server.rest.domain.JsonHelper.jsonNode) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test) Neo4jJsonCodecTest(org.neo4j.server.rest.transactional.Neo4jJsonCodecTest)

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