forked from apache/shardingsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
17 changed files
with
534 additions
and
361 deletions.
There are no files selected for viewing
146 changes: 146 additions & 0 deletions
146
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/api/HintManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/** | ||
* Copyright 1999-2015 dangdang.com. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* </p> | ||
*/ | ||
|
||
package com.dangdang.ddframe.rdb.sharding.api; | ||
|
||
import com.dangdang.ddframe.rdb.sharding.hint.HintManagerHolder; | ||
import com.dangdang.ddframe.rdb.sharding.hint.ShardingKey; | ||
import com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition; | ||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.BoundType; | ||
import com.google.common.collect.Range; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* 通过线索传递分片值的管理器. | ||
* | ||
* @author gaohongtao | ||
* @author zhangliang | ||
*/ | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public final class HintManager implements AutoCloseable { | ||
|
||
private final Map<ShardingKey, ShardingValue<?>> databaseShardingValues = new HashMap<>(); | ||
|
||
private final Map<ShardingKey, ShardingValue<?>> tableShardingValues = new HashMap<>(); | ||
|
||
/** | ||
* 获取线索分片管理器实例. | ||
* | ||
* @return 线索分片管理器实例 | ||
*/ | ||
public static HintManager getInstance() { | ||
HintManager result = new HintManager(); | ||
HintManagerHolder.setHintManager(result); | ||
return result; | ||
} | ||
|
||
/** | ||
* 添加分库分片值. | ||
* | ||
* <p>分片操作符为等号.</p> | ||
* | ||
* @param logicTable 逻辑表名称 | ||
* @param shardingColumn 分片键 | ||
* @param value 分片值 | ||
*/ | ||
public void addDatabaseShardingValue(final String logicTable, final String shardingColumn, final Comparable<?> value) { | ||
addDatabaseShardingValue(logicTable, shardingColumn, Condition.BinaryOperator.EQUAL, value); | ||
} | ||
|
||
/** | ||
* 添加分库分片值. | ||
* | ||
* @param logicTable 逻辑表名称 | ||
* @param shardingColumn 分片键 | ||
* @param binaryOperator 分片操作符 | ||
* @param values 分片值 | ||
*/ | ||
public void addDatabaseShardingValue(final String logicTable, final String shardingColumn, final Condition.BinaryOperator binaryOperator, final Comparable<?>... values) { | ||
databaseShardingValues.put(new ShardingKey(logicTable, shardingColumn), getShardingValue(shardingColumn, binaryOperator, values)); | ||
} | ||
|
||
/** | ||
* 添加分表分片值. | ||
* | ||
* <p>分片操作符为等号.</p> | ||
* | ||
* @param logicTable 逻辑表名称 | ||
* @param shardingColumn 分片键 | ||
* @param value 分片值 | ||
*/ | ||
public void addTableShardingValue(final String logicTable, final String shardingColumn, final Comparable<?> value) { | ||
addTableShardingValue(logicTable, shardingColumn, Condition.BinaryOperator.EQUAL, value); | ||
} | ||
|
||
/** | ||
* 添加分表分片值. | ||
* | ||
* @param logicTable 逻辑表名称 | ||
* @param shardingColumn 分片键 | ||
* @param binaryOperator 分片操作符 | ||
* @param values 分片值 | ||
*/ | ||
public void addTableShardingValue(final String logicTable, final String shardingColumn, final Condition.BinaryOperator binaryOperator, final Comparable<?>... values) { | ||
tableShardingValues.put(new ShardingKey(logicTable, shardingColumn), getShardingValue(shardingColumn, binaryOperator, values)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private ShardingValue getShardingValue(final String shardingColumn, final Condition.BinaryOperator binaryOperator, final Comparable<?>[] values) { | ||
Preconditions.checkArgument(null != values && values.length > 0); | ||
switch (binaryOperator) { | ||
case EQUAL: | ||
return new ShardingValue<Comparable<?>>(shardingColumn, values[0]); | ||
case IN: | ||
return new ShardingValue(shardingColumn, Arrays.asList(values)); | ||
case BETWEEN: | ||
return new ShardingValue(shardingColumn, Range.range(values[0], BoundType.CLOSED, values[1], BoundType.CLOSED)); | ||
default: | ||
throw new UnsupportedOperationException(binaryOperator.getExpression()); | ||
} | ||
} | ||
|
||
/** | ||
* 获取分库分片键值. | ||
* | ||
* @param shardingKey 分片键 | ||
* @return 分库分片键值 | ||
*/ | ||
public ShardingValue<?> getDatabaseShardingValue(final ShardingKey shardingKey) { | ||
return databaseShardingValues.get(shardingKey); | ||
} | ||
|
||
/** | ||
* 获取分表分片键值. | ||
* | ||
* @param shardingKey 分片键 | ||
* @return 分表分片键值 | ||
*/ | ||
public ShardingValue<?> getTableShardingValue(final ShardingKey shardingKey) { | ||
return tableShardingValues.get(shardingKey); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
HintManagerHolder.clear(); | ||
} | ||
} |
166 changes: 0 additions & 166 deletions
166
...bc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/api/HintShardingValueManager.java
This file was deleted.
Oops, something went wrong.
81 changes: 81 additions & 0 deletions
81
...ing-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/hint/HintManagerHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* Copyright 1999-2015 dangdang.com. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* </p> | ||
*/ | ||
|
||
package com.dangdang.ddframe.rdb.sharding.hint; | ||
|
||
import com.dangdang.ddframe.rdb.sharding.api.HintManager; | ||
import com.dangdang.ddframe.rdb.sharding.api.ShardingValue; | ||
import com.google.common.base.Optional; | ||
import com.google.common.base.Preconditions; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* 线索分片管理器的本地线程持有者. | ||
* | ||
* @author zhangliang | ||
*/ | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public final class HintManagerHolder { | ||
|
||
private static final ThreadLocal<HintManager> HINT_MANAGER_HOLDER = new ThreadLocal<>(); | ||
|
||
/** | ||
* 设置线索分片管理器. | ||
* | ||
* @param hintManager 线索分片管理器 | ||
*/ | ||
public static void setHintManager(HintManager hintManager) { | ||
Preconditions.checkState(null == HINT_MANAGER_HOLDER.get(), "HintManagerHolder has previous value, please clear first."); | ||
HINT_MANAGER_HOLDER.set(hintManager); | ||
} | ||
|
||
/** | ||
* 判断当前线程是否使用线索分片. | ||
* @return 当前线程是否使用线索分片 | ||
*/ | ||
public static boolean isUseHint() { | ||
return null != HINT_MANAGER_HOLDER.get(); | ||
} | ||
|
||
/** | ||
* 获取分库分片键值. | ||
* | ||
* @param shardingKey 分片键 | ||
* @return 分库分片键值 | ||
*/ | ||
public static Optional<ShardingValue<?>> getDatabaseShardingValue(final ShardingKey shardingKey) { | ||
return isUseHint() ? Optional.<ShardingValue<?>>fromNullable(HINT_MANAGER_HOLDER.get().getDatabaseShardingValue(shardingKey)) : Optional.<ShardingValue<?>>absent(); | ||
} | ||
|
||
/** | ||
* 获取分表分片键值. | ||
* | ||
* @param shardingKey 分片键 | ||
* @return 分表分片键值 | ||
*/ | ||
public static Optional<ShardingValue<?>> getTableShardingValue(final ShardingKey shardingKey) { | ||
return isUseHint() ? Optional.<ShardingValue<?>>fromNullable(HINT_MANAGER_HOLDER.get().getTableShardingValue(shardingKey)) : Optional.<ShardingValue<?>>absent(); | ||
} | ||
|
||
/** | ||
* 清理线索分片管理器的本地线程持有者 | ||
*/ | ||
public static void clear() { | ||
HINT_MANAGER_HOLDER.remove(); | ||
} | ||
} |
Oops, something went wrong.