<commit_before>/* This file is part of VoltDB.
 * Copyright (C) 2008-2013 VoltDB Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with VoltDB.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.voltdb;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Sets;
import com.google.common.collect.TreeMultimap;

import java.util.*;

/**
 * Helper class for distributing a fixed number of buckets over some number of partitions.
 * Can handle adding new partitions by taking buckets from existing partitions and assigning them
 * to new ones
 */
public class Buckets {
    private final List<TreeSet<Integer>> m_partitionTokens = new ArrayList<TreeSet<Integer>>();
    private final int m_tokenCount;
    private final long m_tokenInterval;

    public Buckets(int partitionCount, int tokenCount) {
        Preconditions.checkArgument(partitionCount > 0);
        Preconditions.checkArgument(tokenCount > partitionCount);
        Preconditions.checkArgument(tokenCount % 2 == 0);
        m_tokenCount = tokenCount;
        m_tokenInterval = calculateTokenInterval(tokenCount);
        m_partitionTokens.add(Sets.<Integer>newTreeSet());
        long token = Integer.MIN_VALUE;
        for (int ii = 0; ii < tokenCount; ii++) {
            m_partitionTokens.get(0).add((int)token);
            token += m_tokenInterval;
        }

        addPartitions(partitionCount - 1);

    }

    public void addPartitions(int partitionCount) {
        TreeSet<LoadPair> loadSet = Sets.newTreeSet();
        for (int ii = 0; ii < m_partitionTokens.size(); ii++) {
            loadSet.add(new LoadPair(ii, m_partitionTokens.get(ii)));
        }

        for (int ii = 0; ii < partitionCount; ii++) {
            TreeSet<Integer> d = Sets.newTreeSet();
            m_partitionTokens.add(d);
            loadSet.add(new LoadPair(m_partitionTokens.size() - 1, d)) ;
            addPartition(loadSet);
        }
    }
<commit_msg>For ENG-5256, add a precondition check to prevent more partitions then tokens<commit_after>/* This file is part of VoltDB.
 * Copyright (C) 2008-2013 VoltDB Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with VoltDB.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.voltdb;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Sets;
import com.google.common.collect.TreeMultimap;

import java.util.*;

/**
 * Helper class for distributing a fixed number of buckets over some number of partitions.
 * Can handle adding new partitions by taking buckets from existing partitions and assigning them
 * to new ones
 */
public class Buckets {
    private final List<TreeSet<Integer>> m_partitionTokens = new ArrayList<TreeSet<Integer>>();
    private final int m_tokenCount;
    private final long m_tokenInterval;

    public Buckets(int partitionCount, int tokenCount) {
        Preconditions.checkArgument(partitionCount > 0);
        Preconditions.checkArgument(tokenCount > partitionCount);
        Preconditions.checkArgument(tokenCount % 2 == 0);
        m_tokenCount = tokenCount;
        m_tokenInterval = calculateTokenInterval(tokenCount);
        m_partitionTokens.add(Sets.<Integer>newTreeSet());
        long token = Integer.MIN_VALUE;
        for (int ii = 0; ii < tokenCount; ii++) {
            m_partitionTokens.get(0).add((int)token);
            token += m_tokenInterval;
        }

        addPartitions(partitionCount - 1);

    }

    public void addPartitions(int partitionCount) {
        //Can't have more partitions than tokens
        Preconditions.checkArgument(m_tokenCount > m_partitionTokens.size() + partitionCount);
        TreeSet<LoadPair> loadSet = Sets.newTreeSet();
        for (int ii = 0; ii < m_partitionTokens.size(); ii++) {
            loadSet.add(new LoadPair(ii, m_partitionTokens.get(ii)));
        }

        for (int ii = 0; ii < partitionCount; ii++) {
            TreeSet<Integer> d = Sets.newTreeSet();
            m_partitionTokens.add(d);
            loadSet.add(new LoadPair(m_partitionTokens.size() - 1, d)) ;
            addPartition(loadSet);
        }
    }

{'fraction_non_alphanumeric': 0.09049943672549755, 'fraction_numerical': 0.007510326699211416, 'mean_word_length': 3.2344992050874404, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6067183', 'n_tokens_mistral': 1487, 'n_tokens_neox': 1437, 'n_words': 496}