From b71635f1e5f7f48f65b5bd8f146defa55f91cb62 Mon Sep 17 00:00:00 2001 From: awildturtok <1553491+awildturtok@users.noreply.github.com> Date: Thu, 14 Jul 2022 15:35:54 +0200 Subject: [PATCH] simplify code of ConcepElementsAgg --- .../select/concept/ConceptColumnSelect.java | 23 +-------- ...or.java => ConceptElementsAggregator.java} | 47 +++++++++++-------- 2 files changed, 29 insertions(+), 41 deletions(-) rename backend/src/main/java/com/bakdata/conquery/models/query/queryplan/aggregators/specific/value/{ConceptColumnsAggregator.java => ConceptElementsAggregator.java} (56%) diff --git a/backend/src/main/java/com/bakdata/conquery/models/datasets/concepts/select/concept/ConceptColumnSelect.java b/backend/src/main/java/com/bakdata/conquery/models/datasets/concepts/select/concept/ConceptColumnSelect.java index c7dc617a19..0e5896b662 100644 --- a/backend/src/main/java/com/bakdata/conquery/models/datasets/concepts/select/concept/ConceptColumnSelect.java +++ b/backend/src/main/java/com/bakdata/conquery/models/datasets/concepts/select/concept/ConceptColumnSelect.java @@ -5,13 +5,10 @@ import com.bakdata.conquery.apiv1.query.concept.specific.CQConcept; import com.bakdata.conquery.io.cps.CPSType; import com.bakdata.conquery.models.datasets.concepts.select.Select; -import com.bakdata.conquery.models.datasets.concepts.tree.ConceptTreeNode; import com.bakdata.conquery.models.datasets.concepts.tree.TreeConcept; -import com.bakdata.conquery.models.query.PrintSettings; import com.bakdata.conquery.models.query.queryplan.aggregators.Aggregator; -import com.bakdata.conquery.models.query.queryplan.aggregators.specific.value.ConceptColumnsAggregator; +import com.bakdata.conquery.models.query.queryplan.aggregators.specific.value.ConceptElementsAggregator; import com.bakdata.conquery.models.query.resultinfo.SelectResultInfo; -import com.bakdata.conquery.models.types.ResultType; import com.bakdata.conquery.models.types.SemanticType; import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonIgnore; @@ -27,7 +24,7 @@ public class ConceptColumnSelect extends Select { @Override public Aggregator createAggregator() { - return new ConceptColumnsAggregator(concept); + return new ConceptElementsAggregator(concept); } @Override @@ -35,20 +32,4 @@ public SelectResultInfo getResultInfo(CQConcept cqConcept) { return new SelectResultInfo(this, cqConcept, Set.of(new SemanticType.ConceptColumnT(concept))); } - @Override - public ResultType getResultType() { - return new ResultType.ListT(new ResultType.StringT(this::printValue)); - } - - private String printValue(Object o, PrintSettings printSettings) { - final int[] mostSpecificChild = (int[]) o; - - final ConceptTreeNode node = concept.getElementByLocalId(mostSpecificChild); - - if (!printSettings.isPrettyPrint()) { - return node.getId().toStringWithoutDataset(); - } - - return node.getName(); - } } diff --git a/backend/src/main/java/com/bakdata/conquery/models/query/queryplan/aggregators/specific/value/ConceptColumnsAggregator.java b/backend/src/main/java/com/bakdata/conquery/models/query/queryplan/aggregators/specific/value/ConceptElementsAggregator.java similarity index 56% rename from backend/src/main/java/com/bakdata/conquery/models/query/queryplan/aggregators/specific/value/ConceptColumnsAggregator.java rename to backend/src/main/java/com/bakdata/conquery/models/query/queryplan/aggregators/specific/value/ConceptElementsAggregator.java index 5db5cd057e..43cc4fd8ef 100644 --- a/backend/src/main/java/com/bakdata/conquery/models/query/queryplan/aggregators/specific/value/ConceptColumnsAggregator.java +++ b/backend/src/main/java/com/bakdata/conquery/models/query/queryplan/aggregators/specific/value/ConceptElementsAggregator.java @@ -1,13 +1,13 @@ package com.bakdata.conquery.models.query.queryplan.aggregators.specific.value; -import java.util.HashSet; import java.util.Map; +import java.util.Optional; import java.util.Set; +import com.bakdata.conquery.apiv1.query.TableExportQuery; import com.bakdata.conquery.models.datasets.Column; import com.bakdata.conquery.models.datasets.Table; import com.bakdata.conquery.models.datasets.concepts.tree.ConceptTreeConnector; -import com.bakdata.conquery.models.datasets.concepts.tree.ConceptTreeNode; import com.bakdata.conquery.models.datasets.concepts.tree.TreeConcept; import com.bakdata.conquery.models.events.Bucket; import com.bakdata.conquery.models.events.CBlock; @@ -16,12 +16,14 @@ import com.bakdata.conquery.models.query.queryplan.aggregators.Aggregator; import com.bakdata.conquery.models.types.ResultType; import com.google.common.collect.ImmutableSet; +import it.unimi.dsi.fastutil.ints.IntOpenHashSet; +import it.unimi.dsi.fastutil.ints.IntSet; import lombok.ToString; @ToString(callSuper = true, onlyExplicitlyIncluded = true) -public class ConceptColumnsAggregator extends Aggregator> { +public class ConceptElementsAggregator extends Aggregator> { - private final Set entries = new HashSet<>(); + private final IntSet entries = new IntOpenHashSet(); private final TreeConcept concept; private Column column; @@ -29,27 +31,33 @@ public class ConceptColumnsAggregator extends Aggregator> { private Map cblocks; private CBlock cblock; - public ConceptColumnsAggregator(TreeConcept concept) { + public ConceptElementsAggregator(TreeConcept concept) { super(); this.concept = concept; } @Override public void nextTable(QueryExecutionContext ctx, Table currentTable) { - for (ConceptTreeConnector connector : concept.getConnectors()) { - if (connector.getTable().equals(currentTable)) { - column = connector.getColumn(); - cblocks = ctx.getBucketManager().getEntityCBlocksForConnector(entity, connector); - break; - } + Optional maybeConnector = + concept.getConnectors().stream() + .filter(conn -> conn.getTable().equals(currentTable)) + .findAny(); + + if (maybeConnector.isEmpty()) { + cblocks = null; + column = null; + return; } + final ConceptTreeConnector connector = maybeConnector.get(); + column = connector.getColumn(); + cblocks = ctx.getBucketManager().getEntityCBlocksForConnector(entity, connector); } @Override public void nextBlock(Bucket bucket) { - this.cblock = cblocks.get(bucket); + cblock = cblocks.get(bucket); } @Override @@ -60,23 +68,22 @@ public void init(Entity entity, QueryExecutionContext context) { @Override public void acceptEvent(Bucket bucket, int event) { - if (bucket.has(event, column)) { - final int[] mostSpecificChild = cblock.getEventMostSpecificChild(event); - final ConceptTreeNode element = concept.getElementByLocalId(mostSpecificChild); - - entries.add(mostSpecificChild); - + if (!bucket.has(event, column)) { + return; } + + final int mostSpecificChild = cblock.getMostSpecificChildLocalId(event); + entries.add(mostSpecificChild); } @Override - public Set createAggregationResult() { + public Set createAggregationResult() { return entries.isEmpty() ? null : ImmutableSet.copyOf(entries); } @Override public ResultType getResultType() { - throw new IllegalStateException(); + return new ResultType.ListT(new ResultType.StringT((val, settings) -> TableExportQuery.printValue(concept, val, settings))); } }