vikramahuja1001 commented on a change in pull request #3917:
URL: https://github.com/apache/carbondata/pull/3917#discussion_r509138245



##########
File path: 
core/src/main/java/org/apache/carbondata/core/util/path/TrashUtil.java
##########
@@ -0,0 +1,114 @@
+/*
+ * 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.path;
+
+import java.io.File;
+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.util.CarbonUtil;
+
+import org.apache.commons.io.FileUtils;
+
+import org.apache.hadoop.fs.permission.FsAction;
+import org.apache.hadoop.fs.permission.FsPermission;
+
+import org.apache.log4j.Logger;
+
+public final class TrashUtil {
+
+  /**
+   * Attribute for Carbon LOGGER
+   */
+  private static final Logger LOGGER =
+          LogServiceFactory.getLogService(CarbonUtil.class.getName());
+
+  private TrashUtil() {
+
+  }
+
+  public static void copyDataToTrashFolder(String carbonTablePath, String 
pathOfFileToCopy,
+                                           String suffixToAdd) throws 
IOException {
+    String trashFolderPath = carbonTablePath + 
CarbonCommonConstants.FILE_SEPARATOR +
+            CarbonCommonConstants.CARBON_TRASH_FOLDER_NAME + 
CarbonCommonConstants.FILE_SEPARATOR
+            + suffixToAdd;
+    try {
+      if (new File(pathOfFileToCopy).exists()) {
+        if (!FileFactory.isFileExist(trashFolderPath)) {
+          LOGGER.info("Creating Trash folder at:" + trashFolderPath);
+          FileFactory.createDirectoryAndSetPermission(trashFolderPath,
+                  new FsPermission(FsAction.ALL, FsAction.ALL, FsAction.ALL));
+        }
+        FileUtils.copyFileToDirectory(new File(pathOfFileToCopy),
+                new File(trashFolderPath));
+      }
+    } catch (IOException e) {
+      LOGGER.error("Unable to copy " + pathOfFileToCopy + " to the trash 
folder");
+    }
+  }
+
+  public static void copyDataRecursivelyToTrashFolder(CarbonFile path, String 
carbonTablePath,
+                                                      String segmentNo) throws 
IOException {
+    if (!path.isDirectory()) {
+      // copy data to trash
+      copyDataToTrashFolder(carbonTablePath, path.getAbsolutePath(), 
segmentNo);
+      return;
+    }
+    CarbonFile[] files = path.listFiles();

Review comment:
       changed logic

##########
File path: 
core/src/main/java/org/apache/carbondata/core/util/path/TrashUtil.java
##########
@@ -0,0 +1,114 @@
+/*
+ * 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.path;
+
+import java.io.File;
+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.util.CarbonUtil;
+
+import org.apache.commons.io.FileUtils;
+
+import org.apache.hadoop.fs.permission.FsAction;
+import org.apache.hadoop.fs.permission.FsPermission;
+
+import org.apache.log4j.Logger;
+
+public final class TrashUtil {
+
+  /**
+   * Attribute for Carbon LOGGER
+   */
+  private static final Logger LOGGER =
+          LogServiceFactory.getLogService(CarbonUtil.class.getName());
+
+  private TrashUtil() {
+
+  }
+
+  public static void copyDataToTrashFolder(String carbonTablePath, String 
pathOfFileToCopy,
+                                           String suffixToAdd) throws 
IOException {
+    String trashFolderPath = carbonTablePath + 
CarbonCommonConstants.FILE_SEPARATOR +
+            CarbonCommonConstants.CARBON_TRASH_FOLDER_NAME + 
CarbonCommonConstants.FILE_SEPARATOR
+            + suffixToAdd;
+    try {
+      if (new File(pathOfFileToCopy).exists()) {
+        if (!FileFactory.isFileExist(trashFolderPath)) {
+          LOGGER.info("Creating Trash folder at:" + trashFolderPath);
+          FileFactory.createDirectoryAndSetPermission(trashFolderPath,
+                  new FsPermission(FsAction.ALL, FsAction.ALL, FsAction.ALL));
+        }
+        FileUtils.copyFileToDirectory(new File(pathOfFileToCopy),
+                new File(trashFolderPath));
+      }
+    } catch (IOException e) {
+      LOGGER.error("Unable to copy " + pathOfFileToCopy + " to the trash 
folder");
+    }
+  }
+
+  public static void copyDataRecursivelyToTrashFolder(CarbonFile path, String 
carbonTablePath,
+                                                      String segmentNo) throws 
IOException {
+    if (!path.isDirectory()) {
+      // copy data to trash
+      copyDataToTrashFolder(carbonTablePath, path.getAbsolutePath(), 
segmentNo);
+      return;
+    }
+    CarbonFile[] files = path.listFiles();
+    for (int i = 0; i < files.length; i++) {
+      copyDataRecursivelyToTrashFolder(files[i], carbonTablePath, segmentNo);
+    }
+    return;
+  }
+
+  /**
+   * The below method deletes all the files and folders in the trash folder of 
a carbon table.
+   */
+  public static void deleteAllDataFromTrashFolder(String carbonTablePath)
+          throws IOException {
+    String pathOfTrashFolder = carbonTablePath + 
CarbonCommonConstants.FILE_SEPARATOR +
+            CarbonCommonConstants.CARBON_TRASH_FOLDER_NAME;
+    // if the trash folder exists delete the contents of the trash folder, if 
it does not exists
+    // create a trash folder
+    if (FileFactory.isFileExist(pathOfTrashFolder)) {
+      List<CarbonFile> carbonFileList = 
FileFactory.getFolderList(pathOfTrashFolder);
+      for (CarbonFile carbonFile : carbonFileList) {
+        deleteDataFromTrashFolderByFile(carbonFile);
+      }
+    } else {
+      // Create the new index server temp directory if it does not exist

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:
us...@infra.apache.org


Reply via email to