You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2020/10/08 11:38:40 UTC
[shardingsphere] branch master updated: Add TimeServiceFactory to
refactor TimeService (#7712)
This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 a55e6a3 Add TimeServiceFactory to refactor TimeService (#7712)
a55e6a3 is described below
commit a55e6a36cbe9475fdf5a0684a679b8820eb533ee
Author: Liang Zhang <te...@163.com>
AuthorDate: Thu Oct 8 19:38:19 2020 +0800
Add TimeServiceFactory to refactor TimeService (#7712)
---
...SPITimeService.java => TimeServiceFactory.java} | 29 ++++-----
.../impl/InsertClauseShardingConditionEngine.java | 7 ++-
.../ConditionValueBetweenOperatorGenerator.java | 4 +-
.../ConditionValueCompareOperatorGenerator.java | 4 +-
.../impl/ConditionValueInOperatorGenerator.java | 5 +-
...rdingsphere.sharding.route.datatime.TimeService | 18 ------
.../route/datatime/SPITimeServiceTest.java | 72 ----------------------
.../route/datatime/fixture/TimeServiceFixture.java | 2 +-
8 files changed, 24 insertions(+), 117 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/datatime/SPITimeService.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/datatime/TimeServiceFactory.java
similarity index 64%
rename from shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/datatime/SPITimeService.java
rename to shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/datatime/TimeServiceFactory.java
index 9a2fa81..a466101 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/datatime/SPITimeService.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/datatime/TimeServiceFactory.java
@@ -17,31 +17,26 @@
package org.apache.shardingsphere.sharding.route.datatime;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
-import java.util.Collection;
-import java.util.Date;
-
/**
- * Time service for SPI.
+ * Time service factory.
*/
-public final class SPITimeService implements TimeService {
-
- private final Collection<TimeService> timeServices = ShardingSphereServiceLoader.newServiceInstances(TimeService.class);
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class TimeServiceFactory {
static {
ShardingSphereServiceLoader.register(TimeService.class);
}
- @Override
- public Date getTime() {
- Date result = null;
- for (TimeService server : timeServices) {
- result = server.getTime();
- if (!(server instanceof DefaultTimeService) && null != result) {
- return result;
- }
- }
- return result;
+ /**
+ * Create new instance of time service.
+ *
+ * @return time service
+ */
+ public static TimeService newInstance() {
+ return ShardingSphereServiceLoader.newServiceInstances(TimeService.class).stream().findFirst().orElseGet(DefaultTimeService::new);
}
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java
index 4317bc9..2f446ce 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java
@@ -20,12 +20,13 @@ package org.apache.shardingsphere.sharding.route.engine.condition.engine.impl;
import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
+import org.apache.shardingsphere.sharding.route.datatime.TimeService;
+import org.apache.shardingsphere.sharding.route.datatime.TimeServiceFactory;
import org.apache.shardingsphere.sharding.route.engine.condition.ExpressionConditionUtils;
import org.apache.shardingsphere.sharding.route.engine.condition.ShardingCondition;
import org.apache.shardingsphere.sharding.route.engine.condition.engine.ShardingConditionEngine;
-import org.apache.shardingsphere.sharding.route.datatime.SPITimeService;
-import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.route.engine.condition.value.ListShardingConditionValue;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
import org.apache.shardingsphere.sql.parser.binder.segment.insert.keygen.GeneratedKeyContext;
import org.apache.shardingsphere.sql.parser.binder.segment.insert.values.InsertValueContext;
@@ -85,7 +86,7 @@ public final class InsertClauseShardingConditionEngine implements ShardingCondit
private ShardingCondition createShardingCondition(final String tableName, final Iterator<String> columnNames, final InsertValueContext insertValueContext, final List<Object> parameters) {
ShardingCondition result = new ShardingCondition();
- SPITimeService timeService = new SPITimeService();
+ TimeService timeService = TimeServiceFactory.newInstance();
for (ExpressionSegment each : insertValueContext.getValueExpressions()) {
String columnName = columnNames.next();
if (shardingRule.isShardingColumn(columnName, tableName)) {
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueBetweenOperatorGenerator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueBetweenOperatorGenerator.java
index 63ba4eb..0bc02ef 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueBetweenOperatorGenerator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueBetweenOperatorGenerator.java
@@ -18,11 +18,11 @@
package org.apache.shardingsphere.sharding.route.engine.condition.generator.impl;
import com.google.common.collect.Range;
+import org.apache.shardingsphere.sharding.route.datatime.TimeServiceFactory;
import org.apache.shardingsphere.sharding.route.engine.condition.Column;
import org.apache.shardingsphere.sharding.route.engine.condition.ExpressionConditionUtils;
import org.apache.shardingsphere.sharding.route.engine.condition.generator.ConditionValue;
import org.apache.shardingsphere.sharding.route.engine.condition.generator.ConditionValueGenerator;
-import org.apache.shardingsphere.sharding.route.datatime.SPITimeService;
import org.apache.shardingsphere.sharding.route.engine.condition.value.RangeShardingConditionValue;
import org.apache.shardingsphere.sharding.route.engine.condition.value.ShardingConditionValue;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BetweenExpression;
@@ -44,7 +44,7 @@ public final class ConditionValueBetweenOperatorGenerator implements ConditionVa
if (betweenConditionValue.isPresent() && andConditionValue.isPresent()) {
return Optional.of(new RangeShardingConditionValue<>(column.getName(), column.getTableName(), SafeNumberOperationUtils.safeClosed(betweenConditionValue.get(), andConditionValue.get())));
}
- Date date = new SPITimeService().getTime();
+ Date date = TimeServiceFactory.newInstance().getTime();
if (!betweenConditionValue.isPresent() && ExpressionConditionUtils.isNowExpression(predicate.getBetweenExpr())) {
betweenConditionValue = Optional.of(date);
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueCompareOperatorGenerator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueCompareOperatorGenerator.java
index 1c76305..210ff3d 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueCompareOperatorGenerator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueCompareOperatorGenerator.java
@@ -19,11 +19,11 @@ package org.apache.shardingsphere.sharding.route.engine.condition.generator.impl
import com.google.common.collect.Lists;
import com.google.common.collect.Range;
+import org.apache.shardingsphere.sharding.route.datatime.TimeServiceFactory;
import org.apache.shardingsphere.sharding.route.engine.condition.Column;
import org.apache.shardingsphere.sharding.route.engine.condition.ExpressionConditionUtils;
import org.apache.shardingsphere.sharding.route.engine.condition.generator.ConditionValue;
import org.apache.shardingsphere.sharding.route.engine.condition.generator.ConditionValueGenerator;
-import org.apache.shardingsphere.sharding.route.datatime.SPITimeService;
import org.apache.shardingsphere.sharding.route.engine.condition.value.ListShardingConditionValue;
import org.apache.shardingsphere.sharding.route.engine.condition.value.RangeShardingConditionValue;
import org.apache.shardingsphere.sharding.route.engine.condition.value.ShardingConditionValue;
@@ -61,7 +61,7 @@ public final class ConditionValueCompareOperatorGenerator implements ConditionVa
return generate(conditionValue.get(), column, operator);
}
if (ExpressionConditionUtils.isNowExpression(predicate.getRight())) {
- return generate(new SPITimeService().getTime(), column, operator);
+ return generate(TimeServiceFactory.newInstance().getTime(), column, operator);
}
return Optional.empty();
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueInOperatorGenerator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueInOperatorGenerator.java
index 4427d04..98e722f 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueInOperatorGenerator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueInOperatorGenerator.java
@@ -17,11 +17,12 @@
package org.apache.shardingsphere.sharding.route.engine.condition.generator.impl;
+import org.apache.shardingsphere.sharding.route.datatime.TimeService;
+import org.apache.shardingsphere.sharding.route.datatime.TimeServiceFactory;
import org.apache.shardingsphere.sharding.route.engine.condition.Column;
import org.apache.shardingsphere.sharding.route.engine.condition.ExpressionConditionUtils;
import org.apache.shardingsphere.sharding.route.engine.condition.generator.ConditionValue;
import org.apache.shardingsphere.sharding.route.engine.condition.generator.ConditionValueGenerator;
-import org.apache.shardingsphere.sharding.route.datatime.SPITimeService;
import org.apache.shardingsphere.sharding.route.engine.condition.value.ListShardingConditionValue;
import org.apache.shardingsphere.sharding.route.engine.condition.value.ShardingConditionValue;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
@@ -39,7 +40,7 @@ public final class ConditionValueInOperatorGenerator implements ConditionValueGe
@Override
public Optional<ShardingConditionValue> generate(final InExpression predicate, final Column column, final List<Object> parameters) {
List<Comparable<?>> shardingConditionValues = new LinkedList<>();
- SPITimeService timeService = new SPITimeService();
+ TimeService timeService = TimeServiceFactory.newInstance();
for (ExpressionSegment each : predicate.getExpressionList()) {
Optional<Comparable<?>> shardingConditionValue = new ConditionValue(each, parameters).getValue();
if (shardingConditionValue.isPresent()) {
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/resources/META-INF/services/org.apache.shardingsphere.sharding.route.datatime.TimeService b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/resources/META-INF/services/org.apache.shardingsphere.sharding.route.datatime.TimeService
deleted file mode 100644
index c1862a6..0000000
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/resources/META-INF/services/org.apache.shardingsphere.sharding.route.datatime.TimeService
+++ /dev/null
@@ -1,18 +0,0 @@
-#
-# 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.
-#
-
-org.apache.shardingsphere.sharding.route.datatime.DefaultTimeService
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/datatime/SPITimeServiceTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/datatime/SPITimeServiceTest.java
deleted file mode 100644
index 666e8c0..0000000
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/datatime/SPITimeServiceTest.java
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * 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.sharding.route.datatime;
-
-import lombok.SneakyThrows;
-import org.apache.shardingsphere.sharding.route.datatime.fixture.TimeServiceFixture;
-import org.junit.Test;
-
-import java.lang.reflect.Field;
-import java.util.Calendar;
-import java.util.Collection;
-import java.util.Date;
-import java.util.Optional;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
-
-public final class SPITimeServiceTest {
-
- private final SPITimeService timeService = new SPITimeService();
-
- @Test
- public void assertGetTime() {
- Optional<TimeServiceFixture> optional = getFixtureHook();
- assertTrue(optional.isPresent());
- Date date = new Date();
- optional.get().setDate(date);
- Date time = timeService.getTime();
- assertThat(date, is(time));
- }
-
- @Test
- public void assertGetTimeWithDefault() {
- Optional<TimeServiceFixture> optional = getFixtureHook();
- assertTrue(optional.isPresent());
- Calendar calendar = Calendar.getInstance();
- calendar.add(Calendar.DATE, -1);
- Date date = calendar.getTime();
- Date time = timeService.getTime();
- assertTrue(time.after(date));
- }
-
- @SuppressWarnings("unchecked")
- @SneakyThrows(ReflectiveOperationException.class)
- private Optional<TimeServiceFixture> getFixtureHook() {
- Field routingHooksField = SPITimeService.class.getDeclaredField("timeServices");
- routingHooksField.setAccessible(true);
- Collection<TimeService> timeServices = (Collection<TimeService>) routingHooksField.get(timeService);
- for (TimeService timeService : timeServices) {
- if (timeService instanceof TimeServiceFixture) {
- return Optional.of((TimeServiceFixture) timeService);
- }
- }
- return Optional.empty();
- }
-}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/datatime/fixture/TimeServiceFixture.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/datatime/fixture/TimeServiceFixture.java
index 42c6da3..bbadd29 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/datatime/fixture/TimeServiceFixture.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/datatime/fixture/TimeServiceFixture.java
@@ -31,6 +31,6 @@ public final class TimeServiceFixture implements TimeService {
@Override
public Date getTime() {
- return date;
+ return null == date ? new Date() : date;
}
}