/
BlockEncodingManager.java
147 lines (126 loc) · 5.65 KB
/
BlockEncodingManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* 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;
import com.facebook.presto.spi.block.ArrayBlockEncoding;
import com.facebook.presto.spi.block.Block;
import com.facebook.presto.spi.block.BlockEncoding;
import com.facebook.presto.spi.block.BlockEncodingSerde;
import com.facebook.presto.spi.block.ByteArrayBlockEncoding;
import com.facebook.presto.spi.block.DictionaryBlockEncoding;
import com.facebook.presto.spi.block.FixedWidthBlockEncoding;
import com.facebook.presto.spi.block.IntArrayBlockEncoding;
import com.facebook.presto.spi.block.LazyBlockEncoding;
import com.facebook.presto.spi.block.LongArrayBlockEncoding;
import com.facebook.presto.spi.block.MapBlockEncoding;
import com.facebook.presto.spi.block.RowBlockEncoding;
import com.facebook.presto.spi.block.RunLengthBlockEncoding;
import com.facebook.presto.spi.block.ShortArrayBlockEncoding;
import com.facebook.presto.spi.block.SingleMapBlockEncoding;
import com.facebook.presto.spi.block.SingleRowBlockEncoding;
import com.facebook.presto.spi.block.VariableWidthBlockEncoding;
import com.facebook.presto.spi.type.TypeManager;
import com.google.common.collect.ImmutableSet;
import io.airlift.slice.SliceInput;
import io.airlift.slice.SliceOutput;
import javax.inject.Inject;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import static com.google.common.base.Preconditions.checkArgument;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;
public final class BlockEncodingManager
implements BlockEncodingSerde
{
private final ConcurrentMap<String, BlockEncoding> blockEncodings = new ConcurrentHashMap<>();
public BlockEncodingManager(TypeManager typeManager, BlockEncoding... blockEncodings)
{
this(typeManager, ImmutableSet.copyOf(blockEncodings));
}
@Inject
public BlockEncodingManager(TypeManager typeManager, Set<BlockEncoding> blockEncodings)
{
// This function should be called from Guice and tests only
// always add the built-in BlockEncodings
addBlockEncoding(new VariableWidthBlockEncoding());
addBlockEncoding(new FixedWidthBlockEncoding());
addBlockEncoding(new ByteArrayBlockEncoding());
addBlockEncoding(new ShortArrayBlockEncoding());
addBlockEncoding(new IntArrayBlockEncoding());
addBlockEncoding(new LongArrayBlockEncoding());
addBlockEncoding(new DictionaryBlockEncoding());
addBlockEncoding(new ArrayBlockEncoding());
addBlockEncoding(new MapBlockEncoding(typeManager));
addBlockEncoding(new SingleMapBlockEncoding(typeManager));
addBlockEncoding(new RowBlockEncoding());
addBlockEncoding(new SingleRowBlockEncoding());
addBlockEncoding(new RunLengthBlockEncoding());
addBlockEncoding(new LazyBlockEncoding());
for (BlockEncoding blockEncoding : requireNonNull(blockEncodings, "blockEncodings is null")) {
addBlockEncoding(blockEncoding);
}
}
public void addBlockEncoding(BlockEncoding blockEncoding)
{
requireNonNull(blockEncoding, "blockEncoding is null");
BlockEncoding existingEntry = blockEncodings.putIfAbsent(blockEncoding.getName(), blockEncoding);
checkArgument(existingEntry == null, "Encoding %s is already registered", blockEncoding.getName());
}
@Override
public Block readBlock(SliceInput input)
{
// read the encoding name
String encodingName = readLengthPrefixedString(input);
// look up the encoding factory
BlockEncoding blockEncoding = blockEncodings.get(encodingName);
checkArgument(blockEncoding != null, "Unknown block encoding %s", encodingName);
// load read the encoding factory from the output stream
return blockEncoding.readBlock(this, input);
}
@Override
public void writeBlock(SliceOutput output, Block block)
{
while (true) {
// get the encoding name
String encodingName = block.getEncodingName();
// look up the BlockEncoding
BlockEncoding blockEncoding = blockEncodings.get(encodingName);
// see if a replacement block should be written instead
Optional<Block> replacementBlock = blockEncoding.replacementBlockForWrite(block);
if (replacementBlock.isPresent()) {
block = replacementBlock.get();
continue;
}
// write the name to the output
writeLengthPrefixedString(output, encodingName);
// write the block to the output
blockEncoding.writeBlock(this, output, block);
break;
}
}
private static String readLengthPrefixedString(SliceInput input)
{
int length = input.readInt();
byte[] bytes = new byte[length];
input.readBytes(bytes);
return new String(bytes, UTF_8);
}
private static void writeLengthPrefixedString(SliceOutput output, String value)
{
byte[] bytes = value.getBytes(UTF_8);
output.writeInt(bytes.length);
output.writeBytes(bytes);
}
}