Skip to content

Commit

Permalink
[MINOR] Simple CacheIndexImpl#count refactoring to improve readability (
Browse files Browse the repository at this point in the history
  • Loading branch information
nizhikov committed Sep 13, 2024
1 parent 7f107fd commit cfed6ad
Showing 1 changed file with 51 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.calcite.plan.RelOptCluster;
import org.apache.calcite.plan.RelOptTable;
import org.apache.calcite.rel.RelCollation;
Expand Down Expand Up @@ -153,63 +152,63 @@ public Index queryIndex() {

/** {@inheritDoc} */
@Override public long count(ExecutionContext<?> ectx, ColocationGroup grp, boolean notNull) {
long cnt = 0;
if (idx == null || !grp.nodeIds().contains(ectx.localNodeId()))
return 0;

if (idx != null && grp.nodeIds().contains(ectx.localNodeId())) {
InlineIndex iidx = idx.unwrap(InlineIndex.class);

try {
IndexingQueryFilter filter = new IndexingQueryFilterImpl(tbl.descriptor().cacheContext().kernalContext(),
ectx.topologyVersion(), grp.partitions(ectx.localNodeId()));

InlineIndex iidx = idx.unwrap(InlineIndex.class);

BPlusTree.TreeRowClosure<IndexRow, IndexRow> rowFilter = null;

boolean checkExpired = !tbl.descriptor().cacheContext().config().isEagerTtl();

if (notNull) {
boolean nullsFirst = collation.getFieldCollations().get(0).nullDirection ==
RelFieldCollation.NullDirection.FIRST;

BPlusTree.TreeRowClosure<IndexRow, IndexRow> notNullRowFilter =
IndexScan.createNotNullRowFilter(iidx, checkExpired);

AtomicBoolean skipCheck = new AtomicBoolean();

rowFilter = new BPlusTree.TreeRowClosure<IndexRow, IndexRow>() {
@Override public boolean apply(
BPlusTree<IndexRow, IndexRow> tree,
BPlusIO<IndexRow> io,
long pageAddr,
int idx
) throws IgniteCheckedException {
// If we have NULLS-FIRST collation, all values after first not-null value will be not-null,
// don't need to check it with notNullRowFilter.
// In case of NULL-LAST collation, all values after first null value will be null,
// don't need to check it too.
if (skipCheck.get() && !checkExpired)
return nullsFirst;

boolean res = notNullRowFilter.apply(tree, io, pageAddr, idx);

if (res == nullsFirst)
skipCheck.set(true);

return res;
}
};
}
else if (checkExpired)
rowFilter = IndexScan.createNotExpiredRowFilter();

try {
for (int i = 0; i < iidx.segmentsCount(); ++i)
cnt += iidx.count(i, new IndexQueryContext(filter, rowFilter));
}
catch (IgniteCheckedException e) {
throw new IgniteException("Unable to count index records.", e);
}
long cnt = 0;

for (int i = 0; i < iidx.segmentsCount(); ++i)
cnt += iidx.count(i, new IndexQueryContext(filter, countRowFilter(notNull, iidx)));

return cnt;
}
catch (IgniteCheckedException e) {
throw new IgniteException("Unable to count index records.", e);
}
}

/** */
private @Nullable BPlusTree.TreeRowClosure<IndexRow, IndexRow> countRowFilter(boolean notNull, InlineIndex iidx) {
boolean checkExpired = !tbl.descriptor().cacheContext().config().isEagerTtl();

if (notNull) {
boolean nullsFirst = collation.getFieldCollations().get(0).nullDirection == RelFieldCollation.NullDirection.FIRST;

BPlusTree.TreeRowClosure<IndexRow, IndexRow> notNullRowFilter = IndexScan.createNotNullRowFilter(iidx, checkExpired);

return new BPlusTree.TreeRowClosure<>() {
private boolean skipCheck;

@Override public boolean apply(
BPlusTree<IndexRow, IndexRow> tree,
BPlusIO<IndexRow> io,
long pageAddr,
int idx
) throws IgniteCheckedException {
// If we have NULLS-FIRST collation, all values after first not-null value will be not-null,
// don't need to check it with notNullRowFilter.
// In case of NULL-LAST collation, all values after first null value will be null,
// don't need to check it too.
if (skipCheck && !checkExpired)
return nullsFirst;

boolean res = notNullRowFilter.apply(tree, io, pageAddr, idx);

if (res == nullsFirst)
skipCheck = true;

return res;
}
};
}

return cnt;
return checkExpired ? IndexScan.createNotExpiredRowFilter() : null;
}

/** {@inheritDoc} */
Expand Down

0 comments on commit cfed6ad

Please sign in to comment.