Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
0fd8e9df
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,发现更多精彩内容 >>
提交
0fd8e9df
编写于
4月 15, 2018
作者:
T
terrymanu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
revise #727: split OptimizeEngine to InsertOptimizeEngine and QueryOptimizeEngine
上级
d5a6d96d
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
401 addition
and
172 deletion
+401
-172
sharding-core/src/main/java/io/shardingjdbc/core/optimizer/InsertOptimizeEngine.java
.../io/shardingjdbc/core/optimizer/InsertOptimizeEngine.java
+73
-0
sharding-core/src/main/java/io/shardingjdbc/core/optimizer/OptimizeEngine.java
...n/java/io/shardingjdbc/core/optimizer/OptimizeEngine.java
+2
-117
sharding-core/src/main/java/io/shardingjdbc/core/optimizer/OptimizeEngineFactory.java
...io/shardingjdbc/core/optimizer/OptimizeEngineFactory.java
+56
-0
sharding-core/src/main/java/io/shardingjdbc/core/optimizer/QueryOptimizeEngine.java
...a/io/shardingjdbc/core/optimizer/QueryOptimizeEngine.java
+129
-0
sharding-core/src/main/java/io/shardingjdbc/core/parsing/parser/context/condition/GeneratedKeyCondition.java
...rsing/parser/context/condition/GeneratedKeyCondition.java
+9
-0
sharding-core/src/main/java/io/shardingjdbc/core/routing/router/ParsingSQLRouter.java
...io/shardingjdbc/core/routing/router/ParsingSQLRouter.java
+2
-2
sharding-core/src/main/java/io/shardingjdbc/core/routing/sharding/GeneratedKey.java
...a/io/shardingjdbc/core/routing/sharding/GeneratedKey.java
+0
-15
sharding-core/src/main/java/io/shardingjdbc/core/routing/sharding/ShardingConditions.java
...hardingjdbc/core/routing/sharding/ShardingConditions.java
+0
-5
sharding-core/src/test/java/io/shardingjdbc/core/optimizer/AllOptimizerTests.java
...ava/io/shardingjdbc/core/optimizer/AllOptimizerTests.java
+4
-1
sharding-core/src/test/java/io/shardingjdbc/core/optimizer/InsertOptimizeEngineTest.java
...shardingjdbc/core/optimizer/InsertOptimizeEngineTest.java
+107
-0
sharding-core/src/test/java/io/shardingjdbc/core/optimizer/QueryOptimizeEngineTest.java
.../shardingjdbc/core/optimizer/QueryOptimizeEngineTest.java
+19
-32
未找到文件。
sharding-core/src/main/java/io/shardingjdbc/core/optimizer/InsertOptimizeEngine.java
0 → 100644
浏览文件 @
0fd8e9df
/*
* 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
io.shardingjdbc.core.optimizer
;
import
io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue
;
import
io.shardingjdbc.core.parsing.parser.context.condition.AndCondition
;
import
io.shardingjdbc.core.parsing.parser.context.condition.Condition
;
import
io.shardingjdbc.core.parsing.parser.context.condition.GeneratedKeyCondition
;
import
io.shardingjdbc.core.parsing.parser.context.condition.OrCondition
;
import
io.shardingjdbc.core.routing.sharding.GeneratedKey
;
import
io.shardingjdbc.core.routing.sharding.ShardingCondition
;
import
io.shardingjdbc.core.routing.sharding.ShardingConditions
;
import
lombok.RequiredArgsConstructor
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.LinkedList
;
import
java.util.List
;
/**
* Insert optimize engine.
*
* @author zhangliang
*/
@RequiredArgsConstructor
public
final
class
InsertOptimizeEngine
implements
OptimizeEngine
{
private
final
OrCondition
orCondition
;
private
final
List
<
Object
>
parameters
;
private
final
GeneratedKey
generatedKey
;
@Override
public
ShardingConditions
optimize
()
{
ShardingCondition
result
=
new
ShardingCondition
();
for
(
AndCondition
each
:
orCondition
.
getAndConditions
())
{
result
.
getShardingValues
().
addAll
(
getShardingCondition
(
each
));
}
if
(
null
!=
generatedKey
)
{
result
.
getShardingValues
().
add
(
getShardingCondition
(
generatedKey
));
}
return
new
ShardingConditions
(
result
.
getShardingValues
().
isEmpty
()
?
Collections
.<
ShardingCondition
>
emptyList
()
:
Collections
.
singletonList
(
result
));
}
private
Collection
<
ListShardingValue
>
getShardingCondition
(
final
AndCondition
andCondition
)
{
Collection
<
ListShardingValue
>
result
=
new
LinkedList
<>();
for
(
Condition
each
:
andCondition
.
getConditions
())
{
result
.
add
(
new
ListShardingValue
<>(
each
.
getColumn
().
getTableName
(),
each
.
getColumn
().
getName
(),
each
.
getConditionValues
(
parameters
)));
}
return
result
;
}
private
ListShardingValue
getShardingCondition
(
final
GeneratedKey
generatedKey
)
{
return
new
ListShardingValue
<>(
generatedKey
.
getColumn
().
getTableName
(),
generatedKey
.
getColumn
().
getName
(),
new
GeneratedKeyCondition
(
generatedKey
.
getColumn
(),
generatedKey
.
getIndex
(),
generatedKey
.
getValue
()).
getConditionValues
(
parameters
));
}
}
sharding-core/src/main/java/io/shardingjdbc/core/optimizer/OptimizeEngine.java
浏览文件 @
0fd8e9df
...
@@ -17,134 +17,19 @@
...
@@ -17,134 +17,19 @@
package
io.shardingjdbc.core.optimizer
;
package
io.shardingjdbc.core.optimizer
;
import
com.google.common.collect.BoundType
;
import
com.google.common.collect.Range
;
import
io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue
;
import
io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue
;
import
io.shardingjdbc.core.api.algorithm.sharding.ShardingValue
;
import
io.shardingjdbc.core.constant.ShardingOperator
;
import
io.shardingjdbc.core.exception.ShardingJdbcException
;
import
io.shardingjdbc.core.parsing.parser.context.condition.AndCondition
;
import
io.shardingjdbc.core.parsing.parser.context.condition.Column
;
import
io.shardingjdbc.core.parsing.parser.context.condition.Condition
;
import
io.shardingjdbc.core.parsing.parser.context.condition.OrCondition
;
import
io.shardingjdbc.core.routing.sharding.AlwaysFalseShardingCondition
;
import
io.shardingjdbc.core.routing.sharding.GeneratedKey
;
import
io.shardingjdbc.core.routing.sharding.ShardingCondition
;
import
io.shardingjdbc.core.routing.sharding.ShardingConditions
;
import
io.shardingjdbc.core.routing.sharding.ShardingConditions
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.LinkedHashMap
;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map.Entry
;
/**
/**
* Optimize engine.
* Optimize engine.
*
*
* @author maxiaoguang
* @author maxiaoguang
*/
*/
public
final
class
OptimizeEngine
{
public
interface
OptimizeEngine
{
/**
/**
* Optimize sharding conditions.
* Optimize sharding conditions.
*
*
* @param orCondition or condition
* @param parameters parameters
* @param generatedKey generated key
* @return sharding conditions
* @return sharding conditions
*/
*/
public
ShardingConditions
optimize
(
final
OrCondition
orCondition
,
final
List
<
Object
>
parameters
,
final
GeneratedKey
generatedKey
)
{
ShardingConditions
optimize
();
if
(
null
==
generatedKey
&&
orCondition
.
getAndConditions
().
isEmpty
())
{
return
new
ShardingConditions
();
}
if
(
null
!=
generatedKey
&&
orCondition
.
getAndConditions
().
isEmpty
())
{
return
new
ShardingConditions
(
Collections
.
singletonList
(
optimize
(
generatedKey
.
getConditionsMap
(),
parameters
)));
}
List
<
ShardingCondition
>
shardingConditions
=
new
ArrayList
<>(
orCondition
.
getAndConditions
().
size
());
for
(
AndCondition
each
:
orCondition
.
getAndConditions
())
{
shardingConditions
.
add
(
optimize
(
getConditionsMap
(
each
,
generatedKey
),
parameters
));
}
return
new
ShardingConditions
(
shardingConditions
);
}
private
ShardingCondition
optimize
(
final
Map
<
Column
,
List
<
Condition
>>
conditionsMap
,
final
List
<
Object
>
parameters
)
{
ShardingCondition
result
=
new
ShardingCondition
();
for
(
Entry
<
Column
,
List
<
Condition
>>
entry
:
conditionsMap
.
entrySet
())
{
try
{
ShardingValue
shardingValue
=
optimize
(
entry
.
getKey
(),
entry
.
getValue
(),
parameters
);
if
(
shardingValue
instanceof
AlwaysFalseShardingValue
)
{
return
new
AlwaysFalseShardingCondition
();
}
result
.
getShardingValues
().
add
(
shardingValue
);
}
catch
(
final
ClassCastException
ex
)
{
throw
new
ShardingJdbcException
(
"Found different types for sharding value `%s`."
,
entry
.
getKey
());
}
}
return
result
;
}
private
ShardingValue
optimize
(
final
Column
column
,
final
List
<
Condition
>
conditions
,
final
List
<
Object
>
parameters
)
{
List
<
Comparable
<?>>
listValue
=
null
;
Range
<
Comparable
<?>>
rangeValue
=
null
;
for
(
Condition
each
:
conditions
)
{
List
<
Comparable
<?>>
conditionValues
=
each
.
getConditionValues
(
parameters
);
if
(
ShardingOperator
.
EQUAL
==
each
.
getOperator
()
||
ShardingOperator
.
IN
==
each
.
getOperator
())
{
listValue
=
optimize
(
conditionValues
,
listValue
);
if
(
listValue
.
isEmpty
())
{
return
new
AlwaysFalseShardingValue
();
}
}
if
(
ShardingOperator
.
BETWEEN
==
each
.
getOperator
())
{
try
{
rangeValue
=
optimize
(
Range
.
range
(
conditionValues
.
get
(
0
),
BoundType
.
CLOSED
,
conditionValues
.
get
(
1
),
BoundType
.
CLOSED
),
rangeValue
);
}
catch
(
final
IllegalArgumentException
ex
)
{
return
new
AlwaysFalseShardingValue
();
}
}
}
if
(
null
==
listValue
)
{
return
new
RangeShardingValue
<>(
column
.
getTableName
(),
column
.
getName
(),
rangeValue
);
}
if
(
null
==
rangeValue
)
{
return
new
ListShardingValue
<>(
column
.
getTableName
(),
column
.
getName
(),
listValue
);
}
listValue
=
optimize
(
listValue
,
rangeValue
);
return
listValue
.
isEmpty
()
?
new
AlwaysFalseShardingValue
()
:
new
ListShardingValue
<>(
column
.
getTableName
(),
column
.
getName
(),
listValue
);
}
private
List
<
Comparable
<?>>
optimize
(
final
List
<
Comparable
<?>>
value1
,
final
List
<
Comparable
<?>>
value2
)
{
if
(
null
==
value2
)
{
return
value1
;
}
value1
.
retainAll
(
value2
);
return
value1
;
}
private
Range
<
Comparable
<?>>
optimize
(
final
Range
<
Comparable
<?>>
value1
,
final
Range
<
Comparable
<?>>
value2
)
{
return
null
==
value2
?
value1
:
value1
.
intersection
(
value2
);
}
private
List
<
Comparable
<?>>
optimize
(
final
List
<
Comparable
<?>>
listValue
,
final
Range
<
Comparable
<?>>
rangeValue
)
{
List
<
Comparable
<?>>
result
=
new
LinkedList
<>();
for
(
Comparable
<?>
each
:
listValue
)
{
if
(
rangeValue
.
contains
(
each
))
{
result
.
add
(
each
);
}
}
return
result
;
}
private
Map
<
Column
,
List
<
Condition
>>
getConditionsMap
(
final
AndCondition
andCondition
,
final
GeneratedKey
generatedKey
)
{
Map
<
Column
,
List
<
Condition
>>
andConditionsMap
=
andCondition
.
getConditionsMap
();
if
(
null
==
generatedKey
)
{
return
andConditionsMap
;
}
Map
<
Column
,
List
<
Condition
>>
result
=
new
LinkedHashMap
<>(
andConditionsMap
.
size
()
+
1
);
result
.
putAll
(
andConditionsMap
);
result
.
putAll
(
generatedKey
.
getConditionsMap
());
return
result
;
}
}
}
sharding-core/src/main/java/io/shardingjdbc/core/optimizer/OptimizeEngineFactory.java
0 → 100644
浏览文件 @
0fd8e9df
/*
* 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
io.shardingjdbc.core.optimizer
;
import
io.shardingjdbc.core.parsing.parser.sql.SQLStatement
;
import
io.shardingjdbc.core.parsing.parser.sql.dml.DMLStatement
;
import
io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement
;
import
io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement
;
import
io.shardingjdbc.core.routing.sharding.GeneratedKey
;
import
lombok.AccessLevel
;
import
lombok.NoArgsConstructor
;
import
java.util.List
;
/**
* Optimize engine factory.
*
* @author zhangliang
*/
@NoArgsConstructor
(
access
=
AccessLevel
.
PRIVATE
)
public
final
class
OptimizeEngineFactory
{
/**
* Create optimize engine instance.
*
* @param sqlStatement SQL statement
* @param parameters parameters
* @param generatedKey generated key
* @return optimize engine instance
*/
public
static
OptimizeEngine
newInstance
(
final
SQLStatement
sqlStatement
,
final
List
<
Object
>
parameters
,
final
GeneratedKey
generatedKey
)
{
if
(
sqlStatement
instanceof
InsertStatement
)
{
return
new
InsertOptimizeEngine
(
sqlStatement
.
getConditions
().
getOrCondition
(),
parameters
,
generatedKey
);
}
if
(
sqlStatement
instanceof
SelectStatement
||
sqlStatement
instanceof
DMLStatement
)
{
return
new
QueryOptimizeEngine
(
sqlStatement
.
getConditions
().
getOrCondition
(),
parameters
);
}
// TODO do with DDL and DAL
return
new
QueryOptimizeEngine
(
sqlStatement
.
getConditions
().
getOrCondition
(),
parameters
);
}
}
sharding-core/src/main/java/io/shardingjdbc/core/optimizer/QueryOptimizeEngine.java
0 → 100644
浏览文件 @
0fd8e9df
/*
* 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
io.shardingjdbc.core.optimizer
;
import
com.google.common.collect.BoundType
;
import
com.google.common.collect.Range
;
import
io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue
;
import
io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue
;
import
io.shardingjdbc.core.api.algorithm.sharding.ShardingValue
;
import
io.shardingjdbc.core.constant.ShardingOperator
;
import
io.shardingjdbc.core.exception.ShardingJdbcException
;
import
io.shardingjdbc.core.parsing.parser.context.condition.AndCondition
;
import
io.shardingjdbc.core.parsing.parser.context.condition.Column
;
import
io.shardingjdbc.core.parsing.parser.context.condition.Condition
;
import
io.shardingjdbc.core.parsing.parser.context.condition.OrCondition
;
import
io.shardingjdbc.core.routing.sharding.AlwaysFalseShardingCondition
;
import
io.shardingjdbc.core.routing.sharding.ShardingCondition
;
import
io.shardingjdbc.core.routing.sharding.ShardingConditions
;
import
lombok.RequiredArgsConstructor
;
import
java.util.ArrayList
;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map.Entry
;
/**
* Query optimize engine.
*
* @author zhangliang
*/
@RequiredArgsConstructor
public
final
class
QueryOptimizeEngine
implements
OptimizeEngine
{
private
final
OrCondition
orCondition
;
private
final
List
<
Object
>
parameters
;
@Override
public
ShardingConditions
optimize
()
{
List
<
ShardingCondition
>
result
=
new
ArrayList
<>(
orCondition
.
getAndConditions
().
size
());
for
(
AndCondition
each
:
orCondition
.
getAndConditions
())
{
result
.
add
(
optimize
(
each
.
getConditionsMap
()));
}
return
new
ShardingConditions
(
result
);
}
private
ShardingCondition
optimize
(
final
Map
<
Column
,
List
<
Condition
>>
conditionsMap
)
{
ShardingCondition
result
=
new
ShardingCondition
();
for
(
Entry
<
Column
,
List
<
Condition
>>
entry
:
conditionsMap
.
entrySet
())
{
try
{
ShardingValue
shardingValue
=
optimize
(
entry
.
getKey
(),
entry
.
getValue
());
if
(
shardingValue
instanceof
AlwaysFalseShardingValue
)
{
return
new
AlwaysFalseShardingCondition
();
}
result
.
getShardingValues
().
add
(
shardingValue
);
}
catch
(
final
ClassCastException
ex
)
{
throw
new
ShardingJdbcException
(
"Found different types for sharding value `%s`."
,
entry
.
getKey
());
}
}
return
result
;
}
private
ShardingValue
optimize
(
final
Column
column
,
final
List
<
Condition
>
conditions
)
{
List
<
Comparable
<?>>
listValue
=
null
;
Range
<
Comparable
<?>>
rangeValue
=
null
;
for
(
Condition
each
:
conditions
)
{
List
<
Comparable
<?>>
conditionValues
=
each
.
getConditionValues
(
parameters
);
if
(
ShardingOperator
.
EQUAL
==
each
.
getOperator
()
||
ShardingOperator
.
IN
==
each
.
getOperator
())
{
listValue
=
optimize
(
conditionValues
,
listValue
);
if
(
listValue
.
isEmpty
())
{
return
new
AlwaysFalseShardingValue
();
}
}
if
(
ShardingOperator
.
BETWEEN
==
each
.
getOperator
())
{
try
{
rangeValue
=
optimize
(
Range
.
range
(
conditionValues
.
get
(
0
),
BoundType
.
CLOSED
,
conditionValues
.
get
(
1
),
BoundType
.
CLOSED
),
rangeValue
);
}
catch
(
final
IllegalArgumentException
ex
)
{
return
new
AlwaysFalseShardingValue
();
}
}
}
if
(
null
==
listValue
)
{
return
new
RangeShardingValue
<>(
column
.
getTableName
(),
column
.
getName
(),
rangeValue
);
}
if
(
null
==
rangeValue
)
{
return
new
ListShardingValue
<>(
column
.
getTableName
(),
column
.
getName
(),
listValue
);
}
listValue
=
optimize
(
listValue
,
rangeValue
);
return
listValue
.
isEmpty
()
?
new
AlwaysFalseShardingValue
()
:
new
ListShardingValue
<>(
column
.
getTableName
(),
column
.
getName
(),
listValue
);
}
private
List
<
Comparable
<?>>
optimize
(
final
List
<
Comparable
<?>>
value1
,
final
List
<
Comparable
<?>>
value2
)
{
if
(
null
==
value2
)
{
return
value1
;
}
value1
.
retainAll
(
value2
);
return
value1
;
}
private
Range
<
Comparable
<?>>
optimize
(
final
Range
<
Comparable
<?>>
value1
,
final
Range
<
Comparable
<?>>
value2
)
{
return
null
==
value2
?
value1
:
value1
.
intersection
(
value2
);
}
private
List
<
Comparable
<?>>
optimize
(
final
List
<
Comparable
<?>>
listValue
,
final
Range
<
Comparable
<?>>
rangeValue
)
{
List
<
Comparable
<?>>
result
=
new
LinkedList
<>();
for
(
Comparable
<?>
each
:
listValue
)
{
if
(
rangeValue
.
contains
(
each
))
{
result
.
add
(
each
);
}
}
return
result
;
}
}
sharding-core/src/main/java/io/shardingjdbc/core/parsing/parser/context/condition/GeneratedKeyCondition.java
浏览文件 @
0fd8e9df
...
@@ -21,6 +21,9 @@ import io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression;
...
@@ -21,6 +21,9 @@ import io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression;
import
lombok.Getter
;
import
lombok.Getter
;
import
lombok.ToString
;
import
lombok.ToString
;
import
java.util.Collections
;
import
java.util.List
;
/**
/**
* Generated key condition.
* Generated key condition.
*
*
...
@@ -43,4 +46,10 @@ public final class GeneratedKeyCondition extends Condition {
...
@@ -43,4 +46,10 @@ public final class GeneratedKeyCondition extends Condition {
this
.
index
=
index
;
this
.
index
=
index
;
this
.
value
=
value
;
this
.
value
=
value
;
}
}
@Override
public
List
<
Comparable
<?>>
getConditionValues
(
final
List
<?>
parameters
)
{
Comparable
<?>
result
=
null
==
value
?
(
Comparable
<?>)
parameters
.
get
(
index
)
:
(
Comparable
<?>)
value
;
return
Collections
.<
Comparable
<?>>
singletonList
(
result
);
}
}
}
sharding-core/src/main/java/io/shardingjdbc/core/routing/router/ParsingSQLRouter.java
浏览文件 @
0fd8e9df
...
@@ -20,7 +20,7 @@ package io.shardingjdbc.core.routing.router;
...
@@ -20,7 +20,7 @@ package io.shardingjdbc.core.routing.router;
import
com.google.common.base.Optional
;
import
com.google.common.base.Optional
;
import
com.google.common.base.Preconditions
;
import
com.google.common.base.Preconditions
;
import
io.shardingjdbc.core.constant.DatabaseType
;
import
io.shardingjdbc.core.constant.DatabaseType
;
import
io.shardingjdbc.core.optimizer.OptimizeEngine
;
import
io.shardingjdbc.core.optimizer.OptimizeEngine
Factory
;
import
io.shardingjdbc.core.parsing.SQLParsingEngine
;
import
io.shardingjdbc.core.parsing.SQLParsingEngine
;
import
io.shardingjdbc.core.parsing.parser.context.condition.Column
;
import
io.shardingjdbc.core.parsing.parser.context.condition.Column
;
import
io.shardingjdbc.core.parsing.parser.dialect.mysql.statement.ShowDatabasesStatement
;
import
io.shardingjdbc.core.parsing.parser.dialect.mysql.statement.ShowDatabasesStatement
;
...
@@ -120,7 +120,7 @@ public final class ParsingSQLRouter implements SQLRouter {
...
@@ -120,7 +120,7 @@ public final class ParsingSQLRouter implements SQLRouter {
private
RoutingResult
route
(
final
List
<
Object
>
parameters
,
final
SQLStatement
sqlStatement
,
final
GeneratedKey
generatedKey
)
{
private
RoutingResult
route
(
final
List
<
Object
>
parameters
,
final
SQLStatement
sqlStatement
,
final
GeneratedKey
generatedKey
)
{
Collection
<
String
>
tableNames
=
sqlStatement
.
getTables
().
getTableNames
();
Collection
<
String
>
tableNames
=
sqlStatement
.
getTables
().
getTableNames
();
ShardingConditions
shardingConditions
=
new
OptimizeEngine
().
optimize
(
sqlStatement
.
getConditions
().
getOrCondition
(),
parameters
,
generatedKey
);
ShardingConditions
shardingConditions
=
OptimizeEngineFactory
.
newInstance
(
sqlStatement
,
parameters
,
generatedKey
).
optimize
(
);
RoutingEngine
routingEngine
;
RoutingEngine
routingEngine
;
if
(
sqlStatement
instanceof
UseStatement
)
{
if
(
sqlStatement
instanceof
UseStatement
)
{
routingEngine
=
new
IgnoreRoutingEngine
();
routingEngine
=
new
IgnoreRoutingEngine
();
...
...
sharding-core/src/main/java/io/shardingjdbc/core/routing/sharding/GeneratedKey.java
浏览文件 @
0fd8e9df
...
@@ -18,16 +18,12 @@
...
@@ -18,16 +18,12 @@
package
io.shardingjdbc.core.routing.sharding
;
package
io.shardingjdbc.core.routing.sharding
;
import
io.shardingjdbc.core.parsing.parser.context.condition.Column
;
import
io.shardingjdbc.core.parsing.parser.context.condition.Column
;
import
io.shardingjdbc.core.parsing.parser.context.condition.Condition
;
import
io.shardingjdbc.core.parsing.parser.context.condition.GeneratedKeyCondition
;
import
io.shardingjdbc.core.parsing.parser.context.condition.GeneratedKeyCondition
;
import
lombok.Getter
;
import
lombok.Getter
;
import
lombok.RequiredArgsConstructor
;
import
lombok.RequiredArgsConstructor
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.LinkedList
;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
/**
/**
* Generated key.
* Generated key.
...
@@ -52,15 +48,4 @@ public final class GeneratedKey {
...
@@ -52,15 +48,4 @@ public final class GeneratedKey {
index
=
generatedKeyCondition
.
getIndex
();
index
=
generatedKeyCondition
.
getIndex
();
value
=
generatedKeyCondition
.
getValue
();
value
=
generatedKeyCondition
.
getValue
();
}
}
/**
* Get conditions map.
*
* @return conditions map
*/
public
Map
<
Column
,
List
<
Condition
>>
getConditionsMap
()
{
Map
<
Column
,
List
<
Condition
>>
result
=
new
HashMap
<>(
1
,
1
);
result
.
put
(
column
,
Collections
.<
Condition
>
singletonList
(
new
GeneratedKeyCondition
(
column
,
index
,
value
)));
return
result
;
}
}
}
sharding-core/src/main/java/io/shardingjdbc/core/routing/sharding/ShardingConditions.java
浏览文件 @
0fd8e9df
...
@@ -20,7 +20,6 @@ package io.shardingjdbc.core.routing.sharding;
...
@@ -20,7 +20,6 @@ package io.shardingjdbc.core.routing.sharding;
import
lombok.Getter
;
import
lombok.Getter
;
import
lombok.RequiredArgsConstructor
;
import
lombok.RequiredArgsConstructor
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.List
;
/**
/**
...
@@ -35,10 +34,6 @@ public final class ShardingConditions {
...
@@ -35,10 +34,6 @@ public final class ShardingConditions {
private
final
List
<
ShardingCondition
>
shardingConditions
;
private
final
List
<
ShardingCondition
>
shardingConditions
;
public
ShardingConditions
()
{
shardingConditions
=
Collections
.
emptyList
();
}
/**
/**
* Adjust sharding conditions is always false or not.
* Adjust sharding conditions is always false or not.
*
*
...
...
sharding-core/src/test/java/io/shardingjdbc/core/optimizer/AllOptimizerTests.java
浏览文件 @
0fd8e9df
...
@@ -21,6 +21,9 @@ import org.junit.runner.RunWith;
...
@@ -21,6 +21,9 @@ import org.junit.runner.RunWith;
import
org.junit.runners.Suite
;
import
org.junit.runners.Suite
;
@RunWith
(
Suite
.
class
)
@RunWith
(
Suite
.
class
)
@Suite
.
SuiteClasses
(
OptimizeEngineTest
.
class
)
@Suite
.
SuiteClasses
({
InsertOptimizeEngineTest
.
class
,
QueryOptimizeEngineTest
.
class
})
public
class
AllOptimizerTests
{
public
class
AllOptimizerTests
{
}
}
sharding-core/src/test/java/io/shardingjdbc/core/optimizer/InsertOptimizeEngineTest.java
0 → 100644
浏览文件 @
0fd8e9df
/*
* 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
io.shardingjdbc.core.optimizer
;
import
io.shardingjdbc.core.api.algorithm.sharding.ListShardingValue
;
import
io.shardingjdbc.core.parsing.parser.context.condition.AndCondition
;
import
io.shardingjdbc.core.parsing.parser.context.condition.Column
;
import
io.shardingjdbc.core.parsing.parser.context.condition.Condition
;
import
io.shardingjdbc.core.parsing.parser.context.condition.OrCondition
;
import
io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression
;
import
io.shardingjdbc.core.parsing.parser.expression.SQLPlaceholderExpression
;
import
io.shardingjdbc.core.routing.sharding.GeneratedKey
;
import
io.shardingjdbc.core.routing.sharding.ShardingConditions
;
import
org.junit.Test
;
import
java.util.Collections
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
public
final
class
InsertOptimizeEngineTest
{
@Test
public
void
assertOptimizeWithoutConditionsAndGeneratedKey
()
{
ShardingConditions
shardingConditions
=
new
InsertOptimizeEngine
(
new
OrCondition
(),
Collections
.
emptyList
(),
null
).
optimize
();
assertFalse
(
shardingConditions
.
isAlwaysFalse
());
assertTrue
(
shardingConditions
.
getShardingConditions
().
isEmpty
());
}
@Test
public
void
assertOptimizeWithConditionsOnly
()
{
Condition
condition1
=
new
Condition
(
new
Column
(
"column1"
,
"tbl"
),
new
SQLNumberExpression
(
1000L
));
Condition
condition2
=
new
Condition
(
new
Column
(
"column2"
,
"tbl"
),
new
SQLPlaceholderExpression
(
0
));
AndCondition
andCondition
=
new
AndCondition
();
andCondition
.
getConditions
().
add
(
condition1
);
andCondition
.
getConditions
().
add
(
condition2
);
OrCondition
orCondition
=
new
OrCondition
();
orCondition
.
getAndConditions
().
add
(
andCondition
);
ShardingConditions
actual
=
new
InsertOptimizeEngine
(
orCondition
,
Collections
.<
Object
>
singletonList
(
2000L
),
null
).
optimize
();
assertFalse
(
actual
.
isAlwaysFalse
());
assertThat
(
actual
.
getShardingConditions
().
size
(),
is
(
1
));
assertThat
(
actual
.
getShardingConditions
().
get
(
0
).
getShardingValues
().
size
(),
is
(
2
));
assertShardingValue
((
ListShardingValue
)
actual
.
getShardingConditions
().
get
(
0
).
getShardingValues
().
get
(
0
),
1000L
);
assertShardingValue
((
ListShardingValue
)
actual
.
getShardingConditions
().
get
(
0
).
getShardingValues
().
get
(
1
),
2000L
);
}
@Test
public
void
assertOptimizeWithGeneratedKeyOnly
()
{
ShardingConditions
actual
=
new
InsertOptimizeEngine
(
new
OrCondition
(),
Collections
.
emptyList
(),
new
GeneratedKey
(
new
Column
(
"column3"
,
"tbl"
),
-
1
,
3000L
)).
optimize
();
assertFalse
(
actual
.
isAlwaysFalse
());
assertThat
(
actual
.
getShardingConditions
().
size
(),
is
(
1
));
assertThat
(
actual
.
getShardingConditions
().
get
(
0
).
getShardingValues
().
size
(),
is
(
1
));
assertShardingValue
((
ListShardingValue
)
actual
.
getShardingConditions
().
get
(
0
).
getShardingValues
().
get
(
0
),
3000L
);
}
@Test
public
void
assertOptimizeWithConditionsAndGeneratedKey
()
{
Condition
condition1
=
new
Condition
(
new
Column
(
"column1"
,
"tbl"
),
new
SQLNumberExpression
(
1000L
));
Condition
condition2
=
new
Condition
(
new
Column
(
"column2"
,
"tbl"
),
new
SQLPlaceholderExpression
(
0
));
AndCondition
andCondition
=
new
AndCondition
();
andCondition
.
getConditions
().
add
(
condition1
);
andCondition
.
getConditions
().
add
(
condition2
);
OrCondition
orCondition
=
new
OrCondition
();
orCondition
.
getAndConditions
().
add
(
andCondition
);
ShardingConditions
actual
=
new
InsertOptimizeEngine
(
orCondition
,
Collections
.<
Object
>
singletonList
(
2000L
),
new
GeneratedKey
(
new
Column
(
"column3"
,
"tbl"
),
-
1
,
3000L
)).
optimize
();
assertFalse
(
actual
.
isAlwaysFalse
());
assertThat
(
actual
.
getShardingConditions
().
size
(),
is
(
1
));
assertThat
(
actual
.
getShardingConditions
().
get
(
0
).
getShardingValues
().
size
(),
is
(
3
));
assertShardingValue
((
ListShardingValue
)
actual
.
getShardingConditions
().
get
(
0
).
getShardingValues
().
get
(
0
),
1000L
);
assertShardingValue
((
ListShardingValue
)
actual
.
getShardingConditions
().
get
(
0
).
getShardingValues
().
get
(
1
),
2000L
);
assertShardingValue
((
ListShardingValue
)
actual
.
getShardingConditions
().
get
(
0
).
getShardingValues
().
get
(
2
),
3000L
);
}
private
void
assertShardingValue
(
final
ListShardingValue
actual
,
final
long
expected
)
{
assertThat
(
actual
.
getValues
().
size
(),
is
(
1
));
assertThat
((
Long
)
actual
.
getValues
().
iterator
().
next
(),
is
(
expected
));
}
// @SuppressWarnings("unchecked")
// @Test
// public void assertOptimizeGeneratedKeyCondition() {
// ShardingConditions shardingConditions = new OptimizeEngine().optimize(new OrCondition(), Collections.emptyList(), new GeneratedKey(new Column("test", "test"), 0, 1));
// assertFalse(shardingConditions.isAlwaysFalse());
// ShardingCondition shardingCondition = shardingConditions.getShardingConditions().get(0);
// ShardingValue shardingValue = shardingCondition.getShardingValues().get(0);
// Collection<Comparable<?>> values = ((ListShardingValue<Comparable<?>>) shardingValue).getValues();
// assertThat(values.size(), is(1));
// assertTrue(values.contains(1));
// }
}
sharding-core/src/test/java/io/shardingjdbc/core/optimizer/OptimizeEngineTest.java
→
sharding-core/src/test/java/io/shardingjdbc/core/optimizer/
Query
OptimizeEngineTest.java
浏览文件 @
0fd8e9df
...
@@ -27,7 +27,6 @@ import io.shardingjdbc.core.parsing.parser.context.condition.Condition;
...
@@ -27,7 +27,6 @@ import io.shardingjdbc.core.parsing.parser.context.condition.Condition;
import
io.shardingjdbc.core.parsing.parser.context.condition.OrCondition
;
import
io.shardingjdbc.core.parsing.parser.context.condition.OrCondition
;
import
io.shardingjdbc.core.parsing.parser.expression.SQLExpression
;
import
io.shardingjdbc.core.parsing.parser.expression.SQLExpression
;
import
io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression
;
import
io.shardingjdbc.core.parsing.parser.expression.SQLNumberExpression
;
import
io.shardingjdbc.core.routing.sharding.GeneratedKey
;
import
io.shardingjdbc.core.routing.sharding.ShardingCondition
;
import
io.shardingjdbc.core.routing.sharding.ShardingCondition
;
import
io.shardingjdbc.core.routing.sharding.ShardingConditions
;
import
io.shardingjdbc.core.routing.sharding.ShardingConditions
;
import
org.hamcrest.CoreMatchers
;
import
org.hamcrest.CoreMatchers
;
...
@@ -42,58 +41,58 @@ import static org.junit.Assert.assertFalse;
...
@@ -42,58 +41,58 @@ import static org.junit.Assert.assertFalse;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
public
class
OptimizeEngineTest
{
public
final
class
Query
OptimizeEngineTest
{
@Test
@Test
public
void
assertOptimizeAlwaysFalseListConditions
()
{
public
void
assertOptimizeAlwaysFalseListConditions
()
{
Condition
condition1
=
new
Condition
(
new
Column
(
"
test"
,
"test
"
),
Arrays
.<
SQLExpression
>
asList
(
new
SQLNumberExpression
(
1
),
new
SQLNumberExpression
(
2
)));
Condition
condition1
=
new
Condition
(
new
Column
(
"
column"
,
"tbl
"
),
Arrays
.<
SQLExpression
>
asList
(
new
SQLNumberExpression
(
1
),
new
SQLNumberExpression
(
2
)));
Condition
condition2
=
new
Condition
(
new
Column
(
"
test"
,
"test
"
),
new
SQLNumberExpression
(
3
));
Condition
condition2
=
new
Condition
(
new
Column
(
"
column"
,
"tbl
"
),
new
SQLNumberExpression
(
3
));
AndCondition
andCondition
=
new
AndCondition
();
AndCondition
andCondition
=
new
AndCondition
();
andCondition
.
getConditions
().
add
(
condition1
);
andCondition
.
getConditions
().
add
(
condition1
);
andCondition
.
getConditions
().
add
(
condition2
);
andCondition
.
getConditions
().
add
(
condition2
);
OrCondition
orCondition
=
new
OrCondition
();
OrCondition
orCondition
=
new
OrCondition
();
orCondition
.
getAndConditions
().
add
(
andCondition
);
orCondition
.
getAndConditions
().
add
(
andCondition
);
ShardingConditions
shardingConditions
=
new
OptimizeEngine
().
optimize
(
orCondition
,
Collections
.
emptyList
(),
null
);
ShardingConditions
shardingConditions
=
new
QueryOptimizeEngine
(
orCondition
,
Collections
.
emptyList
()).
optimize
(
);
assertTrue
(
shardingConditions
.
isAlwaysFalse
());
assertTrue
(
shardingConditions
.
isAlwaysFalse
());
}
}
@Test
@Test
public
void
assertOptimizeAlwaysFalseRangeConditions
()
{
public
void
assertOptimizeAlwaysFalseRangeConditions
()
{
Condition
condition1
=
new
Condition
(
new
Column
(
"
test"
,
"test
"
),
new
SQLNumberExpression
(
1
),
new
SQLNumberExpression
(
2
));
Condition
condition1
=
new
Condition
(
new
Column
(
"
column"
,
"tbl
"
),
new
SQLNumberExpression
(
1
),
new
SQLNumberExpression
(
2
));
Condition
condition2
=
new
Condition
(
new
Column
(
"
test"
,
"test
"
),
new
SQLNumberExpression
(
3
),
new
SQLNumberExpression
(
4
));
Condition
condition2
=
new
Condition
(
new
Column
(
"
column"
,
"tbl
"
),
new
SQLNumberExpression
(
3
),
new
SQLNumberExpression
(
4
));
AndCondition
andCondition
=
new
AndCondition
();
AndCondition
andCondition
=
new
AndCondition
();
andCondition
.
getConditions
().
add
(
condition1
);
andCondition
.
getConditions
().
add
(
condition1
);
andCondition
.
getConditions
().
add
(
condition2
);
andCondition
.
getConditions
().
add
(
condition2
);
OrCondition
orCondition
=
new
OrCondition
();
OrCondition
orCondition
=
new
OrCondition
();
orCondition
.
getAndConditions
().
add
(
andCondition
);
orCondition
.
getAndConditions
().
add
(
andCondition
);
ShardingConditions
shardingConditions
=
new
OptimizeEngine
().
optimize
(
orCondition
,
Collections
.
emptyList
(),
null
);
ShardingConditions
shardingConditions
=
new
QueryOptimizeEngine
(
orCondition
,
Collections
.
emptyList
()).
optimize
(
);
assertTrue
(
shardingConditions
.
isAlwaysFalse
());
assertTrue
(
shardingConditions
.
isAlwaysFalse
());
}
}
@Test
@Test
public
void
assertOptimizeAlwaysFalseListConditionsAndRangeConditions
()
{
public
void
assertOptimizeAlwaysFalseListConditionsAndRangeConditions
()
{
Condition
condition1
=
new
Condition
(
new
Column
(
"
test"
,
"test
"
),
Arrays
.<
SQLExpression
>
asList
(
new
SQLNumberExpression
(
1
),
new
SQLNumberExpression
(
2
)));
Condition
condition1
=
new
Condition
(
new
Column
(
"
column"
,
"tbl
"
),
Arrays
.<
SQLExpression
>
asList
(
new
SQLNumberExpression
(
1
),
new
SQLNumberExpression
(
2
)));
Condition
condition2
=
new
Condition
(
new
Column
(
"
test"
,
"test
"
),
new
SQLNumberExpression
(
3
),
new
SQLNumberExpression
(
4
));
Condition
condition2
=
new
Condition
(
new
Column
(
"
column"
,
"tbl
"
),
new
SQLNumberExpression
(
3
),
new
SQLNumberExpression
(
4
));
AndCondition
andCondition
=
new
AndCondition
();
AndCondition
andCondition
=
new
AndCondition
();
andCondition
.
getConditions
().
add
(
condition1
);
andCondition
.
getConditions
().
add
(
condition1
);
andCondition
.
getConditions
().
add
(
condition2
);
andCondition
.
getConditions
().
add
(
condition2
);
OrCondition
orCondition
=
new
OrCondition
();
OrCondition
orCondition
=
new
OrCondition
();
orCondition
.
getAndConditions
().
add
(
andCondition
);
orCondition
.
getAndConditions
().
add
(
andCondition
);
ShardingConditions
shardingConditions
=
new
OptimizeEngine
().
optimize
(
orCondition
,
Collections
.
emptyList
(),
null
);
ShardingConditions
shardingConditions
=
new
QueryOptimizeEngine
(
orCondition
,
Collections
.
emptyList
()).
optimize
(
);
assertTrue
(
shardingConditions
.
isAlwaysFalse
());
assertTrue
(
shardingConditions
.
isAlwaysFalse
());
}
}
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
@Test
@Test
public
void
assertOptimizeListConditions
()
{
public
void
assertOptimizeListConditions
()
{
Condition
condition1
=
new
Condition
(
new
Column
(
"
test"
,
"test
"
),
Arrays
.<
SQLExpression
>
asList
(
new
SQLNumberExpression
(
1
),
new
SQLNumberExpression
(
2
)));
Condition
condition1
=
new
Condition
(
new
Column
(
"
column"
,
"tbl
"
),
Arrays
.<
SQLExpression
>
asList
(
new
SQLNumberExpression
(
1
),
new
SQLNumberExpression
(
2
)));
Condition
condition2
=
new
Condition
(
new
Column
(
"
test"
,
"test
"
),
new
SQLNumberExpression
(
1
));
Condition
condition2
=
new
Condition
(
new
Column
(
"
column"
,
"tbl
"
),
new
SQLNumberExpression
(
1
));
AndCondition
andCondition
=
new
AndCondition
();
AndCondition
andCondition
=
new
AndCondition
();
andCondition
.
getConditions
().
add
(
condition1
);
andCondition
.
getConditions
().
add
(
condition1
);
andCondition
.
getConditions
().
add
(
condition2
);
andCondition
.
getConditions
().
add
(
condition2
);
OrCondition
orCondition
=
new
OrCondition
();
OrCondition
orCondition
=
new
OrCondition
();
orCondition
.
getAndConditions
().
add
(
andCondition
);
orCondition
.
getAndConditions
().
add
(
andCondition
);
ShardingConditions
shardingConditions
=
new
OptimizeEngine
().
optimize
(
orCondition
,
Collections
.
emptyList
(),
null
);
ShardingConditions
shardingConditions
=
new
QueryOptimizeEngine
(
orCondition
,
Collections
.
emptyList
()).
optimize
(
);
assertFalse
(
shardingConditions
.
isAlwaysFalse
());
assertFalse
(
shardingConditions
.
isAlwaysFalse
());
ShardingCondition
shardingCondition
=
shardingConditions
.
getShardingConditions
().
get
(
0
);
ShardingCondition
shardingCondition
=
shardingConditions
.
getShardingConditions
().
get
(
0
);
ShardingValue
shardingValue
=
shardingCondition
.
getShardingValues
().
get
(
0
);
ShardingValue
shardingValue
=
shardingCondition
.
getShardingValues
().
get
(
0
);
...
@@ -105,14 +104,14 @@ public class OptimizeEngineTest {
...
@@ -105,14 +104,14 @@ public class OptimizeEngineTest {
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
@Test
@Test
public
void
assertOptimizeRangeConditions
()
{
public
void
assertOptimizeRangeConditions
()
{
Condition
condition1
=
new
Condition
(
new
Column
(
"
test"
,
"test
"
),
new
SQLNumberExpression
(
1
),
new
SQLNumberExpression
(
2
));
Condition
condition1
=
new
Condition
(
new
Column
(
"
column"
,
"tbl
"
),
new
SQLNumberExpression
(
1
),
new
SQLNumberExpression
(
2
));
Condition
condition2
=
new
Condition
(
new
Column
(
"
test"
,
"test
"
),
new
SQLNumberExpression
(
1
),
new
SQLNumberExpression
(
3
));
Condition
condition2
=
new
Condition
(
new
Column
(
"
column"
,
"tbl
"
),
new
SQLNumberExpression
(
1
),
new
SQLNumberExpression
(
3
));
AndCondition
andCondition
=
new
AndCondition
();
AndCondition
andCondition
=
new
AndCondition
();
andCondition
.
getConditions
().
add
(
condition1
);
andCondition
.
getConditions
().
add
(
condition1
);
andCondition
.
getConditions
().
add
(
condition2
);
andCondition
.
getConditions
().
add
(
condition2
);
OrCondition
orCondition
=
new
OrCondition
();
OrCondition
orCondition
=
new
OrCondition
();
orCondition
.
getAndConditions
().
add
(
andCondition
);
orCondition
.
getAndConditions
().
add
(
andCondition
);
ShardingConditions
shardingConditions
=
new
OptimizeEngine
().
optimize
(
orCondition
,
Collections
.
emptyList
(),
null
);
ShardingConditions
shardingConditions
=
new
QueryOptimizeEngine
(
orCondition
,
Collections
.
emptyList
()).
optimize
(
);
assertFalse
(
shardingConditions
.
isAlwaysFalse
());
assertFalse
(
shardingConditions
.
isAlwaysFalse
());
ShardingCondition
shardingCondition
=
shardingConditions
.
getShardingConditions
().
get
(
0
);
ShardingCondition
shardingCondition
=
shardingConditions
.
getShardingConditions
().
get
(
0
);
ShardingValue
shardingValue
=
shardingCondition
.
getShardingValues
().
get
(
0
);
ShardingValue
shardingValue
=
shardingCondition
.
getShardingValues
().
get
(
0
);
...
@@ -124,14 +123,14 @@ public class OptimizeEngineTest {
...
@@ -124,14 +123,14 @@ public class OptimizeEngineTest {
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
@Test
@Test
public
void
assertOptimizeListConditionsAndRangeConditions
()
{
public
void
assertOptimizeListConditionsAndRangeConditions
()
{
Condition
condition1
=
new
Condition
(
new
Column
(
"
test"
,
"test
"
),
Arrays
.<
SQLExpression
>
asList
(
new
SQLNumberExpression
(
1
),
new
SQLNumberExpression
(
2
)));
Condition
condition1
=
new
Condition
(
new
Column
(
"
column"
,
"tbl
"
),
Arrays
.<
SQLExpression
>
asList
(
new
SQLNumberExpression
(
1
),
new
SQLNumberExpression
(
2
)));
Condition
condition2
=
new
Condition
(
new
Column
(
"
test"
,
"test
"
),
new
SQLNumberExpression
(
1
),
new
SQLNumberExpression
(
2
));
Condition
condition2
=
new
Condition
(
new
Column
(
"
column"
,
"tbl
"
),
new
SQLNumberExpression
(
1
),
new
SQLNumberExpression
(
2
));
AndCondition
andCondition
=
new
AndCondition
();
AndCondition
andCondition
=
new
AndCondition
();
andCondition
.
getConditions
().
add
(
condition1
);
andCondition
.
getConditions
().
add
(
condition1
);
andCondition
.
getConditions
().
add
(
condition2
);
andCondition
.
getConditions
().
add
(
condition2
);
OrCondition
orCondition
=
new
OrCondition
();
OrCondition
orCondition
=
new
OrCondition
();
orCondition
.
getAndConditions
().
add
(
andCondition
);
orCondition
.
getAndConditions
().
add
(
andCondition
);
ShardingConditions
shardingConditions
=
new
OptimizeEngine
().
optimize
(
orCondition
,
Collections
.
emptyList
(),
null
);
ShardingConditions
shardingConditions
=
new
QueryOptimizeEngine
(
orCondition
,
Collections
.
emptyList
()).
optimize
(
);
assertFalse
(
shardingConditions
.
isAlwaysFalse
());
assertFalse
(
shardingConditions
.
isAlwaysFalse
());
ShardingCondition
shardingCondition
=
shardingConditions
.
getShardingConditions
().
get
(
0
);
ShardingCondition
shardingCondition
=
shardingConditions
.
getShardingConditions
().
get
(
0
);
ShardingValue
shardingValue
=
shardingCondition
.
getShardingValues
().
get
(
0
);
ShardingValue
shardingValue
=
shardingCondition
.
getShardingValues
().
get
(
0
);
...
@@ -139,16 +138,4 @@ public class OptimizeEngineTest {
...
@@ -139,16 +138,4 @@ public class OptimizeEngineTest {
assertThat
(
values
.
size
(),
is
(
2
));
assertThat
(
values
.
size
(),
is
(
2
));
assertTrue
(
values
.
containsAll
(
Arrays
.
asList
(
1
,
2
)));
assertTrue
(
values
.
containsAll
(
Arrays
.
asList
(
1
,
2
)));
}
}
@SuppressWarnings
(
"unchecked"
)
@Test
public
void
assertOptimizeGeneratedKeyCondition
()
{
ShardingConditions
shardingConditions
=
new
OptimizeEngine
().
optimize
(
new
OrCondition
(),
Collections
.
emptyList
(),
new
GeneratedKey
(
new
Column
(
"test"
,
"test"
),
0
,
1
));
assertFalse
(
shardingConditions
.
isAlwaysFalse
());
ShardingCondition
shardingCondition
=
shardingConditions
.
getShardingConditions
().
get
(
0
);
ShardingValue
shardingValue
=
shardingCondition
.
getShardingValues
().
get
(
0
);
Collection
<
Comparable
<?>>
values
=
((
ListShardingValue
<
Comparable
<?>>)
shardingValue
).
getValues
();
assertThat
(
values
.
size
(),
is
(
1
));
assertTrue
(
values
.
contains
(
1
));
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录