/
DimensionRawColumnChunk.java
198 lines (179 loc) · 7.11 KB
/
DimensionRawColumnChunk.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.carbondata.core.datastore.chunk.impl;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.BitSet;
import java.util.List;
import org.apache.carbondata.core.constants.CarbonCommonConstants;
import org.apache.carbondata.core.datastore.FileReader;
import org.apache.carbondata.core.datastore.chunk.AbstractRawColumnChunk;
import org.apache.carbondata.core.datastore.chunk.DimensionColumnPage;
import org.apache.carbondata.core.datastore.chunk.reader.DimensionColumnChunkReader;
import org.apache.carbondata.core.datastore.compression.Compressor;
import org.apache.carbondata.core.datastore.compression.CompressorFactory;
import org.apache.carbondata.core.datastore.page.ColumnPage;
import org.apache.carbondata.core.datastore.page.encoding.ColumnPageDecoder;
import org.apache.carbondata.core.datastore.page.encoding.DefaultEncodingFactory;
import org.apache.carbondata.core.memory.MemoryException;
import org.apache.carbondata.core.scan.result.vector.CarbonDictionary;
import org.apache.carbondata.core.scan.result.vector.impl.CarbonDictionaryImpl;
import org.apache.carbondata.core.util.CarbonMetadataUtil;
import org.apache.carbondata.format.Encoding;
import org.apache.carbondata.format.LocalDictionaryChunk;
/**
* Contains raw dimension data,
* 1. The read uncompressed raw data of column chunk with all pages is stored in this instance.
* 2. The raw data can be converted to processed chunk using decodeColumnPage method
* by specifying page number.
*/
public class DimensionRawColumnChunk extends AbstractRawColumnChunk {
private DimensionColumnPage[] dataChunks;
private DimensionColumnChunkReader chunkReader;
private FileReader fileReader;
private CarbonDictionary localDictionary;
public DimensionRawColumnChunk(int columnIndex, ByteBuffer rawData, long offSet, int length,
DimensionColumnChunkReader columnChunkReader) {
super(columnIndex, rawData, offSet, length);
this.chunkReader = columnChunkReader;
}
/**
* Convert all raw data with all pages to processed DimensionColumnPage's
* @return
*/
public DimensionColumnPage[] decodeAllColumnPages() {
if (dataChunks == null) {
dataChunks = new DimensionColumnPage[pagesCount];
}
for (int i = 0; i < pagesCount; i++) {
try {
if (dataChunks[i] == null) {
dataChunks[i] = chunkReader.decodeColumnPage(this, i);
}
} catch (IOException | MemoryException e) {
throw new RuntimeException(e);
}
}
return dataChunks;
}
/**
* Convert raw data with specified page number processed to DimensionColumnPage
* @param pageNumber
* @return
*/
public DimensionColumnPage decodeColumnPage(int pageNumber) {
assert pageNumber < pagesCount;
if (dataChunks == null) {
dataChunks = new DimensionColumnPage[pagesCount];
}
if (dataChunks[pageNumber] == null) {
try {
dataChunks[pageNumber] = chunkReader.decodeColumnPage(this, pageNumber);
} catch (IOException | MemoryException e) {
throw new RuntimeException(e);
}
}
return dataChunks[pageNumber];
}
/**
* Convert raw data with specified page number processed to DimensionColumnDataChunk
*
* @param index
* @return
*/
public DimensionColumnPage convertToDimColDataChunkWithOutCache(int index) {
assert index < pagesCount;
// in case of filter query filter column if filter column is decoded and stored.
// then return the same
if (dataChunks != null && null != dataChunks[index]) {
return dataChunks[index];
}
try {
return chunkReader.decodeColumnPage(this, index);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override public void freeMemory() {
super.freeMemory();
if (null != dataChunks) {
for (int i = 0; i < dataChunks.length; i++) {
if (dataChunks[i] != null) {
dataChunks[i].freeMemory();
dataChunks[i] = null;
}
}
}
rawData = null;
}
public void setFileReader(FileReader fileReader) {
this.fileReader = fileReader;
}
public FileReader getFileReader() {
return fileReader;
}
public CarbonDictionary getLocalDictionary() {
if (null != getDataChunkV3() && null != getDataChunkV3().local_dictionary
&& null == localDictionary) {
try {
String compressorName = CarbonMetadataUtil.getCompressorNameFromChunkMeta(
getDataChunkV3().data_chunk_list.get(0).chunk_meta);
Compressor compressor = CompressorFactory.getInstance().getCompressor(compressorName);
localDictionary = getDictionary(getDataChunkV3().local_dictionary, compressor);
} catch (IOException | MemoryException e) {
throw new RuntimeException(e);
}
}
return localDictionary;
}
/**
* Below method will be used to get the local dictionary for a blocklet
* @param localDictionaryChunk
* local dictionary chunk thrift object
* @return local dictionary
* @throws IOException
* @throws MemoryException
*/
private CarbonDictionary getDictionary(LocalDictionaryChunk localDictionaryChunk,
Compressor compressor) throws IOException, MemoryException {
if (null != localDictionaryChunk) {
List<Encoding> encodings = localDictionaryChunk.getDictionary_meta().getEncoders();
List<ByteBuffer> encoderMetas = localDictionaryChunk.getDictionary_meta().getEncoder_meta();
ColumnPageDecoder decoder = DefaultEncodingFactory.getInstance().createDecoder(
encodings, encoderMetas, compressor.getName());
ColumnPage decode = decoder.decode(localDictionaryChunk.getDictionary_data(), 0,
localDictionaryChunk.getDictionary_data().length);
BitSet usedDictionary = BitSet.valueOf(compressor.unCompressByte(
localDictionaryChunk.getDictionary_values()));
int length = usedDictionary.length();
int index = 0;
byte[][] dictionary = new byte[length][];
for (int i = 0; i < length; i++) {
if (usedDictionary.get(i)) {
dictionary[i] = decode.getBytes(index++);
} else {
dictionary[i] = null;
}
}
decode.freeMemory();
// as dictionary values starts from 1 setting null default value
dictionary[1] = CarbonCommonConstants.MEMBER_DEFAULT_VAL_ARRAY;
return new CarbonDictionaryImpl(dictionary, usedDictionary.cardinality());
}
return null;
}
}