Search in sources :

Example 41 with IndexReadSession

use of org.neo4j.internal.kernel.api.IndexReadSession in project neo4j by neo4j.

the class AbstractIndexProvidedOrderTest method shouldProvideResultInOrderIfCapable.

@Test
void shouldProvideResultInOrderIfCapable() throws KernelException {
    int prop = token.propertyKey(PROPERTY_KEY);
    RandomValues randomValues = randomRule.randomValues();
    IndexReadSession index = read.indexReadSession(tx.schemaRead().indexGetForName(INDEX_NAME));
    for (int i = 0; i < N_ITERATIONS; i++) {
        ValueType type = randomValues.among(targetedTypes);
        IndexOrderCapability order = index.reference().getCapability().orderCapability(type.valueGroup.category());
        if (order.supportsAsc()) {
            expectResultInOrder(randomValues, type, prop, index, IndexOrder.ASCENDING);
        }
        if (order.supportsDesc()) {
            expectResultInOrder(randomValues, type, prop, index, IndexOrder.DESCENDING);
        }
    }
}
Also used : ValueType(org.neo4j.values.storable.ValueType) RandomValues(org.neo4j.values.storable.RandomValues) IndexOrderCapability(org.neo4j.internal.schema.IndexOrderCapability) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession) Test(org.junit.jupiter.api.Test)

Example 42 with IndexReadSession

use of org.neo4j.internal.kernel.api.IndexReadSession in project neo4j by neo4j.

the class DefaultPooledCursorsTestBase method shouldReuseFullAccessNodeValueIndexCursor.

@Test
void shouldReuseFullAccessNodeValueIndexCursor() throws Exception {
    int prop = token.propertyKey("prop");
    IndexDescriptor indexDescriptor = tx.schemaRead().indexGetForName(NODE_PROP_INDEX_NAME);
    Predicates.awaitEx(() -> tx.schemaRead().indexGetState(indexDescriptor) == ONLINE, 1, MINUTES);
    IndexReadSession indexSession = tx.dataRead().indexReadSession(indexDescriptor);
    NodeValueIndexCursor c1 = cursors.allocateFullAccessNodeValueIndexCursor(NULL, EmptyMemoryTracker.INSTANCE);
    read.nodeIndexSeek(indexSession, c1, IndexQueryConstraints.unconstrained(), PropertyIndexQuery.exact(prop, "zero"));
    c1.close();
    NodeValueIndexCursor c2 = cursors.allocateFullAccessNodeValueIndexCursor(NULL, EmptyMemoryTracker.INSTANCE);
    assertThat(c1).isSameAs(c2);
    c2.close();
}
Also used : NodeValueIndexCursor(org.neo4j.internal.kernel.api.NodeValueIndexCursor) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession) Test(org.junit.jupiter.api.Test)

Example 43 with IndexReadSession

use of org.neo4j.internal.kernel.api.IndexReadSession in project neo4j by neo4j.

the class IndexOrderTestBase method shouldNodeIndexScanInOrderWithStringInMemoryAndConcurrentUpdate.

@ParameterizedTest
@EnumSource(value = IndexOrder.class, names = { "ASCENDING", "DESCENDING" })
void shouldNodeIndexScanInOrderWithStringInMemoryAndConcurrentUpdate(IndexOrder indexOrder) throws Exception {
    String a = "a";
    String b = "b";
    String c = "c";
    createIndex();
    TextValue expectedFirst = indexOrder == IndexOrder.ASCENDING ? stringValue(a) : stringValue(c);
    TextValue expectedLast = indexOrder == IndexOrder.ASCENDING ? stringValue(c) : stringValue(a);
    try (KernelTransaction tx = beginTransaction()) {
        int prop = tx.tokenRead().propertyKey(DEFAULT_PROPERTY_NAME);
        entityWithProp(tx, a);
        entityWithProp(tx, c);
        IndexReadSession index = tx.dataRead().indexReadSession(tx.schemaRead().indexGetForName(INDEX_NAME));
        try (var cursor = getEntityValueIndexCursor(tx)) {
            PropertyIndexQuery query = PropertyIndexQuery.stringPrefix(prop, stringValue(""));
            entityIndexSeek(tx, index, cursor, constrained(indexOrder, true), query);
            assertTrue(cursor.next());
            assertThat(cursor.propertyValue(0)).isEqualTo(expectedFirst);
            assertTrue(cursor.next());
            assertThat(cursor.propertyValue(0)).isEqualTo(expectedLast);
            concurrentInsert(b);
            assertFalse(cursor.next(), () -> "Did not expect to find anything more but found " + cursor.propertyValue(0));
        }
        tx.commit();
    }
    // Verify we see all data in the end
    try (KernelTransaction tx = beginTransaction()) {
        int prop = tx.tokenRead().propertyKey(DEFAULT_PROPERTY_NAME);
        IndexReadSession index = tx.dataRead().indexReadSession(tx.schemaRead().indexGetForName(INDEX_NAME));
        try (var cursor = getEntityValueIndexCursor(tx)) {
            PropertyIndexQuery query = PropertyIndexQuery.stringPrefix(prop, stringValue(""));
            entityIndexSeek(tx, index, cursor, constrained(indexOrder, true), query);
            assertTrue(cursor.next());
            assertThat(cursor.propertyValue(0)).isEqualTo(expectedFirst);
            assertTrue(cursor.next());
            assertThat(cursor.propertyValue(0)).isEqualTo(stringValue(b));
            assertTrue(cursor.next());
            assertThat(cursor.propertyValue(0)).isEqualTo(expectedLast);
            assertFalse(cursor.next());
        }
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) PropertyIndexQuery(org.neo4j.internal.kernel.api.PropertyIndexQuery) TextValue(org.neo4j.values.storable.TextValue) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 44 with IndexReadSession

use of org.neo4j.internal.kernel.api.IndexReadSession in project neo4j by neo4j.

the class IndexOrderTestBase method shouldNodeIndexScanInOrderWithPointsWithinSameTile.

@ParameterizedTest
@EnumSource(value = IndexOrder.class, names = { "ASCENDING", "DESCENDING" })
void shouldNodeIndexScanInOrderWithPointsWithinSameTile(IndexOrder indexOrder) throws Exception {
    Config config = Config.defaults();
    IndexSpecificSpaceFillingCurveSettings indexSettings = IndexSpecificSpaceFillingCurveSettings.fromConfig(config);
    SpaceFillingCurve curve = indexSettings.forCrs(WGS84);
    // given
    // Many random points that all are close enough to each other to belong to the same tile on the space filling curve.
    int nbrOfValues = 10000;
    PointValue origin = Values.pointValue(WGS84, 0.0, 0.0);
    Long derivedValueForCenterPoint = curve.derivedValueFor(origin.coordinate());
    double[] centerPoint = curve.centerPointFor(derivedValueForCenterPoint);
    double xWidthMultiplier = curve.getTileWidth(0, curve.getMaxLevel()) / 2;
    double yWidthMultiplier = curve.getTileWidth(1, curve.getMaxLevel()) / 2;
    List<Pair<Long, Value>> expected = new ArrayList<>();
    try (KernelTransaction tx = beginTransaction()) {
        // NOTE: strings come after points in natural ascending sort order
        expected.add(entityWithProp(tx, "a"));
        expected.add(entityWithProp(tx, "b"));
        for (int i = 0; i < nbrOfValues / 8; i++) {
            double x1 = (random.nextDouble() * 2 - 1) * xWidthMultiplier;
            double x2 = (random.nextDouble() * 2 - 1) * xWidthMultiplier;
            double y1 = (random.nextDouble() * 2 - 1) * yWidthMultiplier;
            double y2 = (random.nextDouble() * 2 - 1) * yWidthMultiplier;
            expected.add(entityWithProp(tx, Values.pointValue(WGS84, centerPoint[0] + x1, centerPoint[1] + y1)));
            expected.add(entityWithProp(tx, Values.pointValue(WGS84, centerPoint[0] + x1, centerPoint[1] + y2)));
            expected.add(entityWithProp(tx, Values.pointValue(WGS84, centerPoint[0] + x2, centerPoint[1] + y1)));
            expected.add(entityWithProp(tx, Values.pointValue(WGS84, centerPoint[0] + x2, centerPoint[1] + y2)));
        }
        tx.commit();
    }
    createIndex();
    // when
    try (KernelTransaction tx = beginTransaction()) {
        IndexReadSession index = tx.dataRead().indexReadSession(tx.schemaRead().indexGetForName(INDEX_NAME));
        try (var cursor = getEntityValueIndexCursor(tx)) {
            for (int i = 0; i < nbrOfValues / 8; i++) {
                double x1 = (random.nextDouble() * 2 - 1) * xWidthMultiplier;
                double x2 = (random.nextDouble() * 2 - 1) * xWidthMultiplier;
                double y1 = (random.nextDouble() * 2 - 1) * yWidthMultiplier;
                double y2 = (random.nextDouble() * 2 - 1) * yWidthMultiplier;
                expected.add(entityWithProp(tx, Values.pointValue(WGS84, centerPoint[0] + x1, centerPoint[1] + y1)));
                expected.add(entityWithProp(tx, Values.pointValue(WGS84, centerPoint[0] + x1, centerPoint[1] + y2)));
                expected.add(entityWithProp(tx, Values.pointValue(WGS84, centerPoint[0] + x2, centerPoint[1] + y1)));
                expected.add(entityWithProp(tx, Values.pointValue(WGS84, centerPoint[0] + x2, centerPoint[1] + y2)));
            }
            expected.add(entityWithProp(tx, "c"));
            expected.add(entityWithProp(tx, "d"));
            entityIndexScan(tx, index, cursor, constrained(indexOrder, true));
            assertResultsInOrder(expected, cursor, indexOrder);
        }
    }
}
Also used : IndexSpecificSpaceFillingCurveSettings(org.neo4j.kernel.impl.index.schema.config.IndexSpecificSpaceFillingCurveSettings) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) PointValue(org.neo4j.values.storable.PointValue) Config(org.neo4j.configuration.Config) ArrayList(java.util.ArrayList) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession) SpaceFillingCurve(org.neo4j.gis.spatial.index.curves.SpaceFillingCurve) Pair(org.neo4j.internal.helpers.collection.Pair) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 45 with IndexReadSession

use of org.neo4j.internal.kernel.api.IndexReadSession in project neo4j by neo4j.

the class RelationshipIndexTransactionStateTest method assertEntityAndValueForScan.

@Override
void assertEntityAndValueForScan(Set<Pair<Long, Value>> expected, KernelTransaction tx, IndexDescriptor index, boolean needsValues, Object anotherValueFoundByQuery) throws Exception {
    IndexReadSession indexSession = tx.dataRead().indexReadSession(index);
    try (RelationshipValueIndexCursor relationships = tx.cursors().allocateRelationshipValueIndexCursor(tx.cursorContext(), tx.memoryTracker())) {
        tx.dataRead().relationshipIndexScan(indexSession, relationships, unordered(needsValues));
        assertEntityAndValue(expected, tx, needsValues, anotherValueFoundByQuery, new RelationshipCursorAdapter(relationships));
    }
}
Also used : RelationshipValueIndexCursor(org.neo4j.internal.kernel.api.RelationshipValueIndexCursor) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession)

Aggregations

IndexReadSession (org.neo4j.internal.kernel.api.IndexReadSession)93 Test (org.junit.jupiter.api.Test)62 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)47 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)22 NodeValueIndexCursor (org.neo4j.internal.kernel.api.NodeValueIndexCursor)20 MutableLongSet (org.eclipse.collections.api.set.primitive.MutableLongSet)17 LongHashSet (org.eclipse.collections.impl.set.mutable.primitive.LongHashSet)15 ArrayList (java.util.ArrayList)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 EnumSource (org.junit.jupiter.params.provider.EnumSource)12 RelationshipValueIndexCursor (org.neo4j.internal.kernel.api.RelationshipValueIndexCursor)12 IndexValueCapability (org.neo4j.internal.schema.IndexValueCapability)12 Pair (org.neo4j.internal.helpers.collection.Pair)11 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)10 Transaction (org.neo4j.graphdb.Transaction)9 IndexQueryConstraints (org.neo4j.internal.kernel.api.IndexQueryConstraints)9 IndexOrderCapability (org.neo4j.internal.schema.IndexOrderCapability)7 Read (org.neo4j.internal.kernel.api.Read)6 TokenRead (org.neo4j.internal.kernel.api.TokenRead)6 PropertyIndexQuery (org.neo4j.internal.kernel.api.PropertyIndexQuery)5