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

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

Go to download

Neo4j kernel is a lightweight, embedded Java database designed to store data structured as graphs rather than tables. For more information, see http://neo4j.org.

There is a newer version: 5.26.0
Show newest version
/*
 * Copyright (c) "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 java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

import org.neo4j.common.DependencyResolver;
import org.neo4j.configuration.Config;
import org.neo4j.configuration.GraphDatabaseInternalSettings;
import org.neo4j.configuration.GraphDatabaseSettings;
import org.neo4j.internal.schema.IndexDescriptor;
import org.neo4j.internal.schema.IndexProviderDescriptor;
import org.neo4j.kernel.api.index.IndexProvider;
import org.neo4j.kernel.impl.api.index.IndexProviderMap;
import org.neo4j.kernel.impl.api.index.IndexProviderNotFoundException;
import org.neo4j.kernel.impl.index.schema.TokenIndexProvider;
import org.neo4j.kernel.lifecycle.LifecycleAdapter;

import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public class DefaultIndexProviderMap extends LifecycleAdapter implements IndexProviderMap
{
    private static final String TOKEN_INDEX_PROVIDER_NAME = TokenIndexProvider.DESCRIPTOR.name();

    private final Map indexProvidersByDescriptor = new HashMap<>();
    private final Map indexProvidersByName = new HashMap<>();
    private final DependencyResolver dependencies;
    private IndexProvider defaultIndexProvider;
    private IndexProvider fulltextIndexProvider;
    private IndexProvider tokenIndexProvider;
    private final Config config;

    public DefaultIndexProviderMap( DependencyResolver dependencies, Config config )
    {
        this.dependencies = dependencies;
        this.config = config;
    }

    @Override
    public void init()
    {
        Iterable indexProviders = dependencies.resolveTypeDependencies( IndexProvider.class );
        for ( IndexProvider provider : indexProviders )
        {
            IndexProviderDescriptor providerDescriptor = provider.getProviderDescriptor();
            requireNonNull( providerDescriptor );
            IndexProvider existing = put( providerDescriptor, provider );
            if ( existing != null )
            {
                throw new IllegalArgumentException( "Tried to load multiple schema index providers with the same provider descriptor " +
                        providerDescriptor + ". First loaded " + existing + " then " + provider );
            }
        }
        initDefaultProviders();
    }

    @Override
    public IndexProvider getDefaultProvider()
    {
        assertInit();
        return defaultIndexProvider;
    }

    @Override
    public IndexProvider getFulltextProvider()
    {
        assertInit();
        return fulltextIndexProvider;
    }

    @Override
    public IndexProvider getTokenIndexProvider()
    {
        assertInit();
        return tokenIndexProvider;
    }

    @Override
    public IndexProvider lookup( IndexProviderDescriptor providerDescriptor )
    {
        assertInit();
        IndexProvider provider = indexProvidersByDescriptor.get( providerDescriptor );
        assertProviderFound( provider, providerDescriptor.name() );
        return provider;
    }

    @Override
    public IndexProvider lookup( String providerDescriptorName )
    {
        assertInit();
        IndexProvider provider = indexProvidersByName.get( providerDescriptorName );
        assertProviderFound( provider, providerDescriptorName );
        return provider;
    }

    @Override
    public void accept( Consumer visitor )
    {
        assertInit();
        indexProvidersByDescriptor.values().forEach( visitor );
    }

    private void assertProviderFound( IndexProvider provider, String providerDescriptorName )
    {
        if ( provider == null )
        {
            throw new IndexProviderNotFoundException( "Tried to get index provider with name " + providerDescriptorName +
                    " whereas available providers in this session being " + indexProvidersByName.keySet() + ", and default being " +
                    defaultIndexProvider.getProviderDescriptor().name() );
        }
    }

    private void assertInit()
    {
        if ( defaultIndexProvider == null || fulltextIndexProvider == null || tokenIndexProvider == null )
        {
            throw new IllegalStateException( "DefaultIndexProviderMap must be part of life cycle and initialized before getting providers." );
        }
    }

    private void initDefaultProviders()
    {
        String providerName = config.get( GraphDatabaseSettings.default_schema_provider );
        IndexProvider configuredDefaultProvider = indexProvidersByName.get( providerName );
        requireNonNull( configuredDefaultProvider, () -> format( "Configured default provider: `%s` not found. Available index providers: %s.", providerName,
                indexProvidersByName.keySet() ) );
        defaultIndexProvider = configuredDefaultProvider;

        String fulltextProviderName = config.get( GraphDatabaseInternalSettings.default_fulltext_provider );
        fulltextIndexProvider = indexProvidersByName.get( fulltextProviderName );
        if ( fulltextIndexProvider == null )
        {
            // Not all environments have the full-text index provider available.
            fulltextIndexProvider = IndexProvider.EMPTY;
        }

        var configuredTokenIndexProvider = indexProvidersByName.get( TOKEN_INDEX_PROVIDER_NAME );
        requireNonNull( configuredTokenIndexProvider, () -> format( "Token index provider: `%s` not found. Available index providers: %s.",
                TOKEN_INDEX_PROVIDER_NAME, indexProvidersByName.keySet().toString() ) );
        tokenIndexProvider = configuredTokenIndexProvider;
    }

    private IndexProvider put( IndexProviderDescriptor providerDescriptor, IndexProvider provider )
    {
        IndexProvider existing = indexProvidersByDescriptor.putIfAbsent( providerDescriptor, provider );
        indexProvidersByName.putIfAbsent( providerDescriptor.name(), provider );
        return existing;
    }

    @Override
    public IndexDescriptor completeConfiguration( IndexDescriptor index )
    {
        IndexProviderDescriptor providerDescriptor = index.getIndexProvider();
        IndexProvider provider = lookup( providerDescriptor );
        return provider.completeConfiguration( index );
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy