Search in sources :

Example 16 with TokenRead

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

the class CompositeCountsTest method countsForRelationship.

/**
 * @param start the label of the start node of relationships to get the number of, or {@code null} for "any".
 * @param type  the type of the relationships to get the number of, or {@code null} for "any".
 * @param end   the label of the end node of relationships to get the number of, or {@code null} for "any".
 */
private long countsForRelationship(Transaction tx, Label start, RelationshipType type, Label end) {
    KernelTransaction transaction = ((InternalTransaction) tx).kernelTransaction();
    TokenRead tokenRead = transaction.tokenRead();
    int startId;
    int typeId;
    int endId;
    // start
    if (start == null) {
        startId = ANY_LABEL;
    } else {
        if (TokenRead.NO_TOKEN == (startId = tokenRead.nodeLabel(start.name()))) {
            return 0;
        }
    }
    // type
    if (type == null) {
        typeId = TokenRead.NO_TOKEN;
    } else {
        if (TokenRead.NO_TOKEN == (typeId = tokenRead.relationshipType(type.name()))) {
            return 0;
        }
    }
    // end
    if (end == null) {
        endId = ANY_LABEL;
    } else {
        if (TokenRead.NO_TOKEN == (endId = tokenRead.nodeLabel(end.name()))) {
            return 0;
        }
    }
    return transaction.dataRead().countsForRelationship(startId, typeId, endId);
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) TokenRead(org.neo4j.internal.kernel.api.TokenRead)

Example 17 with TokenRead

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

the class RelationshipCountsTest method countsForRelationship.

/**
 * @param start the label of the start node of relationships to get the number of, or {@code null} for "any".
 * @param type  the type of the relationships to get the number of, or {@code null} for "any".
 * @param end   the label of the end node of relationships to get the number of, or {@code null} for "any".
 */
private long countsForRelationship(Transaction tx, Label start, RelationshipType type, Label end) {
    KernelTransaction ktx = ((InternalTransaction) tx).kernelTransaction();
    TokenRead tokenRead = ktx.tokenRead();
    int startId;
    int typeId;
    int endId;
    // start
    if (start == null) {
        startId = TokenRead.ANY_LABEL;
    } else {
        if (TokenRead.NO_TOKEN == (startId = tokenRead.nodeLabel(start.name()))) {
            return 0;
        }
    }
    // type
    if (type == null) {
        typeId = TokenRead.ANY_RELATIONSHIP_TYPE;
    } else {
        if (TokenRead.NO_TOKEN == (typeId = tokenRead.relationshipType(type.name()))) {
            return 0;
        }
    }
    // end
    if (end == null) {
        endId = TokenRead.ANY_LABEL;
    } else {
        if (TokenRead.NO_TOKEN == (endId = tokenRead.nodeLabel(end.name()))) {
            return 0;
        }
    }
    return ktx.dataRead().countsForRelationship(startId, typeId, endId);
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) TokenRead(org.neo4j.internal.kernel.api.TokenRead)

Example 18 with TokenRead

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

the class GraphDbStructureGuide method showRelCounts.

private static void showRelCounts(InternalTransaction transaction, DbStructureVisitor visitor) {
    // all wildcards
    KernelTransaction ktx = transaction.kernelTransaction();
    noSide(ktx, visitor, WILDCARD_REL_TYPE, ANY_RELATIONSHIP_TYPE);
    TokenRead tokenRead = ktx.tokenRead();
    // one label only
    for (Label label : transaction.getAllLabels()) {
        int labelId = tokenRead.nodeLabel(label.name());
        leftSide(ktx, visitor, label, labelId, WILDCARD_REL_TYPE, ANY_RELATIONSHIP_TYPE);
        rightSide(ktx, visitor, label, labelId, WILDCARD_REL_TYPE, ANY_RELATIONSHIP_TYPE);
    }
    // fixed rel type
    for (RelationshipType relType : transaction.getAllRelationshipTypes()) {
        int relTypeId = tokenRead.relationshipType(relType.name());
        noSide(ktx, visitor, relType, relTypeId);
        for (Label label : transaction.getAllLabels()) {
            int labelId = tokenRead.nodeLabel(label.name());
            // wildcard on right
            leftSide(ktx, visitor, label, labelId, relType, relTypeId);
            // wildcard on left
            rightSide(ktx, visitor, label, labelId, relType, relTypeId);
        }
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Label(org.neo4j.graphdb.Label) RelationshipType(org.neo4j.graphdb.RelationshipType) TokenRead(org.neo4j.internal.kernel.api.TokenRead)

Example 19 with TokenRead

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

the class GraphCountsSection method retrieve.

static Stream<RetrieveResult> retrieve(Kernel kernel, Anonymizer anonymizer) throws TransactionFailureException, IndexNotFoundKernelException {
    try (KernelTransaction tx = kernel.beginTransaction(KernelTransaction.Type.EXPLICIT, LoginContext.AUTH_DISABLED)) {
        TokenRead tokens = tx.tokenRead();
        Read read = tx.dataRead();
        Map<String, Object> data = new HashMap<>();
        data.put("nodes", nodeCounts(tokens, read, anonymizer));
        data.put("relationships", relationshipCounts(tokens, read, anonymizer));
        data.put("indexes", indexes(tokens, tx.schemaRead(), anonymizer));
        data.put("constraints", constraints(tokens, tx.schemaRead(), anonymizer));
        return Stream.of(new RetrieveResult(Sections.GRAPH_COUNTS, data));
    }
}
Also used : Read(org.neo4j.internal.kernel.api.Read) TokenRead(org.neo4j.internal.kernel.api.TokenRead) SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) HashMap(java.util.HashMap) TokenRead(org.neo4j.internal.kernel.api.TokenRead)

Example 20 with TokenRead

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

the class GraphCountsSection method constraints.

private static List<Map<String, Object>> constraints(TokenRead tokens, SchemaRead schemaRead, Anonymizer anonymizer) {
    List<Map<String, Object>> constraints = new ArrayList<>();
    Iterator<ConstraintDescriptor> iterator = schemaRead.constraintsGetAll();
    while (iterator.hasNext()) {
        ConstraintDescriptor constraint = iterator.next();
        EntityType entityType = constraint.schema().entityType();
        Map<String, Object> data = new HashMap<>();
        data.put("properties", map(constraint.schema().getPropertyIds(), id -> anonymizer.propertyKey(tokens.propertyKeyGetName(id), id)));
        data.put("type", constraintType(constraint));
        int entityTokenId = constraint.schema().getEntityTokenIds()[0];
        switch(entityType) {
            case NODE:
                data.put("label", anonymizer.label(tokens.labelGetName(entityTokenId), entityTokenId));
                constraints.add(data);
                break;
            case RELATIONSHIP:
                data.put("relationshipType", anonymizer.relationshipType(tokens.relationshipTypeGetName(entityTokenId), entityTokenId));
                constraints.add(data);
                break;
            default:
        }
    }
    return constraints;
}
Also used : EntityType(org.neo4j.common.EntityType) Arrays(java.util.Arrays) Iterator(java.util.Iterator) Read(org.neo4j.internal.kernel.api.Read) Iterators(org.neo4j.internal.helpers.collection.Iterators) IndexType(org.neo4j.internal.schema.IndexType) TokenRead(org.neo4j.internal.kernel.api.TokenRead) HashMap(java.util.HashMap) LoginContext(org.neo4j.internal.kernel.api.security.LoginContext) Kernel(org.neo4j.kernel.api.Kernel) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) Stream(java.util.stream.Stream) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) EntityType(org.neo4j.common.EntityType) Map(java.util.Map) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NamedToken(org.neo4j.token.api.NamedToken) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) TransactionFailureException(org.neo4j.internal.kernel.api.exceptions.TransactionFailureException) IntFunction(java.util.function.IntFunction) HashMap(java.util.HashMap) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

TokenRead (org.neo4j.internal.kernel.api.TokenRead)59 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)42 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)27 Test (org.junit.jupiter.api.Test)19 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)18 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)13 Transaction (org.neo4j.graphdb.Transaction)13 SchemaReadCore (org.neo4j.internal.kernel.api.SchemaReadCore)13 Label (org.neo4j.graphdb.Label)12 SchemaRead (org.neo4j.internal.kernel.api.SchemaRead)11 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)11 InternalIndexState (org.neo4j.internal.kernel.api.InternalIndexState)10 RelationshipType (org.neo4j.graphdb.RelationshipType)7 PropertyKeyIdNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException)6 Arrays (java.util.Arrays)5 List (java.util.List)5 Map (java.util.Map)5 Collectors (java.util.stream.Collectors)5 Stream (java.util.stream.Stream)5