/*
* 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.block.uncompressed;
import com.facebook.presto.block.Block;
import com.facebook.presto.block.BlockCursor;
import com.facebook.presto.block.RandomAccessBlock;
import com.facebook.presto.serde.BlockEncoding;
import com.facebook.presto.serde.UncompressedBlockEncoding;
import com.facebook.presto.tuple.TupleInfo;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import io.airlift.slice.Slice;
import io.airlift.units.DataSize;
import io.airlift.units.DataSize.Unit;
import static io.airlift.slice.SizeOf.SIZE_OF_BYTE;
import static io.airlift.slice.SizeOf.SIZE_OF_DOUBLE;
public class UncompressedDoubleBlock
implements RandomAccessBlock
{
private static final int ENTRY_SIZE = SIZE_OF_DOUBLE + SIZE_OF_BYTE;
private final Slice slice;
private final int positionCount;
public UncompressedDoubleBlock(int positionCount, Slice slice)
{
Preconditions.checkArgument(positionCount >= 0, "positionCount is negative");
Preconditions.checkNotNull(positionCount, "positionCount is null");
this.positionCount = positionCount;
this.slice = slice;
}
@Override
public TupleInfo getTupleInfo()
{
return TupleInfo.SINGLE_DOUBLE;
}
@Override
public int getPositionCount()
{
return positionCount;
}
@Override
public DataSize getDataSize()
{
return new DataSize(slice.length(), Unit.BYTE);
}
@Override
public BlockCursor cursor()
{
return new UncompressedDoubleBlockCursor(positionCount, slice);
}
@Override
public BlockEncoding getEncoding()
{
return new UncompressedBlockEncoding(TupleInfo.SINGLE_DOUBLE);
}
@Override
public Block getRegion(int positionOffset, int length)
{
Preconditions.checkPositionIndexes(positionOffset, positionOffset + length, positionCount);
return cursor().getRegionAndAdvance(length);
}
@Override
public RandomAccessBlock toRandomAccessBlock()
{
return this;
}
@Override
public boolean getBoolean(int position)
{
throw new UnsupportedOperationException();
}
@Override
public long getLong(int position)
{
throw new UnsupportedOperationException();
}
@Override
public double getDouble(int position)
{
checkReadablePosition(position);
return slice.getDouble((position * ENTRY_SIZE) + SIZE_OF_BYTE);
}
@Override
public Slice getSlice(int position)
{
throw new UnsupportedOperationException();
}
@Override
public boolean isNull(int position)
{
checkReadablePosition(position);
return slice.getByte((position * ENTRY_SIZE)) != 0;
}
@Override
public String toString()
{
return Objects.toStringHelper(this)
.add("positionCount", positionCount)
.add("slice", slice)
.toString();
}
private void checkReadablePosition(int position)
{
Preconditions.checkState(position > 0 && position < positionCount, "position is not valid");
}
}