Skip to content

Commit

Permalink
simplify code of ConcepElementsAgg
Browse files Browse the repository at this point in the history
  • Loading branch information
awildturtok committed Jul 14, 2022
1 parent 26d4466 commit b71635f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,28 +24,12 @@ public class ConceptColumnSelect extends Select {

@Override
public Aggregator<?> createAggregator() {
return new ConceptColumnsAggregator(concept);
return new ConceptElementsAggregator(concept);
}

@Override
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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,40 +16,48 @@
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<Set<int[]>> {
public class ConceptElementsAggregator extends Aggregator<Set<Integer>> {

private final Set<int[]> entries = new HashSet<>();
private final IntSet entries = new IntOpenHashSet();
private final TreeConcept concept;

private Column column;
private Entity entity;
private Map<Bucket, CBlock> 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<ConceptTreeConnector> 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
Expand All @@ -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<int[]> createAggregationResult() {
public Set<Integer> 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)));
}
}

0 comments on commit b71635f

Please sign in to comment.