Search in sources :

Example 71 with RelationshipType

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

the class NodeImpl method getSingleRelationship.

public Relationship getSingleRelationship(NodeManager nodeManager, RelationshipType type, Direction dir) {
    RelationshipType[] types = new RelationshipType[] { type };
    Iterator<Relationship> rels = new IntArrayIterator(getAllRelationshipsOfType(nodeManager, types), this, dir, nodeManager, types);
    if (!rels.hasNext()) {
        return null;
    }
    Relationship rel = rels.next();
    if (rels.hasNext()) {
        throw new NotFoundException("More than one relationship[" + type + ", " + dir + "] found for " + this);
    }
    return rel;
}
Also used : Relationship(org.neo4j.graphdb.Relationship) RelationshipType(org.neo4j.graphdb.RelationshipType) NotFoundException(org.neo4j.graphdb.NotFoundException)

Example 72 with RelationshipType

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

the class NodeManager method getMoreRelationships.

Pair<ArrayMap<String, RelIdArray>, Map<Long, RelationshipImpl>> getMoreRelationships(NodeImpl node) {
    long nodeId = node.getId();
    RelationshipChainPosition position = node.getRelChainPosition();
    Iterable<RelationshipData> rels = persistenceManager.getMoreRelationships(nodeId, position);
    ArrayMap<String, RelIdArray> newRelationshipMap = new ArrayMap<String, RelIdArray>();
    Map<Long, RelationshipImpl> relsMap = new HashMap<Long, RelationshipImpl>(150);
    for (RelationshipData rel : rels) {
        long relId = rel.getId();
        RelationshipImpl relImpl = relCache.get(relId);
        RelationshipType type = null;
        if (relImpl == null) {
            type = getRelationshipTypeById(rel.relationshipType());
            assert type != null;
            relImpl = new RelationshipImpl(relId, rel.firstNode(), rel.secondNode(), type, false);
            relsMap.put(relId, relImpl);
        // relCache.put( relId, relImpl );
        } else {
            type = relImpl.getType();
        }
        RelIdArray relationshipSet = newRelationshipMap.get(type.name());
        if (relationshipSet == null) {
            relationshipSet = new RelIdArray();
            newRelationshipMap.put(type.name(), relationshipSet);
        }
        relationshipSet.add(relId);
    }
    // relCache.putAll( relsMap );
    return Pair.of(newRelationshipMap, relsMap);
}
Also used : RelationshipChainPosition(org.neo4j.kernel.impl.nioneo.store.RelationshipChainPosition) HashMap(java.util.HashMap) RelationshipType(org.neo4j.graphdb.RelationshipType) ArrayMap(org.neo4j.kernel.impl.util.ArrayMap) RelationshipData(org.neo4j.kernel.impl.nioneo.store.RelationshipData) RelIdArray(org.neo4j.kernel.impl.util.RelIdArray)

Example 73 with RelationshipType

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

the class NodeManager method getRelationshipById.

public Relationship getRelationshipById(long relId) throws NotFoundException {
    RelationshipImpl relationship = relCache.get(relId);
    if (relationship != null) {
        return new RelationshipProxy(relId, this);
    }
    ReentrantLock loadLock = lockId(relId);
    try {
        relationship = relCache.get(relId);
        if (relationship != null) {
            return new RelationshipProxy(relId, this);
        }
        RelationshipData data = persistenceManager.loadLightRelationship(relId);
        if (data == null) {
            throw new NotFoundException("Relationship[" + relId + "]");
        }
        int typeId = data.relationshipType();
        RelationshipType type = getRelationshipTypeById(typeId);
        if (type == null) {
            throw new NotFoundException("Relationship[" + data.getId() + "] exist but relationship type[" + typeId + "] not found.");
        }
        final long startNodeId = data.firstNode();
        final long endNodeId = data.secondNode();
        relationship = new RelationshipImpl(relId, startNodeId, endNodeId, type, false);
        relCache.put(relId, relationship);
        return new RelationshipProxy(relId, this);
    } finally {
        loadLock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) RelationshipData(org.neo4j.kernel.impl.nioneo.store.RelationshipData) RelationshipType(org.neo4j.graphdb.RelationshipType) NotFoundException(org.neo4j.graphdb.NotFoundException)

Example 74 with RelationshipType

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

the class TestTraversal method testMultiRelDepthTraversal.

// Traverses the test "ise-tree-like" population depth first,
// but only traverses "ise" relationships (the population also contains
// "ise_clone" rels)
@Test
public void testMultiRelDepthTraversal() throws Exception {
    Node root = this.buildIseTreePopulation();
    RelationshipType[] traversableRels = new RelationshipType[] { MyRelTypes.TEST };
    Traverser traverser = root.traverse(DEPTH_FIRST, StopEvaluator.END_OF_GRAPH, ReturnableEvaluator.ALL, traversableRels[0], Direction.BOTH);
    try {
        assertNodes(traverser, "1", "2", "3", "4", "5", "6", "7", "10", "11", "12", "13");
        assertTrue("Too many nodes returned from traversal", traverser.iterator().hasNext() == false);
    } catch (java.util.NoSuchElementException nsee) {
        fail("Too few nodes returned from traversal");
    } finally {
        this.deleteNodeTreeRecursively(root, 0);
    }
}
Also used : Node(org.neo4j.graphdb.Node) Traverser(org.neo4j.graphdb.Traverser) RelationshipType(org.neo4j.graphdb.RelationshipType) Test(org.junit.Test)

Example 75 with RelationshipType

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

the class CircularGraphTest method testCircularBug.

@Test
public void testCircularBug() {
    final long timestamp = 3;
    Transaction tx = beginTx();
    getNodeWithName("2").setProperty("timestamp", 1L);
    getNodeWithName("3").setProperty("timestamp", 2L);
    tx.success();
    tx.finish();
    final RelationshipType type = DynamicRelationshipType.withName("TO");
    Traverser t = referenceNode().traverse(Order.DEPTH_FIRST, new StopEvaluator() {

        public boolean isStopNode(TraversalPosition position) {
            Relationship last = position.lastRelationshipTraversed();
            if (last != null && last.isType(type)) {
                Node node = position.currentNode();
                long currentTime = (Long) node.getProperty("timestamp");
                return currentTime >= timestamp;
            }
            return false;
        }
    }, new ReturnableEvaluator() {

        public boolean isReturnableNode(TraversalPosition position) {
            Relationship last = position.lastRelationshipTraversed();
            if (last != null && last.isType(type)) {
                return true;
            }
            return false;
        }
    }, type, Direction.OUTGOING);
    Iterator<Node> nodes = t.iterator();
    assertEquals("2", nodes.next().getProperty("name"));
    assertEquals("3", nodes.next().getProperty("name"));
    assertFalse(nodes.hasNext());
}
Also used : ReturnableEvaluator(org.neo4j.graphdb.ReturnableEvaluator) Transaction(org.neo4j.graphdb.Transaction) StopEvaluator(org.neo4j.graphdb.StopEvaluator) Traverser(org.neo4j.graphdb.Traverser) Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) RelationshipType(org.neo4j.graphdb.RelationshipType) DynamicRelationshipType(org.neo4j.graphdb.DynamicRelationshipType) TraversalPosition(org.neo4j.graphdb.TraversalPosition) Test(org.junit.Test)

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