Search in sources :

Example 91 with RelationshipType

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

the class TestNeo4jCacheAndPersistence method testLowGrabSize.

@Test
public void testLowGrabSize() {
    Map<String, String> config = new HashMap<>();
    config.put("relationship_grab_size", "1");
    File storeDir = getStorePath("neo2");
    deleteFileOrDirectory(storeDir);
    GraphDatabaseService graphDb = new TestGraphDatabaseFactory().newImpermanentDatabaseBuilder().setConfig(config).newGraphDatabase();
    Node node1, node2;
    try (Transaction tx = graphDb.beginTx()) {
        node1 = graphDb.createNode();
        node2 = graphDb.createNode();
        node1.createRelationshipTo(node2, MyRelTypes.TEST);
        node2.createRelationshipTo(node1, MyRelTypes.TEST2);
        node1.createRelationshipTo(node2, MyRelTypes.TEST_TRAVERSAL);
        tx.success();
    }
    try (Transaction tx = graphDb.beginTx()) {
        RelationshipType[] types = new RelationshipType[] { MyRelTypes.TEST, MyRelTypes.TEST2, MyRelTypes.TEST_TRAVERSAL };
        assertEquals(3, Iterables.count(node1.getRelationships(types)));
        assertEquals(3, Iterables.count(node1.getRelationships()));
        assertEquals(3, Iterables.count(node2.getRelationships(types)));
        assertEquals(3, Iterables.count(node2.getRelationships()));
        assertEquals(2, Iterables.count(node1.getRelationships(OUTGOING)));
        assertEquals(1, Iterables.count(node1.getRelationships(INCOMING)));
        assertEquals(1, Iterables.count(node2.getRelationships(OUTGOING)));
        assertEquals(2, Iterables.count(node2.getRelationships(INCOMING)));
        tx.success();
    }
    graphDb.shutdown();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) RelationshipType(org.neo4j.graphdb.RelationshipType) File(java.io.File) Test(org.junit.Test)

Example 92 with RelationshipType

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

the class TestReadOnlyNeo4j method createSomeData.

private DbRepresentation createSomeData() {
    RelationshipType type = withName("KNOWS");
    GraphDatabaseService db = new TestGraphDatabaseFactory().setFileSystem(new UncloseableDelegatingFileSystemAbstraction(fs.get())).newImpermanentDatabase(PATH);
    try (Transaction tx = db.beginTx()) {
        Node prevNode = db.createNode();
        for (int i = 0; i < 100; i++) {
            Node node = db.createNode();
            Relationship rel = prevNode.createRelationshipTo(node, type);
            node.setProperty("someKey" + i % 10, i % 15);
            rel.setProperty("since", System.currentTimeMillis());
        }
        tx.success();
    }
    DbRepresentation result = DbRepresentation.of(db);
    db.shutdown();
    return result;
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) RelationshipType(org.neo4j.graphdb.RelationshipType) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) UncloseableDelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.UncloseableDelegatingFileSystemAbstraction) DbRepresentation(org.neo4j.test.DbRepresentation)

Example 93 with RelationshipType

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

the class IdGeneratorTest method commandsGetWrittenOnceSoThatFreedIdsGetsAddedOnlyOnce.

@Test
public void commandsGetWrittenOnceSoThatFreedIdsGetsAddedOnlyOnce() throws Exception {
    File storeDir = new File("target/var/free-id-once");
    deleteRecursively(storeDir);
    GraphDatabaseService db = createTestDatabase(storeDir);
    RelationshipType type = withName("SOME_TYPE");
    // This transaction will, if some commands may be executed more than
    // once,
    // add the freed ids to the defrag list more than once - making the id
    // generator
    // return the same id more than once during the next session.
    Set<Long> createdNodeIds = new HashSet<>();
    Set<Long> createdRelationshipIds = new HashSet<>();
    Transaction tx = db.beginTx();
    Node commonNode = db.createNode();
    for (int i = 0; i < 20; i++) {
        Node otherNode = db.createNode();
        Relationship relationship = commonNode.createRelationshipTo(otherNode, type);
        if (i % 5 == 0) {
            otherNode.delete();
            relationship.delete();
        } else {
            createdNodeIds.add(otherNode.getId());
            createdRelationshipIds.add(relationship.getId());
        }
    }
    tx.success();
    tx.close();
    db.shutdown();
    // After a clean shutdown, create new nodes and relationships and see so
    // that
    // all ids are unique.
    db = createTestDatabase(storeDir);
    tx = db.beginTx();
    commonNode = db.getNodeById(commonNode.getId());
    for (int i = 0; i < 100; i++) {
        Node otherNode = db.createNode();
        if (!createdNodeIds.add(otherNode.getId())) {
            fail("Managed to create a node with an id that was already in use");
        }
        Relationship relationship = commonNode.createRelationshipTo(otherNode, type);
        if (!createdRelationshipIds.add(relationship.getId())) {
            fail("Managed to create a relationship with an id that was already in use");
        }
    }
    tx.success();
    tx.close();
    // Verify by loading everything from scratch
    tx = db.beginTx();
    for (Node node : db.getAllNodes()) {
        Iterables.lastOrNull(node.getRelationships());
    }
    tx.close();
    db.shutdown();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) RelationshipType(org.neo4j.graphdb.RelationshipType) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 94 with RelationshipType

use of org.neo4j.graphdb.RelationshipType in project qi4j-sdk by Qi4j.

the class NeoEntityState method namedAssociationValueOf.

@Override
public NamedAssociationState namedAssociationValueOf(QualifiedName stateName) {
    RelationshipType namedAssociation = namedAssociation(stateName);
    Relationship rel = underlyingNode.getSingleRelationship(namedAssociation, Direction.OUTGOING);
    if (rel != null) {
        return new NeoNamedAssociationState(uow, this, rel.getEndNode());
    }
    Node node = uow.getNeo().createNode();
    node.setProperty(NeoNamedAssociationState.COUNT, 0);
    underlyingNode.createRelationshipTo(node, namedAssociation);
    return new NeoNamedAssociationState(uow, this, node);
}
Also used : Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) RelationshipType(org.neo4j.graphdb.RelationshipType) DynamicRelationshipType(org.neo4j.graphdb.DynamicRelationshipType)

Example 95 with RelationshipType

use of org.neo4j.graphdb.RelationshipType in project qi4j-sdk by Qi4j.

the class NeoEntityState method manyAssociationValueOf.

@Override
public ManyAssociationState manyAssociationValueOf(QualifiedName stateName) {
    RelationshipType manyAssociation = manyAssociation(stateName);
    Relationship rel = underlyingNode.getSingleRelationship(manyAssociation, Direction.OUTGOING);
    if (rel != null) {
        return new NeoManyAssociationState(uow, this, rel.getEndNode());
    }
    Node node = uow.getNeo().createNode();
    node.setProperty(NeoManyAssociationState.COUNT, 0);
    underlyingNode.createRelationshipTo(node, manyAssociation);
    return new NeoManyAssociationState(uow, this, node);
}
Also used : Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) RelationshipType(org.neo4j.graphdb.RelationshipType) DynamicRelationshipType(org.neo4j.graphdb.DynamicRelationshipType)

Aggregations

RelationshipType (org.neo4j.graphdb.RelationshipType)97 Node (org.neo4j.graphdb.Node)53 Test (org.junit.Test)45 Relationship (org.neo4j.graphdb.Relationship)38 Transaction (org.neo4j.graphdb.Transaction)18 Direction (org.neo4j.graphdb.Direction)15 Traverser (org.neo4j.graphdb.Traverser)10 NotFoundException (org.neo4j.graphdb.NotFoundException)9 DynamicRelationshipType (org.neo4j.graphdb.DynamicRelationshipType)7 Label (org.neo4j.graphdb.Label)7 RelationshipRecord (org.neo4j.kernel.impl.nioneo.store.RelationshipRecord)7 Collection (java.util.Collection)6 HashSet (java.util.HashSet)6 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)6 Path (org.neo4j.graphdb.Path)6 StopEvaluator (org.neo4j.graphdb.StopEvaluator)6 TraversalPosition (org.neo4j.graphdb.TraversalPosition)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 LinkedList (java.util.LinkedList)4