/*
 * 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.facebook.presto.operator.repartition;

import com.facebook.presto.spi.block.Block;
import com.facebook.presto.spi.block.ColumnarArray;
import com.facebook.presto.spi.block.ColumnarMap;
import com.facebook.presto.spi.block.ColumnarRow;
import org.openjdk.jol.info.ClassLayout;

import java.util.List;

import static java.util.Objects.requireNonNull;

/**
 * A tree structure to represent a decoded block. Dictionary/Rle blocks will be kept as a
 * separate node because we need the position mappings. For example, a block of map<int, bigint> where the key
 * is dictionary encoded will result in the following tree structure:
 *
 *             ColumnarMap
 *               /      \
 *    DictionaryBlock   LongArrayBlock
 *          |
 *     IntArrayBlock
 */
class DecodedBlockNode
{
    private static final int INSTANCE_SIZE = ClassLayout.parseClass(DecodedBlockNode.class).instanceSize();

    // The decodedBlock could be primitive block, Dictionary/Rle block, or ColumnarArray/Map/Row object
    private final Object decodedBlock;
    private final List<DecodedBlockNode> children;

    public DecodedBlockNode(Object decodedBlock, List<DecodedBlockNode> children)
    {
        this.decodedBlock = requireNonNull(decodedBlock, "decodedBlock is null");
        this.children = requireNonNull(children, "children is null");
    }

    public Object getDecodedBlock()
    {
        return decodedBlock;
    }

    public List<DecodedBlockNode> getChildren()
    {
        return children;
    }

    public long getRetainedSizeInBytes()
    {
        long size = INSTANCE_SIZE;
        if (decodedBlock instanceof Block) {
            size += ((Block) decodedBlock).getRetainedSizeInBytes();
        }
        else if (decodedBlock instanceof ColumnarArray) {
            size += ((ColumnarArray) decodedBlock).getRetainedSizeInBytes();
        }
        else if (decodedBlock instanceof ColumnarMap) {
            size += ((ColumnarMap) decodedBlock).getRetainedSizeInBytes();
        }
        else if (decodedBlock instanceof ColumnarRow) {
            size += ((ColumnarRow) decodedBlock).getRetainedSizeInBytes();
        }

        return size;
    }

    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder("DecodedBlockNode{");
        sb.append("decodedBlock=").append(decodedBlock.toString()).append(",");
        sb.append("childrenCount=").append(children.size()).append(",");
        for (int i = 0; i < children.size(); i++) {
            sb.append("fieldBuffer_").append(i).append("=").append(children.get(i).toString()).append(",");
        }
        sb.append("}");

        return sb.toString();
    }
}

@@ -1653,16 +1653,60 @@
 hildren;
+%0A    private final long retainedSizeInBytes;
 %0A%0A    pu
@@ -1943,199 +1943,23 @@
 %22);%0A
+%0A   
     
-%7D%0A%0A    public Object getDecodedBlock()%0A    %7B%0A        return decodedBlock;%0A    %7D%0A%0A    public List%3CDecodedBlockNode%3E getChildren()%0A    %7B%0A        return children;%0A    %7D%0A%0A    public
  long 
-getR
+r
 etai
@@ -1969,41 +1969,8 @@
 Size
-InBytes()%0A    %7B%0A        long size
  = I
@@ -2032,33 +2032,41 @@
 ) %7B%0A            
-s
+retainedS
 ize += ((Block) 
@@ -2177,33 +2177,41 @@
 ) %7B%0A            
-s
+retainedS
 ize += ((Columna
@@ -2328,33 +2328,41 @@
 ) %7B%0A            
-s
+retainedS
 ize += ((Columna
@@ -2485,17 +2485,25 @@
         
-s
+retainedS
 ize += (
@@ -2570,28 +2570,304 @@
   %7D%0A
-%0A        return size
+        retainedSizeInBytes = retainedSize;%0A    %7D%0A%0A    public Object getDecodedBlock()%0A    %7B%0A        return decodedBlock;%0A    %7D%0A%0A    public List%3CDecodedBlockNode%3E getChildren()%0A    %7B%0A        return children;%0A    %7D%0A%0A    public long getRetainedSizeInBytes()%0A    %7B%0A        return retainedSizeInBytes
 ;%0A  

Refactor getRetainedSizeInBytes in DecodedBlockNode