com.yeah.lbmall.dao.ykpay.PayOrderTableShardingAlgorithm.java Source code

Java tutorial

Introduction

Here is the source code for com.yeah.lbmall.dao.ykpay.PayOrderTableShardingAlgorithm.java

Source

/**
 * 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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.yeah.lbmall.dao.ykpay;

import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.SingleKeyTableShardingAlgorithm;
import com.google.common.collect.Range;

import java.text.DecimalFormat;
import java.util.Collection;
import java.util.LinkedHashSet;

public final class PayOrderTableShardingAlgorithm implements SingleKeyTableShardingAlgorithm<String> {

    @Override
    public String doEqualSharding(final Collection<String> availableTargetNames,
            final ShardingValue<String> shardingValue) {
        String f_order_id = shardingValue.getValue();
        for (String each : availableTargetNames) {
            //
            if (each.endsWith(f_order_id.substring(f_order_id.length() - 2, f_order_id.length()))) {
                return each;
            }
        }
        throw new UnsupportedOperationException();
    }

    @Override
    public Collection<String> doInSharding(final Collection<String> availableTargetNames,
            final ShardingValue<String> shardingValue) {
        Collection<String> result = new LinkedHashSet<>(availableTargetNames.size());
        Collection<String> values = shardingValue.getValues();
        for (String value : values) {
            for (String tableNames : availableTargetNames) {
                if (tableNames.endsWith(value.substring(value.length() - 2, value.length()))) {
                    result.add(tableNames);
                }
            }
        }
        return result;
    }

    @Override
    public Collection<String> doBetweenSharding(final Collection<String> availableTargetNames,
            final ShardingValue<String> shardingValue) {
        Collection<String> result = new LinkedHashSet<>(availableTargetNames.size());
        Range<String> range = shardingValue.getValueRange();

        int min = Integer.parseInt(range.lowerEndpoint().substring(range.lowerEndpoint().length() - 2,
                range.lowerEndpoint().length()));
        int max = Integer.parseInt(range.upperEndpoint().substring(range.upperEndpoint().length() - 2,
                range.upperEndpoint().length()));

        for (Integer i = min; i <= max; i++) {
            for (String each : availableTargetNames) {
                String STR_FORMAT = "00";
                DecimalFormat df = new DecimalFormat(STR_FORMAT);
                if (each.endsWith(df.format(i))) {
                    result.add(each);
                }
            }
        }
        return result;
    }

}