Skip to content

Commit

Permalink
Refresh TableAddressingMetaData when DDL executed (apache#8013)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Nov 3, 2020
1 parent 2d61007 commit 2d58c97
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import lombok.Getter;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Table addressing meta data.
*/
@Getter
public final class TableAddressingMetaData {

private final Map<String, Collection<String>> tableDataSourceNamesMapper = new HashMap<>();
private final Map<String, Collection<String>> tableDataSourceNamesMapper = new ConcurrentHashMap<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.collect.Lists;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.model.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.model.addressing.TableAddressingMetaData;
import org.apache.shardingsphere.infra.metadata.model.physical.model.table.PhysicalTableMetaData;
import org.apache.shardingsphere.infra.metadata.refresh.MetaDataRefreshStrategy;
import org.apache.shardingsphere.infra.metadata.refresh.TableMetaDataLoaderCallback;
Expand All @@ -46,6 +47,7 @@ public void refreshMetaData(final ShardingSphereMetaData metaData, final Databas
refreshUnconfiguredMetaData(metaData, routeDataSourceNames, tableName);
metaData.getSchemaMetaData().getSchemaMetaData().put(tableName, new PhysicalTableMetaData());
}
refreshTableAddressingMetaData(metaData.getTableAddressingMetaData(), tableName, routeDataSourceNames);
}

private void refreshUnconfiguredMetaData(final ShardingSphereMetaData metaData, final Collection<String> routeDataSourceNames, final String tableName) {
Expand All @@ -62,4 +64,17 @@ private void refreshUnconfiguredMetaData(final ShardingSphereMetaData metaData,
schemaMetaData.add(tableName);
}
}

private void refreshTableAddressingMetaData(final TableAddressingMetaData tableAddressingMetaData, final String tableName, final Collection<String> routeDataSourceNames) {
for (String each : routeDataSourceNames) {
refreshTableAddressingMetaData(tableAddressingMetaData, tableName, each);
}
}

private void refreshTableAddressingMetaData(final TableAddressingMetaData tableAddressingMetaData, final String tableName, final String dataSourceName) {
Collection<String> previousDataSourceNames = tableAddressingMetaData.getTableDataSourceNamesMapper().putIfAbsent(tableName, Lists.newArrayList(dataSourceName));
if (null != previousDataSourceNames) {
previousDataSourceNames.add(dataSourceName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import com.google.common.collect.Lists;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.model.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.model.addressing.TableAddressingMetaData;
import org.apache.shardingsphere.infra.metadata.model.physical.model.table.PhysicalTableMetaData;
import org.apache.shardingsphere.infra.metadata.refresh.MetaDataRefreshStrategy;
import org.apache.shardingsphere.infra.metadata.refresh.TableMetaDataLoaderCallback;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateViewStatement;

import java.sql.SQLException;
import java.util.Collection;

/**
Expand All @@ -35,10 +35,11 @@ public final class CreateViewStatementMetaDataRefreshStrategy implements MetaDat

@Override
public void refreshMetaData(final ShardingSphereMetaData metaData, final DatabaseType databaseType, final Collection<String> routeDataSourceNames,
final CreateViewStatement sqlStatement, final TableMetaDataLoaderCallback callback) throws SQLException {
final CreateViewStatement sqlStatement, final TableMetaDataLoaderCallback callback) {
String viewName = sqlStatement.getView().getTableName().getIdentifier().getValue();
refreshUnconfiguredMetaData(metaData, routeDataSourceNames, viewName);
metaData.getSchemaMetaData().getSchemaMetaData().put(viewName, new PhysicalTableMetaData());
refreshTableAddressingMetaData(metaData.getTableAddressingMetaData(), viewName, routeDataSourceNames);
}

private void refreshUnconfiguredMetaData(final ShardingSphereMetaData metaData, final Collection<String> routeDataSourceNames, final String viewName) {
Expand All @@ -55,4 +56,17 @@ private void refreshUnconfiguredMetaData(final ShardingSphereMetaData metaData,
schemaMetaData.add(viewName);
}
}

private void refreshTableAddressingMetaData(final TableAddressingMetaData tableAddressingMetaData, final String tableName, final Collection<String> routeDataSourceNames) {
for (String each : routeDataSourceNames) {
refreshTableAddressingMetaData(tableAddressingMetaData, tableName, each);
}
}

private void refreshTableAddressingMetaData(final TableAddressingMetaData tableAddressingMetaData, final String tableName, final String dataSourceName) {
Collection<String> previousDataSourceNames = tableAddressingMetaData.getTableDataSourceNamesMapper().putIfAbsent(tableName, Lists.newArrayList(dataSourceName));
if (null != previousDataSourceNames) {
previousDataSourceNames.add(dataSourceName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ private void removeMetaData(final ShardingSphereMetaData metaData, final String
}
}
metaData.getSchemaMetaData().getSchemaMetaData().remove(tableName);
metaData.getTableAddressingMetaData().getTableDataSourceNamesMapper().remove(tableName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ private void removeMetaData(final ShardingSphereMetaData metaData, final String
}
}
metaData.getSchemaMetaData().getSchemaMetaData().remove(viewName);
metaData.getTableAddressingMetaData().getTableDataSourceNamesMapper().remove(viewName);
}
}

0 comments on commit 2d58c97

Please sign in to comment.