Search in sources :

Example 6 with SchemaRule

use of org.neo4j.storageengine.api.schema.SchemaRule in project neo4j by neo4j.

the class PhysicalLogCommandReaderV3_0 method visitSchemaRuleCommand.

private Command visitSchemaRuleCommand(ReadableChannel channel) throws IOException {
    Collection<DynamicRecord> recordsBefore = new ArrayList<>();
    readDynamicRecords(channel, recordsBefore, COLLECTION_DYNAMIC_RECORD_ADDER);
    Collection<DynamicRecord> recordsAfter = new ArrayList<>();
    readDynamicRecords(channel, recordsAfter, COLLECTION_DYNAMIC_RECORD_ADDER);
    byte isCreated = channel.get();
    if (1 == isCreated) {
        for (DynamicRecord record : recordsAfter) {
            record.setCreated();
        }
    }
    SchemaRule rule = Iterables.first(recordsAfter).inUse() ? readSchemaRule(recordsAfter) : readSchemaRule(recordsBefore);
    return new Command.SchemaRuleCommand(recordsBefore, recordsAfter, rule);
}
Also used : DynamicRecord(org.neo4j.kernel.impl.store.record.DynamicRecord) ArrayList(java.util.ArrayList) SchemaRule(org.neo4j.storageengine.api.schema.SchemaRule)

Example 7 with SchemaRule

use of org.neo4j.storageengine.api.schema.SchemaRule in project neo4j by neo4j.

the class SchemaStoreTest method storeAndLoadAllRules.

@Test
public void storeAndLoadAllRules() throws Exception {
    // GIVEN
    long indexId = store.nextId();
    long constraintId = store.nextId();
    Collection<SchemaRule> rules = Arrays.asList(uniqueIndexRule(indexId, constraintId, PROVIDER_DESCRIPTOR, 2, 5, 3), constraintUniqueRule(constraintId, indexId, 2, 5, 3), indexRule(store.nextId(), PROVIDER_DESCRIPTOR, 0, 5), indexRule(store.nextId(), PROVIDER_DESCRIPTOR, 1, 6, 10, 99), constraintExistsRule(store.nextId(), 5, 1));
    for (SchemaRule rule : rules) {
        storeRule(rule);
    }
    // WHEN
    Collection<SchemaRule> readRules = asCollection(store.loadAllSchemaRules());
    // THEN
    assertEquals(rules, readRules);
}
Also used : SchemaRule(org.neo4j.storageengine.api.schema.SchemaRule) Test(org.junit.Test)

Example 8 with SchemaRule

use of org.neo4j.storageengine.api.schema.SchemaRule 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 9 with SchemaRule

use of org.neo4j.storageengine.api.schema.SchemaRule in project neo4j by neo4j.

the class SchemaStorage method loadAllSchemaRules.

<ReturnType extends SchemaRule> Iterator<ReturnType> loadAllSchemaRules(final Predicate<ReturnType> predicate, final Class<ReturnType> returnType, final boolean ignoreMalformed) {
    return new PrefetchingIterator<ReturnType>() {

        private final long highestId = schemaStore.getHighestPossibleIdInUse();

        private long currentId = 1;

        /*record 0 contains the block size*/
        private final byte[] scratchData = newRecordBuffer();

        private final DynamicRecord record = schemaStore.newRecord();

        @Override
        protected ReturnType fetchNextOrNull() {
            while (currentId <= highestId) {
                long id = currentId++;
                schemaStore.getRecord(id, record, RecordLoad.FORCE);
                if (record.inUse() && record.isStartRecord()) {
                    try {
                        SchemaRule schemaRule = loadSingleSchemaRuleViaBuffer(id, scratchData);
                        if (returnType.isInstance(schemaRule)) {
                            ReturnType returnRule = returnType.cast(schemaRule);
                            if (predicate.test(returnRule)) {
                                return returnRule;
                            }
                        }
                    } catch (MalformedSchemaRuleException e) {
                        if (!ignoreMalformed) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
            return null;
        }
    };
}
Also used : DynamicRecord(org.neo4j.kernel.impl.store.record.DynamicRecord) PrefetchingIterator(org.neo4j.helpers.collection.PrefetchingIterator) MalformedSchemaRuleException(org.neo4j.kernel.api.exceptions.schema.MalformedSchemaRuleException) SchemaRule(org.neo4j.storageengine.api.schema.SchemaRule)

Example 10 with SchemaRule

use of org.neo4j.storageengine.api.schema.SchemaRule in project neo4j by neo4j.

the class PhysicalLogCommandReaderV2_1 method readSchemaRule.

private SchemaRule readSchemaRule(Collection<DynamicRecord> recordsBefore) {
    // TODO: Why was this assertion here?
    //            assert first(recordsBefore).inUse() : "Asked to deserialize schema records that were not in
    // use.";
    SchemaRule rule;
    ByteBuffer deserialized = AbstractDynamicStore.concatData(recordsBefore, new byte[100]);
    try {
        rule = SchemaRuleSerialization.deserialize(Iterables.first(recordsBefore).getId(), deserialized);
    } catch (MalformedSchemaRuleException e) {
        return null;
    }
    return rule;
}
Also used : MalformedSchemaRuleException(org.neo4j.kernel.api.exceptions.schema.MalformedSchemaRuleException) SchemaRule(org.neo4j.storageengine.api.schema.SchemaRule) ByteBuffer(java.nio.ByteBuffer)

Aggregations

SchemaRule (org.neo4j.storageengine.api.schema.SchemaRule)27 DynamicRecord (org.neo4j.kernel.impl.store.record.DynamicRecord)13 ArrayList (java.util.ArrayList)9 MalformedSchemaRuleException (org.neo4j.kernel.api.exceptions.schema.MalformedSchemaRuleException)8 ByteBuffer (java.nio.ByteBuffer)7 Test (org.junit.Test)4 SchemaRecord (org.neo4j.kernel.impl.store.record.SchemaRecord)4 NeoStores (org.neo4j.kernel.impl.store.NeoStores)3 LinkedList (java.util.LinkedList)2 IndexRule (org.neo4j.kernel.impl.store.record.IndexRule)2 LabelTokenRecord (org.neo4j.kernel.impl.store.record.LabelTokenRecord)2 NodeRecord (org.neo4j.kernel.impl.store.record.NodeRecord)2 PrimitiveRecord (org.neo4j.kernel.impl.store.record.PrimitiveRecord)2 PropertyKeyTokenRecord (org.neo4j.kernel.impl.store.record.PropertyKeyTokenRecord)2 PropertyRecord (org.neo4j.kernel.impl.store.record.PropertyRecord)2 RelationshipGroupRecord (org.neo4j.kernel.impl.store.record.RelationshipGroupRecord)2 RelationshipRecord (org.neo4j.kernel.impl.store.record.RelationshipRecord)2 RelationshipTypeTokenRecord (org.neo4j.kernel.impl.store.record.RelationshipTypeTokenRecord)2 List (java.util.List)1 Matchers.anyLong (org.mockito.Matchers.anyLong)1