/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.kylin.metadata.serializer;

import org.apache.kylin.common.util.Bytes;
import org.apache.kylin.common.util.BytesUtil;
import org.apache.kylin.metadata.model.DataType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.ByteBuffer;

/**
 * @author yangli9
 * 
 */
public class BigDecimalSerializer extends DataTypeSerializer<BigDecimal> {

    private static final Logger logger = LoggerFactory.getLogger(BigDecimalSerializer.class);
    
    final DataType type;
    final int maxLength;
    
    public BigDecimalSerializer(DataType type) {
        this.type = type;
        // see serialize(): 1 byte scale, 1 byte length, assume every 2 digits takes 1 byte
        this.maxLength = 1 + 1 + (type.getPrecision() + 1) / 2;
    }

    @Override
    public void serialize(BigDecimal value, ByteBuffer out) {
        if (value.scale() > type.getScale()) {
            logger.warn("value's scale has exceeded the " + type.getScale() +", cut it off, to ensure encoded value do not exceed maxLength " + maxLength);
            value = value.setScale(type.getScale(), BigDecimal.ROUND_HALF_EVEN);
        }
        byte[] bytes = value.unscaledValue().toByteArray();
        if (bytes.length + 2 > maxLength) {
            throw new IllegalArgumentException("'" + value + "' exceeds the expected length for type " + type);
        }

        BytesUtil.writeVInt(value.scale(), out);
        BytesUtil.writeVInt(bytes.length, out);
        out.put(bytes);
    }

    @Override
    public BigDecimal deserialize(ByteBuffer in) {
        int scale = BytesUtil.readVInt(in);
        int n = BytesUtil.readVInt(in);

        byte[] bytes = new byte[n];
        in.get(bytes);

        return new BigDecimal(new BigInteger(bytes), scale);
    }


    @Override
    public int peekLength(ByteBuffer in) {
        int mark = in.position();
        
        @SuppressWarnings("unused")
        int scale = BytesUtil.readVInt(in);
        int n = BytesUtil.readVInt(in);
        int len = in.position() - mark + n;
        
        in.position(mark);
        return len;
    }
    
    @Override
    public int maxLength() {
        return maxLength;
    }

    @Override
    public BigDecimal valueOf(byte[] value) {
        if (value == null)
            return new BigDecimal(0);
        else
            return new BigDecimal(Bytes.toString(value));
    }

}

@@ -1424,24 +1424,46 @@
 maxLength;%0D%0A
+    int counter = 0;%0D%0A
     %0D%0A    pu
@@ -1822,24 +1822,69 @@
 Scale()) %7B%0D%0A
+            if (counter %25 10000 == 0) %7B%0D%0A    
             
@@ -1948,16 +1948,17 @@
 cale() +
+ 
 %22, cut i
@@ -2025,18 +2025,59 @@
 axLength
-);
+ + %22 times:%22 + (counter++));%0D%0A            %7D
 %0D%0A      

optimize log