-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathDocumentDbDatabaseSchemaMetadata.java
234 lines (212 loc) · 8.76 KB
/
DocumentDbDatabaseSchemaMetadata.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Copyright <2021> Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.
*
*/
package software.amazon.documentdb.jdbc.metadata;
import com.mongodb.client.MongoClient;
import software.amazon.documentdb.jdbc.DocumentDbConnectionProperties;
import software.amazon.documentdb.jdbc.persist.DocumentDbSchemaSecurityException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Contains the metadata for a DocumentDB database including all of the collection and any
* virtual tables.
*/
public final class DocumentDbDatabaseSchemaMetadata {
public static final int VERSION_LATEST_OR_NEW = 0;
public static final int VERSION_NEW = -1;
public static final int VERSION_LATEST_OR_NONE = -2;
private final DocumentDbSchema schema;
/**
* Gets the schema name for this database metadata.
*
* @return a String representing the schema name.
*/
public String getSchemaName() {
return schema.getSchemaName();
}
/**
* Gets the version of this database metadata.
*
* @return a number representing the version of the database metadata.
*/
public int getSchemaVersion() {
return schema.getSchemaVersion();
}
public Map<String, DocumentDbSchemaTable> getTableSchemaMap() {
return schema.getTableMap();
}
/**
* Constructs a {@link DocumentDbDatabaseSchemaMetadata} instance from properties.
*
* @param schema the database schema.
*/
protected DocumentDbDatabaseSchemaMetadata(
final DocumentDbSchema schema) {
this.schema = schema;
}
/**
* Gets the latest or a new {@link DocumentDbDatabaseSchemaMetadata} instance based on the
* schemaName and properties. It uses a value of {@link DocumentDbDatabaseSchemaMetadata#VERSION_LATEST_OR_NEW}
* for the version to indicate to get the latest or create a new instance if none exists.
*
* @param properties the connection properties.
* @param schemaName the schema name.
* @param client the {@link MongoClient} client.
* @return a {@link DocumentDbDatabaseSchemaMetadata} instance.
*/
public static DocumentDbDatabaseSchemaMetadata get(
final DocumentDbConnectionProperties properties,
final String schemaName,
final DocumentDbMetadataService metadataService,
final MongoClient client)
throws SQLException {
return get(properties, schemaName, VERSION_LATEST_OR_NEW, metadataService, client);
}
/**
* Gets an existing {@link DocumentDbDatabaseSchemaMetadata} instance based on the schema name
* and version.
*
* @param properties the properties of the connection.
* @param schemaName the name of the schema.
* @param schemaVersion the version of the schema. A version number of
* {@link DocumentDbDatabaseSchemaMetadata#VERSION_LATEST_OR_NEW} indicates to get the latest
* or create a new instance.
* @param client the {@link MongoClient} client.
* @return a {@link DocumentDbDatabaseSchemaMetadata} instance if the schema and version exist,
* null otherwise.
*/
public static DocumentDbDatabaseSchemaMetadata get(
final DocumentDbConnectionProperties properties, final String schemaName,
final int schemaVersion,
final DocumentDbMetadataService metadataService,
final MongoClient client) throws SQLException {
// Try to get it from the service.
final DocumentDbDatabaseSchemaMetadata databaseMetadata;
final DocumentDbSchema schema = metadataService
.get(properties, schemaName, schemaVersion, client);
if (schema != null) {
// Setup lazy load based on table ID.
setSchemaGetTableFunction(properties, schemaName, schemaVersion, schema, metadataService, client);
databaseMetadata = new DocumentDbDatabaseSchemaMetadata(schema);
} else {
databaseMetadata = null;
}
return databaseMetadata;
}
/**
* Removes all versions of the schema for the given schema name.
*
* @param properties the connection properties.
* @param schemaName the name of the schema.
* @param client the {@link MongoClient} client.
*
* @throws SQLException if invalid connection properties.
*/
public static void remove(
final DocumentDbConnectionProperties properties,
final String schemaName,
final DocumentDbMetadataService metadataService,
final MongoClient client) throws SQLException {
metadataService.remove(properties, schemaName, client);
}
/**
* Removes the specific schema for the given schema name and version.
*
* @param properties the connection properties.
* @param schemaName the name of the schema.
* @param schemaVersion the version of the schema.
* @param client the {@link MongoClient} client.
*
* @throws SQLException if invalid connection properties.
*/
public static void remove(
final DocumentDbConnectionProperties properties,
final String schemaName,
final int schemaVersion,
final DocumentDbMetadataService metadataService,
final MongoClient client) throws SQLException {
metadataService.remove(properties, schemaName, schemaVersion, client);
}
/**
* Gets the list of all persisted schema.
*
* @param properties the connection properties.
* @param client the {@link MongoClient} client.
* @return a list of {@link DocumentDbSchema} schema.
* @throws SQLException if unable to connect.
*/
public static List<DocumentDbSchema> getSchemaList(
final DocumentDbConnectionProperties properties,
final DocumentDbMetadataService metadataService,
final MongoClient client) throws SQLException {
final List<DocumentDbSchema> schemas = metadataService
.getSchemaList(properties, client);
schemas.forEach(schema -> setSchemaGetTableFunction(
properties, schema.getSchemaName(), schema.getSchemaVersion(), schema, metadataService, client));
return schemas;
}
/**
* Updates schema with the given table schema.
*
* @param properties the connection properties.
* @param schemaName the name of the schema.
* @param schemaTables the collection of updated table schema.
* @param client the {@link MongoClient} client.
*
* @throws SQLException if unable to connect or other exception.
* @throws DocumentDbSchemaSecurityException if unable to write to the database due to
* unauthorized user.
*/
public static void update(
final DocumentDbConnectionProperties properties,
final String schemaName,
final Collection<DocumentDbSchemaTable> schemaTables,
final DocumentDbMetadataService metadataService,
final MongoClient client)
throws SQLException, DocumentDbSchemaSecurityException {
metadataService.update(properties, schemaName, schemaTables, client);
}
private static void setSchemaGetTableFunction(
final DocumentDbConnectionProperties properties,
final String schemaName,
final int schemaVersion,
final DocumentDbSchema schema,
final DocumentDbMetadataService metadataService,
final MongoClient client) {
schema.setGetTableFunction(
tableId -> metadataService
.getTable(properties, schemaName, schemaVersion, tableId, client),
remainingTableIds -> metadataService
.getTables(properties, schemaName, schemaVersion, remainingTableIds, client));
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof DocumentDbDatabaseSchemaMetadata)) {
return false;
}
final DocumentDbDatabaseSchemaMetadata metadata = (DocumentDbDatabaseSchemaMetadata) o;
return schema.equals(metadata.schema);
}
@Override
public int hashCode() {
return Objects.hash(schema);
}
}