/
KeyspaceMetadata.java
251 lines (219 loc) · 8.46 KB
/
KeyspaceMetadata.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* Copyright DataStax, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 com.datastax.oss.driver.api.core.metadata.schema;
import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.type.DataType;
import com.datastax.oss.driver.api.core.type.UserDefinedType;
import com.datastax.oss.driver.internal.core.metadata.schema.ScriptBuilder;
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap;
import com.datastax.oss.driver.shaded.guava.common.collect.Iterables;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Map;
import java.util.Optional;
/** A keyspace in the schema metadata. */
public interface KeyspaceMetadata extends Describable {
@NonNull
CqlIdentifier getName();
/** Whether durable writes are set on this keyspace. */
boolean isDurableWrites();
/** Whether this keyspace is virtual */
boolean isVirtual();
/** The replication options defined for this keyspace. */
@NonNull
Map<String, String> getReplication();
@NonNull
Map<CqlIdentifier, ? extends TableMetadata> getTables();
@NonNull
default Optional<? extends TableMetadata> getTable(@NonNull CqlIdentifier tableId) {
return Optional.ofNullable(getTables().get(tableId));
}
/** Shortcut for {@link #getTable(CqlIdentifier) getTable(CqlIdentifier.fromCql(tableName))}. */
@NonNull
default Optional<? extends TableMetadata> getTable(@NonNull String tableName) {
return getTable(CqlIdentifier.fromCql(tableName));
}
@NonNull
Map<CqlIdentifier, ? extends ViewMetadata> getViews();
/** Gets the views based on a given table. */
@NonNull
default Map<CqlIdentifier, ? extends ViewMetadata> getViewsOnTable(
@NonNull CqlIdentifier tableId) {
ImmutableMap.Builder<CqlIdentifier, ViewMetadata> builder = ImmutableMap.builder();
for (ViewMetadata view : getViews().values()) {
if (view.getBaseTable().equals(tableId)) {
builder.put(view.getName(), view);
}
}
return builder.build();
}
@NonNull
default Optional<? extends ViewMetadata> getView(@NonNull CqlIdentifier viewId) {
return Optional.ofNullable(getViews().get(viewId));
}
/** Shortcut for {@link #getView(CqlIdentifier) getView(CqlIdentifier.fromCql(viewName))}. */
@NonNull
default Optional<? extends ViewMetadata> getView(@NonNull String viewName) {
return getView(CqlIdentifier.fromCql(viewName));
}
@NonNull
Map<CqlIdentifier, ? extends UserDefinedType> getUserDefinedTypes();
@NonNull
default Optional<? extends UserDefinedType> getUserDefinedType(@NonNull CqlIdentifier typeId) {
return Optional.ofNullable(getUserDefinedTypes().get(typeId));
}
/**
* Shortcut for {@link #getUserDefinedType(CqlIdentifier)
* getUserDefinedType(CqlIdentifier.fromCql(typeName))}.
*/
@NonNull
default Optional<? extends UserDefinedType> getUserDefinedType(@NonNull String typeName) {
return getUserDefinedType(CqlIdentifier.fromCql(typeName));
}
@NonNull
Map<FunctionSignature, ? extends FunctionMetadata> getFunctions();
@NonNull
default Optional<? extends FunctionMetadata> getFunction(
@NonNull FunctionSignature functionSignature) {
return Optional.ofNullable(getFunctions().get(functionSignature));
}
@NonNull
default Optional<? extends FunctionMetadata> getFunction(
@NonNull CqlIdentifier functionId, @NonNull Iterable<DataType> parameterTypes) {
return Optional.ofNullable(
getFunctions().get(new FunctionSignature(functionId, parameterTypes)));
}
/**
* Shortcut for {@link #getFunction(CqlIdentifier, Iterable)
* getFunction(CqlIdentifier.fromCql(functionName), parameterTypes)}.
*/
@NonNull
default Optional<? extends FunctionMetadata> getFunction(
@NonNull String functionName, @NonNull Iterable<DataType> parameterTypes) {
return getFunction(CqlIdentifier.fromCql(functionName), parameterTypes);
}
/**
* @param parameterTypes neither the individual types, nor the vararg array itself, can be null.
*/
@NonNull
default Optional<? extends FunctionMetadata> getFunction(
@NonNull CqlIdentifier functionId, @NonNull DataType... parameterTypes) {
return Optional.ofNullable(
getFunctions().get(new FunctionSignature(functionId, parameterTypes)));
}
/**
* Shortcut for {@link #getFunction(CqlIdentifier, DataType...)
* getFunction(CqlIdentifier.fromCql(functionName), parameterTypes)}.
*
* @param parameterTypes neither the individual types, nor the vararg array itself, can be null.
*/
@NonNull
default Optional<? extends FunctionMetadata> getFunction(
@NonNull String functionName, @NonNull DataType... parameterTypes) {
return getFunction(CqlIdentifier.fromCql(functionName), parameterTypes);
}
@NonNull
Map<FunctionSignature, ? extends AggregateMetadata> getAggregates();
@NonNull
default Optional<? extends AggregateMetadata> getAggregate(
@NonNull FunctionSignature aggregateSignature) {
return Optional.ofNullable(getAggregates().get(aggregateSignature));
}
@NonNull
default Optional<? extends AggregateMetadata> getAggregate(
@NonNull CqlIdentifier aggregateId, @NonNull Iterable<DataType> parameterTypes) {
return Optional.ofNullable(
getAggregates().get(new FunctionSignature(aggregateId, parameterTypes)));
}
/**
* Shortcut for {@link #getAggregate(CqlIdentifier, Iterable)
* getAggregate(CqlIdentifier.fromCql(aggregateName), parameterTypes)}.
*/
@NonNull
default Optional<? extends AggregateMetadata> getAggregate(
@NonNull String aggregateName, @NonNull Iterable<DataType> parameterTypes) {
return getAggregate(CqlIdentifier.fromCql(aggregateName), parameterTypes);
}
/**
* @param parameterTypes neither the individual types, nor the vararg array itself, can be null.
*/
@NonNull
default Optional<? extends AggregateMetadata> getAggregate(
@NonNull CqlIdentifier aggregateId, @NonNull DataType... parameterTypes) {
return Optional.ofNullable(
getAggregates().get(new FunctionSignature(aggregateId, parameterTypes)));
}
/**
* Shortcut for {@link #getAggregate(CqlIdentifier, DataType...)}
* getAggregate(CqlIdentifier.fromCql(aggregateName), parameterTypes)}.
*
* @param parameterTypes neither the individual types, nor the vararg array itself, can be null.
*/
@NonNull
default Optional<? extends AggregateMetadata> getAggregate(
@NonNull String aggregateName, @NonNull DataType... parameterTypes) {
return getAggregate(CqlIdentifier.fromCql(aggregateName), parameterTypes);
}
@NonNull
@Override
default String describe(boolean pretty) {
ScriptBuilder builder = new ScriptBuilder(pretty);
if (isVirtual()) {
builder.append("/* VIRTUAL ");
} else {
builder.append("CREATE ");
}
builder
.append("KEYSPACE ")
.append(getName())
.append(" WITH replication = { 'class' : '")
.append(getReplication().get("class"))
.append("'");
for (Map.Entry<String, String> entry : getReplication().entrySet()) {
if (!entry.getKey().equals("class")) {
builder
.append(", '")
.append(entry.getKey())
.append("': '")
.append(entry.getValue())
.append("'");
}
}
builder
.append(" } AND durable_writes = ")
.append(Boolean.toString(isDurableWrites()))
.append(";");
if (isVirtual()) {
builder.append(" */");
}
return builder.build();
}
@NonNull
@Override
default String describeWithChildren(boolean pretty) {
String createKeyspace = describe(pretty);
ScriptBuilder builder = new ScriptBuilder(pretty).append(createKeyspace);
for (Describable element :
Iterables.concat(
getUserDefinedTypes().values(),
getTables().values(),
getViews().values(),
getFunctions().values(),
getAggregates().values())) {
builder.forceNewLine(2).append(element.describeWithChildren(pretty));
}
return builder.build();
}
}