1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
58
59
60
61 public Collection<ShardingSphereTable> getAllTables() {
62 return tables.values();
63 }
64
65
66
67
68
69
70
71 public boolean containsTable(final String tableName) {
72 return tables.containsKey(new ShardingSphereIdentifier(tableName));
73 }
74
75
76
77
78
79
80
81 public ShardingSphereTable getTable(final String tableName) {
82 return tables.get(new ShardingSphereIdentifier(tableName));
83 }
84
85
86
87
88
89
90 public void putTable(final ShardingSphereTable table) {
91 tables.put(new ShardingSphereIdentifier(table.getName()), table);
92 }
93
94
95
96
97
98
99 public void removeTable(final String tableName) {
100 tables.remove(new ShardingSphereIdentifier(tableName));
101 }
102
103
104
105
106
107
108 public Collection<ShardingSphereView> getAllViews() {
109 return views.values();
110 }
111
112
113
114
115
116
117
118 public boolean containsView(final String viewName) {
119 return views.containsKey(new ShardingSphereIdentifier(viewName));
120 }
121
122
123
124
125
126
127
128 public ShardingSphereView getView(final String viewName) {
129 return views.get(new ShardingSphereIdentifier(viewName));
130 }
131
132
133
134
135
136
137 public void putView(final ShardingSphereView view) {
138 views.put(new ShardingSphereIdentifier(view.getName()), view);
139 }
140
141
142
143
144
145
146 public void removeView(final String viewName) {
147 views.remove(new ShardingSphereIdentifier(viewName));
148 }
149
150
151
152
153
154
155
156
157 public boolean containsIndex(final String tableName, final String indexName) {
158 return containsTable(tableName) && getTable(tableName).containsIndex(indexName);
159 }
160
161
162
163
164
165
166
167 public List<String> getVisibleColumnNames(final String tableName) {
168 return containsTable(tableName) ? getTable(tableName).getVisibleColumns() : Collections.emptyList();
169 }
170
171
172
173
174
175
176
177 public Map<String, Integer> getVisibleColumnAndIndexMap(final String tableName) {
178 return containsTable(tableName) ? getTable(tableName).getVisibleColumnAndIndexMap() : Collections.emptyMap();
179 }
180
181
182
183
184
185
186 public boolean isEmpty() {
187 return tables.isEmpty() && views.isEmpty();
188 }
189 }