Search in sources :

Example 6 with IterableWrapper

use of org.neo4j.helpers.collection.IterableWrapper in project neo4j-mobile-android by neo4j-contrib.

the class Service method ourOwnLoader.

private static <T> Iterable<T> ourOwnLoader(final Class<T> type) {
    Collection<URL> urls = new HashSet<URL>();
    try {
        Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources("META-INF/services/" + type.getName());
        while (resources.hasMoreElements()) {
            urls.add(resources.nextElement());
        }
    } catch (IOException e) {
        return null;
    }
    return new NestingIterable<T, BufferedReader>(FilteringIterable.notNull(new IterableWrapper<BufferedReader, URL>(urls) {

        @Override
        protected BufferedReader underlyingObjectToObject(URL url) {
            try {
                return new BufferedReader(new InputStreamReader(url.openStream()));
            } catch (IOException e) {
                return null;
            }
        }
    })) {

        @Override
        protected Iterator<T> createNestedIterator(final BufferedReader input) {
            return new PrefetchingIterator<T>() {

                @Override
                protected T fetchNextOrNull() {
                    try {
                        String line;
                        while (null != (line = input.readLine())) {
                            try {
                                return type.cast(Class.forName(line).newInstance());
                            } catch (Exception e) {
                            } catch (LinkageError e) {
                            }
                        }
                        input.close();
                        return null;
                    } catch (IOException e) {
                        return null;
                    }
                }

                /* Finalizer - close the input stream.
                     * Prevent leakage of open files. Finalizers impact GC performance,
                     * but there are expected to be few of these objects.
                     */
                @Override
                protected void finalize() throws Throwable {
                    input.close();
                }
            };
        }
    };
}
Also used : PrefetchingIterator(org.neo4j.helpers.collection.PrefetchingIterator) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) IterableWrapper(org.neo4j.helpers.collection.IterableWrapper) NestingIterable(org.neo4j.helpers.collection.NestingIterable) URL(java.net.URL) IOException(java.io.IOException) NoSuchElementException(java.util.NoSuchElementException) BufferedReader(java.io.BufferedReader) HashSet(java.util.HashSet)

Example 7 with IterableWrapper

use of org.neo4j.helpers.collection.IterableWrapper in project neo4j-mobile-android by neo4j-contrib.

the class ShortestPath method getPaths.

private static Iterable<LinkedList<Relationship>> getPaths(Hit hit, DirectionData data) {
    LevelData levelData = data.visitedNodes.get(hit.connectingNode);
    if (levelData.depth == 0) {
        Collection<LinkedList<Relationship>> result = new ArrayList<LinkedList<Relationship>>();
        result.add(new LinkedList<Relationship>());
        return result;
    }
    Collection<PathData> set = new ArrayList<PathData>();
    GraphDatabaseService graphDb = data.startNode.getGraphDatabase();
    for (long rel : levelData.relsToHere) {
        set.add(new PathData(hit.connectingNode, new LinkedList<Relationship>(Arrays.asList(graphDb.getRelationshipById(rel)))));
    }
    for (int i = 0; i < levelData.depth - 1; i++) {
        // One level
        Collection<PathData> nextSet = new ArrayList<PathData>();
        for (PathData entry : set) {
            // One path...
            Node otherNode = entry.rels.getFirst().getOtherNode(entry.node);
            LevelData otherLevelData = data.visitedNodes.get(otherNode);
            int counter = 0;
            for (long rel : otherLevelData.relsToHere) {
                // ...may split into several paths
                LinkedList<Relationship> rels = ++counter == otherLevelData.relsToHere.length ? // lists being copied
                entry.rels : new LinkedList<Relationship>(entry.rels);
                rels.addFirst(graphDb.getRelationshipById(rel));
                nextSet.add(new PathData(otherNode, rels));
            }
        }
        set = nextSet;
    }
    return new IterableWrapper<LinkedList<Relationship>, PathData>(set) {

        @Override
        protected LinkedList<Relationship> underlyingObjectToObject(PathData object) {
            return object.rels;
        }
    };
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Node(org.neo4j.graphdb.Node) ArrayList(java.util.ArrayList) IterableWrapper(org.neo4j.helpers.collection.IterableWrapper) LinkedList(java.util.LinkedList) Relationship(org.neo4j.graphdb.Relationship)

Example 8 with IterableWrapper

use of org.neo4j.helpers.collection.IterableWrapper in project neo4j by neo4j.

the class RecordAccessStub method populateCache.

public void populateCache() {
    CacheTask action = new CacheTask.CacheNextRel(CheckStage.Stage3_NS_NextRel, cacheAccess, resourceIterable(new IterableWrapper<NodeRecord, Delta<NodeRecord>>(nodes.values()) {

        @Override
        protected NodeRecord underlyingObjectToObject(Delta<NodeRecord> node) {
            return node.newRecord;
        }
    }));
    action.run();
}
Also used : NodeRecord(org.neo4j.kernel.impl.store.record.NodeRecord) CacheTask(org.neo4j.consistency.checking.cache.CacheTask) IterableWrapper(org.neo4j.helpers.collection.IterableWrapper)

Example 9 with IterableWrapper

use of org.neo4j.helpers.collection.IterableWrapper in project neo4j by neo4j.

the class ShortestPath method getPaths.

private static Iterable<LinkedList<Relationship>> getPaths(Node connectingNode, DirectionData data, boolean stopAsap) {
    LevelData levelData = data.visitedNodes.get(connectingNode);
    if (levelData.depth == 0) {
        Collection<LinkedList<Relationship>> result = new ArrayList<>();
        result.add(new LinkedList<>());
        return result;
    }
    Collection<PathData> set = new ArrayList<>();
    GraphDatabaseService graphDb = data.startNode.getGraphDatabase();
    for (long rel : levelData.relsToHere) {
        set.add(new PathData(connectingNode, new LinkedList<>(Arrays.asList(graphDb.getRelationshipById(rel)))));
        if (stopAsap) {
            break;
        }
    }
    for (int i = 0; i < levelData.depth - 1; i++) {
        // One level
        Collection<PathData> nextSet = new ArrayList<>();
        for (PathData entry : set) {
            // One path...
            Node otherNode = entry.rels.getFirst().getOtherNode(entry.node);
            LevelData otherLevelData = data.visitedNodes.get(otherNode);
            int counter = 0;
            for (long rel : otherLevelData.relsToHere) {
                // ...may split into several paths
                LinkedList<Relationship> rels = ++counter == otherLevelData.relsToHere.length ? // lists being copied
                entry.rels : new LinkedList<>(entry.rels);
                rels.addFirst(graphDb.getRelationshipById(rel));
                nextSet.add(new PathData(otherNode, rels));
                if (stopAsap) {
                    break;
                }
            }
        }
        set = nextSet;
    }
    return new IterableWrapper<LinkedList<Relationship>, PathData>(set) {

        @Override
        protected LinkedList<Relationship> underlyingObjectToObject(PathData object) {
            return object.rels;
        }
    };
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Node(org.neo4j.graphdb.Node) ArrayList(java.util.ArrayList) IterableWrapper(org.neo4j.helpers.collection.IterableWrapper) LinkedList(java.util.LinkedList) Relationship(org.neo4j.graphdb.Relationship)

Example 10 with IterableWrapper

use of org.neo4j.helpers.collection.IterableWrapper in project neo4j by neo4j.

the class TestTraversalWithIterable method useTraverserInsideTraverser.

@Test
public void useTraverserInsideTraverser() throws Exception {
    /*
         * (a)-->(b)-->(c)
         *  |
         * \/
         * (d)-->(e)-->(f)
         *
         */
    createGraph("a FIRST d", "a TO b", "b TO c", "d TO e", "e TO f");
    try (Transaction tx = beginTx()) {
        TraversalDescription firstTraverser = getGraphDb().traversalDescription().relationships(RelationshipType.withName("FIRST")).evaluator(Evaluators.toDepth(1));
        final Iterable<Path> firstResult = firstTraverser.traverse(getNodeWithName("a"));
        Iterable<Node> startNodesForNestedTraversal = new IterableWrapper<Node, Path>(firstResult) {

            @Override
            protected Node underlyingObjectToObject(Path path) {
                return path.endNode();
            }
        };
        TraversalDescription nestedTraversal = getGraphDb().traversalDescription().evaluator(Evaluators.atDepth(2));
        expectPaths(nestedTraversal.traverse(startNodesForNestedTraversal), "a,b,c", "d,e,f");
        tx.success();
    }
}
Also used : Path(org.neo4j.graphdb.Path) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) IterableWrapper(org.neo4j.helpers.collection.IterableWrapper) Test(org.junit.Test)

Aggregations

IterableWrapper (org.neo4j.helpers.collection.IterableWrapper)11 Node (org.neo4j.graphdb.Node)6 Relationship (org.neo4j.graphdb.Relationship)4 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)3 ListRepresentation (org.neo4j.server.rest.repr.ListRepresentation)3 PathRepresentation (org.neo4j.server.rest.repr.PathRepresentation)3 WeightedPathRepresentation (org.neo4j.server.rest.repr.WeightedPathRepresentation)3 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 URL (java.net.URL)2 HashSet (java.util.HashSet)2 NoSuchElementException (java.util.NoSuchElementException)2 NotFoundException (org.neo4j.graphdb.NotFoundException)2 Path (org.neo4j.graphdb.Path)2 NestingIterable (org.neo4j.helpers.collection.NestingIterable)2 PrefetchingIterator (org.neo4j.helpers.collection.PrefetchingIterator)2 EndNodeNotFoundException (org.neo4j.server.rest.domain.EndNodeNotFoundException)2