Example usage for com.google.common.collect TreeMultimap create

List of usage examples for com.google.common.collect TreeMultimap create

Introduction

In this page you can find the example usage for com.google.common.collect TreeMultimap create.

Prototype

public static <K extends Comparable, V extends Comparable> TreeMultimap<K, V> create() 

Source Link

Document

Creates an empty TreeMultimap ordered by the natural ordering of its keys and values.

Usage

From source file:com.ikanow.infinit.e.harvest.extraction.document.HarvestStatus_Standalone.java

/**
 * getLogMessages//  w  ww.  j  a v  a2s . c o m
 * Returns a list of up to 20 errors (eg encountered when parsing JavaScrip)t for 
 * a source, sorted by frequency in ascending order
 * @return
 */
private StringBuffer getLogMessages(boolean bReset) {
    if ((null != _messages) && (_messages.size() > 0)) {
        StringBuffer messagesString = new StringBuffer();

        // Create multimap to store errors in, reverse the order of key (error message) and
        // value (count) to sort on error count
        Multimap<Integer, String> mm = TreeMultimap.create();
        for (java.util.Map.Entry<String, Integer> entry : _messages.entrySet()) {
            StringBuffer msg = new StringBuffer(entry.getKey()).append(" (Occurences: ")
                    .append(entry.getValue()).append(')');
            mm.put(-entry.getValue(), msg.toString());
        }

        // Write the error messages to a Collection<String>
        Collection<String> messages = mm.values();

        // Append up to the top 20 messages to our StringBuffer and return
        int messageCount = 1;
        for (String s : messages) {
            if (messageCount > 1) {
                messagesString.append('\n');
            }
            messagesString.append(s);
            messageCount++;
            if (messageCount > 20)
                break;
        }
        if (bReset) {
            _messages.clear();
        }
        return messagesString;
    } else {
        return null;
    }
}

From source file:org.jclouds.s3.filters.RequestAuthorizeSignatureV2.java

public String createStringToSign(HttpRequest request) {
    utils.logRequest(signatureLog, request, ">>");
    SortedSetMultimap<String, String> canonicalizedHeaders = TreeMultimap.create();
    StringBuilder buffer = new StringBuilder();
    // re-sign the request
    appendMethod(request, buffer);// w  w w .j  a  va  2s  . c o m
    appendPayloadMetadata(request, buffer);
    appendHttpHeaders(request, canonicalizedHeaders);

    // Remove default date timestamp if "x-amz-date" is set.
    if (canonicalizedHeaders.containsKey("x-" + headerTag + "-date")) {
        canonicalizedHeaders.removeAll("date");
    }

    appendAmzHeaders(canonicalizedHeaders, buffer);
    appendBucketName(request, buffer);
    appendUriPath(request, buffer);
    if (signatureWire.enabled()) {
        signatureWire.output(buffer.toString());
    }
    return buffer.toString();
}

From source file:voldemort.client.rebalance.RebalancePlan.java

/**
 * Create a plan. The plan consists of batches. Each batch involves the
 * movement of no more than batchSize primary partitions. The movement of a
 * single primary partition may require migration of other n-ary replicas,
 * and potentially deletions. Migrating a primary or n-ary partition
 * requires migrating one partition-store for every store hosted at that
 * partition./* w  w w.ja  va2  s .co  m*/
 * 
 */
private void plan() {
    // Mapping of stealer node to list of primary partitions being moved
    final TreeMultimap<Integer, Integer> stealerToStolenPrimaryPartitions = TreeMultimap.create();

    // Output initial and final cluster
    if (outputDir != null)
        RebalanceUtils.dumpClusters(currentCluster, finalCluster, outputDir);

    // Determine which partitions must be stolen
    for (Node stealerNode : finalCluster.getNodes()) {
        List<Integer> stolenPrimaryPartitions = RebalanceUtils.getStolenPrimaryPartitions(currentCluster,
                finalCluster, stealerNode.getId());
        if (stolenPrimaryPartitions.size() > 0) {
            numPrimaryPartitionMoves += stolenPrimaryPartitions.size();
            stealerToStolenPrimaryPartitions.putAll(stealerNode.getId(), stolenPrimaryPartitions);
        }
    }

    // Determine plan batch-by-batch
    int batches = 0;
    Cluster batchCurrentCluster = Cluster.cloneCluster(currentCluster);
    List<StoreDefinition> batchCurrentStoreDefs = this.currentStoreDefs;
    List<StoreDefinition> batchFinalStoreDefs = this.finalStoreDefs;
    Cluster batchFinalCluster = RebalanceUtils.getInterimCluster(this.currentCluster, this.finalCluster);

    while (!stealerToStolenPrimaryPartitions.isEmpty()) {

        int partitions = 0;
        List<Entry<Integer, Integer>> partitionsMoved = Lists.newArrayList();
        for (Entry<Integer, Integer> stealerToPartition : stealerToStolenPrimaryPartitions.entries()) {
            partitionsMoved.add(stealerToPartition);
            batchFinalCluster = UpdateClusterUtils.createUpdatedCluster(batchFinalCluster,
                    stealerToPartition.getKey(), Lists.newArrayList(stealerToPartition.getValue()));
            partitions++;
            if (partitions == batchSize)
                break;
        }

        // Remove the partitions moved
        for (Iterator<Entry<Integer, Integer>> partitionMoved = partitionsMoved.iterator(); partitionMoved
                .hasNext();) {
            Entry<Integer, Integer> entry = partitionMoved.next();
            stealerToStolenPrimaryPartitions.remove(entry.getKey(), entry.getValue());
        }

        if (outputDir != null)
            RebalanceUtils.dumpClusters(batchCurrentCluster, batchFinalCluster, outputDir,
                    "batch-" + Integer.toString(batches) + ".");

        // Generate a plan to compute the tasks
        final RebalanceBatchPlan RebalanceBatchPlan = new RebalanceBatchPlan(batchCurrentCluster,
                batchCurrentStoreDefs, batchFinalCluster, batchFinalStoreDefs);
        batchPlans.add(RebalanceBatchPlan);

        numXZonePartitionStoreMoves += RebalanceBatchPlan.getCrossZonePartitionStoreMoves();
        numPartitionStoreMoves += RebalanceBatchPlan.getPartitionStoreMoves();
        nodeMoveMap.add(RebalanceBatchPlan.getNodeMoveMap());
        zoneMoveMap.add(RebalanceBatchPlan.getZoneMoveMap());

        batches++;
        batchCurrentCluster = Cluster.cloneCluster(batchFinalCluster);
        // batchCurrentStoreDefs can only be different from
        // batchFinalStoreDefs for the initial batch.
        batchCurrentStoreDefs = batchFinalStoreDefs;
    }

    logger.info(this);
}

From source file:com.facebook.buck.android.MergeAndroidResourcesStep.java

@VisibleForTesting
static SortedSetMultimap<String, Resource> sortSymbols(Map<Path, String> symbolsFileToRDotJavaPackage,
        ProjectFilesystem filesystem, boolean reenumerate) {
    // If we're reenumerating, start at 0x7f01001 so that the resulting file is human readable.
    // This value range (0x7f010001 - ...) is easier to spot as an actual resource id instead of
    // other values in styleable which can be enumerated integers starting at 0.
    IntEnumerator enumerator = reenumerate ? new IntEnumerator(0x7f01001) : null;
    SortedSetMultimap<String, Resource> rDotJavaPackageToSymbolsFiles = TreeMultimap.create();
    for (Map.Entry<Path, String> entry : symbolsFileToRDotJavaPackage.entrySet()) {
        Path symbolsFile = entry.getKey();

        // Read the symbols file and parse each line as a Resource.
        List<String> linesInSymbolsFile;
        try {/* w  w  w .java 2  s .co m*/
            linesInSymbolsFile = FluentIterable.from(filesystem.readLines(symbolsFile))
                    .filter(MoreStrings.NON_EMPTY).toList();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        String packageName = entry.getValue();
        for (String line : linesInSymbolsFile) {
            Matcher matcher = TEXT_SYMBOLS_LINE.matcher(line);
            boolean isMatch = matcher.matches();
            Preconditions.checkState(isMatch, "Should be able to match '%s'.", line);
            String idType = matcher.group(1);
            String type = matcher.group(2);
            String name = matcher.group(3);
            String idValue = matcher.group(4);

            // We're only doing the remapping so Roboelectric is happy and it is already ignoring the
            // id references found in the styleable section.  So let's do that as well so we don't have
            // to get fancier than is needed.  That is, just re-enumerate all app-level resource ids
            // and ignore everything else, allowing the styleable references to be messed up.
            String idValueToUse = idValue;
            if (reenumerate && idValue.startsWith("0x7f")) {
                idValueToUse = String.format("0x%08x", enumerator.next());
            }

            Resource resource = new Resource(idType, type, name, idValue, idValueToUse);
            rDotJavaPackageToSymbolsFiles.put(packageName, resource);
        }
    }
    return rDotJavaPackageToSymbolsFiles;
}

From source file:org.jclouds.s3.filters.RequestAuthorizeSignature.java

public String createStringToSign(HttpRequest request) {
    utils.logRequest(signatureLog, request, ">>");
    SortedSetMultimap<String, String> canonicalizedHeaders = TreeMultimap.create();
    StringBuilder buffer = new StringBuilder();
    // re-sign the request
    appendMethod(request, buffer);//from   ww  w  .  j a  va  2 s  . c  o m
    appendPayloadMetadata(request, buffer);
    appendHttpHeaders(request, canonicalizedHeaders);

    // Remove default date timestamp if "x-amz-date" is set.
    if (canonicalizedHeaders.containsKey("x-" + headerTag + "-date")) {
        canonicalizedHeaders.removeAll("date");
    }

    appendAmzHeaders(canonicalizedHeaders, buffer);
    appendBucketName(request, buffer);
    appendUriPath(request, buffer);
    if (signatureWire.enabled())
        signatureWire.output(buffer.toString());
    return buffer.toString();
}

From source file:org.sonar.server.permission.ws.UsersAction.java

private static UsersWsResponse buildResponse(List<UserDto> users, List<UserPermissionDto> userPermissions,
        Paging paging) {//www . jav a2 s  .  c om
    Multimap<Integer, String> permissionsByUserId = TreeMultimap.create();
    userPermissions.forEach(userPermission -> permissionsByUserId.put(userPermission.getUserId(),
            userPermission.getPermission()));

    UsersWsResponse.Builder response = UsersWsResponse.newBuilder();
    users.forEach(user -> {
        WsPermissions.User.Builder userResponse = response.addUsersBuilder().setLogin(user.getLogin())
                .addAllPermissions(permissionsByUserId.get(user.getId()));
        setNullable(user.getEmail(), userResponse::setEmail);
        setNullable(user.getName(), userResponse::setName);
    });

    response.getPagingBuilder().setPageIndex(paging.pageIndex()).setPageSize(paging.pageSize())
            .setTotal(paging.total()).build();

    return response.build();
}

From source file:com.facebook.buck.rules.MergeAndroidResourcesStep.java

@VisibleForTesting
static SortedSetMultimap<String, Resource> sortSymbols(Function<String, Readable> filePathToReadable,
        Map<String, String> symbolsFileToRDotJavaPackage, boolean reenumerate) {
    // If we're reenumerating, start at 0x7f01001 so that the resulting file is human readable.
    // This value range (0x7f010001 - ...) is easier to spot as an actual resource id instead of
    // other values in styleable which can be enumerated integers starting at 0.
    IntEnumerator enumerator = reenumerate ? new IntEnumerator(0x7f01001) : null;
    SortedSetMultimap<String, Resource> rDotJavaPackageToSymbolsFiles = TreeMultimap.create();
    for (Map.Entry<String, String> entry : symbolsFileToRDotJavaPackage.entrySet()) {
        String symbolsFile = entry.getKey();
        String packageName = entry.getValue();

        // Read the symbols file and parse each line as a Resource.
        Readable readable = filePathToReadable.apply(symbolsFile);
        Scanner scanner = new Scanner(readable);
        while (scanner.hasNext()) {
            String line = scanner.nextLine();
            Matcher matcher = TEXT_SYMBOLS_LINE.matcher(line);
            boolean isMatch = matcher.matches();
            Preconditions.checkState(isMatch, "Should be able to match '%s'.", line);
            String idType = matcher.group(1);
            String type = matcher.group(2);
            String name = matcher.group(3);
            String idValue = matcher.group(4);

            // We're only doing the remapping so Roboelectric is happy and it is already ignoring the
            // id references found in the styleable section.  So let's do that as well so we don't have
            // to get fancier than is needed.  That is, just re-enumerate all app-level resource ids
            // and ignore everything else, allowing the styleable references to be messed up.
            String idValueToUse = idValue;
            if (reenumerate && idValue.startsWith("0x7f")) {
                idValueToUse = String.format("0x%08x", enumerator.next());
            }//from   w  w w .j a v a 2  s.  c  om

            Resource resource = new Resource(idType, type, name, idValue, idValueToUse);
            rDotJavaPackageToSymbolsFiles.put(packageName, resource);
        }
    }
    return rDotJavaPackageToSymbolsFiles;
}

From source file:com.facebook.buck.cli.QueryCommand.java

/**
 * Evaluate multiple queries in a single `buck query` run. Usage:
 *   buck query <query format> <input1> <input2> <...> <inputN>
 */// w  w w.  j av  a2s  . co  m
static int runMultipleQuery(CommandRunnerParams params, BuckQueryEnvironment env,
        ListeningExecutorService executor, String queryFormat, List<String> inputsFormattedAsBuildTargets,
        boolean generateJsonOutput) throws IOException, InterruptedException, QueryException {
    if (inputsFormattedAsBuildTargets.isEmpty()) {
        params.getBuckEventBus().post(
                ConsoleEvent.severe("Specify one or more input targets after the query expression format"));
        return 1;
    }

    // Do an initial pass over the query arguments and parse them into their expressions so we can
    // preload all the target patterns from every argument in one go, as doing them one-by-one is
    // really inefficient.
    Set<String> targetLiterals = new LinkedHashSet<>();
    for (String input : inputsFormattedAsBuildTargets) {
        String query = queryFormat.replace("%s", input);
        QueryExpression expr = QueryExpression.parse(query, env);
        expr.collectTargetPatterns(targetLiterals);
    }
    env.preloadTargetPatterns(targetLiterals, executor);

    // Now execute the query on the arguments one-by-one.
    TreeMultimap<String, QueryTarget> queryResultMap = TreeMultimap.create();
    for (String input : inputsFormattedAsBuildTargets) {
        String query = queryFormat.replace("%s", input);
        Set<QueryTarget> queryResult = env.evaluateQuery(query, executor);
        queryResultMap.putAll(input, queryResult);
    }

    LOG.debug("Printing out the following targets: " + queryResultMap);
    if (generateJsonOutput) {
        CommandHelper.printJSON(params, queryResultMap);
    } else {
        CommandHelper.printToConsole(params, queryResultMap);
    }
    return 0;
}

From source file:org.sonar.server.permission.ws.TemplateUsersAction.java

private WsPermissions.UsersWsResponse buildResponse(List<UserDto> users,
        List<PermissionTemplateUserDto> permissionTemplateUsers, Paging paging) {
    Multimap<Long, String> permissionsByUserId = TreeMultimap.create();
    permissionTemplateUsers.forEach(userPermission -> permissionsByUserId.put(userPermission.getUserId(),
            userPermission.getPermission()));

    UsersWsResponse.Builder responseBuilder = UsersWsResponse.newBuilder();
    users.forEach(user -> {/*from w  w  w .  ja va  2 s  .  c  o  m*/
        WsPermissions.User.Builder userResponse = responseBuilder.addUsersBuilder().setLogin(user.getLogin())
                .addAllPermissions(permissionsByUserId.get(user.getId()));
        if (user.getEmail() != null) {
            userResponse.setEmail(user.getEmail());
        }
        if (user.getName() != null) {
            userResponse.setName(user.getName());
        }
    });
    responseBuilder.getPagingBuilder().setPageIndex(paging.pageIndex()).setPageSize(paging.pageSize())
            .setTotal(paging.total()).build();
    return responseBuilder.build();
}

From source file:com.streamsets.pipeline.stage.processor.fuzzy.FuzzyFieldProcessor.java

private TreeMultimap<String, MatchCandidate> findCandidatesFor(Map<String, Field> fields)
        throws StageException {
    TreeMultimap<String, MatchCandidate> candidates = TreeMultimap.create();

    for (String outputField : outputFieldNames) {
        for (Map.Entry<String, Field> entry : fields.entrySet()) {
            String fieldPath = entry.getKey();
            Field field = entry.getValue();
            int score = FuzzyMatch.getRatio(outputField, fieldPath, true);
            if (score >= matchThreshold) {
                if (greaterScore(candidates.get(outputField), score) || allCandidates) {
                    if (!allCandidates) {
                        // If not storing all candidates we must clear any existing candidates for this key
                        // since a Multimap won't replace multiple values for a key.
                        candidates.get(outputField).clear();
                    }/*  ww  w . j  a v  a2  s. c  o  m*/
                    candidates.put(outputField, new MatchCandidate(outputField, score, fieldPath, field));
                }
            }
        }
    }

    return candidates;
}