Search in sources :

Example 1 with CountsEntry

use of org.neo4j.consistency.store.synthetic.CountsEntry in project neo4j by neo4j.

the class CountsBuilderDecorator method checkCounts.

public void checkCounts(CountsAccessor counts, final ConsistencyReporter reporter, ProgressMonitorFactory progressFactory) {
    final int nodes = nodeCounts.uniqueSize();
    final int relationships = relationshipCounts.uniqueSize();
    final int total = nodes + relationships;
    final AtomicInteger nodeEntries = new AtomicInteger(0);
    final AtomicInteger relationshipEntries = new AtomicInteger(0);
    final ProgressListener listener = progressFactory.singlePart("Checking node and relationship counts", total);
    listener.started();
    counts.accept(new CountsVisitor.Adapter() {

        @Override
        public void visitNodeCount(int labelId, long count) {
            nodeEntries.incrementAndGet();
            reporter.forCounts(new CountsEntry(nodeKey(labelId), count), CHECK_NODE_COUNT);
            listener.add(1);
        }

        @Override
        public void visitRelationshipCount(int startLabelId, int relTypeId, int endLabelId, long count) {
            relationshipEntries.incrementAndGet();
            reporter.forCounts(new CountsEntry(relationshipKey(startLabelId, relTypeId, endLabelId), count), CHECK_RELATIONSHIP_COUNT);
            listener.add(1);
        }
    });
    reporter.forCounts(new CountsEntry(nodeKey(WILDCARD), nodeEntries.get()), CHECK_NODE_KEY_COUNT);
    reporter.forCounts(new CountsEntry(relationshipKey(WILDCARD, WILDCARD, WILDCARD), relationshipEntries.get()), CHECK_RELATIONSHIP_KEY_COUNT);
    listener.done();
}
Also used : CountsVisitor(org.neo4j.kernel.impl.api.CountsVisitor) ProgressListener(org.neo4j.helpers.progress.ProgressListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountsEntry(org.neo4j.consistency.store.synthetic.CountsEntry)

Example 2 with CountsEntry

use of org.neo4j.consistency.store.synthetic.CountsEntry in project neo4j by neo4j.

the class CountsState method checker.

CountsChecker checker(ConsistencyReporter reporter) {
    return new CountsChecker() {

        final RelationshipCounter relationshipCounter = instantiateRelationshipCounter();

        @Override
        public void visitNodeCount(int labelId, long count) {
            if (isValidLabelId(labelId)) {
                long pos = labelIdArrayPos(labelId);
                long expected = unmarkCountVisited(nodeCounts.get(pos));
                if (expected != count) {
                    reporter.forCounts(new CountsEntry(nodeKey(labelId), count)).inconsistentNodeCount(expected);
                }
                nodeCounts.set(pos, markCountVisited(expected));
            } else {
                AtomicLong expected = nodeCountsStray.remove(nodeKey(labelId));
                if (expected != null) {
                    if (expected.longValue() != count) {
                        reporter.forCounts(new CountsEntry(nodeKey(labelId), count)).inconsistentNodeCount(expected.longValue());
                    }
                } else {
                    reporter.forCounts(new CountsEntry(nodeKey(labelId), count)).inconsistentNodeCount(0);
                }
            }
        }

        @Override
        public void visitRelationshipCount(int startLabelId, int relTypeId, int endLabelId, long count) {
            CountsKey countsKey = relationshipKey(startLabelId, relTypeId, endLabelId);
            if (relationshipCounter.isValid(startLabelId, relTypeId, endLabelId)) {
                long expected = unmarkCountVisited(relationshipCounter.get(startLabelId, relTypeId, endLabelId));
                if (expected != count) {
                    reporter.forCounts(new CountsEntry(countsKey, count)).inconsistentRelationshipCount(expected);
                }
                relationshipCounter.set(startLabelId, relTypeId, endLabelId, markCountVisited(expected));
            } else {
                AtomicLong expected = relationshipCountsStray.remove(countsKey);
                if (expected != null) {
                    if (expected.longValue() != count) {
                        reporter.forCounts(new CountsEntry(countsKey, count)).inconsistentRelationshipCount(expected.longValue());
                    }
                } else {
                    reporter.forCounts(new CountsEntry(countsKey, count)).inconsistentRelationshipCount(0);
                }
            }
        }

        @Override
        public void close() {
            // Report counts that have been collected in this consistency check, but aren't in the existing store
            for (int labelId = 0; labelId < highLabelId; labelId++) {
                long count = nodeCounts.get(labelId);
                if (!hasVisitedCountMark(count) && count > 0) {
                    reporter.forCounts(new CountsEntry(nodeKey(labelId), 0)).inconsistentNodeCount(count);
                }
            }
            for (int start = ANY_LABEL; start < highLabelId; start++) {
                for (int end = ANY_LABEL; end < highLabelId; end++) {
                    for (int type = ANY_RELATIONSHIP_TYPE; type < highRelationshipTypeId; type++) {
                        if (// we only keep counts for where at least one of start/end is ANY
                        start == ANY_LABEL || end == ANY_LABEL) {
                            long count = relationshipCounter.get(start, type, end);
                            if (!hasVisitedCountMark(count) && count > 0) {
                                reporter.forCounts(new CountsEntry(relationshipKey(start, type, end), 0)).inconsistentRelationshipCount(count);
                            }
                        }
                    }
                }
            }
            nodeCountsStray.forEach((countsKey, count) -> reporter.forCounts(new CountsEntry(countsKey, count.get())));
            relationshipCountsStray.forEach((countsKey, count) -> reporter.forCounts(new CountsEntry(countsKey, count.get())));
        }
    };
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) RelationshipCounter(org.neo4j.internal.recordstorage.RelationshipCounter) CountsEntry(org.neo4j.consistency.store.synthetic.CountsEntry) CountsKey(org.neo4j.internal.counts.CountsKey)

Aggregations

CountsEntry (org.neo4j.consistency.store.synthetic.CountsEntry)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ProgressListener (org.neo4j.helpers.progress.ProgressListener)1 CountsKey (org.neo4j.internal.counts.CountsKey)1 RelationshipCounter (org.neo4j.internal.recordstorage.RelationshipCounter)1 CountsVisitor (org.neo4j.kernel.impl.api.CountsVisitor)1