Search in sources :

Example 11 with Pair

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

the class IndexTransactionStateTestBase method shouldPerformStringContainsSearch.

@ParameterizedTest
@ValueSource(strings = { "true", "false" })
void shouldPerformStringContainsSearch(boolean needsValues) throws Exception {
    // given
    Set<Pair<Long, Value>> expected = new HashSet<>();
    try (KernelTransaction tx = beginTransaction()) {
        expected.add(entityWithProp(tx, "gnomebat"));
        entityWithPropId(tx, "fishwombat");
        tx.commit();
    }
    createIndex();
    // when
    try (KernelTransaction tx = beginTransaction()) {
        expected.add(entityWithProp(tx, "homeopatic"));
        entityWithPropId(tx, "telephonecompany");
        IndexDescriptor index = tx.schemaRead().indexGetForName(INDEX_NAME);
        int prop = tx.tokenRead().propertyKey(DEFAULT_PROPERTY_NAME);
        assertEntityAndValueForSeek(expected, tx, index, needsValues, "immense", PropertyIndexQuery.stringContains(prop, stringValue("me")));
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) Pair(org.neo4j.internal.helpers.collection.Pair) HashSet(java.util.HashSet) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 12 with Pair

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

the class SchemaProcedure method buildSchemaGraph.

public GraphResult buildSchemaGraph() {
    final Map<String, VirtualNodeHack> nodes = new HashMap<>();
    final Map<String, Set<VirtualRelationshipHack>> relationships = new HashMap<>();
    final KernelTransaction kernelTransaction = internalTransaction.kernelTransaction();
    AccessMode mode = kernelTransaction.securityContext().mode();
    try (KernelTransaction.Revertable ignore = kernelTransaction.overrideWith(SecurityContext.AUTH_DISABLED)) {
        Read dataRead = kernelTransaction.dataRead();
        TokenRead tokenRead = kernelTransaction.tokenRead();
        SchemaRead schemaRead = kernelTransaction.schemaRead();
        List<Pair<String, Integer>> labelNamesAndIds = new ArrayList<>();
        // Get all labels that are in use as seen by a super user
        List<Label> labelsInUse = stream(LABELS.inUse(kernelTransaction)).collect(Collectors.toList());
        for (Label label : labelsInUse) {
            String labelName = label.name();
            int labelId = tokenRead.nodeLabel(labelName);
            // Filter out labels that are denied or aren't explicitly allowed
            if (mode.allowsTraverseNode(labelId)) {
                labelNamesAndIds.add(Pair.of(labelName, labelId));
                Map<String, Object> properties = new HashMap<>();
                Iterator<IndexDescriptor> indexReferences = schemaRead.indexesGetForLabel(labelId);
                List<String> indexes = new ArrayList<>();
                while (indexReferences.hasNext()) {
                    IndexDescriptor index = indexReferences.next();
                    if (!index.isUnique()) {
                        String[] propertyNames = PropertyNameUtils.getPropertyKeys(tokenRead, index.schema().getPropertyIds());
                        indexes.add(String.join(",", propertyNames));
                    }
                }
                properties.put("indexes", indexes);
                Iterator<ConstraintDescriptor> nodePropertyConstraintIterator = schemaRead.constraintsGetForLabel(labelId);
                List<String> constraints = new ArrayList<>();
                while (nodePropertyConstraintIterator.hasNext()) {
                    ConstraintDescriptor constraint = nodePropertyConstraintIterator.next();
                    constraints.add(constraint.userDescription(tokenRead));
                }
                properties.put("constraints", constraints);
                getOrCreateLabel(label.name(), properties, nodes);
            }
        }
        // Get all relTypes that are in use as seen by a super user
        List<RelationshipType> relTypesInUse = stream(RELATIONSHIP_TYPES.inUse(kernelTransaction)).collect(Collectors.toList());
        for (RelationshipType relationshipType : relTypesInUse) {
            String relationshipTypeGetName = relationshipType.name();
            int relId = tokenRead.relationshipType(relationshipTypeGetName);
            // Filter out relTypes that are denied or aren't explicitly allowed
            if (mode.allowsTraverseRelType(relId)) {
                List<VirtualNodeHack> startNodes = new LinkedList<>();
                List<VirtualNodeHack> endNodes = new LinkedList<>();
                for (Pair<String, Integer> labelNameAndId : labelNamesAndIds) {
                    String labelName = labelNameAndId.first();
                    int labelId = labelNameAndId.other();
                    Map<String, Object> properties = new HashMap<>();
                    VirtualNodeHack node = getOrCreateLabel(labelName, properties, nodes);
                    if (dataRead.countsForRelationship(labelId, relId, TokenRead.ANY_LABEL) > 0) {
                        startNodes.add(node);
                    }
                    if (dataRead.countsForRelationship(TokenRead.ANY_LABEL, relId, labelId) > 0) {
                        endNodes.add(node);
                    }
                }
                for (VirtualNodeHack startNode : startNodes) {
                    for (VirtualNodeHack endNode : endNodes) {
                        addRelationship(startNode, endNode, relationshipTypeGetName, relationships);
                    }
                }
            }
        }
    }
    return getGraphResult(nodes, relationships);
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Label(org.neo4j.graphdb.Label) RelationshipType(org.neo4j.graphdb.RelationshipType) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) TokenRead(org.neo4j.internal.kernel.api.TokenRead) SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) Read(org.neo4j.internal.kernel.api.Read) Pair(org.neo4j.internal.helpers.collection.Pair) SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) LinkedList(java.util.LinkedList) TokenRead(org.neo4j.internal.kernel.api.TokenRead) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) AccessMode(org.neo4j.internal.kernel.api.security.AccessMode)

Example 13 with Pair

use of org.neo4j.internal.helpers.collection.Pair 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 14 with Pair

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

the class IndexOrderTestBase method assertCompositeResultsInOrder.

private void assertCompositeResultsInOrder(List<Pair<Long, Value[]>> expected, ENTITY_VALUE_INDEX_CURSOR cursor, IndexOrder indexOrder) {
    Comparator<Pair<Long, Value[]>> comparator = indexOrder == IndexOrder.ASCENDING ? (a, b) -> {
        int compare = Values.COMPARATOR.compare(a.other()[0], b.other()[0]);
        if (compare == 0) {
            return Values.COMPARATOR.compare(a.other()[1], b.other()[1]);
        }
        return compare;
    } : (a, b) -> {
        int compare = -Values.COMPARATOR.compare(a.other()[0], b.other()[0]);
        if (compare == 0) {
            return -Values.COMPARATOR.compare(a.other()[1], b.other()[1]);
        }
        return compare;
    };
    expected.sort(comparator);
    Iterator<Pair<Long, Value[]>> expectedRows = expected.iterator();
    while (cursor.next() && expectedRows.hasNext()) {
        Pair<Long, Value[]> expectedRow = expectedRows.next();
        assertThat(entityReference(cursor)).as(expectedRow.other()[0] + " == " + cursor.propertyValue(0) + " && " + expectedRow.other()[1] + " == " + cursor.propertyValue(1)).isEqualTo(expectedRow.first());
        for (int i = 0; i < cursor.numberOfProperties(); i++) {
            Value value = cursor.propertyValue(i);
            assertThat(value).isEqualTo(expectedRow.other()[i]);
        }
    }
    assertFalse(expectedRows.hasNext());
    assertFalse(cursor.next());
}
Also used : Value(org.neo4j.values.storable.Value) Values.pointValue(org.neo4j.values.storable.Values.pointValue) TextValue(org.neo4j.values.storable.TextValue) Values.stringValue(org.neo4j.values.storable.Values.stringValue) PointValue(org.neo4j.values.storable.PointValue) Pair(org.neo4j.internal.helpers.collection.Pair)

Example 15 with Pair

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

the class DbStructureCollector method lookup.

public DbStructureLookup lookup() {
    return new DbStructureLookup() {

        @Override
        public Iterator<Pair<Integer, String>> labels() {
            return labels.iterator();
        }

        @Override
        public Iterator<Pair<Integer, String>> properties() {
            return propertyKeys.iterator();
        }

        @Override
        public Iterator<Pair<Integer, String>> relationshipTypes() {
            return relationshipTypes.iterator();
        }

        @Override
        public Iterator<Pair<String[], String[]>> knownIndices() {
            return regularIndices.iterator();
        }

        @Override
        public Iterator<Pair<String[], String[]>> knownUniqueIndices() {
            return uniqueIndices.iterator();
        }

        @Override
        public Iterator<Pair<String, String[]>> knownUniqueConstraints() {
            return idsToNames(uniquenessConstraints);
        }

        @Override
        public Iterator<Pair<String, String[]>> knownNodePropertyExistenceConstraints() {
            return idsToNames(nodePropertyExistenceConstraints);
        }

        @Override
        public Iterator<Pair<String, String[]>> knownNodeKeyConstraints() {
            return idsToNames(nodeKeyConstraints);
        }

        @Override
        public long nodesAllCardinality() {
            return allNodesCount;
        }

        @Override
        public Iterator<Pair<String, String[]>> knownRelationshipPropertyExistenceConstraints() {
            return Iterators.map(relConstraint -> {
                String label = labels.byIdOrFail(relConstraint.schema().getRelTypeId());
                String[] propertyKeyNames = propertyKeys.byIdOrFail(relConstraint.schema().getPropertyIds());
                return Pair.of(label, propertyKeyNames);
            }, relPropertyExistenceConstraints.iterator());
        }

        @Override
        public long nodesWithLabelCardinality(int labelId) {
            return nodeCounts.getIfAbsent(labelId, 0L);
        }

        @Override
        public long cardinalityByLabelsAndRelationshipType(int fromLabelId, int relTypeId, int toLabelId) {
            RelSpecifier specifier = new RelSpecifier(fromLabelId, relTypeId, toLabelId);
            Long result = relCounts.get(specifier);
            return result == null ? 0L : result;
        }

        @Override
        public double indexUniqueValueSelectivity(int labelId, int... propertyKeyIds) {
            SchemaDescriptor descriptor = SchemaDescriptor.forLabel(labelId, propertyKeyIds);
            IndexStatistics result1 = regularIndices.getIndex(descriptor);
            IndexStatistics result2 = result1 == null ? uniqueIndices.getIndex(descriptor) : result1;
            return result2 == null ? Double.NaN : result2.uniqueValuesPercentage;
        }

        @Override
        public double indexPropertyExistsSelectivity(int labelId, int... propertyKeyIds) {
            SchemaDescriptor descriptor = SchemaDescriptor.forLabel(labelId, propertyKeyIds);
            IndexStatistics result1 = regularIndices.getIndex(descriptor);
            IndexStatistics result2 = result1 == null ? uniqueIndices.getIndex(descriptor) : result1;
            double indexSize = result2 == null ? Double.NaN : result2.size;
            return indexSize / nodesWithLabelCardinality(labelId);
        }

        private Iterator<Pair<String, String[]>> idsToNames(Iterable<? extends SchemaDescriptorSupplier> nodeConstraints) {
            return Iterators.map(nodeConstraint -> {
                String label = labels.byIdOrFail(nodeConstraint.schema().getLabelId());
                String[] propertyKeyNames = propertyKeys.byIdOrFail(nodeConstraint.schema().getPropertyIds());
                return Pair.of(label, propertyKeyNames);
            }, nodeConstraints.iterator());
        }
    };
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) SchemaDescriptorSupplier(org.neo4j.internal.schema.SchemaDescriptorSupplier) Pair(org.neo4j.internal.helpers.collection.Pair)

Aggregations

Pair (org.neo4j.internal.helpers.collection.Pair)39 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)22 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)22 ArrayList (java.util.ArrayList)15 HashSet (java.util.HashSet)12 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)12 EnumSource (org.junit.jupiter.params.provider.EnumSource)11 IndexReadSession (org.neo4j.internal.kernel.api.IndexReadSession)11 ValueSource (org.junit.jupiter.params.provider.ValueSource)9 TextValue (org.neo4j.values.storable.TextValue)9 PointValue (org.neo4j.values.storable.PointValue)8 Value (org.neo4j.values.storable.Value)8 Values.stringValue (org.neo4j.values.storable.Values.stringValue)8 Values.pointValue (org.neo4j.values.storable.Values.pointValue)7 Test (org.junit.jupiter.api.Test)6 HashMap (java.util.HashMap)5 Label (org.neo4j.graphdb.Label)5 Path (java.nio.file.Path)4 Transaction (org.neo4j.graphdb.Transaction)4 Map (java.util.Map)3