Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
9d3f7e41
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 搜索 >>
未验证
提交
9d3f7e41
编写于
11月 02, 2020
作者:
L
Liang Zhang
提交者:
GitHub
11月 02, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use SQLRouteExecutor to refactor SQL route engine (#8009)
* For code format * Use SQLRouteExecutor to refactor SQL route engine
上级
e240277d
变更
12
隐藏空白更改
内联
并排
Showing
12 changed file
with
150 addition
and
65 deletion
+150
-65
shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/test/java/org/apache/shardingsphere/encrypt/rewrite/parameterized/EncryptSQLRewriterParameterizedTest.java
...te/parameterized/EncryptSQLRewriterParameterizedTest.java
+1
-1
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/metadata/ShardingTableAddressingMetaDataDecorator.java
...ng/metadata/ShardingTableAddressingMetaDataDecorator.java
+5
-2
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/MixSQLRewriterParameterizedTest.java
...ewrite/parameterized/MixSQLRewriterParameterizedTest.java
+1
-1
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/ShardingSQLRewriterParameterizedTest.java
...e/parameterized/ShardingSQLRewriterParameterizedTest.java
+1
-1
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/AbstractSQLRouteTest.java
...ding/route/engine/type/standard/AbstractSQLRouteTest.java
+1
-1
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/model/logic/LogicSchemaMetaDataLoader.java
...infra/metadata/model/logic/LogicSchemaMetaDataLoader.java
+2
-2
shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java
.../shardingsphere/infra/context/kernel/KernelProcessor.java
+1
-1
shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngine.java
...che/shardingsphere/infra/route/engine/SQLRouteEngine.java
+14
-33
shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteExecutor.java
...e/shardingsphere/infra/route/engine/SQLRouteExecutor.java
+37
-0
shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/AllSQLRouteExecutor.java
...ngsphere/infra/route/engine/impl/AllSQLRouteExecutor.java
+19
-21
shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java
...here/infra/route/engine/impl/PartialSQLRouteExecutor.java
+66
-0
shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngineTest.java
...shardingsphere/infra/route/engine/SQLRouteEngineTest.java
+2
-2
未找到文件。
shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/test/java/org/apache/shardingsphere/encrypt/rewrite/parameterized/EncryptSQLRewriterParameterizedTest.java
浏览文件 @
9d3f7e41
...
...
@@ -82,7 +82,7 @@ public final class EncryptSQLRewriterParameterizedTest extends AbstractSQLRewrit
getTestParameters
().
getInputParameters
(),
sqlStatementParserEngine
.
parse
(
getTestParameters
().
getInputSQL
(),
false
));
LogicSQL
logicSQL
=
new
LogicSQL
(
sqlStatementContext
,
getTestParameters
().
getInputSQL
(),
getTestParameters
().
getInputParameters
());
ShardingSphereSchema
schema
=
new
ShardingSphereSchema
(
"sharding_db"
,
Collections
.
emptyList
(),
rules
,
Collections
.
emptyMap
(),
metaData
);
RouteContext
routeContext
=
new
SQLRouteEngine
(
props
,
rule
s
).
route
(
logicSQL
,
schema
);
RouteContext
routeContext
=
new
SQLRouteEngine
(
rules
,
prop
s
).
route
(
logicSQL
,
schema
);
SQLRewriteResult
sqlRewriteResult
=
new
SQLRewriteEntry
(
metaData
.
getSchemaMetaData
().
getConfiguredSchemaMetaData
(),
props
,
rules
).
rewrite
(
getTestParameters
().
getInputSQL
(),
getTestParameters
().
getInputParameters
(),
sqlStatementContext
,
routeContext
);
return
sqlRewriteResult
instanceof
GenericSQLRewriteResult
...
...
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/metadata/ShardingTableAddressingMetaDataDecorator.java
浏览文件 @
9d3f7e41
...
...
@@ -38,10 +38,13 @@ public final class ShardingTableAddressingMetaDataDecorator implements TableAddr
}
private
void
decorate
(
final
TableRule
tableRule
,
final
TableAddressingMetaData
metaData
)
{
boolean
found
=
false
;
for
(
String
each
:
tableRule
.
getActualDataNodes
().
stream
().
map
(
DataNode:
:
getTableName
).
collect
(
Collectors
.
toSet
()))
{
metaData
.
getTableDataSourceNamesMapper
().
remove
(
each
);
found
=
null
!=
metaData
.
getTableDataSourceNamesMapper
().
remove
(
each
)
||
found
;
}
if
(
found
)
{
metaData
.
getTableDataSourceNamesMapper
().
put
(
tableRule
.
getLogicTable
(),
new
LinkedList
<>(
tableRule
.
getActualDatasourceNames
()));
}
metaData
.
getTableDataSourceNamesMapper
().
put
(
tableRule
.
getLogicTable
(),
new
LinkedList
<>(
tableRule
.
getActualDatasourceNames
()));
}
@Override
...
...
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/MixSQLRewriterParameterizedTest.java
浏览文件 @
9d3f7e41
...
...
@@ -89,7 +89,7 @@ public final class MixSQLRewriterParameterizedTest extends AbstractSQLRewriterPa
getTestParameters
().
getInputParameters
(),
sqlStatementParserEngine
.
parse
(
getTestParameters
().
getInputSQL
(),
false
));
LogicSQL
logicSQL
=
new
LogicSQL
(
sqlStatementContext
,
getTestParameters
().
getInputSQL
(),
getTestParameters
().
getInputParameters
());
ShardingSphereSchema
schema
=
new
ShardingSphereSchema
(
"sharding_db"
,
Collections
.
emptyList
(),
rules
,
Collections
.
emptyMap
(),
metaData
);
RouteContext
routeContext
=
new
SQLRouteEngine
(
props
,
rule
s
).
route
(
logicSQL
,
schema
);
RouteContext
routeContext
=
new
SQLRouteEngine
(
rules
,
prop
s
).
route
(
logicSQL
,
schema
);
SQLRewriteResult
sqlRewriteResult
=
new
SQLRewriteEntry
(
metaData
.
getSchemaMetaData
().
getConfiguredSchemaMetaData
(),
props
,
rules
).
rewrite
(
getTestParameters
().
getInputSQL
(),
getTestParameters
().
getInputParameters
(),
sqlStatementContext
,
routeContext
);
return
sqlRewriteResult
instanceof
GenericSQLRewriteResult
...
...
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/ShardingSQLRewriterParameterizedTest.java
浏览文件 @
9d3f7e41
...
...
@@ -89,7 +89,7 @@ public final class ShardingSQLRewriterParameterizedTest extends AbstractSQLRewri
getTestParameters
().
getInputParameters
(),
sqlStatementParserEngine
.
parse
(
getTestParameters
().
getInputSQL
(),
false
));
LogicSQL
logicSQL
=
new
LogicSQL
(
sqlStatementContext
,
getTestParameters
().
getInputSQL
(),
getTestParameters
().
getInputParameters
());
ShardingSphereSchema
schema
=
new
ShardingSphereSchema
(
"sharding_db"
,
Collections
.
emptyList
(),
rules
,
Collections
.
emptyMap
(),
metaData
);
RouteContext
routeContext
=
new
SQLRouteEngine
(
props
,
rule
s
).
route
(
logicSQL
,
schema
);
RouteContext
routeContext
=
new
SQLRouteEngine
(
rules
,
prop
s
).
route
(
logicSQL
,
schema
);
SQLRewriteResult
sqlRewriteResult
=
new
SQLRewriteEntry
(
metaData
.
getSchemaMetaData
().
getConfiguredSchemaMetaData
(),
props
,
rules
).
rewrite
(
getTestParameters
().
getInputSQL
(),
getTestParameters
().
getInputParameters
(),
sqlStatementContext
,
routeContext
);
return
sqlRewriteResult
instanceof
GenericSQLRewriteResult
...
...
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/AbstractSQLRouteTest.java
浏览文件 @
9d3f7e41
...
...
@@ -62,7 +62,7 @@ public abstract class AbstractSQLRouteTest extends AbstractRoutingEngineTest {
metaData
.
getSchemaMetaData
().
getConfiguredSchemaMetaData
(),
parameters
,
sqlStatementParserEngine
.
parse
(
sql
,
false
));
LogicSQL
logicSQL
=
new
LogicSQL
(
sqlStatementContext
,
sql
,
parameters
);
ShardingSphereSchema
schema
=
new
ShardingSphereSchema
(
"sharding_db"
,
Collections
.
emptyList
(),
Collections
.
singleton
(
shardingRule
),
Collections
.
emptyMap
(),
metaData
);
RouteContext
result
=
new
SQLRouteEngine
(
props
,
Collections
.
singletonList
(
shardingRule
)
).
route
(
logicSQL
,
schema
);
RouteContext
result
=
new
SQLRouteEngine
(
Collections
.
singletonList
(
shardingRule
),
props
).
route
(
logicSQL
,
schema
);
assertThat
(
result
.
getRouteUnits
().
size
(),
is
(
1
));
return
result
;
}
...
...
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/model/logic/LogicSchemaMetaDataLoader.java
浏览文件 @
9d3f7e41
...
...
@@ -136,8 +136,8 @@ public final class LogicSchemaMetaDataLoader {
return
load
(
databaseType
,
dataSourceMap
,
tableName
,
props
);
}
private
Map
<
String
,
Collection
<
String
>>
loadUnConfiguredSchemaMetaData
(
final
DatabaseType
databaseType
,
final
Map
<
String
,
DataSource
>
dataSourceMap
,
final
Collection
<
String
>
excludedTableNames
)
throws
SQLException
{
private
Map
<
String
,
Collection
<
String
>>
loadUnConfiguredSchemaMetaData
(
final
DatabaseType
databaseType
,
final
Map
<
String
,
DataSource
>
dataSourceMap
,
final
Collection
<
String
>
excludedTableNames
)
throws
SQLException
{
Map
<
String
,
Collection
<
String
>>
result
=
new
HashMap
<>(
dataSourceMap
.
size
(),
1
);
for
(
Entry
<
String
,
DataSource
>
entry
:
dataSourceMap
.
entrySet
())
{
Collection
<
String
>
tableNames
=
PhysicalSchemaMetaDataLoader
.
loadTableNames
(
entry
.
getValue
(),
databaseType
,
excludedTableNames
);
...
...
shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java
浏览文件 @
9d3f7e41
...
...
@@ -47,7 +47,7 @@ public final class KernelProcessor {
*/
public
ExecutionContext
generateExecutionContext
(
final
LogicSQL
logicSQL
,
final
ShardingSphereSchema
schema
,
final
ConfigurationProperties
props
)
{
Collection
<
ShardingSphereRule
>
rules
=
schema
.
getRules
();
SQLRouteEngine
sqlRouteEngine
=
new
SQLRouteEngine
(
props
,
rule
s
);
SQLRouteEngine
sqlRouteEngine
=
new
SQLRouteEngine
(
rules
,
prop
s
);
SQLStatementContext
<?>
sqlStatementContext
=
logicSQL
.
getSqlStatementContext
();
RouteContext
routeContext
=
sqlRouteEngine
.
route
(
logicSQL
,
schema
);
SQLRewriteEntry
rewriteEntry
=
new
SQLRewriteEntry
(
schema
.
getMetaData
().
getSchemaMetaData
().
getConfiguredSchemaMetaData
(),
props
,
rules
);
...
...
shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngine.java
浏览文件 @
9d3f7e41
...
...
@@ -17,42 +17,31 @@
package
org.apache.shardingsphere.infra.route.engine
;
import
lombok.RequiredArgsConstructor
;
import
org.apache.shardingsphere.infra.binder.LogicSQL
;
import
org.apache.shardingsphere.infra.config.properties.ConfigurationProperties
;
import
org.apache.shardingsphere.infra.route.SQLRouter
;
import
org.apache.shardingsphere.infra.route.UnconfiguredSchemaSQLRouter
;
import
org.apache.shardingsphere.infra.route.context.RouteContext
;
import
org.apache.shardingsphere.infra.route.engine.impl.AllSQLRouteExecutor
;
import
org.apache.shardingsphere.infra.route.engine.impl.PartialSQLRouteExecutor
;
import
org.apache.shardingsphere.infra.route.hook.SPIRoutingHook
;
import
org.apache.shardingsphere.infra.rule.ShardingSphereRule
;
import
org.apache.shardingsphere.infra.schema.ShardingSphereSchema
;
import
org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader
;
import
org.apache.shardingsphere.infra.spi.ordered.OrderedSPIRegistry
;
import
org.apache.shardingsphere.infra.binder.LogicSQL
;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement
;
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowTablesStatement
;
import
java.util.Collection
;
import
java.util.Map
;
import
java.util.Map.Entry
;
/**
* SQL route engine.
*/
@RequiredArgsConstructor
public
final
class
SQLRouteEngine
{
static
{
ShardingSphereServiceLoader
.
register
(
SQLRouter
.
class
);
}
private
final
Collection
<
ShardingSphereRule
>
rules
;
private
final
ConfigurationProperties
props
;
@SuppressWarnings
(
"rawtypes"
)
private
final
Map
<
ShardingSphereRule
,
SQLRouter
>
routers
;
private
final
SPIRoutingHook
routingHook
;
public
SQLRouteEngine
(
final
ConfigurationProperties
props
,
final
Collection
<
ShardingSphereRule
>
rules
)
{
this
.
props
=
props
;
routers
=
OrderedSPIRegistry
.
getRegisteredServices
(
rules
,
SQLRouter
.
class
);
routingHook
=
new
SPIRoutingHook
();
}
private
final
SPIRoutingHook
routingHook
=
new
SPIRoutingHook
();
/**
* Route SQL.
...
...
@@ -64,7 +53,8 @@ public final class SQLRouteEngine {
public
RouteContext
route
(
final
LogicSQL
logicSQL
,
final
ShardingSphereSchema
schema
)
{
routingHook
.
start
(
logicSQL
.
getSql
());
try
{
RouteContext
result
=
doRoute
(
logicSQL
,
schema
);
SQLRouteExecutor
executor
=
isNeedAllSchemas
(
logicSQL
.
getSqlStatementContext
().
getSqlStatement
())
?
new
AllSQLRouteExecutor
()
:
new
PartialSQLRouteExecutor
(
rules
,
props
);
RouteContext
result
=
executor
.
route
(
logicSQL
,
schema
);
routingHook
.
finishSuccess
(
result
,
schema
.
getMetaData
().
getSchemaMetaData
().
getConfiguredSchemaMetaData
());
return
result
;
// CHECKSTYLE:OFF
...
...
@@ -75,17 +65,8 @@ public final class SQLRouteEngine {
}
}
@SuppressWarnings
({
"unchecked"
,
"rawtypes"
})
private
RouteContext
doRoute
(
final
LogicSQL
logicSQL
,
final
ShardingSphereSchema
schema
)
{
RouteContext
result
=
new
RouteContext
();
for
(
Entry
<
ShardingSphereRule
,
SQLRouter
>
entry
:
routers
.
entrySet
())
{
if
(
result
.
getRouteUnits
().
isEmpty
())
{
result
=
entry
.
getValue
().
createRouteContext
(
logicSQL
,
schema
,
entry
.
getKey
(),
props
);
}
else
{
entry
.
getValue
().
decorateRouteContext
(
result
,
logicSQL
,
schema
,
entry
.
getKey
(),
props
);
}
}
new
UnconfiguredSchemaSQLRouter
().
decorate
(
result
,
logicSQL
,
schema
);
return
result
;
// TODO use dynamic config to judge UnconfiguredSchema
private
boolean
isNeedAllSchemas
(
final
SQLStatement
sqlStatement
)
{
return
sqlStatement
instanceof
MySQLShowTablesStatement
;
}
}
shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteExecutor.java
0 → 100644
浏览文件 @
9d3f7e41
/*
* 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.infra.route.engine
;
import
org.apache.shardingsphere.infra.binder.LogicSQL
;
import
org.apache.shardingsphere.infra.route.context.RouteContext
;
import
org.apache.shardingsphere.infra.schema.ShardingSphereSchema
;
/**
* SQL route executor.
*/
public
interface
SQLRouteExecutor
{
/**
* Route.
*
* @param logicSQL logic SQL
* @param schema ShardingSphere schema
* @return route context
*/
RouteContext
route
(
LogicSQL
logicSQL
,
ShardingSphereSchema
schema
);
}
shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/
UnconfiguredSchemaSQLRoute
r.java
→
shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/
engine/impl/AllSQLRouteExecuto
r.java
浏览文件 @
9d3f7e41
...
...
@@ -15,40 +15,38 @@
* limitations under the License.
*/
package
org.apache.shardingsphere.infra.route
;
package
org.apache.shardingsphere.infra.route
.engine.impl
;
import
org.apache.shardingsphere.infra.binder.LogicSQL
;
import
org.apache.shardingsphere.infra.route.context.RouteContext
;
import
org.apache.shardingsphere.infra.route.context.RouteMapper
;
import
org.apache.shardingsphere.infra.route.context.RouteUnit
;
import
org.apache.shardingsphere.infra.route.engine.SQLRouteExecutor
;
import
org.apache.shardingsphere.infra.schema.ShardingSphereSchema
;
import
org.apache.shardingsphere.infra.binder.LogicSQL
;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement
;
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowTablesStatement
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.LinkedHashSet
;
/**
*
Unconfigured schema SQL route
r.
*
All SQL route executo
r.
*/
public
final
class
UnconfiguredSchemaSQLRoute
r
{
public
final
class
AllSQLRouteExecutor
implements
SQLRouteExecuto
r
{
/**
* Decorate route context.
*
* @param routeContext route context
* @param logicSQL logic SQL
* @param schema ShardingSphere schema
*/
public
void
decorate
(
final
RouteContext
routeContext
,
final
LogicSQL
logicSQL
,
final
ShardingSphereSchema
schema
)
{
if
(
isNeedUnconfiguredSchema
(
logicSQL
.
getSqlStatementContext
().
getSqlStatement
()))
{
for
(
String
each
:
schema
.
getMetaData
().
getSchemaMetaData
().
getUnconfiguredSchemaMetaDataMap
().
keySet
())
{
routeContext
.
getRouteUnits
().
add
(
new
RouteUnit
(
new
RouteMapper
(
each
,
each
),
Collections
.
emptyList
()));
}
@Override
public
RouteContext
route
(
final
LogicSQL
logicSQL
,
final
ShardingSphereSchema
schema
)
{
RouteContext
result
=
new
RouteContext
();
for
(
String
each
:
getAllDataSourceNames
(
schema
))
{
result
.
getRouteUnits
().
add
(
new
RouteUnit
(
new
RouteMapper
(
each
,
each
),
Collections
.
emptyList
()));
}
return
result
;
}
// TODO use dynamic config to judge UnconfiguredSchema
private
boolean
isNeedUnconfiguredSchema
(
final
SQLStatement
sqlStatement
)
{
return
sqlStatement
instanceof
MySQLShowTablesStatement
;
private
Collection
<
String
>
getAllDataSourceNames
(
final
ShardingSphereSchema
schema
)
{
Collection
<
String
>
result
=
new
LinkedHashSet
<>();
for
(
Collection
<
String
>
each
:
schema
.
getMetaData
().
getTableAddressingMetaData
().
getTableDataSourceNamesMapper
().
values
())
{
result
.
addAll
(
each
);
}
return
result
;
}
}
shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java
0 → 100644
浏览文件 @
9d3f7e41
/*
* 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.infra.route.engine.impl
;
import
org.apache.shardingsphere.infra.binder.LogicSQL
;
import
org.apache.shardingsphere.infra.config.properties.ConfigurationProperties
;
import
org.apache.shardingsphere.infra.route.SQLRouter
;
import
org.apache.shardingsphere.infra.route.context.RouteContext
;
import
org.apache.shardingsphere.infra.route.engine.SQLRouteExecutor
;
import
org.apache.shardingsphere.infra.rule.ShardingSphereRule
;
import
org.apache.shardingsphere.infra.schema.ShardingSphereSchema
;
import
org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader
;
import
org.apache.shardingsphere.infra.spi.ordered.OrderedSPIRegistry
;
import
java.util.Collection
;
import
java.util.Map
;
import
java.util.Map.Entry
;
/**
* Partial SQL route executor.
*/
public
final
class
PartialSQLRouteExecutor
implements
SQLRouteExecutor
{
static
{
ShardingSphereServiceLoader
.
register
(
SQLRouter
.
class
);
}
private
final
ConfigurationProperties
props
;
@SuppressWarnings
(
"rawtypes"
)
private
final
Map
<
ShardingSphereRule
,
SQLRouter
>
routers
;
public
PartialSQLRouteExecutor
(
final
Collection
<
ShardingSphereRule
>
rules
,
final
ConfigurationProperties
props
)
{
this
.
props
=
props
;
routers
=
OrderedSPIRegistry
.
getRegisteredServices
(
rules
,
SQLRouter
.
class
);
}
@Override
@SuppressWarnings
({
"unchecked"
,
"rawtypes"
})
public
RouteContext
route
(
final
LogicSQL
logicSQL
,
final
ShardingSphereSchema
schema
)
{
RouteContext
result
=
new
RouteContext
();
for
(
Entry
<
ShardingSphereRule
,
SQLRouter
>
entry
:
routers
.
entrySet
())
{
if
(
result
.
getRouteUnits
().
isEmpty
())
{
result
=
entry
.
getValue
().
createRouteContext
(
logicSQL
,
schema
,
entry
.
getKey
(),
props
);
}
else
{
entry
.
getValue
().
decorateRouteContext
(
result
,
logicSQL
,
schema
,
entry
.
getKey
(),
props
);
}
}
return
result
;
}
}
shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngineTest.java
浏览文件 @
9d3f7e41
...
...
@@ -67,7 +67,7 @@ public final class SQLRouteEngineTest {
public
void
assertRouteSuccess
()
{
LogicSQL
logicSQL
=
new
LogicSQL
(
mock
(
SQLStatementContext
.
class
),
"SELECT 1"
,
Collections
.
emptyList
());
ShardingSphereSchema
schema
=
new
ShardingSphereSchema
(
"logic_schema"
,
Collections
.
emptyList
(),
Collections
.
singleton
(
new
RouteRuleFixture
()),
Collections
.
emptyMap
(),
metaData
);
SQLRouteEngine
sqlRouteEngine
=
new
SQLRouteEngine
(
props
,
Collections
.
singleton
(
new
RouteRuleFixture
())
);
SQLRouteEngine
sqlRouteEngine
=
new
SQLRouteEngine
(
Collections
.
singleton
(
new
RouteRuleFixture
()),
props
);
setSPIRoutingHook
(
sqlRouteEngine
);
RouteContext
actual
=
sqlRouteEngine
.
route
(
logicSQL
,
schema
);
assertThat
(
actual
.
getRouteUnits
().
size
(),
is
(
1
));
...
...
@@ -83,7 +83,7 @@ public final class SQLRouteEngineTest {
public
void
assertRouteFailure
()
{
LogicSQL
logicSQL
=
new
LogicSQL
(
mock
(
SQLStatementContext
.
class
),
"SELECT 1"
,
Collections
.
emptyList
());
ShardingSphereSchema
schema
=
new
ShardingSphereSchema
(
"logic_schema"
,
Collections
.
emptyList
(),
Collections
.
singleton
(
new
RouteRuleFixture
()),
Collections
.
emptyMap
(),
metaData
);
SQLRouteEngine
sqlRouteEngine
=
new
SQLRouteEngine
(
props
,
Collections
.
singleton
(
new
RouteFailureRuleFixture
())
);
SQLRouteEngine
sqlRouteEngine
=
new
SQLRouteEngine
(
Collections
.
singleton
(
new
RouteFailureRuleFixture
()),
props
);
setSPIRoutingHook
(
sqlRouteEngine
);
try
{
sqlRouteEngine
.
route
(
logicSQL
,
schema
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录