All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.taotao.boot.data.shardingsphere.algorithm.ShardingAlgorithmDB Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
 *
 * 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
 *
 *      https://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 com.taotao.boot.data.shardingsphere.algorithm;

import com.taotao.boot.common.utils.log.LogUtils;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;
import java.util.Collection;

/**
 * 按月分片算法
 */
@Configuration
public class ShardingAlgorithmDB implements StandardShardingAlgorithm {

    /**
     * 精准分片
     * @param availableTargetNames available data sources or table names
     * @param shardingValue sharding value
     * @return
     */
    @Override
    public String doSharding(
            Collection availableTargetNames, PreciseShardingValue shardingValue) {

        LogUtils.info("[分库算法开始]");
        // 真实节点
        availableTargetNames.stream().forEach((item) -> {
            LogUtils.info("[存在的库名<{}>]", item);
        });

        LogUtils.info("[表名<{}> 列名<{}>]", shardingValue.getLogicTableName(), shardingValue.getColumnName());

        // 精确分片
        LogUtils.info("[分库列的值<{}>]", shardingValue.getValue());

        //        long orderId = shardingValue.getValue();
        //
        //        // 效果等同于 orderId % 2
        //        long db_index = orderId & 1;

        for (String each : availableTargetNames) {
            LogUtils.info("[分库列的值<{}>]", shardingValue.getValue());
            if (each.equals("db0")) {
                return "db0";
            }
        }

        throw new IllegalArgumentException();
    }

    /**
     * 范围分片
     * @param availableTargetNames available data sources or table names
     * @param shardingValue sharding value
     * @return
     */
    @Override
    public Collection doSharding(
            Collection availableTargetNames, RangeShardingValue shardingValue) {
        return availableTargetNames;
    }

    @Override
    //    public String getType() {
    //        return ShardingConstants.MONTH_SHARD;
    //    }
    public String getType() {
        return "COMMON_SHARD";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy