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.metadata.database.schema.model;
19  
20  import com.cedarsoftware.util.CaseInsensitiveMap;
21  import lombok.AccessLevel;
22  import lombok.Getter;
23  import lombok.ToString;
24  import org.apache.shardingsphere.database.connector.core.metadata.database.enums.TableType;
25  import org.apache.shardingsphere.infra.metadata.identifier.ShardingSphereIdentifier;
26  
27  import java.util.ArrayList;
28  import java.util.Collection;
29  import java.util.Collections;
30  import java.util.LinkedHashMap;
31  import java.util.LinkedHashSet;
32  import java.util.List;
33  import java.util.Map;
34  import java.util.stream.Collectors;
35  
36  /**
37   * ShardingSphere table.
38   */
39  @Getter
40  @ToString
41  public final class ShardingSphereTable {
42      
43      private final String name;
44      
45      @Getter(AccessLevel.NONE)
46      private final Map<ShardingSphereIdentifier, ShardingSphereColumn> columns;
47      
48      private final List<ShardingSphereIdentifier> columnNames = new ArrayList<>();
49      
50      private final List<String> primaryKeyColumns = new ArrayList<>();
51      
52      private final List<String> visibleColumns = new ArrayList<>();
53      
54      private final Map<String, Integer> visibleColumnAndIndexMap = new CaseInsensitiveMap<>();
55      
56      @Getter(AccessLevel.NONE)
57      private final Map<ShardingSphereIdentifier, ShardingSphereIndex> indexes;
58      
59      @Getter(AccessLevel.NONE)
60      private final Map<ShardingSphereIdentifier, ShardingSphereConstraint> constraints;
61      
62      private final TableType type;
63      
64      public ShardingSphereTable(final String name, final Collection<ShardingSphereColumn> columns,
65                                 final Collection<ShardingSphereIndex> indexes, final Collection<ShardingSphereConstraint> constraints) {
66          this(name, columns, indexes, constraints, TableType.TABLE);
67      }
68      
69      public ShardingSphereTable(final String name, final Collection<ShardingSphereColumn> columns,
70                                 final Collection<ShardingSphereIndex> indexes, final Collection<ShardingSphereConstraint> constraints, final TableType type) {
71          this.name = name;
72          this.columns = createColumns(columns);
73          this.indexes = createIndexes(indexes);
74          this.constraints = createConstraints(constraints);
75          this.type = type;
76      }
77      
78      private Map<ShardingSphereIdentifier, ShardingSphereColumn> createColumns(final Collection<ShardingSphereColumn> columns) {
79          Map<ShardingSphereIdentifier, ShardingSphereColumn> result = new LinkedHashMap<>(columns.size(), 1F);
80          int index = 0;
81          for (ShardingSphereColumn each : columns) {
82              ShardingSphereIdentifier columnName = new ShardingSphereIdentifier(each.getName());
83              if (result.containsKey(columnName)) {
84                  continue;
85              }
86              result.put(columnName, each);
87              columnNames.add(columnName);
88              if (each.isPrimaryKey()) {
89                  primaryKeyColumns.add(each.getName());
90              }
91              if (each.isVisible()) {
92                  visibleColumns.add(each.getName());
93                  visibleColumnAndIndexMap.put(each.getName(), index++);
94              }
95          }
96          return result;
97      }
98      
99      private Map<ShardingSphereIdentifier, ShardingSphereIndex> createIndexes(final Collection<ShardingSphereIndex> indexes) {
100         return indexes.stream()
101                 .collect(Collectors.toMap(each -> new ShardingSphereIdentifier(each.getName()), each -> each, (oldValue, currentValue) -> currentValue, () -> new LinkedHashMap<>(indexes.size(), 1F)));
102     }
103     
104     private Map<ShardingSphereIdentifier, ShardingSphereConstraint> createConstraints(final Collection<ShardingSphereConstraint> constraints) {
105         return constraints.stream()
106                 .collect(Collectors.toMap(each -> new ShardingSphereIdentifier(each.getName()), each -> each, (oldValue, currentValue) -> currentValue,
107                         () -> new LinkedHashMap<>(constraints.size(), 1F)));
108     }
109     
110     /**
111      * Judge whether contains column.
112      *
113      * @param columnName column name
114      * @return contains column or not
115      */
116     public boolean containsColumn(final String columnName) {
117         return null != columnName && columns.containsKey(new ShardingSphereIdentifier(columnName));
118     }
119     
120     /**
121      * Get column.
122      *
123      * @param columnName column name
124      * @return column
125      */
126     public ShardingSphereColumn getColumn(final String columnName) {
127         return columns.get(new ShardingSphereIdentifier(columnName));
128     }
129     
130     /**
131      * Get all columns.
132      *
133      * @return columns
134      */
135     public Collection<ShardingSphereColumn> getAllColumns() {
136         return columns.values();
137     }
138     
139     /**
140      * Find column names If not existed from passing by column names.
141      *
142      * @param columnNames column names
143      * @return found column names
144      */
145     public Collection<String> findColumnNamesIfNotExistedFrom(final Collection<String> columnNames) {
146         if (columnNames.size() == columns.size()) {
147             return Collections.emptyList();
148         }
149         Collection<ShardingSphereIdentifier> result = new LinkedHashSet<>(columns.keySet());
150         result.removeAll(columnNames.stream().map(ShardingSphereIdentifier::new).collect(Collectors.toSet()));
151         return result.stream().map(ShardingSphereIdentifier::getValue).collect(Collectors.toList());
152     }
153     
154     /**
155      * Judge whether contains index.
156      *
157      * @param indexName index name
158      * @return contains index or not
159      */
160     public boolean containsIndex(final String indexName) {
161         return null != indexName && indexes.containsKey(new ShardingSphereIdentifier(indexName));
162     }
163     
164     /**
165      * Get all indexes.
166      *
167      * @return indexes
168      */
169     public Collection<ShardingSphereIndex> getAllIndexes() {
170         return indexes.values();
171     }
172     
173     /**
174      * Put index.
175      *
176      * @param index index
177      */
178     public void putIndex(final ShardingSphereIndex index) {
179         indexes.put(new ShardingSphereIdentifier(index.getName()), index);
180     }
181     
182     /**
183      * Remove index.
184      *
185      * @param indexName index name
186      */
187     public void removeIndex(final String indexName) {
188         indexes.remove(new ShardingSphereIdentifier(indexName));
189     }
190     
191     /**
192      * Get all constraint.
193      *
194      * @return constraint
195      */
196     public Collection<ShardingSphereConstraint> getAllConstraints() {
197         return constraints.values();
198     }
199 }