Search in sources :

Example 1 with PrefetchingIterator

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

the class SchemaStorage method loadAllSchemaRules.

<ReturnType extends SchemaRule> Iterator<ReturnType> loadAllSchemaRules(final Predicate<ReturnType> predicate, final Class<ReturnType> returnType, final boolean ignoreMalformed) {
    return new PrefetchingIterator<ReturnType>() {

        private final long highestId = schemaStore.getHighestPossibleIdInUse();

        private long currentId = 1;

        /*record 0 contains the block size*/
        private final byte[] scratchData = newRecordBuffer();

        private final DynamicRecord record = schemaStore.newRecord();

        @Override
        protected ReturnType fetchNextOrNull() {
            while (currentId <= highestId) {
                long id = currentId++;
                schemaStore.getRecord(id, record, RecordLoad.FORCE);
                if (record.inUse() && record.isStartRecord()) {
                    try {
                        SchemaRule schemaRule = loadSingleSchemaRuleViaBuffer(id, scratchData);
                        if (returnType.isInstance(schemaRule)) {
                            ReturnType returnRule = returnType.cast(schemaRule);
                            if (predicate.test(returnRule)) {
                                return returnRule;
                            }
                        }
                    } catch (MalformedSchemaRuleException e) {
                        if (!ignoreMalformed) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
            return null;
        }
    };
}
Also used : DynamicRecord(org.neo4j.kernel.impl.store.record.DynamicRecord) PrefetchingIterator(org.neo4j.helpers.collection.PrefetchingIterator) MalformedSchemaRuleException(org.neo4j.kernel.api.exceptions.schema.MalformedSchemaRuleException) SchemaRule(org.neo4j.storageengine.api.schema.SchemaRule)

Example 2 with PrefetchingIterator

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

the class RecordAccessStub method rawPropertyChain.

@Override
public Iterator<PropertyRecord> rawPropertyChain(final long firstId) {
    return new PrefetchingIterator<PropertyRecord>() {

        private long next = firstId;

        @Override
        protected PropertyRecord fetchNextOrNull() {
            if (Record.NO_NEXT_PROPERTY.is(next)) {
                return null;
            }
            PropertyRecord record = reference(properties, next, Version.LATEST).record();
            next = record.getNextProp();
            return record;
        }
    };
}
Also used : PrefetchingIterator(org.neo4j.helpers.collection.PrefetchingIterator) PropertyRecord(org.neo4j.kernel.impl.store.record.PropertyRecord)

Example 3 with PrefetchingIterator

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

the class DirectRecordAccess method rawPropertyChain.

@Override
public Iterator<PropertyRecord> rawPropertyChain(final long firstId) {
    return new PrefetchingIterator<PropertyRecord>() {

        private long next = firstId;

        @Override
        protected PropertyRecord fetchNextOrNull() {
            if (Record.NO_NEXT_PROPERTY.is(next)) {
                return null;
            }
            PropertyRecord record = referenceTo(access.getPropertyStore(), next).record();
            next = record.getNextProp();
            return record;
        }
    };
}
Also used : PrefetchingIterator(org.neo4j.helpers.collection.PrefetchingIterator) PropertyRecord(org.neo4j.kernel.impl.store.record.PropertyRecord)

Example 4 with PrefetchingIterator

use of org.neo4j.helpers.collection.PrefetchingIterator in project graphdb by neo4j-attic.

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 5 with PrefetchingIterator

use of org.neo4j.helpers.collection.PrefetchingIterator 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)

Aggregations

PrefetchingIterator (org.neo4j.helpers.collection.PrefetchingIterator)11 IOException (java.io.IOException)4 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 URL (java.net.URL)2 ByteBuffer (java.nio.ByteBuffer)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 NoSuchElementException (java.util.NoSuchElementException)2 LiteDepthFirstSelector (org.neo4j.graphalgo.impl.util.LiteDepthFirstSelector)2 Node (org.neo4j.graphdb.Node)2 Path (org.neo4j.graphdb.Path)2 BranchOrderingPolicy (org.neo4j.graphdb.traversal.BranchOrderingPolicy)2 BranchSelector (org.neo4j.graphdb.traversal.BranchSelector)2 TraversalBranch (org.neo4j.graphdb.traversal.TraversalBranch)2 TraversalDescription (org.neo4j.graphdb.traversal.TraversalDescription)2 Traverser (org.neo4j.graphdb.traversal.Traverser)2 Predicate (org.neo4j.helpers.Predicate)2 IterableWrapper (org.neo4j.helpers.collection.IterableWrapper)2 NestingIterable (org.neo4j.helpers.collection.NestingIterable)2