Search in sources :

Example 16 with IndexRule

use of org.neo4j.kernel.impl.store.record.IndexRule in project neo4j by neo4j.

the class BatchInsertTest method shouldCreateConsistentUniquenessConstraint.

@Test
public void shouldCreateConsistentUniquenessConstraint() throws Exception {
    // given
    BatchInserter inserter = newBatchInserter();
    // when
    inserter.createDeferredConstraint(label("Hacker")).assertPropertyIsUnique("handle").create();
    // then
    GraphDatabaseAPI graphdb = (GraphDatabaseAPI) switchToEmbeddedGraphDatabaseService(inserter);
    try {
        NeoStores neoStores = graphdb.getDependencyResolver().resolveDependency(RecordStorageEngine.class).testAccessNeoStores();
        SchemaStore store = neoStores.getSchemaStore();
        SchemaStorage storage = new SchemaStorage(store);
        List<Long> inUse = new ArrayList<>();
        DynamicRecord record = store.nextRecord();
        for (long i = 1, high = store.getHighestPossibleIdInUse(); i <= high; i++) {
            store.getRecord(i, record, RecordLoad.FORCE);
            if (record.inUse() && record.isStartRecord()) {
                inUse.add(i);
            }
        }
        assertEquals("records in use", 2, inUse.size());
        SchemaRule rule0 = storage.loadSingleSchemaRule(inUse.get(0));
        SchemaRule rule1 = storage.loadSingleSchemaRule(inUse.get(1));
        IndexRule indexRule;
        ConstraintRule constraintRule;
        if (rule0 instanceof IndexRule) {
            indexRule = (IndexRule) rule0;
            constraintRule = (ConstraintRule) rule1;
        } else {
            constraintRule = (ConstraintRule) rule0;
            indexRule = (IndexRule) rule1;
        }
        assertEquals("index should reference constraint", constraintRule.getId(), indexRule.getOwningConstraint().longValue());
        assertEquals("constraint should reference index", indexRule.getId(), constraintRule.getOwnedIndex());
    } finally {
        graphdb.shutdown();
    }
}
Also used : DynamicRecord(org.neo4j.kernel.impl.store.record.DynamicRecord) IndexRule(org.neo4j.kernel.impl.store.record.IndexRule) SchemaStore(org.neo4j.kernel.impl.store.SchemaStore) ArrayList(java.util.ArrayList) SchemaRule(org.neo4j.storageengine.api.schema.SchemaRule) BatchInserter(org.neo4j.unsafe.batchinsert.BatchInserter) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) SchemaStorage(org.neo4j.kernel.impl.store.SchemaStorage) ConstraintRule(org.neo4j.kernel.impl.store.record.ConstraintRule) RecordStorageEngine(org.neo4j.kernel.impl.storageengine.impl.recordstorage.RecordStorageEngine) NeoStores(org.neo4j.kernel.impl.store.NeoStores) Matchers.anyLong(org.mockito.Matchers.anyLong) Test(org.junit.Test)

Example 17 with IndexRule

use of org.neo4j.kernel.impl.store.record.IndexRule in project neo4j by neo4j.

the class IndexLookup method getAnyIndexOrNull.

public Index getAnyIndexOrNull(final long[] labelIds, final int propertyKeyId) throws IOException {
    List<IndexRule> indexRules = indexRuleIndex.get(propertyKeyId);
    if (indexRules == null) {
        return null;
    }
    IndexRule rule = findIndexRuleWithOneOfLabels(indexRules, labelIds);
    if (rule == null) {
        return null;
    }
    final IndexReader reader = getIndexReader(rule);
    return (nodeId, propertyValue) -> reader.countIndexedNodes(nodeId, propertyValue) > 0;
}
Also used : Primitive(org.neo4j.collection.primitive.Primitive) Config(org.neo4j.kernel.configuration.Config) SchemaStore(org.neo4j.kernel.impl.store.SchemaStore) IndexRule(org.neo4j.kernel.impl.store.record.IndexRule) IOException(java.io.IOException) HashMap(java.util.HashMap) IndexAccessor(org.neo4j.kernel.api.index.IndexAccessor) SchemaRule(org.neo4j.storageengine.api.schema.SchemaRule) IndexReader(org.neo4j.storageengine.api.schema.IndexReader) ArrayList(java.util.ArrayList) SchemaDescriptorPredicates(org.neo4j.kernel.api.schema_new.SchemaDescriptorPredicates) List(java.util.List) SchemaIndexProvider(org.neo4j.kernel.api.index.SchemaIndexProvider) Map(java.util.Map) PrimitiveIntObjectMap(org.neo4j.collection.primitive.PrimitiveIntObjectMap) LinkedList(java.util.LinkedList) IndexSamplingConfig(org.neo4j.kernel.impl.api.index.sampling.IndexSamplingConfig) IndexRule(org.neo4j.kernel.impl.store.record.IndexRule) IndexReader(org.neo4j.storageengine.api.schema.IndexReader)

Example 18 with IndexRule

use of org.neo4j.kernel.impl.store.record.IndexRule in project neo4j by neo4j.

the class FullCheckIntegrationTest method shouldReportNodesThatAreNotIndexed.

@Test
public void shouldReportNodesThatAreNotIndexed() throws Exception {
    // given
    IndexSamplingConfig samplingConfig = new IndexSamplingConfig(Config.empty());
    Iterator<IndexRule> indexRuleIterator = new SchemaStorage(fixture.directStoreAccess().nativeStores().getSchemaStore()).indexesGetAll();
    while (indexRuleIterator.hasNext()) {
        IndexRule indexRule = indexRuleIterator.next();
        IndexAccessor accessor = fixture.directStoreAccess().indexes().getOnlineAccessor(indexRule.getId(), indexRule.getIndexDescriptor(), samplingConfig);
        IndexUpdater updater = accessor.newUpdater(IndexUpdateMode.ONLINE);
        updater.remove(asPrimitiveLongSet(indexedNodes));
        updater.close();
        accessor.close();
    }
    // when
    ConsistencySummaryStatistics stats = check();
    // then
    on(stats).verify(RecordType.NODE, 1).andThatsAllFolks();
}
Also used : IndexSamplingConfig(org.neo4j.kernel.impl.api.index.sampling.IndexSamplingConfig) IndexRule(org.neo4j.kernel.impl.store.record.IndexRule) SchemaRuleUtil.constraintIndexRule(org.neo4j.consistency.checking.SchemaRuleUtil.constraintIndexRule) SchemaStorage(org.neo4j.kernel.impl.store.SchemaStorage) IndexAccessor(org.neo4j.kernel.api.index.IndexAccessor) IndexUpdater(org.neo4j.kernel.api.index.IndexUpdater) ConsistencySummaryStatistics(org.neo4j.consistency.report.ConsistencySummaryStatistics) Test(org.junit.Test)

Example 19 with IndexRule

use of org.neo4j.kernel.impl.store.record.IndexRule in project neo4j by neo4j.

the class FullCheckIntegrationTest method shouldReportDuplicateConstraintReferences.

@Test
public void shouldReportDuplicateConstraintReferences() throws Exception {
    // given
    fixture.apply(new GraphStoreFixture.Transaction() {

        @Override
        protected void transactionData(GraphStoreFixture.TransactionDataBuilder tx, GraphStoreFixture.IdGenerator next) {
            int ruleId1 = (int) next.schema();
            int ruleId2 = (int) next.schema();
            int labelId = next.label();
            int propertyKeyId = next.propertyKey();
            DynamicRecord record1 = new DynamicRecord(ruleId1);
            DynamicRecord record2 = new DynamicRecord(ruleId2);
            DynamicRecord record1Before = record1.clone();
            DynamicRecord record2Before = record2.clone();
            IndexRule rule1 = constraintIndexRule(ruleId1, labelId, propertyKeyId, DESCRIPTOR, (long) ruleId1);
            IndexRule rule2 = constraintIndexRule(ruleId2, labelId, propertyKeyId, DESCRIPTOR, (long) ruleId1);
            Collection<DynamicRecord> records1 = serializeRule(rule1, record1);
            Collection<DynamicRecord> records2 = serializeRule(rule2, record2);
            assertEquals(asList(record1), records1);
            assertEquals(asList(record2), records2);
            tx.nodeLabel(labelId, "label");
            tx.propertyKey(propertyKeyId, "property");
            tx.createSchema(asList(record1Before), records1, rule1);
            tx.createSchema(asList(record2Before), records2, rule2);
        }
    });
    // when
    ConsistencySummaryStatistics stats = check();
    // then
    on(stats).verify(RecordType.SCHEMA, 4).andThatsAllFolks();
}
Also used : DynamicRecord(org.neo4j.kernel.impl.store.record.DynamicRecord) IndexRule(org.neo4j.kernel.impl.store.record.IndexRule) SchemaRuleUtil.constraintIndexRule(org.neo4j.consistency.checking.SchemaRuleUtil.constraintIndexRule) TransactionDataBuilder(org.neo4j.consistency.checking.GraphStoreFixture.TransactionDataBuilder) IdGenerator(org.neo4j.consistency.checking.GraphStoreFixture.IdGenerator) Collection(java.util.Collection) GraphStoreFixture(org.neo4j.consistency.checking.GraphStoreFixture) ConsistencySummaryStatistics(org.neo4j.consistency.report.ConsistencySummaryStatistics) Test(org.junit.Test)

Example 20 with IndexRule

use of org.neo4j.kernel.impl.store.record.IndexRule in project neo4j by neo4j.

the class SchemaRecordCheckTest method shouldReportTwoConstraintIndexesReferencingSameConstraint.

@Test
public void shouldReportTwoConstraintIndexesReferencingSameConstraint() throws Exception {
    // given
    int ruleId1 = 0;
    int ruleId2 = 1;
    DynamicRecord record1 = inUse(dynamicRecord(ruleId1));
    DynamicRecord record2 = inUse(dynamicRecord(ruleId2));
    SchemaIndexProvider.Descriptor providerDescriptor = new SchemaIndexProvider.Descriptor("in-memory", "1.0");
    IndexRule rule1 = constraintIndexRule(ruleId1, labelId, propertyKeyId, providerDescriptor, (long) ruleId1);
    IndexRule rule2 = constraintIndexRule(ruleId2, labelId, propertyKeyId, providerDescriptor, (long) ruleId1);
    when(checker().ruleAccess.loadSingleSchemaRule(ruleId1)).thenReturn(rule1);
    when(checker().ruleAccess.loadSingleSchemaRule(ruleId2)).thenReturn(rule2);
    add(inUse(new LabelTokenRecord(labelId)));
    add(inUse(new PropertyKeyTokenRecord(propertyKeyId)));
    // when
    check(record1);
    ConsistencyReport.SchemaConsistencyReport report = check(record2);
    // then
    verify(report).duplicateObligation(record1);
}
Also used : DynamicRecord(org.neo4j.kernel.impl.store.record.DynamicRecord) IndexRule(org.neo4j.kernel.impl.store.record.IndexRule) SchemaRuleUtil.constraintIndexRule(org.neo4j.consistency.checking.SchemaRuleUtil.constraintIndexRule) SchemaIndexProvider(org.neo4j.kernel.api.index.SchemaIndexProvider) NodePropertyDescriptor(org.neo4j.kernel.api.schema.NodePropertyDescriptor) LabelTokenRecord(org.neo4j.kernel.impl.store.record.LabelTokenRecord) PropertyKeyTokenRecord(org.neo4j.kernel.impl.store.record.PropertyKeyTokenRecord) ConsistencyReport(org.neo4j.consistency.report.ConsistencyReport) Test(org.junit.Test)

Aggregations

IndexRule (org.neo4j.kernel.impl.store.record.IndexRule)60 Test (org.junit.Test)42 SchemaIndexProvider (org.neo4j.kernel.api.index.SchemaIndexProvider)24 DynamicRecord (org.neo4j.kernel.impl.store.record.DynamicRecord)22 SchemaRuleUtil.constraintIndexRule (org.neo4j.consistency.checking.SchemaRuleUtil.constraintIndexRule)14 NodePropertyDescriptor (org.neo4j.kernel.api.schema.NodePropertyDescriptor)9 ConstraintRule (org.neo4j.kernel.impl.store.record.ConstraintRule)9 LabelTokenRecord (org.neo4j.kernel.impl.store.record.LabelTokenRecord)9 PropertyKeyTokenRecord (org.neo4j.kernel.impl.store.record.PropertyKeyTokenRecord)9 ConsistencyReport (org.neo4j.consistency.report.ConsistencyReport)8 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)8 IndexSamplingConfig (org.neo4j.kernel.impl.api.index.sampling.IndexSamplingConfig)8 IndexAccessor (org.neo4j.kernel.api.index.IndexAccessor)7 BatchTransactionApplier (org.neo4j.kernel.impl.api.BatchTransactionApplier)7 SchemaStorage (org.neo4j.kernel.impl.store.SchemaStorage)7 LabelTokenCommand (org.neo4j.kernel.impl.transaction.command.Command.LabelTokenCommand)7 PropertyKeyTokenCommand (org.neo4j.kernel.impl.transaction.command.Command.PropertyKeyTokenCommand)7 RelationshipTypeTokenCommand (org.neo4j.kernel.impl.transaction.command.Command.RelationshipTypeTokenCommand)7 ArrayList (java.util.ArrayList)6 ConsistencySummaryStatistics (org.neo4j.consistency.report.ConsistencySummaryStatistics)6