<commit_before>package com.jivesoftware.os.filer.keyed.store;

import com.google.common.collect.Iterators;
import com.jivesoftware.os.filer.map.store.api.KeyValueStore;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class VariableKeySizeMapChunkBackedKeyedStore implements KeyedFilerStore, Iterable<KeyValueStore.Entry<IBA, SwappableFiler>> {

    private final PartitionSizedByKeyStore[] keyedStores;

    private VariableKeySizeMapChunkBackedKeyedStore(PartitionSizedByKeyStore[] keyedStores)
            throws Exception {
        this.keyedStores = keyedStores;
    }

    private PartitionSizedByKeyStore get(byte[] key) {

        for (PartitionSizedByKeyStore keyedStore : keyedStores) {
            if (keyedStore.keySize >= key.length) {
                return keyedStore;
            }
        }
        throw new IndexOutOfBoundsException("Key is too long");
    }

    private byte[] pad(byte[] key) {
        int keySize = get(key).keySize;
        byte[] padded = new byte[keySize];
        System.arraycopy(key, 0, padded, 0, key.length);
        return padded;
    }

    @Override
    public SwappableFiler get(byte[] keyBytes, long newFilerInitialCapacity) throws Exception {
        return get(keyBytes).store.get(pad(keyBytes), newFilerInitialCapacity);
    }

    @Override
    public long sizeInBytes() throws Exception {
        long sizeInBytes = 0;
        for (PartitionSizedByKeyStore keyedStore : keyedStores) {
            sizeInBytes += keyedStore.store.mapStoreSizeInBytes();
        }
        return sizeInBytes;
    }

    @Override
    public void close() {
        for (PartitionSizedByKeyStore keyedStore : keyedStores) {
            keyedStore.store.close();
        }
    }


    @Override
    public Iterator<KeyValueStore.Entry<IBA, SwappableFiler>> iterator() {
        List<Iterator<KeyValueStore.Entry<IBA, SwappableFiler>>> iterators = new ArrayList<>();
        for (PartitionSizedByKeyStore keyStore : keyedStores) {
            iterators.add(keyStore.store.iterator());
        }
        return Iterators.concat(iterators.iterator());
    }

    public static class Builder {

        private final List<PartitionSizedByKeyStore> stores = new ArrayList<>();

        public Builder add(int keySize, PartitionedMapChunkBackedKeyedStore partitionedMapChunkBackedKeyedStore) {
            stores.add(new PartitionSizedByKeyStore(keySize, partitionedMapChunkBackedKeyedStore));
            return this;<commit_msg>Added copy to<commit_after>package com.jivesoftware.os.filer.keyed.store;

import com.google.common.collect.Iterators;
import com.jivesoftware.os.filer.map.store.api.KeyValueStore;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class VariableKeySizeMapChunkBackedKeyedStore implements KeyedFilerStore, Iterable<KeyValueStore.Entry<IBA, SwappableFiler>> {

    private final PartitionSizedByKeyStore[] keyedStores;

    private VariableKeySizeMapChunkBackedKeyedStore(PartitionSizedByKeyStore[] keyedStores)
        throws Exception {
        this.keyedStores = keyedStores;
    }

    private PartitionSizedByKeyStore get(byte[] key) {

        for (PartitionSizedByKeyStore keyedStore : keyedStores) {
            if (keyedStore.keySize >= key.length) {
                return keyedStore;
            }
        }
        throw new IndexOutOfBoundsException("Key is too long");
    }

    private byte[] pad(byte[] key) {
        int keySize = get(key).keySize;
        byte[] padded = new byte[keySize];
        System.arraycopy(key, 0, padded, 0, key.length);
        return padded;
    }

    @Override
    public SwappableFiler get(byte[] keyBytes, long newFilerInitialCapacity) throws Exception {
        return get(keyBytes).store.get(pad(keyBytes), newFilerInitialCapacity);
    }

    @Override
    public long sizeInBytes() throws Exception {
        long sizeInBytes = 0;
        for (PartitionSizedByKeyStore keyedStore : keyedStores) {
            sizeInBytes += keyedStore.store.mapStoreSizeInBytes();
        }
        return sizeInBytes;
    }

    @Override
    public void close() {
        for (PartitionSizedByKeyStore keyedStore : keyedStores) {
            keyedStore.store.close();
        }
    }

    public void copyTo(VariableKeySizeMapChunkBackedKeyedStore to) throws Exception {
        for (int i = 0; i < keyedStores.length; i++) {
            keyedStores[i].store.copyTo(to.keyedStores[i].store);
        }
    }

    @Override
    public Iterator<KeyValueStore.Entry<IBA, SwappableFiler>> iterator() {
        List<Iterator<KeyValueStore.Entry<IBA, SwappableFiler>>> iterators = new ArrayList<>();
        for (PartitionSizedByKeyStore keyStore : keyedStores) {
            iterators.add(keyStore.store.iterator());
        }
        return Iterators.concat(iterators.iterator());
    }

    public static class Builder {

        private final List<PartitionSizedByKeyStore> stores = new ArrayList<>();

        public Builder add(int keySize, PartitionedMapChunkBackedKeyedStore partitionedMapChunkBackedKeyedStore) {
            stores.add(new PartitionSizedByKeyStore(keySize, partitionedMapChunkBackedKeyedStore));
            return this;
{'fraction_non_alphanumeric': 0.09106431417188389, 'fraction_numerical': 0.0013280212483399733, 'mean_word_length': 3.1642969984202214, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 25, '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': '1897600', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1527, 'n_words': 305}