Search in sources :

Example 1 with DirectionWrapper

use of org.neo4j.kernel.impl.util.RelIdArray.DirectionWrapper in project neo4j-mobile-android by neo4j-contrib.

the class ReadTransaction method getMoreRelationships.

static Pair<Map<DirectionWrapper, Iterable<RelationshipRecord>>, Long> getMoreRelationships(long nodeId, long position, int grabSize, RelationshipStore relStore) {
    // initialCapacity=grabSize saves the lists the trouble of resizing
    List<RelationshipRecord> out = new ArrayList<RelationshipRecord>();
    List<RelationshipRecord> in = new ArrayList<RelationshipRecord>();
    List<RelationshipRecord> loop = null;
    Map<DirectionWrapper, Iterable<RelationshipRecord>> result = new EnumMap<DirectionWrapper, Iterable<RelationshipRecord>>(DirectionWrapper.class);
    result.put(DirectionWrapper.OUTGOING, out);
    result.put(DirectionWrapper.INCOMING, in);
    for (int i = 0; i < grabSize && position != Record.NO_NEXT_RELATIONSHIP.intValue(); i++) {
        RelationshipRecord relRecord = relStore.getChainRecord(position);
        if (relRecord == null) {
            // return what we got so far
            return Pair.of(result, position);
        }
        long firstNode = relRecord.getFirstNode();
        long secondNode = relRecord.getSecondNode();
        if (relRecord.inUse()) {
            if (firstNode == secondNode) {
                if (loop == null) {
                    // This is done lazily because loops are probably quite
                    // rarely encountered
                    loop = new ArrayList<RelationshipRecord>();
                    result.put(DirectionWrapper.BOTH, loop);
                }
                loop.add(relRecord);
            } else if (firstNode == nodeId) {
                out.add(relRecord);
            } else if (secondNode == nodeId) {
                in.add(relRecord);
            }
        } else {
            i--;
        }
        if (firstNode == nodeId) {
            position = relRecord.getFirstNextRel();
        } else if (secondNode == nodeId) {
            position = relRecord.getSecondNextRel();
        } else {
            throw new InvalidRecordException("Node[" + nodeId + "] is neither firstNode[" + firstNode + "] nor secondNode[" + secondNode + "] for Relationship[" + relRecord.getId() + "]");
        }
    }
    return Pair.of(result, position);
}
Also used : DirectionWrapper(org.neo4j.kernel.impl.util.RelIdArray.DirectionWrapper) ArrayList(java.util.ArrayList) RelationshipRecord(org.neo4j.kernel.impl.nioneo.store.RelationshipRecord) EnumMap(java.util.EnumMap) InvalidRecordException(org.neo4j.kernel.impl.nioneo.store.InvalidRecordException)

Example 2 with DirectionWrapper

use of org.neo4j.kernel.impl.util.RelIdArray.DirectionWrapper in project neo4j-mobile-android by neo4j-contrib.

the class NodeImpl method getRelationships.

public Iterable<Relationship> getRelationships(NodeManager nodeManager, RelationshipType type, Direction dir) {
    RelationshipType[] types = new RelationshipType[] { type };
    DirectionWrapper direction = RelIdArray.wrap(dir);
    return new IntArrayIterator(getAllRelationshipsOfType(nodeManager, direction, types), this, direction, nodeManager, types, !hasMoreRelationshipsToLoad());
}
Also used : DirectionWrapper(org.neo4j.kernel.impl.util.RelIdArray.DirectionWrapper) RelationshipType(org.neo4j.graphdb.RelationshipType)

Example 3 with DirectionWrapper

use of org.neo4j.kernel.impl.util.RelIdArray.DirectionWrapper in project neo4j-mobile-android by neo4j-contrib.

the class NodeImpl method getSingleRelationship.

public Relationship getSingleRelationship(NodeManager nodeManager, RelationshipType type, Direction dir) {
    DirectionWrapper direction = RelIdArray.wrap(dir);
    RelationshipType[] types = new RelationshipType[] { type };
    Iterator<Relationship> rels = new IntArrayIterator(getAllRelationshipsOfType(nodeManager, direction, types), this, direction, nodeManager, types, !hasMoreRelationshipsToLoad());
    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 : DirectionWrapper(org.neo4j.kernel.impl.util.RelIdArray.DirectionWrapper) Relationship(org.neo4j.graphdb.Relationship) RelationshipType(org.neo4j.graphdb.RelationshipType) NotFoundException(org.neo4j.graphdb.NotFoundException)

Aggregations

DirectionWrapper (org.neo4j.kernel.impl.util.RelIdArray.DirectionWrapper)3 RelationshipType (org.neo4j.graphdb.RelationshipType)2 ArrayList (java.util.ArrayList)1 EnumMap (java.util.EnumMap)1 NotFoundException (org.neo4j.graphdb.NotFoundException)1 Relationship (org.neo4j.graphdb.Relationship)1 InvalidRecordException (org.neo4j.kernel.impl.nioneo.store.InvalidRecordException)1 RelationshipRecord (org.neo4j.kernel.impl.nioneo.store.RelationshipRecord)1