Java Code Examples for io.airlift.slice.Slice#getRetainedSize()

The following examples show how to use io.airlift.slice.Slice#getRetainedSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VariableWidthBlock.java    From presto with Apache License 2.0 5 votes vote down vote up
VariableWidthBlock(int arrayOffset, int positionCount, Slice slice, int[] offsets, boolean[] valueIsNull)
{
    if (arrayOffset < 0) {
        throw new IllegalArgumentException("arrayOffset is negative");
    }
    this.arrayOffset = arrayOffset;
    if (positionCount < 0) {
        throw new IllegalArgumentException("positionCount is negative");
    }
    this.positionCount = positionCount;

    if (slice == null) {
        throw new IllegalArgumentException("slice is null");
    }
    this.slice = slice;

    if (offsets.length - arrayOffset < (positionCount + 1)) {
        throw new IllegalArgumentException("offsets length is less than positionCount");
    }
    this.offsets = offsets;

    if (valueIsNull != null && valueIsNull.length - arrayOffset < positionCount) {
        throw new IllegalArgumentException("valueIsNull length is less than positionCount");
    }
    this.valueIsNull = valueIsNull;

    sizeInBytes = offsets[arrayOffset + positionCount] - offsets[arrayOffset] + ((Integer.BYTES + Byte.BYTES) * (long) positionCount);
    retainedSizeInBytes = INSTANCE_SIZE + slice.getRetainedSize() + sizeOf(valueIsNull) + sizeOf(offsets);
}
 
Example 2
Source File: AbstractTestBlock.java    From presto with Apache License 2.0 4 votes vote down vote up
private void assertRetainedSize(Block block)
{
    long retainedSize = ClassLayout.parseClass(block.getClass()).instanceSize();
    Field[] fields = block.getClass().getDeclaredFields();
    try {
        for (Field field : fields) {
            if (Modifier.isStatic(field.getModifiers())) {
                continue;
            }
            Class<?> type = field.getType();
            if (type.isPrimitive()) {
                continue;
            }

            field.setAccessible(true);

            if (type == Slice.class) {
                Slice slice = (Slice) field.get(block);
                if (slice != null) {
                    retainedSize += slice.getRetainedSize();
                }
            }
            else if (type == BlockBuilderStatus.class) {
                if (field.get(block) != null) {
                    retainedSize += BlockBuilderStatus.INSTANCE_SIZE;
                }
            }
            else if (type == BlockBuilder.class || type == Block.class) {
                retainedSize += ((Block) field.get(block)).getRetainedSizeInBytes();
            }
            else if (type == BlockBuilder[].class || type == Block[].class) {
                Block[] blocks = (Block[]) field.get(block);
                for (Block innerBlock : blocks) {
                    assertRetainedSize(innerBlock);
                    retainedSize += innerBlock.getRetainedSizeInBytes();
                }
            }
            else if (type == SliceOutput.class) {
                retainedSize += ((SliceOutput) field.get(block)).getRetainedSize();
            }
            else if (type == int[].class) {
                retainedSize += sizeOf((int[]) field.get(block));
            }
            else if (type == boolean[].class) {
                retainedSize += sizeOf((boolean[]) field.get(block));
            }
            else if (type == byte[].class) {
                retainedSize += sizeOf((byte[]) field.get(block));
            }
            else if (type == long[].class) {
                retainedSize += sizeOf((long[]) field.get(block));
            }
            else if (type == short[].class) {
                retainedSize += sizeOf((short[]) field.get(block));
            }
            else if (type == DictionaryId.class) {
                retainedSize += ClassLayout.parseClass(DictionaryId.class).instanceSize();
            }
            else if (type == MapHashTables.class) {
                retainedSize += ((MapHashTables) field.get(block)).getRetainedSizeInBytes();
            }
            else if (type == MethodHandle.class) {
                // MethodHandles are only used in MapBlock/MapBlockBuilder,
                // and they are shared among blocks created by the same MapType.
                // So we don't account for the memory held onto by MethodHandle instances.
                // Otherwise, we will be counting it multiple times.
            }
            else {
                throw new IllegalArgumentException(format("Unknown type encountered: %s", type));
            }
        }
    }
    catch (IllegalAccessException t) {
        throw new RuntimeException(t);
    }
    assertEquals(block.getRetainedSizeInBytes(), retainedSize);
}