Search in sources :

Example 21 with RelationshipRecord

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

the class RelationshipRecordCheckTest method shouldReportTargetPrevReferencingOtherNodes.

@Test
public void shouldReportTargetPrevReferencingOtherNodes() throws Exception {
    // given
    RelationshipRecord relationship = inUse(new RelationshipRecord(42, 1, 2, 4));
    add(inUse(new RelationshipTypeTokenRecord(4)));
    add(inUse(new NodeRecord(1, false, 42, NONE)));
    add(inUse(new NodeRecord(2, false, 0, NONE)));
    RelationshipRecord tPrev = add(inUse(new RelationshipRecord(51, 8, 9, 0)));
    relationship.setSecondPrevRel(tPrev.getId());
    relationship.setFirstInSecondChain(false);
    // when
    ConsistencyReport.RelationshipConsistencyReport report = check(relationship);
    // then
    verify(report).targetPrevReferencesOtherNodes(tPrev);
    verifyNoMoreInteractions(report);
}
Also used : NodeRecord(org.neo4j.kernel.impl.store.record.NodeRecord) RelationshipTypeTokenRecord(org.neo4j.kernel.impl.store.record.RelationshipTypeTokenRecord) RelationshipRecord(org.neo4j.kernel.impl.store.record.RelationshipRecord) ConsistencyReport(org.neo4j.consistency.report.ConsistencyReport) Test(org.junit.Test)

Example 22 with RelationshipRecord

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

the class RelationshipRecordCheckTest method shouldReportSourceNextReferencingOtherNodesWhenReferencingTargetNode.

@Test
public void shouldReportSourceNextReferencingOtherNodesWhenReferencingTargetNode() throws Exception {
    // given
    RelationshipRecord relationship = inUse(new RelationshipRecord(42, 1, 2, 4));
    add(inUse(new RelationshipTypeTokenRecord(4)));
    add(inUse(new NodeRecord(1, false, 42, NONE)));
    add(inUse(new NodeRecord(2, false, 42, NONE)));
    RelationshipRecord sNext = add(inUse(new RelationshipRecord(51, 2, 0, 0)));
    relationship.setFirstNextRel(sNext.getId());
    // when
    ConsistencyReport.RelationshipConsistencyReport report = check(relationship);
    // then
    verify(report).sourceNextReferencesOtherNodes(sNext);
    verifyNoMoreInteractions(report);
}
Also used : NodeRecord(org.neo4j.kernel.impl.store.record.NodeRecord) RelationshipTypeTokenRecord(org.neo4j.kernel.impl.store.record.RelationshipTypeTokenRecord) RelationshipRecord(org.neo4j.kernel.impl.store.record.RelationshipRecord) ConsistencyReport(org.neo4j.consistency.report.ConsistencyReport) Test(org.junit.Test)

Example 23 with RelationshipRecord

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

the class RelationshipRecordCheckTest method shouldReportTargetNodeWithoutChainForRelationshipInTheMiddleOfChain.

@Test
public void shouldReportTargetNodeWithoutChainForRelationshipInTheMiddleOfChain() throws Exception {
    // given
    checkSingleDirection();
    initialize(RELATIONSHIPS, NODES);
    RelationshipRecord relationship = inUse(new RelationshipRecord(42, 1, 2, 4));
    add(inUse(new RelationshipTypeTokenRecord(4)));
    add(inUse(new NodeRecord(1, false, 42, NONE)));
    NodeRecord target = add(inUse(new NodeRecord(2, false, NONE, NONE)));
    RelationshipRecord tPrev = add(inUse(new RelationshipRecord(51, 0, 2, 0)));
    relationship.setSecondPrevRel(tPrev.getId());
    relationship.setFirstInSecondChain(false);
    tPrev.setSecondNextRel(relationship.getId());
    // when
    ConsistencyReport.RelationshipConsistencyReport report = check(relationship);
    // then
    verify(report).targetNodeHasNoRelationships(target);
    verifyNoMoreInteractions(report);
}
Also used : NodeRecord(org.neo4j.kernel.impl.store.record.NodeRecord) RelationshipTypeTokenRecord(org.neo4j.kernel.impl.store.record.RelationshipTypeTokenRecord) RelationshipRecord(org.neo4j.kernel.impl.store.record.RelationshipRecord) ConsistencyReport(org.neo4j.consistency.report.ConsistencyReport) Test(org.junit.Test)

Example 24 with RelationshipRecord

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

the class TransactionRecordState method extractCommands.

@Override
public void extractCommands(Collection<StorageCommand> commands) throws TransactionFailureException {
    assert !prepared : "Transaction has already been prepared";
    integrityValidator.validateTransactionStartKnowledge(lastCommittedTxWhenTransactionStarted);
    int noOfCommands = recordChangeSet.changeSize() + (neoStoreRecord != null ? neoStoreRecord.changeSize() : 0);
    for (RecordProxy<Integer, LabelTokenRecord, Void> record : recordChangeSet.getLabelTokenChanges().changes()) {
        commands.add(new Command.LabelTokenCommand(record.getBefore(), record.forReadingLinkage()));
    }
    for (RecordProxy<Integer, RelationshipTypeTokenRecord, Void> record : recordChangeSet.getRelationshipTypeTokenChanges().changes()) {
        commands.add(new Command.RelationshipTypeTokenCommand(record.getBefore(), record.forReadingLinkage()));
    }
    for (RecordProxy<Integer, PropertyKeyTokenRecord, Void> record : recordChangeSet.getPropertyKeyTokenChanges().changes()) {
        commands.add(new Command.PropertyKeyTokenCommand(record.getBefore(), record.forReadingLinkage()));
    }
    // Collect nodes, relationships, properties
    Command[] nodeCommands = EMPTY_COMMANDS;
    int skippedCommands = 0;
    if (recordChangeSet.getNodeRecords().changeSize() > 0) {
        nodeCommands = new Command[recordChangeSet.getNodeRecords().changeSize()];
        int i = 0;
        for (RecordProxy<Long, NodeRecord, Void> change : recordChangeSet.getNodeRecords().changes()) {
            NodeRecord record = prepared(change, nodeStore);
            integrityValidator.validateNodeRecord(record);
            nodeCommands[i++] = new Command.NodeCommand(change.getBefore(), record);
        }
        Arrays.sort(nodeCommands, COMMAND_SORTER);
    }
    Command[] relCommands = EMPTY_COMMANDS;
    if (recordChangeSet.getRelRecords().changeSize() > 0) {
        relCommands = new Command[recordChangeSet.getRelRecords().changeSize()];
        int i = 0;
        for (RecordProxy<Long, RelationshipRecord, Void> change : recordChangeSet.getRelRecords().changes()) {
            relCommands[i++] = new Command.RelationshipCommand(change.getBefore(), prepared(change, relationshipStore));
        }
        Arrays.sort(relCommands, COMMAND_SORTER);
    }
    Command[] propCommands = EMPTY_COMMANDS;
    if (recordChangeSet.getPropertyRecords().changeSize() > 0) {
        propCommands = new Command[recordChangeSet.getPropertyRecords().changeSize()];
        int i = 0;
        for (RecordProxy<Long, PropertyRecord, PrimitiveRecord> change : recordChangeSet.getPropertyRecords().changes()) {
            propCommands[i++] = new Command.PropertyCommand(change.getBefore(), prepared(change, propertyStore));
        }
        Arrays.sort(propCommands, COMMAND_SORTER);
    }
    Command[] relGroupCommands = EMPTY_COMMANDS;
    if (recordChangeSet.getRelGroupRecords().changeSize() > 0) {
        relGroupCommands = new Command[recordChangeSet.getRelGroupRecords().changeSize()];
        int i = 0;
        for (RecordProxy<Long, RelationshipGroupRecord, Integer> change : recordChangeSet.getRelGroupRecords().changes()) {
            if (change.isCreated() && !change.forReadingLinkage().inUse()) {
                /*
                     * This is an edge case that may come up and which we must handle properly. Relationship groups are
                     * not managed by the tx state, since they are created as side effects rather than through
                     * direct calls. However, they differ from say, dynamic records, in that their management can happen
                     * through separate code paths. What we are interested in here is the following scenario.
                     * 0. A node has one less relationship that is required to transition to dense node. The relationships
                     *    it has belong to at least two different types
                     * 1. In the same tx, a relationship is added making the node dense and all the relationships of a type
                     *    are removed from that node. Regardless of the order these operations happen, the creation of the
                     *    relationship (and the transition of the node to dense) will happen first.
                     * 2. A relationship group will be created because of the transition to dense and then deleted because
                     *    all the relationships it would hold are no longer there. This results in a relationship group
                     *    command that appears in the tx as not in use. Depending on the final order of operations, this
                     *    can end up using an id that is higher than the highest id seen so far. This may not be a problem
                     *    for a single instance, but it can result in errors in cases where transactions are applied
                     *    externally, such as backup or HA.
                     *
                     * The way we deal with this issue here is by not issuing a command for that offending record. This is
                     * safe, since the record is not in use and never was, so the high id is not necessary to change and
                     * the store remains consistent.
                     */
                skippedCommands++;
                continue;
            }
            relGroupCommands[i++] = new Command.RelationshipGroupCommand(change.getBefore(), prepared(change, relationshipGroupStore));
        }
        relGroupCommands = i < relGroupCommands.length ? Arrays.copyOf(relGroupCommands, i) : relGroupCommands;
        Arrays.sort(relGroupCommands, COMMAND_SORTER);
    }
    addFiltered(commands, Mode.CREATE, propCommands, relCommands, relGroupCommands, nodeCommands);
    addFiltered(commands, Mode.UPDATE, propCommands, relCommands, relGroupCommands, nodeCommands);
    addFiltered(commands, Mode.DELETE, propCommands, relCommands, relGroupCommands, nodeCommands);
    if (neoStoreRecord != null) {
        for (RecordProxy<Long, NeoStoreRecord, Void> change : neoStoreRecord.changes()) {
            commands.add(new Command.NeoStoreCommand(change.getBefore(), change.forReadingData()));
        }
    }
    for (RecordProxy<Long, SchemaRecord, SchemaRule> change : recordChangeSet.getSchemaRuleChanges().changes()) {
        integrityValidator.validateSchemaRule(change.getAdditionalData());
        commands.add(new Command.SchemaRuleCommand(change.getBefore(), change.forChangingData(), change.getAdditionalData()));
    }
    assert commands.size() == noOfCommands - skippedCommands : format("Expected %d final commands, got %d " + "instead, with %d skipped", noOfCommands, commands.size(), skippedCommands);
    prepared = true;
}
Also used : RelationshipTypeTokenRecord(org.neo4j.kernel.impl.store.record.RelationshipTypeTokenRecord) RelationshipRecord(org.neo4j.kernel.impl.store.record.RelationshipRecord) SchemaRule(org.neo4j.storageengine.api.schema.SchemaRule) PropertyKeyTokenRecord(org.neo4j.kernel.impl.store.record.PropertyKeyTokenRecord) NodeRecord(org.neo4j.kernel.impl.store.record.NodeRecord) NeoStoreRecord(org.neo4j.kernel.impl.store.record.NeoStoreRecord) PropertyRecord(org.neo4j.kernel.impl.store.record.PropertyRecord) SchemaRecord(org.neo4j.kernel.impl.store.record.SchemaRecord) LabelTokenRecord(org.neo4j.kernel.impl.store.record.LabelTokenRecord) PrimitiveRecord(org.neo4j.kernel.impl.store.record.PrimitiveRecord) RelationshipGroupRecord(org.neo4j.kernel.impl.store.record.RelationshipGroupRecord) StorageCommand(org.neo4j.storageengine.api.StorageCommand) Command(org.neo4j.kernel.impl.transaction.command.Command)

Example 25 with RelationshipRecord

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

the class RelationshipCreator method connect.

private void connect(long nodeId, long firstRelId, RelationshipRecord rel, RecordAccess<Long, RelationshipRecord, Void> relRecords, ResourceLocker locks) {
    long newCount = 1;
    if (firstRelId != Record.NO_NEXT_RELATIONSHIP.intValue()) {
        locks.acquireExclusive(LockTracer.NONE, ResourceTypes.RELATIONSHIP, firstRelId);
        RelationshipRecord firstRel = relRecords.getOrLoad(firstRelId, null).forChangingLinkage();
        boolean changed = false;
        if (firstRel.getFirstNode() == nodeId) {
            newCount = firstRel.getFirstPrevRel() + 1;
            firstRel.setFirstPrevRel(rel.getId());
            firstRel.setFirstInFirstChain(false);
            changed = true;
        }
        if (firstRel.getSecondNode() == nodeId) {
            newCount = firstRel.getSecondPrevRel() + 1;
            firstRel.setSecondPrevRel(rel.getId());
            firstRel.setFirstInSecondChain(false);
            changed = true;
        }
        if (!changed) {
            throw new InvalidRecordException(nodeId + " doesn't match " + firstRel);
        }
    }
    // Set the relationship count
    if (rel.getFirstNode() == nodeId) {
        rel.setFirstPrevRel(newCount);
        rel.setFirstInFirstChain(true);
    }
    if (rel.getSecondNode() == nodeId) {
        rel.setSecondPrevRel(newCount);
        rel.setFirstInSecondChain(true);
    }
}
Also used : RelationshipRecord(org.neo4j.kernel.impl.store.record.RelationshipRecord) InvalidRecordException(org.neo4j.kernel.impl.store.InvalidRecordException)

Aggregations

RelationshipRecord (org.neo4j.kernel.impl.store.record.RelationshipRecord)207 Test (org.junit.Test)73 NodeRecord (org.neo4j.kernel.impl.store.record.NodeRecord)69 ConsistencyReport (org.neo4j.consistency.report.ConsistencyReport)43 Test (org.junit.jupiter.api.Test)34 RelationshipTypeTokenRecord (org.neo4j.kernel.impl.store.record.RelationshipTypeTokenRecord)30 PropertyRecord (org.neo4j.kernel.impl.store.record.PropertyRecord)19 RelationshipGroupRecord (org.neo4j.kernel.impl.store.record.RelationshipGroupRecord)19 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)15 RelationshipStore (org.neo4j.kernel.impl.store.RelationshipStore)14 GraphStoreFixture (org.neo4j.consistency.checking.GraphStoreFixture)12 IdGenerator (org.neo4j.consistency.checking.GraphStoreFixture.IdGenerator)12 ConsistencySummaryStatistics (org.neo4j.consistency.report.ConsistencySummaryStatistics)12 TransactionDataBuilder (org.neo4j.consistency.checking.GraphStoreFixture.TransactionDataBuilder)11 InMemoryClosableChannel (org.neo4j.kernel.impl.transaction.log.InMemoryClosableChannel)11 RecordAccessStub (org.neo4j.consistency.store.RecordAccessStub)9 InputRelationship (org.neo4j.unsafe.impl.batchimport.input.InputRelationship)8 RepeatedTest (org.junit.jupiter.api.RepeatedTest)7 IOException (java.io.IOException)6 CursorContext (org.neo4j.io.pagecache.context.CursorContext)6