Search in sources :

Example 96 with Relationship

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

the class TestAutoIndexReopen method testForceOpenIfChanged.

@Test
public void testForceOpenIfChanged() {
    // do some actions to force the indexreader to be reopened
    try (Transaction tx = graphDb.beginTx()) {
        Node node1 = graphDb.getNodeById(id1);
        Node node2 = graphDb.getNodeById(id2);
        Node node3 = graphDb.getNodeById(id3);
        node1.setProperty("np2", "test property");
        node1.getRelationships(RelationshipType.withName("FOO")).forEach(r -> {
            r.delete();
        });
        // check first node
        assertEquals(0, relationShipAutoIndex().get("type", "FOO", node1, node3).size());
        // create second relation ship
        Relationship rel = node1.createRelationshipTo(node3, RelationshipType.withName("FOO"));
        rel.setProperty("type", "FOO");
        // check second node -> crashs with old FullTxData
        assertEquals(0, relationShipAutoIndex().get("type", "FOO", node1, node2).size());
        // create second relation ship
        rel = node1.createRelationshipTo(node2, RelationshipType.withName("FOO"));
        rel.setProperty("type", "FOO");
        assertEquals(1, relationShipAutoIndex().get("type", "FOO", node1, node2).size());
        tx.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 97 with Relationship

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

the class TestAutoIndexReopen method startDb.

@Before
public void startDb() {
    graphDb = (GraphDatabaseAPI) new TestGraphDatabaseFactory().newImpermanentDatabaseBuilder().setConfig(new HashMap<>()).newGraphDatabase();
    try (Transaction tx = graphDb.beginTx()) {
        // Create the node and relationship auto-indexes
        graphDb.index().getNodeAutoIndexer().setEnabled(true);
        graphDb.index().getNodeAutoIndexer().startAutoIndexingProperty("nodeProp");
        graphDb.index().getRelationshipAutoIndexer().setEnabled(true);
        graphDb.index().getRelationshipAutoIndexer().startAutoIndexingProperty("type");
        tx.success();
    }
    try (Transaction tx = graphDb.beginTx()) {
        Node node1 = graphDb.createNode();
        Node node2 = graphDb.createNode();
        Node node3 = graphDb.createNode();
        id1 = node1.getId();
        id2 = node2.getId();
        id3 = node3.getId();
        Relationship rel = node1.createRelationshipTo(node2, RelationshipType.withName("FOO"));
        rel.setProperty("type", "FOO");
        tx.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) Before(org.junit.Before)

Example 98 with Relationship

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

the class TestAutoIndexing method testRemoveRelationshipRemovesDocument.

@Test
public void testRemoveRelationshipRemovesDocument() {
    AutoIndexer<Relationship> autoIndexer = graphDb.index().getRelationshipAutoIndexer();
    autoIndexer.startAutoIndexingProperty("foo");
    autoIndexer.setEnabled(true);
    newTransaction();
    Node node1 = graphDb.createNode();
    Node node2 = graphDb.createNode();
    Relationship rel = node1.createRelationshipTo(node2, RelationshipType.withName("foo"));
    rel.setProperty("foo", "bar");
    newTransaction();
    assertThat(graphDb.index().forRelationships("relationship_auto_index").query("_id_:*").size(), equalTo(1));
    newTransaction();
    rel.delete();
    newTransaction();
    assertThat(graphDb.index().forRelationships("relationship_auto_index").query("_id_:*").size(), equalTo(0));
}
Also used : Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Example 99 with Relationship

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

the class TestLuceneIndex method makeSureYouCanRemoveFromRelationshipIndex.

@Test
public void makeSureYouCanRemoveFromRelationshipIndex() {
    Node n1 = graphDb.createNode();
    Node n2 = graphDb.createNode();
    Relationship r = n1.createRelationshipTo(n2, withName("foo"));
    RelationshipIndex index = graphDb.index().forRelationships("rel-index");
    String key = "bar";
    index.remove(r, key, "value");
    index.add(r, key, "otherValue");
    for (int i = 0; i < 2; i++) {
        assertThat(index.get(key, "value"), emptyIterable());
        assertThat(index.get(key, "otherValue"), Contains.contains(r));
        restartTx();
    }
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) RelationshipIndex(org.neo4j.graphdb.index.RelationshipIndex) Test(org.junit.Test)

Example 100 with Relationship

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

the class TestLuceneIndex method removingAnIndexedRelationshipWillAlsoRemoveItFromTheIndex.

@Test
public void removingAnIndexedRelationshipWillAlsoRemoveItFromTheIndex() {
    Index<Relationship> index = relationshipIndex(LuceneIndexImplementation.EXACT_CONFIG);
    Node a = graphDb.createNode();
    Node b = graphDb.createNode();
    Relationship rel = a.createRelationshipTo(b, withName("REL"));
    rel.setProperty("poke", 1);
    index.add(rel, "key", "value");
    commitTx();
    beginTx();
    rel.delete();
    commitTx();
    beginTx();
    IndexHits<Relationship> rels = index.get("key", "value");
    // IndexHits.size is allowed to be inaccurate in this case:
    assertThat(rels.size(), isOneOf(0, 1));
    for (Relationship r : rels) {
        r.getProperty("poke");
        fail("Found relationship " + r);
    }
    commitTx();
    beginTx();
    IndexHits<Relationship> relsAgain = index.get("key", "value");
    // After a read, the index should be repaired:
    assertThat(relsAgain.size(), is(0));
    for (Relationship r : relsAgain) {
        r.getProperty("poke");
        fail("Found relationship " + r);
    }
}
Also used : Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Aggregations

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