/*
 * 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.routing.type.database;

import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;
import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule;
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy;
import com.dangdang.ddframe.rdb.sharding.hint.HintManagerHolder;
import com.dangdang.ddframe.rdb.sharding.hint.ShardingKey;
import com.dangdang.ddframe.rdb.sharding.constant.SQLType;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.Collections;

/**
 * 库路由.
 * 
 * @author gaohongtao
 */
@RequiredArgsConstructor
@Slf4j
public class DatabaseRouter {
    
    private final DataSourceRule dataSourceRule;
    
    private final DatabaseShardingStrategy databaseShardingStrategy;
    
    private final SQLType sqlType;
    
    /**
     * 根据Hint路由到库.
     * 
     * @return 库路由结果
     */
    public DatabaseRoutingResult route() {
        Optional<ShardingValue<?>> shardingValueOptional = HintManagerHolder.getDatabaseShardingValue(new ShardingKey(HintManagerHolder.DB_TABLE_NAME, HintManagerHolder.DB_COLUMN_NAME));
        Preconditions.checkState(shardingValueOptional.isPresent());
        log.debug("Before database sharding only db:{} sharding values: {}", dataSourceRule.getDataSourceNames(), shardingValueOptional.get());
        Collection<String> routedResult = databaseShardingStrategy.doStaticSharding(sqlType, dataSourceRule.getDataSourceNames(), Collections.<ShardingValue<?>>singleton(shardingValueOptional.get()));
        Preconditions.checkState(!routedResult.isEmpty(), "no database route info");
        log.debug("After database sharding only result: {}", routedResult);
        return new DatabaseRoutingResult(routedResult);
    }
}

@@ -1373,16 +1373,22 @@
 %0Apublic 
+final 
 class Da
@@ -1402,16 +1402,16 @@
 outer %7B%0A
-
     %0A   
@@ -1592,20 +1592,18 @@
     * %E6%A0%B9%E6%8D%AE
-Hint
+%E7%BA%BF%E7%B4%A2
 %E8%B7%AF%E7%94%B1%E5%88%B0%E5%BA%93.%0A  
@@ -1728,24 +1728,16 @@
 ingValue
-Optional
  = HintM
@@ -1905,24 +1905,16 @@
 ingValue
-Optional
 .isPrese
@@ -2043,32 +2043,24 @@
 hardingValue
-Optional
 .get());%0A   
@@ -2084,22 +2084,16 @@
 tring%3E r
-outedR
 esult = 
@@ -2233,24 +2233,16 @@
 ingValue
-Optional
 .get()))
@@ -2278,22 +2278,16 @@
 State(!r
-outedR
 esult.is
@@ -2384,22 +2384,16 @@
 : %7B%7D%22, r
-outedR
 esult);%0A
@@ -2438,14 +2438,8 @@
 lt(r
-outedR
 esul

refactor database routing