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

org.neo4j.kernel.impl.transaction.state.Loaders Maven / Gradle / Ivy

/*
 * Copyright (c) 2018-2020 "Graph Foundation,"
 * Graph Foundation, Inc. [https://graphfoundation.org]
 *
 * This file is part of ONgDB.
 *
 * ONgDB 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 .
 */
/*
 * 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.kernel.impl.transaction.state;

import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.store.PropertyStore;
import org.neo4j.kernel.impl.store.RecordStore;
import org.neo4j.kernel.impl.store.SchemaStore;
import org.neo4j.kernel.impl.store.record.AbstractBaseRecord;
import org.neo4j.kernel.impl.store.record.DynamicRecord;
import org.neo4j.kernel.impl.store.record.LabelTokenRecord;
import org.neo4j.kernel.impl.store.record.NodeRecord;
import org.neo4j.kernel.impl.store.record.PrimitiveRecord;
import org.neo4j.kernel.impl.store.record.PropertyBlock;
import org.neo4j.kernel.impl.store.record.PropertyKeyTokenRecord;
import org.neo4j.kernel.impl.store.record.PropertyRecord;
import org.neo4j.kernel.impl.store.record.RecordLoad;
import org.neo4j.kernel.impl.store.record.RelationshipGroupRecord;
import org.neo4j.kernel.impl.store.record.RelationshipRecord;
import org.neo4j.kernel.impl.store.record.RelationshipTypeTokenRecord;
import org.neo4j.kernel.impl.store.record.SchemaRecord;
import org.neo4j.kernel.impl.transaction.state.RecordAccess.Loader;
import org.neo4j.storageengine.api.schema.SchemaRule;

import static java.lang.Math.toIntExact;

import static org.neo4j.kernel.impl.store.record.RecordLoad.NORMAL;

public class Loaders
{
    private final Loader nodeLoader;
    private final Loader propertyLoader;
    private final Loader relationshipLoader;
    private final Loader relationshipGroupLoader;
    private final Loader schemaRuleLoader;
    private final Loader propertyKeyTokenLoader;
    private final Loader labelTokenLoader;
    private final Loader relationshipTypeTokenLoader;

    public Loaders( NeoStores neoStores )
    {
        this(
                neoStores.getNodeStore(),
                neoStores.getPropertyStore(),
                neoStores.getRelationshipStore(),
                neoStores.getRelationshipGroupStore(),
                neoStores.getPropertyKeyTokenStore(),
                neoStores.getRelationshipTypeTokenStore(),
                neoStores.getLabelTokenStore(),
                neoStores.getSchemaStore() );
    }

    public Loaders(
            RecordStore nodeStore,
            PropertyStore propertyStore,
            RecordStore relationshipStore,
            RecordStore relationshipGroupStore,
            RecordStore propertyKeyTokenStore,
            RecordStore relationshipTypeTokenStore,
            RecordStore labelTokenStore,
            SchemaStore schemaStore )
    {
        nodeLoader = nodeLoader( nodeStore );
        propertyLoader = propertyLoader( propertyStore );
        relationshipLoader = relationshipLoader( relationshipStore );
        relationshipGroupLoader = relationshipGroupLoader( relationshipGroupStore );
        schemaRuleLoader = schemaRuleLoader( schemaStore );
        propertyKeyTokenLoader = propertyKeyTokenLoader( propertyKeyTokenStore );
        labelTokenLoader = labelTokenLoader( labelTokenStore );
        relationshipTypeTokenLoader = relationshipTypeTokenLoader( relationshipTypeTokenStore );
    }

    public Loader nodeLoader()
    {
        return nodeLoader;
    }

    public Loader propertyLoader()
    {
        return propertyLoader;
    }

    public Loader relationshipLoader()
    {
        return relationshipLoader;
    }

    public Loader relationshipGroupLoader()
    {
        return relationshipGroupLoader;
    }

    public Loader schemaRuleLoader()
    {
        return schemaRuleLoader;
    }

    public Loader propertyKeyTokenLoader()
    {
        return propertyKeyTokenLoader;
    }

    public Loader labelTokenLoader()
    {
        return labelTokenLoader;
    }

    public Loader relationshipTypeTokenLoader()
    {
        return relationshipTypeTokenLoader;
    }

    public static Loader nodeLoader( final RecordStore store )
    {
        return new Loader()
        {
            @Override
            public NodeRecord newUnused( long key, Void additionalData )
            {
                return andMarkAsCreated( new NodeRecord( key ) );
            }

            @Override
            public NodeRecord load( long key, Void additionalData )
            {
                return store.getRecord( key, store.newRecord(), NORMAL );
            }

            @Override
            public void ensureHeavy( NodeRecord record )
            {
                store.ensureHeavy( record );
            }

            @Override
            public NodeRecord clone( NodeRecord nodeRecord )
            {
                return nodeRecord.clone();
            }
        };
    }

    public static Loader propertyLoader( final PropertyStore store )
    {
        return new Loader()
        {
            @Override
            public PropertyRecord newUnused( long key, PrimitiveRecord additionalData )
            {
                PropertyRecord record = new PropertyRecord( key );
                setOwner( record, additionalData );
                return andMarkAsCreated( record );
            }

            private void setOwner( PropertyRecord record, PrimitiveRecord owner )
            {
                if ( owner != null )
                {
                    owner.setIdTo( record );
                }
            }

            @Override
            public PropertyRecord load( long key, PrimitiveRecord additionalData )
            {
                PropertyRecord record = store.getRecord( key, store.newRecord(), NORMAL );
                setOwner( record, additionalData );
                return record;
            }

            @Override
            public void ensureHeavy( PropertyRecord record )
            {
                for ( PropertyBlock block : record )
                {
                    store.ensureHeavy( block );
                }
            }

            @Override
            public PropertyRecord clone( PropertyRecord propertyRecord )
            {
                return propertyRecord.clone();
            }
        };
    }

    public static Loader relationshipLoader(
            final RecordStore store )
    {
        return new Loader()
        {
            @Override
            public RelationshipRecord newUnused( long key, Void additionalData )
            {
                return andMarkAsCreated( new RelationshipRecord( key ) );
            }

            @Override
            public RelationshipRecord load( long key, Void additionalData )
            {
                return store.getRecord( key, store.newRecord(), NORMAL );
            }

            @Override
            public void ensureHeavy( RelationshipRecord record )
            {   // Nothing to load
            }

            @Override
            public RelationshipRecord clone( RelationshipRecord relationshipRecord )
            {
                return relationshipRecord.clone();
            }
        };
    }

    public static Loader relationshipGroupLoader(
            final RecordStore store )
    {
        return new Loader()
        {
            @Override
            public RelationshipGroupRecord newUnused( long key, Integer type )
            {
                RelationshipGroupRecord record = new RelationshipGroupRecord( key );
                record.setType( type );
                return andMarkAsCreated( record );
            }

            @Override
            public RelationshipGroupRecord load( long key, Integer type )
            {
                return store.getRecord( key, store.newRecord(), NORMAL );
            }

            @Override
            public void ensureHeavy( RelationshipGroupRecord record )
            {   // Not needed
            }

            @Override
            public RelationshipGroupRecord clone( RelationshipGroupRecord record )
            {
                return record.clone();
            }
        };
    }

    public static Loader schemaRuleLoader( final SchemaStore store )
    {
        return new Loader()
        {
            @Override
            public SchemaRecord newUnused( long key, SchemaRule additionalData )
            {
                // Don't blindly mark as created here since some records may be reused.
                return new SchemaRecord( store.allocateFrom( additionalData ) );
            }

            @Override
            public SchemaRecord load( long key, SchemaRule additionalData )
            {
                return new SchemaRecord( store.getRecords( key, RecordLoad.NORMAL ) );
            }

            @Override
            public void ensureHeavy( SchemaRecord records )
            {
                for ( DynamicRecord record : records )
                {
                    store.ensureHeavy(record);
                }
            }

            @Override
            public SchemaRecord clone( SchemaRecord records )
            {
                return records.clone();
            }
        };
    }

    public static Loader propertyKeyTokenLoader(
            final RecordStore store )
    {
        return new Loader()
        {
            @Override
            public PropertyKeyTokenRecord newUnused( long key, Void additionalData )
            {
                return andMarkAsCreated( new PropertyKeyTokenRecord( toIntExact( key ) ) );
            }

            @Override
            public PropertyKeyTokenRecord load( long key, Void additionalData )
            {
                return store.getRecord( key, store.newRecord(), NORMAL );
            }

            @Override
            public void ensureHeavy( PropertyKeyTokenRecord record )
            {
                store.ensureHeavy( record );
            }

            @Override
            public PropertyKeyTokenRecord clone( PropertyKeyTokenRecord record )
            {
                return record.clone();
            }
        };
    }

    public static Loader labelTokenLoader(
            final RecordStore store )
    {
        return new Loader()
        {
            @Override
            public LabelTokenRecord newUnused( long key, Void additionalData )
            {
                return andMarkAsCreated( new LabelTokenRecord( toIntExact( key ) ) );
            }

            @Override
            public LabelTokenRecord load( long key, Void additionalData )
            {
                return store.getRecord( key, store.newRecord(), NORMAL );
            }

            @Override
            public void ensureHeavy( LabelTokenRecord record )
            {
                store.ensureHeavy( record );
            }

            @Override
            public LabelTokenRecord clone( LabelTokenRecord record )
            {
                return record.clone();
            }
        };
    }

    public static Loader relationshipTypeTokenLoader(
            final RecordStore store )
    {
        return new Loader()
        {
            @Override
            public RelationshipTypeTokenRecord newUnused( long key, Void additionalData )
            {
                return andMarkAsCreated( new RelationshipTypeTokenRecord( toIntExact( key ) ) );
            }

            @Override
            public RelationshipTypeTokenRecord load( long key, Void additionalData )
            {
                return store.getRecord( key, store.newRecord(), NORMAL );
            }

            @Override
            public void ensureHeavy( RelationshipTypeTokenRecord record )
            {
                store.ensureHeavy( record );
            }

            @Override
            public RelationshipTypeTokenRecord clone( RelationshipTypeTokenRecord record )
            {
                return record.clone();
            }
        };
    }

    protected static  RECORD andMarkAsCreated( RECORD record )
    {
        record.setCreated();
        return record;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy