Example usage for com.google.common.collect Multimap asMap

List of usage examples for com.google.common.collect Multimap asMap

Introduction

In this page you can find the example usage for com.google.common.collect Multimap asMap.

Prototype

Map<K, Collection<V>> asMap();

Source Link

Document

Returns a view of this multimap as a Map from each distinct key to the nonempty collection of that key's associated values.

Usage

From source file:com.willowtreeapps.saguaro.plugin.SaguaroGenerate.java

private Set<LicenseDependency> collectDefinedLicenses(SaguaroConfig config) {
    Set<LicenseDependency> licenseDependencies = new LinkedHashSet<LicenseDependency>();
    Multimap<String, LicenseInfo> licenseMap = HashMultimap.create();

    for (License license : config.getLicenses()) {
        for (String name : license.getLibraries()) {
            licenseMap.put(name, license.getLicenseInfo());
        }//from  w ww.  j a  v a2  s  . c  o  m
    }

    for (Map.Entry<String, Collection<LicenseInfo>> entry : licenseMap.asMap().entrySet()) {
        licenseDependencies
                .add(LicenseDependency.of(entry.getKey(), new LinkedHashSet<LicenseInfo>(entry.getValue())));
    }

    return licenseDependencies;
}

From source file:org.apache.phoenix.index.PhoenixIndexFailurePolicy.java

private void handleFailureWithExceptions(Multimap<HTableInterfaceReference, Mutation> attempted,
        Exception cause) throws Throwable {
    Set<HTableInterfaceReference> refs = attempted.asMap().keySet();
    Map<String, Long> indexTableNames = new HashMap<String, Long>(refs.size());
    // start by looking at all the tables to which we attempted to write
    for (HTableInterfaceReference ref : refs) {
        long minTimeStamp = 0;

        // get the minimum timestamp across all the mutations we attempted on that table
        Collection<Mutation> mutations = attempted.get(ref);
        if (mutations != null) {
            for (Mutation m : mutations) {
                for (List<Cell> kvs : m.getFamilyCellMap().values()) {
                    for (Cell kv : kvs) {
                        if (minTimeStamp == 0 || (kv.getTimestamp() >= 0 && minTimeStamp < kv.getTimestamp())) {
                            minTimeStamp = kv.getTimestamp();
                        }/*www. j ava2 s  .  co  m*/
                    }
                }
            }
        }

        // its a local index table, so we need to convert it to the index table names we should disable
        if (ref.getTableName().startsWith(MetaDataUtil.LOCAL_INDEX_TABLE_PREFIX)) {
            for (String tableName : getLocalIndexNames(ref, mutations)) {
                indexTableNames.put(tableName, minTimeStamp);
            }
        } else {
            indexTableNames.put(ref.getTableName(), minTimeStamp);
        }
    }

    // for all the index tables that we've found, try to disable them and if that fails, try to
    for (Map.Entry<String, Long> tableTimeElement : indexTableNames.entrySet()) {
        String indexTableName = tableTimeElement.getKey();
        long minTimeStamp = tableTimeElement.getValue();
        // Disable the index by using the updateIndexState method of MetaDataProtocol end point coprocessor.
        byte[] indexTableKey = SchemaUtil.getTableKeyFromFullName(indexTableName);
        HTableInterface systemTable = env
                .getTable(TableName.valueOf(PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME_BYTES));
        // Mimic the Put that gets generated by the client on an update of the index state
        Put put = new Put(indexTableKey);
        put.add(PhoenixDatabaseMetaData.TABLE_FAMILY_BYTES, PhoenixDatabaseMetaData.INDEX_STATE_BYTES,
                PIndexState.DISABLE.getSerializedBytes());
        put.add(PhoenixDatabaseMetaData.TABLE_FAMILY_BYTES,
                PhoenixDatabaseMetaData.INDEX_DISABLE_TIMESTAMP_BYTES, PLong.INSTANCE.toBytes(minTimeStamp));
        final List<Mutation> tableMetadata = Collections.<Mutation>singletonList(put);

        final Map<byte[], MetaDataResponse> results = systemTable.coprocessorService(MetaDataService.class,
                indexTableKey, indexTableKey, new Batch.Call<MetaDataService, MetaDataResponse>() {
                    @Override
                    public MetaDataResponse call(MetaDataService instance) throws IOException {
                        ServerRpcController controller = new ServerRpcController();
                        BlockingRpcCallback<MetaDataResponse> rpcCallback = new BlockingRpcCallback<MetaDataResponse>();
                        UpdateIndexStateRequest.Builder builder = UpdateIndexStateRequest.newBuilder();
                        for (Mutation m : tableMetadata) {
                            MutationProto mp = ProtobufUtil.toProto(m);
                            builder.addTableMetadataMutations(mp.toByteString());
                        }
                        instance.updateIndexState(controller, builder.build(), rpcCallback);
                        if (controller.getFailedOn() != null) {
                            throw controller.getFailedOn();
                        }
                        return rpcCallback.get();
                    }
                });
        if (results.isEmpty()) {
            throw new IOException("Didn't get expected result size");
        }
        MetaDataResponse tmpResponse = results.values().iterator().next();
        MetaDataMutationResult result = MetaDataMutationResult.constructFromProto(tmpResponse);

        if (result.getMutationCode() == MutationCode.TABLE_NOT_FOUND) {
            LOG.info("Index " + indexTableName + " has been dropped. Ignore uncommitted mutations");
            continue;
        }
        if (result.getMutationCode() != MutationCode.TABLE_ALREADY_EXISTS) {
            LOG.warn("Attempt to disable index " + indexTableName + " failed with code = "
                    + result.getMutationCode() + ". Will use default failure policy instead.");
            throw new DoNotRetryIOException("Attempt to disable " + indexTableName + " failed.");
        }
        LOG.info(
                "Successfully disabled index " + indexTableName + " due to an exception while writing updates.",
                cause);
    }
}

From source file:org.apache.hadoop.hbase.index.write.recovery.TrackingParallelWriterIndexCommitter.java

@Override
public void write(Multimap<HTableInterfaceReference, Mutation> toWrite) throws MultiIndexWriteFailureException {
    Set<Entry<HTableInterfaceReference, Collection<Mutation>>> entries = toWrite.asMap().entrySet();
    TaskBatch<Boolean> tasks = new TaskBatch<Boolean>(entries.size());
    List<HTableInterfaceReference> tables = new ArrayList<HTableInterfaceReference>(entries.size());
    for (Entry<HTableInterfaceReference, Collection<Mutation>> entry : entries) {
        // get the mutations for each table. We leak the implementation here a little bit to save
        // doing a complete copy over of all the index update for each table.
        final List<Mutation> mutations = (List<Mutation>) entry.getValue();
        // track each reference so we can get at it easily later, when determing failures
        final HTableInterfaceReference tableReference = entry.getKey();
        tables.add(tableReference);/*  www  .j  a va 2s.c  om*/

        /*
         * Write a batch of index updates to an index table. This operation stops (is cancelable) via
         * two mechanisms: (1) setting aborted or stopped on the IndexWriter or, (2) interrupting the
         * running thread. The former will only work if we are not in the midst of writing the current
         * batch to the table, though we do check these status variables before starting and before
         * writing the batch. The latter usage, interrupting the thread, will work in the previous
         * situations as was at some points while writing the batch, depending on the underlying
         * writer implementation (HTableInterface#batch is blocking, but doesn't elaborate when is
         * supports an interrupt).
         */
        tasks.add(new Task<Boolean>() {

            /**
             * Do the actual write to the primary table. We don't need to worry about closing the table
             * because that is handled the {@link CachingHTableFactory}.
             */
            @Override
            public Boolean call() throws Exception {
                try {
                    // this may have been queued, but there was an abort/stop so we try to early exit
                    throwFailureIfDone();

                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Writing index update:" + mutations + " to table: " + tableReference);
                    }
                    HTableInterface table = factory.getTable(tableReference.get());
                    throwFailureIfDone();
                    table.batch(mutations);
                } catch (InterruptedException e) {
                    // reset the interrupt status on the thread
                    Thread.currentThread().interrupt();
                    throw e;
                } catch (Exception e) {
                    throw e;
                }
                return Boolean.TRUE;
            }

            private void throwFailureIfDone() throws SingleIndexWriteFailureException {
                if (stopped.isStopped() || abortable.isAborted() || Thread.currentThread().isInterrupted()) {
                    throw new SingleIndexWriteFailureException(
                            "Pool closed, not attempting to write to the index!", null);
                }

            }
        });
    }

    List<Boolean> results = null;
    try {
        LOG.debug("Waiting on index update tasks to complete...");
        results = this.pool.submitUninterruptible(tasks);
    } catch (ExecutionException e) {
        throw new RuntimeException("Should not fail on the results while using a WaitForCompletionTaskRunner",
                e);
    } catch (EarlyExitFailure e) {
        throw new RuntimeException("Stopped while waiting for batch, quiting!", e);
    }

    // track the failures. We only ever access this on return from our calls, so no extra
    // synchronization is needed. We could update all the failures as we find them, but that add a
    // lot of locking overhead, and just doing the copy later is about as efficient.
    List<HTableInterfaceReference> failures = new ArrayList<HTableInterfaceReference>();
    int index = 0;
    for (Boolean result : results) {
        // there was a failure
        if (result == null) {
            // we know which table failed by the index of the result
            failures.add(tables.get(index));
        }
        index++;
    }

    // if any of the tasks failed, then we need to propagate the failure
    if (failures.size() > 0) {
        // make the list unmodifiable to avoid any more synchronization concerns
        throw new MultiIndexWriteFailureException(Collections.unmodifiableList(failures));
    }
    return;
}

From source file:com.continuuity.weave.yarn.YarnWeavePreparer.java

private String encodeRunnableArguments(Multimap<String, String> args) {
    return new Gson().toJson(args.asMap());
}

From source file:fr.jcgay.maven.plugin.buildplan.ListPluginMojo.java

public void execute() throws MojoExecutionException, MojoFailureException {

    Multimap<String, MojoExecution> plan = Groups.ByPlugin.of(calculateExecutionPlan().getMojoExecutions(),
            plugin);/*  w  w w  . j  a va 2 s  . c o m*/

    if (plan.isEmpty()) {
        getLog().warn("No plugin found with artifactId: " + plugin);
    } else {
        TableDescriptor descriptor = ListPluginTableDescriptor.of(plan.values());
        for (Map.Entry<String, Collection<MojoExecution>> executions : plan.asMap().entrySet()) {
            getLog().info(pluginTitleLine(descriptor, executions.getKey()));
            for (MojoExecution execution : executions.getValue()) {
                getLog().info(line(descriptor.rowFormat(), execution));
            }
        }
    }
}

From source file:com.palantir.atlasdb.keyvalue.impl.ValidatingQueryRewritingKeyValueService.java

@Override
public void putWithTimestamps(String tableName, Multimap<Cell, Value> cellValues)
        throws KeyAlreadyExistsException {
    if (cellValues.isEmpty()) {
        return;/*w  w  w. j ava2 s . co m*/
    }
    Validate.isTrue(!tableName.equals(TransactionConstants.TRANSACTION_TABLE), TRANSACTION_ERROR);

    long lastTimestamp = -1;
    boolean allAtSameTimestamp = true;
    for (Value value : cellValues.values()) {
        long timestamp = value.getTimestamp();
        Validate.isTrue(timestamp != Long.MAX_VALUE);
        Validate.isTrue(timestamp >= 0);
        if (lastTimestamp != -1 && timestamp != lastTimestamp) {
            allAtSameTimestamp = false;
        }
        lastTimestamp = timestamp;
    }

    if (allAtSameTimestamp) {
        Multimap<Cell, byte[]> cellValuesWithStrippedTimestamp = Multimaps.transformValues(cellValues,
                Value.GET_VALUE);

        Map<Cell, byte[]> putMap = Maps.transformValues(cellValuesWithStrippedTimestamp.asMap(),
                new Function<Collection<byte[]>, byte[]>() {

                    @Override
                    public byte[] apply(Collection<byte[]> input) {
                        try {
                            return Iterables.getOnlyElement(input);
                        } catch (IllegalArgumentException e) {
                            log.error(
                                    "Application tried to put multiple same-cell values in at same timestamp; attempting to perform last-write-wins, but ordering is not guaranteed.");
                            return Iterables.getLast(input);
                        }
                    }

                });

        put(tableName, putMap, lastTimestamp);
        return;
    }
    delegate.putWithTimestamps(tableName, cellValues);
}

From source file:org.richfaces.application.push.impl.jms.SessionImpl.java

private void createSubscriptions(Iterable<TopicKey> topicKeys) {
    javax.jms.Session jmsSession = null;
    try {/*w  ww.  j ava  2  s .  co  m*/
        Multimap<TopicKey, TopicKey> rootTopicsMap = createRootTopicsKeysMap(topicKeys);

        jmsSession = messagingContext.createSession();

        for (Entry<TopicKey, Collection<TopicKey>> entry : rootTopicsMap.asMap().entrySet()) {
            TopicSubscriber subscriber = null;

            try {
                subscriber = messagingContext.createTopicSubscriber(this, jmsSession, entry);
                successfulSubscriptions.putAll(entry.getKey(), entry.getValue());
            } finally {
                if (subscriber != null) {
                    subscriber.close();
                }
            }

        }
    } catch (JMSException e) {
        LOGGER.error(e.getMessage(), e);
    } catch (NamingException e) {
        LOGGER.error(e.getMessage(), e);
    } finally {
        if (jmsSession != null) {
            try {
                jmsSession.close();
            } catch (JMSException e) {
                LOGGER.error(e.getMessage(), e);
            }
        }
    }
}

From source file:org.sonar.api.config.PropertyDefinitions.java

private Map<String, Collection<PropertyDefinition>> getPropertiesByCategory(PropertyDefinitionFilter filter) {
    Multimap<String, PropertyDefinition> byCategory = ArrayListMultimap.create();

    for (PropertyDefinition definition : getAll()) {
        if (filter.accept(definition)) {
            byCategory.put(getCategory(definition.getKey()), definition);
        }//from   w  ww  . j  a va2s . c  om
    }

    return byCategory.asMap();
}

From source file:org.sonar.server.component.es.ProjectMeasuresIndex.java

private Map<String, QueryBuilder> createFilters(ProjectMeasuresQuery query) {
    Map<String, QueryBuilder> filters = new HashMap<>();
    filters.put("__authorization", createAuthorizationFilter());
    Multimap<String, MetricCriterion> metricCriterionMultimap = ArrayListMultimap.create();
    query.getMetricCriteria().forEach(/*from   w ww  .  j  a va2  s. c  om*/
            metricCriterion -> metricCriterionMultimap.put(metricCriterion.getMetricKey(), metricCriterion));
    metricCriterionMultimap.asMap().entrySet().forEach(entry -> {
        BoolQueryBuilder metricFilters = boolQuery();
        entry.getValue().stream().map(criterion -> nestedQuery(FIELD_MEASURES, boolQuery()
                .filter(termQuery(FIELD_KEY, criterion.getMetricKey())).filter(toValueQuery(criterion))))
                .forEach(metricFilters::must);
        filters.put(entry.getKey(), metricFilters);

    });
    if (query.hasQualityGateStatus()) {
        filters.put(ALERT_STATUS_KEY, termQuery(FIELD_QUALITY_GATE, query.getQualityGateStatus().name()));
    }
    if (query.doesFilterOnProjectUuids()) {
        filters.put("ids", termsQuery("_id", query.getProjectUuids()));
    }
    return filters;
}

From source file:org.lanternpowered.server.network.vanilla.message.processor.play.ProcessorPlayOutTabListEntries.java

@Override
public void process(CodecContext context, MessagePlayOutTabListEntries message, List<Message> output)
        throws CodecException {
    final Multimap<Class<?>, Entry> entriesByType = HashMultimap.create();
    for (Entry entry : message.getEntries()) {
        entriesByType.put(entry.getClass(), entry);
    }//from   w ww .ja v a 2 s  .com
    if (entriesByType.isEmpty()) {
        return;
    }
    if (entriesByType.keySet().size() == 1) {
        output.add(message);
    } else {
        for (java.util.Map.Entry<Class<?>, Collection<Entry>> en : entriesByType.asMap().entrySet()) {
            output.add(new MessagePlayOutTabListEntries(en.getValue()));
        }
    }
}