Skip to content

Commit

Permalink
Add MetaDataContextsInitFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Jan 19, 2025
1 parent a7873a0 commit c4fa537
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
import org.apache.shardingsphere.infra.rule.builder.global.GlobalRulesBuilder;
import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.metadata.factory.type.LocalConfigurationMetaDataContextsFactory;
import org.apache.shardingsphere.mode.metadata.factory.type.RegisterCenterMetaDataContextsFactory;
import org.apache.shardingsphere.mode.metadata.factory.init.MetaDataContextsInitFactory;
import org.apache.shardingsphere.mode.metadata.factory.init.type.LocalConfigurationMetaDataContextsInitFactory;
import org.apache.shardingsphere.mode.metadata.factory.init.type.RegisterCenterMetaDataContextsInitFactory;
import org.apache.shardingsphere.mode.metadata.manager.SwitchingResource;
import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;

Expand Down Expand Up @@ -67,9 +68,10 @@ public final class MetaDataContextsFactory {
* @throws SQLException SQL exception
*/
public MetaDataContexts create(final ContextManagerBuilderParameter param) throws SQLException {
return containsRegisteredDatabases()
? new RegisterCenterMetaDataContextsFactory(persistService, instanceContext).create(param)
: new LocalConfigurationMetaDataContextsFactory(persistService, instanceContext).create(param);
MetaDataContextsInitFactory initFactory = containsRegisteredDatabases()
? new RegisterCenterMetaDataContextsInitFactory(persistService, instanceContext)
: new LocalConfigurationMetaDataContextsInitFactory(persistService, instanceContext);
return initFactory.create(param);
}

private boolean containsRegisteredDatabases() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.mode.metadata.factory.init;

import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;

import java.sql.SQLException;

/**
* Meta data contexts init factory.
*/
public interface MetaDataContextsInitFactory {

/**
* Create meta data contexts.
*
* @param param context manager builder parameter
* @return created meta data contexts
* @throws SQLException SQL exception
*/
MetaDataContexts create(final ContextManagerBuilderParameter param) throws SQLException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.mode.metadata.factory.type;
package org.apache.shardingsphere.mode.metadata.factory.init.type;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
Expand All @@ -35,6 +35,7 @@
import org.apache.shardingsphere.infra.rule.builder.global.GlobalRulesBuilder;
import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.metadata.factory.init.MetaDataContextsInitFactory;
import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;

import javax.sql.DataSource;
Expand All @@ -46,22 +47,16 @@
import java.util.stream.Collectors;

/**
* Local configuration meta data contexts factory.
* Local configuration meta data contexts init factory.
*/
@RequiredArgsConstructor
public final class LocalConfigurationMetaDataContextsFactory {
public final class LocalConfigurationMetaDataContextsInitFactory implements MetaDataContextsInitFactory {

private final MetaDataPersistService persistService;

private final ComputeNodeInstanceContext instanceContext;

/**
* Create meta data contexts.
*
* @param param context manager builder parameter
* @return meta data contexts
* @throws SQLException SQL exception
*/
@Override
public MetaDataContexts create(final ContextManagerBuilderParameter param) throws SQLException {
ConfigurationProperties props = new ConfigurationProperties(param.getProps());
Collection<ShardingSphereDatabase> databases = ShardingSphereDatabasesFactory.create(param.getDatabaseConfigs(), props, instanceContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.mode.metadata.factory.type;
package org.apache.shardingsphere.mode.metadata.factory.init.type;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
Expand All @@ -38,6 +38,7 @@
import org.apache.shardingsphere.infra.rule.builder.global.GlobalRulesBuilder;
import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.metadata.factory.init.MetaDataContextsInitFactory;
import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;

import javax.sql.DataSource;
Expand All @@ -47,22 +48,16 @@
import java.util.stream.Collectors;

/**
* Register center meta data contexts factory.
* Register center meta data contexts init factory.
*/
@RequiredArgsConstructor
public final class RegisterCenterMetaDataContextsFactory {
public final class RegisterCenterMetaDataContextsInitFactory implements MetaDataContextsInitFactory {

private final MetaDataPersistService persistService;

private final ComputeNodeInstanceContext instanceContext;

/**
* Create meta data contexts.
*
* @param param context manager builder parameter
* @return meta data contexts
* @throws SQLException SQL exception
*/
@Override
public MetaDataContexts create(final ContextManagerBuilderParameter param) throws SQLException {
Map<String, DatabaseConfiguration> effectiveDatabaseConfigs = createEffectiveDatabaseConfigurations(getDatabaseNames(param.getDatabaseConfigs()), param.getDatabaseConfigs());
Collection<RuleConfiguration> globalRuleConfigs = persistService.getGlobalRuleService().load();
Expand Down

0 comments on commit c4fa537

Please sign in to comment.