Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
4a016561
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,发现更多精彩内容 >>
未验证
提交
4a016561
编写于
8月 13, 2020
作者:
J
Juan Pan(Trista)
提交者:
GitHub
8月 13, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor CreateShardingRuleStatementContext (#6828)
上级
5ad256e4
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
120 addition
and
83 deletion
+120
-83
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/convert/CreateShardingRuleStatementContextConverter.java
.../convert/CreateShardingRuleStatementContextConverter.java
+25
-19
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/convert/CreateShardingRuleStatementContextConverterTest.java
...vert/CreateShardingRuleStatementContextConverterTest.java
+15
-14
shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/RDLExecuteEngineTest.java
...kend/communication/jdbc/execute/RDLExecuteEngineTest.java
+8
-10
shardingsphere-rdl-parser/shardingsphere-rdl-parser-binder/src/main/java/org/apache/shardingsphere/rdl/parser/binder/context/CreateShardingRuleStatementContext.java
...er/binder/context/CreateShardingRuleStatementContext.java
+5
-40
shardingsphere-rdl-parser/shardingsphere-rdl-parser-binder/src/main/java/org/apache/shardingsphere/rdl/parser/binder/util/ShardingAlgorithmPropertiesUtil.java
...l/parser/binder/util/ShardingAlgorithmPropertiesUtil.java
+67
-0
未找到文件。
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/convert/CreateShardingRuleStatementContextConverter.java
浏览文件 @
4a016561
...
...
@@ -21,11 +21,14 @@ import com.google.common.base.Joiner;
import
org.apache.shardingsphere.infra.yaml.config.algorithm.YamlShardingSphereAlgorithmConfiguration
;
import
org.apache.shardingsphere.rdl.parser.binder.context.CreateShardingRuleStatementContext
;
import
org.apache.shardingsphere.rdl.parser.binder.generator.SQLStatementContextConverter
;
import
org.apache.shardingsphere.rdl.parser.statement.rdl.TableRuleSegment
;
import
org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration
;
import
org.apache.shardingsphere.sharding.yaml.config.rule.YamlShardingAutoTableRuleConfiguration
;
import
org.apache.shardingsphere.sharding.yaml.config.strategy.sharding.YamlShardingStrategyConfiguration
;
import
org.apache.shardingsphere.sharding.yaml.config.strategy.sharding.YamlStandardShardingStrategyConfiguration
;
import
java.util.Properties
;
/**
* Create sharding rule statement context converter.
*/
...
...
@@ -34,33 +37,36 @@ public final class CreateShardingRuleStatementContextConverter implements SQLSta
@Override
public
YamlShardingRuleConfiguration
convert
(
final
CreateShardingRuleStatementContext
context
)
{
YamlShardingRuleConfiguration
result
=
new
YamlShardingRuleConfiguration
();
addYamlShardingSphereAlgorithmConfiguration
(
context
,
result
);
addYamlShardingAutoTableRuleConfiguration
(
context
,
result
);
for
(
TableRuleSegment
each
:
context
.
getSqlStatement
().
getTables
())
{
result
.
getShardingAlgorithms
().
put
(
getAlgorithmName
(
each
.
getLogicTable
(),
each
.
getAlgorithmType
()),
createAlgorithmConfiguration
(
each
,
context
.
getAlgorithmProperties
(
each
)));
result
.
getAutoTables
().
put
(
each
.
getLogicTable
(),
createAutoTableRuleConfiguration
(
each
));
}
return
result
;
}
private
void
addYamlShardingSphereAlgorithmConfiguration
(
final
CreateShardingRuleStatementContext
context
,
final
YamlShardingRuleConfiguration
ruleConfiguration
)
{
YamlShardingSphereAlgorithmConfiguration
algorithmConfiguration
=
new
YamlShardingSphereAlgorithmConfiguration
();
algorithmConfiguration
.
setType
(
contex
t
.
getAlgorithmType
());
algorithmConfiguration
.
setProps
(
context
.
getAlgorithmProperties
()
);
r
uleConfiguration
.
getShardingAlgorithms
().
put
(
getAlgorithmName
(
context
.
getLogicTable
(),
context
.
getAlgorithmType
()),
algorithmConfiguration
)
;
private
YamlShardingSphereAlgorithmConfiguration
createAlgorithmConfiguration
(
final
TableRuleSegment
segment
,
final
Properties
properties
)
{
YamlShardingSphereAlgorithmConfiguration
result
=
new
YamlShardingSphereAlgorithmConfiguration
();
result
.
setType
(
segmen
t
.
getAlgorithmType
());
result
.
setProps
(
properties
);
r
eturn
result
;
}
private
void
addYamlShardingAutoTableRuleConfiguration
(
final
CreateShardingRuleStatementContext
context
,
final
YamlShardingRuleConfiguration
ruleConfiguration
)
{
YamlShardingAutoTableRuleConfiguration
tableRuleConfiguration
=
new
YamlShardingAutoTableRuleConfiguration
();
tableRuleConfiguration
.
setLogicTable
(
contex
t
.
getLogicTable
());
tableRuleConfiguration
.
setActualDataSources
(
Joiner
.
on
(
","
).
join
(
contex
t
.
getDataSources
()));
tableRuleConfiguration
.
setShardingStrategy
(
createYamlShardingStrategyConfiguration
(
contex
t
));
r
uleConfiguration
.
getAutoTables
().
put
(
context
.
getLogicTable
(),
tableRuleConfiguration
)
;
private
YamlShardingAutoTableRuleConfiguration
createAutoTableRuleConfiguration
(
final
TableRuleSegment
segment
)
{
YamlShardingAutoTableRuleConfiguration
result
=
new
YamlShardingAutoTableRuleConfiguration
();
result
.
setLogicTable
(
segmen
t
.
getLogicTable
());
result
.
setActualDataSources
(
Joiner
.
on
(
","
).
join
(
segmen
t
.
getDataSources
()));
result
.
setShardingStrategy
(
createStrategyConfiguration
(
segmen
t
));
r
eturn
result
;
}
private
YamlShardingStrategyConfiguration
create
YamlShardingStrategyConfiguration
(
final
CreateShardingRuleStatementContext
contex
t
)
{
YamlShardingStrategyConfiguration
strategy
=
new
YamlShardingStrategyConfiguration
();
private
YamlShardingStrategyConfiguration
create
StrategyConfiguration
(
final
TableRuleSegment
segmen
t
)
{
YamlShardingStrategyConfiguration
result
=
new
YamlShardingStrategyConfiguration
();
YamlStandardShardingStrategyConfiguration
standard
=
new
YamlStandardShardingStrategyConfiguration
();
standard
.
setShardingColumn
(
contex
t
.
getShardingColumn
());
standard
.
setShardingAlgorithmName
(
getAlgorithmName
(
context
.
getLogicTable
(),
contex
t
.
getAlgorithmType
()));
strategy
.
setStandard
(
standard
);
return
strategy
;
standard
.
setShardingColumn
(
segmen
t
.
getShardingColumn
());
standard
.
setShardingAlgorithmName
(
getAlgorithmName
(
segment
.
getLogicTable
(),
segmen
t
.
getAlgorithmType
()));
result
.
setStandard
(
standard
);
return
result
;
}
private
String
getAlgorithmName
(
final
String
tableName
,
final
String
algorithmType
)
{
...
...
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/convert/CreateShardingRuleStatementContextConverterTest.java
浏览文件 @
4a016561
...
...
@@ -18,33 +18,34 @@
package
org.apache.shardingsphere.sharding.convert
;
import
org.apache.shardingsphere.rdl.parser.binder.context.CreateShardingRuleStatementContext
;
import
org.apache.shardingsphere.rdl.parser.statement.rdl.CreateShardingRuleStatement
;
import
org.apache.shardingsphere.rdl.parser.statement.rdl.TableRuleSegment
;
import
org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration
;
import
org.junit.Before
;
import
org.junit.Test
;
import
java.util.Arrays
;
import
java.util.
Propertie
s
;
import
java.util.
Collection
s
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
public
class
CreateShardingRuleStatementContextConverterTest
{
private
TableRuleSegment
segment
;
private
CreateShardingRuleStatementContext
context
;
@Before
public
void
setUp
()
{
context
=
mock
(
CreateShardingRuleStatementContext
.
class
);
when
(
context
.
getLogicTable
()).
thenReturn
(
"t_order"
);
when
(
context
.
getDataSources
()).
thenReturn
(
Arrays
.
asList
(
"ds0"
,
"ds1"
));
when
(
context
.
getShardingColumn
()).
thenReturn
(
"order_id"
);
when
(
context
.
getAlgorithmType
()).
thenReturn
(
"MOD"
);
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
"sharding.count"
,
"2"
);
when
(
context
.
getAlgorithmProperties
()).
thenReturn
(
properties
);
segment
=
new
TableRuleSegment
();
segment
.
setLogicTable
(
"t_order"
);
segment
.
setDataSources
(
Arrays
.
asList
(
"ds0"
,
"ds1"
));
segment
.
setShardingColumn
(
"order_id"
);
segment
.
setAlgorithmType
(
"MOD"
);
segment
.
setProperties
(
Collections
.
singleton
(
"2"
));
context
=
new
CreateShardingRuleStatementContext
(
new
CreateShardingRuleStatement
(
Collections
.
singleton
(
segment
)));
}
@Test
...
...
@@ -52,9 +53,9 @@ public class CreateShardingRuleStatementContextConverterTest {
YamlShardingRuleConfiguration
rule
=
new
CreateShardingRuleStatementContextConverter
().
convert
(
context
);
assertTrue
(
rule
.
getTables
().
isEmpty
());
assertThat
(
rule
.
getAutoTables
().
size
(),
is
(
1
));
assertThat
(
rule
.
getAutoTables
().
get
(
contex
t
.
getLogicTable
()).
getActualDataSources
(),
is
(
"ds0,ds1"
));
assertThat
(
rule
.
getAutoTables
().
get
(
contex
t
.
getLogicTable
()).
getShardingStrategy
().
getStandard
().
getShardingColumn
(),
is
(
"order_id"
));
assertThat
(
rule
.
getAutoTables
().
get
(
contex
t
.
getLogicTable
()).
getShardingStrategy
().
getStandard
().
getShardingAlgorithmName
(),
is
(
"t_order_MOD"
));
assertThat
(
rule
.
getAutoTables
().
get
(
segmen
t
.
getLogicTable
()).
getActualDataSources
(),
is
(
"ds0,ds1"
));
assertThat
(
rule
.
getAutoTables
().
get
(
segmen
t
.
getLogicTable
()).
getShardingStrategy
().
getStandard
().
getShardingColumn
(),
is
(
"order_id"
));
assertThat
(
rule
.
getAutoTables
().
get
(
segmen
t
.
getLogicTable
()).
getShardingStrategy
().
getStandard
().
getShardingAlgorithmName
(),
is
(
"t_order_MOD"
));
assertTrue
(
rule
.
getShardingAlgorithms
().
containsKey
(
"t_order_MOD"
));
assertThat
(
rule
.
getShardingAlgorithms
().
get
(
"t_order_MOD"
).
getType
(),
is
(
"MOD"
));
}
...
...
shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/RDLExecuteEngineTest.java
浏览文件 @
4a016561
...
...
@@ -31,6 +31,7 @@ import org.apache.shardingsphere.rdl.parser.binder.context.CreateDataSourcesStat
import
org.apache.shardingsphere.rdl.parser.binder.context.CreateShardingRuleStatementContext
;
import
org.apache.shardingsphere.rdl.parser.statement.rdl.CreateDataSourcesStatement
;
import
org.apache.shardingsphere.rdl.parser.statement.rdl.CreateShardingRuleStatement
;
import
org.apache.shardingsphere.rdl.parser.statement.rdl.TableRuleSegment
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
...
...
@@ -39,12 +40,10 @@ import java.lang.reflect.Field;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.LinkedList
;
import
java.util.Properties
;
import
static
org
.
hamcrest
.
CoreMatchers
.
instanceOf
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
public
final
class
RDLExecuteEngineTest
{
...
...
@@ -63,14 +62,13 @@ public final class RDLExecuteEngineTest {
}
private
void
createRuleContext
()
{
ruleContext
=
mock
(
CreateShardingRuleStatementContext
.
class
);
when
(
ruleContext
.
getLogicTable
()).
thenReturn
(
"t_order"
);
when
(
ruleContext
.
getDataSources
()).
thenReturn
(
Arrays
.
asList
(
"ds0"
,
"ds1"
));
when
(
ruleContext
.
getShardingColumn
()).
thenReturn
(
"order_id"
);
when
(
ruleContext
.
getAlgorithmType
()).
thenReturn
(
"MOD"
);
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
"sharding.count"
,
"2"
);
when
(
ruleContext
.
getAlgorithmProperties
()).
thenReturn
(
properties
);
TableRuleSegment
segment
=
new
TableRuleSegment
();
segment
.
setLogicTable
(
"t_order"
);
segment
.
setDataSources
(
Arrays
.
asList
(
"ds0"
,
"ds1"
));
segment
.
setShardingColumn
(
"order_id"
);
segment
.
setAlgorithmType
(
"MOD"
);
segment
.
setProperties
(
Collections
.
singleton
(
"2"
));
ruleContext
=
new
CreateShardingRuleStatementContext
(
new
CreateShardingRuleStatement
(
Collections
.
singleton
(
segment
)));
}
@Test
...
...
shardingsphere-rdl-parser/shardingsphere-rdl-parser-binder/src/main/java/org/apache/shardingsphere/rdl/parser/binder/context/CreateShardingRuleStatementContext.java
浏览文件 @
4a016561
...
...
@@ -17,11 +17,11 @@
package
org.apache.shardingsphere.rdl.parser.binder.context
;
import
org.apache.shardingsphere.rdl.parser.binder.util.ShardingAlgorithmPropertiesUtil
;
import
org.apache.shardingsphere.rdl.parser.statement.rdl.CreateShardingRuleStatement
;
import
org.apache.shardingsphere.rdl.parser.statement.rdl.TableRuleSegment
;
import
org.apache.shardingsphere.sql.parser.binder.statement.CommonSQLStatementContext
;
import
java.util.Collection
;
import
java.util.LinkedList
;
import
java.util.Properties
;
/**
...
...
@@ -33,48 +33,13 @@ public final class CreateShardingRuleStatementContext extends CommonSQLStatement
super
(
sqlStatement
);
}
/**
* Get logic table.
*
* @return logic table
*/
public
String
getLogicTable
()
{
return
""
;
}
/**
* Get data sources.
*
* @return data sources
*/
public
Collection
<
String
>
getDataSources
()
{
return
new
LinkedList
<>();
}
/**
* Get sharding column.
*
* @return sharding column
*/
public
String
getShardingColumn
()
{
return
""
;
}
/**
* Get sharding algorithm type.
*
* @return sharding algorithm type
*/
public
String
getAlgorithmType
()
{
return
""
;
}
/**
* Get algorithm properties.
*
* @param segment segment
* @return algorithm properties
*/
public
Properties
getAlgorithmProperties
()
{
return
new
Properties
(
);
public
Properties
getAlgorithmProperties
(
final
TableRuleSegment
segment
)
{
return
ShardingAlgorithmPropertiesUtil
.
getProperties
(
segment
.
getAlgorithmType
(),
segment
.
getProperties
()
);
}
}
shardingsphere-rdl-parser/shardingsphere-rdl-parser-binder/src/main/java/org/apache/shardingsphere/rdl/parser/binder/util/ShardingAlgorithmPropertiesUtil.java
0 → 100644
浏览文件 @
4a016561
/*
* 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.rdl.parser.binder.util
;
import
com.google.common.base.Preconditions
;
import
lombok.AccessLevel
;
import
lombok.NoArgsConstructor
;
import
java.util.Arrays
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.Iterator
;
import
java.util.LinkedHashMap
;
import
java.util.Map
;
import
java.util.Properties
;
/**
* Sharding algorithm properties util.
*/
@NoArgsConstructor
(
access
=
AccessLevel
.
PRIVATE
)
public
final
class
ShardingAlgorithmPropertiesUtil
{
public
static
final
Map
<
String
,
Collection
<
String
>>
TYPE_AND_PROPERTIES
;
static
{
TYPE_AND_PROPERTIES
=
new
LinkedHashMap
<>();
TYPE_AND_PROPERTIES
.
put
(
"MOD"
,
Collections
.
singleton
(
"sharding.count"
));
TYPE_AND_PROPERTIES
.
put
(
"HASH_MOD"
,
Collections
.
singleton
(
"sharding.count"
));
TYPE_AND_PROPERTIES
.
put
(
"VOLUME_RANGE"
,
Arrays
.
asList
(
"range.lower"
,
"range.upper"
,
"sharding.volume"
));
TYPE_AND_PROPERTIES
.
put
(
"BOUNDARY_RANGE"
,
Collections
.
singleton
(
"sharding.ranges"
));
}
/**
* Get properties.
*
* @param shardingAlgorithmType sharding algorithm type
* @param properties properties
* @return properties
*/
public
static
Properties
getProperties
(
final
String
shardingAlgorithmType
,
final
Collection
<
String
>
properties
)
{
Preconditions
.
checkArgument
(
TYPE_AND_PROPERTIES
.
containsKey
(
shardingAlgorithmType
),
"Bad sharding algorithm type: %s."
,
shardingAlgorithmType
);
Preconditions
.
checkArgument
(
TYPE_AND_PROPERTIES
.
get
(
shardingAlgorithmType
).
size
()
==
properties
.
size
(),
"%s needs %d properties, but %s properties are given."
,
shardingAlgorithmType
,
TYPE_AND_PROPERTIES
.
get
(
shardingAlgorithmType
).
size
(),
properties
.
size
());
Properties
result
=
new
Properties
();
Iterator
<
String
>
keys
=
TYPE_AND_PROPERTIES
.
get
(
shardingAlgorithmType
).
iterator
();
Iterator
<
String
>
values
=
properties
.
iterator
();
while
(
keys
.
hasNext
())
{
result
.
setProperty
(
keys
.
next
(),
values
.
next
());
}
return
result
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录