Search in sources :

Example 31 with IndexDefinition

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

the class LuceneIndexRecoveryIT method removeShouldBeIdempotentWhenDoingRecovery.

@Test
public void removeShouldBeIdempotentWhenDoingRecovery() throws Exception {
    // Given
    startDb(createLuceneIndexFactory());
    IndexDefinition indexDefinition = createIndex(myLabel);
    waitForIndex(indexDefinition);
    long node = createNode(myLabel, 12);
    rotateLogsAndCheckPoint();
    deleteNode(node);
    // And Given
    killDb();
    // When
    startDb(createLuceneIndexFactory());
    // Then
    assertEquals(0, doIndexLookup(myLabel, 12).size());
}
Also used : IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) Test(org.junit.Test)

Example 32 with IndexDefinition

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

the class IndexValuesValidationTest method createIndex.

private IndexDefinition createIndex(Label label, String propertyName) {
    try (Transaction transaction = database.beginTx()) {
        IndexDefinition indexDefinition = database.schema().indexFor(label).on(propertyName).create();
        transaction.success();
        return indexDefinition;
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition)

Example 33 with IndexDefinition

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

the class IndexSamplingIntegrationTest method shouldSampleNotUniqueIndex.

@Test
public void shouldSampleNotUniqueIndex() throws Throwable {
    GraphDatabaseService db = null;
    long deletedNodes = 0;
    try {
        // Given
        db = new TestGraphDatabaseFactory().newEmbeddedDatabase(testDirectory.graphDbDir());
        IndexDefinition indexDefinition;
        try (Transaction tx = db.beginTx()) {
            indexDefinition = db.schema().indexFor(label).on(property).create();
            tx.success();
        }
        try (Transaction tx = db.beginTx()) {
            db.schema().awaitIndexOnline(indexDefinition, 10, TimeUnit.SECONDS);
            tx.success();
        }
        try (Transaction tx = db.beginTx()) {
            for (int i = 0; i < nodes; i++) {
                db.createNode(label).setProperty(property, names[i % names.length]);
                tx.success();
            }
        }
        try (Transaction tx = db.beginTx()) {
            for (int i = 0; i < (nodes / 10); i++) {
                db.findNodes(label, property, names[i % names.length]).next().delete();
                deletedNodes++;
                tx.success();
            }
        }
    } finally {
        if (db != null) {
            db.shutdown();
        }
    }
    // When
    triggerIndexResamplingOnNextStartup();
    // Then
    // sampling will consider also the delete nodes till the next lucene compaction
    DoubleLongRegister register = fetchIndexSamplingValues(db);
    assertEquals(names.length, register.readFirst());
    assertEquals(nodes, register.readSecond());
    // but the deleted nodes should not be considered in the index size value
    DoubleLongRegister indexSizeRegister = fetchIndexSizeValues(db);
    assertEquals(0, indexSizeRegister.readFirst());
    assertEquals(nodes - deletedNodes, indexSizeRegister.readSecond());
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) Transaction(org.neo4j.graphdb.Transaction) DoubleLongRegister(org.neo4j.register.Register.DoubleLongRegister) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) Test(org.junit.Test)

Example 34 with IndexDefinition

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

the class CypherResultSubGraph method from.

public static SubGraph from(Result result, GraphDatabaseService gds, boolean addBetween) {
    final CypherResultSubGraph graph = new CypherResultSubGraph();
    final List<String> columns = result.columns();
    for (Map<String, Object> row : loop(result)) {
        for (String column : columns) {
            final Object value = row.get(column);
            graph.addToGraph(value);
        }
    }
    for (IndexDefinition def : gds.schema().getIndexes()) {
        if (graph.getLabels().contains(def.getLabel())) {
            graph.addIndex(def);
        }
    }
    for (ConstraintDefinition def : gds.schema().getConstraints()) {
        if (graph.getLabels().contains(def.getLabel())) {
            graph.addConstraint(def);
        }
    }
    if (addBetween) {
        graph.addRelationshipsBetweenNodes();
    }
    return graph;
}
Also used : IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) ConstraintDefinition(org.neo4j.graphdb.schema.ConstraintDefinition)

Example 35 with IndexDefinition

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

the class SubGraphExporter method exportIndexes.

private Collection<String> exportIndexes() {
    final List<String> result = new ArrayList<>();
    for (IndexDefinition index : graph.getIndexes()) {
        if (!index.isConstraintIndex()) {
            Iterator<String> propertyKeys = index.getPropertyKeys().iterator();
            if (!propertyKeys.hasNext()) {
                throw new IllegalStateException("Indexes should have at least one property key");
            }
            String key = quote(propertyKeys.next());
            if (propertyKeys.hasNext()) {
                throw new RuntimeException("Exporting compound indexes is not implemented yet");
            }
            String label = quote(index.getLabel().name());
            result.add("create index on :" + label + "(" + key + ")");
        }
    }
    Collections.sort(result);
    return result;
}
Also used : IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) ArrayList(java.util.ArrayList)

Aggregations

IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)84 Test (org.junit.Test)56 Transaction (org.neo4j.graphdb.Transaction)32 StringContains.containsString (org.hamcrest.core.StringContains.containsString)11 Node (org.neo4j.graphdb.Node)9 Statement (org.neo4j.kernel.api.Statement)7 ArrayList (java.util.ArrayList)4 PrimitiveLongSet (org.neo4j.collection.primitive.PrimitiveLongSet)4 Label (org.neo4j.graphdb.Label)4 ReadOperations (org.neo4j.kernel.api.ReadOperations)4 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)4 HighlyAvailableGraphDatabase (org.neo4j.kernel.ha.HighlyAvailableGraphDatabase)4 ManagedCluster (org.neo4j.kernel.impl.ha.ClusterManager.ManagedCluster)4 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)3 Iterator (java.util.Iterator)2 PrimitiveLongIterator (org.neo4j.collection.primitive.PrimitiveLongIterator)2 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)2 TransactionData (org.neo4j.graphdb.event.TransactionData)2 TransactionEventHandler (org.neo4j.graphdb.event.TransactionEventHandler)2 ConstraintDefinition (org.neo4j.graphdb.schema.ConstraintDefinition)2