public Multimap<Long, AccumuloColumnConstraint> getCardinalities(String schema, String table, Authorizations auths, Multimap<AccumuloColumnConstraint, Range> idxConstraintRangePairs, long earlyReturnThreshold, Duration pollingDuration)
    {
        // Submit tasks to the executor to fetch column cardinality, adding it to the Guava cache if necessary
        CompletionService<Pair<Long, AccumuloColumnConstraint>> executor = new ExecutorCompletionService<>(executorService);
        idxConstraintRangePairs.asMap().forEach((key, value) -> executor.submit(() -> {
            long cardinality = getColumnCardinality(schema, table, auths, key.getFamily(), key.getQualifier(), value);
            LOG.debug("Cardinality for column %s is %s", key.getName(), cardinality);
            return Pair.of(cardinality, key);
        }));

        // Create a multi map sorted by cardinality
        ListMultimap<Long, AccumuloColumnConstraint> cardinalityToConstraints = MultimapBuilder.treeKeys().arrayListValues().build();
        try {
            boolean earlyReturn = false;
            int numTasks = idxConstraintRangePairs.asMap().entrySet().size();
            do {
                // Sleep for the polling duration to allow concurrent tasks to run for this time
                Thread.sleep(pollingDuration.toMillis());

                // Poll each task, retrieving the result if it is done
                for (int i = 0; i < numTasks; ++i) {
                    Future<Pair<Long, AccumuloColumnConstraint>> futureCardinality = executor.poll();
                    if (futureCardinality != null && futureCardinality.isDone()) {
                        Pair<Long, AccumuloColumnConstraint> columnCardinality = futureCardinality.get();
                        cardinalityToConstraints.put(columnCardinality.getLeft(), columnCardinality.getRight());
                    }
                }

                // If the smallest cardinality is present and below the threshold, set the earlyReturn flag
                Optional<Entry<Long, AccumuloColumnConstraint>> smallestCardinality = cardinalityToConstraints.entries().stream().findFirst();
                if (smallestCardinality.isPresent()) {
                    if (smallestCardinality.get().getKey() <= earlyReturnThreshold) {
                        LOG.info("Cardinality %s, is below threshold. Returning early while other tasks finish", smallestCardinality);
                        earlyReturn = true;
                    }
                }
            }
            while (!earlyReturn && cardinalityToConstraints.entries().size() < numTasks);
        }
        catch (ExecutionException | InterruptedException e) {
            if (e instanceof InterruptedException) {
                Thread.currentThread().interrupt();
            }
            throw new PrestoException(UNEXPECTED_ACCUMULO_ERROR, "Exception when getting cardinality", e);
        }

        // Create a copy of the cardinalities
        return ImmutableMultimap.copyOf(cardinalityToConstraints);
    }
public Multimap < Long , AccumuloColumnConstraint > getCardinalities ( String schema , String table , Authorizations auths , Multimap < AccumuloColumnConstraint , Range > idxConstraintRangePairs , long earlyReturnThreshold , Duration pollingDuration ) { CompletionService < Pair < Long , AccumuloColumnConstraint > > executor = new ExecutorCompletionService < > ( executorService ) ; idxConstraintRangePairs . asMap ( ) . forEach ( ( key , value ) -> executor . submit ( ( ) -> { long cardinality = getColumnCardinality ( schema , table , auths , key . getFamily ( ) , key . getQualifier ( ) , value ) ; LOG . debug ( "Cardinality for column %s is %s" , key . getName ( ) , cardinality ) ; return Pair . of ( cardinality , key ) ; } ) ) ; ListMultimap < Long , AccumuloColumnConstraint > cardinalityToConstraints = MultimapBuilder . treeKeys ( ) . arrayListValues ( ) . build ( ) ; try { boolean earlyReturn = false ; int numTasks = idxConstraintRangePairs . asMap ( ) . entrySet ( ) . size ( ) ; do { Thread . sleep ( pollingDuration . toMillis ( ) ) ; for ( int i = 0 ; i < numTasks ; ++ i ) { Future < Pair < Long , AccumuloColumnConstraint > > futureCardinality = executor . poll ( ) ; if ( futureCardinality != null && futureCardinality . isDone ( ) ) { Pair < Long , AccumuloColumnConstraint > columnCardinality = futureCardinality . get ( ) ; cardinalityToConstraints . put ( columnCardinality . getLeft ( ) , columnCardinality . getRight ( ) ) ; } } Optional < Entry < Long , AccumuloColumnConstraint > > smallestCardinality = cardinalityToConstraints . entries ( ) . stream ( ) . findFirst ( ) ; if ( smallestCardinality . isPresent ( ) ) { if ( smallestCardinality . get ( ) . getKey ( ) <= earlyReturnThreshold ) { LOG . info ( "Cardinality %s, is below threshold. Returning early while other tasks finish" , smallestCardinality ) ; earlyReturn = true ; } } } while ( ! earlyReturn && cardinalityToConstraints . entries ( ) . size ( ) < numTasks ) ; } catch ( ExecutionException | InterruptedException e ) { if ( e instanceof InterruptedException ) { Thread . currentThread ( ) . interrupt ( ) ; } throw new PrestoException ( UNEXPECTED_ACCUMULO_ERROR , "Exception when getting cardinality" , e ) ; } return ImmutableMultimap . copyOf ( cardinalityToConstraints ) ; }
Gets the cardinality for each {@link AccumuloColumnConstraint}.
Given constraints are expected to be indexed! Who knows what would happen if they weren't!

@param schema Schema name
@param table Table name
@param auths Scan authorizations
@param idxConstraintRangePairs Mapping of all ranges for a given constraint
@param earlyReturnThreshold Smallest acceptable cardinality to return early while other tasks complete
@param pollingDuration Duration for polling the cardinality completion service
@return An immutable multimap of cardinality to column constraint, sorted by cardinality from smallest to largest
@throws TableNotFoundException If the metrics table does not exist
@throws ExecutionException If another error occurs; I really don't even know anymore.