未验证 提交 31670513 编写于 作者: L Liang Zhang 提交者: GitHub

Use java 8 Optional instead of Guava for sharding-ui (#4679)

* Use java 8 Optional instead of Guava

* Use java 8 Optional instead of Guava for sharding-ui
上级 80773268
...@@ -17,9 +17,7 @@ ...@@ -17,9 +17,7 @@
package org.apache.shardingsphere.shardingjdbc.executor.batch; package org.apache.shardingsphere.shardingjdbc.executor.batch;
import com.google.common.base.Optional; import com.google.common.base.Preconditions;
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterators;
import lombok.Getter; import lombok.Getter;
import org.apache.shardingsphere.sharding.execute.context.ShardingExecutionContext; import org.apache.shardingsphere.sharding.execute.context.ShardingExecutionContext;
import org.apache.shardingsphere.sharding.execute.sql.StatementExecuteUnit; import org.apache.shardingsphere.sharding.execute.sql.StatementExecuteUnit;
...@@ -44,7 +42,7 @@ import java.util.LinkedList; ...@@ -44,7 +42,7 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Objects; import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
...@@ -118,10 +116,11 @@ public final class BatchPreparedStatementExecutor extends AbstractStatementExecu ...@@ -118,10 +116,11 @@ public final class BatchPreparedStatementExecutor extends AbstractStatementExecu
} }
private void handleOldBatchRouteUnits(final Collection<BatchRouteUnit> newRouteUnits) { private void handleOldBatchRouteUnits(final Collection<BatchRouteUnit> newRouteUnits) {
for (final BatchRouteUnit each : newRouteUnits) { for (BatchRouteUnit each : newRouteUnits) {
Optional<BatchRouteUnit> batchRouteUnit = Iterators.tryFind(routeUnits.iterator(), input -> Objects.equals(input, each)); for (BatchRouteUnit unit : routeUnits) {
if (batchRouteUnit.isPresent()) { if (unit.equals(each)) {
reviseBatchRouteUnit(batchRouteUnit.get(), each); reviseBatchRouteUnit(unit, each);
}
} }
} }
} }
...@@ -169,8 +168,7 @@ public final class BatchPreparedStatementExecutor extends AbstractStatementExecu ...@@ -169,8 +168,7 @@ public final class BatchPreparedStatementExecutor extends AbstractStatementExecu
for (StatementExecuteUnit eachUnit : each.getInputs()) { for (StatementExecuteUnit eachUnit : each.getInputs()) {
Map<Integer, Integer> jdbcAndActualAddBatchCallTimesMap = Collections.emptyMap(); Map<Integer, Integer> jdbcAndActualAddBatchCallTimesMap = Collections.emptyMap();
for (BatchRouteUnit eachRouteUnit : routeUnits) { for (BatchRouteUnit eachRouteUnit : routeUnits) {
if (eachRouteUnit.getExecutionUnit().getDataSourceName().equals(eachUnit.getExecutionUnit().getDataSourceName()) if (isSameDataSourceAndSQL(eachRouteUnit, eachUnit)) {
&& eachRouteUnit.getExecutionUnit().getSqlUnit().getSql().equals(eachUnit.getExecutionUnit().getSqlUnit().getSql())) {
jdbcAndActualAddBatchCallTimesMap = eachRouteUnit.getJdbcAndActualAddBatchCallTimesMap(); jdbcAndActualAddBatchCallTimesMap = eachRouteUnit.getJdbcAndActualAddBatchCallTimesMap();
break; break;
} }
...@@ -185,6 +183,11 @@ public final class BatchPreparedStatementExecutor extends AbstractStatementExecu ...@@ -185,6 +183,11 @@ public final class BatchPreparedStatementExecutor extends AbstractStatementExecu
return result; return result;
} }
private boolean isSameDataSourceAndSQL(final BatchRouteUnit batchRouteUnit, final StatementExecuteUnit statementExecuteUnit) {
return batchRouteUnit.getExecutionUnit().getDataSourceName().equals(statementExecuteUnit.getExecutionUnit().getDataSourceName())
&& batchRouteUnit.getExecutionUnit().getSqlUnit().getSql().equals(statementExecuteUnit.getExecutionUnit().getSqlUnit().getSql());
}
/** /**
* Get statements. * Get statements.
* *
...@@ -218,14 +221,18 @@ public final class BatchPreparedStatementExecutor extends AbstractStatementExecu ...@@ -218,14 +221,18 @@ public final class BatchPreparedStatementExecutor extends AbstractStatementExecu
} }
private Optional<StatementExecuteUnit> getStatementExecuteUnit(final Statement statement, final InputGroup<StatementExecuteUnit> executeGroup) { private Optional<StatementExecuteUnit> getStatementExecuteUnit(final Statement statement, final InputGroup<StatementExecuteUnit> executeGroup) {
return Iterators.tryFind(executeGroup.getInputs().iterator(), input -> input.getStatement().equals(statement)); for (StatementExecuteUnit each : executeGroup.getInputs()) {
if (each.getStatement().equals(statement)) {
return Optional.of(each);
}
}
return Optional.empty();
} }
private List<List<Object>> getParameterSets(final StatementExecuteUnit executeUnit) { private List<List<Object>> getParameterSets(final StatementExecuteUnit executeUnit) {
List<List<Object>> result; Optional<BatchRouteUnit> batchRouteUnit = routeUnits.stream().filter(routeUnit -> isSameDataSourceAndSQL(routeUnit, executeUnit)).findFirst();
result = Collections2.filter(routeUnits, input -> input.getExecutionUnit().getDataSourceName().equals(executeUnit.getExecutionUnit().getDataSourceName()) Preconditions.checkState(batchRouteUnit.isPresent());
&& input.getExecutionUnit().getSqlUnit().getSql().equals(executeUnit.getExecutionUnit().getSqlUnit().getSql())).iterator().next().getParameterSets(); return batchRouteUnit.get().getParameterSets();
return result;
} }
@Override @Override
......
...@@ -36,12 +36,14 @@ ...@@ -36,12 +36,14 @@
</modules> </modules>
<properties> <properties>
<java.version>1.8</java.version>
<spring-boot.version>1.5.21.RELEASE</spring-boot.version> <spring-boot.version>1.5.21.RELEASE</spring-boot.version>
<apache-rat-plugin.version>0.12</apache-rat-plugin.version> <apache-rat-plugin.version>0.12</apache-rat-plugin.version>
<takari-maven-plugin.version>0.6.1</takari-maven-plugin.version> <maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
<frontend-maven-plugin.version>1.6</frontend-maven-plugin.version> <frontend-maven-plugin.version>1.6</frontend-maven-plugin.version>
<os-maven-plugin.version>1.5.0.Final</os-maven-plugin.version> <os-maven-plugin.version>1.5.0.Final</os-maven-plugin.version>
<takari-maven-plugin.version>0.6.1</takari-maven-plugin.version>
<maven.deploy.skip>true</maven.deploy.skip> <maven.deploy.skip>true</maven.deploy.skip>
</properties> </properties>
...@@ -70,6 +72,17 @@ ...@@ -70,6 +72,17 @@
<pluginManagement> <pluginManagement>
<plugins> <plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<testSource>${java.version}</testSource>
<testTarget>${java.version}</testTarget>
</configuration>
<version>${maven-compiler-plugin.version}</version>
</plugin>
<!-- mvn -N io.takari:maven:wrapper -Dmaven=3.5.4 --> <!-- mvn -N io.takari:maven:wrapper -Dmaven=3.5.4 -->
<plugin> <plugin>
<groupId>io.takari</groupId> <groupId>io.takari</groupId>
......
...@@ -17,13 +17,13 @@ ...@@ -17,13 +17,13 @@
package org.apache.shardingsphere.ui.common.domain; package org.apache.shardingsphere.ui.common.domain;
import com.google.common.base.Optional;
import java.util.HashMap;
import java.util.Map;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
/** /**
* Forward service configs. * Forward service configs.
*/ */
...@@ -50,7 +50,7 @@ public final class ForwardServiceConfigs { ...@@ -50,7 +50,7 @@ public final class ForwardServiceConfigs {
* @return forward service config * @return forward service config
*/ */
public Optional<ForwardServiceConfig> getForwardServiceConfig(final String forwardServiceType) { public Optional<ForwardServiceConfig> getForwardServiceConfig(final String forwardServiceType) {
return Optional.fromNullable(forwardServiceConfigs.get(forwardServiceType)); return Optional.ofNullable(forwardServiceConfigs.get(forwardServiceType));
} }
/** /**
...@@ -60,6 +60,6 @@ public final class ForwardServiceConfigs { ...@@ -60,6 +60,6 @@ public final class ForwardServiceConfigs {
* @return forward service config * @return forward service config
*/ */
public Optional<ForwardServiceConfig> removeForwardServiceConfig(final String forwardServiceType) { public Optional<ForwardServiceConfig> removeForwardServiceConfig(final String forwardServiceType) {
return Optional.fromNullable(forwardServiceConfigs.remove(forwardServiceType)); return Optional.ofNullable(forwardServiceConfigs.remove(forwardServiceType));
} }
} }
...@@ -17,10 +17,11 @@ ...@@ -17,10 +17,11 @@
package org.apache.shardingsphere.ui.servcie; package org.apache.shardingsphere.ui.servcie;
import com.google.common.base.Optional;
import org.apache.shardingsphere.ui.common.domain.RegistryCenterConfig; import org.apache.shardingsphere.ui.common.domain.RegistryCenterConfig;
import org.apache.shardingsphere.ui.common.domain.RegistryCenterConfigs; import org.apache.shardingsphere.ui.common.domain.RegistryCenterConfigs;
import java.util.Optional;
/** /**
* Registry center config service. * Registry center config service.
*/ */
......
...@@ -20,7 +20,7 @@ package org.apache.shardingsphere.ui.servcie.forward.impl; ...@@ -20,7 +20,7 @@ package org.apache.shardingsphere.ui.servcie.forward.impl;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import com.google.common.base.Optional; import java.util.Optional;
import org.apache.shardingsphere.ui.common.constant.ForwardServiceType; import org.apache.shardingsphere.ui.common.constant.ForwardServiceType;
import org.apache.shardingsphere.ui.common.domain.ForwardServiceConfig; import org.apache.shardingsphere.ui.common.domain.ForwardServiceConfig;
import org.apache.shardingsphere.ui.common.exception.ShardingSphereUIException; import org.apache.shardingsphere.ui.common.exception.ShardingSphereUIException;
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
package org.apache.shardingsphere.ui.servcie.impl; package org.apache.shardingsphere.ui.servcie.impl;
import com.google.common.base.Optional;
import org.apache.shardingsphere.ui.common.domain.RegistryCenterConfig; import org.apache.shardingsphere.ui.common.domain.RegistryCenterConfig;
import org.apache.shardingsphere.ui.common.domain.RegistryCenterConfigs; import org.apache.shardingsphere.ui.common.domain.RegistryCenterConfigs;
import org.apache.shardingsphere.ui.common.exception.ShardingSphereUIException; import org.apache.shardingsphere.ui.common.exception.ShardingSphereUIException;
...@@ -26,6 +25,8 @@ import org.apache.shardingsphere.ui.servcie.RegistryCenterConfigService; ...@@ -26,6 +25,8 @@ import org.apache.shardingsphere.ui.servcie.RegistryCenterConfigService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Optional;
/** /**
* Implementation of Registry center config service. * Implementation of Registry center config service.
*/ */
...@@ -42,7 +43,7 @@ public final class RegistryCenterConfigServiceImpl implements RegistryCenterConf ...@@ -42,7 +43,7 @@ public final class RegistryCenterConfigServiceImpl implements RegistryCenterConf
@Override @Override
public Optional<RegistryCenterConfig> loadActivated() { public Optional<RegistryCenterConfig> loadActivated() {
return Optional.fromNullable(findActivatedRegistryCenterConfiguration(loadAll())); return Optional.ofNullable(findActivatedRegistryCenterConfiguration(loadAll()));
} }
@Override @Override
......
...@@ -17,18 +17,19 @@ ...@@ -17,18 +17,19 @@
package org.apache.shardingsphere.ui.servcie.impl; package org.apache.shardingsphere.ui.servcie.impl;
import com.google.common.base.Optional;
import org.apache.shardingsphere.orchestration.center.api.RegistryCenterRepository; import org.apache.shardingsphere.orchestration.center.api.RegistryCenterRepository;
import org.apache.shardingsphere.orchestration.internal.registry.config.node.ConfigurationNode;
import org.apache.shardingsphere.orchestration.internal.registry.state.node.StateNode;
import org.apache.shardingsphere.ui.common.domain.RegistryCenterConfig; import org.apache.shardingsphere.ui.common.domain.RegistryCenterConfig;
import org.apache.shardingsphere.ui.common.exception.ShardingSphereUIException; import org.apache.shardingsphere.ui.common.exception.ShardingSphereUIException;
import org.apache.shardingsphere.ui.servcie.RegistryCenterConfigService; import org.apache.shardingsphere.ui.servcie.RegistryCenterConfigService;
import org.apache.shardingsphere.ui.servcie.RegistryCenterService; import org.apache.shardingsphere.ui.servcie.RegistryCenterService;
import org.apache.shardingsphere.ui.util.RegistryCenterRepositoryFactory; import org.apache.shardingsphere.ui.util.RegistryCenterRepositoryFactory;
import org.apache.shardingsphere.orchestration.internal.registry.config.node.ConfigurationNode;
import org.apache.shardingsphere.orchestration.internal.registry.state.node.StateNode;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Optional;
/** /**
* Implementation of Registry center service. * Implementation of Registry center service.
*/ */
......
...@@ -94,7 +94,7 @@ public final class RegistryCenterController { ...@@ -94,7 +94,7 @@ public final class RegistryCenterController {
*/ */
@RequestMapping(value = "/activated", method = RequestMethod.GET) @RequestMapping(value = "/activated", method = RequestMethod.GET)
public ResponseResult<RegistryCenterConfig> activated() { public ResponseResult<RegistryCenterConfig> activated() {
return ResponseResultUtil.build(registryCenterConfigService.loadActivated().orNull()); return ResponseResultUtil.build(registryCenterConfigService.loadActivated().orElse(null));
} }
} }
...@@ -31,7 +31,7 @@ import org.springframework.web.bind.annotation.RequestMapping; ...@@ -31,7 +31,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.google.common.base.Optional; import java.util.Optional;
/** /**
* Sharding scaling forward controller. * Sharding scaling forward controller.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册