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;
}
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.
}
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);
}
}
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();
}
}
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;
}
Aggregations