提交 9252aa9a 编写于 作者: T terrymanu

refactor api 16th version

上级 4bc8853f
......@@ -26,7 +26,7 @@ import com.dangdang.ddframe.rdb.sharding.config.common.api.config.GenerateKeyCol
import com.dangdang.ddframe.rdb.sharding.config.common.api.config.ShardingRuleConfig;
import com.dangdang.ddframe.rdb.sharding.config.common.api.config.StrategyConfig;
import com.dangdang.ddframe.rdb.sharding.config.common.api.config.TableRuleConfig;
import com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm.ClosureDatabaseShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm.ClosureShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.config.common.internal.parser.InlineParser;
import com.dangdang.ddframe.rdb.sharding.keygen.KeyGenerator;
import com.dangdang.ddframe.rdb.sharding.routing.strategy.ShardingAlgorithm;
......@@ -175,7 +175,7 @@ public final class ShardingRuleBuilder {
@SuppressWarnings("unchecked")
private <T extends ShardingStrategy> T buildShardingAlgorithmExpression(final List<String> shardingColumns, final String algorithmExpression) {
return (T) new ComplexShardingStrategy(shardingColumns, new ClosureDatabaseShardingAlgorithm(algorithmExpression, logRoot));
return (T) new ComplexShardingStrategy(shardingColumns, new ClosureShardingAlgorithm(algorithmExpression, logRoot));
}
@SuppressWarnings("unchecked")
......
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed 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.
* </p>
*/
package com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm;
import com.dangdang.ddframe.rdb.sharding.routing.strategy.complex.ComplexKeysShardingAlgorithm;
/**
* Closure for database sharding algorithm.
*
* @author gaohongtao
*/
public class ClosureDatabaseShardingAlgorithm extends ClosureShardingAlgorithm implements ComplexKeysShardingAlgorithm {
public ClosureDatabaseShardingAlgorithm(final String expression, final String logRoot) {
super(expression, logRoot);
}
}
......@@ -41,7 +41,7 @@ import java.util.Set;
*
* @author gaohongtao
*/
public class ClosureShardingAlgorithm implements ComplexKeysShardingAlgorithm {
public final class ClosureShardingAlgorithm implements ComplexKeysShardingAlgorithm {
private final Closure<?> closureTemplate;
......
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed 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.
* </p>
*/
package com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm;
import com.dangdang.ddframe.rdb.sharding.routing.strategy.complex.ComplexKeysShardingAlgorithm;
/**
* Closure for table sharding algorithm.
*
* @author gaohongtao
*/
public class ClosureTableShardingAlgorithm extends ClosureShardingAlgorithm implements ComplexKeysShardingAlgorithm {
public ClosureTableShardingAlgorithm(final String expression, final String logRoot) {
super(expression, logRoot);
}
}
......@@ -18,8 +18,7 @@
package com.dangdang.ddframe.rdb.sharding.config.common;
import com.dangdang.ddframe.rdb.sharding.config.common.api.ShardingRuleBuilderTest;
import com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm.ClosureDatabaseShardingAlgorithmTest;
import com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm.ClosureTableShardingAlgorithmTest;
import com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm.ClosureShardingAlgorithmTest;
import com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm.ShardingValueWrapperTest;
import com.dangdang.ddframe.rdb.sharding.config.common.internal.parser.InlineParserTest;
import org.junit.runner.RunWith;
......@@ -28,8 +27,7 @@ import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
ShardingRuleBuilderTest.class,
ClosureDatabaseShardingAlgorithmTest.class,
ClosureTableShardingAlgorithmTest.class,
ClosureShardingAlgorithmTest.class,
ShardingValueWrapperTest.class,
InlineParserTest.class
})
......
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed 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.
* </p>
*/
package com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm;
public final class ClosureDatabaseShardingAlgorithmTest extends AbstractClosureShardingAlgorithmTest {
@Override
protected ClosureShardingAlgorithm createClosureShardingAlgorithm() {
return new ClosureDatabaseShardingAlgorithm(EXPRESSION, LOG_ROOT);
}
@Override
protected ClosureShardingAlgorithm createErrorClosureShardingAlgorithm() {
return new ClosureDatabaseShardingAlgorithm(WRONG_EXPRESSION, LOG_ROOT);
}
}
......@@ -33,21 +33,17 @@ import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.junit.Assert.assertThat;
public abstract class AbstractClosureShardingAlgorithmTest {
public final class ClosureShardingAlgorithmTest {
protected static final String EXPRESSION = "target_${log.info(id.toString()); id.longValue() % 2}";
private static final String EXPRESSION = "target_${log.info(id.toString()); id.longValue() % 2}";
protected static final String WRONG_EXPRESSION = "target_${log.info(id.error());}";
private static final String WRONG_EXPRESSION = "target_${log.info(id.error());}";
protected static final String LOG_ROOT = "default";
protected abstract ClosureShardingAlgorithm createClosureShardingAlgorithm();
protected abstract ClosureShardingAlgorithm createErrorClosureShardingAlgorithm();
private static final String LOG_ROOT = "default";
@Test
public void assertEqual() {
Collection<String> result = createClosureShardingAlgorithm().doSharding(
Collection<String> result = new ClosureShardingAlgorithm(EXPRESSION, LOG_ROOT).doSharding(
Collections.singletonList("target_1"), Collections.<ShardingValue>singletonList(new ListShardingValue<>("target", "id", Collections.singletonList(1L))));
assertThat(result.size(), is(1));
assertThat(result, hasItem("target_1"));
......@@ -55,7 +51,7 @@ public abstract class AbstractClosureShardingAlgorithmTest {
@Test
public void assertIn() {
Collection<String> result = createClosureShardingAlgorithm().doSharding(Arrays.asList("target_0", "target_1"),
Collection<String> result = new ClosureShardingAlgorithm(EXPRESSION, LOG_ROOT).doSharding(Arrays.asList("target_0", "target_1"),
Collections.<ShardingValue>singletonList(new ListShardingValue<>("target", "id", Arrays.asList(1, 2))));
assertThat(result.size(), is(2));
assertThat(result, hasItem("target_0"));
......@@ -64,13 +60,13 @@ public abstract class AbstractClosureShardingAlgorithmTest {
@Test(expected = UnsupportedOperationException.class)
public void assertBetween() {
createClosureShardingAlgorithm().doSharding(Arrays.asList("target_0", "target_1"),
new ClosureShardingAlgorithm(EXPRESSION, LOG_ROOT).doSharding(Arrays.asList("target_0", "target_1"),
Collections.<ShardingValue>singletonList(new RangeShardingValue<>("target", "id", Range.range(1, BoundType.CLOSED, 2, BoundType.OPEN))));
}
@Test(expected = MissingMethodException.class)
public void assertEvaluateInlineExpressionFailure() {
createErrorClosureShardingAlgorithm().doSharding(
new ClosureShardingAlgorithm(WRONG_EXPRESSION, LOG_ROOT).doSharding(
Collections.singletonList("target_1"), Collections.<ShardingValue>singletonList(new ListShardingValue<>("target", "id", Collections.singletonList(1L))));
}
}
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed 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.
* </p>
*/
package com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm;
public final class ClosureTableShardingAlgorithmTest extends AbstractClosureShardingAlgorithmTest {
@Override
protected ClosureShardingAlgorithm createClosureShardingAlgorithm() {
return new ClosureTableShardingAlgorithm(EXPRESSION, LOG_ROOT);
}
@Override
protected ClosureShardingAlgorithm createErrorClosureShardingAlgorithm() {
return new ClosureTableShardingAlgorithm(WRONG_EXPRESSION, LOG_ROOT);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册