Skip to content

Commit

Permalink
Refactor shardingsphere-infra-expr to expose the use of SPI
Browse files Browse the repository at this point in the history
  • Loading branch information
linghengqian committed Sep 19, 2023
1 parent b11bbfb commit 0732525
Show file tree
Hide file tree
Showing 34 changed files with 500 additions and 245 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void checkDataSources(final String databaseName, final Map<String, DataS

private void checkWriteDataSourceNames(final String databaseName, final Map<String, DataSource> dataSourceMap, final Collection<String> addedWriteDataSourceNames,
final ReadwriteSplittingDataSourceRuleConfiguration config, final Collection<ShardingSphereRule> rules) {
for (String each : InlineExpressionParserFactory.newInstance().splitAndEvaluate(config.getWriteDataSourceName())) {
for (String each : InlineExpressionParserFactory.newInstance(config.getWriteDataSourceName()).splitAndEvaluate()) {
ShardingSpherePreconditions.checkState(dataSourceMap.containsKey(each) || containsInOtherRules(each, rules),
() -> new DataSourceNameExistedException(String.format("Write data source name `%s` not in database `%s`.", each, databaseName)));
ShardingSpherePreconditions.checkState(addedWriteDataSourceNames.add(each),
Expand All @@ -97,7 +97,7 @@ private boolean containsInOtherRules(final String datasourceName, final Collecti
}

private void checkReadeDataSourceNames(final String databaseName, final Map<String, DataSource> dataSourceMap, final Collection<String> addedReadDataSourceNames, final String readDataSourceName) {
for (String each : InlineExpressionParserFactory.newInstance().splitAndEvaluate(readDataSourceName)) {
for (String each : InlineExpressionParserFactory.newInstance(readDataSourceName).splitAndEvaluate()) {
ShardingSpherePreconditions.checkState(dataSourceMap.containsKey(each),
() -> new DataSourceNameExistedException(String.format("Read data source name `%s` not in database `%s`.", each, databaseName)));
ShardingSpherePreconditions.checkState(addedReadDataSourceNames.add(each),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ private Map<String, ReadwriteSplittingDataSourceRule> createDataSourceRules(fina

private Map<String, ReadwriteSplittingDataSourceRule> createStaticDataSourceRules(final ReadwriteSplittingDataSourceRuleConfiguration config,
final ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm) {
List<String> inlineReadwriteDataSourceNames = InlineExpressionParserFactory.newInstance().splitAndEvaluate(config.getName());
List<String> inlineWriteDatasourceNames = InlineExpressionParserFactory.newInstance().splitAndEvaluate(config.getWriteDataSourceName());
List<String> inlineReadwriteDataSourceNames = InlineExpressionParserFactory.newInstance(config.getName()).splitAndEvaluate();
List<String> inlineWriteDatasourceNames = InlineExpressionParserFactory.newInstance(config.getWriteDataSourceName()).splitAndEvaluate();
List<List<String>> inlineReadDatasourceNames = config.getReadDataSourceNames().stream()
.map(each -> InlineExpressionParserFactory.newInstance().splitAndEvaluate(each)).collect(Collectors.toList());
.map(each -> InlineExpressionParserFactory.newInstance(each).splitAndEvaluate()).collect(Collectors.toList());
ShardingSpherePreconditions.checkState(inlineWriteDatasourceNames.size() == inlineReadwriteDataSourceNames.size(),
() -> new InvalidInlineExpressionDataSourceNameException("Inline expression write data source names size error."));
inlineReadDatasourceNames.forEach(each -> ShardingSpherePreconditions.checkState(each.size() == inlineReadwriteDataSourceNames.size(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void init(final Properties props) {
private String getAlgorithmExpression(final Properties props) {
String algorithmExpression = props.getProperty(ALGORITHM_EXPRESSION_KEY, DEFAULT_ALGORITHM_EXPRESSION);
ShardingSpherePreconditions.checkNotNull(algorithmExpression, () -> new ShardingAlgorithmInitializationException(getType(), "Inline sharding algorithm expression can not be null."));
return InlineExpressionParserFactory.newInstance().handlePlaceHolder(algorithmExpression.trim());
return InlineExpressionParserFactory.newInstance(algorithmExpression.trim()).handlePlaceHolder();
}

@Override
Expand All @@ -67,7 +67,7 @@ private String doSharding(final Comparable<?> shardingValue) {
}

private Closure<?> createClosure() {
Closure<?> result = InlineExpressionParserFactory.newInstance().evaluateClosure(algorithmExpression).rehydrate(new Expando(), null, null);
Closure<?> result = InlineExpressionParserFactory.newInstance(algorithmExpression).evaluateClosure().rehydrate(new Expando(), null, null);
result.setResolveStrategy(Closure.DELEGATE_ONLY);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private String getAlgorithmExpression(final Properties props) {
String algorithmExpression = props.getProperty(ALGORITHM_EXPRESSION_KEY);
ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(algorithmExpression),
() -> new ShardingAlgorithmInitializationException(getType(), "Inline sharding algorithm expression can not be null."));
return InlineExpressionParserFactory.newInstance().handlePlaceHolder(algorithmExpression.trim());
return InlineExpressionParserFactory.newInstance(algorithmExpression.trim()).handlePlaceHolder();
}

private Collection<String> getShardingColumns(final Properties props) {
Expand Down Expand Up @@ -132,7 +132,7 @@ private Collection<Map<String, Comparable<?>>> flatten(final Collection<Map<Stri
}

private Closure<?> createClosure() {
Closure<?> result = InlineExpressionParserFactory.newInstance().evaluateClosure(algorithmExpression).rehydrate(new Expando(), null, null);
Closure<?> result = InlineExpressionParserFactory.newInstance(algorithmExpression).evaluateClosure().rehydrate(new Expando(), null, null);
result.setResolveStrategy(Closure.DELEGATE_ONLY);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private String getAlgorithmExpression(final Properties props) {
String expression = props.getProperty(ALGORITHM_EXPRESSION_KEY);
ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(expression),
() -> new ShardingAlgorithmInitializationException(getType(), "Inline sharding algorithm expression cannot be null or empty"));
return InlineExpressionParserFactory.newInstance().handlePlaceHolder(expression.trim());
return InlineExpressionParserFactory.newInstance(expression.trim()).handlePlaceHolder();
}

private boolean isAllowRangeQuery(final Properties props) {
Expand All @@ -83,7 +83,7 @@ public Collection<String> doSharding(final Collection<String> availableTargetNam
}

private Closure<?> createClosure() {
Closure<?> result = InlineExpressionParserFactory.newInstance().evaluateClosure(algorithmExpression).rehydrate(new Expando(), null, null);
Closure<?> result = InlineExpressionParserFactory.newInstance(algorithmExpression).evaluateClosure().rehydrate(new Expando(), null, null);
result.setResolveStrategy(Closure.DELEGATE_ONLY);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,12 @@ private Collection<String> getDataSourceNames(final Collection<ShardingTableRule
}

private Collection<String> getDataSourceNames(final ShardingAutoTableRuleConfiguration shardingAutoTableRuleConfig) {
List<String> actualDataSources = InlineExpressionParserFactory.newInstance().splitAndEvaluate(shardingAutoTableRuleConfig.getActualDataSources());
List<String> actualDataSources = InlineExpressionParserFactory.newInstance(shardingAutoTableRuleConfig.getActualDataSources()).splitAndEvaluate();
return new HashSet<>(actualDataSources);
}

private Collection<String> getDataSourceNames(final ShardingTableRuleConfiguration shardingTableRuleConfig) {
List<String> actualDataNodes = InlineExpressionParserFactory.newInstance().splitAndEvaluate(shardingTableRuleConfig.getActualDataNodes());
List<String> actualDataNodes = InlineExpressionParserFactory.newInstance(shardingTableRuleConfig.getActualDataNodes()).splitAndEvaluate();
return actualDataNodes.stream().map(each -> new DataNode(each).getDataSourceName()).collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public TableRule(final Collection<String> dataSourceNames, final String logicTab

public TableRule(final ShardingTableRuleConfiguration tableRuleConfig, final Collection<String> dataSourceNames, final String defaultGenerateKeyColumn) {
logicTable = tableRuleConfig.getLogicTable();
List<String> dataNodes = InlineExpressionParserFactory.newInstance().splitAndEvaluate(tableRuleConfig.getActualDataNodes());
List<String> dataNodes = InlineExpressionParserFactory.newInstance(tableRuleConfig.getActualDataNodes()).splitAndEvaluate();
dataNodeIndexMap = new HashMap<>(dataNodes.size(), 1F);
actualDataNodes = isEmptyDataNodes(dataNodes) ? generateDataNodes(tableRuleConfig.getLogicTable(), dataSourceNames) : generateDataNodes(dataNodes, dataSourceNames);
actualTables = getActualTables();
Expand Down Expand Up @@ -158,7 +158,7 @@ private List<String> getDataNodes(final ShardingAutoTableRuleConfiguration table
return new LinkedList<>();
}
List<String> dataSources = Strings.isNullOrEmpty(tableRuleConfig.getActualDataSources()) ? new LinkedList<>(dataSourceNames)
: InlineExpressionParserFactory.newInstance().splitAndEvaluate(tableRuleConfig.getActualDataSources());
: InlineExpressionParserFactory.newInstance(tableRuleConfig.getActualDataSources()).splitAndEvaluate();
return DataNodeUtils.getFormatDataNodes(shardingAlgorithm.getAutoTablesAmount(), logicTable, dataSources);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,13 @@ private static Collection<String> getDataSourceNames(final Collection<ShardingTa
}
Collection<String> result = new LinkedHashSet<>();
tableRuleConfigs.forEach(each -> result.addAll(getDataSourceNames(each)));
autoTableRuleConfigs.forEach(each -> result.addAll(InlineExpressionParserFactory.newInstance().splitAndEvaluate(each.getActualDataSources())));
autoTableRuleConfigs.forEach(each -> result.addAll(InlineExpressionParserFactory.newInstance(each.getActualDataSources()).splitAndEvaluate()));
return result;
}

private static Collection<String> getDataSourceNames(final ShardingTableRuleConfiguration shardingTableRuleConfig) {
return InlineExpressionParserFactory.newInstance()
.splitAndEvaluate(shardingTableRuleConfig.getActualDataNodes()).stream().map(each -> new DataNode(each).getDataSourceName()).collect(Collectors.toList());
return InlineExpressionParserFactory.newInstance(shardingTableRuleConfig.getActualDataNodes())
.splitAndEvaluate().stream().map(each -> new DataNode(each).getDataSourceName()).collect(Collectors.toList());
}

private static Collection<String> getDataSourceNames(final Collection<String> actualDataNodes) {
Expand Down Expand Up @@ -316,7 +316,7 @@ private static Collection<String> getRequiredDataSources(final ShardingRuleConfi
Collection<String> result = new LinkedHashSet<>();
result.addAll(config.getAutoTables().stream().map(ShardingAutoTableRuleConfiguration::getActualDataSources)
.map(each -> Splitter.on(",").trimResults().splitToList(each)).flatMap(Collection::stream).collect(Collectors.toSet()));
result.addAll(config.getTables().stream().map(each -> InlineExpressionParserFactory.newInstance().splitAndEvaluate(each.getActualDataNodes()))
result.addAll(config.getTables().stream().map(each -> InlineExpressionParserFactory.newInstance(each.getActualDataNodes()).splitAndEvaluate())
.flatMap(Collection::stream).distinct().map(each -> new DataNode(each).getDataSourceName()).collect(Collectors.toSet()));
return result;
}
Expand All @@ -327,7 +327,7 @@ private static <T extends AbstractTableRuleSegment> Collection<String> getRequir
}

private static Collection<String> parseDateSource(final String dateSource) {
return InlineExpressionParserFactory.newInstance().splitAndEvaluate(dateSource);
return InlineExpressionParserFactory.newInstance(dateSource).splitAndEvaluate();
}

private static Collection<String> getLogicDataSources(final ShardingSphereDatabase database) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void init(final Properties props) {
private String getAlgorithmExpression(final Properties props) {
String algorithmExpression = props.getProperty(ALGORITHM_EXPRESSION_KEY, DEFAULT_ALGORITHM_EXPRESSION);
Preconditions.checkNotNull(algorithmExpression, "Inline sharding algorithm expression can not be null.");
return InlineExpressionParserFactory.newInstance().handlePlaceHolder(algorithmExpression.trim());
return InlineExpressionParserFactory.newInstance(algorithmExpression.trim()).handlePlaceHolder();
}

@Override
Expand All @@ -61,7 +61,7 @@ private String doSharding(final Comparable<?> shardingValue) {
}

private Closure<?> createClosure() {
Closure<?> result = InlineExpressionParserFactory.newInstance().evaluateClosure(algorithmExpression).rehydrate(new Expando(), null, null);
Closure<?> result = InlineExpressionParserFactory.newInstance(algorithmExpression).evaluateClosure().rehydrate(new Expando(), null, null);
result.setResolveStrategy(Closure.DELEGATE_ONLY);
return result;
}
Expand Down
7 changes: 1 addition & 6 deletions infra/expr/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-infra-expr-hotsopt</artifactId>
<artifactId>shardingsphere-infra-expr-groovy</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
Expand All @@ -49,10 +49,5 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,45 @@
import org.apache.shardingsphere.infra.expr.spi.InlineExpressionParser;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;

import java.util.Properties;

/**
* Inline expression parser factory.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class InlineExpressionParserFactory {

// workaround for https://junit.org/junit5/docs/5.10.0/api/org.junit.jupiter.api/org/junit/jupiter/api/condition/EnabledInNativeImage.html
private static final boolean IS_SUBSTRATE_VM = "runtime".equals(System.getProperty("org.graalvm.nativeimage.imagecode"));
private static final String TYPE_NAME_BEGIN_SYMBOL = "<";

private static final String TYPE_NAME_END_SYMBOL = ">";

/**
* Create new instance of inline expression parser.
*
* Create new instance of inline expression parser by inlineExpression.
* And for compatibility reasons, inlineExpression allows to be null.
*
* @param inlineExpression inline expression
* @return created instance
*/
public static InlineExpressionParser newInstance() {
return TypedSPILoader.getService(InlineExpressionParser.class, IS_SUBSTRATE_VM ? "PURELIST" : "HOTSPOT");
public static InlineExpressionParser newInstance(final String inlineExpression) {
Properties props = new Properties();
if (null == inlineExpression) {
return TypedSPILoader.getService(InlineExpressionParser.class, "GROOVY", props);
}
if (!inlineExpression.startsWith(TYPE_NAME_BEGIN_SYMBOL)) {
props.setProperty(InlineExpressionParser.INLINE_EXPRESSION_KEY, inlineExpression);
return TypedSPILoader.getService(InlineExpressionParser.class, "GROOVY", props);
}
Integer typeBeginIndex = inlineExpression.indexOf(TYPE_NAME_BEGIN_SYMBOL);
Integer typeEndIndex = inlineExpression.indexOf(TYPE_NAME_END_SYMBOL);
props.setProperty(InlineExpressionParser.INLINE_EXPRESSION_KEY, removeTypeNameInExpr(inlineExpression, typeBeginIndex, typeEndIndex));
return TypedSPILoader.getService(InlineExpressionParser.class, getTypeName(inlineExpression, typeBeginIndex, typeEndIndex), props);
}

private static String getTypeName(final String inlineExpression, final Integer beginIndex, final Integer endIndex) {
return beginIndex.equals(-1) || endIndex.equals(-1) ? "GROOVY" : inlineExpression.substring(beginIndex + 1, endIndex);
}

private static String removeTypeNameInExpr(final String inlineExpression, final Integer beginIndex, final Integer endIndex) {
return inlineExpression.substring(0, beginIndex) + inlineExpression.substring(endIndex + 1);
}
}
Loading

0 comments on commit 0732525

Please sign in to comment.