Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
6230b4ed
Shardingsphere
项目概览
apache
/
Shardingsphere
通知
56
Star
3
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Shardingsphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
6230b4ed
编写于
6月 08, 2020
作者:
J
Juan Pan(Trista)
提交者:
GitHub
6月 08, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Create ShardingAutoTableAlgorithm (#5943)
上级
ee8432f7
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
96 addition
and
15 deletion
+96
-15
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/api/sharding/ShardingAutoTableAlgorithm.java
...ere/sharding/api/sharding/ShardingAutoTableAlgorithm.java
+32
-0
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/TableRule.java
...va/org/apache/shardingsphere/sharding/rule/TableRule.java
+28
-9
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/route/ShardingStrategy.java
...rdingsphere/sharding/strategy/route/ShardingStrategy.java
+9
-1
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/route/complex/ComplexShardingStrategy.java
...rding/strategy/route/complex/ComplexShardingStrategy.java
+1
-1
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/route/hint/HintShardingStrategy.java
...re/sharding/strategy/route/hint/HintShardingStrategy.java
+1
-1
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/route/none/NoneShardingStrategy.java
...re/sharding/strategy/route/none/NoneShardingStrategy.java
+7
-1
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/route/standard/StandardShardingStrategy.java
...ing/strategy/route/standard/StandardShardingStrategy.java
+3
-1
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/yaml/config/YamlShardingRuleConfiguration.java
...e/sharding/yaml/config/YamlShardingRuleConfiguration.java
+3
-1
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleConfigurationYamlSwapper.java
...ng/yaml/swapper/ShardingRuleConfigurationYamlSwapper.java
+12
-0
未找到文件。
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/api/sharding/ShardingAutoTableAlgorithm.java
0 → 100644
浏览文件 @
6230b4ed
/*
* 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.api.sharding
;
/**
* Sharding auto table algorithm.
*
*/
public
interface
ShardingAutoTableAlgorithm
{
/**
* Get auto tables amount.
*
* @return the auto tables amount
*/
int
getAutoTablesAmount
();
}
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/TableRule.java
浏览文件 @
6230b4ed
...
...
@@ -17,21 +17,24 @@
package
org.apache.shardingsphere.sharding.rule
;
import
com.google.common.base.Preconditions
;
import
com.google.common.base.Strings
;
import
lombok.AccessLevel
;
import
lombok.Getter
;
import
lombok.ToString
;
import
org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException
;
import
org.apache.shardingsphere.infra.datanode.DataNode
;
import
org.apache.shardingsphere.infra.datanode.DataNodeUtil
;
import
org.apache.shardingsphere.infra.exception.ShardingSphereException
;
import
org.apache.shardingsphere.sharding.api.config.KeyGeneratorConfiguration
;
import
org.apache.shardingsphere.sharding.api.config.ShardingAutoTableRuleConfiguration
;
import
org.apache.shardingsphere.sharding.api.config.ShardingTableRuleConfiguration
;
import
org.apache.shardingsphere.sharding.api.sharding.ShardingAutoTableAlgorithm
;
import
org.apache.shardingsphere.sharding.spi.keygen.KeyGenerateAlgorithm
;
import
org.apache.shardingsphere.sharding.strategy.algorithm.sharding.inline.InlineExpressionParser
;
import
org.apache.shardingsphere.sharding.strategy.route.ShardingStrategy
;
import
org.apache.shardingsphere.sharding.strategy.route.ShardingStrategyFactory
;
import
org.apache.shardingsphere.sharding.strategy.route.none.NoneShardingStrategy
;
import
org.apache.shardingsphere.sharding.spi.keygen.KeyGenerateAlgorithm
;
import
org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException
;
import
org.apache.shardingsphere.infra.datanode.DataNode
;
import
org.apache.shardingsphere.infra.datanode.DataNodeUtil
;
import
org.apache.shardingsphere.infra.exception.ShardingSphereException
;
import
java.util.Collection
;
import
java.util.Collections
;
...
...
@@ -95,10 +98,26 @@ public final class TableRule {
tableShardingStrategy
=
null
==
tableRuleConfig
.
getTableShardingStrategy
()
?
null
:
ShardingStrategyFactory
.
newInstance
(
tableRuleConfig
.
getTableShardingStrategy
());
final
KeyGeneratorConfiguration
keyGeneratorConfiguration
=
tableRuleConfig
.
getKeyGenerator
();
generateKeyColumn
=
null
!=
keyGeneratorConfiguration
&&
!
Strings
.
isNullOrEmpty
(
keyGeneratorConfiguration
.
getColumn
())
?
keyGeneratorConfiguration
.
getColumn
()
:
defaultGenerateKeyColumn
;
keyGenerateAlgorithm
=
containsKeyGenerateAlgorithm
(
tableRuleConfig
)
?
tableRuleConfig
.
getKeyGenerator
().
getKeyGenerateAlgorithm
()
:
null
;
keyGenerateAlgorithm
=
containsKeyGenerateAlgorithm
(
tableRuleConfig
.
getKeyGenerator
()
)
?
tableRuleConfig
.
getKeyGenerator
().
getKeyGenerateAlgorithm
()
:
null
;
checkRule
(
dataNodes
);
}
public
TableRule
(
final
ShardingAutoTableRuleConfiguration
tableRuleConfig
,
final
Collection
<
String
>
dataSourceNames
,
final
String
defaultGenerateKeyColumn
)
{
Preconditions
.
checkArgument
(
null
==
tableRuleConfig
.
getShardingStrategy
()
||
tableRuleConfig
.
getShardingStrategy
()
instanceof
ShardingAutoTableAlgorithm
,
"ShardingAutoTableAlgorithm is required."
);
logicTable
=
tableRuleConfig
.
getLogicTable
().
toLowerCase
();
tableShardingStrategy
=
null
==
tableRuleConfig
.
getShardingStrategy
()
?
null
:
ShardingStrategyFactory
.
newInstance
(
tableRuleConfig
.
getShardingStrategy
());
List
<
String
>
dataNodes
=
null
;
dataNodeIndexMap
=
new
HashMap
<>(
dataNodes
.
size
(),
1
);
actualDataNodes
=
isEmptyDataNodes
(
dataNodes
)
?
generateDataNodes
(
tableRuleConfig
.
getLogicTable
(),
dataSourceNames
)
:
generateDataNodes
(
dataNodes
,
dataSourceNames
);
actualTables
=
getActualTables
();
databaseShardingStrategy
=
null
;
final
KeyGeneratorConfiguration
keyGeneratorConfiguration
=
tableRuleConfig
.
getKeyGenerator
();
generateKeyColumn
=
null
!=
keyGeneratorConfiguration
&&
!
Strings
.
isNullOrEmpty
(
keyGeneratorConfiguration
.
getColumn
())
?
keyGeneratorConfiguration
.
getColumn
()
:
defaultGenerateKeyColumn
;
keyGenerateAlgorithm
=
containsKeyGenerateAlgorithm
(
tableRuleConfig
.
getKeyGenerator
())
?
tableRuleConfig
.
getKeyGenerator
().
getKeyGenerateAlgorithm
()
:
null
;
checkRule
(
dataNodes
);
}
private
Set
<
String
>
getActualTables
()
{
return
actualDataNodes
.
stream
().
map
(
DataNode:
:
getTableName
).
collect
(
Collectors
.
toSet
());
}
...
...
@@ -107,8 +126,8 @@ public final class TableRule {
datasourceToTablesMap
.
computeIfAbsent
(
datasourceName
,
key
->
new
LinkedHashSet
<>()).
add
(
tableName
);
}
private
boolean
containsKeyGenerateAlgorithm
(
final
ShardingTableRuleConfiguration
shardingTableRuleConfiguration
)
{
return
null
!=
shardingTableRuleConfiguration
.
getKeyGenerator
()
&&
null
!=
shardingTableRuleConfiguration
.
getKeyGenerator
()
.
getKeyGenerateAlgorithm
();
private
boolean
containsKeyGenerateAlgorithm
(
final
KeyGeneratorConfiguration
keyGenerator
)
{
return
null
!=
keyGenerator
&&
null
!=
keyGenerator
.
getKeyGenerateAlgorithm
();
}
private
boolean
isEmptyDataNodes
(
final
List
<
String
>
dataNodes
)
{
...
...
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/route/ShardingStrategy.java
浏览文件 @
6230b4ed
...
...
@@ -17,8 +17,9 @@
package
org.apache.shardingsphere.sharding.strategy.route
;
import
org.apache.shardingsphere.sharding.strategy.route.value.RouteValue
;
import
org.apache.shardingsphere.infra.config.properties.ConfigurationProperties
;
import
org.apache.shardingsphere.sharding.spi.algorithm.ShardingAlgorithm
;
import
org.apache.shardingsphere.sharding.strategy.route.value.RouteValue
;
import
java.util.Collection
;
...
...
@@ -33,6 +34,13 @@ public interface ShardingStrategy {
* @return sharding columns
*/
Collection
<
String
>
getShardingColumns
();
/**
* Get sharding algorithm.
*
* @return sharding algorithm
*/
ShardingAlgorithm
getShardingAlgorithm
();
/**
* Sharding.
...
...
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/route/complex/ComplexShardingStrategy.java
浏览文件 @
6230b4ed
...
...
@@ -38,9 +38,9 @@ import java.util.TreeSet;
/**
* Complex sharding strategy.
*/
@Getter
public
final
class
ComplexShardingStrategy
implements
ShardingStrategy
{
@Getter
private
final
Collection
<
String
>
shardingColumns
;
private
final
ComplexKeysShardingAlgorithm
shardingAlgorithm
;
...
...
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/route/hint/HintShardingStrategy.java
浏览文件 @
6230b4ed
...
...
@@ -33,9 +33,9 @@ import java.util.TreeSet;
/**
* Hint sharding strategy.
*/
@Getter
public
final
class
HintShardingStrategy
implements
ShardingStrategy
{
@Getter
private
final
Collection
<
String
>
shardingColumns
;
private
final
HintShardingAlgorithm
shardingAlgorithm
;
...
...
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/route/none/NoneShardingStrategy.java
浏览文件 @
6230b4ed
...
...
@@ -18,6 +18,7 @@
package
org.apache.shardingsphere.sharding.strategy.route.none
;
import
lombok.Getter
;
import
org.apache.shardingsphere.sharding.spi.algorithm.ShardingAlgorithm
;
import
org.apache.shardingsphere.sharding.strategy.route.ShardingStrategy
;
import
org.apache.shardingsphere.sharding.strategy.route.value.RouteValue
;
import
org.apache.shardingsphere.infra.config.properties.ConfigurationProperties
;
...
...
@@ -32,7 +33,12 @@ import java.util.Collections;
public
final
class
NoneShardingStrategy
implements
ShardingStrategy
{
private
final
Collection
<
String
>
shardingColumns
=
Collections
.
emptyList
();
@Override
public
ShardingAlgorithm
getShardingAlgorithm
()
{
return
null
;
}
@Override
public
Collection
<
String
>
doSharding
(
final
Collection
<
String
>
availableTargetNames
,
final
Collection
<
RouteValue
>
shardingValues
,
final
ConfigurationProperties
properties
)
{
return
availableTargetNames
;
...
...
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/strategy/route/standard/StandardShardingStrategy.java
浏览文件 @
6230b4ed
...
...
@@ -18,6 +18,7 @@
package
org.apache.shardingsphere.sharding.strategy.route.standard
;
import
com.google.common.base.Preconditions
;
import
lombok.Getter
;
import
org.apache.shardingsphere.sharding.api.config.strategy.StandardShardingStrategyConfiguration
;
import
org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue
;
import
org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue
;
...
...
@@ -38,7 +39,8 @@ import java.util.TreeSet;
public
final
class
StandardShardingStrategy
implements
ShardingStrategy
{
private
final
String
shardingColumn
;
@Getter
private
final
StandardShardingAlgorithm
shardingAlgorithm
;
public
StandardShardingStrategy
(
final
StandardShardingStrategyConfiguration
standardShardingStrategyConfig
)
{
...
...
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/yaml/config/YamlShardingRuleConfiguration.java
浏览文件 @
6230b4ed
...
...
@@ -35,7 +35,9 @@ import java.util.Map;
public
final
class
YamlShardingRuleConfiguration
implements
YamlRuleConfiguration
{
private
Map
<
String
,
YamlTableRuleConfiguration
>
tables
=
new
LinkedHashMap
<>();
private
Map
<
String
,
YamlShardingAutoTableRuleConfiguration
>
autoTables
=
new
LinkedHashMap
<>();
private
Collection
<
String
>
bindingTables
=
new
ArrayList
<>();
private
Collection
<
String
>
broadcastTables
=
new
ArrayList
<>();
...
...
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleConfigurationYamlSwapper.java
浏览文件 @
6230b4ed
...
...
@@ -18,9 +18,11 @@
package
org.apache.shardingsphere.sharding.yaml.swapper
;
import
org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
;
import
org.apache.shardingsphere.sharding.api.config.ShardingAutoTableRuleConfiguration
;
import
org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration
;
import
org.apache.shardingsphere.sharding.api.config.ShardingTableRuleConfiguration
;
import
org.apache.shardingsphere.sharding.constant.ShardingOrder
;
import
org.apache.shardingsphere.sharding.yaml.config.YamlShardingAutoTableRuleConfiguration
;
import
org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration
;
import
org.apache.shardingsphere.sharding.yaml.config.YamlTableRuleConfiguration
;
...
...
@@ -32,6 +34,8 @@ import java.util.Map.Entry;
public
final
class
ShardingRuleConfigurationYamlSwapper
implements
YamlRuleConfigurationSwapper
<
YamlShardingRuleConfiguration
,
ShardingRuleConfiguration
>
{
private
final
TableRuleConfigurationYamlSwapper
tableRuleConfigurationYamlSwapper
=
new
TableRuleConfigurationYamlSwapper
();
private
final
ShardingAutoTableRuleConfigurationYamlSwapper
autoTableRuleConfigurationYamlSwapper
=
new
ShardingAutoTableRuleConfigurationYamlSwapper
();
private
final
ShardingStrategyConfigurationYamlSwapper
shardingStrategyConfigurationYamlSwapper
=
new
ShardingStrategyConfigurationYamlSwapper
();
...
...
@@ -43,6 +47,9 @@ public final class ShardingRuleConfigurationYamlSwapper implements YamlRuleConfi
for
(
ShardingTableRuleConfiguration
each
:
data
.
getTables
())
{
result
.
getTables
().
put
(
each
.
getLogicTable
(),
tableRuleConfigurationYamlSwapper
.
swap
(
each
));
}
for
(
ShardingAutoTableRuleConfiguration
each
:
data
.
getAutoTables
())
{
result
.
getAutoTables
().
put
(
each
.
getLogicTable
(),
autoTableRuleConfigurationYamlSwapper
.
swap
(
each
));
}
result
.
getBindingTables
().
addAll
(
data
.
getBindingTableGroups
());
result
.
getBroadcastTables
().
addAll
(
data
.
getBroadcastTables
());
if
(
null
!=
data
.
getDefaultDatabaseShardingStrategy
())
{
...
...
@@ -65,6 +72,11 @@ public final class ShardingRuleConfigurationYamlSwapper implements YamlRuleConfi
tableRuleConfig
.
setLogicTable
(
entry
.
getKey
());
result
.
getTables
().
add
(
tableRuleConfigurationYamlSwapper
.
swap
(
tableRuleConfig
));
}
for
(
Entry
<
String
,
YamlShardingAutoTableRuleConfiguration
>
entry
:
yamlConfiguration
.
getAutoTables
().
entrySet
())
{
YamlShardingAutoTableRuleConfiguration
tableRuleConfig
=
entry
.
getValue
();
tableRuleConfig
.
setLogicTable
(
entry
.
getKey
());
result
.
getAutoTables
().
add
(
autoTableRuleConfigurationYamlSwapper
.
swap
(
tableRuleConfig
));
}
result
.
getBindingTableGroups
().
addAll
(
yamlConfiguration
.
getBindingTables
());
result
.
getBroadcastTables
().
addAll
(
yamlConfiguration
.
getBroadcastTables
());
if
(
null
!=
yamlConfiguration
.
getDefaultDatabaseStrategy
())
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录