use of com.facebook.presto.spi.block.DictionaryBlock in project presto by prestodb.
the class TestPage method testCompactDictionaryBlocks.
@Test
public void testCompactDictionaryBlocks() throws Exception {
int positionCount = 100;
// Create 2 dictionary blocks with the same source id
DictionaryId commonSourceId = randomDictionaryId();
int commonDictionaryUsedPositions = 20;
int[] commonDictionaryIds = getDictionaryIds(positionCount, commonDictionaryUsedPositions);
// first dictionary contains "varbinary" values
Slice[] dictionaryValues1 = createExpectedValues(50);
SliceArrayBlock dictionary1 = new SliceArrayBlock(dictionaryValues1.length, dictionaryValues1);
DictionaryBlock commonSourceIdBlock1 = new DictionaryBlock(positionCount, dictionary1, commonDictionaryIds, commonSourceId);
// second dictionary block is "length(firstColumn)"
BlockBuilder dictionary2 = BIGINT.createBlockBuilder(new BlockBuilderStatus(), dictionary1.getPositionCount());
for (Slice expectedValue : dictionaryValues1) {
BIGINT.writeLong(dictionary2, expectedValue.length());
}
DictionaryBlock commonSourceIdBlock2 = new DictionaryBlock(positionCount, dictionary2.build(), commonDictionaryIds, commonSourceId);
// Create block with a different source id, dictionary size, used
int otherDictionaryUsedPositions = 30;
int[] otherDictionaryIds = getDictionaryIds(positionCount, otherDictionaryUsedPositions);
SliceArrayBlock dictionary3 = new SliceArrayBlock(70, createExpectedValues(70));
DictionaryBlock randomSourceIdBlock = new DictionaryBlock(positionCount, dictionary3, otherDictionaryIds);
Page page = new Page(commonSourceIdBlock1, randomSourceIdBlock, commonSourceIdBlock2);
page.compact();
// dictionary blocks should all be compact
assertTrue(((DictionaryBlock) page.getBlock(0)).isCompact());
assertTrue(((DictionaryBlock) page.getBlock(1)).isCompact());
assertTrue(((DictionaryBlock) page.getBlock(2)).isCompact());
assertEquals(((DictionaryBlock) page.getBlock(0)).getDictionary().getPositionCount(), commonDictionaryUsedPositions);
assertEquals(((DictionaryBlock) page.getBlock(1)).getDictionary().getPositionCount(), otherDictionaryUsedPositions);
assertEquals(((DictionaryBlock) page.getBlock(2)).getDictionary().getPositionCount(), commonDictionaryUsedPositions);
// Blocks that had the same source id before compacting page should have the same source id after compacting page
assertNotEquals(((DictionaryBlock) page.getBlock(0)).getDictionarySourceId(), ((DictionaryBlock) page.getBlock(1)).getDictionarySourceId());
assertEquals(((DictionaryBlock) page.getBlock(0)).getDictionarySourceId(), ((DictionaryBlock) page.getBlock(2)).getDictionarySourceId());
}
use of com.facebook.presto.spi.block.DictionaryBlock in project presto by prestodb.
the class Page method compact.
public void compact() {
if (getRetainedSizeInBytes() <= getSizeInBytes()) {
return;
}
for (int i = 0; i < blocks.length; i++) {
Block block = blocks[i];
if (block instanceof DictionaryBlock) {
continue;
}
if (block.getSizeInBytes() < block.getRetainedSizeInBytes()) {
// Copy the block to compact its size
Block compactedBlock = block.copyRegion(0, block.getPositionCount());
blocks[i] = compactedBlock;
}
}
Map<DictionaryId, DictionaryBlockIndexes> dictionaryBlocks = getRelatedDictionaryBlocks();
for (DictionaryBlockIndexes blockIndexes : dictionaryBlocks.values()) {
List<DictionaryBlock> compactBlocks = compactRelatedBlocks(blockIndexes.getBlocks());
List<Integer> indexes = blockIndexes.getIndexes();
for (int i = 0; i < compactBlocks.size(); i++) {
blocks[indexes.get(i)] = compactBlocks.get(i);
}
}
long retainedSize = 0;
for (Block block : blocks) {
retainedSize += block.getRetainedSizeInBytes();
}
retainedSizeInBytes.set(retainedSize);
}
Aggregations