Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* For code format

* Fixes apache#8912

* Fix test case
  • Loading branch information
terrymanu authored Jan 8, 2021
1 parent ed30fc6 commit 7953008
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.eventbus.Subscribe;
import lombok.AccessLevel;
import lombok.Getter;
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmFactory;
import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
import org.apache.shardingsphere.infra.metadata.schema.refresher.event.CreateTableEvent;
import org.apache.shardingsphere.infra.metadata.schema.refresher.event.DropTableEvent;
import org.apache.shardingsphere.infra.rule.type.DataNodeContainedRule;
import org.apache.shardingsphere.infra.rule.type.TableContainedRule;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
Expand Down Expand Up @@ -80,7 +84,7 @@ public final class ShardingRule implements DataNodeContainedRule, TableContained
private final Collection<BindingTableRule> bindingTableRules;

private final Collection<String> broadcastTables;

private final Map<String, SingleTableRule> singleTableRules;

@Getter(AccessLevel.NONE)
Expand All @@ -106,6 +110,7 @@ public ShardingRule(final ShardingRuleConfiguration config, final DatabaseType d
defaultTableShardingStrategyConfig = null == config.getDefaultTableShardingStrategy() ? new NoneShardingStrategyConfiguration() : config.getDefaultTableShardingStrategy();
defaultKeyGenerateAlgorithm = null == config.getDefaultKeyGenerateStrategy()
? TypedSPIRegistry.getRegisteredService(KeyGenerateAlgorithm.class) : keyGenerators.get(config.getDefaultKeyGenerateStrategy().getKeyGeneratorName());
ShardingSphereEventBus.getInstance().register(this);
}

public ShardingRule(final AlgorithmProvidedShardingRuleConfiguration config, final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap) {
Expand Down Expand Up @@ -414,6 +419,32 @@ public Map<String, String> getLogicAndActualTablesFromBindingTable(final String
return result;
}

/**
* Add single table.
*
* @param event create table event
*/
@Subscribe
public void createSingleTable(final CreateTableEvent event) {
if (!isConfiguredTable(event.getTableName())) {
singleTableRules.put(event.getTableName(), new SingleTableRule(event.getTableName(), event.getDataSourceName()));
}
}

private boolean isConfiguredTable(final String tableName) {
return findTableRule(tableName).isPresent() || findBindingTableRule(tableName).isPresent() || broadcastTables.contains(tableName) || singleTableRules.containsKey(tableName);
}

/**
* Drop single table.
*
* @param event drop table event
*/
@Subscribe
public void dropSingleTable(final DropTableEvent event) {
singleTableRules.remove(event.getTableName());
}

@Override
public Map<String, Collection<DataNode>> getAllDataNodes() {
return tableRules.stream().collect(Collectors.toMap(TableRule::getLogicTable, TableRule::getActualDataNodes, (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.infra.metadata.schema.refresher.event;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;

/**
* Create table event.
*/
@RequiredArgsConstructor
@Getter
public final class CreateTableEvent {

private final String dataSourceName;

private final String tableName;

private final TableMetaData tableMetaData;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.infra.metadata.schema.refresher.event;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* Drop table event.
*/
@RequiredArgsConstructor
@Getter
public final class DropTableEvent {

private final String tableName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@

package org.apache.shardingsphere.infra.metadata.schema.refresher.type;

import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.schema.builder.SchemaBuilderMaterials;
import org.apache.shardingsphere.infra.metadata.schema.builder.TableMetaDataBuilder;
import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
import org.apache.shardingsphere.infra.metadata.schema.refresher.SchemaRefresher;
import org.apache.shardingsphere.infra.metadata.schema.refresher.event.CreateTableEvent;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.type.TableContainedRule;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateTableStatement;
Expand All @@ -45,6 +47,7 @@ public void refresh(final ShardingSphereSchema schema,
tableMetaData = new TableMetaData();
}
schema.put(tableName, tableMetaData);
ShardingSphereEventBus.getInstance().post(new CreateTableEvent(routeDataSourceNames.iterator().next(), tableName, tableMetaData));
}

private boolean containsInTableContainedRule(final String tableName, final SchemaBuilderMaterials materials) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

package org.apache.shardingsphere.infra.metadata.schema.refresher.type;

import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.schema.builder.SchemaBuilderMaterials;
import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
import org.apache.shardingsphere.infra.metadata.schema.refresher.SchemaRefresher;
import org.apache.shardingsphere.infra.metadata.schema.refresher.event.CreateTableEvent;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateViewStatement;

import java.util.Collection;
Expand All @@ -34,6 +36,8 @@ public final class CreateViewStatementSchemaRefresher implements SchemaRefresher
public void refresh(final ShardingSphereSchema schema,
final Collection<String> routeDataSourceNames, final CreateViewStatement sqlStatement, final SchemaBuilderMaterials materials) {
String viewName = sqlStatement.getView().getTableName().getIdentifier().getValue();
schema.put(viewName, new TableMetaData());
TableMetaData tableMetaData = new TableMetaData();
schema.put(viewName, tableMetaData);
ShardingSphereEventBus.getInstance().post(new CreateTableEvent(routeDataSourceNames.iterator().next(), viewName, tableMetaData));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

package org.apache.shardingsphere.infra.metadata.schema.refresher.type;

import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.schema.builder.SchemaBuilderMaterials;
import org.apache.shardingsphere.infra.metadata.schema.refresher.SchemaRefresher;
import org.apache.shardingsphere.infra.metadata.schema.refresher.event.DropTableEvent;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropTableStatement;

import java.util.Collection;
Expand All @@ -33,5 +36,8 @@ public final class DropTableStatementSchemaRefresher implements SchemaRefresher<
public void refresh(final ShardingSphereSchema schema,
final Collection<String> routeDataSourceNames, final DropTableStatement sqlStatement, final SchemaBuilderMaterials materials) {
sqlStatement.getTables().forEach(each -> schema.remove(each.getTableName().getIdentifier().getValue()));
for (SimpleTableSegment each : sqlStatement.getTables()) {
ShardingSphereEventBus.getInstance().post(new DropTableEvent(each.getTableName().getIdentifier().getValue()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

package org.apache.shardingsphere.infra.metadata.schema.refresher.type;

import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.schema.builder.SchemaBuilderMaterials;
import org.apache.shardingsphere.infra.metadata.schema.refresher.SchemaRefresher;
import org.apache.shardingsphere.infra.metadata.schema.refresher.event.DropTableEvent;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropViewStatement;

import java.util.Collection;
Expand All @@ -33,5 +36,8 @@ public final class DropViewStatementSchemaRefresher implements SchemaRefresher<D
public void refresh(final ShardingSphereSchema schema,
final Collection<String> routeDataSourceNames, final DropViewStatement sqlStatement, final SchemaBuilderMaterials materials) {
sqlStatement.getViews().forEach(each -> schema.remove(each.getTableName().getIdentifier().getValue()));
for (SimpleTableSegment each : sqlStatement.getViews()) {
ShardingSphereEventBus.getInstance().post(new DropTableEvent(each.getTableName().getIdentifier().getValue()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private void refresh(final CreateTableStatement createTableStatement) throws SQL
createTableStatement.setTable(new SimpleTableSegment(new TableNameSegment(1, 3, new IdentifierValue("t_order_0"))));
SchemaRefresher<CreateTableStatement> schemaRefresher = new CreateTableStatementSchemaRefresher();
SchemaBuilderMaterials materials = mock(SchemaBuilderMaterials.class);
schemaRefresher.refresh(schema, Collections.emptyList(), createTableStatement, materials);
schemaRefresher.refresh(schema, Collections.singleton("ds"), createTableStatement, materials);
assertTrue(schema.containsTable("t_order_0"));
}

Expand Down

0 comments on commit 7953008

Please sign in to comment.