Search in sources :

Example 41 with NewIndexDescriptor

use of org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor in project neo4j by neo4j.

the class StoreUpgradeIntegrationTest method checkIndexCounts.

private static void checkIndexCounts(Store store, GraphDatabaseAPI db) throws KernelException {
    KernelAPI kernel = db.getDependencyResolver().resolveDependency(KernelAPI.class);
    try (KernelTransaction tx = kernel.newTransaction(KernelTransaction.Type.implicit, AnonymousContext.read());
        Statement statement = tx.acquireStatement()) {
        Iterator<NewIndexDescriptor> indexes = getAllIndexes(db);
        DoubleLongRegister register = Registers.newDoubleLongRegister();
        for (int i = 0; indexes.hasNext(); i++) {
            NewIndexDescriptor descriptor = indexes.next();
            // wait index to be online since sometimes we need to rebuild the indexes on migration
            awaitOnline(statement.readOperations(), descriptor);
            assertDoubleLongEquals(store.indexCounts[i][0], store.indexCounts[i][1], statement.readOperations().indexUpdatesAndSize(descriptor, register));
            assertDoubleLongEquals(store.indexCounts[i][2], store.indexCounts[i][3], statement.readOperations().indexSample(descriptor, register));
            double selectivity = statement.readOperations().indexUniqueValuesSelectivity(descriptor);
            assertEquals(store.indexSelectivity[i], selectivity, 0.0000001d);
        }
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) Statement(org.neo4j.kernel.api.Statement) DoubleLongRegister(org.neo4j.register.Register.DoubleLongRegister) KernelAPI(org.neo4j.kernel.api.KernelAPI)

Example 42 with NewIndexDescriptor

use of org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor 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();
    }
}
Also used : IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) Label(org.neo4j.graphdb.Label) Context(org.neo4j.procedure.Context) Status(org.neo4j.kernel.api.exceptions.Status) Iterators.asSet(org.neo4j.helpers.collection.Iterators.asSet) StatementTokenNameLookup(org.neo4j.kernel.api.StatementTokenNameLookup) Statement(org.neo4j.kernel.api.Statement) IndexNotFoundKernelException(org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException) ArrayList(java.util.ArrayList) TokenNameLookup(org.neo4j.kernel.api.TokenNameLookup) Procedure(org.neo4j.procedure.Procedure) TokenAccess(org.neo4j.kernel.impl.api.TokenAccess) ReadOperations(org.neo4j.kernel.api.ReadOperations) Set(java.util.Set) READ(org.neo4j.procedure.Mode.READ) Description(org.neo4j.procedure.Description) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Stream(java.util.stream.Stream) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Iterators.asList(org.neo4j.helpers.collection.Iterators.asList) DependencyResolver(org.neo4j.graphdb.DependencyResolver) Name(org.neo4j.procedure.Name) RelationshipType(org.neo4j.graphdb.RelationshipType) Comparator(java.util.Comparator) StatementTokenNameLookup(org.neo4j.kernel.api.StatementTokenNameLookup) Statement(org.neo4j.kernel.api.Statement) ArrayList(java.util.ArrayList) IndexNotFoundKernelException(org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException) ReadOperations(org.neo4j.kernel.api.ReadOperations) StatementTokenNameLookup(org.neo4j.kernel.api.StatementTokenNameLookup) TokenNameLookup(org.neo4j.kernel.api.TokenNameLookup) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) Description(org.neo4j.procedure.Description) Procedure(org.neo4j.procedure.Procedure)

Example 43 with NewIndexDescriptor

use of org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor 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);
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Label(org.neo4j.graphdb.Label) ArrayList(java.util.ArrayList) RelationshipType(org.neo4j.graphdb.RelationshipType) PropertyConstraint(org.neo4j.kernel.api.constraints.PropertyConstraint) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) StatementTokenNameLookup(org.neo4j.kernel.api.StatementTokenNameLookup) Statement(org.neo4j.kernel.api.Statement) PropertyConstraint(org.neo4j.kernel.api.constraints.PropertyConstraint) LinkedList(java.util.LinkedList) ReadOperations(org.neo4j.kernel.api.ReadOperations) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor)

Example 44 with NewIndexDescriptor

use of org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor in project neo4j by neo4j.

the class IndexingService method start.

// Recovery semantics: This is to be called after init, and after the database has run recovery.
@Override
public void start() throws Exception {
    state = State.STARTING;
    applyRecoveredUpdates();
    IndexMap indexMap = indexMapRef.indexMapSnapshot();
    final Map<Long, RebuildingIndexDescriptor> rebuildingDescriptors = new HashMap<>();
    // Find all indexes that are not already online, do not require rebuilding, and create them
    indexMap.foreachIndexProxy((indexId, proxy) -> {
        InternalIndexState state = proxy.getState();
        NewIndexDescriptor descriptor = proxy.getDescriptor();
        log.info(indexStateInfo("start", indexId, state, descriptor));
        switch(state) {
            case ONLINE:
                // Don't do anything, index is ok.
                break;
            case POPULATING:
                // Remember for rebuilding
                rebuildingDescriptors.put(indexId, new RebuildingIndexDescriptor(descriptor, proxy.getProviderDescriptor()));
                break;
            case FAILED:
                // Don't do anything, the user needs to drop the index and re-create
                break;
            default:
                throw new IllegalStateException("Unknown state: " + state);
        }
    });
    // Drop placeholder proxies for indexes that need to be rebuilt
    dropRecoveringIndexes(indexMap, rebuildingDescriptors.keySet());
    // Rebuild indexes by recreating and repopulating them
    if (!rebuildingDescriptors.isEmpty()) {
        IndexPopulationJob populationJob = newIndexPopulationJob();
        for (Map.Entry<Long, RebuildingIndexDescriptor> entry : rebuildingDescriptors.entrySet()) {
            long indexId = entry.getKey();
            RebuildingIndexDescriptor descriptor = entry.getValue();
            IndexProxy proxy = indexProxyCreator.createPopulatingIndexProxy(indexId, descriptor.getIndexDescriptor(), descriptor.getProviderDescriptor(), // never pass through a tentative online state during recovery
            false, monitor, populationJob);
            proxy.start();
            indexMap.putIndexProxy(indexId, proxy);
        }
        startIndexPopulation(populationJob);
    }
    indexMapRef.setIndexMap(indexMap);
    samplingController.recoverIndexSamples();
    samplingController.start();
    // This is why we now go and wait for those indexes to be fully populated.
    for (Map.Entry<Long, RebuildingIndexDescriptor> entry : rebuildingDescriptors.entrySet()) {
        if (entry.getValue().getIndexDescriptor().type() != NewIndexDescriptor.Type.UNIQUE) {
            // It's not a uniqueness constraint, so don't wait for it to be rebuilt
            continue;
        }
        IndexProxy proxy;
        try {
            proxy = getIndexProxy(entry.getKey());
        } catch (IndexNotFoundKernelException e) {
            throw new IllegalStateException("What? This index was seen during recovery just now, why isn't it available now?");
        }
        monitor.awaitingPopulationOfRecoveredIndex(entry.getKey(), entry.getValue().getIndexDescriptor());
        awaitOnline(proxy);
    }
    state = State.RUNNING;
}
Also used : HashMap(java.util.HashMap) IndexNotFoundKernelException(org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException) InternalIndexState(org.neo4j.kernel.api.index.InternalIndexState) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) Map(java.util.Map) HashMap(java.util.HashMap)

Example 45 with NewIndexDescriptor

use of org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor in project neo4j by neo4j.

the class OperationsFacade method indexGetForLabelAndPropertyKey.

// </DataReadCursors>
// <SchemaRead>
@Override
public NewIndexDescriptor indexGetForLabelAndPropertyKey(NodePropertyDescriptor descriptor) throws SchemaRuleNotFoundException {
    statement.assertOpen();
    NewIndexDescriptor indexDescriptor = schemaRead().indexGetForLabelAndPropertyKey(statement, descriptor);
    if (indexDescriptor == null) {
        throw new SchemaRuleNotFoundException(SchemaRule.Kind.INDEX_RULE, SchemaBoundary.map(descriptor));
    }
    return indexDescriptor;
}
Also used : NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) SchemaRuleNotFoundException(org.neo4j.kernel.api.exceptions.schema.SchemaRuleNotFoundException)

Aggregations

NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)99 Test (org.junit.Test)55 Statement (org.neo4j.kernel.api.Statement)24 ReadOperations (org.neo4j.kernel.api.ReadOperations)17 IndexNotFoundKernelException (org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException)10 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)9 SchemaIndexProvider (org.neo4j.kernel.api.index.SchemaIndexProvider)9 InternalIndexState (org.neo4j.kernel.api.index.InternalIndexState)7 Transaction (org.neo4j.graphdb.Transaction)6 IndexDefinition (org.neo4j.graphdb.schema.IndexDefinition)5 IndexEntryConflictException (org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException)5 SchemaRuleNotFoundException (org.neo4j.kernel.api.exceptions.schema.SchemaRuleNotFoundException)5 LabelSchemaDescriptor (org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 PrimitiveLongSet (org.neo4j.collection.primitive.PrimitiveLongSet)4 Label (org.neo4j.graphdb.Label)4 NotFoundException (org.neo4j.graphdb.NotFoundException)4 KernelException (org.neo4j.kernel.api.exceptions.KernelException)4 NodePropertyDescriptor (org.neo4j.kernel.api.schema.NodePropertyDescriptor)4