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

io.shardingsphere.core.rule.TableRule Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version
/*
 * Copyright 2016-2018 shardingsphere.io.
 * 

* Licensed 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 io.shardingsphere.core.rule; import com.google.common.base.Preconditions; import io.shardingsphere.core.api.config.TableRuleConfiguration; import io.shardingsphere.core.exception.ShardingException; import io.shardingsphere.core.keygen.KeyGenerator; import io.shardingsphere.core.routing.strategy.ShardingStrategy; import io.shardingsphere.core.routing.strategy.ShardingStrategyFactory; import io.shardingsphere.core.util.InlineExpressionParser; import lombok.Getter; import lombok.ToString; import java.util.Collection; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; /** * Table rule configuration. * * @author zhangliang */ @Getter @ToString public final class TableRule { private final String logicTable; private final List actualDataNodes; private final ShardingStrategy databaseShardingStrategy; private final ShardingStrategy tableShardingStrategy; private final String generateKeyColumn; private final KeyGenerator keyGenerator; private final String logicIndex; public TableRule(final TableRuleConfiguration tableRuleConfig, final ShardingDataSourceNames shardingDataSourceNames) { Preconditions.checkNotNull(tableRuleConfig.getLogicTable(), "Logic table cannot be null."); logicTable = tableRuleConfig.getLogicTable().toLowerCase(); List dataNodes = new InlineExpressionParser(tableRuleConfig.getActualDataNodes()).splitAndEvaluate(); actualDataNodes = isEmptyDataNodes(dataNodes) ? generateDataNodes(tableRuleConfig.getLogicTable(), shardingDataSourceNames.getDataSourceNames()) : generateDataNodes(dataNodes, shardingDataSourceNames.getDataSourceNames()); databaseShardingStrategy = null == tableRuleConfig.getDatabaseShardingStrategyConfig() ? null : ShardingStrategyFactory.newInstance(tableRuleConfig.getDatabaseShardingStrategyConfig()); tableShardingStrategy = null == tableRuleConfig.getTableShardingStrategyConfig() ? null : ShardingStrategyFactory.newInstance(tableRuleConfig.getTableShardingStrategyConfig()); generateKeyColumn = tableRuleConfig.getKeyGeneratorColumnName(); keyGenerator = tableRuleConfig.getKeyGenerator(); logicIndex = null == tableRuleConfig.getLogicIndex() ? null : tableRuleConfig.getLogicIndex().toLowerCase(); } private boolean isEmptyDataNodes(final List dataNodes) { return null == dataNodes || dataNodes.isEmpty(); } private List generateDataNodes(final String logicTable, final Collection dataSourceNames) { List result = new LinkedList<>(); for (String each : dataSourceNames) { result.add(new DataNode(each, logicTable)); } return result; } private List generateDataNodes(final List actualDataNodes, final Collection dataSourceNames) { List result = new LinkedList<>(); for (String each : actualDataNodes) { DataNode dataNode = new DataNode(each); if (!dataSourceNames.contains(dataNode.getDataSourceName())) { throw new ShardingException("Cannot find data source in sharding rule, invalid actual data node is: '%s'", each); } result.add(dataNode); } return result; } /** * Get actual data source names. * * @return actual data source names */ public Collection getActualDatasourceNames() { Collection result = new LinkedHashSet<>(actualDataNodes.size()); for (DataNode each : actualDataNodes) { result.add(each.getDataSourceName()); } return result; } /** * Get actual table names via target data source name. * * @param targetDataSource target data source name * @return names of actual tables */ public Collection getActualTableNames(final String targetDataSource) { Collection result = new LinkedHashSet<>(actualDataNodes.size()); for (DataNode each : actualDataNodes) { if (targetDataSource.equals(each.getDataSourceName())) { result.add(each.getTableName()); } } return result; } int findActualTableIndex(final String dataSourceName, final String actualTableName) { int result = 0; for (DataNode each : actualDataNodes) { if (each.getDataSourceName().equalsIgnoreCase(dataSourceName) && each.getTableName().equalsIgnoreCase(actualTableName)) { return result; } result++; } return -1; } boolean isExisted(final String actualTableName) { for (DataNode each : actualDataNodes) { if (each.getTableName().equalsIgnoreCase(actualTableName)) { return true; } } return false; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy