View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.infra.hint;
19  
20  import com.google.common.base.Joiner;
21  import com.google.common.collect.ArrayListMultimap;
22  import com.google.common.collect.Multimap;
23  import lombok.Getter;
24  import lombok.Setter;
25  
26  import java.util.Collection;
27  import java.util.LinkedHashSet;
28  import java.util.Optional;
29  
30  /**
31   * Hint value context.
32   */
33  @Getter
34  @Setter
35  public final class HintValueContext {
36      
37      private final Multimap<String, Comparable<?>> shardingDatabaseValues = ArrayListMultimap.create();
38      
39      private final Multimap<String, Comparable<?>> shardingTableValues = ArrayListMultimap.create();
40      
41      private final Collection<String> disableAuditNames = new LinkedHashSet<>();
42      
43      private String dataSourceName = "";
44      
45      private boolean databaseShardingOnly;
46      
47      private boolean writeRouteOnly;
48      
49      private boolean useTraffic;
50      
51      private boolean skipSQLRewrite;
52      
53      private boolean shadow;
54      
55      /**
56       * Find hint data source name.
57       *
58       * @return data source name
59       */
60      public Optional<String> findHintDataSourceName() {
61          return dataSourceName.isEmpty() ? Optional.empty() : Optional.of(dataSourceName);
62      }
63      
64      /**
65       * Judge contains hint sharding databases value or not.
66       *
67       * @param tableName table name
68       * @return contains hint sharding databases value or not
69       */
70      public boolean containsHintShardingDatabaseValue(final String tableName) {
71          String key = Joiner.on(".").join(tableName.toUpperCase(), SQLHintPropertiesKey.SHARDING_DATABASE_VALUE_KEY.getKey());
72          return shardingDatabaseValues.containsKey(key) || shardingDatabaseValues.containsKey(SQLHintPropertiesKey.SHARDING_DATABASE_VALUE_KEY.getKey());
73      }
74      
75      /**
76       * Judge contains hint sharding table value or not.
77       *
78       * @param tableName table name
79       * @return Contains hint sharding table value or not
80       */
81      public boolean containsHintShardingTableValue(final String tableName) {
82          String key = Joiner.on(".").join(tableName.toUpperCase(), SQLHintPropertiesKey.SHARDING_TABLE_VALUE_KEY.getKey());
83          return shardingTableValues.containsKey(key) || shardingTableValues.containsKey(SQLHintPropertiesKey.SHARDING_TABLE_VALUE_KEY.getKey());
84      }
85      
86      /**
87       * Judge contains hint sharding value or not.
88       *
89       * @param tableName table name
90       * @return Contains hint sharding value or not
91       */
92      public boolean containsHintShardingValue(final String tableName) {
93          return containsHintShardingDatabaseValue(tableName) || containsHintShardingTableValue(tableName);
94      }
95      
96      /**
97       * Get hint sharding table value.
98       *
99       * @param tableName table name
100      * @return sharding table value
101      */
102     public Collection<Comparable<?>> getHintShardingTableValue(final String tableName) {
103         String key = String.join(".", tableName.toUpperCase(), SQLHintPropertiesKey.SHARDING_TABLE_VALUE_KEY.getKey());
104         return shardingTableValues.containsKey(key)
105                 ? shardingTableValues.get(key)
106                 : shardingTableValues.get(SQLHintPropertiesKey.SHARDING_TABLE_VALUE_KEY.getKey());
107     }
108     
109     /**
110      * Get hint sharding database value.
111      *
112      * @param tableName table name
113      * @return sharding database value
114      */
115     public Collection<Comparable<?>> getHintShardingDatabaseValue(final String tableName) {
116         String key = String.join(".", tableName.toUpperCase(), SQLHintPropertiesKey.SHARDING_DATABASE_VALUE_KEY.getKey());
117         return shardingDatabaseValues.containsKey(key)
118                 ? shardingDatabaseValues.get(key)
119                 : shardingDatabaseValues.get(SQLHintPropertiesKey.SHARDING_DATABASE_VALUE_KEY.getKey());
120     }
121 }