Example usage for com.google.common.collect MultimapBuilder hashKeys

List of usage examples for com.google.common.collect MultimapBuilder hashKeys

Introduction

In this page you can find the example usage for com.google.common.collect MultimapBuilder hashKeys.

Prototype

public static MultimapBuilderWithKeys<Object> hashKeys() 

Source Link

Document

Uses a HashMap to map keys to value collections.

Usage

From source file:com.google.gerrit.server.notedb.ChangeNotesParser.java

private ListMultimap<PatchSet.Id, PatchSetApproval> buildApprovals() {
    ListMultimap<PatchSet.Id, PatchSetApproval> result = MultimapBuilder.hashKeys().arrayListValues().build();
    for (PatchSetApproval a : approvals.values()) {
        if (!patchSets.containsKey(a.getPatchSetId())) {
            continue; // Patch set deleted or missing.
        } else if (allPastReviewers.contains(a.getAccountId()) && !reviewers.containsRow(a.getAccountId())) {
            continue; // Reviewer was explicitly removed.
        }/*from  w  ww.j  a v a2 s .c o  m*/
        result.put(a.getPatchSetId(), a);
    }
    for (Collection<PatchSetApproval> v : result.asMap().values()) {
        Collections.sort((List<PatchSetApproval>) v, ChangeNotes.PSA_BY_TIME);
    }
    return result;
}

From source file:com.facebook.presto.accumulo.tools.RewriteIndex.java

private void deleteIndexEntries(Connector connector, AccumuloTable table, long start) {
    LOG.info(format("Scanning index table %s to delete index entries", table.getIndexTableName()));
    BatchScanner scanner = null;/* ww w  .  j  a  v  a  2 s  .c o  m*/
    BatchWriter indexWriter = null;
    try {
        // Create index writer and metrics writer, but we are never going to flush the metrics writer
        indexWriter = connector.createBatchWriter(table.getIndexTableName(), bwc);
        scanner = connector.createBatchScanner(table.getIndexTableName(), auths, 10);
        LOG.info(format("Created batch scanner against %s with auths %s", table.getIndexTableName(), auths));

        IteratorSetting timestampFilter = new IteratorSetting(21, "timestamp", TimestampFilter.class);
        TimestampFilter.setRange(timestampFilter, 0L, start);
        scanner.addScanIterator(timestampFilter);

        scanner.setRanges(connector.tableOperations().splitRangeByTablets(table.getIndexTableName(),
                new Range(), Integer.MAX_VALUE));

        // Scan the index table, gathering row IDs into batches
        long numTotalMutations = 0L;

        Map<ByteBuffer, RowStatus> rowIdStatuses = new HashMap<>();
        Multimap<ByteBuffer, Mutation> queryIndexEntries = MultimapBuilder.hashKeys().hashSetValues().build();
        Text text = new Text();
        for (Entry<Key, Value> entry : scanner) {
            ++numTotalMutations;

            ByteBuffer rowID = ByteBuffer.wrap(entry.getKey().getColumnQualifier(text).copyBytes());
            Mutation mutation = new Mutation(entry.getKey().getRow(text).copyBytes());
            mutation.putDelete(entry.getKey().getColumnFamily(text).copyBytes(),
                    entry.getKey().getColumnQualifier(text).copyBytes(),
                    entry.getKey().getColumnVisibilityParsed(), start);

            // Get status of this row ID
            switch (rowIdStatuses.getOrDefault(rowID, RowStatus.UNKNOWN)) {
            case ABSENT:
            case UNKNOWN:
                // Absent or unknown? Add it to the collection to check the status and/or delete
                queryIndexEntries.put(rowID, mutation);
                break;
            case PRESENT: // Present? No op
                break;
            }

            if (queryIndexEntries.size() == 100000) {
                flushDeleteEntries(connector, table, start, indexWriter,
                        ImmutableMultimap.copyOf(queryIndexEntries), rowIdStatuses);
                queryIndexEntries.clear();
            }
        }

        flushDeleteEntries(connector, table, start, indexWriter, ImmutableMultimap.copyOf(queryIndexEntries),
                rowIdStatuses);
        queryIndexEntries.clear();

        LOG.info(format(
                "Finished scanning index entries. There are %s distinct row IDs containing %s entries. %s rows present in the data table and %s absent",
                rowIdStatuses.size(), numTotalMutations,
                rowIdStatuses.entrySet().stream().filter(entry -> entry.getValue().equals(RowStatus.PRESENT))
                        .count(),
                rowIdStatuses.entrySet().stream().filter(entry -> entry.getValue().equals(RowStatus.ABSENT))
                        .count()));

        if (dryRun) {
            LOG.info(format("Would have deleted %s index entries", numDeletedIndexEntries));
        } else {
            LOG.info(format("Deleted %s index entries", numDeletedIndexEntries));
        }
    } catch (AccumuloException | AccumuloSecurityException e) {
        LOG.error("Accumulo exception", e);
    } catch (TableNotFoundException e) {
        LOG.error("Table not found, must have been deleted during process", e);
    } finally {
        if (indexWriter != null) {
            try {
                indexWriter.close();
            } catch (MutationsRejectedException e) {
                LOG.error("Server rejected mutations", e);
            }
        }

        if (scanner != null) {
            scanner.close();
        }
    }
}

From source file:com.google.gerrit.server.notedb.rebuild.ChangeRebuilderImpl.java

@Override
public void buildUpdates(NoteDbUpdateManager manager, ChangeBundle bundle) throws IOException, OrmException {
    manager.setCheckExpectedState(false).setRefLogMessage("Rebuilding change");
    Change change = new Change(bundle.getChange());
    if (bundle.getPatchSets().isEmpty()) {
        throw new NoPatchSetsException(change.getId());
    }/*w w  w  . j av  a 2  s.  c om*/

    // We will rebuild all events, except for draft comments, in buckets based
    // on author and timestamp.
    List<Event> events = new ArrayList<>();
    ListMultimap<Account.Id, DraftCommentEvent> draftCommentEvents = MultimapBuilder.hashKeys()
            .arrayListValues().build();

    events.addAll(getHashtagsEvents(change, manager));

    // Delete ref only after hashtags have been read
    deleteChangeMetaRef(change, manager.getChangeRepo().cmds);
    deleteDraftRefs(change, manager.getAllUsersRepo());

    Integer minPsNum = getMinPatchSetNum(bundle);
    TreeMap<PatchSet.Id, PatchSetEvent> patchSetEvents = new TreeMap<>(ReviewDbUtil.intKeyOrdering());

    for (PatchSet ps : bundle.getPatchSets()) {
        PatchSetEvent pse = new PatchSetEvent(change, ps, manager.getChangeRepo().rw);
        patchSetEvents.put(ps.getId(), pse);
        events.add(pse);
        for (Comment c : getComments(bundle, serverId, Status.PUBLISHED, ps)) {
            CommentEvent e = new CommentEvent(c, change, ps, patchListCache);
            events.add(e.addDep(pse));
        }
        for (Comment c : getComments(bundle, serverId, Status.DRAFT, ps)) {
            DraftCommentEvent e = new DraftCommentEvent(c, change, ps, patchListCache);
            draftCommentEvents.put(c.author.getId(), e);
        }
    }
    ensurePatchSetOrder(patchSetEvents);

    for (PatchSetApproval psa : bundle.getPatchSetApprovals()) {
        PatchSetEvent pse = patchSetEvents.get(psa.getPatchSetId());
        if (pse != null) {
            events.add(new ApprovalEvent(psa, change.getCreatedOn()).addDep(pse));
        }
    }

    for (Table.Cell<ReviewerStateInternal, Account.Id, Timestamp> r : bundle.getReviewers().asTable()
            .cellSet()) {
        events.add(new ReviewerEvent(r, change.getCreatedOn()));
    }

    Change noteDbChange = new Change(null, null, null, null, null);
    for (ChangeMessage msg : bundle.getChangeMessages()) {
        Event msgEvent = new ChangeMessageEvent(change, noteDbChange, msg, change.getCreatedOn());
        if (msg.getPatchSetId() != null) {
            PatchSetEvent pse = patchSetEvents.get(msg.getPatchSetId());
            if (pse == null) {
                continue; // Ignore events for missing patch sets.
            }
            msgEvent.addDep(pse);
        }
        events.add(msgEvent);
    }

    sortAndFillEvents(change, noteDbChange, bundle.getPatchSets(), events, minPsNum);

    EventList<Event> el = new EventList<>();
    for (Event e : events) {
        if (!el.canAdd(e)) {
            flushEventsToUpdate(manager, el, change);
            checkState(el.canAdd(e));
        }
        el.add(e);
    }
    flushEventsToUpdate(manager, el, change);

    EventList<DraftCommentEvent> plcel = new EventList<>();
    for (Account.Id author : draftCommentEvents.keys()) {
        for (DraftCommentEvent e : Ordering.natural().sortedCopy(draftCommentEvents.get(author))) {
            if (!plcel.canAdd(e)) {
                flushEventsToDraftUpdate(manager, plcel, change);
                checkState(plcel.canAdd(e));
            }
            plcel.add(e);
        }
        flushEventsToDraftUpdate(manager, plcel, change);
    }
}

From source file:com.google.inject.internal.SpiUtils.java

@SuppressWarnings("unchecked")
private static <T> void mapModuleTest(Key<T> mapKey, TypeLiteral<?> keyType, TypeLiteral<?> valueType,
        Iterable<? extends Module> modules, boolean allowDuplicates, int expectedMapBindings,
        MapResult<?, ?>... results) {
    Set<Element> elements = ImmutableSet.copyOf(Elements.getElements(modules));
    Visitor<T> visitor = new Visitor<T>();
    MapBinderBinding<T> mapbinder = null;
    Map<Key<?>, Binding<?>> keyMap = Maps.newHashMap();
    for (Element element : elements) {
        if (element instanceof Binding) {
            Binding<?> binding = (Binding<?>) element;
            keyMap.put(binding.getKey(), binding);
            if (binding.getKey().equals(mapKey)) {
                mapbinder = (MapBinderBinding<T>) ((Binding<T>) binding).acceptTargetVisitor(visitor);
            }//from  w  w  w .j a  v  a2s.c  o  m
        }
    }
    assertNotNull(mapbinder);

    List<MapResult<?, ?>> mapResults = Lists.newArrayList(results);

    // Make sure the entries returned from getEntries(elements) are correct.
    // Because getEntries() can return duplicates, make sure to continue searching, even
    // after we find one match.
    List<Map.Entry<?, Binding<?>>> entries = Lists.newArrayList(mapbinder.getEntries(elements));
    for (MapResult<?, ?> result : mapResults) {
        List<Map.Entry<?, Binding<?>>> foundEntries = Lists.newArrayList();
        for (Map.Entry<?, Binding<?>> entry : entries) {
            Object key = entry.getKey();
            Binding<?> value = entry.getValue();
            if (key.equals(result.k) && matches(value, result.v)) {
                assertTrue("mapBinder doesn't contain: " + entry.getValue(),
                        mapbinder.containsElement(entry.getValue()));
                foundEntries.add(entry);
            }
        }
        assertTrue("Could not find entry: " + result + " in remaining entries: " + entries,
                !foundEntries.isEmpty());

        entries.removeAll(foundEntries);
    }

    assertTrue("Found all entries of: " + mapResults + ", but more were left over: " + entries,
            entries.isEmpty());

    assertEquals(keyType, mapbinder.getKeyTypeLiteral());
    assertEquals(valueType, mapbinder.getValueTypeLiteral());

    Key<?> mapOfProvider = mapKey.ofType(mapOfProviderOf(keyType, valueType));
    Key<?> mapOfJavaxProvider = mapKey.ofType(mapOfJavaxProviderOf(keyType, valueType));
    Key<?> mapOfSetOfProvider = mapKey.ofType(mapOfSetOfProviderOf(keyType, valueType));
    Key<?> mapOfSetOfJavaxProvider = mapKey.ofType(mapOfSetOfJavaxProviderOf(keyType, valueType));
    Key<?> mapOfCollectionOfProvider = mapKey.ofType(mapOfCollectionOfProviderOf(keyType, valueType));
    Key<?> mapOfCollectionOfJavaxProvider = mapKey.ofType(mapOfCollectionOfJavaxProviderOf(keyType, valueType));
    Key<?> mapOfSet = mapKey.ofType(mapOf(keyType, setOf(valueType)));
    Key<?> setOfEntry = mapKey.ofType(setOf(entryOfProviderOf(keyType, valueType)));
    Key<?> setOfJavaxEntry = mapKey.ofType(setOf(entryOfJavaxProviderOf(keyType, valueType)));
    Key<?> collectionOfProvidersOfEntryOfProvider = mapKey
            .ofType(collectionOfProvidersOf(entryOfProviderOf(keyType, valueType)));
    Key<?> collectionOfJavaxProvidersOfEntryOfProvider = mapKey
            .ofType(collectionOfJavaxProvidersOf(entryOfProviderOf(keyType, valueType)));
    boolean entrySetMatch = false;
    boolean entrySetJavaxMatch = false;
    boolean mapProviderMatch = false;
    boolean mapJavaxProviderMatch = false;
    boolean mapSetMatch = false;
    boolean mapSetProviderMatch = false;
    boolean mapSetJavaxProviderMatch = false;
    boolean mapCollectionProviderMatch = false;
    boolean mapCollectionJavaxProviderMatch = false;
    boolean collectionOfProvidersOfEntryOfProviderMatch = false;
    boolean collectionOfJavaxProvidersOfEntryOfProviderMatch = false;
    List<Object> otherMapBindings = Lists.newArrayList();
    List<Element> otherMatches = Lists.newArrayList();
    List<Element> otherElements = Lists.newArrayList();
    Indexer indexer = new Indexer(null);
    Multimap<Object, IndexedBinding> indexedEntries = MultimapBuilder.hashKeys().hashSetValues().build();
    int duplicates = 0;
    for (Element element : elements) {
        boolean contains = mapbinder.containsElement(element);
        if (!contains) {
            otherElements.add(element);
        }
        boolean matched = false;
        Key key = null;
        Binding b = null;
        if (element instanceof Binding) {
            b = (Binding) element;
            if (b instanceof ProviderInstanceBinding) {
                ProviderInstanceBinding<?> pb = (ProviderInstanceBinding<?>) b;
                if (pb.getUserSuppliedProvider() instanceof ProviderMapEntry) {
                    // weird casting required to workaround jdk6 compilation problems
                    ProviderMapEntry<?, ?> pme = (ProviderMapEntry<?, ?>) (Provider) pb
                            .getUserSuppliedProvider();
                    Binding<?> valueBinding = keyMap.get(pme.getValueKey());
                    if (indexer.isIndexable(valueBinding)
                            && !indexedEntries.put(pme.getKey(), valueBinding.acceptTargetVisitor(indexer))) {
                        duplicates++;
                    }
                }
            }

            key = b.getKey();
            Object visited = b.acceptTargetVisitor(visitor);
            if (visited instanceof MapBinderBinding) {
                matched = true;
                if (visited.equals(mapbinder)) {
                    assertTrue(contains);
                } else {
                    otherMapBindings.add(visited);
                }
            }
        } else if (element instanceof ProviderLookup) {
            key = ((ProviderLookup) element).getKey();
        }

        if (!matched && key != null) {
            if (key.equals(mapOfProvider)) {
                matched = true;
                assertTrue(contains);
                mapProviderMatch = true;
            } else if (key.equals(mapOfJavaxProvider)) {
                matched = true;
                assertTrue(contains);
                mapJavaxProviderMatch = true;
            } else if (key.equals(mapOfSet)) {
                matched = true;
                assertTrue(contains);
                mapSetMatch = true;
            } else if (key.equals(mapOfSetOfProvider)) {
                matched = true;
                assertTrue(contains);
                mapSetProviderMatch = true;
            } else if (key.equals(mapOfSetOfJavaxProvider)) {
                matched = true;
                assertTrue(contains);
                mapSetJavaxProviderMatch = true;
            } else if (key.equals(mapOfCollectionOfProvider)) {
                matched = true;
                assertTrue(contains);
                mapCollectionProviderMatch = true;
            } else if (key.equals(mapOfCollectionOfJavaxProvider)) {
                matched = true;
                assertTrue(contains);
                mapCollectionJavaxProviderMatch = true;
            } else if (key.equals(setOfEntry)) {
                matched = true;
                assertTrue(contains);
                entrySetMatch = true;
                // Validate that this binding is also a MultibinderBinding.
                if (b != null) {
                    assertTrue(b.acceptTargetVisitor(visitor) instanceof MultibinderBinding);
                }
            } else if (key.equals(setOfJavaxEntry)) {
                matched = true;
                assertTrue(contains);
                entrySetJavaxMatch = true;
            } else if (key.equals(collectionOfProvidersOfEntryOfProvider)) {
                matched = true;
                assertTrue(contains);
                collectionOfProvidersOfEntryOfProviderMatch = true;
            } else if (key.equals(collectionOfJavaxProvidersOfEntryOfProvider)) {
                matched = true;
                assertTrue(contains);
                collectionOfJavaxProvidersOfEntryOfProviderMatch = true;
            }
        }

        if (!matched && contains) {
            otherMatches.add(element);
        }
    }

    int otherMatchesSize = otherMatches.size();
    if (allowDuplicates) {
        otherMatchesSize--; // allow for 1 duplicate binding
    }
    // Multiply by 3 because each has a value, ProviderLookup, and Map.Entry
    int expectedSize = (mapResults.size() + duplicates) * 3;
    assertEquals("incorrect number of contains, leftover matches:\n" + Joiner.on("\n\t").join(otherMatches),
            expectedSize, otherMatchesSize);

    assertTrue(entrySetMatch);
    assertTrue(entrySetJavaxMatch);
    assertTrue(mapProviderMatch);
    assertTrue(mapJavaxProviderMatch);
    assertTrue(collectionOfProvidersOfEntryOfProviderMatch);
    assertTrue(collectionOfJavaxProvidersOfEntryOfProviderMatch);
    assertEquals(allowDuplicates, mapSetMatch);
    assertEquals(allowDuplicates, mapSetProviderMatch);
    assertEquals(allowDuplicates, mapSetJavaxProviderMatch);
    assertEquals(allowDuplicates, mapCollectionProviderMatch);
    assertEquals(allowDuplicates, mapCollectionJavaxProviderMatch);
    assertEquals("other MapBindings found: " + otherMapBindings, expectedMapBindings, otherMapBindings.size());

    // Validate that we can construct an injector out of the remaining bindings.
    Guice.createInjector(Elements.getModule(otherElements));
}

From source file:org.eobjects.analyzer.job.JaxbJobReader.java

private void applyInputColumns(List<InputType> input, Map<String, InputColumn<?>> inputColumns,
        AbstractBeanWithInputColumnsBuilder<?, ?, ?> componentJobBuilder) {
    // build a map of inputs first so that we can set the
    // input in one go
    final ListMultimap<ConfiguredPropertyDescriptor, InputColumn<?>> inputMap = MultimapBuilder.hashKeys()
            .arrayListValues().build();/*from w w  w  .  j  ava  2  s. co m*/

    for (InputType inputType : input) {
        String name = inputType.getName();
        String ref = inputType.getRef();
        InputColumn<?> inputColumn;
        if (StringUtils.isNullOrEmpty(ref)) {
            inputColumn = createExpressionBasedInputColumn(inputType);
        } else {
            inputColumn = inputColumns.get(ref);
        }
        if (StringUtils.isNullOrEmpty(name)) {
            ConfiguredPropertyDescriptor propertyDescriptor = componentJobBuilder
                    .getDefaultConfiguredPropertyForInput();
            inputMap.put(propertyDescriptor, inputColumn);
        } else {
            ConfiguredPropertyDescriptor propertyDescriptor = componentJobBuilder.getDescriptor()
                    .getConfiguredProperty(name);
            inputMap.put(propertyDescriptor, inputColumn);
        }
    }

    final Set<ConfiguredPropertyDescriptor> keys = inputMap.keySet();
    for (ConfiguredPropertyDescriptor propertyDescriptor : keys) {
        List<InputColumn<?>> inputColumnsForProperty = inputMap.get(propertyDescriptor);
        componentJobBuilder.addInputColumns(inputColumnsForProperty, propertyDescriptor);
    }
}