Skip to content

Commit

Permalink
Add AlgorithmDistSQLConverter (#30594)
Browse files Browse the repository at this point in the history
* Refactor ConvertYamlConfigurationExecutor

* Add AlgorithmDistSQLConverter
  • Loading branch information
terrymanu authored Mar 21, 2024
1 parent 9f6d0be commit d083faa
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shardingsphere.encrypt.distsql.handler.provider;

import com.google.common.base.Strings;
import org.apache.shardingsphere.distsql.handler.engine.query.ral.convert.AlgorithmDistSQLConverter;
import org.apache.shardingsphere.distsql.handler.engine.query.ral.convert.ConvertRuleConfigurationProvider;
import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnItemRuleConfiguration;
Expand Down Expand Up @@ -92,14 +93,15 @@ private String getEncryptAlgorithms(final EncryptColumnRuleConfiguration ruleCon
String assistedQueryEncryptorName = ruleConfig.getAssistedQuery().map(EncryptColumnItemRuleConfiguration::getEncryptorName).orElse("");
String likeQueryEncryptorName = ruleConfig.getLikeQuery().map(EncryptColumnItemRuleConfiguration::getEncryptorName).orElse("");
if (!Strings.isNullOrEmpty(cipherEncryptorName)) {
result.append(String.format(EncryptDistSQLConstants.ENCRYPT_ALGORITHM, getAlgorithmType(encryptors.get(cipherEncryptorName))));
result.append(String.format(EncryptDistSQLConstants.ENCRYPT_ALGORITHM, AlgorithmDistSQLConverter.getAlgorithmType(encryptors.get(cipherEncryptorName))));
}
if (!Strings.isNullOrEmpty(assistedQueryEncryptorName)) {
result.append(EncryptDistSQLConstants.COMMA).append(' ')
.append(String.format(EncryptDistSQLConstants.ASSISTED_QUERY_ALGORITHM, getAlgorithmType(encryptors.get(assistedQueryEncryptorName))));
.append(String.format(EncryptDistSQLConstants.ASSISTED_QUERY_ALGORITHM, AlgorithmDistSQLConverter.getAlgorithmType(encryptors.get(assistedQueryEncryptorName))));
}
if (!Strings.isNullOrEmpty(likeQueryEncryptorName)) {
result.append(EncryptDistSQLConstants.COMMA).append(' ').append(String.format(EncryptDistSQLConstants.LIKE_QUERY_ALGORITHM, getAlgorithmType(encryptors.get(likeQueryEncryptorName))));
result.append(EncryptDistSQLConstants.COMMA).append(' ')
.append(String.format(EncryptDistSQLConstants.LIKE_QUERY_ALGORITHM, AlgorithmDistSQLConverter.getAlgorithmType(encryptors.get(likeQueryEncryptorName))));
}
return result.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.mask.distsql.handler.provider;

import org.apache.shardingsphere.distsql.handler.engine.query.ral.convert.AlgorithmDistSQLConverter;
import org.apache.shardingsphere.distsql.handler.engine.query.ral.convert.ConvertRuleConfigurationProvider;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
Expand Down Expand Up @@ -67,7 +68,7 @@ private String getMaskColumns(final Collection<MaskColumnRuleConfiguration> colu
}

private String getMaskAlgorithms(final MaskColumnRuleConfiguration columnRuleConfig, final Map<String, AlgorithmConfiguration> maskAlgorithms) {
return getAlgorithmType(maskAlgorithms.get(columnRuleConfig.getMaskAlgorithm()));
return AlgorithmDistSQLConverter.getAlgorithmType(maskAlgorithms.get(columnRuleConfig.getMaskAlgorithm()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shardingsphere.readwritesplitting.distsql.handler.provider;

import com.google.common.base.Strings;
import org.apache.shardingsphere.distsql.handler.engine.query.ral.convert.AlgorithmDistSQLConverter;
import org.apache.shardingsphere.distsql.handler.engine.query.ral.convert.ConvertRuleConfigurationProvider;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
Expand Down Expand Up @@ -78,7 +79,7 @@ private String getReadDataSourceNames(final Collection<String> readDataSourceNam

private String getLoadBalancerType(final AlgorithmConfiguration algorithmConfig) {
StringBuilder result = new StringBuilder();
String loadBalancerType = getAlgorithmType(algorithmConfig);
String loadBalancerType = AlgorithmDistSQLConverter.getAlgorithmType(algorithmConfig);
if (!Strings.isNullOrEmpty(loadBalancerType)) {
result.append(ReadwriteSplittingDistSQLConstants.COMMA).append(System.lineSeparator()).append(loadBalancerType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.shadow.distsql.handler.provider;

import org.apache.shardingsphere.distsql.handler.engine.query.ral.convert.AlgorithmDistSQLConverter;
import org.apache.shardingsphere.distsql.handler.engine.query.ral.convert.ConvertRuleConfigurationProvider;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
Expand Down Expand Up @@ -78,7 +79,7 @@ private String getShadowTableTypes(final Collection<String> shadowAlgorithmNames
StringBuilder result = new StringBuilder();
Iterator<String> iterator = shadowAlgorithmNames.iterator();
while (iterator.hasNext()) {
result.append(getAlgorithmType(algorithmConfigs.get(iterator.next())));
result.append(AlgorithmDistSQLConverter.getAlgorithmType(algorithmConfigs.get(iterator.next())));
if (iterator.hasNext()) {
result.append(ShadowDistSQLConstants.COMMA).append(' ');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shardingsphere.sharding.distsql.handler.provider;

import com.google.common.base.Strings;
import org.apache.shardingsphere.distsql.handler.engine.query.ral.convert.AlgorithmDistSQLConverter;
import org.apache.shardingsphere.distsql.handler.engine.query.ral.convert.ConvertRuleConfigurationProvider;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
Expand Down Expand Up @@ -140,7 +141,8 @@ private String appendAutoTableStrategy(final ShardingAutoTableRuleConfiguration
StringBuilder result = new StringBuilder();
StandardShardingStrategyConfiguration strategyConfig = (StandardShardingStrategyConfiguration) autoTableRuleConfig.getShardingStrategy();
String shardingColumn = Strings.isNullOrEmpty(strategyConfig.getShardingColumn()) ? ruleConfig.getDefaultShardingColumn() : strategyConfig.getShardingColumn();
result.append(String.format(ShardingDistSQLConstants.AUTO_TABLE_STRATEGY, shardingColumn, getAlgorithmType(ruleConfig.getShardingAlgorithms().get(strategyConfig.getShardingAlgorithmName()))));
result.append(String.format(ShardingDistSQLConstants.AUTO_TABLE_STRATEGY,
shardingColumn, AlgorithmDistSQLConverter.getAlgorithmType(ruleConfig.getShardingAlgorithms().get(strategyConfig.getShardingAlgorithmName()))));
appendKeyGenerateStrategy(ruleConfig.getKeyGenerators(), autoTableRuleConfig.getKeyGenerateStrategy(), result);
appendAuditStrategy(ruleConfig.getAuditors(), null != autoTableRuleConfig.getAuditStrategy() ? autoTableRuleConfig.getAuditStrategy() : ruleConfig.getDefaultAuditStrategy(), result);
return result.toString();
Expand All @@ -155,7 +157,7 @@ private void appendStrategy(final ShardingStrategyConfiguration strategyConfig,
stringBuilder.append(ShardingDistSQLConstants.COMMA).append(System.lineSeparator());
}
String type = strategyConfig.getType().toLowerCase();
String algorithmDefinition = getAlgorithmType(shardingAlgorithms.get(strategyConfig.getShardingAlgorithmName()));
String algorithmDefinition = AlgorithmDistSQLConverter.getAlgorithmType(shardingAlgorithms.get(strategyConfig.getShardingAlgorithmName()));
switch (type) {
case ShardingDistSQLConstants.STANDARD:
StandardShardingStrategyConfiguration standardShardingStrategyConfig = (StandardShardingStrategyConfiguration) strategyConfig;
Expand All @@ -182,7 +184,7 @@ private void appendKeyGenerateStrategy(final Map<String, AlgorithmConfiguration>
return;
}
stringBuilder.append(ShardingDistSQLConstants.COMMA).append(System.lineSeparator());
String algorithmDefinition = getAlgorithmType(keyGenerators.get(keyGenerateStrategyConfig.getKeyGeneratorName()));
String algorithmDefinition = AlgorithmDistSQLConverter.getAlgorithmType(keyGenerators.get(keyGenerateStrategyConfig.getKeyGeneratorName()));
stringBuilder.append(String.format(ShardingDistSQLConstants.KEY_GENERATOR_STRATEGY, keyGenerateStrategyConfig.getColumn(), algorithmDefinition));
}

Expand All @@ -198,7 +200,7 @@ private String getAlgorithmTypes(final Map<String, AlgorithmConfiguration> audit
if (!auditorNames.isEmpty()) {
Iterator<String> iterator = auditorNames.iterator();
while (iterator.hasNext()) {
result.append(getAlgorithmType(auditors.get(iterator.next())));
result.append(AlgorithmDistSQLConverter.getAlgorithmType(auditors.get(iterator.next())));
if (iterator.hasNext()) {
result.append(ShardingDistSQLConstants.COMMA);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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.distsql.handler.engine.query.ral.convert;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;

import java.util.Iterator;
import java.util.Properties;
import java.util.TreeMap;

/**
* Algorithm DistSQL converter.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class AlgorithmDistSQLConverter {

/**
* Get algorithm type.
*
* @param algorithmConfig algorithm configuration
* @return algorithm type
*/
public static String getAlgorithmType(final AlgorithmConfiguration algorithmConfig) {
StringBuilder result = new StringBuilder();
if (null == algorithmConfig) {
return result.toString();
}
String type = algorithmConfig.getType().toLowerCase();
result.append(algorithmConfig.getProps().isEmpty()
? String.format(DistSQLScriptConstants.ALGORITHM_TYPE_WITHOUT_PROPS, type)
: String.format(DistSQLScriptConstants.ALGORITHM_TYPE, type, getAlgorithmProperties(algorithmConfig.getProps())));
return result.toString();
}

/**
* Get algorithm properties.
*
* @param props properties
* @return algorithm properties
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static String getAlgorithmProperties(final Properties props) {
StringBuilder result = new StringBuilder();
Iterator<String> iterator = new TreeMap(props).keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
Object value = props.get(key);
if (null == value) {
continue;
}
result.append(String.format(DistSQLScriptConstants.PROPERTY, key, value));
if (iterator.hasNext()) {
result.append(DistSQLScriptConstants.COMMA).append(' ');
}
}
return result.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@

package org.apache.shardingsphere.distsql.handler.engine.query.ral.convert;

import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI;

import java.util.Iterator;
import java.util.Properties;
import java.util.TreeMap;

/**
* Convert rule configuration provider.
*/
Expand All @@ -36,52 +31,10 @@ public interface ConvertRuleConfigurationProvider extends TypedSPI {
* Convert rule configuration to DistSQL.
*
* @param ruleConfig rule configuration
* @return DistSQL script
* @return DistSQL
*/
String convert(RuleConfiguration ruleConfig);

/**
* Get algorithm type.
*
* @param algorithmConfig algorithm configuration
* @return algorithm type
*/
default String getAlgorithmType(final AlgorithmConfiguration algorithmConfig) {
StringBuilder result = new StringBuilder();
if (null == algorithmConfig) {
return result.toString();
}
String type = algorithmConfig.getType().toLowerCase();
result.append(algorithmConfig.getProps().isEmpty()
? String.format(DistSQLScriptConstants.ALGORITHM_TYPE_WITHOUT_PROPS, type)
: String.format(DistSQLScriptConstants.ALGORITHM_TYPE, type, getAlgorithmProperties(algorithmConfig.getProps())));
return result.toString();
}

/**
* Get algorithm properties.
*
* @param props properties
* @return algorithm properties
*/
@SuppressWarnings({"unchecked", "rawtypes"})
default String getAlgorithmProperties(final Properties props) {
StringBuilder result = new StringBuilder();
Iterator<String> iterator = new TreeMap(props).keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
Object value = props.get(key);
if (null == value) {
continue;
}
result.append(String.format(DistSQLScriptConstants.PROPERTY, key, value));
if (iterator.hasNext()) {
result.append(DistSQLScriptConstants.COMMA).append(' ');
}
}
return result.toString();
}

@Override
Class<? extends RuleConfiguration> getType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ private String generateDistSQL(final YamlProxyDatabaseConfiguration yamlConfig)
StringBuilder result = new StringBuilder();
appendResourceDistSQL(yamlConfig, result);
for (RuleConfiguration each : swapToRuleConfigs(yamlConfig).values()) {
Class<? extends RuleConfiguration> type = each.getClass();
ConvertRuleConfigurationProvider convertRuleConfigProvider = TypedSPILoader.getService(ConvertRuleConfigurationProvider.class, type);
result.append(convertRuleConfigProvider.convert(each));
result.append(TypedSPILoader.getService(ConvertRuleConfigurationProvider.class, each.getClass()).convert(each));
}
return result.toString();
}
Expand Down

0 comments on commit d083faa

Please sign in to comment.