RangeDynamicModuloTableShardingAlgorithm.java 1.5 KB
Newer Older
T
terrymanu 已提交
1
/*
T
fix #53  
terrymanu 已提交
2 3 4 5 6
 * 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
T
terrymanu 已提交
7
 *
T
fix #53  
terrymanu 已提交
8
 *      http://www.apache.org/licenses/LICENSE-2.0
T
terrymanu 已提交
9
 *
T
fix #53  
terrymanu 已提交
10 11 12 13 14 15 16 17 18 19
 * 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.integrate.fixture;

T
terrymanu 已提交
20
import com.dangdang.ddframe.rdb.sharding.api.strategy.RangeShardingValue;
21
import com.dangdang.ddframe.rdb.sharding.routing.strategy.standard.RangeShardingAlgorithm;
T
fix #53  
terrymanu 已提交
22 23 24 25 26 27 28
import com.google.common.collect.Range;
import lombok.RequiredArgsConstructor;

import java.util.Collection;
import java.util.LinkedHashSet;

@RequiredArgsConstructor
29
public final class RangeDynamicModuloTableShardingAlgorithm implements RangeShardingAlgorithm<Integer> {
T
fix #53  
terrymanu 已提交
30 31
    
    @Override
32
    public Collection<String> doSharding(final Collection<String> availableTargetNames, final RangeShardingValue<Integer> shardingValue) {
T
fix #53  
terrymanu 已提交
33 34 35
        Collection<String> result = new LinkedHashSet<>(availableTargetNames.size());
        Range<Integer> range = shardingValue.getValueRange();
        for (Integer i = range.lowerEndpoint(); i <= range.upperEndpoint(); i++) {
36
            result.add("t_order_" + i % 10);
T
fix #53  
terrymanu 已提交
37 38 39 40
        }
        return result;
    }
}