Search in sources :

Example 86 with Relationship

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

the class GraphDbHelper method getIndexedRelationships.

public Collection<Long> getIndexedRelationships(String indexName, String key, Object value) {
    try (Transaction tx = database.getGraph().beginTransaction(implicit, AnonymousContext.write())) {
        Collection<Long> result = new ArrayList<>();
        for (Relationship relationship : database.getGraph().index().forRelationships(indexName).get(key, value)) {
            result.add(relationship.getId());
        }
        tx.success();
        return result;
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Relationship(org.neo4j.graphdb.Relationship) ArrayList(java.util.ArrayList)

Example 87 with Relationship

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

the class GraphDbHelper method addRelationshipToIndex.

public void addRelationshipToIndex(String indexName, String key, String value, long relationshipId) {
    try (Transaction tx = database.getGraph().beginTransaction(implicit, AUTH_DISABLED)) {
        Index<Relationship> index = database.getGraph().index().forRelationships(indexName);
        index.add(database.getGraph().getRelationshipById(relationshipId), key, value);
        tx.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Relationship(org.neo4j.graphdb.Relationship)

Example 88 with Relationship

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

the class IncrementalBackupTests method createInitialDataSet.

private DbRepresentation createInitialDataSet(File path) {
    db = startGraphDatabase(path);
    try (Transaction tx = db.beginTx()) {
        db.createNode().setProperty("name", "Goofy");
        Node donald = db.createNode();
        donald.setProperty("name", "Donald");
        Node daisy = db.createNode();
        daisy.setProperty("name", "Daisy");
        Relationship knows = donald.createRelationshipTo(daisy, RelationshipType.withName("LOVES"));
        knows.setProperty("since", 1940);
        tx.success();
    }
    DbRepresentation result = DbRepresentation.of(db);
    db.shutdown();
    return result;
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) DbRepresentation(org.neo4j.test.DbRepresentation)

Example 89 with Relationship

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

the class IdReuseTest method shouldReuseRelationshipIdsFromRolledBackTransaction.

@Test
public void shouldReuseRelationshipIdsFromRolledBackTransaction() throws Exception {
    // Given
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    Node node1, node2;
    try (Transaction tx = db.beginTx()) {
        node1 = db.createNode();
        node2 = db.createNode();
        tx.success();
    }
    try (Transaction tx = db.beginTx()) {
        node1.createRelationshipTo(node2, RelationshipType.withName("LIKE"));
        tx.failure();
    }
    db = dbRule.restartDatabase();
    // When
    Relationship relationship;
    try (Transaction tx = db.beginTx()) {
        node1 = db.getNodeById(node1.getId());
        node2 = db.getNodeById(node2.getId());
        relationship = node1.createRelationshipTo(node2, RelationshipType.withName("LIKE"));
        tx.success();
    }
    // Then
    assertThat(relationship.getId(), equalTo(0L));
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 90 with Relationship

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

the class IdReuseTest method relationshipIdReusableOnlyAfterTransactionFinish.

@Test
public void relationshipIdReusableOnlyAfterTransactionFinish() {
    Label testLabel = Label.label("testLabel");
    long relationshipId = createRelationship(testLabel);
    final IdController idMaintenanceController = getIdMaintenanceController();
    try (Transaction transaction = dbRule.beginTx();
        ResourceIterator<Node> nodes = dbRule.findNodes(testLabel)) {
        List<Node> nodeList = Iterators.asList(nodes);
        for (Node node : nodeList) {
            Iterable<Relationship> relationships = node.getRelationships(TestRelationshipType.MARKER);
            for (Relationship relationship : relationships) {
                relationship.delete();
            }
        }
        idMaintenanceController.maintenance();
        Node node1 = dbRule.createNode(testLabel);
        Node node2 = dbRule.createNode(testLabel);
        Relationship relationshipTo = node1.createRelationshipTo(node2, TestRelationshipType.MARKER);
        assertNotEquals("Relatioships should have different ids.", relationshipId, relationshipTo.getId());
        transaction.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Label(org.neo4j.graphdb.Label) IdController(org.neo4j.kernel.impl.storageengine.impl.recordstorage.id.IdController) 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