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;
/**
* 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 {
private String name;
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.name = colname;
}
public void setPosition(FieldPosition position) {
public void setPosition(ColumnPosition position) {
this.position = position;
}
public FieldPosition getPosition() {
public ColumnPosition getPosition() {
return this.position;
}
public void setTypeDef(String type) {
@@ -68,7 +64,6 @@ public abstract class CqlColumnBase implements NBNamedElement, Labeled {
public Map<String, String> getLabels() {
return Map.of(
"name", name,
"typedef", typedef,
"type", "column"
);
}

View File

@@ -38,11 +38,7 @@ public class CqlModelBuilder extends CqlParserBaseListener {
private int colindex;
private CqlKeyspaceDef keyspace;
private CqlType usertype;
transient CqlTable table;
private CqlTable table;
public CqlModelBuilder(CGErrorListener errorListener) {
this.errorListener = errorListener;
@@ -106,20 +102,26 @@ public class CqlModelBuilder extends CqlParserBaseListener {
@Override
public void exitPrimaryKeyDefinition(CqlParser.PrimaryKeyDefinitionContext ctx) {
// PRIMARY KEY (PK)
if (ctx.singlePrimaryKey() != null) {
addPartitionKey(ctx.singlePrimaryKey().column().getText());
} else if (ctx.compositeKey() != null) {
} 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 if (ctx.compoundKey() != null) {
} 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());
@@ -143,18 +145,15 @@ public class CqlModelBuilder extends CqlParserBaseListener {
usertype = null;
}
// HERE consider building hierarchic type model
@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()));
new CqlTypeColumn(columns.get(idx).getText(), dataTypes.get(idx).getText(), usertype)
);
}
// dataTypes.get(0).dataType().get(0).dataType().get(0)
}
@Override
@@ -165,19 +164,10 @@ public class CqlModelBuilder extends CqlParserBaseListener {
@Override
public void exitCreateTable(CqlParser.CreateTableContext ctx) {
table.setName(ctx.table().getText());
saveTable(
ctx.keyspace().getText(),
ctx.table().getText()
);
}
private void saveTable(String ksname, String tableName) {
table.setName(tableName);
model.addTable(ksname, table);
model.addTable(ctx.keyspace().getText(), table);
table = null;
}
@Override
public void exitOrderDirection(CqlParser.OrderDirectionContext ctx) {
}
@@ -209,12 +199,6 @@ public class CqlModelBuilder extends CqlParserBaseListener {
.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) {
int startIndex = ctx.start.getStartIndex();
int stopIndex = ctx.stop.getStopIndex();
@@ -223,10 +207,6 @@ public class CqlModelBuilder extends CqlParserBaseListener {
return text;
}
@Override
public void enterColumnDefinition(CqlParser.ColumnDefinitionContext ctx) {
}
@Override
public void enterColumnDefinitionList(CqlParser.ColumnDefinitionListContext ctx) {
this.colindex = 0;
@@ -234,11 +214,26 @@ public class CqlModelBuilder extends CqlParserBaseListener {
@Override
public void exitColumnDefinition(CqlParser.ColumnDefinitionContext ctx) {
addColumnDefinition(
if (usertype != null) {
CqlTypeColumn coldef = new CqlTypeColumn(
ctx.column().getText(),
textOf(ctx.dataType()),
ctx.primaryKeyColumn() != null
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
@@ -255,19 +250,6 @@ public class CqlModelBuilder extends CqlParserBaseListener {
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) {
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) {
cqlField.setTable(this);
this.coldefs.add(cqlField);
}
@@ -83,34 +84,48 @@ public class CqlTable implements NBNamedElement, Labeled {
@Override
public Map<String, String> getLabels() {
return Map.of(
"keyspace", this.getName(),
"keyspace", this.keyspace.getName(),
"name", this.name,
"type", "table"
);
}
public void addPartitionKey(String pkey) {
int[] newdefs = new int[partitioning.length + 1];
System.arraycopy(partitioning, 0, newdefs, 0, partitioning.length);
for (int i = 0; i < coldefs.size(); i++) {
if (coldefs.get(i).getName().equals(pkey)) {
newdefs[newdefs.length - 1] = i;
int[] new_partitioning = partitioning;
for (int idx = 0; idx < coldefs.size(); idx++) {
if (coldefs.get(idx).getName().equals(pkey)) {
coldefs.get(idx).setPosition(ColumnPosition.Partitioning);
new_partitioning = new int[partitioning.length + 1];
System.arraycopy(partitioning, 0, new_partitioning, 0, partitioning.length);
new_partitioning[new_partitioning.length - 1] = idx;
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) {
int[] newdefs = new int[clustering.length + 1];
System.arraycopy(clustering, 0, newdefs, 0, clustering.length);
int[] new_clustering = clustering;
for (int i = 0; i < coldefs.size(); i++) {
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;
}
}
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) {

View File

@@ -16,11 +16,16 @@
package io.nosqlbench.cqlgen.model;
import java.util.HashMap;
import java.util.Map;
public class CqlTableColumn extends CqlColumnBase {
private CqlTable table;
public CqlTableColumn(String colname, String typedef) {
public CqlTableColumn(String colname, String typedef, CqlTable table) {
super(colname, typedef);
setTable(table);
}
@Override
@@ -35,4 +40,11 @@ public class CqlTableColumn extends CqlColumnBase {
public void setTable(CqlTable 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) {
this.columnDefs.add(this.columnDefs.size(),def);
def.setPosition(ColumnPosition.TypeDef);
}
public List<CqlTypeColumn> columns() {

View File

@@ -16,11 +16,16 @@
package io.nosqlbench.cqlgen.model;
import java.util.LinkedHashMap;
import java.util.Map;
public class CqlTypeColumn extends CqlColumnBase {
CqlType type;
public CqlTypeColumn(String colname, String typedef) {
public CqlTypeColumn(String colname, String typedef, CqlType usertype) {
super(colname, typedef);
this.setType(usertype);
}
@Override
@@ -35,4 +40,11 @@ public class CqlTypeColumn extends CqlColumnBase {
public void setType(CqlType 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;
}
}