<commit_before>/*
 *
 *  Copyright 2016 Netflix, Inc.
 *
 *     Licensed under the Apache License, Version 2.0 (the "License");
 *     you may not use this file except in compliance with the License.
 *     You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS,
 *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *     See the License for the specific language governing permissions and
 *     limitations under the License.
 *
 */
package com.netflix.hollow.core.write;

import com.netflix.hollow.core.memory.encoding.FixedLengthElementArray;
import com.netflix.hollow.core.memory.encoding.VarInt;

import com.netflix.hollow.core.schema.HollowListSchema;
import com.netflix.hollow.core.memory.ByteData;
import com.netflix.hollow.core.memory.ByteDataBuffer;
import com.netflix.hollow.core.memory.ThreadSafeBitSet;
import com.netflix.hollow.core.memory.pool.WastefulRecycler;
import java.io.DataOutputStream;
import java.io.IOException;

public class HollowListTypeWriteState extends HollowTypeWriteState {

    /// statistics required for writing fixed length list data
    private int bitsPerListPointer;
    private int bitsPerElement;
    private long totalOfListSizes;
    private long shardTotalOfListSizes[];

    /// data required for writing snapshot or delta
    private int maxOrdinal;
    private int maxShardOrdinal[];
    private FixedLengthElementArray listPointerArray[];
    private FixedLengthElementArray elementArray[];

    /// additional data required for writing delta
    private int numListsInDelta[];
    private long numElementsInDelta[];
    private ByteDataBuffer deltaAddedOrdinals[];
    private ByteDataBuffer deltaRemovedOrdinals[];

    public HollowListTypeWriteState(HollowListSchema schema) {
        this(schema, 1);
    }
    
    public HollowListTypeWriteState(HollowListSchema schema, int numShards) {
        super(schema, numShards);
    }

    @Override
    public HollowListSchema getSchema() {
        return (HollowListSchema)schema;
    }

    @Override
    public void prepareForWrite() {
        super.prepareForWrite();

        totalOfListSizes = 0;

        gatherStatistics();
    }

    private void gatherStatistics() {
        int maxElementOrdinal = 0;

        int maxOrdinal = ordinalMap.maxOrdinal();

        maxShardOrdinal = new int[numShards];
        int minOrdinalsPerShard = (maxOrdinal + 1) / numShards; 
        for(int i=0;i<numShards;i++)
            maxShardOrdinal[i] = (i < ((maxOrdinal + 1) & (numShards - 1))) ? minOrdinalsPerShard : minOrdinalsPerShard - 1;
        
        ByteData data = ordinalMap.getByteData().getUnderlyingArray();
        
        shardTotalOfListSizes = new long[numShards];

        for(int i=0;i<=maxOrdinal;i++) {
            if(currentCyclePopulated.get(i) || previousCyclePopulated.get(i)) {
                long pointer = ordinalMap.getPointerForData(i);
                int size = VarInt.readVInt(data, pointer);

                pointer += VarInt.sizeOfVInt(size);

                for(int j=0;j<size;j++) {
                    int elementOrdinal = VarInt.readVInt(data, pointer);
                    if(elementOrdinal > maxElementOrdinal)
                        maxElementOrdinal = elementOrdinal;
                    pointer += VarInt.sizeOfVInt(elementOrdinal);
                }

                totalOfListSizes += size;
                shardTotalOfListSizes[i & (numShards-1)] += size;
            }
        }

        bitsPerElement = maxElementOrdinal == 0 ? 1 : 64 - Long.numberOfLeadingZeros(maxElementOrdinal);

        bitsPerListPointer = totalOfListSizes == 0 ? 1 : 64 - Long.numberOfLeadingZeros(totalOfListSizes);
    }

    @Override
    public void calculateSnapshot() {
        maxOrdinal = ordinalMap.maxOrdinal();
        
        listPointerArray = new FixedLengthElementArray[numShards];
        elementArray = new FixedLengthElementArray[numShards];

        for(int i=0;i<numShards;i++) {
            listPointerArray[i] = new FixedLengthElementArray(WastefulRecycler.DEFAULT_INSTANCE, (long)bitsPerListPointer * (maxShardOrdinal[i] + 1));
            elementArray[i] = new FixedLengthElementArray(WastefulRecycler.DEFAULT_INSTANCE, (long)bitsPerElement * shardTotalOfListSizes[i]);
        }<commit_msg>Type sharding: List pointers may require fewer bits if sharding results in fewer total elements<commit_after>/*
 *
 *  Copyright 2016 Netflix, Inc.
 *
 *     Licensed under the Apache License, Version 2.0 (the "License");
 *     you may not use this file except in compliance with the License.
 *     You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS,
 *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *     See the License for the specific language governing permissions and
 *     limitations under the License.
 *
 */
package com.netflix.hollow.core.write;

import com.netflix.hollow.core.memory.encoding.FixedLengthElementArray;
import com.netflix.hollow.core.memory.encoding.VarInt;

import com.netflix.hollow.core.schema.HollowListSchema;
import com.netflix.hollow.core.memory.ByteData;
import com.netflix.hollow.core.memory.ByteDataBuffer;
import com.netflix.hollow.core.memory.ThreadSafeBitSet;
import com.netflix.hollow.core.memory.pool.WastefulRecycler;
import java.io.DataOutputStream;
import java.io.IOException;

public class HollowListTypeWriteState extends HollowTypeWriteState {

    /// statistics required for writing fixed length list data
    private int bitsPerListPointer;
    private int bitsPerElement;
    private long shardTotalOfListSizes[];

    /// data required for writing snapshot or delta
    private int maxOrdinal;
    private int maxShardOrdinal[];
    private FixedLengthElementArray listPointerArray[];
    private FixedLengthElementArray elementArray[];

    /// additional data required for writing delta
    private int numListsInDelta[];
    private long numElementsInDelta[];
    private ByteDataBuffer deltaAddedOrdinals[];
    private ByteDataBuffer deltaRemovedOrdinals[];

    public HollowListTypeWriteState(HollowListSchema schema) {
        this(schema, 1);
    }
    
    public HollowListTypeWriteState(HollowListSchema schema, int numShards) {
        super(schema, numShards);
    }

    @Override
    public HollowListSchema getSchema() {
        return (HollowListSchema)schema;
    }

    @Override
    public void prepareForWrite() {
        super.prepareForWrite();

        gatherStatistics();
    }

    private void gatherStatistics() {
        int maxElementOrdinal = 0;

        int maxOrdinal = ordinalMap.maxOrdinal();

        maxShardOrdinal = new int[numShards];
        int minOrdinalsPerShard = (maxOrdinal + 1) / numShards; 
        for(int i=0;i<numShards;i++)
            maxShardOrdinal[i] = (i < ((maxOrdinal + 1) & (numShards - 1))) ? minOrdinalsPerShard : minOrdinalsPerShard - 1;
        
        ByteData data = ordinalMap.getByteData().getUnderlyingArray();
        
        shardTotalOfListSizes = new long[numShards];

        for(int i=0;i<=maxOrdinal;i++) {
            if(currentCyclePopulated.get(i) || previousCyclePopulated.get(i)) {
                long pointer = ordinalMap.getPointerForData(i);
                int size = VarInt.readVInt(data, pointer);

                pointer += VarInt.sizeOfVInt(size);

                for(int j=0;j<size;j++) {
                    int elementOrdinal = VarInt.readVInt(data, pointer);
                    if(elementOrdinal > maxElementOrdinal)
                        maxElementOrdinal = elementOrdinal;
                    pointer += VarInt.sizeOfVInt(elementOrdinal);
                }

                shardTotalOfListSizes[i & (numShards-1)] += size;
            }
        }
        
        long maxShardTotalOfListSizes = 0;
        for(int i=0;i<numShards;i++) {
            if(shardTotalOfListSizes[i] > maxShardTotalOfListSizes)
                maxShardTotalOfListSizes = shardTotalOfListSizes[i];
        }

        bitsPerElement = maxElementOrdinal == 0 ? 1 : 64 - Long.numberOfLeadingZeros(maxElementOrdinal);

        bitsPerListPointer = maxShardTotalOfListSizes == 0 ? 1 : 64 - Long.numberOfLeadingZeros(maxShardTotalOfListSizes);
    }

    @Override
    public void calculateSnapshot() {
        maxOrdinal = ordinalMap.maxOrdinal();
        
        listPointerArray = new FixedLengthElementArray[numShards];
        elementArray = new FixedLengthElementArray[numShards];

        for(int i=0;i<numShards;i++) {
            listPointerArray[i] = new FixedLengthElementArray(WastefulRecycler.DEFAULT_INSTANCE, (long)bitsPerListPointer * (maxShardOrdinal[i] + 1));
            elementArray[i] = new FixedLengthElementArray(WastefulRecycler.DEFAULT_INSTANCE, (long)bitsPerElement * shardTotalOfListSizes[i]);
        }
{'fraction_non_alphanumeric': 0.08824814329401486, 'fraction_numerical': 0.006443861948449104, 'mean_word_length': 2.9675043327556327, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 6, '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': '6964191', 'n_tokens_mistral': 2664, 'n_tokens_neox': 2564, 'n_words': 600}