Search in sources :

Example 6 with PrefetchingIterator

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

the class EncodingIdMapperTest method shouldDetectCorrectDuplicateInputIdsWhereManyAccidentalInManyGroups.

@Test
public void shouldDetectCorrectDuplicateInputIdsWhereManyAccidentalInManyGroups() throws Exception {
    // GIVEN
    final ControlledEncoder encoder = new ControlledEncoder(new LongEncoder());
    IdMapper mapper = mapper(encoder, Radix.LONG, NO_MONITOR);
    final int idsPerGroup = 20, groups = 5;
    final AtomicReference<Group> group = new AtomicReference<>();
    InputIterable<Object> ids = SimpleInputIteratorWrapper.wrap("source", new Iterable<Object>() {

        @Override
        public Iterator<Object> iterator() {
            return new PrefetchingIterator<Object>() {

                private int i;

                @Override
                protected Object fetchNextOrNull() {
                    // Change group every <idsPerGroup> id
                    if (i % idsPerGroup == 0) {
                        int groupId = i / idsPerGroup;
                        if (groupId == groups) {
                            return null;
                        }
                        group.set(new Group.Adapter(groupId, "Group " + groupId));
                    }
                    try {
                        // i.e. all first 10% in each group collides with all other first 10% in each group
                        if (i % idsPerGroup < 2) {
                            // Let these colliding values encode into the same eId as well,
                            // so that they are definitely marked as collisions
                            encoder.useThisIdToEncodeNoMatterWhatComesIn(Long.valueOf(1234567));
                            return Long.valueOf(i % idsPerGroup);
                        }
                        // The other 90% will be accidental collisions for something else
                        encoder.useThisIdToEncodeNoMatterWhatComesIn(Long.valueOf(123456 - group.get().id()));
                        return Long.valueOf(i);
                    } finally {
                        i++;
                    }
                }
            };
        }
    });
    // WHEN
    long actualId = 0;
    for (Object id : ids) {
        mapper.put(id, actualId++, group.get());
    }
    Collector collector = mock(Collector.class);
    mapper.prepare(ids, collector, NONE);
    // THEN
    verifyNoMoreInteractions(collector);
    actualId = 0;
    for (Object id : ids) {
        assertEquals(actualId++, mapper.get(id, group.get()));
    }
}
Also used : Group(org.neo4j.unsafe.impl.batchimport.input.Group) IdMapper(org.neo4j.unsafe.impl.batchimport.cache.idmapping.IdMapper) AtomicReference(java.util.concurrent.atomic.AtomicReference) ResourceIterator(org.neo4j.graphdb.ResourceIterator) PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) SimpleInputIterator(org.neo4j.unsafe.impl.batchimport.input.SimpleInputIterator) PrefetchingIterator(org.neo4j.helpers.collection.PrefetchingIterator) InputIterator(org.neo4j.unsafe.impl.batchimport.InputIterator) Iterator(java.util.Iterator) Collector(org.neo4j.unsafe.impl.batchimport.input.Collector) Collectors.badCollector(org.neo4j.unsafe.impl.batchimport.input.Collectors.badCollector) Test(org.junit.Test)

Example 7 with PrefetchingIterator

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

the class LegacyNodeStoreReader method readNodeStore.

public Iterable<NodeRecord> readNodeStore() throws IOException {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(RECORD_LENGTH);
    return new Iterable<NodeRecord>() {

        @Override
        public Iterator<NodeRecord> iterator() {
            return new PrefetchingIterator<NodeRecord>() {

                long id = 0;

                @Override
                protected NodeRecord fetchNextOrNull() {
                    NodeRecord nodeRecord = null;
                    while (nodeRecord == null && id <= maxId) {
                        buffer.clear();
                        try {
                            fileChannel.read(buffer);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                        buffer.flip();
                        long inUseByte = buffer.get();
                        boolean inUse = (inUseByte & 0x1) == Record.IN_USE.intValue();
                        nodeRecord = new NodeRecord(id);
                        nodeRecord.setInUse(inUse);
                        if (inUse) {
                            long nextRel = LegacyStore.getUnsignedInt(buffer);
                            long nextProp = LegacyStore.getUnsignedInt(buffer);
                            long relModifier = (inUseByte & 0xEL) << 31;
                            long propModifier = (inUseByte & 0xF0L) << 28;
                            nodeRecord.setNextRel(LegacyStore.longFromIntAndMod(nextRel, relModifier));
                            nodeRecord.setNextProp(LegacyStore.longFromIntAndMod(nextProp, propModifier));
                        }
                        id++;
                    }
                    return nodeRecord;
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
}
Also used : NodeRecord(org.neo4j.kernel.impl.nioneo.store.NodeRecord) PrefetchingIterator(org.neo4j.helpers.collection.PrefetchingIterator) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 8 with PrefetchingIterator

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

the class ExactDepthPathFinder method paths.

private Iterator<Path> paths(final Node start, final Node end) {
    TraversalDescription base = Traversal.description().uniqueness(Uniqueness.RELATIONSHIP_PATH).order(new BranchOrderingPolicy() {

        public BranchSelector create(TraversalBranch startSource) {
            return new LiteDepthFirstSelector(startSource, startThreshold);
        }
    });
    final int firstHalf = onDepth / 2;
    Traverser startTraverser = base.prune(Traversal.pruneAfterDepth(firstHalf)).expand(expander).filter(new Predicate<Path>() {

        public boolean accept(Path item) {
            return item.length() == firstHalf;
        }
    }).traverse(start);
    final int secondHalf = onDepth - firstHalf;
    Traverser endTraverser = base.prune(Traversal.pruneAfterDepth(secondHalf)).expand(expander.reversed()).filter(new Predicate<Path>() {

        public boolean accept(Path item) {
            return item.length() == secondHalf;
        }
    }).traverse(end);
    final Iterator<Path> startIterator = startTraverser.iterator();
    final Iterator<Path> endIterator = endTraverser.iterator();
    final Map<Node, Visit> visits = new HashMap<Node, Visit>();
    return new PrefetchingIterator<Path>() {

        @Override
        protected Path fetchNextOrNull() {
            Path[] found = null;
            while (found == null && (startIterator.hasNext() || endIterator.hasNext())) {
                found = goOneStep(start, startIterator, visits);
                if (found == null) {
                    found = goOneStep(end, endIterator, visits);
                }
            }
            return found != null ? toPath(found, start) : null;
        }
    };
}
Also used : Path(org.neo4j.graphdb.Path) PrefetchingIterator(org.neo4j.helpers.collection.PrefetchingIterator) BranchOrderingPolicy(org.neo4j.graphdb.traversal.BranchOrderingPolicy) HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) BranchSelector(org.neo4j.graphdb.traversal.BranchSelector) Predicate(org.neo4j.helpers.Predicate) Traverser(org.neo4j.graphdb.traversal.Traverser) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) LiteDepthFirstSelector(org.neo4j.graphalgo.impl.util.LiteDepthFirstSelector) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch)

Example 9 with PrefetchingIterator

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

the class ExactDepthPathFinder method paths.

private Iterator<Path> paths(final Node start, final Node end) {
    TraversalDescription base = Traversal.description().uniqueness(Uniqueness.RELATIONSHIP_PATH).order(new BranchOrderingPolicy() {

        public BranchSelector create(TraversalBranch startSource) {
            return new LiteDepthFirstSelector(startSource, startThreshold);
        }
    });
    final int firstHalf = onDepth / 2;
    Traverser startTraverser = base.prune(Traversal.pruneAfterDepth(firstHalf)).expand(expander).filter(new Predicate<Path>() {

        public boolean accept(Path item) {
            return item.length() == firstHalf;
        }
    }).traverse(start);
    final int secondHalf = onDepth - firstHalf;
    Traverser endTraverser = base.prune(Traversal.pruneAfterDepth(secondHalf)).expand(expander.reversed()).filter(new Predicate<Path>() {

        public boolean accept(Path item) {
            return item.length() == secondHalf;
        }
    }).traverse(end);
    final Iterator<Path> startIterator = startTraverser.iterator();
    final Iterator<Path> endIterator = endTraverser.iterator();
    final Map<Node, Visit> visits = new HashMap<Node, Visit>();
    return new PrefetchingIterator<Path>() {

        @Override
        protected Path fetchNextOrNull() {
            Path[] found = null;
            while (found == null && (startIterator.hasNext() || endIterator.hasNext())) {
                found = goOneStep(start, startIterator, visits);
                if (found == null) {
                    found = goOneStep(end, endIterator, visits);
                }
            }
            return found != null ? toPath(found, start) : null;
        }
    };
}
Also used : Path(org.neo4j.graphdb.Path) PrefetchingIterator(org.neo4j.helpers.collection.PrefetchingIterator) BranchOrderingPolicy(org.neo4j.graphdb.traversal.BranchOrderingPolicy) HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) BranchSelector(org.neo4j.graphdb.traversal.BranchSelector) Predicate(org.neo4j.helpers.Predicate) Traverser(org.neo4j.graphdb.traversal.Traverser) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) LiteDepthFirstSelector(org.neo4j.graphalgo.impl.util.LiteDepthFirstSelector) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch)

Example 10 with PrefetchingIterator

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

the class LegacyRelationshipStoreReader method readRelationshipStore.

public Iterable<RelationshipRecord> readRelationshipStore() throws IOException {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(RECORD_LENGTH);
    return new Iterable<RelationshipRecord>() {

        @Override
        public Iterator<RelationshipRecord> iterator() {
            return new PrefetchingIterator<RelationshipRecord>() {

                long id = 0;

                @Override
                protected RelationshipRecord fetchNextOrNull() {
                    RelationshipRecord record = null;
                    while (record == null && id <= maxId) {
                        buffer.clear();
                        try {
                            fileChannel.read(buffer);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                        buffer.flip();
                        long inUseByte = buffer.get();
                        boolean inUse = (inUseByte & 0x1) == Record.IN_USE.intValue();
                        if (inUse) {
                            long firstNode = LegacyStore.getUnsignedInt(buffer);
                            long firstNodeMod = (inUseByte & 0xEL) << 31;
                            long secondNode = LegacyStore.getUnsignedInt(buffer);
                            // [ xxx,    ][    ,    ][    ,    ][    ,    ] second node high order bits,     0x70000000
                            // [    ,xxx ][    ,    ][    ,    ][    ,    ] first prev rel high order bits,  0xE000000
                            // [    ,   x][xx  ,    ][    ,    ][    ,    ] first next rel high order bits,  0x1C00000
                            // [    ,    ][  xx,x   ][    ,    ][    ,    ] second prev rel high order bits, 0x380000
                            // [    ,    ][    , xxx][    ,    ][    ,    ] second next rel high order bits, 0x70000
                            // [    ,    ][    ,    ][xxxx,xxxx][xxxx,xxxx] type
                            long typeInt = buffer.getInt();
                            long secondNodeMod = (typeInt & 0x70000000L) << 4;
                            int type = (int) (typeInt & 0xFFFF);
                            record = new RelationshipRecord(id, LegacyStore.longFromIntAndMod(firstNode, firstNodeMod), LegacyStore.longFromIntAndMod(secondNode, secondNodeMod), type);
                            record.setInUse(inUse);
                            long firstPrevRel = LegacyStore.getUnsignedInt(buffer);
                            long firstPrevRelMod = (typeInt & 0xE000000L) << 7;
                            record.setFirstPrevRel(LegacyStore.longFromIntAndMod(firstPrevRel, firstPrevRelMod));
                            long firstNextRel = LegacyStore.getUnsignedInt(buffer);
                            long firstNextRelMod = (typeInt & 0x1C00000L) << 10;
                            record.setFirstNextRel(LegacyStore.longFromIntAndMod(firstNextRel, firstNextRelMod));
                            long secondPrevRel = LegacyStore.getUnsignedInt(buffer);
                            long secondPrevRelMod = (typeInt & 0x380000L) << 13;
                            record.setSecondPrevRel(LegacyStore.longFromIntAndMod(secondPrevRel, secondPrevRelMod));
                            long secondNextRel = LegacyStore.getUnsignedInt(buffer);
                            long secondNextRelMod = (typeInt & 0x70000L) << 16;
                            record.setSecondNextRel(LegacyStore.longFromIntAndMod(secondNextRel, secondNextRelMod));
                            long nextProp = LegacyStore.getUnsignedInt(buffer);
                            long nextPropMod = (inUseByte & 0xF0L) << 28;
                            record.setNextProp(LegacyStore.longFromIntAndMod(nextProp, nextPropMod));
                        } else {
                            record = new RelationshipRecord(id, -1, -1, -1);
                            record.setInUse(false);
                        }
                        id++;
                    }
                    return record;
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
}
Also used : PrefetchingIterator(org.neo4j.helpers.collection.PrefetchingIterator) RelationshipRecord(org.neo4j.kernel.impl.nioneo.store.RelationshipRecord) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

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