forked from apache/shardingsphere
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Refactor meta data refresher with spi for DDL (apache#13947)
Co-authored-by: shardingsphere <dev@shardingsphere.apache.org>
- Loading branch information
1 parent
e784e0e
commit 00e067d
Showing
11 changed files
with
588 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...xt/src/main/java/org/apache/shardingsphere/infra/context/refresher/MetaDataRefresher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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.context.refresher; | ||
|
||
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties; | ||
import org.apache.shardingsphere.infra.federation.optimizer.metadata.FederationSchemaMetaData; | ||
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; | ||
import org.apache.shardingsphere.spi.typed.TypedSPI; | ||
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; | ||
|
||
import java.sql.SQLException; | ||
import java.util.Collection; | ||
|
||
/** | ||
* ShardingSphere schema refresher. | ||
* | ||
* @param <T> type of SQL statement | ||
*/ | ||
public interface MetaDataRefresher<T extends SQLStatement> extends TypedSPI { | ||
|
||
/** | ||
* Refresh ShardingSphere schema. | ||
* | ||
* @param schemaMetaData schema meta data | ||
* @param schema federation schema meta data | ||
* @param logicDataSourceNames route data source names | ||
* @param sqlStatement SQL statement | ||
* @param props configuration properties | ||
* @throws SQLException SQL exception | ||
*/ | ||
void refresh(ShardingSphereMetaData schemaMetaData, FederationSchemaMetaData schema, Collection<String> logicDataSourceNames, T sqlStatement, ConfigurationProperties props) throws SQLException; | ||
} |
67 changes: 67 additions & 0 deletions
67
...pache/shardingsphere/infra/context/refresher/type/AlterIndexStatementSchemaRefresher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* 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.context.refresher.type; | ||
|
||
import com.google.common.base.Preconditions; | ||
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties; | ||
import org.apache.shardingsphere.infra.context.refresher.MetaDataRefresher; | ||
import org.apache.shardingsphere.infra.federation.optimizer.metadata.FederationSchemaMetaData; | ||
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; | ||
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema; | ||
import org.apache.shardingsphere.infra.metadata.schema.model.IndexMetaData; | ||
import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData; | ||
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment; | ||
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.AlterIndexStatement; | ||
import org.apache.shardingsphere.sql.parser.sql.dialect.handler.ddl.AlterIndexStatementHandler; | ||
|
||
import java.sql.SQLException; | ||
import java.util.Collection; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Schema refresher for alter index statement. | ||
*/ | ||
public final class AlterIndexStatementSchemaRefresher implements MetaDataRefresher<AlterIndexStatement> { | ||
|
||
@Override | ||
public void refresh(final ShardingSphereMetaData schemaMetaData, final FederationSchemaMetaData schema, final Collection<String> logicDataSourceNames, final AlterIndexStatement sqlStatement, | ||
final ConfigurationProperties props) throws SQLException { | ||
Optional<IndexSegment> renameIndex = AlterIndexStatementHandler.getRenameIndexSegment(sqlStatement); | ||
if (!sqlStatement.getIndex().isPresent() || !renameIndex.isPresent()) { | ||
return; | ||
} | ||
String indexName = sqlStatement.getIndex().get().getIdentifier().getValue(); | ||
Optional<String> logicTableName = findLogicTableName(schemaMetaData.getSchema(), indexName); | ||
if (logicTableName.isPresent()) { | ||
TableMetaData tableMetaData = schemaMetaData.getSchema().get(logicTableName.get()); | ||
Preconditions.checkNotNull(tableMetaData, "Can not get the table '%s' metadata!", logicTableName.get()); | ||
tableMetaData.getIndexes().remove(indexName); | ||
String renameIndexName = renameIndex.get().getIdentifier().getValue(); | ||
tableMetaData.getIndexes().put(renameIndexName, new IndexMetaData(renameIndexName)); | ||
} | ||
} | ||
|
||
private Optional<String> findLogicTableName(final ShardingSphereSchema schema, final String indexName) { | ||
return schema.getAllTableNames().stream().filter(each -> schema.get(each).getIndexes().containsKey(indexName)).findFirst(); | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return AlterIndexStatement.class.getCanonicalName(); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...pache/shardingsphere/infra/context/refresher/type/AlterTableStatementSchemaRefresher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* 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.context.refresher.type; | ||
|
||
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties; | ||
import org.apache.shardingsphere.infra.context.refresher.MetaDataRefresher; | ||
import org.apache.shardingsphere.infra.federation.optimizer.metadata.FederationSchemaMetaData; | ||
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; | ||
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.rule.identifier.type.DataNodeContainedRule; | ||
import org.apache.shardingsphere.infra.rule.identifier.type.MutableDataNodeRule; | ||
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.AlterTableStatement; | ||
|
||
import java.sql.SQLException; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Schema refresher for alter table statement. | ||
*/ | ||
public final class AlterTableStatementSchemaRefresher implements MetaDataRefresher<AlterTableStatement> { | ||
|
||
@Override | ||
public void refresh(final ShardingSphereMetaData schemaMetaData, final FederationSchemaMetaData schema, final Collection<String> logicDataSourceNames, final AlterTableStatement sqlStatement, | ||
final ConfigurationProperties props) throws SQLException { | ||
String tableName = sqlStatement.getTable().getTableName().getIdentifier().getValue(); | ||
if (sqlStatement.getRenameTable().isPresent()) { | ||
putTableMetaData(schemaMetaData, schema, logicDataSourceNames, sqlStatement.getRenameTable().get().getTableName().getIdentifier().getValue(), props); | ||
removeTableMetaData(schemaMetaData, schema, tableName); | ||
} else { | ||
putTableMetaData(schemaMetaData, schema, logicDataSourceNames, tableName, props); | ||
} | ||
} | ||
|
||
private void removeTableMetaData(final ShardingSphereMetaData schemaMetaData, final FederationSchemaMetaData schema, final String tableName) { | ||
schemaMetaData.getSchema().remove(tableName); | ||
schemaMetaData.getRuleMetaData().findRules(MutableDataNodeRule.class).forEach(each -> each.remove(tableName)); | ||
schema.remove(tableName); | ||
} | ||
|
||
private void putTableMetaData(final ShardingSphereMetaData schemaMetaData, final FederationSchemaMetaData schema, final Collection<String> logicDataSourceNames, final String tableName, | ||
final ConfigurationProperties props) throws SQLException { | ||
if (!containsInDataNodeContainedRule(tableName, schemaMetaData)) { | ||
schemaMetaData.getRuleMetaData().findRules(MutableDataNodeRule.class).forEach(each -> each.put(tableName, logicDataSourceNames.iterator().next())); | ||
} | ||
SchemaBuilderMaterials materials = new SchemaBuilderMaterials( | ||
schemaMetaData.getResource().getDatabaseType(), schemaMetaData.getResource().getDataSources(), schemaMetaData.getRuleMetaData().getRules(), props); | ||
Optional<TableMetaData> actualTableMetaData = Optional.ofNullable(TableMetaDataBuilder.load(Collections.singletonList(tableName), materials).get(tableName)); | ||
actualTableMetaData.ifPresent(tableMetaData -> { | ||
schemaMetaData.getSchema().put(tableName, TableMetaDataBuilder.decorateKernelTableMetaData(tableMetaData, materials.getRules())); | ||
schema.put(TableMetaDataBuilder.decorateFederationTableMetaData(tableMetaData, materials.getRules())); | ||
}); | ||
} | ||
|
||
private boolean containsInDataNodeContainedRule(final String tableName, final ShardingSphereMetaData schemaMetaData) { | ||
return schemaMetaData.getRuleMetaData().findRules(DataNodeContainedRule.class).stream().anyMatch(each -> each.getAllTables().contains(tableName)); | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return AlterTableStatement.class.getCanonicalName(); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...ache/shardingsphere/infra/context/refresher/type/CreateIndexStatementSchemaRefresher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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.context.refresher.type; | ||
|
||
import com.google.common.base.Strings; | ||
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties; | ||
import org.apache.shardingsphere.infra.context.refresher.MetaDataRefresher; | ||
import org.apache.shardingsphere.infra.federation.optimizer.metadata.FederationSchemaMetaData; | ||
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; | ||
import org.apache.shardingsphere.infra.metadata.schema.builder.util.IndexMetaDataUtil; | ||
import org.apache.shardingsphere.infra.metadata.schema.model.IndexMetaData; | ||
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateIndexStatement; | ||
|
||
import java.sql.SQLException; | ||
import java.util.Collection; | ||
|
||
/** | ||
* Schema refresher for create index statement. | ||
*/ | ||
public final class CreateIndexStatementSchemaRefresher implements MetaDataRefresher<CreateIndexStatement> { | ||
|
||
@Override | ||
public void refresh(final ShardingSphereMetaData schemaMetaData, final FederationSchemaMetaData schema, final Collection<String> logicDataSourceNames, final CreateIndexStatement sqlStatement, | ||
final ConfigurationProperties props) throws SQLException { | ||
String indexName = null != sqlStatement.getIndex() ? sqlStatement.getIndex().getIdentifier().getValue() : IndexMetaDataUtil.getGeneratedLogicIndexName(sqlStatement.getColumns()); | ||
if (Strings.isNullOrEmpty(indexName)) { | ||
return; | ||
} | ||
String tableName = sqlStatement.getTable().getTableName().getIdentifier().getValue(); | ||
schemaMetaData.getSchema().get(tableName).getIndexes().put(indexName, new IndexMetaData(indexName)); | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return CreateIndexStatement.class.getCanonicalName(); | ||
} | ||
} |
Oops, something went wrong.