Search in sources :

Example 31 with Multiset

use of com.google.common.collect.Multiset in project BloodMagic by WayofTime.

the class ItemBoundTool method dropStacks.

protected static void dropStacks(Multiset<ItemStackWrapper> drops, World world, BlockPos posToDrop) {
    for (Multiset.Entry<ItemStackWrapper> entry : drops.entrySet()) {
        int count = entry.getCount();
        ItemStackWrapper stack = entry.getElement();
        int maxStackSize = stack.item.getItemStackLimit(stack.toStack(1));
        while (count >= maxStackSize) {
            world.spawnEntity(new EntityItem(world, posToDrop.getX(), posToDrop.getY(), posToDrop.getZ(), stack.toStack(maxStackSize)));
            count -= maxStackSize;
        }
        if (count > 0)
            world.spawnEntity(new EntityItem(world, posToDrop.getX(), posToDrop.getY(), posToDrop.getZ(), stack.toStack(count)));
    }
}
Also used : Multiset(com.google.common.collect.Multiset) ItemStackWrapper(WayofTime.bloodmagic.util.ItemStackWrapper) EntityItem(net.minecraft.entity.item.EntityItem)

Example 32 with Multiset

use of com.google.common.collect.Multiset in project acidisland by tastybento.

the class LavaCheck method onCobbleGen.

/**
 * Magic Cobble Generator
 * @param e - event
 */
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onCobbleGen(BlockFromToEvent e) {
    // If magic cobble gen isnt used
    if (!Settings.useMagicCobbleGen) {
        // plugin.getLogger().info("DEBUG: no magic cobble gen");
        return;
    }
    // Only do this in ASkyBlock world
    if (!e.getBlock().getWorld().equals(ASkyBlock.getIslandWorld())) {
        // plugin.getLogger().info("DEBUG: wrong world");
        return;
    }
    // Do nothing if a new island is being created
    if (plugin.isNewIsland()) {
        // plugin.getLogger().info("DEBUG: new island in creation");
        return;
    }
    // If only at spawn, do nothing if we're not at spawn
    if (Settings.magicCobbleGenOnlyAtSpawn && (!ASkyBlockAPI.getInstance().isAtSpawn(e.getBlock().getLocation()))) {
        return;
    }
    final Block b = e.getBlock();
    if (b.getType().equals(Material.WATER) || b.getType().equals(Material.STATIONARY_WATER) || b.getType().equals(Material.LAVA) || b.getType().equals(Material.STATIONARY_LAVA)) {
        // plugin.getLogger().info("DEBUG: From block is water or lava. To = " + e.getToBlock().getType());
        final Block toBlock = e.getToBlock();
        if (toBlock.getType().equals(Material.AIR) && generatesCobble(b, toBlock)) {
            // plugin.getLogger().info("DEBUG: potential cobble gen");
            // Get island level or use default
            long l = Long.MIN_VALUE;
            Island island = plugin.getGrid().getIslandAt(b.getLocation());
            if (island != null) {
                if (island.getOwner() != null) {
                    l = plugin.getPlayers().getIslandLevel(island.getOwner());
                // plugin.getLogger().info("DEBUG: level " + level);
                }
            }
            final long level = l;
            // Check if cobble was generated next tick
            // Store surrounding blocks and their current material types
            final List<Block> prevBlock = new ArrayList<Block>();
            final List<Material> prevMat = new ArrayList<Material>();
            for (BlockFace face : FACES) {
                Block r = toBlock.getRelative(face);
                prevBlock.add(r);
                prevMat.add(r.getType());
            // r = toBlock.getRelative(face,2);
            // prevBlock.add(r);
            // prevMat.add(r.getType());
            }
            // Check if they became cobblestone next tick
            plugin.getServer().getScheduler().runTask(plugin, new Runnable() {

                @Override
                public void run() {
                    Iterator<Block> blockIt = prevBlock.iterator();
                    Iterator<Material> matIt = prevMat.iterator();
                    while (blockIt.hasNext() && matIt.hasNext()) {
                        Block block = blockIt.next();
                        Material material = matIt.next();
                        if (block.getType().equals(Material.COBBLESTONE) && !block.getType().equals(material)) {
                            // plugin.getLogger().info("DEBUG: Cobble generated. Island level = " + level);
                            if (!Settings.magicCobbleGenChances.isEmpty()) {
                                Entry<Long, TreeMap<Double, Material>> entry = Settings.magicCobbleGenChances.floorEntry(level);
                                double maxValue = entry.getValue().lastKey();
                                double rnd = Util.randomDouble() * maxValue;
                                Entry<Double, Material> en = entry.getValue().ceilingEntry(rnd);
                                // plugin.getLogger().info("DEBUG: material = " + en.getValue());
                                if (en != null) {
                                    block.setType(en.getValue());
                                    // Record stats, per level
                                    if (stats.containsKey(entry.getKey())) {
                                        stats.get(entry.getKey()).add(en.getValue());
                                    } else {
                                        Multiset<Material> set = HashMultiset.create();
                                        set.add(en.getValue());
                                        stats.put(entry.getKey(), set);
                                    }
                                }
                            }
                        }
                    }
                }
            });
        }
    }
}
Also used : BlockFace(org.bukkit.block.BlockFace) ArrayList(java.util.ArrayList) Material(org.bukkit.Material) Island(com.wasteofplastic.acidisland.Island) Entry(java.util.Map.Entry) Iterator(java.util.Iterator) Block(org.bukkit.block.Block) ASkyBlock(com.wasteofplastic.acidisland.ASkyBlock) Multiset(com.google.common.collect.Multiset) HashMultiset(com.google.common.collect.HashMultiset) EventHandler(org.bukkit.event.EventHandler)

Example 33 with Multiset

use of com.google.common.collect.Multiset in project intellij by bazelbuild.

the class SourceDirectoryCalculator method calculateJavaSourceDirectories.

/**
 * Adds the java source directories.
 */
private void calculateJavaSourceDirectories(BlazeContext context, WorkspaceRoot workspaceRoot, ArtifactLocationDecoder artifactLocationDecoder, WorkspacePath directoryRoot, Collection<SourceArtifact> javaArtifacts, Collection<JavaPackageReader> javaPackageReaders, Collection<BlazeSourceDirectory> result) {
    List<SourceRoot> sourceRootsPerFile = Lists.newArrayList();
    // Get java sources
    List<ListenableFuture<SourceRoot>> sourceRootFutures = Lists.newArrayList();
    for (final SourceArtifact sourceArtifact : javaArtifacts) {
        ListenableFuture<SourceRoot> future = executorService.submit(() -> sourceRootForJavaSource(context, artifactLocationDecoder, sourceArtifact, javaPackageReaders));
        sourceRootFutures.add(future);
    }
    try {
        for (SourceRoot sourceRoot : Futures.allAsList(sourceRootFutures).get()) {
            if (sourceRoot != null) {
                sourceRootsPerFile.add(sourceRoot);
            }
        }
    } catch (ExecutionException | InterruptedException e) {
        logger.error(e);
        throw new IllegalStateException("Could not read sources");
    }
    // Sort source roots into their respective directories
    Map<WorkspacePath, Multiset<SourceRoot>> sourceDirectoryToSourceRoots = new HashMap<>();
    for (SourceRoot sourceRoot : sourceRootsPerFile) {
        sourceDirectoryToSourceRoots.computeIfAbsent(sourceRoot.workspacePath, k -> HashMultiset.create()).add(sourceRoot);
    }
    // Create a mapping from directory to package prefix
    Map<WorkspacePath, SourceRoot> workspacePathToSourceRoot = Maps.newHashMap();
    for (WorkspacePath workspacePath : sourceDirectoryToSourceRoots.keySet()) {
        Multiset<SourceRoot> sources = sourceDirectoryToSourceRoots.get(workspacePath);
        Multiset<String> packages = HashMultiset.create();
        for (Multiset.Entry<SourceRoot> entry : sources.entrySet()) {
            packages.setCount(entry.getElement().packagePrefix, entry.getCount());
        }
        final String directoryPackagePrefix;
        // Common case -- all source files agree on a single package
        if (packages.elementSet().size() == 1) {
            directoryPackagePrefix = packages.elementSet().iterator().next();
        } else {
            String preferredPackagePrefix = PackagePrefixCalculator.packagePrefixOf(workspacePath);
            directoryPackagePrefix = pickMostFrequentlyOccurring(packages, preferredPackagePrefix);
        }
        SourceRoot candidateRoot = new SourceRoot(workspacePath, directoryPackagePrefix);
        workspacePathToSourceRoot.put(workspacePath, candidateRoot);
    }
    // Add content entry base if it doesn't exist
    if (!workspacePathToSourceRoot.containsKey(directoryRoot)) {
        SourceRoot candidateRoot = new SourceRoot(directoryRoot, PackagePrefixCalculator.packagePrefixOf(directoryRoot));
        workspacePathToSourceRoot.put(directoryRoot, candidateRoot);
    }
    // First, create a graph of the directory structure from root to each source file
    Map<WorkspacePath, SourceRootDirectoryNode> sourceRootDirectoryNodeMap = Maps.newHashMap();
    SourceRootDirectoryNode rootNode = new SourceRootDirectoryNode(directoryRoot, null);
    sourceRootDirectoryNodeMap.put(directoryRoot, rootNode);
    for (SourceRoot sourceRoot : workspacePathToSourceRoot.values()) {
        final String sourcePathRelativeToDirectoryRoot = sourcePathRelativeToDirectoryRoot(directoryRoot, sourceRoot.workspacePath);
        List<String> pathComponents = !Strings.isNullOrEmpty(sourcePathRelativeToDirectoryRoot) ? PATH_SPLITTER.splitToList(sourcePathRelativeToDirectoryRoot) : ImmutableList.of();
        SourceRootDirectoryNode previousNode = rootNode;
        for (int i = 0; i < pathComponents.size(); ++i) {
            final WorkspacePath workspacePath = getWorkspacePathFromPathComponents(directoryRoot, pathComponents, i + 1);
            SourceRootDirectoryNode node = sourceRootDirectoryNodeMap.get(workspacePath);
            if (node == null) {
                node = new SourceRootDirectoryNode(workspacePath, pathComponents.get(i));
                sourceRootDirectoryNodeMap.put(workspacePath, node);
                previousNode.children.add(node);
            }
            previousNode = node;
        }
    }
    // Add package prefix votes at each directory node
    for (SourceRoot sourceRoot : workspacePathToSourceRoot.values()) {
        final String sourcePathRelativeToDirectoryRoot = sourcePathRelativeToDirectoryRoot(directoryRoot, sourceRoot.workspacePath);
        List<String> packageComponents = PACKAGE_SPLITTER.splitToList(sourceRoot.packagePrefix);
        List<String> pathComponents = !Strings.isNullOrEmpty(sourcePathRelativeToDirectoryRoot) ? PATH_SPLITTER.splitToList(sourcePathRelativeToDirectoryRoot) : ImmutableList.of();
        int packageIndex = packageComponents.size();
        int pathIndex = pathComponents.size();
        while (pathIndex >= 0 && packageIndex >= 0) {
            final WorkspacePath workspacePath = getWorkspacePathFromPathComponents(directoryRoot, pathComponents, pathIndex);
            SourceRootDirectoryNode node = sourceRootDirectoryNodeMap.get(workspacePath);
            String packagePrefix = PACKAGE_JOINER.join(packageComponents.subList(0, packageIndex));
            // Otherwise just add a vote
            if (sourceRoot.workspacePath.equals(workspacePath)) {
                node.forcedPackagePrefix = packagePrefix;
            } else {
                node.packagePrefixVotes.add(packagePrefix);
            }
            String pathComponent = pathIndex > 0 ? pathComponents.get(pathIndex - 1) : "";
            String packageComponent = packageIndex > 0 ? packageComponents.get(packageIndex - 1) : "";
            if (!pathComponent.equals(packageComponent)) {
                break;
            }
            --packageIndex;
            --pathIndex;
        }
    }
    Map<WorkspacePath, SourceRoot> sourceRoots = Maps.newHashMap();
    SourceRootDirectoryNode root = sourceRootDirectoryNodeMap.get(directoryRoot);
    visitDirectoryNode(sourceRoots, root, null);
    for (SourceRoot sourceRoot : sourceRoots.values()) {
        result.add(BlazeSourceDirectory.builder(workspaceRoot.fileForPath(sourceRoot.workspacePath)).setPackagePrefix(sourceRoot.packagePrefix).setGenerated(false).build());
    }
}
Also used : WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) TransientExecutor(com.google.idea.blaze.base.async.executor.TransientExecutor) BlazeContext(com.google.idea.blaze.base.scope.BlazeContext) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Multiset(com.google.common.collect.Multiset) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) Strings(com.google.common.base.Strings) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) HashMultiset(com.google.common.collect.HashMultiset) Scope(com.google.idea.blaze.base.scope.Scope) Map(java.util.Map) IssueOutput(com.google.idea.blaze.base.scope.output.IssueOutput) Project(com.intellij.openapi.project.Project) Objects(com.google.common.base.Objects) Logger(com.intellij.openapi.diagnostic.Logger) Splitter(com.google.common.base.Splitter) Nullable(javax.annotation.Nullable) ImportRoots(com.google.idea.blaze.base.sync.projectview.ImportRoots) ArtifactLocation(com.google.idea.blaze.base.ideinfo.ArtifactLocation) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) BlazeContentEntry(com.google.idea.blaze.java.sync.model.BlazeContentEntry) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) TimingScope(com.google.idea.blaze.base.scope.scopes.TimingScope) BlazeSourceDirectory(com.google.idea.blaze.java.sync.model.BlazeSourceDirectory) WorkspaceRoot(com.google.idea.blaze.base.model.primitives.WorkspaceRoot) TargetKey(com.google.idea.blaze.base.ideinfo.TargetKey) WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) Comparator(java.util.Comparator) ArtifactLocationDecoder(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder) PackagePrefixCalculator(com.google.idea.blaze.base.util.PackagePrefixCalculator) Collections(java.util.Collections) Joiner(com.google.common.base.Joiner) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) EventType(com.google.idea.blaze.base.scope.scopes.TimingScope.EventType) HashMap(java.util.HashMap) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Multiset(com.google.common.collect.Multiset) HashMultiset(com.google.common.collect.HashMultiset) ExecutionException(java.util.concurrent.ExecutionException)

Example 34 with Multiset

use of com.google.common.collect.Multiset in project presto by prestodb.

the class BucketBalancer method computeAssignmentChanges.

private static Multimap<String, BucketAssignment> computeAssignmentChanges(ClusterState clusterState) {
    Multimap<String, BucketAssignment> sourceToAllocationChanges = HashMultimap.create();
    Map<String, Long> allocationBytes = new HashMap<>(clusterState.getAssignedBytes());
    Set<String> activeNodes = clusterState.getActiveNodes();
    for (Distribution distribution : clusterState.getDistributionAssignments().keySet()) {
        // number of buckets in this distribution assigned to a node
        Multiset<String> allocationCounts = HashMultiset.create();
        Collection<BucketAssignment> distributionAssignments = clusterState.getDistributionAssignments().get(distribution);
        distributionAssignments.stream().map(BucketAssignment::getNodeIdentifier).forEach(allocationCounts::add);
        int currentMin = allocationBytes.keySet().stream().mapToInt(allocationCounts::count).min().getAsInt();
        int currentMax = allocationBytes.keySet().stream().mapToInt(allocationCounts::count).max().getAsInt();
        int numBuckets = distributionAssignments.size();
        int targetMin = (int) Math.floor((numBuckets * 1.0) / clusterState.getActiveNodes().size());
        int targetMax = (int) Math.ceil((numBuckets * 1.0) / clusterState.getActiveNodes().size());
        log.info("Distribution %s: Current bucket skew: min %s, max %s. Target bucket skew: min %s, max %s", distribution.getId(), currentMin, currentMax, targetMin, targetMax);
        for (String source : ImmutableSet.copyOf(allocationCounts)) {
            List<BucketAssignment> existingAssignments = distributionAssignments.stream().filter(assignment -> assignment.getNodeIdentifier().equals(source)).collect(toList());
            for (BucketAssignment existingAssignment : existingAssignments) {
                if (activeNodes.contains(source) && allocationCounts.count(source) <= targetMin) {
                    break;
                }
                // identify nodes with bucket counts lower than the computed target, and greedily select from this set based on projected disk utilization.
                // greediness means that this may produce decidedly non-optimal results if one looks at the global distribution of buckets->nodes.
                // also, this assumes that nodes in a cluster have identical storage capacity
                String target = activeNodes.stream().filter(candidate -> !candidate.equals(source) && allocationCounts.count(candidate) < targetMax).sorted(comparingInt(allocationCounts::count)).min(Comparator.comparingDouble(allocationBytes::get)).orElseThrow(() -> new VerifyException("unable to find target for rebalancing"));
                long bucketSize = clusterState.getDistributionBucketSize().get(distribution);
                // only move bucket if it reduces imbalance
                if (activeNodes.contains(source) && (allocationCounts.count(source) == targetMax && allocationCounts.count(target) == targetMin)) {
                    break;
                }
                allocationCounts.remove(source);
                allocationCounts.add(target);
                allocationBytes.compute(source, (k, v) -> v - bucketSize);
                allocationBytes.compute(target, (k, v) -> v + bucketSize);
                sourceToAllocationChanges.put(existingAssignment.getNodeIdentifier(), new BucketAssignment(existingAssignment.getDistributionId(), existingAssignment.getBucketNumber(), target));
            }
        }
    }
    return sourceToAllocationChanges;
}
Also used : Nested(org.weakref.jmx.Nested) Logger(com.facebook.airlift.log.Logger) NodeSupplier(com.facebook.presto.raptor.NodeSupplier) MetadataConfig(com.facebook.presto.raptor.metadata.MetadataConfig) Multiset(com.google.common.collect.Multiset) CounterStat(com.facebook.airlift.stats.CounterStat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) Duration(io.airlift.units.Duration) Inject(javax.inject.Inject) PreDestroy(javax.annotation.PreDestroy) Executors.newSingleThreadScheduledExecutor(java.util.concurrent.Executors.newSingleThreadScheduledExecutor) HashMultimap(com.google.common.collect.HashMultimap) Node(com.facebook.presto.spi.Node) Managed(org.weakref.jmx.Managed) Collectors.toMap(java.util.stream.Collectors.toMap) HashMultiset(com.google.common.collect.HashMultiset) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ShardManager(com.facebook.presto.raptor.metadata.ShardManager) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) BackupService(com.facebook.presto.raptor.backup.BackupService) Collectors.toSet(java.util.stream.Collectors.toSet) VerifyException(com.google.common.base.VerifyException) Distribution(com.facebook.presto.raptor.metadata.Distribution) Comparator.comparingInt(java.util.Comparator.comparingInt) ImmutableSet(com.google.common.collect.ImmutableSet) NodeManager(com.facebook.presto.spi.NodeManager) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) String.format(java.lang.String.format) Threads.daemonThreadsNamed(com.facebook.airlift.concurrent.Threads.daemonThreadsNamed) BucketNode(com.facebook.presto.raptor.metadata.BucketNode) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) RaptorConnectorId(com.facebook.presto.raptor.RaptorConnectorId) PostConstruct(javax.annotation.PostConstruct) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) HashMap(java.util.HashMap) VerifyException(com.google.common.base.VerifyException) Distribution(com.facebook.presto.raptor.metadata.Distribution)

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