Search in sources :

Example 1 with PrefetchingIterator

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

the class NativeAllEntriesReader method iterator.

@Override
public Iterator<Long> iterator() {
    KEY from = layout.newKey();
    from.initialize(Long.MIN_VALUE);
    from.initValuesAsLowest();
    KEY to = layout.newKey();
    to.initialize(Long.MAX_VALUE);
    to.initValuesAsHighest();
    try {
        closeSeeker();
        seeker = tree.seek(from, to, cursorContext);
        return new PrefetchingIterator<>() {

            @Override
            protected Long fetchNextOrNull() {
                do {
                    try {
                        if (!seeker.next()) {
                            return null;
                        }
                        long id = seeker.key().getEntityId();
                        if (id >= fromIdInclusive && id < toIdExclusive) {
                            return id;
                        }
                    } catch (IOException e) {
                        throw new UncheckedIOException(e);
                    }
                } while (true);
            }
        };
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : PrefetchingIterator(org.neo4j.internal.helpers.collection.PrefetchingIterator) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException)

Example 2 with PrefetchingIterator

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

the class SchemaStorage35 method loadAllSchemaRules.

/**
 * Scans the schema store and loads all {@link SchemaRule rules} in it. This method is written with the assumption
 * that there's no id reuse on schema records.
 *
 * @param returnType type of {@link SchemaRule} to load.
 * @return {@link Iterator} of the loaded schema rules, lazily loaded when advancing the iterator.
 */
private <ReturnType extends SchemaRule> Iterator<ReturnType> loadAllSchemaRules(final Class<ReturnType> returnType, CursorContext cursorContext, ThrowingConsumer<Exception, RuntimeException> malformedExceptionHandler) {
    return new PrefetchingIterator<>() {

        private final long highestId = schemaStore.getHighestPossibleIdInUse(cursorContext);

        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) {
                try {
                    long id = currentId++;
                    schemaStore.getRecord(id, record, RecordLoad.LENIENT_CHECK, cursorContext);
                    if (!record.inUse()) {
                        continue;
                    }
                    schemaStore.getRecord(id, record, RecordLoad.NORMAL, cursorContext);
                    if (record.isStartRecord()) {
                        // that we're reading and that rule may have spanned multiple dynamic records.
                        try {
                            Collection<DynamicRecord> records;
                            try {
                                records = schemaStore.getRecords(id, RecordLoad.NORMAL, false, cursorContext);
                            } catch (InvalidRecordException e) {
                                // This may have been due to a concurrent drop of this rule.
                                continue;
                            }
                            SchemaRule schemaRule = SchemaStore35.readSchemaRule(id, records, scratchData);
                            if (returnType.isInstance(schemaRule)) {
                                return returnType.cast(schemaRule);
                            }
                        } catch (MalformedSchemaRuleException e) {
                            throw new RuntimeException(e);
                        }
                    }
                } catch (Exception e) {
                    malformedExceptionHandler.accept(e);
                }
            }
            return null;
        }
    };
}
Also used : DynamicRecord(org.neo4j.kernel.impl.store.record.DynamicRecord) PrefetchingIterator(org.neo4j.internal.helpers.collection.PrefetchingIterator) MalformedSchemaRuleException(org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException) SchemaRule(org.neo4j.internal.schema.SchemaRule) InvalidRecordException(org.neo4j.kernel.impl.store.InvalidRecordException) InvalidRecordException(org.neo4j.kernel.impl.store.InvalidRecordException) MalformedSchemaRuleException(org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException)

Example 3 with PrefetchingIterator

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

the class RelationshipGroupCache method iterator.

/**
 * @return cached {@link RelationshipGroupRecord} sorted by node id and then type id.
 */
@Override
public Iterator<RelationshipGroupRecord> iterator() {
    return new PrefetchingIterator<>() {

        private long cursor;

        private long nodeId = fromNodeId;

        private int countLeftForThisNode = groupCount(nodeId);

        {
            findNextNodeWithGroupsIfNeeded();
        }

        @Override
        protected RelationshipGroupRecord fetchNextOrNull() {
            while (cursor < highCacheId) {
                RelationshipGroupRecord group = null;
                if (cache.getByte(cursor, 0) == 1) {
                    // Here we have an alive group
                    group = new RelationshipGroupRecord(-1).initialize(true, cache.get3ByteInt(cursor, 1), cache.get6ByteLong(cursor, 1 + 3), cache.get6ByteLong(cursor, 1 + 3 + 6), cache.get6ByteLong(cursor, 1 + 3 + 6 + 6), nodeId, // so this isn't at all "next" in the true sense of chain next.
                    countLeftForThisNode - 1);
                }
                cursor++;
                countLeftForThisNode--;
                findNextNodeWithGroupsIfNeeded();
                if (group != null) {
                    return group;
                }
            }
            return null;
        }

        private void findNextNodeWithGroupsIfNeeded() {
            if (countLeftForThisNode == 0) {
                do {
                    nodeId++;
                    countLeftForThisNode = nodeId >= groupCountCache.length() ? 0 : groupCount(nodeId);
                } while (countLeftForThisNode == 0 && nodeId < groupCountCache.length());
            }
        }
    };
}
Also used : PrefetchingIterator(org.neo4j.internal.helpers.collection.PrefetchingIterator) RelationshipGroupRecord(org.neo4j.kernel.impl.store.record.RelationshipGroupRecord)

Aggregations

PrefetchingIterator (org.neo4j.internal.helpers.collection.PrefetchingIterator)3 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 MalformedSchemaRuleException (org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException)1 SchemaRule (org.neo4j.internal.schema.SchemaRule)1 InvalidRecordException (org.neo4j.kernel.impl.store.InvalidRecordException)1 DynamicRecord (org.neo4j.kernel.impl.store.record.DynamicRecord)1 RelationshipGroupRecord (org.neo4j.kernel.impl.store.record.RelationshipGroupRecord)1