-
Notifications
You must be signed in to change notification settings - Fork 703
/
TableInfo.java
422 lines (372 loc) · 13.5 KB
/
TableInfo.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
* 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.metadata.schema.table;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.carbondata.common.annotations.Since;
import org.apache.carbondata.common.logging.LogService;
import org.apache.carbondata.common.logging.LogServiceFactory;
import org.apache.carbondata.core.constants.CarbonCommonConstants;
import org.apache.carbondata.core.metadata.AbsoluteTableIdentifier;
import org.apache.carbondata.core.metadata.CarbonTableIdentifier;
import org.apache.carbondata.core.metadata.schema.table.column.ColumnSchema;
import org.apache.carbondata.core.metadata.schema.table.column.ParentColumnTableRelation;
/**
* Store the information about the table.
* it stores the fact table as well as aggregate table present in the schema
*/
public class TableInfo implements Serializable, Writable {
private static final LogService LOGGER =
LogServiceFactory.getLogService(TableInfo.class.getName());
/**
* serialization version
*/
private static final long serialVersionUID = -5034287968314105193L;
/**
* name of the database;
*/
private String databaseName;
/**
* table name to group fact table and aggregate table
*/
private String tableUniqueName;
/**
* fact table information
*/
private TableSchema factTable;
/**
* last updated time to update the table if any changes
*/
private long lastUpdatedTime;
/**
* store location of the table, it will be set in identifier.tablePath also
*/
private String tablePath;
/**
* The boolean field which points if the data written for Non Transactional Table
* or Transactional Table. The difference between Transactional and Non Transactional table is
* Non Transactional Table will not contain any Metadata folder and subsequently
* no TableStatus or Schema files.
* All ACID properties cannot be aplied to Non Transactional Table as there is no Commit points
* i.e. no TableStatus File.
* What ever files present in the path will be read but it system doesnot ensure ACID rules for
* this data, mostly Consistency part.
*
*/
private boolean isTransactionalTable = true;
/**
* The format of the fact table.
* By default it is carbondata, and we also support other format like CSV
*/
@Since("1.4.1")
private String format = "carbondata";
/**
* properties for the format, such as delimiter/header for csv format
*/
@Since("1.4.1")
private Map<String, String> formatProperties;
// this identifier is a lazy field which will be created when it is used first time
private AbsoluteTableIdentifier identifier;
private List<DataMapSchema> dataMapSchemaList;
private List<RelationIdentifier> parentRelationIdentifiers;
/**
* flag to check whether any schema modification operation has happened after creation of table
*/
private boolean isSchemaModified;
public TableInfo() {
dataMapSchemaList = new ArrayList<>();
formatProperties = new HashMap<>();
isTransactionalTable = true;
}
/**
* @return the factTable
*/
public TableSchema getFactTable() {
return factTable;
}
/**
* @param factTable the factTable to set
*/
public void setFactTable(TableSchema factTable) {
this.factTable = factTable;
updateParentRelationIdentifier();
updateIsSchemaModified();
}
private void updateIsSchemaModified() {
if (null != factTable.getSchemaEvolution()) {
// If schema evolution entry list size is > 1 that means an alter operation is performed
// which has added the new schema entry in the schema evolution list.
// Currently apart from create table schema evolution entries
// are getting added only in the alter operations.
isSchemaModified =
factTable.getSchemaEvolution().getSchemaEvolutionEntryList().size() > 1 ? true : false;
}
}
private void updateParentRelationIdentifier() {
Set<RelationIdentifier> parentRelationIdentifiers = new HashSet<>();
this.parentRelationIdentifiers = new ArrayList<>();
List<ColumnSchema> listOfColumns = this.factTable.getListOfColumns();
for (ColumnSchema columnSchema : listOfColumns) {
List<ParentColumnTableRelation> parentColumnTableRelations =
columnSchema.getParentColumnTableRelations();
if (null != parentColumnTableRelations) {
for (int i = 0; i < parentColumnTableRelations.size(); i++) {
parentRelationIdentifiers.add(parentColumnTableRelations.get(i).getRelationIdentifier());
}
}
}
this.parentRelationIdentifiers.addAll(parentRelationIdentifiers);
}
/**
* @return the databaseName
*/
public String getDatabaseName() {
return databaseName;
}
/**
* @param databaseName the databaseName to set
*/
public void setDatabaseName(String databaseName) {
this.databaseName = databaseName;
}
/**
* @return the tableUniqueName
*/
public String getTableUniqueName() {
return tableUniqueName;
}
/**
* @param tableUniqueName the tableUniqueName to set
*/
public void setTableUniqueName(String tableUniqueName) {
this.tableUniqueName = tableUniqueName;
}
/**
* @return the lastUpdatedTime
*/
public long getLastUpdatedTime() {
return lastUpdatedTime;
}
/**
* @param lastUpdatedTime the lastUpdatedTime to set
*/
public void setLastUpdatedTime(long lastUpdatedTime) {
this.lastUpdatedTime = lastUpdatedTime;
}
public void setTablePath(String tablePath) {
this.tablePath = tablePath;
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
public Map<String, String> getFormatProperties() {
return formatProperties;
}
public void setFormatProperties(Map<String, String> formatProperties) {
this.formatProperties = formatProperties;
}
public List<DataMapSchema> getDataMapSchemaList() {
return dataMapSchemaList;
}
public void setDataMapSchemaList(List<DataMapSchema> dataMapSchemaList) {
this.dataMapSchemaList = dataMapSchemaList;
}
/**
* to generate the hash code
*/
@Override public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((databaseName == null) ? 0 : databaseName.hashCode());
result = prime * result + ((tableUniqueName == null) ? 0 : tableUniqueName.hashCode());
return result;
}
/**
* Overridden equals method
*/
@Override public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof TableInfo)) {
return false;
}
TableInfo other = (TableInfo) obj;
if (null == databaseName || null == other.databaseName) {
return false;
}
if (null == tableUniqueName || null == other.tableUniqueName) {
return false;
}
if (!tableUniqueName.equals(other.tableUniqueName)) {
return false;
}
return true;
}
/**
* This method will return the table size. Default table block size will be considered
* in case not specified by the user
*/
int getTableBlockSizeInMB() {
String tableBlockSize = null;
// In case of old store there will not be any map for table properties so table properties
// will be null
Map<String, String> tableProperties = getFactTable().getTableProperties();
if (null != tableProperties) {
tableBlockSize = tableProperties.get(CarbonCommonConstants.TABLE_BLOCKSIZE);
}
if (null == tableBlockSize) {
tableBlockSize = CarbonCommonConstants.BLOCK_SIZE_DEFAULT_VAL;
LOGGER.info("Table block size not specified for " + getTableUniqueName()
+ ". Therefore considering the default value "
+ CarbonCommonConstants.BLOCK_SIZE_DEFAULT_VAL + " MB");
}
return Integer.parseInt(tableBlockSize);
}
@Override
public void write(DataOutput out) throws IOException {
out.writeUTF(databaseName);
out.writeUTF(tableUniqueName);
factTable.write(out);
out.writeLong(lastUpdatedTime);
out.writeUTF(getOrCreateAbsoluteTableIdentifier().getTablePath());
out.writeBoolean(isTransactionalTable);
boolean isChildSchemaExists =
null != dataMapSchemaList && dataMapSchemaList.size() > 0;
out.writeBoolean(isChildSchemaExists);
if (isChildSchemaExists) {
out.writeShort(dataMapSchemaList.size());
for (int i = 0; i < dataMapSchemaList.size(); i++) {
dataMapSchemaList.get(i).write(out);
}
}
boolean isParentTableRelationIndentifierExists =
null != parentRelationIdentifiers && parentRelationIdentifiers.size() > 0;
out.writeBoolean(isParentTableRelationIndentifierExists);
if (isParentTableRelationIndentifierExists) {
out.writeShort(parentRelationIdentifiers.size());
for (int i = 0; i < parentRelationIdentifiers.size(); i++) {
parentRelationIdentifiers.get(i).write(out);
}
}
out.writeBoolean(isSchemaModified);
out.writeUTF(format);
boolean isFormatPropertiesExists = null != formatProperties && formatProperties.size() > 0;
out.writeBoolean(isFormatPropertiesExists);
if (isFormatPropertiesExists) {
out.writeShort(formatProperties.size());
for (Map.Entry<String, String> entry : formatProperties.entrySet()) {
out.writeUTF(entry.getKey());
out.writeUTF(entry.getValue());
}
}
}
@Override public void readFields(DataInput in) throws IOException {
this.databaseName = in.readUTF();
this.tableUniqueName = in.readUTF();
this.factTable = new TableSchema();
this.factTable.readFields(in);
this.lastUpdatedTime = in.readLong();
this.tablePath = in.readUTF();
this.isTransactionalTable = in.readBoolean();
boolean isChildSchemaExists = in.readBoolean();
this.dataMapSchemaList = new ArrayList<>();
if (isChildSchemaExists) {
short numberOfChildTable = in.readShort();
for (int i = 0; i < numberOfChildTable; i++) {
DataMapSchema childSchema = new DataMapSchema();
childSchema.readFields(in);
DataMapSchema dataMapSchema = DataMapSchemaFactory.INSTANCE
.getDataMapSchema(childSchema.getDataMapName(), childSchema.getProviderName());
dataMapSchema.setChildSchema(childSchema.getChildSchema());
dataMapSchema.setRelationIdentifier(childSchema.getRelationIdentifier());
dataMapSchema.setProperties(childSchema.getProperties());
dataMapSchemaList.add(dataMapSchema);
}
}
boolean isParentTableRelationIdentifierExists = in.readBoolean();
if (isParentTableRelationIdentifierExists) {
short parentTableIdentifiersListSize = in.readShort();
this.parentRelationIdentifiers = new ArrayList<>();
for (int i = 0; i < parentTableIdentifiersListSize; i++) {
RelationIdentifier relationIdentifier = new RelationIdentifier(null, null, null);
relationIdentifier.readFields(in);
this.parentRelationIdentifiers.add(relationIdentifier);
}
}
this.isSchemaModified = in.readBoolean();
this.format = in.readUTF();
boolean isFormatPropertiesExists = in.readBoolean();
if (isFormatPropertiesExists) {
short size = in.readShort();
for (int i = 0; i < size; i++) {
String key = in.readUTF();
String value = in.readUTF();
this.formatProperties.put(key, value);
}
}
}
public AbsoluteTableIdentifier getOrCreateAbsoluteTableIdentifier() {
if (identifier == null) {
CarbonTableIdentifier carbontableIdentifier =
new CarbonTableIdentifier(databaseName, factTable.getTableName(), factTable.getTableId());
identifier = AbsoluteTableIdentifier.from(tablePath, carbontableIdentifier);
}
return identifier;
}
public byte[] serialize() throws IOException {
ByteArrayOutputStream bao = new ByteArrayOutputStream();
this.write(new DataOutputStream(bao));
return bao.toByteArray();
}
public static TableInfo deserialize(byte[] bytes) throws IOException {
TableInfo tableInfo = new TableInfo();
DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
tableInfo.readFields(in);
return tableInfo;
}
public List<RelationIdentifier> getParentRelationIdentifiers() {
return parentRelationIdentifiers;
}
public boolean isTransactionalTable() {
return isTransactionalTable;
}
public void setTransactionalTable(boolean transactionalTable) {
isTransactionalTable = transactionalTable;
}
public boolean isSchemaModified() {
return isSchemaModified;
}
}