Skip to content

Commit

Permalink
add debugging information for ConditionMatchBloomDBTables (#167)
Browse files Browse the repository at this point in the history
* add debugging information for ConditionMatchBloomDBTables

* do not snapshot meta

* trace meta.getTables()
  • Loading branch information
kortemik authored Dec 20, 2024
1 parent ffe5737 commit e4acb3b
Showing 1 changed file with 58 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@

import com.teragrep.pth_06.planner.walker.conditions.RegexLikeCondition;
import com.teragrep.pth_06.planner.walker.conditions.QueryCondition;
import org.jooq.DSLContext;
import org.jooq.Field;
import org.jooq.Table;
import org.jooq.*;
import org.jooq.types.ULong;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -84,24 +82,70 @@ public ConditionMatchBloomDBTables(DSLContext ctx, QueryCondition condition) {
* @return List of tables that matched QueryCondition and were not empty
*/
public List<Table<?>> tables() {
final List<Table<?>> tables = ctx
.meta()
LOGGER.debug("Matching tables for condition <[{}]>, using ctx <{}>", condition, ctx);
TablesWithMatchingRegexAndNotEmpty tablesWithMatchingRegexAndNotEmpty = new TablesWithMatchingRegexAndNotEmpty(
ctx,
condition
);

Meta meta = ctx.meta();

if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Found meta.getTables() <[{}]>", meta.getTables());
}

final List<Table<?>> tables = meta
.filterSchemas(s -> s.equals(BLOOMDB)) // select bloomdb
.filterTables(t -> !t.equals(BLOOMDB.FILTERTYPE)) // remove filtertype table
.filterTables(t -> ctx.select((Field<ULong>) t.field("id"))// for each remaining table
.from(t)
.leftJoin(BLOOMDB.FILTERTYPE)// join filtertype to access patterns
.on(BLOOMDB.FILTERTYPE.ID.eq((Field<ULong>) t.field("filter_type_id")))
.where(condition.condition())// select tables that match the condition
.limit(1)// limit 1 since we are checking only if the table is not empty
.fetch()
.isNotEmpty() // select table if not empty
)
.filterTables(tablesWithMatchingRegexAndNotEmpty) // for each remaining table
.getTables();

LOGGER.debug("Table(s) with a pattern match <{}>", tables);

return tables;
}

private static class TablesWithMatchingRegexAndNotEmpty implements Meta.Predicate<Table<?>> {

private final DSLContext ctx;
private final QueryCondition condition;

public TablesWithMatchingRegexAndNotEmpty(DSLContext ctx, QueryCondition condition) {
this.ctx = ctx;
this.condition = condition;
}

@Override
public boolean test(final Table<?> t) {
// select id from each of the tables from the first row, then join to 'filtertype' via the filter_type_id, to access the pattern so that we know if the table is relevant for the query

SelectLimitPercentStep<Record1<ULong>> selectQuery = ctx
.select((Field<ULong>) t.field("id"))
.from(t)
.leftJoin(BLOOMDB.FILTERTYPE)// join filtertype to access patterns
.on(BLOOMDB.FILTERTYPE.ID.eq((Field<ULong>) t.field("filter_type_id")))
.where(condition.condition())// select tables that match the condition
.limit(1); // limit 1 since we are checking only if the table is not empty

LOGGER.trace("Testing a match for table <[{}]> using selectQuery <[{}]> ", t, selectQuery);

Result<Record1<ULong>> firstRowIdOnBloomCategoryTable = selectQuery.fetch();

LOGGER
.trace(
"Result for table <[{}]> firstRowIdOnBloomCategoryTable <[{}]>", t,
firstRowIdOnBloomCategoryTable
);

boolean isIncluded = firstRowIdOnBloomCategoryTable.isNotEmpty(); // select table if not empty

LOGGER.debug("Table <[{}]>, isIncluded <{}>", t, isIncluded);

return isIncluded;
}

}

/**
* Equal if the compared object is the same instance or if the compared object is of the same class, object fields
* are equal, and DSLContext is the same instance
Expand Down

0 comments on commit e4acb3b

Please sign in to comment.