Example usage for com.google.common.collect Table columnKeySet

List of usage examples for com.google.common.collect Table columnKeySet

Introduction

In this page you can find the example usage for com.google.common.collect Table columnKeySet.

Prototype

Set<C> columnKeySet();

Source Link

Document

Returns a set of column keys that have one or more values in the table.

Usage

From source file:co.cask.cdap.internal.app.runtime.distributed.DistributedProgramRuntimeService.java

@Override
public synchronized Map<RunId, RuntimeInfo> list(ProgramType type) {
    Map<RunId, RuntimeInfo> result = Maps.newHashMap();
    result.putAll(super.list(type));

    // Table holds the Twill RunId and TwillController associated with the program matching the input type
    Table<Id.Program, RunId, TwillController> twillProgramInfo = HashBasedTable.create();

    // Goes through all live application and fill the twillProgramInfo table
    for (TwillRunner.LiveInfo liveInfo : twillRunner.lookupLive()) {
        String appName = liveInfo.getApplicationName();
        Matcher matcher = APP_NAME_PATTERN.matcher(appName);
        if (!matcher.matches()) {
            continue;
        }/*from   w  w  w . j  av  a 2s  . c o m*/
        if (!type.equals(getType(matcher.group(1)))) {
            continue;
        }

        for (TwillController controller : liveInfo.getControllers()) {
            RunId twillRunId = controller.getRunId();
            if (isTwillRunIdCached(twillRunId)) {
                continue;
            }

            Id.Program programId = Id.Program.from(matcher.group(2), matcher.group(3), type, matcher.group(4));
            twillProgramInfo.put(programId, twillRunId, controller);
        }
    }

    if (twillProgramInfo.isEmpty()) {
        return ImmutableMap.copyOf(result);
    }

    final Set<RunId> twillRunIds = twillProgramInfo.columnKeySet();
    List<RunRecordMeta> activeRunRecords = store.getRuns(ProgramRunStatus.RUNNING,
            new Predicate<RunRecordMeta>() {
                @Override
                public boolean apply(RunRecordMeta record) {
                    return record.getTwillRunId() != null && twillRunIds
                            .contains(org.apache.twill.internal.RunIds.fromString(record.getTwillRunId()));
                }
            });

    for (RunRecordMeta record : activeRunRecords) {
        RunId twillRunIdFromRecord = org.apache.twill.internal.RunIds.fromString(record.getTwillRunId());
        // Get the CDAP RunId from RunRecord
        RunId runId = RunIds.fromString(record.getPid());
        // Get the Program and TwillController for the current twillRunId
        Map<Id.Program, TwillController> mapForTwillId = twillProgramInfo.columnMap().get(twillRunIdFromRecord);
        Map.Entry<Id.Program, TwillController> entry = mapForTwillId.entrySet().iterator().next();

        // Create RuntimeInfo for the current Twill RunId
        RuntimeInfo runtimeInfo = createRuntimeInfo(entry.getKey(), entry.getValue(), runId);
        if (runtimeInfo != null) {
            result.put(runId, runtimeInfo);
            updateRuntimeInfo(type, runId, runtimeInfo);
        } else {
            LOG.warn("Unable to find program {} {}", type, entry.getKey());
        }
    }

    return ImmutableMap.copyOf(result);
}

From source file:org.splevo.vpm.analyzer.semantic.SemanticVPMAnalyzer.java

/**
 * Build filter for variation points not sharing the same set of terms with all of it's
 * connected vps.//from  ww w .j  a  va2 s  .  c  o m
 *
 * If configured, also variation points sharing more than one term are included in the filter.
 *
 * @param sharedTermTable
 *            The table of variation points, there referenced other VPs and the terms they
 *            share.
 * @return The set of VP ids to not register any relationships for.
 */
private Set<String> buildImpreciseVPFilter(Table<String, String, Set<String>> sharedTermTable) {
    Set<String> vpFilter = Sets.newLinkedHashSet();
    if (similarTermSetOnlyConfig.getCurrentValue()) {

        // CHECK VPs in both directions as shared term is a bi-derectional relationship
        // and some VPs might be contained in only one direction.

        // CHECK ROWS
        for (String referenceVP : sharedTermTable.rowKeySet()) {
            Map<String, Set<String>> row = sharedTermTable.row(referenceVP);
            Set<String> referenceTerms = null;
            for (Set<String> currentTerms : row.values()) {
                if (referenceTerms == null) {
                    if (oneSharedTermOnlyConfig.getCurrentValue() && currentTerms.size() > 1) {
                        vpFilter.add(referenceVP);
                        break;
                    }
                    referenceTerms = currentTerms;
                } else {
                    if (!referenceTerms.containsAll(currentTerms)
                            || !currentTerms.containsAll(referenceTerms)) {
                        vpFilter.add(referenceVP);
                        break;
                    }
                }
            }
        }

        // CHECK COLUMNS
        for (String referenceVP : sharedTermTable.columnKeySet()) {
            Map<String, Set<String>> column = sharedTermTable.column(referenceVP);
            Set<String> referenceTerms = null;
            for (Set<String> currentTerms : column.values()) {
                if (referenceTerms == null) {
                    if (oneSharedTermOnlyConfig.getCurrentValue() && currentTerms.size() > 1) {
                        vpFilter.add(referenceVP);
                        break;
                    }
                    referenceTerms = currentTerms;
                } else {
                    if (!referenceTerms.containsAll(currentTerms)
                            || !currentTerms.containsAll(referenceTerms)) {
                        vpFilter.add(referenceVP);
                        break;
                    }
                }
            }
        }
    }
    return vpFilter;
}

From source file:accumulo.balancer.GroupBalancer.java

private void balanceExtraExtra(Map<TServerInstance, TserverGroupInfo> tservers, int maxExtraGroups,
        Moves moves) {/*w w  w  . ja va2 s. c  om*/
    Table<String, TServerInstance, TserverGroupInfo> surplusExtra = HashBasedTable.create();
    for (TserverGroupInfo tgi : tservers.values()) {
        Map<String, Integer> extras = tgi.getExtras();
        if (extras.size() > maxExtraGroups) {
            for (String group : extras.keySet()) {
                surplusExtra.put(group, tgi.getTserverInstance(), tgi);
            }
        }
    }

    ArrayList<Pair<String, TServerInstance>> serversGroupsToRemove = new ArrayList<>();
    ArrayList<TServerInstance> serversToRemove = new ArrayList<>();

    for (TserverGroupInfo destTgi : tservers.values()) {
        if (surplusExtra.size() == 0) {
            break;
        }

        Map<String, Integer> extras = destTgi.getExtras();
        if (extras.size() < maxExtraGroups) {
            serversToRemove.clear();
            serversGroupsToRemove.clear();
            for (String group : surplusExtra.rowKeySet()) {
                if (!extras.containsKey(group)) {
                    TserverGroupInfo srcTgi = surplusExtra.row(group).values().iterator().next();

                    moves.move(group, 1, srcTgi, destTgi);

                    if (srcTgi.getExtras().size() <= maxExtraGroups) {
                        serversToRemove.add(srcTgi.getTserverInstance());
                    } else {
                        serversGroupsToRemove
                                .add(new Pair<String, TServerInstance>(group, srcTgi.getTserverInstance()));
                    }

                    if (destTgi.getExtras().size() >= maxExtraGroups || moves.size() >= getMaxMigrations()) {
                        break;
                    }
                }
            }

            if (serversToRemove.size() > 0) {
                surplusExtra.columnKeySet().removeAll(serversToRemove);
            }

            for (Pair<String, TServerInstance> pair : serversGroupsToRemove) {
                surplusExtra.remove(pair.getFirst(), pair.getSecond());
            }

            if (moves.size() >= getMaxMigrations()) {
                break;
            }
        }
    }
}

From source file:accumulo.balancer.GroupBalancer.java

private void balanceExtraExpected(Map<TServerInstance, TserverGroupInfo> tservers, int expectedExtra,
        Moves moves) {/*from w  w  w  .  j  a  v a  2 s .co  m*/

    Table<String, TServerInstance, TserverGroupInfo> extraSurplus = HashBasedTable.create();

    for (TserverGroupInfo tgi : tservers.values()) {
        Map<String, Integer> extras = tgi.getExtras();
        if (extras.size() > expectedExtra) {
            for (String group : extras.keySet()) {
                extraSurplus.put(group, tgi.getTserverInstance(), tgi);
            }
        }
    }

    ArrayList<TServerInstance> emptyServers = new ArrayList<>();
    ArrayList<Pair<String, TServerInstance>> emptyServerGroups = new ArrayList<>();
    for (TserverGroupInfo destTgi : tservers.values()) {
        if (extraSurplus.size() == 0) {
            break;
        }

        Map<String, Integer> extras = destTgi.getExtras();
        if (extras.size() < expectedExtra) {
            emptyServers.clear();
            emptyServerGroups.clear();
            nextGroup: for (String group : extraSurplus.rowKeySet()) {
                if (!extras.containsKey(group)) {
                    Iterator<TserverGroupInfo> iter = extraSurplus.row(group).values().iterator();
                    TserverGroupInfo srcTgi = iter.next();

                    while (srcTgi.getExtras().size() <= expectedExtra) {
                        if (iter.hasNext()) {
                            srcTgi = iter.next();
                        } else {
                            continue nextGroup;
                        }
                    }

                    moves.move(group, 1, srcTgi, destTgi);

                    if (srcTgi.getExtras().size() <= expectedExtra) {
                        emptyServers.add(srcTgi.getTserverInstance());
                    } else if (srcTgi.getExtras().get(group) == null) {
                        emptyServerGroups
                                .add(new Pair<String, TServerInstance>(group, srcTgi.getTserverInstance()));
                    }

                    if (destTgi.getExtras().size() >= expectedExtra || moves.size() >= getMaxMigrations()) {
                        break;
                    }
                }
            }

            if (emptyServers.size() > 0) {
                extraSurplus.columnKeySet().removeAll(emptyServers);
            }

            for (Pair<String, TServerInstance> pair : emptyServerGroups) {
                extraSurplus.remove(pair.getFirst(), pair.getSecond());
            }

            if (moves.size() >= getMaxMigrations()) {
                break;
            }
        }
    }
}