Search in sources :

Example 6 with SchemaRead

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

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

the class IndexIT method shouldListAll.

@Test
void shouldListAll() throws Exception {
    // given
    SchemaWrite schemaWrite = schemaWriteInNewTransaction();
    IndexDescriptor index1 = schemaWrite.indexCreate(schema, "my index");
    IndexBackedConstraintDescriptor constraint = schemaWrite.uniquePropertyConstraintCreate(uniqueForSchema(schema2).withName("constraint name")).asIndexBackedConstraint();
    commit();
    // then/when
    SchemaRead schemaRead = newTransaction().schemaRead();
    IndexDescriptor index2 = Iterators.single(schemaRead.index(constraint.schema()));
    List<IndexDescriptor> indexes = Iterators.asList(schemaRead.indexesGetAll());
    assertThat(indexes).contains(index1, index2);
    commit();
}
Also used : SchemaWrite(org.neo4j.internal.kernel.api.SchemaWrite) IndexBackedConstraintDescriptor(org.neo4j.internal.schema.constraints.IndexBackedConstraintDescriptor) SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) Test(org.junit.jupiter.api.Test) KernelIntegrationTest(org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)

Example 8 with SchemaRead

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

the class SchemaReadWriteTestBase method shouldSeeUniqueConstraintFromTransaction.

@Test
void shouldSeeUniqueConstraintFromTransaction() throws Exception {
    ConstraintDescriptor existing;
    try (KernelTransaction transaction = beginTransaction()) {
        existing = transaction.schemaWrite().uniquePropertyConstraintCreate(uniqueForSchema(forLabel(label, prop1)).withName("existing constraint"));
        transaction.commit();
    }
    try (KernelTransaction transaction = beginTransaction()) {
        SchemaReadCore before = transaction.schemaRead().snapshot();
        ConstraintDescriptor newConstraint = transaction.schemaWrite().uniquePropertyConstraintCreate(uniqueForSchema(forLabel(label, prop2)).withName("new constraint"));
        SchemaRead schemaRead = transaction.schemaRead();
        SchemaReadCore after = schemaRead.snapshot();
        assertTrue(schemaRead.constraintExists(existing));
        assertTrue(schemaRead.constraintExists(newConstraint));
        assertThat(asList(schemaRead.constraintsGetForLabel(label))).contains(existing, newConstraint);
        assertThat(asList(before.constraintsGetForLabel(label))).contains(existing, newConstraint);
        assertThat(asList(after.constraintsGetForLabel(label))).contains(existing, newConstraint);
        assertThat(before.constraintGetForName("existing constraint")).isEqualTo(existing);
        assertThat(after.constraintGetForName("existing constraint")).isEqualTo(existing);
        assertThat(before.constraintGetForName("new constraint")).isEqualTo(newConstraint);
        assertThat(after.constraintGetForName("new constraint")).isEqualTo(newConstraint);
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) SchemaReadCore(org.neo4j.internal.kernel.api.SchemaReadCore) Test(org.junit.jupiter.api.Test)

Example 9 with SchemaRead

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

the class SchemaReadWriteTestBase method shouldNotSeeDroppedUniqueConstraintFromTransaction.

@Test
void shouldNotSeeDroppedUniqueConstraintFromTransaction() throws Exception {
    ConstraintDescriptor existing;
    try (KernelTransaction transaction = beginTransaction()) {
        existing = transaction.schemaWrite().uniquePropertyConstraintCreate(uniqueForSchema(forLabel(label, prop1)));
        transaction.commit();
    }
    try (KernelTransaction transaction = beginTransaction()) {
        SchemaReadCore before = transaction.schemaRead().snapshot();
        transaction.schemaWrite().constraintDrop(existing);
        SchemaRead schemaRead = transaction.schemaRead();
        assertFalse(schemaRead.constraintExists(existing));
        assertThat(asList(schemaRead.constraintsGetForLabel(label))).isEmpty();
        assertThat(asList(schemaRead.snapshot().constraintsGetForLabel(label))).isEmpty();
        assertThat(asList(before.constraintsGetForLabel(label))).isEmpty();
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) SchemaReadCore(org.neo4j.internal.kernel.api.SchemaReadCore) Test(org.junit.jupiter.api.Test)

Example 10 with SchemaRead

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

the class SchemaReadWriteTestBase method setUp.

@BeforeEach
void setUp() throws Exception {
    try (KernelTransaction transaction = beginTransaction()) {
        SchemaRead schemaRead = transaction.schemaRead();
        SchemaWrite schemaWrite = transaction.schemaWrite();
        Iterator<ConstraintDescriptor> constraints = schemaRead.constraintsGetAll();
        while (constraints.hasNext()) {
            schemaWrite.constraintDrop(constraints.next());
        }
        Iterator<IndexDescriptor> indexes = schemaRead.indexesGetAll();
        while (indexes.hasNext()) {
            schemaWrite.indexDrop(indexes.next());
        }
        TokenWrite tokenWrite = transaction.tokenWrite();
        label = tokenWrite.labelGetOrCreateForName("label");
        label2 = tokenWrite.labelGetOrCreateForName("label2");
        type = tokenWrite.relationshipTypeGetOrCreateForName("relationship");
        prop1 = tokenWrite.propertyKeyGetOrCreateForName("prop1");
        prop2 = tokenWrite.propertyKeyGetOrCreateForName("prop2");
        prop3 = tokenWrite.propertyKeyGetOrCreateForName("prop3");
        transaction.commit();
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) SchemaRead(org.neo4j.internal.kernel.api.SchemaRead) SchemaWrite(org.neo4j.internal.kernel.api.SchemaWrite) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

SchemaRead (org.neo4j.internal.kernel.api.SchemaRead)52 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)36 Test (org.junit.jupiter.api.Test)28 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)26 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)22 TokenRead (org.neo4j.internal.kernel.api.TokenRead)12 SchemaReadCore (org.neo4j.internal.kernel.api.SchemaReadCore)9 IndexNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotFoundKernelException)8 ArrayList (java.util.ArrayList)7 Read (org.neo4j.internal.kernel.api.Read)5 HashMap (java.util.HashMap)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 Arrays (java.util.Arrays)3 Iterator (java.util.Iterator)3 List (java.util.List)3 Map (java.util.Map)3 EntityType (org.neo4j.common.EntityType)3 KernelException (org.neo4j.exceptions.KernelException)3 Transaction (org.neo4j.graphdb.Transaction)3 Iterators (org.neo4j.internal.helpers.collection.Iterators)3