Search in sources :

Example 26 with Multiset

use of com.google.common.collect.Multiset in project tez by apache.

the class VertexImpl method logLocationHints.

private static void logLocationHints(String vertexName, VertexLocationHint locationHint) {
    if (locationHint == null) {
        LOG.debug("No Vertex LocationHint specified for vertex=" + vertexName);
        return;
    }
    Multiset<String> hosts = HashMultiset.create();
    Multiset<String> racks = HashMultiset.create();
    int counter = 0;
    for (TaskLocationHint taskLocationHint : locationHint.getTaskLocationHints()) {
        StringBuilder sb = new StringBuilder();
        if (taskLocationHint.getHosts() == null) {
            sb.append("No Hosts");
        } else {
            sb.append("Hosts: ");
            for (String host : taskLocationHint.getHosts()) {
                hosts.add(host);
                sb.append(host).append(", ");
            }
        }
        if (taskLocationHint.getRacks() == null) {
            sb.append("No Racks");
        } else {
            sb.append("Racks: ");
            for (String rack : taskLocationHint.getRacks()) {
                racks.add(rack);
                sb.append(rack).append(", ");
            }
        }
        LOG.debug("Vertex: " + vertexName + ", Location: " + counter + " : " + sb.toString());
        counter++;
    }
    LOG.debug("Vertex: " + vertexName + ", Host Counts");
    for (Multiset.Entry<String> host : hosts.entrySet()) {
        LOG.debug("Vertex: " + vertexName + ", host: " + host.toString());
    }
    LOG.debug("Vertex: " + vertexName + ", Rack Counts");
    for (Multiset.Entry<String> rack : racks.entrySet()) {
        LOG.debug("Vertex: " + vertexName + ", rack: " + rack.toString());
    }
}
Also used : TaskLocationHint(org.apache.tez.dag.api.TaskLocationHint) HashMultiset(com.google.common.collect.HashMultiset) Multiset(com.google.common.collect.Multiset) TaskLocationHint(org.apache.tez.dag.api.TaskLocationHint) VertexLocationHint(org.apache.tez.dag.api.VertexLocationHint)

Example 27 with Multiset

use of com.google.common.collect.Multiset in project sonar-java by SonarSource.

the class CheckVerifier method validateAnalyzerMessage.

private static void validateAnalyzerMessage(Map<IssueAttribute, String> attrs, AnalyzerMessage analyzerMessage) {
    Double effortToFix = analyzerMessage.getCost();
    if (effortToFix != null) {
        assertEquals(Integer.toString(effortToFix.intValue()), attrs, IssueAttribute.EFFORT_TO_FIX);
    }
    AnalyzerMessage.TextSpan textSpan = analyzerMessage.primaryLocation();
    assertEquals(normalizeColumn(textSpan.startCharacter), attrs, IssueAttribute.START_COLUMN);
    assertEquals(Integer.toString(textSpan.endLine), attrs, IssueAttribute.END_LINE);
    assertEquals(normalizeColumn(textSpan.endCharacter), attrs, IssueAttribute.END_COLUMN);
    if (attrs.containsKey(IssueAttribute.SECONDARY_LOCATIONS)) {
        List<AnalyzerMessage> secondaryLocations = analyzerMessage.flows.stream().map(l -> l.get(0)).collect(Collectors.toList());
        Multiset<String> actualLines = HashMultiset.create();
        for (AnalyzerMessage secondaryLocation : secondaryLocations) {
            actualLines.add(Integer.toString(secondaryLocation.getLine()));
        }
        List<String> expected = Lists.newArrayList(Splitter.on(",").omitEmptyStrings().trimResults().split(attrs.get(IssueAttribute.SECONDARY_LOCATIONS)));
        List<String> unexpected = new ArrayList<>();
        for (String actualLine : actualLines) {
            if (expected.contains(actualLine)) {
                expected.remove(actualLine);
            } else {
                unexpected.add(actualLine);
            }
        }
        if (!expected.isEmpty() || !unexpected.isEmpty()) {
            // Line is not covered by JaCoCo because of thrown exception but effectively covered in UT.
            Fail.fail(String.format("Secondary locations: expected: %s unexpected:%s. In %s:%d", expected, unexpected, normalizedFilePath(analyzerMessage), analyzerMessage.getLine()));
        }
    }
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) JsonParseException(com.google.gson.JsonParseException) Iterables(com.google.common.collect.Iterables) StringUtils(org.apache.commons.lang.StringUtils) Multiset(com.google.common.collect.Multiset) URL(java.net.URL) AnalyzerMessage(org.sonar.java.AnalyzerMessage) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AnnotationUtils(org.sonar.api.utils.AnnotationUtils) Multimap(com.google.common.collect.Multimap) MapSettings(org.sonar.api.config.internal.MapSettings) ArrayList(java.util.ArrayList) Loggers(org.sonar.api.utils.log.Loggers) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) Lists(com.google.common.collect.Lists) HashMultiset(com.google.common.collect.HashMultiset) RecognitionException(com.sonar.sslr.api.RecognitionException) Gson(com.google.gson.Gson) Map(java.util.Map) SonarComponents(org.sonar.java.SonarComponents) Splitter(com.google.common.base.Splitter) Nullable(javax.annotation.Nullable) Logger(org.sonar.api.utils.log.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) EnumMap(java.util.EnumMap) RspecKey(org.sonar.java.RspecKey) Set(java.util.Set) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Version(org.sonar.api.utils.Version) Fail(org.assertj.core.api.Fail) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) List(java.util.List) SonarRuntimeImpl(org.sonar.api.internal.SonarRuntimeImpl) Preconditions(com.google.common.base.Preconditions) Rule(org.sonar.check.Rule) Collections(java.util.Collections) CheckForNull(javax.annotation.CheckForNull) ArrayList(java.util.ArrayList) AnalyzerMessage(org.sonar.java.AnalyzerMessage)

Example 28 with Multiset

use of com.google.common.collect.Multiset in project ANNIS by korpling.

the class SemanticValidator method checkAlternative.

public void checkAlternative(QueryData data, List<QueryNode> alternative, int alternativeIndex, boolean queryWasNormalized) {
    // check if there is at least one search expression
    if (alternative.isEmpty()) {
        throw new AnnisQLSemanticsException("Missing search expression.");
    }
    // there are not linguistic binary relations allowed if there is only one node
    if (alternative.size() == 1) {
        QueryNode n = alternative.get(0);
        for (Join j : n.getOutgoingJoins()) {
            if (j.getTarget() != null) {
                throw new AnnisQLSemanticsException(j.getParseLocation(), "No binary linguistic relations allowed if there is only one node in query.");
            }
        }
    }
    // get all nodes connected to the first one
    Multimap<Long, QueryNode> connected = calculateConnected(alternative);
    Set<Long> transitiveHull = new HashSet<>();
    transitiveHull.add(alternative.get(0).getId());
    createTransitiveHull(alternative.get(0), connected, transitiveHull);
    Multiset<String> variableNames = TreeMultiset.create();
    Set<Long> unconnectedNodes = new HashSet<>();
    for (QueryNode n : alternative) {
        unconnectedNodes.add(n.getId());
        variableNames.add(n.getVariable());
    }
    unconnectedNodes.removeAll(transitiveHull);
    // check if each node is contained in the connected nodes
    if (!unconnectedNodes.isEmpty()) {
        List<AqlParseError> errors = new LinkedList<>();
        for (QueryNode n : alternative) {
            if (unconnectedNodes.contains(n.getId())) {
                errors.add(new AqlParseError(n, "variable \"" + n.getVariable() + "\" not bound (use linguistic operators)"));
            }
        }
        if (!errors.isEmpty()) {
            if (queryWasNormalized) {
                // add the normalized query as "error" so the user is able to see it
                errors.add(new AqlParseError("Normalized query is: \n" + data.toAQL()));
            }
            throw new AnnisQLSemanticsException("Not all variables bound", errors);
        }
    }
    // check if any variable name was given more than once
    List<String> invalidNames = new LinkedList<>();
    for (Multiset.Entry<String> e : variableNames.entrySet()) {
        if (e.getCount() > 1) {
            invalidNames.add(e.getElement());
        }
    }
    if (!invalidNames.isEmpty()) {
        throw new AnnisQLSemanticsException("The following variable names are " + "used for more than one node: " + Joiner.on(", ").join(invalidNames) + "\nNormalized Query is: \n" + data.toAQL());
    }
    // check no non-reflexive operator is used with the same operands
    for (QueryNode source : alternative) {
        for (Join join : source.getOutgoingJoins()) {
            if (join instanceof Inclusion || join instanceof SameSpan || join instanceof Overlap || join instanceof RightOverlap || join instanceof LeftOverlap || join instanceof RightAlignment || join instanceof LeftAlignment) {
                if (source.equals(join.getTarget())) {
                    throw new AnnisQLSemanticsException(join, "Not-reflexive operator used with the same node as argument.");
                }
            }
        }
    }
}
Also used : Inclusion(annis.sqlgen.model.Inclusion) SameSpan(annis.sqlgen.model.SameSpan) RightAlignment(annis.sqlgen.model.RightAlignment) AqlParseError(annis.model.AqlParseError) RightOverlap(annis.sqlgen.model.RightOverlap) LeftAlignment(annis.sqlgen.model.LeftAlignment) AnnisQLSemanticsException(annis.exceptions.AnnisQLSemanticsException) NonBindingJoin(annis.sqlgen.model.NonBindingJoin) Join(annis.model.Join) RightOverlap(annis.sqlgen.model.RightOverlap) Overlap(annis.sqlgen.model.Overlap) LeftOverlap(annis.sqlgen.model.LeftOverlap) LinkedList(java.util.LinkedList) QueryNode(annis.model.QueryNode) LeftOverlap(annis.sqlgen.model.LeftOverlap) Multiset(com.google.common.collect.Multiset) TreeMultiset(com.google.common.collect.TreeMultiset) HashSet(java.util.HashSet)

Example 29 with Multiset

use of com.google.common.collect.Multiset in project atlasdb by palantir.

the class SweepStatsKeyValueService method flushWrites.

private void flushWrites(Multiset<TableReference> writes, Set<TableReference> clears) {
    if (writes.isEmpty() && clears.isEmpty()) {
        log.info("No writes to flush");
        return;
    }
    log.info("Flushing stats for {} writes and {} clears", SafeArg.of("writes", writes.size()), SafeArg.of("clears", clears.size()));
    log.trace("Flushing writes: {}", UnsafeArg.of("writes", writes));
    log.trace("Flushing clears: {}", UnsafeArg.of("clears", clears));
    try {
        Set<TableReference> tableNames = Sets.difference(writes.elementSet(), clears);
        Collection<byte[]> rows = Collections2.transform(Collections2.transform(tableNames, t -> t.getQualifiedName()), Functions.compose(Persistables.persistToBytesFunction(), SweepPriorityRow.fromFullTableNameFun()));
        Map<Cell, Value> oldWriteCounts = delegate().getRows(SWEEP_PRIORITY_TABLE, rows, SweepPriorityTable.getColumnSelection(SweepPriorityNamedColumn.WRITE_COUNT), Long.MAX_VALUE);
        Map<Cell, byte[]> newWriteCounts = Maps.newHashMapWithExpectedSize(writes.elementSet().size());
        byte[] col = SweepPriorityNamedColumn.WRITE_COUNT.getShortName();
        for (TableReference tableRef : tableNames) {
            Preconditions.checkState(!tableRef.getQualifiedName().startsWith(AtlasDbConstants.NAMESPACE_PREFIX), "The sweep stats kvs should wrap the namespace mapping kvs, not the other way around.");
            byte[] row = SweepPriorityRow.of(tableRef.getQualifiedName()).persistToBytes();
            Cell cell = Cell.create(row, col);
            Value oldValue = oldWriteCounts.get(cell);
            long oldCount = oldValue == null || oldValue.getContents().length == 0 ? 0 : SweepPriorityTable.WriteCount.BYTES_HYDRATOR.hydrateFromBytes(oldValue.getContents()).getValue();
            long newValue = clears.contains(tableRef) ? writes.count(tableRef) : oldCount + writes.count(tableRef);
            log.debug("Sweep priority for {} has {} writes (was {})", tableRef, newValue, oldCount);
            newWriteCounts.put(cell, SweepPriorityTable.WriteCount.of(newValue).persistValue());
        }
        long timestamp = timestampService.getFreshTimestamp();
        // Committing before writing is intentional, we want the start timestamp to
        // show up in the transaction table before we write do our writes.
        commit(timestamp);
        delegate().put(SWEEP_PRIORITY_TABLE, newWriteCounts, timestamp);
    } catch (RuntimeException e) {
        if (Thread.interrupted()) {
            return;
        }
        Set<TableReference> allTableNames = delegate().getAllTableNames();
        if (!allTableNames.contains(SWEEP_PRIORITY_TABLE) || !allTableNames.contains(TransactionConstants.TRANSACTION_TABLE)) {
            // ignore problems when sweep or transaction tables don't exist
            log.warn("Ignoring failed sweep stats flush due to ", e);
        }
        log.error("Unable to flush sweep stats for writes {} and clears {}: ", writes, clears, e);
        throw e;
    }
}
Also used : Multiset(com.google.common.collect.Multiset) LoggerFactory(org.slf4j.LoggerFactory) SweepSchema(com.palantir.atlasdb.schema.SweepSchema) SweepPriorityRow(com.palantir.atlasdb.schema.generated.SweepPriorityTable.SweepPriorityRow) Collections2(com.google.common.collect.Collections2) Multimap(com.google.common.collect.Multimap) Supplier(java.util.function.Supplier) Persistables(com.palantir.common.persist.Persistables) SweepPriorityNamedColumn(com.palantir.atlasdb.schema.generated.SweepPriorityTable.SweepPriorityNamedColumn) ClusterAvailabilityStatus(com.palantir.atlasdb.keyvalue.api.ClusterAvailabilityStatus) SafeArg(com.palantir.logsafe.SafeArg) PTExecutors(com.palantir.common.concurrent.PTExecutors) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ImmutableMultiset(com.google.common.collect.ImmutableMultiset) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) TimestampService(com.palantir.timestamp.TimestampService) AtlasDbConstants(com.palantir.atlasdb.AtlasDbConstants) Functions(com.google.common.base.Functions) Logger(org.slf4j.Logger) ImmutableSet(com.google.common.collect.ImmutableSet) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Value(com.palantir.atlasdb.keyvalue.api.Value) ImmutableMap(com.google.common.collect.ImmutableMap) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest) SweepPriorityTable(com.palantir.atlasdb.schema.generated.SweepPriorityTable) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Lock(java.util.concurrent.locks.Lock) TransactionConstants(com.palantir.atlasdb.transaction.impl.TransactionConstants) ConcurrentHashMultiset(com.google.common.collect.ConcurrentHashMultiset) UnsafeArg(com.palantir.logsafe.UnsafeArg) KeyValueService(com.palantir.atlasdb.keyvalue.api.KeyValueService) Entry(java.util.Map.Entry) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Value(com.palantir.atlasdb.keyvalue.api.Value) Cell(com.palantir.atlasdb.keyvalue.api.Cell)

Example 30 with Multiset

use of com.google.common.collect.Multiset in project ForestryMC by ForestryMC.

the class MultiblockControllerForestry method onMachineAssembled.

@Override
protected void onMachineAssembled() {
    super.onMachineAssembled();
    if (world.isRemote) {
        return;
    }
    // Figure out who owns the multiblock, by majority
    Multiset<GameProfile> owners = HashMultiset.create();
    for (IMultiblockComponent part : connectedParts) {
        GameProfile owner = part.getOwner();
        if (owner != null) {
            owners.add(owner);
        }
    }
    GameProfile owner = null;
    int max = 0;
    for (Multiset.Entry<GameProfile> entry : owners.entrySet()) {
        int count = entry.getCount();
        if (count > max) {
            max = count;
            owner = entry.getElement();
        }
    }
    if (owner != null) {
        getOwnerHandler().setOwner(owner);
    }
}
Also used : GameProfile(com.mojang.authlib.GameProfile) Multiset(com.google.common.collect.Multiset) HashMultiset(com.google.common.collect.HashMultiset) IMultiblockComponent(forestry.api.multiblock.IMultiblockComponent)

Aggregations

Multiset (com.google.common.collect.Multiset)34 HashMultiset (com.google.common.collect.HashMultiset)20 List (java.util.List)11 ArrayList (java.util.ArrayList)10 Set (java.util.Set)10 File (java.io.File)9 Map (java.util.Map)9 IOException (java.io.IOException)7 HashMap (java.util.HashMap)7 Collectors (java.util.stream.Collectors)7 Test (org.junit.Test)6 Collection (java.util.Collection)5 Entry (java.util.Map.Entry)5 UUID (java.util.UUID)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 Lists (com.google.common.collect.Lists)4 Multimap (com.google.common.collect.Multimap)4 Predicate (java.util.function.Predicate)4 ConcurrentHashMultiset (com.google.common.collect.ConcurrentHashMultiset)3 ImmutableMap (com.google.common.collect.ImmutableMap)3