siddharthteotia commented on a change in pull request #6710: URL: https://github.com/apache/incubator-pinot/pull/6710#discussion_r606091778
########## File path: pinot-core/src/main/java/org/apache/pinot/core/common/datatable/BaseDataTable.java ########## @@ -0,0 +1,283 @@ +/** + * 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.pinot.core.common.datatable; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.HashMap; +import java.util.Map; +import org.apache.pinot.common.utils.DataSchema; +import org.apache.pinot.common.utils.DataTable; +import org.apache.pinot.common.utils.StringUtil; +import org.apache.pinot.core.common.ObjectSerDeUtils; +import org.apache.pinot.spi.utils.ByteArray; +import org.apache.pinot.spi.utils.BytesUtils; + +import static org.apache.pinot.core.common.datatable.DataTableUtils.decodeString; + + +/** + * Base implementation of the DataTable interface. + */ +public abstract class BaseDataTable implements DataTable { + protected int _numRows; + protected int _numColumns; + protected DataSchema _dataSchema; + protected int[] _columnOffsets; + protected int _rowSizeInBytes; + protected Map<String, Map<Integer, String>> _dictionaryMap; + protected byte[] _fixedSizeDataBytes; + protected ByteBuffer _fixedSizeData; + protected byte[] _variableSizeDataBytes; + protected ByteBuffer _variableSizeData; + protected Map<String, String> _metadata; + + public BaseDataTable(int numRows, DataSchema dataSchema, Map<String, Map<Integer, String>> dictionaryMap, + byte[] fixedSizeDataBytes, byte[] variableSizeDataBytes) { + _numRows = numRows; + _numColumns = dataSchema.size(); + _dataSchema = dataSchema; + _columnOffsets = new int[_numColumns]; + _rowSizeInBytes = DataTableUtils.computeColumnOffsets(dataSchema, _columnOffsets); + _dictionaryMap = dictionaryMap; + _fixedSizeDataBytes = fixedSizeDataBytes; + _fixedSizeData = ByteBuffer.wrap(fixedSizeDataBytes); + _variableSizeDataBytes = variableSizeDataBytes; + _variableSizeData = ByteBuffer.wrap(variableSizeDataBytes); + _metadata = new HashMap<>(); + } + + /** + * Construct empty data table. (Server side) + */ + public BaseDataTable() { + _numRows = 0; + _numColumns = 0; + _dataSchema = null; + _columnOffsets = null; + _rowSizeInBytes = 0; + _dictionaryMap = null; + _fixedSizeDataBytes = null; + _fixedSizeData = null; + _variableSizeDataBytes = null; + _variableSizeData = null; + _metadata = new HashMap<>(); + } + + /** + * Helper method to serialize dictionary map. + */ + protected byte[] serializeDictionaryMap(Map<String, Map<Integer, String>> dictionaryMap) + throws IOException { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream); + + dataOutputStream.writeInt(dictionaryMap.size()); + for (Map.Entry<String, Map<Integer, String>> dictionaryMapEntry : dictionaryMap.entrySet()) { + String columnName = dictionaryMapEntry.getKey(); + Map<Integer, String> dictionary = dictionaryMapEntry.getValue(); + byte[] bytes = StringUtil.encodeUtf8(columnName); + dataOutputStream.writeInt(bytes.length); + dataOutputStream.write(bytes); + dataOutputStream.writeInt(dictionary.size()); + + for (Map.Entry<Integer, String> dictionaryEntry : dictionary.entrySet()) { + dataOutputStream.writeInt(dictionaryEntry.getKey()); + byte[] valueBytes = StringUtil.encodeUtf8(dictionaryEntry.getValue()); + dataOutputStream.writeInt(valueBytes.length); + dataOutputStream.write(valueBytes); + } + } + + return byteArrayOutputStream.toByteArray(); + } + + /** + * Helper method to deserialize dictionary map. + */ + protected Map<String, Map<Integer, String>> deserializeDictionaryMap(byte[] bytes) + throws IOException { + try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); + DataInputStream dataInputStream = new DataInputStream(byteArrayInputStream)) { + int numDictionaries = dataInputStream.readInt(); + Map<String, Map<Integer, String>> dictionaryMap = new HashMap<>(numDictionaries); + + for (int i = 0; i < numDictionaries; i++) { + String column = decodeString(dataInputStream); + int dictionarySize = dataInputStream.readInt(); + Map<Integer, String> dictionary = new HashMap<>(dictionarySize); + for (int j = 0; j < dictionarySize; j++) { + int key = dataInputStream.readInt(); + String value = decodeString(dataInputStream); + dictionary.put(key, value); + } + dictionaryMap.put(column, dictionary); + } + + return dictionaryMap; + } + } + + public Map<String, String> getMetadata() { Review comment: @mqliang , can you please address this? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
