From ae421423360991a84177eaab90d5a67a5dc28440 Mon Sep 17 00:00:00 2001 From: Jonathan Shook Date: Tue, 9 Aug 2022 12:54:17 -0500 Subject: [PATCH] moderate refactoring of module builder, better separation from model --- .../cqlgen/model/CqlColumnBase.java | 13 +- .../cqlgen/model/CqlModelBuilder.java | 115 ++++++++---------- .../io/nosqlbench/cqlgen/model/CqlTable.java | 37 ++++-- .../cqlgen/model/CqlTableColumn.java | 14 ++- .../io/nosqlbench/cqlgen/model/CqlType.java | 1 + .../cqlgen/model/CqlTypeColumn.java | 14 ++- 6 files changed, 105 insertions(+), 89 deletions(-) diff --git a/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlColumnBase.java b/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlColumnBase.java index 9198f1a16..31caf8aa9 100644 --- a/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlColumnBase.java +++ b/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlColumnBase.java @@ -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 getLabels() { return Map.of( "name", name, - "typedef", typedef, "type", "column" ); } diff --git a/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlModelBuilder.java b/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlModelBuilder.java index 57cae834c..7aad53f70 100644 --- a/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlModelBuilder.java +++ b/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlModelBuilder.java @@ -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; @@ -62,14 +58,14 @@ public class CqlModelBuilder extends CqlParserBaseListener { ParseTree parent = node.getParent(); String errorNodeType = parent.getClass().getSimpleName(); - logger.info("PARSE ERROR: " + errorNodeType + "\n"+ node.getSourceInterval()); + logger.info("PARSE ERROR: " + errorNodeType + "\n" + node.getSourceInterval()); super.visitErrorNode(node); } @Override public void enterCreateKeyspace(CqlParser.CreateKeyspaceContext ctx) { - this.keyspace=new CqlKeyspaceDef(); + this.keyspace = new CqlKeyspaceDef(); } @Override @@ -106,25 +102,31 @@ 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) { - if (ctx.compositeKey().partitionKeyList() != null) { - for (CqlParser.PartitionKeyContext pkctx : ctx.compositeKey().partitionKeyList().partitionKey()) { - addPartitionKey(pkctx.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()); + } } - } - if (ctx.compositeKey().clusteringKeyList() != null) { - for (CqlParser.ClusteringKeyContext ccol : ctx.compositeKey().clusteringKeyList().clusteringKey()) { + // 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()); } } - } else if (ctx.compoundKey() != null) { - addPartitionKey(ctx.compoundKey().partitionKey().getText()); - for (CqlParser.ClusteringKeyContext ccol : ctx.compoundKey().clusteringKeyList().clusteringKey()) { - addClusteringColumn(ccol.column().getText()); - } - } } @Override @@ -140,21 +142,18 @@ public class CqlModelBuilder extends CqlParserBaseListener { usertype.setDefined(); model.addType(keyspace, usertype); usertype.validate(); - usertype=null; + usertype = null; } - - // HERE consider building hierarchic type model @Override public void exitTypeMemberColumnList(CqlParser.TypeMemberColumnListContext ctx) { List columns = ctx.column(); List 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,26 +164,17 @@ public class CqlModelBuilder extends CqlParserBaseListener { @Override public void exitCreateTable(CqlParser.CreateTableContext ctx) { table.setName(ctx.table().getText()); - saveTable( - ctx.keyspace().getText(), - ctx.table().getText() - ); + model.addTable(ctx.keyspace().getText(), table); + table = null; } - private void saveTable(String ksname, String tableName) { - table.setName(tableName); - model.addTable(ksname, table); - table=null; - } - - @Override public void exitOrderDirection(CqlParser.OrderDirectionContext ctx) { } @Override public void exitTableOptionItem(CqlParser.TableOptionItemContext ctx) { - table.setCompactStorage(ctx.kwCompactStorage()!=null); + table.setCompactStorage(ctx.kwCompactStorage() != null); } @Override @@ -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( - ctx.column().getText(), - textOf(ctx.dataType()), - ctx.primaryKeyColumn() != null - ); + + 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 @@ -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 { } - } diff --git a/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlTable.java b/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlTable.java index 88ec54a91..5827b09d8 100644 --- a/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlTable.java +++ b/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlTable.java @@ -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 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) { diff --git a/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlTableColumn.java b/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlTableColumn.java index d8dbee822..641b96732 100644 --- a/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlTableColumn.java +++ b/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlTableColumn.java @@ -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 getLabels() { + HashMap map = new HashMap<>(super.getLabels()); + map.put("table",getTable().getName()); + return map; + } } diff --git a/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlType.java b/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlType.java index a9d30fbf9..41c513c13 100644 --- a/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlType.java +++ b/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlType.java @@ -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 columns() { diff --git a/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlTypeColumn.java b/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlTypeColumn.java index 4d4326db9..c9dc169a4 100644 --- a/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlTypeColumn.java +++ b/adapter-cqld4/src/main/java/io/nosqlbench/cqlgen/model/CqlTypeColumn.java @@ -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 getLabels() { + Map map = new LinkedHashMap<>(super.getLabels()); + map.put("name",type.getName()); + return map; + } }