<commit_before>package com.hazelcast.simulator.tests.helpers;

import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.Member;
import com.hazelcast.core.Partition;
import com.hazelcast.core.PartitionService;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import static com.hazelcast.simulator.tests.helpers.HazelcastTestUtils.isClient;
import static com.hazelcast.simulator.utils.CommonUtils.sleepSeconds;
import static com.hazelcast.simulator.utils.GeneratorUtils.generateString;

public final class KeyUtils {

    private KeyUtils() {
    }

    /**
     * Generates an int key with a configurable locality.
     *
     * @param keyMaxValue max value of the key
     * @param keyLocality if the key is local/remote/random
     * @param instance    the HazelcastInstance that is used for keyLocality
     * @return the created key
     */
    public static int generateIntKey(int keyMaxValue, KeyLocality keyLocality, HazelcastInstance instance) {
        return generateKey(keyLocality, instance, new IntGenerator(keyMaxValue));
    }

    /**
     * Generates an array of int keys with a configurable locality.
     *
     * If the instance is a client, keyLocality is ignored.
     *
     * @param keyCount    the number of keys in the array
     * @param keyMaxValue max value of the key
     * @param keyLocality if the key is local/remote/random
     * @param instance    the HazelcastInstance that is used for keyLocality
     * @return the created array of keys
     */
    public static int[] generateIntKeys(int keyCount, int keyMaxValue, KeyLocality keyLocality, HazelcastInstance instance) {
        int[] keys = new int[keyCount];
        for (int k = 0; k < keys.length; k++) {
            keys[k] = generateIntKey(keyMaxValue, keyLocality, instance);
        }
        return keys;
    }

    /**
     * Generates a string key with a configurable locality.
     *
     * @param keyLength   the length of each string key
     * @param keyLocality if the key is local/remote/random
     * @param instance    the HazelcastInstance that is used for keyLocality
     * @return the created key
     */
    public static String generateStringKey(int keyLength, KeyLocality keyLocality, HazelcastInstance instance) {
        return generateKey(keyLocality, instance, new StringGenerator(keyLength));
    }

    public static String[] generateStringKeys(int keyCount, String basename, KeyLocality keyLocality, HazelcastInstance instance) {
        int keyLength = (int) (basename.length() + Math.ceil(Math.log10(keyCount))) + 2;
        return generateStringKeys(keyLength, keyCount, basename, keyLocality, instance);
    }

    public static String[] generateStringKeys(int keyLength, int keyCount, String basename, KeyLocality keyLocality, HazelcastInstance instance) {
        Set<Integer> targetPartitions = getTargetPartitions(keyLocality, instance);
        PartitionService partitionService = instance.getPartitionService();

        Map<Integer, Set<String>> keysPerPartitionMap = new HashMap<Integer, Set<String>>();
        for (Integer partitionId : targetPartitions) {
            keysPerPartitionMap.put(partitionId, new HashSet<String>());
        }

        int maxKeysPerPartition = (int) Math.ceil(keyCount / (float) targetPartitions.size());

        System.out.println("keyCount:"+keyCount);
        System.out.println("targetPartitions.size:" + targetPartitions.size());
        System.out.println("maxKeysPerPartition: " + maxKeysPerPartition);
        System.out.println("targetPartitionCount:" + targetPartitions.size());

        int generatedKeyCount = 0;
        for (; ; ) {
            String key = basename + generateString(keyLength - basename.length());
            Partition partition = partitionService.getPartition(key);
            Set<String> keysPerPartition = keysPerPartitionMap.get(partition.getPartitionId());

            if (keysPerPartition == null) {
                //   System.out.println("wrong partition");
                // we are not interested in this key.
                continue;
            }

            if (keysPerPartition.size() == maxKeysPerPartition) {
                //     System.out.println("max keys per partitions reached");
                // we have reached the maximum number of keys for this given partition
                continue;
            }

            if (!keysPerPartition.add(key)) {
                //   System.out.println("duplicate key");
                // duplicate key, we can ignore it
                continue;
            }

            generatedKeyCount++;
            if (generatedKeyCount == keyCount) {
                break;
            }

            System.out.println("generatedKeyCount:" + generatedKeyCount);
        }

        String[] result = new String[keyCount];
        int index = 0;
        for (Set<String> keysPerPartition : keysPerPartitionMap.values()) {
            for (String s : keysPerPartition) {
                result[index] = s;
                index++;
            }
        }
        return result;<commit_msg>Removed unwanted system.out<commit_after>package com.hazelcast.simulator.tests.helpers;

import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.Member;
import com.hazelcast.core.Partition;
import com.hazelcast.core.PartitionService;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import static com.hazelcast.simulator.tests.helpers.HazelcastTestUtils.isClient;
import static com.hazelcast.simulator.utils.CommonUtils.sleepSeconds;
import static com.hazelcast.simulator.utils.GeneratorUtils.generateString;

public final class KeyUtils {

    private KeyUtils() {
    }

    /**
     * Generates an int key with a configurable locality.
     *
     * @param keyMaxValue max value of the key
     * @param keyLocality if the key is local/remote/random
     * @param instance    the HazelcastInstance that is used for keyLocality
     * @return the created key
     */
    public static int generateIntKey(int keyMaxValue, KeyLocality keyLocality, HazelcastInstance instance) {
        return generateKey(keyLocality, instance, new IntGenerator(keyMaxValue));
    }

    /**
     * Generates an array of int keys with a configurable locality.
     *
     * If the instance is a client, keyLocality is ignored.
     *
     * @param keyCount    the number of keys in the array
     * @param keyMaxValue max value of the key
     * @param keyLocality if the key is local/remote/random
     * @param instance    the HazelcastInstance that is used for keyLocality
     * @return the created array of keys
     */
    public static int[] generateIntKeys(int keyCount, int keyMaxValue, KeyLocality keyLocality, HazelcastInstance instance) {
        int[] keys = new int[keyCount];
        for (int k = 0; k < keys.length; k++) {
            keys[k] = generateIntKey(keyMaxValue, keyLocality, instance);
        }
        return keys;
    }

    /**
     * Generates a string key with a configurable locality.
     *
     * @param keyLength   the length of each string key
     * @param keyLocality if the key is local/remote/random
     * @param instance    the HazelcastInstance that is used for keyLocality
     * @return the created key
     */
    public static String generateStringKey(int keyLength, KeyLocality keyLocality, HazelcastInstance instance) {
        return generateKey(keyLocality, instance, new StringGenerator(keyLength));
    }

    public static String[] generateStringKeys(int keyCount, String basename, KeyLocality keyLocality, HazelcastInstance instance) {
        int keyLength = (int) (basename.length() + Math.ceil(Math.log10(keyCount))) + 2;
        return generateStringKeys(keyLength, keyCount, basename, keyLocality, instance);
    }

    public static String[] generateStringKeys(int keyLength, int keyCount, String basename, KeyLocality keyLocality, HazelcastInstance instance) {
        Set<Integer> targetPartitions = getTargetPartitions(keyLocality, instance);
        PartitionService partitionService = instance.getPartitionService();

        Map<Integer, Set<String>> keysPerPartitionMap = new HashMap<Integer, Set<String>>();
        for (Integer partitionId : targetPartitions) {
            keysPerPartitionMap.put(partitionId, new HashSet<String>());
        }

        int maxKeysPerPartition = (int) Math.ceil(keyCount / (float) targetPartitions.size());

        int generatedKeyCount = 0;
        for (; ; ) {
            String key = basename + generateString(keyLength - basename.length());
            Partition partition = partitionService.getPartition(key);
            Set<String> keysPerPartition = keysPerPartitionMap.get(partition.getPartitionId());

            if (keysPerPartition == null) {
                // we are not interested in this key.
                continue;
            }

            if (keysPerPartition.size() == maxKeysPerPartition) {
                // we have reached the maximum number of keys for this given partition
                continue;
            }

            if (!keysPerPartition.add(key)) {
                // duplicate key, we can ignore it
                continue;
            }

            generatedKeyCount++;
            if (generatedKeyCount == keyCount) {
                break;
            }
        }

        String[] result = new String[keyCount];
        int index = 0;
        for (Set<String> keysPerPartition : keysPerPartitionMap.values()) {
            for (String s : keysPerPartition) {
                result[index] = s;
                index++;
            }
        }
        return result;
{'fraction_non_alphanumeric': 0.08756423432682425, 'fraction_numerical': 0.0012332990750256938, 'mean_word_length': 2.6679231059178288, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3111152', 'n_tokens_mistral': 2621, 'n_tokens_neox': 2549, 'n_words': 733}