Search in sources :

Example 11 with TokenRead

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

the class BuiltInProcedures method schemaStatements.

@Deprecated(since = "4.2.0", forRemoval = true)
@SystemProcedure
@Description("List all statements for creating and dropping existing indexes and constraints. " + "Note that only index types introduced before Neo4j 4.3 are included.")
@Procedure(name = "db.schemaStatements", mode = READ, deprecatedBy = "SHOW INDEXES YIELD * command and SHOW CONSTRAINTS YIELD * command")
public Stream<SchemaStatementResult> schemaStatements() throws ProcedureException {
    if (callContext.isSystemDatabase()) {
        return Stream.empty();
    }
    SchemaReadCore schemaRead = kernelTransaction.schemaRead().snapshot();
    final TokenRead tokenRead = kernelTransaction.tokenRead();
    return SchemaStatementProcedure.createSchemaStatementResults(schemaRead, tokenRead).stream();
}
Also used : SchemaReadCore(org.neo4j.internal.kernel.api.SchemaReadCore) TokenRead(org.neo4j.internal.kernel.api.TokenRead) Description(org.neo4j.procedure.Description) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) Procedure(org.neo4j.procedure.Procedure)

Example 12 with TokenRead

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

the class BuiltInProcedures method listRelationshipTypes.

@SystemProcedure
@Description("List all available relationship types in the database.")
@Procedure(name = "db.relationshipTypes", mode = READ)
public Stream<RelationshipTypeResult> listRelationshipTypes() {
    if (callContext.isSystemDatabase()) {
        return Stream.empty();
    }
    AccessMode mode = kernelTransaction.securityContext().mode();
    TokenRead tokenRead = kernelTransaction.tokenRead();
    List<RelationshipTypeResult> relTypesInUse;
    try (KernelTransaction.Revertable ignore = kernelTransaction.overrideWith(SecurityContext.AUTH_DISABLED)) {
        // Get all relTypes that are in use as seen by a super user
        relTypesInUse = stream(RELATIONSHIP_TYPES.inUse(kernelTransaction)).filter(type -> mode.allowsTraverseRelType(tokenRead.relationshipType(type.name()))).map(RelationshipTypeResult::new).collect(Collectors.toList());
    }
    return relTypesInUse.stream();
}
Also used : Mode(org.neo4j.procedure.Mode) Arrays(java.util.Arrays) StoreIdProvider(org.neo4j.storageengine.api.StoreIdProvider) SCHEMA(org.neo4j.procedure.Mode.SCHEMA) Status(org.neo4j.kernel.api.exceptions.Status) Iterators.asList(org.neo4j.internal.helpers.collection.Iterators.asList) TokenNameLookup(org.neo4j.common.TokenNameLookup) Config(org.neo4j.configuration.Config) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) Value(org.neo4j.values.storable.Value) ProceduresTimeFormatHelper.formatTime(org.neo4j.procedure.builtin.ProceduresTimeFormatHelper.formatTime) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) SchemaReadCore(org.neo4j.internal.kernel.api.SchemaReadCore) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) Map(java.util.Map) SecurityContext(org.neo4j.internal.kernel.api.security.SecurityContext) Transaction(org.neo4j.graphdb.Transaction) IndexProviderDescriptor(org.neo4j.internal.schema.IndexProviderDescriptor) Procedure(org.neo4j.procedure.Procedure) PopulationProgress(org.neo4j.internal.kernel.api.PopulationProgress) LABELS(org.neo4j.kernel.impl.api.TokenAccess.LABELS) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) AccessMode(org.neo4j.internal.kernel.api.security.AccessMode) List(java.util.List) Stream(java.util.stream.Stream) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Name(org.neo4j.procedure.Name) RelationshipType(org.neo4j.graphdb.RelationshipType) StoreIdDecodeUtils.decodeId(org.neo4j.procedure.builtin.StoreIdDecodeUtils.decodeId) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) Admin(org.neo4j.procedure.Admin) IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) Label(org.neo4j.graphdb.Label) QueryExecutionEngine(org.neo4j.kernel.impl.query.QueryExecutionEngine) GraphDatabaseSettings(org.neo4j.configuration.GraphDatabaseSettings) InternalIndexState(org.neo4j.internal.kernel.api.InternalIndexState) Context(org.neo4j.procedure.Context) TokenRead(org.neo4j.internal.kernel.api.TokenRead) HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) ArrayList(java.util.ArrayList) Iterators.stream(org.neo4j.internal.helpers.collection.Iterators.stream) DependencyResolver(org.neo4j.common.DependencyResolver) RELATIONSHIP_TYPES(org.neo4j.kernel.impl.api.TokenAccess.RELATIONSHIP_TYPES) READ(org.neo4j.procedure.Mode.READ) Description(org.neo4j.procedure.Description) TimeUnit(java.util.concurrent.TimeUnit) PROPERTY_KEYS(org.neo4j.kernel.impl.api.TokenAccess.PROPERTY_KEYS) ProcedureCallContext(org.neo4j.internal.kernel.api.procs.ProcedureCallContext) Relationship(org.neo4j.graphdb.Relationship) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Comparator(java.util.Comparator) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) AccessMode(org.neo4j.internal.kernel.api.security.AccessMode) TokenRead(org.neo4j.internal.kernel.api.TokenRead) Description(org.neo4j.procedure.Description) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) Procedure(org.neo4j.procedure.Procedure)

Example 13 with TokenRead

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

the class BuiltInProcedures method listIndexes.

@Deprecated(since = "4.2.0", forRemoval = true)
@SystemProcedure
@Description("List all indexes in the database.")
@Procedure(name = "db.indexes", mode = READ, deprecatedBy = "SHOW INDEXES command")
public Stream<IndexResult> listIndexes() {
    if (callContext.isSystemDatabase()) {
        return Stream.empty();
    }
    TokenRead tokenRead = kernelTransaction.tokenRead();
    IndexingService indexingService = resolver.resolveDependency(IndexingService.class);
    SchemaReadCore schemaRead = kernelTransaction.schemaRead().snapshot();
    List<IndexDescriptor> indexes = asList(schemaRead.indexesGetAll());
    List<IndexResult> result = new ArrayList<>();
    for (IndexDescriptor index : indexes) {
        IndexResult indexResult;
        indexResult = asIndexResult(tokenRead, schemaRead, index);
        result.add(indexResult);
    }
    result.sort(Comparator.comparing(r -> r.name));
    return result.stream();
}
Also used : Mode(org.neo4j.procedure.Mode) Arrays(java.util.Arrays) StoreIdProvider(org.neo4j.storageengine.api.StoreIdProvider) SCHEMA(org.neo4j.procedure.Mode.SCHEMA) Status(org.neo4j.kernel.api.exceptions.Status) Iterators.asList(org.neo4j.internal.helpers.collection.Iterators.asList) TokenNameLookup(org.neo4j.common.TokenNameLookup) Config(org.neo4j.configuration.Config) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) Value(org.neo4j.values.storable.Value) ProceduresTimeFormatHelper.formatTime(org.neo4j.procedure.builtin.ProceduresTimeFormatHelper.formatTime) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) SchemaReadCore(org.neo4j.internal.kernel.api.SchemaReadCore) IndexNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException) Map(java.util.Map) SecurityContext(org.neo4j.internal.kernel.api.security.SecurityContext) Transaction(org.neo4j.graphdb.Transaction) IndexProviderDescriptor(org.neo4j.internal.schema.IndexProviderDescriptor) Procedure(org.neo4j.procedure.Procedure) PopulationProgress(org.neo4j.internal.kernel.api.PopulationProgress) LABELS(org.neo4j.kernel.impl.api.TokenAccess.LABELS) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) AccessMode(org.neo4j.internal.kernel.api.security.AccessMode) List(java.util.List) Stream(java.util.stream.Stream) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Name(org.neo4j.procedure.Name) RelationshipType(org.neo4j.graphdb.RelationshipType) StoreIdDecodeUtils.decodeId(org.neo4j.procedure.builtin.StoreIdDecodeUtils.decodeId) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) Admin(org.neo4j.procedure.Admin) IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) Label(org.neo4j.graphdb.Label) QueryExecutionEngine(org.neo4j.kernel.impl.query.QueryExecutionEngine) GraphDatabaseSettings(org.neo4j.configuration.GraphDatabaseSettings) InternalIndexState(org.neo4j.internal.kernel.api.InternalIndexState) Context(org.neo4j.procedure.Context) TokenRead(org.neo4j.internal.kernel.api.TokenRead) HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) ArrayList(java.util.ArrayList) Iterators.stream(org.neo4j.internal.helpers.collection.Iterators.stream) DependencyResolver(org.neo4j.common.DependencyResolver) RELATIONSHIP_TYPES(org.neo4j.kernel.impl.api.TokenAccess.RELATIONSHIP_TYPES) READ(org.neo4j.procedure.Mode.READ) Description(org.neo4j.procedure.Description) TimeUnit(java.util.concurrent.TimeUnit) PROPERTY_KEYS(org.neo4j.kernel.impl.api.TokenAccess.PROPERTY_KEYS) ProcedureCallContext(org.neo4j.internal.kernel.api.procs.ProcedureCallContext) Relationship(org.neo4j.graphdb.Relationship) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Comparator(java.util.Comparator) IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) ArrayList(java.util.ArrayList) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) TokenRead(org.neo4j.internal.kernel.api.TokenRead) SchemaReadCore(org.neo4j.internal.kernel.api.SchemaReadCore) Description(org.neo4j.procedure.Description) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) Procedure(org.neo4j.procedure.Procedure)

Example 14 with TokenRead

use of org.neo4j.internal.kernel.api.TokenRead 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 15 with TokenRead

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

the class IndexCRUDIT method addingALabelToPreExistingNodeShouldGetIndexed.

@Test
void addingALabelToPreExistingNodeShouldGetIndexed() throws Exception {
    // GIVEN
    String indexProperty = "indexProperty";
    GatheringIndexWriter writer = newWriter();
    createIndex(db, myLabel, indexProperty);
    // WHEN
    String otherProperty = "otherProperty";
    int value = 12;
    int otherValue = 17;
    Node node = createNode(map(indexProperty, value, otherProperty, otherValue));
    // THEN
    assertThat(writer.updatesCommitted.size()).isEqualTo(0);
    // AND WHEN
    try (Transaction tx = db.beginTx()) {
        node = tx.getNodeById(node.getId());
        node.addLabel(myLabel);
        tx.commit();
    }
    // THEN
    try (Transaction tx = db.beginTx()) {
        KernelTransaction ktx = ((InternalTransaction) tx).kernelTransaction();
        TokenRead tokenRead = ktx.tokenRead();
        int propertyKey1 = tokenRead.propertyKey(indexProperty);
        int label = tokenRead.nodeLabel(myLabel.name());
        LabelSchemaDescriptor descriptor = SchemaDescriptor.forLabel(label, propertyKey1);
        assertThat(writer.updatesCommitted).isEqualTo(asSet(IndexEntryUpdate.add(node.getId(), descriptor, Values.of(value))));
        tx.commit();
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Node(org.neo4j.graphdb.Node) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) TokenRead(org.neo4j.internal.kernel.api.TokenRead) Test(org.junit.jupiter.api.Test)

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