Search in sources :

Example 6 with IndexSampler

use of org.neo4j.kernel.api.index.IndexSampler in project neo4j by neo4j.

the class LuceneSchemaIndexPopulationIT method partitionedIndexPopulation.

@ParameterizedTest
@ValueSource(ints = { 7, 11, 14, 20, 35, 58 })
void partitionedIndexPopulation(int affectedNodes) throws Exception {
    Path rootFolder = testDir.directory("partitionIndex" + affectedNodes).resolve("uniqueIndex" + affectedNodes);
    try (SchemaIndex uniqueIndex = LuceneSchemaIndexBuilder.create(descriptor, writable(), Config.defaults()).withFileSystem(fileSystem).withIndexRootFolder(rootFolder).build()) {
        uniqueIndex.open();
        // index is empty and not yet exist
        assertEquals(0, uniqueIndex.allDocumentsReader().maxCount());
        assertFalse(uniqueIndex.exists());
        try (LuceneIndexAccessor indexAccessor = new LuceneIndexAccessor(uniqueIndex, descriptor, SIMPLE_TOKEN_LOOKUP)) {
            generateUpdates(indexAccessor, affectedNodes);
            indexAccessor.force(NULL);
            // now index is online and should contain updates data
            assertTrue(uniqueIndex.isOnline());
            try (var indexReader = indexAccessor.newValueReader();
                NodeValueIterator results = new NodeValueIterator();
                IndexSampler indexSampler = indexReader.createSampler()) {
                indexReader.query(NULL_CONTEXT, results, unconstrained(), PropertyIndexQuery.exists(1));
                long[] nodes = PrimitiveLongCollections.asArray(results);
                assertEquals(affectedNodes, nodes.length);
                IndexSample sample = indexSampler.sampleIndex(NULL);
                assertEquals(affectedNodes, sample.indexSize());
                assertEquals(affectedNodes, sample.uniqueValues());
                assertEquals(affectedNodes, sample.sampleSize());
            }
        }
    }
}
Also used : Path(java.nio.file.Path) NodeValueIterator(org.neo4j.kernel.impl.index.schema.NodeValueIterator) LuceneIndexAccessor(org.neo4j.kernel.api.impl.schema.LuceneIndexAccessor) IndexSample(org.neo4j.kernel.api.index.IndexSample) SchemaIndex(org.neo4j.kernel.api.impl.schema.SchemaIndex) IndexSampler(org.neo4j.kernel.api.index.IndexSampler) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with IndexSampler

use of org.neo4j.kernel.api.index.IndexSampler in project neo4j by neo4j.

the class FusionIndexSampler method sampleIndex.

@Override
public IndexSample sampleIndex(CursorContext cursorContext) throws IndexNotFoundKernelException {
    List<IndexSample> samples = new ArrayList<>();
    Exception exception = null;
    for (IndexSampler sampler : samplers) {
        try {
            samples.add(sampler.sampleIndex(cursorContext));
        } catch (IndexNotFoundKernelException | RuntimeException e) {
            exception = Exceptions.chain(exception, e);
        }
    }
    if (exception != null) {
        throwIfUnchecked(exception);
        throwIfInstanceOf(exception, IndexNotFoundKernelException.class);
        throw new RuntimeException(exception);
    }
    return combineSamples(samples);
}
Also used : IndexSample(org.neo4j.kernel.api.index.IndexSample) ArrayList(java.util.ArrayList) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) IndexSampler(org.neo4j.kernel.api.index.IndexSampler)

Example 8 with IndexSampler

use of org.neo4j.kernel.api.index.IndexSampler in project neo4j by neo4j.

the class NativeIndexReader method createSampler.

@Override
public IndexSampler createSampler() {
    // For a unique index there's an optimization, knowing that all values in it are unique, to simply count
    // the number of indexed values and create a sample for that count. The GBPTree doesn't have an O(1)
    // count mechanism, it will have to manually count the indexed values in it to get it.
    // For that reason this implementation opts for keeping complexity down by just using the existing
    // non-unique sampler which scans the index and counts (potentially duplicates, of which there will
    // be none in a unique index).
    FullScanNonUniqueIndexSampler<KEY, VALUE> sampler = new FullScanNonUniqueIndexSampler<>(tree, layout);
    return tracer -> {
        try {
            return sampler.sample(tracer);
        } catch (UncheckedIOException e) {
            if (getRootCause(e) instanceof FileIsNotMappedException) {
                IndexNotFoundKernelException exception = new IndexNotFoundKernelException("Index dropped while sampling.");
                exception.addSuppressed(e);
                throw exception;
            }
            throw e;
        }
    };
}
Also used : IndexOrder(org.neo4j.internal.schema.IndexOrder) ValueIndexReader(org.neo4j.kernel.api.index.ValueIndexReader) Seeker(org.neo4j.index.internal.gbptree.Seeker) CursorContext(org.neo4j.io.pagecache.context.CursorContext) FileIsNotMappedException(org.neo4j.io.pagecache.impl.FileIsNotMappedException) ExceptionUtils.getRootCause(org.apache.commons.lang3.exception.ExceptionUtils.getRootCause) IndexQueryConstraints(org.neo4j.internal.kernel.api.IndexQueryConstraints) IOException(java.io.IOException) Value(org.neo4j.values.storable.Value) UncheckedIOException(java.io.UncheckedIOException) GBPTree(org.neo4j.index.internal.gbptree.GBPTree) IndexProgressor(org.neo4j.kernel.api.index.IndexProgressor) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) IndexSampler(org.neo4j.kernel.api.index.IndexSampler) PropertyIndexQuery(org.neo4j.internal.kernel.api.PropertyIndexQuery) QueryContext(org.neo4j.internal.kernel.api.QueryContext) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) NEUTRAL(org.neo4j.kernel.impl.index.schema.NativeIndexKey.Inclusion.NEUTRAL) FileIsNotMappedException(org.neo4j.io.pagecache.impl.FileIsNotMappedException) UncheckedIOException(java.io.UncheckedIOException) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException)

Example 9 with IndexSampler

use of org.neo4j.kernel.api.index.IndexSampler in project neo4j by neo4j.

the class OnlineIndexSamplingJob method run.

@Override
public void run() {
    try (DurationLogger durationLogger = new DurationLogger(log, "Sampling index " + indexUserDescription)) {
        try {
            try (var reader = indexProxy.newValueReader();
                var cursorContext = new CursorContext(pageCacheTracer.createPageCursorTracer(INDEX_SAMPLER_TAG));
                IndexSampler sampler = reader.createSampler()) {
                IndexSample sample = sampler.sampleIndex(cursorContext);
                // check again if the index is online before saving the counts in the store
                if (indexProxy.getState() == ONLINE) {
                    indexStatisticsStore.replaceStats(indexId, sample);
                    durationLogger.markAsFinished();
                    log.debug(format("Sampled index %s with %d unique values in sample of avg size %d taken from " + "index containing %d entries", indexUserDescription, sample.uniqueValues(), sample.sampleSize(), sample.indexSize()));
                } else {
                    durationLogger.markAsAborted("Index no longer ONLINE");
                }
            }
        } catch (IndexNotFoundKernelException e) {
            durationLogger.markAsAborted("Attempted to sample missing/already deleted index " + indexUserDescription);
        }
    }
}
Also used : DurationLogger(org.neo4j.kernel.impl.util.DurationLogger) IndexSample(org.neo4j.kernel.api.index.IndexSample) CursorContext(org.neo4j.io.pagecache.context.CursorContext) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) IndexSampler(org.neo4j.kernel.api.index.IndexSampler)

Aggregations

IndexSampler (org.neo4j.kernel.api.index.IndexSampler)9 IndexSample (org.neo4j.kernel.api.index.IndexSample)7 IndexNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException)4 Test (org.junit.jupiter.api.Test)3 CursorContext (org.neo4j.io.pagecache.context.CursorContext)3 ArrayList (java.util.ArrayList)2 ValueIndexReader (org.neo4j.kernel.api.index.ValueIndexReader)2 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 Path (java.nio.file.Path)1 Future (java.util.concurrent.Future)1 ExceptionUtils.getRootCause (org.apache.commons.lang3.exception.ExceptionUtils.getRootCause)1 Test (org.junit.Test)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 ValueSource (org.junit.jupiter.params.provider.ValueSource)1 IOFunction (org.neo4j.function.IOFunction)1 GBPTree (org.neo4j.index.internal.gbptree.GBPTree)1 Seeker (org.neo4j.index.internal.gbptree.Seeker)1 IndexQueryConstraints (org.neo4j.internal.kernel.api.IndexQueryConstraints)1