kunal642 commented on a change in pull request #3834: URL: https://github.com/apache/carbondata/pull/3834#discussion_r485374393
########## File path: sdk/sdk/src/main/java/org/apache/carbondata/sdk/file/CarbonIUD.java ########## @@ -0,0 +1,376 @@ +/* + * 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.sdk.file; + +import java.io.File; +import java.io.FilenameFilter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +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.exceptions.sql.InvalidLoadOptionException; +import org.apache.carbondata.core.constants.CarbonCommonConstants; +import org.apache.carbondata.core.metadata.datatype.DataType; +import org.apache.carbondata.core.metadata.datatype.Field; +import org.apache.carbondata.core.scan.expression.ColumnExpression; +import org.apache.carbondata.core.scan.expression.Expression; +import org.apache.carbondata.core.scan.expression.LiteralExpression; +import org.apache.carbondata.core.scan.expression.conditional.EqualToExpression; +import org.apache.carbondata.core.scan.expression.logical.AndExpression; +import org.apache.carbondata.core.scan.expression.logical.OrExpression; +import org.apache.carbondata.hadoop.api.CarbonTableOutputFormat; +import org.apache.carbondata.hadoop.internal.ObjectArrayWritable; + +import org.apache.hadoop.io.NullWritable; +import org.apache.hadoop.mapreduce.RecordWriter; + +public class CarbonIUD { + + private final Map<String, Map<String, Set<String>>> filterColumnToValueMappingForDelete; + private final Map<String, Map<String, Set<String>>> filterColumnToValueMappingForUpdate; + private final Map<String, Map<String, String>> updateColumnToValueMapping; + + private CarbonIUD() { + filterColumnToValueMappingForDelete = new HashMap<>(); + filterColumnToValueMappingForUpdate = new HashMap<>(); + updateColumnToValueMapping = new HashMap<>(); + } + + /** + * @return CarbonIUD object + */ + public static CarbonIUD getInstance() { + return new CarbonIUD(); + } + + /** + * @param path is the table path on which delete is performed + * @param column is the columnName on which records have to be deleted + * @param value of column on which the records have to be deleted + * @return CarbonIUD object + */ + public CarbonIUD delete(String path, String column, String value) { + prepareDelete(path, column, value, filterColumnToValueMappingForDelete); + return this; + } + + /** + * This method deletes the rows at given path by applying the filterExpression + * + * @param path is the table path on which delete is performed + * @param filterExpression is the expression to delete the records + * @throws IOException + * @throws InterruptedException + */ + public void delete(String path, Expression filterExpression) + throws IOException, InterruptedException { + CarbonReader reader = CarbonReader.builder(path) + .projection(new String[] { CarbonCommonConstants.CARBON_IMPLICIT_COLUMN_TUPLEID }) + .filter(filterExpression).build(); + + RecordWriter<NullWritable, ObjectArrayWritable> deleteDeltaWriter = + CarbonTableOutputFormat.getDeleteDeltaRecordWriter(path); + ObjectArrayWritable writable = new ObjectArrayWritable(); + while (reader.hasNext()) { + Object[] row = (Object[]) reader.readNextRow(); + writable.set(row); + deleteDeltaWriter.write(NullWritable.get(), writable); + } + deleteDeltaWriter.close(null); + reader.close(); + } + + /** + * Calling this method will start the execution of delete process + * + * @throws IOException + * @throws InterruptedException + */ + public void closeDelete() throws IOException, InterruptedException { + for (Map.Entry<String, Map<String, Set<String>>> path : this.filterColumnToValueMappingForDelete + .entrySet()) { + deleteExecution(path.getKey()); + } + } + + /** + * @param path is the table path on which update is performed + * @param column is the columnName on which records have to be updated + * @param value of column on which the records have to be updated + * @param updColumn is the name of updatedColumn + * @param updValue is the value of updatedColumn + * @return CarbonUID + */ + public CarbonIUD update(String path, String column, String value, String updColumn, + String updValue) { + prepareUpdate(path, column, value, updColumn, updValue); + return this; + } + + /** + * This method updates the rows at given path by applying the filterExpression + * + * @param path is the table path on which update is performed. + * @param filterExpression is the expression object to update the records + * @param updatedColumnToValueMapping contains the mapping of updatedColumns to updatedValues + * @throws IOException + * @throws InterruptedException + * @throws InvalidLoadOptionException + */ + public void update(String path, Expression filterExpression, + Map<String, String> updatedColumnToValueMapping) + throws IOException, InterruptedException, InvalidLoadOptionException { + File[] indexFiles = getCarbonIndexFile(path); + Schema schema = CarbonSchemaReader.readSchema(indexFiles[0].getAbsolutePath()).asOriginOrder(); + Field[] fields = schema.getFields(); + String[] projectionColumns = new String[fields.length + 1]; + for (int i = 0; i < fields.length; i++) { + projectionColumns[i] = (fields[i].getFieldName()); + } + projectionColumns[projectionColumns.length - 1] = + CarbonCommonConstants.CARBON_IMPLICIT_COLUMN_TUPLEID; + CarbonWriter writer = + CarbonWriter.builder().outputPath(path).withCsvInput(schema).writtenBy("CarbonIUD").build(); + CarbonReader reader = + CarbonReader.builder(path).projection(projectionColumns).filter(filterExpression).build(); + RecordWriter<NullWritable, ObjectArrayWritable> deleteDeltaWriter = + CarbonTableOutputFormat.getDeleteDeltaRecordWriter(path); + ObjectArrayWritable writable = new ObjectArrayWritable(); + + while (reader.hasNext()) { + Object[] row = (Object[]) reader.readNextRow(); + writable.set(Arrays.copyOfRange(row, row.length - 1, row.length)); + for (Map.Entry<String, String> column : updatedColumnToValueMapping.entrySet()) { + row[getColumnIndex(fields, column.getKey())] = column.getValue(); + } + writer.write(Arrays.copyOfRange(row, 0, row.length - 1)); + deleteDeltaWriter.write(NullWritable.get(), writable); + } + deleteDeltaWriter.close(null); + writer.close(); + reader.close(); + } + + /** + * Calling this method will start the execution of update process + * + * @throws IOException + * @throws InterruptedException + * @throws InvalidLoadOptionException + */ + public void closeUpdate() throws IOException, InterruptedException, InvalidLoadOptionException { Review comment: No need to expose closeUpdate and closeDelete to the user, these can be private. The user can simply call commit on all the operations and commit will handle internally ---------------------------------------------------------------- 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]
