use of org.neo4j.kernel.api.Statement in project neo4j by neo4j.
the class StoreUpgradeIntegrationTest method getAllIndexes.
private static Iterator<NewIndexDescriptor> getAllIndexes(GraphDatabaseAPI db) {
try (Transaction ignored = db.beginTx()) {
ThreadToStatementContextBridge bridge = db.getDependencyResolver().resolveDependency(ThreadToStatementContextBridge.class);
Statement statement = bridge.get();
return Iterators.concat(statement.readOperations().indexesGetAll(), statement.readOperations().uniqueIndexesGetAll());
}
}
use of org.neo4j.kernel.api.Statement in project neo4j by neo4j.
the class StoreUpgradeIntegrationTest method checkGlobalNodeCount.
private static void checkGlobalNodeCount(Store store, GraphDatabaseAPI db) {
try (Transaction ignored = db.beginTx()) {
ThreadToStatementContextBridge bridge = db.getDependencyResolver().resolveDependency(ThreadToStatementContextBridge.class);
Statement statement = bridge.get();
assertThat(statement.readOperations().countsForNode(-1), is(store.expectedNodeCount));
}
}
use of org.neo4j.kernel.api.Statement in project neo4j by neo4j.
the class BuiltInProcedures method listIndexes.
@Description("List all indexes in the database.")
@Procedure(name = "db.indexes", mode = READ)
public Stream<IndexResult> listIndexes() throws ProcedureException {
try (Statement statement = tx.acquireStatement()) {
ReadOperations operations = statement.readOperations();
TokenNameLookup tokens = new StatementTokenNameLookup(operations);
List<NewIndexDescriptor> indexes = asList(operations.indexesGetAll());
Set<NewIndexDescriptor> uniqueIndexes = asSet(operations.uniqueIndexesGetAll());
indexes.addAll(uniqueIndexes);
indexes.sort(Comparator.comparing(a -> a.userDescription(tokens)));
ArrayList<IndexResult> result = new ArrayList<>();
for (NewIndexDescriptor index : indexes) {
try {
String type;
if (uniqueIndexes.contains(index)) {
type = IndexType.NODE_UNIQUE_PROPERTY.typeName();
} else {
type = IndexType.NODE_LABEL_PROPERTY.typeName();
}
result.add(new IndexResult("INDEX ON " + index.schema().userDescription(tokens), operations.indexGetState(index).toString(), type));
} catch (IndexNotFoundKernelException e) {
throw new ProcedureException(Status.Schema.IndexNotFound, e, "No index on ", index.userDescription(tokens));
}
}
return result.stream();
}
}
use of org.neo4j.kernel.api.Statement in project neo4j by neo4j.
the class SchemaProcedure method buildSchemaGraph.
public GraphResult buildSchemaGraph() {
final Map<String, NodeImpl> nodes = new HashMap<>();
final Map<String, Set<RelationshipImpl>> relationships = new HashMap<>();
try (Statement statement = kernelTransaction.acquireStatement()) {
ReadOperations readOperations = statement.readOperations();
StatementTokenNameLookup statementTokenNameLookup = new StatementTokenNameLookup(readOperations);
try (Transaction transaction = graphDatabaseAPI.beginTx()) {
// add all labelsInDatabase
ResourceIterator<Label> labelsInDatabase = graphDatabaseAPI.getAllLabelsInUse().iterator();
while (labelsInDatabase.hasNext()) {
Label label = labelsInDatabase.next();
Map<String, Object> properties = new HashMap<>();
Iterator<NewIndexDescriptor> indexDescriptorIterator = readOperations.indexesGetForLabel(readOperations.labelGetForName(label.name()));
ArrayList<String> indexes = new ArrayList<>();
while (indexDescriptorIterator.hasNext()) {
NewIndexDescriptor index = indexDescriptorIterator.next();
String[] propertyNames = PropertyNameUtils.getPropertyKeys(statementTokenNameLookup, index.schema().getPropertyIds());
indexes.add(String.join(",", propertyNames));
}
properties.put("indexes", indexes);
Iterator<ConstraintDescriptor> nodePropertyConstraintIterator = readOperations.constraintsGetForLabel(readOperations.labelGetForName(label.name()));
ArrayList<String> constraints = new ArrayList<>();
while (nodePropertyConstraintIterator.hasNext()) {
PropertyConstraint constraint = ConstraintBoundary.map(nodePropertyConstraintIterator.next());
constraints.add(constraint.userDescription(statementTokenNameLookup));
}
properties.put("constraints", constraints);
getOrCreateLabel(label.name(), properties, nodes);
}
//add all relationships
Iterator<RelationshipType> relationshipTypeIterator = graphDatabaseAPI.getAllRelationshipTypesInUse().iterator();
while (relationshipTypeIterator.hasNext()) {
RelationshipType relationshipType = relationshipTypeIterator.next();
String relationshipTypeGetName = relationshipType.name();
int relId = readOperations.relationshipTypeGetForName(relationshipTypeGetName);
ResourceIterator<Label> labelsInUse = graphDatabaseAPI.getAllLabelsInUse().iterator();
List<NodeImpl> startNodes = new LinkedList<>();
List<NodeImpl> endNodes = new LinkedList<>();
while (labelsInUse.hasNext()) {
Label labelToken = labelsInUse.next();
String labelName = labelToken.name();
Map<String, Object> properties = new HashMap<>();
NodeImpl node = getOrCreateLabel(labelName, properties, nodes);
int labelId = readOperations.labelGetForName(labelName);
if (readOperations.countsForRelationship(labelId, relId, ReadOperations.ANY_LABEL) > 0) {
startNodes.add(node);
}
if (readOperations.countsForRelationship(ReadOperations.ANY_LABEL, relId, labelId) > 0) {
endNodes.add(node);
}
}
for (NodeImpl startNode : startNodes) {
for (NodeImpl endNode : endNodes) {
RelationshipImpl relationship = addRelationship(startNode, endNode, relationshipTypeGetName, relationships);
}
}
}
transaction.success();
return getGraphResult(nodes, relationships);
}
}
}
use of org.neo4j.kernel.api.Statement in project neo4j by neo4j.
the class RemoveOrphanConstraintIndexesOnStartup method perform.
public void perform() {
try (KernelTransaction transaction = kernel.newTransaction(KernelTransaction.Type.implicit, AUTH_DISABLED);
Statement statement = transaction.acquireStatement()) {
for (NewIndexDescriptor index : loop(statement.readOperations().uniqueIndexesGetAll())) {
if (statement.readOperations().indexGetOwningUniquenessConstraintId(index) == null) {
log.info("Removing orphan constraint index: " + index);
statement.schemaWriteOperations().uniqueIndexDrop(index);
}
}
transaction.success();
} catch (KernelException e) {
log.error("Failed to execute orphan index checking transaction.", e);
}
}
Aggregations