Example usage for com.google.common.collect TreeBasedTable rowKeySet

List of usage examples for com.google.common.collect TreeBasedTable rowKeySet

Introduction

In this page you can find the example usage for com.google.common.collect TreeBasedTable rowKeySet.

Prototype

@Override
    public SortedSet<R> rowKeySet() 

Source Link

Usage

From source file:com.gradleware.tooling.toolingmodel.repository.internal.DefaultOmniBuildInvocationsContainerBuilder.java

@SuppressWarnings("StringEquality")
private static ImmutableMultimap<Path, OmniTaskSelector> buildTaskSelectorsRecursively(GradleProject project,
        Multimap<Path, OmniTaskSelector> taskSelectorsPerProject, boolean enforceAllTasksPublic) {
    // add task selectors of the current project
    TreeBasedTable<String, Path, String> aggregatedTasksWithDescription = TreeBasedTable
            .create(Ordering.usingToString(), Path.Comparator.INSTANCE);
    Set<String> publicTasks = Sets.newLinkedHashSet();
    collectAllTasksRecursively(project, aggregatedTasksWithDescription, publicTasks, enforceAllTasksPublic);
    for (String selectorName : aggregatedTasksWithDescription.rowKeySet()) {
        SortedMap<Path, String> pathsAndDescriptions = aggregatedTasksWithDescription.row(selectorName);
        String description = pathsAndDescriptions.get(pathsAndDescriptions.firstKey()); // description from project task with smallest path
        SortedSet<Path> fqnTaskNames = ImmutableSortedSet.orderedBy(Path.Comparator.INSTANCE)
                .addAll(pathsAndDescriptions.keySet()).build();

        OmniTaskSelector taskSelector = DefaultOmniTaskSelector.from(selectorName,
                description != NULL_STRING ? description : null, Path.from(project.getPath()),
                publicTasks.contains(selectorName), fqnTaskNames);

        taskSelectorsPerProject.put(Path.from(project.getPath()), taskSelector);
    }/*  ww w  . j av a 2s .  c o m*/

    // recurse into child projects and add their task selectors
    DomainObjectSet<? extends GradleProject> childProjects = project.getChildren();
    for (GradleProject childProject : childProjects) {
        buildTaskSelectorsRecursively(childProject, taskSelectorsPerProject, enforceAllTasksPublic);
    }

    // return the task selectors grouped by project path
    return ImmutableMultimap.copyOf(taskSelectorsPerProject);
}

From source file:com.oodrive.nuage.dtx.DtxTestHelper.java

/**
 * Prepares, i.e. writes transactions to a set of journals according to the information given as a {@link Table}.
 * /*from  ww  w .  j  a v  a 2 s. c o  m*/
 * @param dtxMgrTxTable
 *            (sorted) {@link TreeBasedTable} mapping resource manager {@link UUID}s, last transaction IDs to
 *            {@link DtxManager} instances
 * @param journalDirMap
 *            a {@link Map} providing the temporary directories for each {@link DtxManager}
 * @param setupRotMgr
 *            a central {@link JournalRotationManager} used to write the prepared journals
 * @return a {@link Table} of non-failing mock {@link DtxResourceManager}s with their last transaction ID and
 *         journal file directories
 * @throws IllegalStateException
 *             if writing to journals fails due to their internal state
 * @throws IOException
 *             if writing or reading data fails
 * @throws XAException
 *             if mock setup fails
 */
public static final Table<DtxResourceManager, Long, Path> prepareExistingJournals(
        final TreeBasedTable<Long, UUID, DtxManager> dtxMgrTxTable, final Map<DtxManager, Path> journalDirMap,
        final JournalRotationManager setupRotMgr) throws IllegalStateException, IOException, XAException {

    // reference map to order resource managers by their last tx ID
    final Map<DtxResourceManager, Long> rankMap = new HashMap<DtxResourceManager, Long>();

    final Comparator<DtxResourceManager> rowComp = new Comparator<DtxResourceManager>() {

        @Override
        public final int compare(final DtxResourceManager o1, final DtxResourceManager o2) {
            // compare last tx IDs before falling back to classic comparison
            final Long rank1 = rankMap.get(o1);
            final Long rank2 = rankMap.get(o2);
            final int rankComp = Long.compare(rank1.longValue(), rank2.longValue()) * -1;
            if (rankComp != 0) {
                return rankComp;
            }

            // fall back to comparing IDs
            final UUID id1 = o1.getId();
            final UUID id2 = o2.getId();
            final int idComp = id1.compareTo(id2);

            // maintain coherence with equals()
            if (idComp == 0) {
                return Integer.compare(o1.hashCode(), o2.hashCode());
            }
            return idComp;
        }
    };

    final Comparator<Long> columnComp = Collections.reverseOrder();

    final Table<DtxResourceManager, Long, Path> result = TreeBasedTable.create(rowComp, columnComp);

    final HashMap<UUID, DtxManager> previousLog = new HashMap<UUID, DtxManager>();

    long lastTxId = TX_ID.get();

    for (final Long currTargetTxId : dtxMgrTxTable.rowKeySet()) {

        final long targetTxId = currTargetTxId.longValue();
        if (targetTxId <= lastTxId) {
            throw new IllegalArgumentException(
                    "Not increasing transaction IDs; lastTxId=" + lastTxId + ", targetTxId=" + targetTxId);
        }

        final Set<TxNode> participants = newRandomParticipantsSet();

        final SortedMap<UUID, DtxManager> currRow = dtxMgrTxTable.row(currTargetTxId);

        // / insert here
        for (final UUID currResMgrId : currRow.keySet()) {

            final DtxResourceManager currResMgr = DtxDummyRmFactory
                    .newResMgrThatDoesEverythingRight(currResMgrId);

            final DtxManager currDtxMgr = currRow.get(currResMgrId);
            final Path currTmpDir = journalDirMap.get(currDtxMgr);

            final String journalFilename = newJournalFilePrefix(currDtxMgr.getNodeId(), currResMgrId);

            final WritableTxJournal targetJournal = new WritableTxJournal(currTmpDir.toFile(), journalFilename,
                    0, setupRotMgr);

            targetJournal.start();
            assertEquals(DEFAULT_LAST_TX_VALUE, targetJournal.getLastFinishedTxId());

            final WritableTxJournal prevJournal;
            final DtxManager prevDtxMgr = previousLog.get(currResMgrId);
            if (prevDtxMgr != null) {
                prevJournal = new WritableTxJournal(journalDirMap.get(prevDtxMgr).toFile(),
                        newJournalFilePrefix(prevDtxMgr.getNodeId(), currResMgrId), 0, setupRotMgr);
            } else {
                prevJournal = null;
            }

            // re-reads the previous journal and copies it to the new target
            if (prevJournal != null) {
                prevJournal.start();
                for (final JournalRecord currRecord : prevJournal.newReadOnlyTxJournal()) {
                    final TxJournalEntry currEntry = TxJournalEntry.parseFrom(currRecord.getEntry());
                    switch (currEntry.getOp()) {
                    case START:
                        targetJournal.writeStart(currEntry.getTx(), currEntry.getTxNodesList());
                        break;
                    case COMMIT:
                        targetJournal.writeCommit(currEntry.getTxId(), currEntry.getTxNodesList());
                        break;
                    case ROLLBACK:
                        targetJournal.writeRollback(currEntry.getTxId(), currEntry.getErrCode(),
                                currEntry.getTxNodesList());
                        break;
                    default:
                        // nothing
                    }
                }
                prevJournal.stop();
            }

            final int nbToWrite = Long
                    .valueOf(lastTxId == DEFAULT_LAST_TX_VALUE ? targetTxId - 1 : targetTxId - lastTxId)
                    .intValue();
            lastTxId = DtxTestHelper.writeCompleteTransactions(targetJournal, nbToWrite, currResMgrId,
                    participants);
            assertEquals(targetTxId, lastTxId);
            assertEquals(lastTxId, targetJournal.getLastFinishedTxId());
            targetJournal.stop();

            previousLog.put(currResMgrId, currDtxMgr);

            rankMap.put(currResMgr, currTargetTxId);
            result.put(currResMgr, currTargetTxId, currTmpDir);
        }

    }
    return result;
}