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

Java tutorial

Introduction

Here is the source code for com.yeah.lbmall.dao.ykpay.PayOrderDatabaseShardingAlgorithm.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.database.SingleKeyDatabaseShardingAlgorithm;
import com.google.common.collect.Range;

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

public final class PayOrderDatabaseShardingAlgorithm implements SingleKeyDatabaseShardingAlgorithm<String> {

    @Override
    public String doEqualSharding(final Collection<String> availableTargetNames,
            final ShardingValue<String> shardingValue) {

        String f_order_id = shardingValue.getValue();
        String dbName = "dataSource-ykpay" + f_order_id.substring(f_order_id.length() - 3, f_order_id.length() - 2);

        for (String each : availableTargetNames) {
            if (dbName.equals(each)) {
                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) {

            String dbName = "dataSource-ykpay" + value.substring(value.length() - 3, value.length() - 2);

            for (String dataSourceName : availableTargetNames) {
                if (dbName.equals(dataSourceName)) {
                    result.add(dataSourceName);
                }
            }
        }
        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() - 3,
                range.lowerEndpoint().length() - 2));
        int max = Integer.parseInt(range.upperEndpoint().substring(range.upperEndpoint().length() - 3,
                range.upperEndpoint().length() - 2));
        for (Integer i = min; i <= max; i++) {

            String dbName = "dataSource-ykpay" + i;
            for (String each : availableTargetNames) {
                if (dbName.equals(each)) {
                    result.add(each);
                }
            }
        }
        return result;
    }

}