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 lombok.Getter;
21  import org.apache.shardingsphere.infra.metadata.identifier.ShardingSphereIdentifier;
22  
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.concurrent.ConcurrentHashMap;
28  
29  /**
30   * ShardingSphere schema.
31   */
32  public final class ShardingSphereSchema {
33      
34      @Getter
35      private final String name;
36      
37      private final Map<ShardingSphereIdentifier, ShardingSphereTable> tables;
38      
39      private final Map<ShardingSphereIdentifier, ShardingSphereView> views;
40      
41      @SuppressWarnings("CollectionWithoutInitialCapacity")
42      public ShardingSphereSchema(final String name) {
43          this.name = name;
44          tables = new ConcurrentHashMap<>();
45          views = new ConcurrentHashMap<>();
46      }
47      
48      public ShardingSphereSchema(final String name, final Collection<ShardingSphereTable> tables, final Collection<ShardingSphereView> views) {
49          this.name = name;
50          this.tables = new ConcurrentHashMap<>(tables.size(), 1F);
51          this.views = new ConcurrentHashMap<>(views.size(), 1F);
52          tables.forEach(each -> this.tables.put(new ShardingSphereIdentifier(each.getName()), each));
53          views.forEach(each -> this.views.put(new ShardingSphereIdentifier(each.getName()), each));
54      }
55      
56      /**
57       * Get all tables.
58       *
59       * @return all tables
60       */
61      public Collection<ShardingSphereTable> getAllTables() {
62          return tables.values();
63      }
64      
65      /**
66       * Judge whether contains table.
67       *
68       * @param tableName table name
69       * @return contains table or not
70       */
71      public boolean containsTable(final String tableName) {
72          return tables.containsKey(new ShardingSphereIdentifier(tableName));
73      }
74      
75      /**
76       * Get table.
77       *
78       * @param tableName table name
79       * @return table
80       */
81      public ShardingSphereTable getTable(final String tableName) {
82          return tables.get(new ShardingSphereIdentifier(tableName));
83      }
84      
85      /**
86       * Add table.
87       *
88       * @param table table
89       */
90      public void putTable(final ShardingSphereTable table) {
91          tables.put(new ShardingSphereIdentifier(table.getName()), table);
92      }
93      
94      /**
95       * Remove table.
96       *
97       * @param tableName table name
98       */
99      public void removeTable(final String tableName) {
100         tables.remove(new ShardingSphereIdentifier(tableName));
101     }
102     
103     /**
104      * Get all views.
105      *
106      * @return all views
107      */
108     public Collection<ShardingSphereView> getAllViews() {
109         return views.values();
110     }
111     
112     /**
113      * Judge whether contains view.
114      *
115      * @param viewName view name
116      * @return contains view or not
117      */
118     public boolean containsView(final String viewName) {
119         return views.containsKey(new ShardingSphereIdentifier(viewName));
120     }
121     
122     /**
123      * Get view.
124      *
125      * @param viewName view name
126      * @return view
127      */
128     public ShardingSphereView getView(final String viewName) {
129         return views.get(new ShardingSphereIdentifier(viewName));
130     }
131     
132     /**
133      * Add view.
134      *
135      * @param view view
136      */
137     public void putView(final ShardingSphereView view) {
138         views.put(new ShardingSphereIdentifier(view.getName()), view);
139     }
140     
141     /**
142      * Remove view.
143      *
144      * @param viewName view name
145      */
146     public void removeView(final String viewName) {
147         views.remove(new ShardingSphereIdentifier(viewName));
148     }
149     
150     /**
151      * Judge whether contains index.
152      *
153      * @param tableName table name
154      * @param indexName index name
155      * @return contains index or not
156      */
157     public boolean containsIndex(final String tableName, final String indexName) {
158         return containsTable(tableName) && getTable(tableName).containsIndex(indexName);
159     }
160     
161     /**
162      * Get visible column names.
163      *
164      * @param tableName table name
165      * @return visible column names
166      */
167     public List<String> getVisibleColumnNames(final String tableName) {
168         return containsTable(tableName) ? getTable(tableName).getVisibleColumns() : Collections.emptyList();
169     }
170     
171     /**
172      * Get visible column and index map.
173      *
174      * @param tableName table name
175      * @return visible column and index map
176      */
177     public Map<String, Integer> getVisibleColumnAndIndexMap(final String tableName) {
178         return containsTable(tableName) ? getTable(tableName).getVisibleColumnAndIndexMap() : Collections.emptyMap();
179     }
180     
181     /**
182      * Whether empty schema.
183      *
184      * @return empty schema or not
185      */
186     public boolean isEmpty() {
187         return tables.isEmpty() && views.isEmpty();
188     }
189 }