Search in sources :

Example 6 with Schema

use of org.neo4j.graphdb.schema.Schema in project neo4j-apoc-procedures by neo4j-contrib.

the class NodesAndRelsSubGraph method getIndexes.

@Override
public Iterable<IndexDefinition> getIndexes() {
    Schema schema = db.schema();
    ArrayList<IndexDefinition> indexes = new ArrayList<>(labels.size() * 2);
    for (String label : labels) {
        Iterables.addAll(indexes, schema.getIndexes(Label.label(label)));
    }
    return indexes;
}
Also used : IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) Schema(org.neo4j.graphdb.schema.Schema) ArrayList(java.util.ArrayList)

Example 7 with Schema

use of org.neo4j.graphdb.schema.Schema in project neo4j by neo4j.

the class FulltextProcedures method awaitOnline.

private void awaitOnline(IndexDescriptor index) {
    // We do the isAdded check on the transaction state first, because indexGetState will grab a schema read-lock, which can deadlock on the write-lock
    // held by the index populator. Also, if the index was created in this transaction, then we will never see it come online in this transaction anyway.
    // Indexes don't come online until the transaction that creates them has committed.
    KernelTransactionImplementation txImpl = (KernelTransactionImplementation) this.tx;
    if (!txImpl.hasTxStateWithChanges() || !txImpl.txState().indexDiffSetsBySchema(index.schema()).isAdded(index)) {
        // If the index was not created in this transaction, then wait for it to come online before querying.
        Schema schema = transaction.schema();
        schema.awaitIndexOnline(index.getName(), INDEX_ONLINE_QUERY_TIMEOUT_SECONDS, TimeUnit.SECONDS);
    }
// If the index was created in this transaction, then we skip this check entirely.
// We will get an exception later, when we try to get an IndexReader, so this is fine.
}
Also used : KernelTransactionImplementation(org.neo4j.kernel.impl.api.KernelTransactionImplementation) Schema(org.neo4j.graphdb.schema.Schema)

Example 8 with Schema

use of org.neo4j.graphdb.schema.Schema in project neo4j by neo4j.

the class SchemaImplTest method testGetIndexPopulationProgress.

@Test
void testGetIndexPopulationProgress() throws Exception {
    assertFalse(indexExists(USER_LABEL));
    // Create some nodes
    try (Transaction tx = db.beginTx()) {
        Label label = Label.label("User");
        // Create a huge bunch of users so the index takes a while to build
        for (int id = 0; id < 100000; id++) {
            Node userNode = tx.createNode(label);
            userNode.setProperty("username", "user" + id + "@neo4j.org");
        }
        tx.commit();
    }
    // Create an index
    IndexDefinition indexDefinition;
    try (Transaction tx = db.beginTx()) {
        Schema schema = tx.schema();
        indexDefinition = schema.indexFor(USER_LABEL).on("username").create();
        tx.commit();
    }
    // Get state and progress
    try (Transaction tx = db.beginTx()) {
        Schema schema = tx.schema();
        Schema.IndexState state;
        IndexPopulationProgress progress;
        do {
            state = schema.getIndexState(indexDefinition);
            progress = schema.getIndexPopulationProgress(indexDefinition);
            assertTrue(progress.getCompletedPercentage() >= 0);
            assertTrue(progress.getCompletedPercentage() <= 100);
            Thread.sleep(10);
        } while (state == Schema.IndexState.POPULATING);
        assertSame(state, Schema.IndexState.ONLINE);
        assertEquals(100.0, progress.getCompletedPercentage(), 0.0001);
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) IndexPopulationProgress(org.neo4j.graphdb.schema.IndexPopulationProgress) Node(org.neo4j.graphdb.Node) Schema(org.neo4j.graphdb.schema.Schema) Label(org.neo4j.graphdb.Label) Test(org.junit.jupiter.api.Test)

Example 9 with Schema

use of org.neo4j.graphdb.schema.Schema in project neo4j by neo4j.

the class SchemaAcceptanceTest method shouldThrowConstraintViolationIfAskedToIndexSamePropertyAndLabelTwiceInSameTx.

@Test
public void shouldThrowConstraintViolationIfAskedToIndexSamePropertyAndLabelTwiceInSameTx() throws Exception {
    // WHEN
    try (Transaction tx = db.beginTx()) {
        Schema schema = db.schema();
        schema.indexFor(label).on(propertyKey).create();
        try {
            schema.indexFor(label).on(propertyKey).create();
            fail("Should not have validated");
        } catch (ConstraintViolationException e) {
            assertEquals("There already exists an index for label 'MY_LABEL' on property 'my_property_key'.", e.getMessage());
        }
        tx.success();
    }
}
Also used : Schema(org.neo4j.graphdb.schema.Schema) Test(org.junit.Test)

Example 10 with Schema

use of org.neo4j.graphdb.schema.Schema in project neo4j-apoc-procedures by neo4j-contrib.

the class Meta method collectMetaData.

private Map<String, Map<String, MetaResult>> collectMetaData() {
    Map<String, Map<String, MetaResult>> metaData = new LinkedHashMap<>(100);
    Schema schema = db.schema();
    Map<String, Iterable<ConstraintDefinition>> relConstraints = new HashMap<>(20);
    for (RelationshipType type : db.getAllRelationshipTypesInUse()) {
        metaData.put(type.name(), new LinkedHashMap<>(10));
        relConstraints.put(type.name(), schema.getConstraints(type));
    }
    for (Label label : db.getAllLabelsInUse()) {
        Map<String, MetaResult> nodeMeta = new LinkedHashMap<>(50);
        String labelName = label.name();
        metaData.put(labelName, nodeMeta);
        Iterable<ConstraintDefinition> constraints = schema.getConstraints(label);
        Set<String> indexed = new LinkedHashSet<>();
        for (IndexDefinition index : schema.getIndexes(label)) {
            for (String prop : index.getPropertyKeys()) {
                indexed.add(prop);
            }
        }
        try (ResourceIterator<Node> nodes = db.findNodes(label)) {
            int count = 0;
            while (nodes.hasNext() && count++ < SAMPLE) {
                Node node = nodes.next();
                addRelationships(metaData, nodeMeta, labelName, node, relConstraints);
                addProperties(nodeMeta, labelName, constraints, indexed, node, node);
            }
        }
    }
    return metaData;
}
Also used : Schema(org.neo4j.graphdb.schema.Schema) VirtualNode(apoc.result.VirtualNode) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) Collectors.toMap(java.util.stream.Collectors.toMap) Collections.singletonMap(java.util.Collections.singletonMap)

Aggregations

Schema (org.neo4j.graphdb.schema.Schema)19 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)10 Transaction (org.neo4j.graphdb.Transaction)9 ConstraintDefinition (org.neo4j.graphdb.schema.ConstraintDefinition)5 ArrayList (java.util.ArrayList)4 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)4 Label (org.neo4j.graphdb.Label)4 AssertSchemaResult (apoc.result.AssertSchemaResult)3 Test (org.junit.Test)3 Test (org.junit.jupiter.api.Test)3 Node (org.neo4j.graphdb.Node)3 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 ResourceIterator (org.neo4j.graphdb.ResourceIterator)2 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)2 ConstraintRelationshipInfo (apoc.result.ConstraintRelationshipInfo)1 IndexConstraintNodeInfo (apoc.result.IndexConstraintNodeInfo)1 VirtualNode (apoc.result.VirtualNode)1 File (java.io.File)1 java.util (java.util)1