-
Notifications
You must be signed in to change notification settings - Fork 68
/
CqlModelBuilder.java
275 lines (230 loc) · 8.84 KB
/
CqlModelBuilder.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* Copyright (c) 2022 nosqlbench
*
* 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 io.nosqlbench.cqlgen.model;
import io.nosqlbench.cqlgen.core.CGWorkloadExporter;
import io.nosqlbench.cqlgen.generated.CqlParser;
import io.nosqlbench.cqlgen.generated.CqlParserBaseListener;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.tree.ErrorNode;
import org.antlr.v4.runtime.tree.ParseTree;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.List;
import java.util.stream.IntStream;
public class CqlModelBuilder extends CqlParserBaseListener {
private final static Logger logger = LogManager.getLogger(CGWorkloadExporter.APPNAME+"/buidler");
private final CGErrorListener errorListener;
private final CqlModel model;
private long counted;
private int colindex;
private CqlKeyspaceDef keyspace;
private CqlType usertype;
private CqlTable table;
public CqlModelBuilder(CGErrorListener errorListener) {
this.errorListener = errorListener;
this.model = new CqlModel(errorListener);
}
@Override
public void exitEveryRule(ParserRuleContext ctx) {
if ((counted++ & 0b11111111111111) == 0b10000000000000) {
logger.trace(() -> "parsed " + counted + " elements...");
}
}
@Override
public void visitErrorNode(ErrorNode node) {
System.out.println("error parsing: " + node.toString());
ParseTree parent = node.getParent();
String errorNodeType = parent.getClass().getSimpleName();
logger.info(() -> "PARSE ERROR: " + errorNodeType + "\n" + node.getSourceInterval());
super.visitErrorNode(node);
}
@Override
public void enterCreateKeyspace(CqlParser.CreateKeyspaceContext ctx) {
this.keyspace = new CqlKeyspaceDef();
}
@Override
public void exitCreateKeyspace(CqlParser.CreateKeyspaceContext ctx) {
saveKeyspace(
ctx.keyspace().getText()
);
}
public void saveKeyspace(String keyspaceName) {
this.keyspace.setKeyspaceName(keyspaceName);
keyspace.validate();
keyspace.setDefined();
model.addKeyspace(keyspace);
this.keyspace = null;
}
@Override
public void exitReplicationList(CqlParser.ReplicationListContext ctx) {
String repldata = textOf(ctx);
keyspace.setReplicationData(repldata);
}
@Override
public void enterCreateTable(CqlParser.CreateTableContext ctx) {
this.table = new CqlTable();
}
@Override
public void exitPrimaryKeyColumn(CqlParser.PrimaryKeyColumnContext ctx) {
super.exitPrimaryKeyColumn(ctx);
}
@Override
public void exitPrimaryKeyDefinition(CqlParser.PrimaryKeyDefinitionContext ctx) {
// PRIMARY KEY (PK)
if (ctx.singlePrimaryKey() != null) {
addPartitionKey(ctx.singlePrimaryKey().column().getText());
} else
// PRIMARY KEY (PK, ck, ...)
if (ctx.compositeKey() != null) {
if (ctx.compositeKey().partitionKeyList() != null) {
for (CqlParser.PartitionKeyContext pkctx : ctx.compositeKey().partitionKeyList().partitionKey()) {
addPartitionKey(pkctx.column().getText());
}
}
// PRIMARY KEY ((pk,...),(CK,...))
if (ctx.compositeKey().clusteringKeyList() != null) {
for (CqlParser.ClusteringKeyContext ccol : ctx.compositeKey().clusteringKeyList().clusteringKey()) {
addClusteringColumn(ccol.column().getText());
}
}
} else
// PRIMARY KEY ((PK), CK, ...)
if (ctx.compoundKey() != null) {
addPartitionKey(ctx.compoundKey().partitionKey().getText());
for (CqlParser.ClusteringKeyContext ccol : ctx.compoundKey().clusteringKeyList().clusteringKey()) {
addClusteringColumn(ccol.column().getText());
}
}
}
@Override
public void enterCreateType(CqlParser.CreateTypeContext ctx) {
this.usertype = new CqlType();
}
@Override
public void exitCreateType(CqlParser.CreateTypeContext ctx) {
String keyspace = ctx.keyspace().getText();
String name = ctx.type_().getText();
usertype.setName(name);
usertype.setDefined();
model.addType(keyspace, usertype);
usertype.validate();
usertype = null;
}
@Override
public void exitTypeMemberColumnList(CqlParser.TypeMemberColumnListContext ctx) {
List<CqlParser.ColumnContext> columns = ctx.column();
List<CqlParser.DataTypeContext> dataTypes = ctx.dataType();
for (int idx = 0; idx < columns.size(); idx++) {
addTypeField(
new CqlTypeColumn(columns.get(idx).getText(), dataTypes.get(idx).getText(), usertype)
);
}
}
@Override
public void exitSinglePrimaryKey(CqlParser.SinglePrimaryKeyContext ctx) {
super.exitSinglePrimaryKey(ctx);
}
@Override
public void exitCreateTable(CqlParser.CreateTableContext ctx) {
table.setName(ctx.table().getText());
model.addTable(ctx.keyspace().getText(), table);
table = null;
}
@Override
public void exitOrderDirection(CqlParser.OrderDirectionContext ctx) {
}
@Override
public void exitTableOptionItem(CqlParser.TableOptionItemContext ctx) {
if (table!=null) {
table.setCompactStorage(ctx.kwCompactStorage() != null);
} else {
logger.debug("table option item found with no table, this is likely for a materialized view");
}
}
@Override
public void exitDurableWrites(CqlParser.DurableWritesContext ctx) {
keyspace.setDurableWrites(Boolean.parseBoolean(ctx.booleanLiteral().getText()));
}
@Override
public void exitClusteringOrder(CqlParser.ClusteringOrderContext ctx) {
List<String> columns = ctx.children.stream()
.filter(c -> c instanceof CqlParser.ColumnContext)
.map(c -> c.getText())
.toList();
List<String> orders = ctx.children.stream()
.filter(c -> c instanceof CqlParser.OrderDirectionContext)
.map(c -> c.getText())
.toList();
if (table!=null) {
IntStream.range(0, columns.size())
.forEach(i -> table.addTableClusteringOrder(columns.get(i), orders.get(i)));
} else {
logger.debug("clustering order found, but not active table. This is likely for a materialized view.");
}
}
private String textOf(ParserRuleContext ctx) {
int startIndex = ctx.start.getStartIndex();
int stopIndex = ctx.stop.getStopIndex();
Interval interval = Interval.of(startIndex, stopIndex);
String text = ctx.start.getInputStream().getText(interval);
return text;
}
@Override
public void enterColumnDefinitionList(CqlParser.ColumnDefinitionListContext ctx) {
this.colindex = 0;
}
@Override
public void exitColumnDefinition(CqlParser.ColumnDefinitionContext ctx) {
if (usertype != null) {
CqlTypeColumn coldef = new CqlTypeColumn(
ctx.column().getText(),
ctx.dataType().getText(),
usertype
);
usertype.addColumn(coldef);
} else if (table != null) {
CqlTableColumn coldef = new CqlTableColumn(
ctx.column().getText(),
ctx.dataType().getText(),
table
);
table.addcolumnDef(coldef);
if (ctx.primaryKeyColumn() != null) {
table.addPartitionKey(ctx.column().getText());
}
}
}
@Override
public String toString() {
return model.toString();
}
public CqlModel getModel() {
return model;
}
public List<String> getErrors() {
return model.getErrors();
}
public void addPartitionKey(String partitionKey) {
table.addPartitionKey(partitionKey);
}
public void addClusteringColumn(String ccolumn) {
table.addClusteringColumn(ccolumn);
}
public void addTypeField(CqlTypeColumn coldef) {
this.usertype.addColumn(coldef);
}
}