vikramahuja1001 commented on a change in pull request #4005: URL: https://github.com/apache/carbondata/pull/4005#discussion_r532396388
########## File path: core/src/main/java/org/apache/carbondata/core/util/TrashUtil.java ########## @@ -0,0 +1,178 @@ +/* + * 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.util; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.List; + +import org.apache.carbondata.common.logging.LogServiceFactory; +import org.apache.carbondata.core.constants.CarbonCommonConstants; +import org.apache.carbondata.core.datastore.filesystem.CarbonFile; +import org.apache.carbondata.core.datastore.impl.FileFactory; +import org.apache.carbondata.core.mutate.CarbonUpdateUtil; +import org.apache.carbondata.core.util.path.CarbonTablePath; + +import org.apache.hadoop.io.IOUtils; +import org.apache.log4j.Logger; + +/** + * Mantains the trash folder in carbondata. This class has methods to copy data to the trash and + * remove data from the trash. + */ +public final class TrashUtil { + + private static final Logger LOGGER = + LogServiceFactory.getLogService(TrashUtil.class.getName()); + + /** + * Base method to copy the data to the trash folder. + * + * @param sourcePath the path from which to copy the file + * @param destinationPath the path where the file will be copied + * @return + */ + private static void copyToTrashFolder(String sourcePath, String destinationPath) + throws IOException { + DataOutputStream dataOutputStream = null; + DataInputStream dataInputStream = null; + try { + dataOutputStream = FileFactory.getDataOutputStream(destinationPath); + dataInputStream = FileFactory.getDataInputStream(sourcePath); + IOUtils.copyBytes(dataInputStream, dataOutputStream, CarbonCommonConstants.BYTEBUFFER_SIZE); + } catch (IOException exception) { + LOGGER.error("Unable to copy " + sourcePath + " to the trash folder", exception); + throw exception; + } finally { + CarbonUtil.closeStreams(dataInputStream, dataOutputStream); + } + } + + /** + * The below method copies the complete a file to the trash folder. + * + * @param filePathToCopy the files which are to be moved to the trash folder + * @param trashFolderWithTimestamp timestamp, partition folder(if any) and segment number + * @return + */ + public static void copyFileToTrashFolder(String filePathToCopy, + String trashFolderWithTimestamp) throws IOException { + CarbonFile carbonFileToCopy = FileFactory.getCarbonFile(filePathToCopy); + try { + if (carbonFileToCopy.exists()) { + if (!FileFactory.isFileExist(trashFolderWithTimestamp)) { + FileFactory.mkdirs(trashFolderWithTimestamp); + } + if (!FileFactory.isFileExist(trashFolderWithTimestamp + CarbonCommonConstants + .FILE_SEPARATOR + carbonFileToCopy.getName())) { + copyToTrashFolder(filePathToCopy, trashFolderWithTimestamp + CarbonCommonConstants + .FILE_SEPARATOR + carbonFileToCopy.getName()); + } + } + } catch (IOException e) { + // in case there is any issue while copying the file to the trash folder, we need to delete + // the complete segment folder from the trash folder. The trashFolderWithTimestamp contains + // the segment folder too. Delete the folder as it is. + FileFactory.deleteFile(trashFolderWithTimestamp); + LOGGER.error("Error while creating trash folder or copying data to the trash folder", e); + throw e; + } + } + + /** + * The below method copies the complete segment folder to the trash folder. Here, the data files + * in segment are listed and copied one by one to the trash folder. + * + * @param segmentPath the folder which are to be moved to the trash folder + * @param trashFolderWithTimestamp trashfolderpath with complete timestamp and segment number + * @return + */ + public static void copySegmentToTrash(CarbonFile segmentPath, + String trashFolderWithTimestamp) throws IOException { + try { + List<CarbonFile> dataFiles = FileFactory.getFolderList(segmentPath.getAbsolutePath()); + for (CarbonFile carbonFile : dataFiles) { + copyFileToTrashFolder(carbonFile.getAbsolutePath(), trashFolderWithTimestamp); + } + LOGGER.info("Segment: " + segmentPath.getAbsolutePath() + " has been copied to" + + " the trash folder successfully"); + } catch (IOException e) { + LOGGER.error("Error while getting folder list for the segment", e); + throw e; + } + } + + /** + * The below method deletes timestamp subdirectories in the trash folder which have expired as + * per the user defined retention time + */ + public static void deleteExpiredDataFromTrash(String tablePath) { + String trashPath = CarbonTablePath.getTrashFolderPath(tablePath); + // Deleting the timestamp based subdirectories in the trashfolder by the given timestamp. + try { + if (FileFactory.isFileExist(trashPath)) { + List<CarbonFile> timestampFolderList = FileFactory.getFolderList(trashPath); + for (CarbonFile timestampFolder : timestampFolderList) { + // If the timeStamp at which the timeStamp subdirectory has expired as per the user + // defined value, delete the complete timeStamp subdirectory + if (isTrashRetentionTimeoutExceeded(Long.parseLong(timestampFolder.getName()))) { + if (timestampFolder.isFileExist()) { Review comment: done ---------------------------------------------------------------- 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]
