use of com.facebook.presto.array.ObjectBigArray in project presto by prestodb.
the class MultiKeyValuePairs method toMultimapNativeEncoding.
/**
* Serialize as a multimap: map(key, array(value)), each key can be associated with multiple values
*/
public Block toMultimapNativeEncoding() {
Block keys = keyBlockBuilder.build();
Block values = valueBlockBuilder.build();
// Merge values of the same key into an array
BlockBuilder distinctKeyBlockBuilder = keyType.createBlockBuilder(new BlockBuilderStatus(), keys.getPositionCount(), expectedValueSize(keyType, EXPECTED_ENTRY_SIZE));
ObjectBigArray<BlockBuilder> valueArrayBlockBuilders = new ObjectBigArray<>();
valueArrayBlockBuilders.ensureCapacity(keys.getPositionCount());
TypedSet keySet = new TypedSet(keyType, keys.getPositionCount());
for (int keyValueIndex = 0; keyValueIndex < keys.getPositionCount(); keyValueIndex++) {
if (!keySet.contains(keys, keyValueIndex)) {
keySet.add(keys, keyValueIndex);
keyType.appendTo(keys, keyValueIndex, distinctKeyBlockBuilder);
BlockBuilder valueArrayBuilder = valueType.createBlockBuilder(new BlockBuilderStatus(), 10, expectedValueSize(valueType, EXPECTED_ENTRY_SIZE));
valueArrayBlockBuilders.set(keySet.positionOf(keys, keyValueIndex), valueArrayBuilder);
}
valueType.appendTo(values, keyValueIndex, valueArrayBlockBuilders.get(keySet.positionOf(keys, keyValueIndex)));
}
// Write keys and value arrays into one Block
Block distinctKeys = distinctKeyBlockBuilder.build();
Type valueArrayType = new ArrayType(valueType);
BlockBuilder multimapBlockBuilder = new InterleavedBlockBuilder(ImmutableList.of(keyType, valueArrayType), new BlockBuilderStatus(), distinctKeyBlockBuilder.getPositionCount());
for (int i = 0; i < distinctKeys.getPositionCount(); i++) {
keyType.appendTo(distinctKeys, i, multimapBlockBuilder);
valueArrayType.writeObject(multimapBlockBuilder, valueArrayBlockBuilders.get(i).build());
}
return multimapBlockBuilder.build();
}
Aggregations