moderate refactoring of module builder, better separation from model

This commit is contained in:
Jonathan Shook
2022-08-09 12:54:17 -05:00
parent f8c4fbe220
commit ae42142336
6 changed files with 105 additions and 89 deletions

View File

@@ -21,26 +21,22 @@ import io.nosqlbench.api.labels.Labeled;
import java.util.Map; import java.util.Map;
/**
* Not anchored to a parent, as it could be a table or a type.
* All access to these must be through their parent element.
*/
public abstract class CqlColumnBase implements NBNamedElement, Labeled { public abstract class CqlColumnBase implements NBNamedElement, Labeled {
private String name; private String name;
private String typedef; private String typedef;
private FieldPosition position; private ColumnPosition position=ColumnPosition.NonKey;
public CqlColumnBase(String colname, String typedef) { protected CqlColumnBase(String colname, String typedef) {
this.typedef = typedef; this.typedef = typedef;
this.name = colname; this.name = colname;
} }
public void setPosition(FieldPosition position) { public void setPosition(ColumnPosition position) {
this.position = position; this.position = position;
} }
public FieldPosition getPosition() { public ColumnPosition getPosition() {
return this.position; return this.position;
} }
public void setTypeDef(String type) { public void setTypeDef(String type) {
@@ -68,7 +64,6 @@ public abstract class CqlColumnBase implements NBNamedElement, Labeled {
public Map<String, String> getLabels() { public Map<String, String> getLabels() {
return Map.of( return Map.of(
"name", name, "name", name,
"typedef", typedef,
"type", "column" "type", "column"
); );
} }

View File

@@ -38,11 +38,7 @@ public class CqlModelBuilder extends CqlParserBaseListener {
private int colindex; private int colindex;
private CqlKeyspaceDef keyspace; private CqlKeyspaceDef keyspace;
private CqlType usertype; private CqlType usertype;
transient CqlTable table; private CqlTable table;
public CqlModelBuilder(CGErrorListener errorListener) { public CqlModelBuilder(CGErrorListener errorListener) {
this.errorListener = errorListener; this.errorListener = errorListener;
@@ -62,14 +58,14 @@ public class CqlModelBuilder extends CqlParserBaseListener {
ParseTree parent = node.getParent(); ParseTree parent = node.getParent();
String errorNodeType = parent.getClass().getSimpleName(); String errorNodeType = parent.getClass().getSimpleName();
logger.info("PARSE ERROR: " + errorNodeType + "\n"+ node.getSourceInterval()); logger.info("PARSE ERROR: " + errorNodeType + "\n" + node.getSourceInterval());
super.visitErrorNode(node); super.visitErrorNode(node);
} }
@Override @Override
public void enterCreateKeyspace(CqlParser.CreateKeyspaceContext ctx) { public void enterCreateKeyspace(CqlParser.CreateKeyspaceContext ctx) {
this.keyspace=new CqlKeyspaceDef(); this.keyspace = new CqlKeyspaceDef();
} }
@Override @Override
@@ -106,25 +102,31 @@ public class CqlModelBuilder extends CqlParserBaseListener {
@Override @Override
public void exitPrimaryKeyDefinition(CqlParser.PrimaryKeyDefinitionContext ctx) { public void exitPrimaryKeyDefinition(CqlParser.PrimaryKeyDefinitionContext ctx) {
// PRIMARY KEY (PK)
if (ctx.singlePrimaryKey() != null) { if (ctx.singlePrimaryKey() != null) {
addPartitionKey(ctx.singlePrimaryKey().column().getText()); addPartitionKey(ctx.singlePrimaryKey().column().getText());
} else if (ctx.compositeKey() != null) { } else
if (ctx.compositeKey().partitionKeyList() != null) { // PRIMARY KEY (PK, ck, ...)
for (CqlParser.PartitionKeyContext pkctx : ctx.compositeKey().partitionKeyList().partitionKey()) { if (ctx.compositeKey() != null) {
addPartitionKey(pkctx.column().getText()); 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) { if (ctx.compositeKey().clusteringKeyList() != null) {
for (CqlParser.ClusteringKeyContext ccol : ctx.compositeKey().clusteringKeyList().clusteringKey()) { 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()); addClusteringColumn(ccol.column().getText());
} }
} }
} else if (ctx.compoundKey() != null) {
addPartitionKey(ctx.compoundKey().partitionKey().getText());
for (CqlParser.ClusteringKeyContext ccol : ctx.compoundKey().clusteringKeyList().clusteringKey()) {
addClusteringColumn(ccol.column().getText());
}
}
} }
@Override @Override
@@ -140,21 +142,18 @@ public class CqlModelBuilder extends CqlParserBaseListener {
usertype.setDefined(); usertype.setDefined();
model.addType(keyspace, usertype); model.addType(keyspace, usertype);
usertype.validate(); usertype.validate();
usertype=null; usertype = null;
} }
// HERE consider building hierarchic type model
@Override @Override
public void exitTypeMemberColumnList(CqlParser.TypeMemberColumnListContext ctx) { public void exitTypeMemberColumnList(CqlParser.TypeMemberColumnListContext ctx) {
List<CqlParser.ColumnContext> columns = ctx.column(); List<CqlParser.ColumnContext> columns = ctx.column();
List<CqlParser.DataTypeContext> dataTypes = ctx.dataType(); List<CqlParser.DataTypeContext> dataTypes = ctx.dataType();
for (int idx = 0; idx < columns.size(); idx++) { for (int idx = 0; idx < columns.size(); idx++) {
addTypeField( addTypeField(
new CqlTypeColumn(columns.get(idx).getText(),dataTypes.get(idx).getText())); new CqlTypeColumn(columns.get(idx).getText(), dataTypes.get(idx).getText(), usertype)
);
} }
// dataTypes.get(0).dataType().get(0).dataType().get(0)
} }
@Override @Override
@@ -165,26 +164,17 @@ public class CqlModelBuilder extends CqlParserBaseListener {
@Override @Override
public void exitCreateTable(CqlParser.CreateTableContext ctx) { public void exitCreateTable(CqlParser.CreateTableContext ctx) {
table.setName(ctx.table().getText()); table.setName(ctx.table().getText());
saveTable( model.addTable(ctx.keyspace().getText(), table);
ctx.keyspace().getText(), table = null;
ctx.table().getText()
);
} }
private void saveTable(String ksname, String tableName) {
table.setName(tableName);
model.addTable(ksname, table);
table=null;
}
@Override @Override
public void exitOrderDirection(CqlParser.OrderDirectionContext ctx) { public void exitOrderDirection(CqlParser.OrderDirectionContext ctx) {
} }
@Override @Override
public void exitTableOptionItem(CqlParser.TableOptionItemContext ctx) { public void exitTableOptionItem(CqlParser.TableOptionItemContext ctx) {
table.setCompactStorage(ctx.kwCompactStorage()!=null); table.setCompactStorage(ctx.kwCompactStorage() != null);
} }
@Override @Override
@@ -209,12 +199,6 @@ public class CqlModelBuilder extends CqlParserBaseListener {
.forEach(i -> table.addTableClusteringOrder(columns.get(i), orders.get(i))); .forEach(i -> table.addTableClusteringOrder(columns.get(i), orders.get(i)));
} }
// @Override
// public void exitColumn(CqlParser.ColumnContext ctx) {
// super.exitColumn(ctx);
// }
private String textOf(ParserRuleContext ctx) { private String textOf(ParserRuleContext ctx) {
int startIndex = ctx.start.getStartIndex(); int startIndex = ctx.start.getStartIndex();
int stopIndex = ctx.stop.getStopIndex(); int stopIndex = ctx.stop.getStopIndex();
@@ -223,10 +207,6 @@ public class CqlModelBuilder extends CqlParserBaseListener {
return text; return text;
} }
@Override
public void enterColumnDefinition(CqlParser.ColumnDefinitionContext ctx) {
}
@Override @Override
public void enterColumnDefinitionList(CqlParser.ColumnDefinitionListContext ctx) { public void enterColumnDefinitionList(CqlParser.ColumnDefinitionListContext ctx) {
this.colindex = 0; this.colindex = 0;
@@ -234,11 +214,26 @@ public class CqlModelBuilder extends CqlParserBaseListener {
@Override @Override
public void exitColumnDefinition(CqlParser.ColumnDefinitionContext ctx) { public void exitColumnDefinition(CqlParser.ColumnDefinitionContext ctx) {
addColumnDefinition(
ctx.column().getText(), if (usertype != null) {
textOf(ctx.dataType()), CqlTypeColumn coldef = new CqlTypeColumn(
ctx.primaryKeyColumn() != null 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 @Override
@@ -255,19 +250,6 @@ public class CqlModelBuilder extends CqlParserBaseListener {
return model.getErrors(); return model.getErrors();
} }
private void addColumnDefinition(String colname, String typedef, boolean isPrimaryKey) {
if (table != null) {
table.addcolumnDef(new CqlTableColumn(colname, typedef));
if (isPrimaryKey) {
this.table.addPartitionKey(colname);
}
} else if (usertype != null) {
usertype.addColumn(
new CqlTypeColumn(colname, typedef)
);
}
}
public void addPartitionKey(String partitionKey) { public void addPartitionKey(String partitionKey) {
table.addPartitionKey(partitionKey); table.addPartitionKey(partitionKey);
} }
@@ -281,5 +263,4 @@ public class CqlModelBuilder extends CqlParserBaseListener {
} }
} }

View File

@@ -52,6 +52,7 @@ public class CqlTable implements NBNamedElement, Labeled {
} }
public void addcolumnDef(CqlTableColumn cqlField) { public void addcolumnDef(CqlTableColumn cqlField) {
cqlField.setTable(this);
this.coldefs.add(cqlField); this.coldefs.add(cqlField);
} }
@@ -83,34 +84,48 @@ public class CqlTable implements NBNamedElement, Labeled {
@Override @Override
public Map<String, String> getLabels() { public Map<String, String> getLabels() {
return Map.of( return Map.of(
"keyspace", this.getName(), "keyspace", this.keyspace.getName(),
"name", this.name, "name", this.name,
"type", "table" "type", "table"
); );
} }
public void addPartitionKey(String pkey) { public void addPartitionKey(String pkey) {
int[] newdefs = new int[partitioning.length + 1]; int[] new_partitioning = partitioning;
System.arraycopy(partitioning, 0, newdefs, 0, partitioning.length); for (int idx = 0; idx < coldefs.size(); idx++) {
for (int i = 0; i < coldefs.size(); i++) { if (coldefs.get(idx).getName().equals(pkey)) {
if (coldefs.get(i).getName().equals(pkey)) { coldefs.get(idx).setPosition(ColumnPosition.Partitioning);
newdefs[newdefs.length - 1] = i; new_partitioning = new int[partitioning.length + 1];
System.arraycopy(partitioning, 0, new_partitioning, 0, partitioning.length);
new_partitioning[new_partitioning.length - 1] = idx;
break; break;
} }
} }
this.partitioning = newdefs; if (new_partitioning==partitioning) {
throw new RuntimeException("Unable to assign partition key '" + pkey + "' to a known column of the same name.");
} else {
this.partitioning = new_partitioning;
}
} }
public void addClusteringColumn(String ccol) { public void addClusteringColumn(String ccol) {
int[] newdefs = new int[clustering.length + 1]; int[] new_clustering = clustering;
System.arraycopy(clustering, 0, newdefs, 0, clustering.length);
for (int i = 0; i < coldefs.size(); i++) { for (int i = 0; i < coldefs.size(); i++) {
if (coldefs.get(i).getName().equals(ccol)) { if (coldefs.get(i).getName().equals(ccol)) {
newdefs[newdefs.length - 1] = i; coldefs.get(i).setPosition(ColumnPosition.Clustering);
new_clustering= new int[clustering.length + 1];
System.arraycopy(clustering, 0, new_clustering, 0, clustering.length);
new_clustering[new_clustering.length - 1] = i;
break; break;
} }
} }
this.clustering = newdefs; if (new_clustering == clustering) {
throw new RuntimeException("Unable to assign clustering field '" + ccol + " to a known column of the same name.");
} else {
this.clustering = new_clustering;
}
} }
public void addTableClusteringOrder(String colname, String order) { public void addTableClusteringOrder(String colname, String order) {

View File

@@ -16,11 +16,16 @@
package io.nosqlbench.cqlgen.model; package io.nosqlbench.cqlgen.model;
import java.util.HashMap;
import java.util.Map;
public class CqlTableColumn extends CqlColumnBase { public class CqlTableColumn extends CqlColumnBase {
private CqlTable table; private CqlTable table;
public CqlTableColumn(String colname, String typedef) {
public CqlTableColumn(String colname, String typedef, CqlTable table) {
super(colname, typedef); super(colname, typedef);
setTable(table);
} }
@Override @Override
@@ -35,4 +40,11 @@ public class CqlTableColumn extends CqlColumnBase {
public void setTable(CqlTable table) { public void setTable(CqlTable table) {
this.table = table; this.table = table;
} }
@Override
public Map<String, String> getLabels() {
HashMap<String, String> map = new HashMap<>(super.getLabels());
map.put("table",getTable().getName());
return map;
}
} }

View File

@@ -48,6 +48,7 @@ public class CqlType implements NBNamedElement, Labeled {
public void addColumn(CqlTypeColumn def) { public void addColumn(CqlTypeColumn def) {
this.columnDefs.add(this.columnDefs.size(),def); this.columnDefs.add(this.columnDefs.size(),def);
def.setPosition(ColumnPosition.TypeDef);
} }
public List<CqlTypeColumn> columns() { public List<CqlTypeColumn> columns() {

View File

@@ -16,11 +16,16 @@
package io.nosqlbench.cqlgen.model; package io.nosqlbench.cqlgen.model;
import java.util.LinkedHashMap;
import java.util.Map;
public class CqlTypeColumn extends CqlColumnBase { public class CqlTypeColumn extends CqlColumnBase {
CqlType type; CqlType type;
public CqlTypeColumn(String colname, String typedef) { public CqlTypeColumn(String colname, String typedef, CqlType usertype) {
super(colname, typedef); super(colname, typedef);
this.setType(usertype);
} }
@Override @Override
@@ -35,4 +40,11 @@ public class CqlTypeColumn extends CqlColumnBase {
public void setType(CqlType type) { public void setType(CqlType type) {
this.type = type; this.type = type;
} }
@Override
public Map<String, String> getLabels() {
Map<String,String> map = new LinkedHashMap<>(super.getLabels());
map.put("name",type.getName());
return map;
}
} }