All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.neo4j.consistency.checking.LabelChainWalker Maven / Gradle / Ivy

There is a newer version: 5.26.0
Show newest version
/*
 * Copyright (c) 2002-2020 "Neo4j,"
 * Neo4j Sweden AB [http://neo4j.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package org.neo4j.consistency.checking;

import org.eclipse.collections.api.map.primitive.MutableLongObjectMap;
import org.eclipse.collections.impl.map.mutable.primitive.LongObjectHashMap;

import java.util.ArrayList;
import java.util.List;

import org.neo4j.consistency.report.ConsistencyReport;
import org.neo4j.consistency.store.RecordAccess;
import org.neo4j.kernel.impl.store.LabelIdArray;
import org.neo4j.kernel.impl.store.PropertyType;
import org.neo4j.kernel.impl.store.record.AbstractBaseRecord;
import org.neo4j.kernel.impl.store.record.DynamicRecord;
import org.neo4j.kernel.impl.store.record.Record;

import static org.neo4j.kernel.impl.store.AbstractDynamicStore.readFullByteArrayFromHeavyRecords;
import static org.neo4j.kernel.impl.store.DynamicArrayStore.getRightArray;

public class LabelChainWalker implements
        ComparativeRecordChecker
{
    private final Validator validator;

    private final MutableLongObjectMap recordIds = new LongObjectHashMap<>();
    private final List recordList = new ArrayList<>();
    private boolean allInUse = true;

    public LabelChainWalker( Validator validator )
    {
        this.validator = validator;
    }

    @Override
    public void checkReference( RECORD record, DynamicRecord dynamicRecord,
                                CheckerEngine engine,
                                RecordAccess records )
    {
        recordIds.put( dynamicRecord.getId(), dynamicRecord );

        if ( dynamicRecord.inUse() )
        {
            recordList.add( dynamicRecord );
        }
        else
        {
            allInUse = false;
            validator.onRecordNotInUse( dynamicRecord, engine );
        }

        long nextBlock = dynamicRecord.getNextBlock();
        if ( Record.NO_NEXT_BLOCK.is( nextBlock ) )
        {
            if ( allInUse )
            {
                // only validate label ids if all dynamic records seen were in use
                validator.onWellFormedChain( labelIds( recordList ), engine, records );
            }
        }
        else
        {
            final DynamicRecord nextRecord = recordIds.get( nextBlock );
            if ( nextRecord != null )
            {
                validator.onRecordChainCycle( nextRecord, engine );
            }
            else
            {
                engine.comparativeCheck( records.nodeLabels( nextBlock ), this );
            }
        }
    }

    public static long[] labelIds( List recordList )
    {
        long[] idArray =
                (long[]) getRightArray( readFullByteArrayFromHeavyRecords( recordList, PropertyType.ARRAY ) ).asObject();
        return LabelIdArray.stripNodeId( idArray );
    }

    public interface Validator
    {
        void onRecordNotInUse( DynamicRecord dynamicRecord, CheckerEngine engine );
        void onRecordChainCycle( DynamicRecord record, CheckerEngine engine );
        void onWellFormedChain( long[] labelIds, CheckerEngine engine, RecordAccess records );
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy