/*
 * 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.cube.kv;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.kylin.cube.CubeSegment;
import org.apache.kylin.cube.cuboid.Cuboid;
import org.apache.kylin.dict.Dictionary;
import org.apache.kylin.metadata.model.TblColRef;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author xjiang
 * 
 */
public abstract class AbstractRowKeyEncoder {

    public static final byte DEFAULT_BLANK_BYTE = Dictionary.NULL;

    protected static final Logger logger = LoggerFactory.getLogger(AbstractRowKeyEncoder.class);

    private static final Map<String, Map<Long, AbstractRowKeyEncoder>> ENCODER_CACHE = new ConcurrentHashMap<String, Map<Long, AbstractRowKeyEncoder>>();

    public static AbstractRowKeyEncoder createInstance(CubeSegment cubeSeg, Cuboid cuboid) {

        // The storage location identifier is unique for every segment
        Map<Long, AbstractRowKeyEncoder> cubeCache = ENCODER_CACHE.get(cubeSeg.getStorageLocationIdentifier());

        if (cubeCache == null) {
            cubeCache = new HashMap<Long, AbstractRowKeyEncoder>();
            ENCODER_CACHE.put(cuboid.getCube().getName(), cubeCache);
        }

        AbstractRowKeyEncoder encoder = cubeCache.get(cuboid.getId());
        if (encoder == null) {
            encoder = new RowKeyEncoder(cubeSeg, cuboid);
            cubeCache.put(cuboid.getId(), encoder);
        }
        return encoder;
    }

    protected final Cuboid cuboid;
    protected byte blankByte = DEFAULT_BLANK_BYTE;
    protected boolean encodeShard = true;

    protected AbstractRowKeyEncoder(Cuboid cuboid) {
        this.cuboid = cuboid;
    }

    public void setBlankByte(byte blankByte) {
        this.blankByte = blankByte;
    }

    public void setEncodeShard(boolean encodeShard) {
        this.encodeShard = encodeShard;
    }

    abstract public byte[] encode(Map<TblColRef, String> valueMap);

    abstract public byte[] encode(byte[][] values);
}

@@ -840,34 +840,8 @@
 v;%0A%0A
-import java.util.HashMap;%0A
 impo
@@ -1636,25 +1636,24 @@
 boid) %7B%0A
-%0A
         
 // The s
@@ -1648,610 +1648,49 @@
     
-// The storage location identifier is unique for every segment%0A        Map%3CLong, AbstractRowKeyEncoder%3E cubeCache = ENCODER_CACHE.get(cubeSeg.getStorageLocationIdentifier());%0A%0A        if (cubeCache == null) %7B%0A            cubeCache = new HashMap%3CLong, AbstractRowKeyEncoder%3E();%0A            ENCODER_CACHE.put(cuboid.getCube().getName(), cubeCache);%0A        %7D%0A%0A        AbstractRowKeyEncoder encoder = cubeCache.get(cuboid.getId());%0A        if (encoder == null) %7B%0A            encoder = new RowKeyEncoder(cubeSeg, cuboid);%0A            cubeCache.put(cuboid.getId(), encoder);%0A        %7D%0A        return encoder
+return new RowKeyEncoder(cubeSeg, cuboid)
 ;%0A  

Drop cache in AbstractRowKeyEncoder.createInstance()