use of org.neo4j.graphdb.schema.IndexCreator in project neo4j by neo4j.
the class DetectRandomSabotageIT method createSchema.
private void createSchema(GraphDatabaseAPI db) {
for (int i = 0; i < NUMBER_OF_INDEXES; i++) {
Label label = Label.label(random.among(TOKEN_NAMES));
String[] keys = random.selection(TOKEN_NAMES, 1, 3, false);
try (Transaction tx = db.beginTx()) {
// A couple of node indexes
IndexCreator indexCreator = tx.schema().indexFor(label);
for (String key : keys) {
indexCreator = indexCreator.on(key);
}
indexCreator.create();
tx.commit();
} catch (ConstraintViolationException e) {
// This is fine we've already created a similar index
}
if (keys.length == 1 && random.nextFloat() < 0.3) {
try (Transaction tx = db.beginTx()) {
// Also create a uniqueness constraint for this index
ConstraintCreator constraintCreator = tx.schema().constraintFor(label);
for (String key : keys) {
constraintCreator = constraintCreator.assertPropertyIsUnique(key);
}
// TODO also make it so that it's possible to add other types of constraints... this would mean
// guaranteeing e.g. property existence on entities given certain entity tokens and such
constraintCreator.create();
tx.commit();
} catch (ConstraintViolationException e) {
// This is fine, either we tried to create a uniqueness constraint on data that isn't unique (we just generate random data above)
// or we already created a similar constraint.
}
}
}
try (Transaction tx = db.beginTx()) {
tx.schema().awaitIndexesOnline(2, TimeUnit.MINUTES);
tx.commit();
}
}
use of org.neo4j.graphdb.schema.IndexCreator in project neo4j by neo4j.
the class FulltextIndexProviderTest method indexWithAnalyzerProviderThatThrowsAnExceptionOnStartupWillBeMarkedAsFailedOnStartup.
@ResourceLock("BrokenAnalyzerProvider")
@Test
void indexWithAnalyzerProviderThatThrowsAnExceptionOnStartupWillBeMarkedAsFailedOnStartup() {
BrokenAnalyzerProvider.shouldThrow = false;
BrokenAnalyzerProvider.shouldReturnNull = false;
try (Transaction tx = db.beginTx()) {
IndexCreator creator = tx.schema().indexFor(label("Label")).withIndexType(org.neo4j.graphdb.schema.IndexType.FULLTEXT).withIndexConfiguration(Map.of(IndexSetting.fulltext_Analyzer(), BrokenAnalyzerProvider.NAME)).on("prop").withName(NAME);
// The analyzer no longer throws.
creator.create();
tx.commit();
}
try (Transaction tx = db.beginTx()) {
tx.schema().awaitIndexOnline(NAME, 1, TimeUnit.MINUTES);
IndexDefinition index = tx.schema().getIndexByName(NAME);
Schema.IndexState indexState = tx.schema().getIndexState(index);
assertThat(indexState).isEqualTo(Schema.IndexState.ONLINE);
}
BrokenAnalyzerProvider.shouldThrow = true;
controller.restartDbms();
try (Transaction tx = db.beginTx()) {
IndexDefinition index = tx.schema().getIndexByName(NAME);
Schema.IndexState indexState = tx.schema().getIndexState(index);
assertThat(indexState).isEqualTo(Schema.IndexState.FAILED);
String indexFailure = tx.schema().getIndexFailure(index);
assertThat(indexFailure).contains("boom");
index.drop();
tx.commit();
}
try (Transaction tx = db.beginTx()) {
assertThrows(IllegalArgumentException.class, () -> tx.schema().getIndexByName(NAME));
tx.commit();
}
}
use of org.neo4j.graphdb.schema.IndexCreator in project neo4j by neo4j.
the class FulltextIndexProviderTest method indexWithAnalyzerThatThrowsWillNotBeCreated.
@ResourceLock("BrokenAnalyzerProvider")
@Test
void indexWithAnalyzerThatThrowsWillNotBeCreated() {
BrokenAnalyzerProvider.shouldThrow = true;
BrokenAnalyzerProvider.shouldReturnNull = false;
try (Transaction tx = db.beginTx()) {
IndexCreator creator = tx.schema().indexFor(label("Label")).withIndexType(org.neo4j.graphdb.schema.IndexType.FULLTEXT).withIndexConfiguration(Map.of(IndexSetting.fulltext_Analyzer(), BrokenAnalyzerProvider.NAME)).on("prop").withName(NAME);
// Validation must initially prevent this index from being created.
var e = assertThrows(RuntimeException.class, creator::create);
assertThat(e.getMessage()).contains("boom");
// Create the index anyway.
BrokenAnalyzerProvider.shouldThrow = false;
creator.create();
BrokenAnalyzerProvider.shouldThrow = true;
// The analyzer will now throw during the index population, and the index should then enter a FAILED state.
tx.commit();
}
try (Transaction tx = db.beginTx()) {
var e = assertThrows(IllegalStateException.class, () -> tx.schema().awaitIndexOnline(NAME, 10, TimeUnit.SECONDS));
assertThat(e.getMessage()).contains("FAILED");
IndexDefinition index = tx.schema().getIndexByName(NAME);
assertThat(tx.schema().getIndexState(index)).isEqualTo(Schema.IndexState.FAILED);
index.drop();
tx.commit();
}
BrokenAnalyzerProvider.shouldThrow = false;
try (Transaction tx = db.beginTx()) {
IndexCreator creator = tx.schema().indexFor(label("Label")).withIndexType(org.neo4j.graphdb.schema.IndexType.FULLTEXT).withIndexConfiguration(Map.of(IndexSetting.fulltext_Analyzer(), BrokenAnalyzerProvider.NAME)).on("prop").withName(NAME);
// The analyzer no longer throws.
creator.create();
tx.commit();
}
try (Transaction tx = db.beginTx()) {
tx.schema().awaitIndexOnline(NAME, 1, TimeUnit.MINUTES);
IndexDefinition index = tx.schema().getIndexByName(NAME);
Schema.IndexState indexState = tx.schema().getIndexState(index);
assertThat(indexState).isEqualTo(Schema.IndexState.ONLINE);
}
controller.restartDbms();
try (Transaction tx = db.beginTx()) {
IndexDefinition index = tx.schema().getIndexByName(NAME);
Schema.IndexState indexState = tx.schema().getIndexState(index);
assertThat(indexState).isEqualTo(Schema.IndexState.ONLINE);
}
}
use of org.neo4j.graphdb.schema.IndexCreator in project neo4j by neo4j.
the class BTreeIndexKeySizeValidationIT method createIndex.
private void createIndex(String... propKeys) {
try (Transaction tx = db.beginTx()) {
IndexCreator indexCreator = tx.schema().indexFor(LABEL_ONE);
for (String propKey : propKeys) {
indexCreator = indexCreator.on(propKey);
}
indexCreator.create();
tx.commit();
}
try (Transaction tx = db.beginTx()) {
tx.schema().awaitIndexesOnline(2, TimeUnit.MINUTES);
tx.commit();
}
}
use of org.neo4j.graphdb.schema.IndexCreator in project neo4j by neo4j.
the class SchemaAcceptanceTest method indexTokensCanContainBackTicks.
@Test
void indexTokensCanContainBackTicks() {
try (Transaction tx = db.beginTx()) {
IndexCreator creator = tx.schema().indexFor(labelWithBackticks).withName("abc").on(propertyKeyWithBackticks);
creator.create();
tx.commit();
}
try (Transaction tx = db.beginTx()) {
final Iterable<IndexDefinition> indexes = tx.schema().getIndexes();
assertThat(count(indexes)).isEqualTo(1L);
final IndexDefinition index = indexes.iterator().next();
assertEquals("abc", index.getName());
assertEquals(labelWithBackticks.name(), index.getLabels().iterator().next().name());
assertEquals(propertyKeyWithBackticks, index.getPropertyKeys().iterator().next());
assertThat(count(tx.schema().getConstraints())).isEqualTo(0L);
tx.commit();
}
}
Aggregations