Search in sources :

Example 51 with TokenRead

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

the class FulltextIndexProviderTest method prepDB.

@BeforeEach
void prepDB() {
    Label hej = label("hej");
    Label ha = label("ha");
    Label he = label("he");
    RelationshipType hejType = RelationshipType.withName("hej");
    try (Transaction transaction = db.beginTx()) {
        node1 = transaction.createNode(hej, ha, he);
        node1.setProperty("hej", "value");
        node1.setProperty("ha", "value1");
        node1.setProperty("he", "value2");
        node1.setProperty("ho", "value3");
        node2 = transaction.createNode();
        Relationship rel = node1.createRelationshipTo(node2, hejType);
        rel.setProperty("hej", "valuuu");
        rel.setProperty("ha", "value1");
        rel.setProperty("he", "value2");
        rel.setProperty("ho", "value3");
        transaction.commit();
    }
    try (Transaction tx = db.beginTx()) {
        TokenRead tokenRead = tokenRead(tx);
        labelIdHej = tokenRead.nodeLabel(hej.name());
        labelIdHa = tokenRead.nodeLabel(ha.name());
        labelIdHe = tokenRead.nodeLabel(he.name());
        relTypeIdHej = tokenRead.relationshipType(hejType.name());
        propIdHej = tokenRead.propertyKey("hej");
        propIdHa = tokenRead.propertyKey("ha");
        propIdHe = tokenRead.propertyKey("he");
        propIdHo = tokenRead.propertyKey("ho");
        tx.commit();
    }
}
Also used : InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Relationship(org.neo4j.graphdb.Relationship) Label(org.neo4j.graphdb.Label) RelationshipType(org.neo4j.graphdb.RelationshipType) TokenRead(org.neo4j.internal.kernel.api.TokenRead) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 52 with TokenRead

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

the class FulltextIndexProviderTest method shouldAnswerRangeIfCypherCompatible.

@Test
void shouldAnswerRangeIfCypherCompatible() throws KernelException {
    IndexDescriptor indexReference;
    Label containsLabel = label("containsLabel");
    String containsProp = "containsProp";
    long nodea;
    long nodeaa;
    long nodeaapa;
    long nodeapa1;
    long nodeapa2;
    try (Transaction tx = db.beginTx()) {
        createNode(tx, containsLabel, containsProp, "1");
        nodea = createNode(tx, containsLabel, containsProp, "a");
        nodeaa = createNode(tx, containsLabel, containsProp, "aa");
        nodeaapa = createNode(tx, containsLabel, containsProp, "aapa");
        nodeapa1 = createNode(tx, containsLabel, containsProp, "apa");
        nodeapa2 = createNode(tx, containsLabel, containsProp, "apa");
        createNode(tx, containsLabel, containsProp, "bpa");
        createNode(tx, containsLabel, containsProp, "A");
        tx.commit();
    }
    int containsLabelId;
    int containsPropertyId;
    try (Transaction tx = db.beginTx()) {
        TokenRead tokenRead = tokenRead(tx);
        containsLabelId = tokenRead.nodeLabel(containsLabel.name());
        containsPropertyId = tokenRead.propertyKey(containsProp);
    }
    indexReference = createIndex(new int[] { containsLabelId }, new int[] { containsPropertyId }, "cypher");
    await(indexReference);
    try (Transaction tx = db.beginTx()) {
        KernelTransaction ktx = LuceneFulltextTestSupport.kernelTransaction(tx);
        assertQueryResult(ktx, rangeQuery(containsPropertyId, "a", true, "apa", true), nodea, nodeaa, nodeaapa, nodeapa1, nodeapa2);
        assertQueryResult(ktx, rangeQuery(containsPropertyId, "a", false, "apa", true), nodeaa, nodeaapa, nodeapa1, nodeapa2);
        assertQueryResult(ktx, rangeQuery(containsPropertyId, "a", true, "apa", false), nodea, nodeaa, nodeaapa);
        assertQueryResult(ktx, rangeQuery(containsPropertyId, "a", false, "apa", false), nodeaa, nodeaapa);
    }
    controller.restartDbms();
    try (Transaction tx = db.beginTx()) {
        KernelTransaction ktx = LuceneFulltextTestSupport.kernelTransaction(tx);
        assertQueryResult(ktx, rangeQuery(containsPropertyId, "a", true, "apa", true), nodea, nodeaa, nodeaapa, nodeapa1, nodeapa2);
        assertQueryResult(ktx, rangeQuery(containsPropertyId, "a", false, "apa", true), nodeaa, nodeaapa, nodeapa1, nodeapa2);
        assertQueryResult(ktx, rangeQuery(containsPropertyId, "a", true, "apa", false), nodea, nodeaa, nodeaapa);
        assertQueryResult(ktx, rangeQuery(containsPropertyId, "a", false, "apa", false), nodeaa, nodeaapa);
    }
}
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) Label(org.neo4j.graphdb.Label) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) TokenRead(org.neo4j.internal.kernel.api.TokenRead) Test(org.junit.jupiter.api.Test)

Example 53 with TokenRead

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

the class BuiltInProcedures method listLabels.

@SystemProcedure
@Description("List all available labels in the database.")
@Procedure(name = "db.labels", mode = READ)
public Stream<LabelResult> listLabels() {
    if (callContext.isSystemDatabase()) {
        return Stream.empty();
    }
    AccessMode mode = kernelTransaction.securityContext().mode();
    TokenRead tokenRead = kernelTransaction.tokenRead();
    List<LabelResult> labelsInUse;
    try (KernelTransaction.Revertable ignore = kernelTransaction.overrideWith(SecurityContext.AUTH_DISABLED)) {
        // Get all labels that are in use as seen by a super user
        labelsInUse = stream(LABELS.inUse(kernelTransaction)).filter(label -> mode.allowsTraverseNode(tokenRead.nodeLabel(label.name()))).map(LabelResult::new).collect(Collectors.toList());
    }
    return labelsInUse.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 54 with TokenRead

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

the class GraphCountsSection method indexes.

private static List<Map<String, Object>> indexes(TokenRead tokens, SchemaRead schemaRead, Anonymizer anonymizer) throws IndexNotFoundKernelException {
    List<Map<String, Object>> indexes = new ArrayList<>();
    Iterator<IndexDescriptor> iterator = schemaRead.indexesGetAll();
    while (iterator.hasNext()) {
        IndexDescriptor index = iterator.next();
        IndexType indexType = index.getIndexType();
        if (indexType == IndexType.FULLTEXT) {
            /* For full text indexes, we currently do not return its options, which makes returning information on
                 * this index not useful and if the index type is ignored, this would even be misleading.
                 */
            continue;
        }
        EntityType entityType = index.schema().entityType();
        Map<String, Object> data = new HashMap<>();
        switch(entityType) {
            case NODE:
                data.put("labels", map(index.schema().getEntityTokenIds(), id -> anonymizer.label(tokens.labelGetName(id), id)));
                break;
            case RELATIONSHIP:
                data.put("relationshipTypes", map(index.schema().getEntityTokenIds(), id -> anonymizer.relationshipType(tokens.relationshipTypeGetName(id), id)));
                break;
            default:
        }
        data.put("properties", map(index.schema().getPropertyIds(), id -> anonymizer.propertyKey(tokens.propertyKeyGetName(id), id)));
        var indexSample = schemaRead.indexSample(index);
        data.put("totalSize", indexSample.indexSize());
        data.put("updatesSinceEstimation", indexSample.updates());
        data.put("estimatedUniqueSize", indexSample.uniqueValues());
        data.put("indexType", indexType.name());
        indexes.add(data);
    }
    return indexes;
}
Also used : 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) ArrayList(java.util.ArrayList) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) EntityType(org.neo4j.common.EntityType) IndexType(org.neo4j.internal.schema.IndexType) HashMap(java.util.HashMap) Map(java.util.Map)

Example 55 with TokenRead

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

the class TokensSection method retrieve.

static Stream<RetrieveResult> retrieve(Kernel kernel) throws TransactionFailureException {
    try (KernelTransaction tx = kernel.beginTransaction(KernelTransaction.Type.EXPLICIT, LoginContext.AUTH_DISABLED)) {
        TokenRead tokens = tx.tokenRead();
        List<String> labels = new ArrayList<>(tokens.labelCount());
        tokens.labelsGetAllTokens().forEachRemaining(t -> labels.add(t.name()));
        List<String> relationshipTypes = new ArrayList<>(tokens.relationshipTypeCount());
        tokens.relationshipTypesGetAllTokens().forEachRemaining(t -> relationshipTypes.add(t.name()));
        List<String> propertyKeys = new ArrayList<>(tokens.propertyKeyCount());
        tokens.propertyKeyGetAllTokens().forEachRemaining(t -> propertyKeys.add(t.name()));
        Map<String, Object> data = new HashMap<>();
        data.put("labels", labels);
        data.put("relationshipTypes", relationshipTypes);
        data.put("propertyKeys", propertyKeys);
        return Stream.of(new RetrieveResult(Sections.TOKENS, data));
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TokenRead(org.neo4j.internal.kernel.api.TokenRead)

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