This is an automated email from the ASF dual-hosted git repository.
sunnianjun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new d198415e2d7 Use orElseThrow to instead of optional.isPresent (#31823)
d198415e2d7 is described below
commit d198415e2d7e69909f46a12dd5f736ce44e5665f
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Jun 23 23:37:29 2024 +0800
Use orElseThrow to instead of optional.isPresent (#31823)
---
.../generator/EncryptPredicateColumnTokenGenerator.java | 6 +++---
.../EncryptPredicateRightValueTokenGenerator.java | 7 ++-----
.../apache/shardingsphere/encrypt/rule/EncryptRule.java | 4 +---
...AlterReadwriteSplittingStorageUnitStatusExecutor.java | 16 +++++++---------
.../shardingsphere/sharding/rule/ShardingRule.java | 4 +---
.../distsql/handler/enums/ShardingStrategyType.java | 7 ++-----
.../update/DropDefaultShardingStrategyExecutor.java | 10 ++++------
.../calculator/CRC32SingleTableInventoryCalculator.java | 7 +++----
.../single/route/engine/SingleStandardRouteEngine.java | 6 +++---
.../proxy/backend/hbase/context/HBaseContext.java | 7 +++----
10 files changed, 29 insertions(+), 45 deletions(-)
diff --git
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptPredicateColumnTokenGenerator.java
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptPredicateColumnTokenGenerator.java
index f514f9eb23c..6689a1f8c89 100644
---
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptPredicateColumnTokenGenerator.java
+++
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptPredicateColumnTokenGenerator.java
@@ -25,6 +25,7 @@ import
org.apache.shardingsphere.encrypt.rewrite.token.util.EncryptTokenGenerato
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.encrypt.rule.EncryptTable;
import org.apache.shardingsphere.encrypt.rule.column.EncryptColumn;
+import org.apache.shardingsphere.encrypt.rule.column.item.LikeQueryColumnItem;
import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.Projection;
import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.ColumnProjection;
import
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
@@ -107,9 +108,8 @@ public final class EncryptPredicateColumnTokenGenerator
implements CollectionSQL
int startIndex = columnSegment.getOwner().isPresent() ?
columnSegment.getOwner().get().getStopIndex() + 2 :
columnSegment.getStartIndex();
int stopIndex = columnSegment.getStopIndex();
if (includesLike(whereSegments, columnSegment)) {
-
ShardingSpherePreconditions.checkState(encryptColumn.getLikeQuery().isPresent(),
() -> new UnsupportedEncryptSQLException("LIKE"));
- return new SubstitutableColumnNameToken(startIndex, stopIndex,
createColumnProjections(encryptColumn.getLikeQuery().get().getName(),
columnSegment.getIdentifier().getQuoteCharacter()),
- databaseType);
+ LikeQueryColumnItem likeQueryColumnItem =
encryptColumn.getLikeQuery().orElseThrow(() -> new
UnsupportedEncryptSQLException("LIKE"));
+ return new SubstitutableColumnNameToken(startIndex, stopIndex,
createColumnProjections(likeQueryColumnItem.getName(),
columnSegment.getIdentifier().getQuoteCharacter()), databaseType);
}
Collection<Projection> columnProjections =
encryptColumn.getAssistedQuery().map(optional ->
createColumnProjections(optional.getName(),
columnSegment.getIdentifier().getQuoteCharacter()))
diff --git
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptPredicateRightValueTokenGenerator.java
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptPredicateRightValueTokenGenerator.java
index 4d3c686d007..76e2a308ed4 100644
---
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptPredicateRightValueTokenGenerator.java
+++
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptPredicateRightValueTokenGenerator.java
@@ -39,7 +39,6 @@ import
org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
import
org.apache.shardingsphere.infra.rewrite.sql.token.generator.CollectionSQLTokenGenerator;
import
org.apache.shardingsphere.infra.rewrite.sql.token.generator.aware.ParametersAware;
import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.SQLToken;
-import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.FunctionSegment;
import java.util.Collection;
@@ -47,7 +46,6 @@ import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
-import java.util.Optional;
/**
* Predicate right value token generator for encrypt.
@@ -101,9 +99,8 @@ public final class EncryptPredicateRightValueTokenGenerator
implements Collectio
private List<Object> getEncryptedValues(final String schemaName, final
EncryptTable encryptTable, final EncryptCondition encryptCondition, final
List<Object> originalValues) {
EncryptColumn encryptColumn =
encryptTable.getEncryptColumn(encryptCondition.getColumnName());
if (encryptCondition instanceof EncryptBinaryCondition &&
"LIKE".equalsIgnoreCase(((EncryptBinaryCondition)
encryptCondition).getOperator())) {
- Optional<LikeQueryColumnItem> likeQueryColumnItem =
encryptColumn.getLikeQuery();
-
ShardingSpherePreconditions.checkState(likeQueryColumnItem.isPresent(), () ->
new UnsupportedEncryptSQLException("LIKE"));
- return likeQueryColumnItem.get().encrypt(databaseName, schemaName,
encryptCondition.getTableName(), encryptCondition.getColumnName(),
originalValues);
+ LikeQueryColumnItem likeQueryColumnItem =
encryptColumn.getLikeQuery().orElseThrow(() -> new
UnsupportedEncryptSQLException("LIKE"));
+ return likeQueryColumnItem.encrypt(databaseName, schemaName,
encryptCondition.getTableName(), encryptCondition.getColumnName(),
originalValues);
}
return encryptColumn.getAssistedQuery()
.map(optional -> optional.encrypt(databaseName, schemaName,
encryptCondition.getTableName(), encryptCondition.getColumnName(),
originalValues))
diff --git
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
index 1240f946fdf..10a4ecf81a6 100644
---
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
+++
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
@@ -116,9 +116,7 @@ public final class EncryptRule implements DatabaseRule,
PartialRuleUpdateSupport
* @return encrypt table
*/
public EncryptTable getEncryptTable(final String tableName) {
- Optional<EncryptTable> encryptTable = findEncryptTable(tableName);
- ShardingSpherePreconditions.checkState(encryptTable.isPresent(), () ->
new EncryptTableNotFoundException(tableName));
- return encryptTable.get();
+ return findEncryptTable(tableName).orElseThrow(() -> new
EncryptTableNotFoundException(tableName));
}
@Override
diff --git
a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingStorageUnitStatusExecutor.java
b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingStorageUnitStatusExecutor.java
index 0b526395485..d96c8f6974d 100644
---
a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingStorageUnitStatusExecutor.java
+++
b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingStorageUnitStatusExecutor.java
@@ -35,8 +35,6 @@ import
org.apache.shardingsphere.readwritesplitting.exception.actual.ReadwriteSp
import
org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingDataSourceGroupRule;
import
org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule;
-import java.util.Optional;
-
/**
* Alter readwrite-splitting storage unit status executor.
*/
@@ -59,17 +57,17 @@ public final class
AlterReadwriteSplittingStorageUnitStatusExecutor
}
private void checkBeforeUpdate(final
AlterReadwriteSplittingStorageUnitStatusStatement sqlStatement) {
- Optional<ReadwriteSplittingDataSourceGroupRule> dataSourceGroupRule =
rule.getDataSourceRuleGroups().values().stream()
- .filter(each ->
each.getName().equalsIgnoreCase(sqlStatement.getRuleName())).findAny();
-
ShardingSpherePreconditions.checkState(dataSourceGroupRule.isPresent(), () ->
new MissingRequiredRuleException("Readwrite-splitting", database.getName(),
sqlStatement.getRuleName()));
-
ShardingSpherePreconditions.checkContains(dataSourceGroupRule.get().getReadwriteSplittingGroup().getReadDataSources(),
sqlStatement.getStorageUnitName(),
+ ReadwriteSplittingDataSourceGroupRule dataSourceGroupRule =
rule.getDataSourceRuleGroups().values().stream()
+ .filter(each ->
each.getName().equalsIgnoreCase(sqlStatement.getRuleName())).findAny()
+ .orElseThrow(() -> new
MissingRequiredRuleException("Readwrite-splitting", database.getName(),
sqlStatement.getRuleName()));
+
ShardingSpherePreconditions.checkContains(dataSourceGroupRule.getReadwriteSplittingGroup().getReadDataSources(),
sqlStatement.getStorageUnitName(),
() -> new
ReadwriteSplittingActualDataSourceNotFoundException(ReadwriteSplittingDataSourceType.READ,
- sqlStatement.getStorageUnitName(), new
ReadwriteSplittingRuleExceptionIdentifier(database.getName(),
dataSourceGroupRule.get().getName())));
+ sqlStatement.getStorageUnitName(), new
ReadwriteSplittingRuleExceptionIdentifier(database.getName(),
dataSourceGroupRule.getName())));
if (sqlStatement.isEnable()) {
-
ShardingSpherePreconditions.checkContains(dataSourceGroupRule.get().getDisabledDataSourceNames(),
sqlStatement.getStorageUnitName(),
+
ShardingSpherePreconditions.checkContains(dataSourceGroupRule.getDisabledDataSourceNames(),
sqlStatement.getStorageUnitName(),
() -> new InvalidStorageUnitStatusException("storage unit
is not disabled"));
} else {
-
ShardingSpherePreconditions.checkNotContains(dataSourceGroupRule.get().getDisabledDataSourceNames(),
sqlStatement.getStorageUnitName(),
+
ShardingSpherePreconditions.checkNotContains(dataSourceGroupRule.getDisabledDataSourceNames(),
sqlStatement.getStorageUnitName(),
() -> new InvalidStorageUnitStatusException("storage unit
is already disabled"));
}
}
diff --git
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
index 6a66c8bab07..3750b4e583a 100644
---
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
+++
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
@@ -309,9 +309,7 @@ public final class ShardingRule implements DatabaseRule {
* @throws ShardingTableRuleNotFoundException sharding table rule not
found exception
*/
public ShardingTable getShardingTable(final String logicTableName) {
- Optional<ShardingTable> shardingTable =
findShardingTable(logicTableName);
- ShardingSpherePreconditions.checkState(shardingTable.isPresent(), ()
-> new
ShardingTableRuleNotFoundException(Collections.singleton(logicTableName)));
- return shardingTable.get();
+ return findShardingTable(logicTableName).orElseThrow(() -> new
ShardingTableRuleNotFoundException(Collections.singleton(logicTableName)));
}
/**
diff --git
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/enums/ShardingStrategyType.java
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/enums/ShardingStrategyType.java
index 347cbdc55a8..06278cc6122 100644
---
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/enums/ShardingStrategyType.java
+++
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/enums/ShardingStrategyType.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.sharding.distsql.handler.enums;
-import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import
org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
import
org.apache.shardingsphere.sharding.api.config.strategy.sharding.ComplexShardingStrategyConfiguration;
import
org.apache.shardingsphere.sharding.api.config.strategy.sharding.HintShardingStrategyConfiguration;
@@ -27,7 +26,6 @@ import
org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardS
import java.util.Arrays;
import java.util.Collection;
-import java.util.Optional;
/**
* Sharding strategy type.
@@ -177,9 +175,8 @@ public enum ShardingStrategyType {
* @return sharding strategy type
*/
public static ShardingStrategyType getValueOf(final
ShardingStrategyConfiguration config) {
- Optional<ShardingStrategyType> result =
Arrays.stream(values()).filter(each ->
config.getClass().isAssignableFrom(each.getImplementedClass())).findFirst();
- ShardingSpherePreconditions.checkState(result.isPresent(), () -> new
UnsupportedOperationException(String.format("unsupported strategy type: `%s`.",
config.getClass().getName())));
- return result.get();
+ return Arrays.stream(values()).filter(each ->
config.getClass().isAssignableFrom(each.getImplementedClass())).findFirst()
+ .orElseThrow(() -> new
UnsupportedOperationException(String.format("unsupported strategy type: `%s`.",
config.getClass().getName())));
}
/**
diff --git
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropDefaultShardingStrategyExecutor.java
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropDefaultShardingStrategyExecutor.java
index b03766cdcf0..14eb116ddb2 100644
---
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropDefaultShardingStrategyExecutor.java
+++
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropDefaultShardingStrategyExecutor.java
@@ -18,10 +18,9 @@
package org.apache.shardingsphere.sharding.distsql.handler.update;
import lombok.Setter;
-import
org.apache.shardingsphere.infra.exception.kernel.metadata.rule.MissingRequiredRuleException;
-import
org.apache.shardingsphere.distsql.handler.required.DistSQLExecutorCurrentRuleRequired;
import
org.apache.shardingsphere.distsql.handler.engine.update.rdl.rule.spi.database.DatabaseRuleDropExecutor;
-import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
+import
org.apache.shardingsphere.distsql.handler.required.DistSQLExecutorCurrentRuleRequired;
+import
org.apache.shardingsphere.infra.exception.kernel.metadata.rule.MissingRequiredRuleException;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import
org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration;
@@ -50,9 +49,8 @@ public final class DropDefaultShardingStrategyExecutor
implements DatabaseRuleDr
}
private void checkExist(final DropDefaultShardingStrategyStatement
sqlStatement) {
- Optional<ShardingStrategyConfiguration> strategyConfig =
getStrategyConfiguration(sqlStatement.getDefaultType());
- ShardingSpherePreconditions.checkState(strategyConfig.isPresent(), ()
-> new MissingRequiredRuleException(
- String.format("Default sharding %s strategy",
sqlStatement.getDefaultType().toLowerCase()), database.getName()));
+ getStrategyConfiguration(sqlStatement.getDefaultType())
+ .orElseThrow(() -> new
MissingRequiredRuleException(String.format("Default sharding %s strategy",
sqlStatement.getDefaultType().toLowerCase()), database.getName()));
}
private Optional<ShardingStrategyConfiguration>
getStrategyConfiguration(final String type) {
diff --git
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/table/calculator/CRC32SingleTableInventoryCalculator.java
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/table/calculator/CRC32SingleTableInventoryCalculator.java
index 6f4e997aa75..455fca98844 100644
---
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/table/calculator/CRC32SingleTableInventoryCalculator.java
+++
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/consistencycheck/table/calculator/CRC32SingleTableInventoryCalculator.java
@@ -24,7 +24,6 @@ import
org.apache.shardingsphere.data.pipeline.core.consistencycheck.result.Sing
import
org.apache.shardingsphere.data.pipeline.core.exception.data.PipelineTableDataConsistencyCheckLoadingFailedException;
import
org.apache.shardingsphere.data.pipeline.core.sqlbuilder.sql.PipelineDataConsistencyCalculateSQLBuilder;
import
org.apache.shardingsphere.infra.algorithm.core.exception.UnsupportedAlgorithmOnDatabaseTypeException;
-import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import java.sql.Connection;
import java.sql.PreparedStatement;
@@ -50,11 +49,11 @@ public final class CRC32SingleTableInventoryCalculator
extends AbstractSingleTab
}
private CalculatedItem calculateCRC32(final
PipelineDataConsistencyCalculateSQLBuilder pipelineSQLBuilder, final
SingleTableInventoryCalculateParameter param, final String columnName) {
- Optional<String> sql =
pipelineSQLBuilder.buildCRC32SQL(param.getSchemaName(),
param.getLogicTableName(), columnName);
- ShardingSpherePreconditions.checkState(sql.isPresent(), () -> new
UnsupportedAlgorithmOnDatabaseTypeException("DataConsistencyCalculate",
"CRC32", param.getDatabaseType()));
+ String sql = pipelineSQLBuilder.buildCRC32SQL(param.getSchemaName(),
param.getLogicTableName(), columnName)
+ .orElseThrow(() -> new
UnsupportedAlgorithmOnDatabaseTypeException("DataConsistencyCalculate",
"CRC32", param.getDatabaseType()));
try (
Connection connection = param.getDataSource().getConnection();
- PreparedStatement preparedStatement =
connection.prepareStatement(sql.get());
+ PreparedStatement preparedStatement =
connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery()) {
setCurrentStatement(preparedStatement);
resultSet.next();
diff --git
a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleStandardRouteEngine.java
b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleStandardRouteEngine.java
index 4563a7623bb..b43d53040c1 100644
---
a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleStandardRouteEngine.java
+++
b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/engine/SingleStandardRouteEngine.java
@@ -104,9 +104,9 @@ public final class SingleStandardRouteEngine implements
SingleRouteEngine {
private void fillRouteContext(final SingleRule singleRule, final
RouteContext routeContext, final Collection<QualifiedTable> logicTables) {
for (QualifiedTable each : logicTables) {
String tableName = each.getTableName();
- Optional<DataNode> dataNode =
singleRule.getAttributes().getAttribute(MutableDataNodeRuleAttribute.class).findTableDataNode(each.getSchemaName(),
tableName);
- ShardingSpherePreconditions.checkState(dataNode.isPresent(), () ->
new SingleTableNotFoundException(tableName));
- String dataSource = dataNode.get().getDataSourceName();
+ DataNode dataNode =
singleRule.getAttributes().getAttribute(MutableDataNodeRuleAttribute.class).findTableDataNode(each.getSchemaName(),
tableName)
+ .orElseThrow(() -> new
SingleTableNotFoundException(tableName));
+ String dataSource = dataNode.getDataSourceName();
routeContext.putRouteUnit(new RouteMapper(dataSource, dataSource),
Collections.singletonList(new RouteMapper(tableName, tableName)));
}
}
diff --git
a/proxy/backend/type/hbase/src/main/java/org/apache/shardingsphere/proxy/backend/hbase/context/HBaseContext.java
b/proxy/backend/type/hbase/src/main/java/org/apache/shardingsphere/proxy/backend/hbase/context/HBaseContext.java
index cc992b41aac..7a8d06f02ed 100644
---
a/proxy/backend/type/hbase/src/main/java/org/apache/shardingsphere/proxy/backend/hbase/context/HBaseContext.java
+++
b/proxy/backend/type/hbase/src/main/java/org/apache/shardingsphere/proxy/backend/hbase/context/HBaseContext.java
@@ -40,7 +40,6 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
-import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
@@ -160,9 +159,9 @@ public final class HBaseContext implements AutoCloseable {
* @throws SQLException SQL exception
*/
public Connection getConnectionByClusterName(final String clusterName)
throws SQLException {
- Optional<HBaseCluster> cluster = connections.stream().filter(each ->
each.getClusterName().equalsIgnoreCase(clusterName)).findFirst();
- ShardingSpherePreconditions.checkState(cluster.isPresent(), () -> new
SQLException(String.format("Cluster `%s` is not exists", clusterName)));
- return cluster.get().getConnection();
+ HBaseCluster cluster = connections.stream().filter(each ->
each.getClusterName().equalsIgnoreCase(clusterName)).findFirst()
+ .orElseThrow(() -> new SQLException(String.format("Cluster
`%s` is not exists", clusterName)));
+ return cluster.getConnection();
}
@Override