core/src/main/java/org/apache/carbondata/core/datastore/filesystem/AbstractDFSCarbonFile.java
/**
   * This method will delete the data in file data from a given offset
   */
This method will delete the data in file data from a given offset
This method will delete the data in file data from a given offset
@Override public boolean truncate(String fileName, long validDataEndOffset) {
    DataOutputStream dataOutputStream = null;
    DataInputStream dataInputStream = null;
    boolean fileTruncatedSuccessfully = false;
    int bufferSize = validDataEndOffset > CarbonCommonConstants.BYTE_TO_KB_CONVERSION_FACTOR ?
        CarbonCommonConstants.BYTE_TO_KB_CONVERSION_FACTOR :
        (int) validDataEndOffset;
    String tempWriteFilePath = fileName + CarbonCommonConstants.TEMPWRITEFILEEXTENSION;
    FileFactory.FileType fileType = FileFactory.getFileType(fileName);
    try {
      CarbonFile tempFile;
      if (FileFactory.isFileExist(tempWriteFilePath, fileType)) {
        tempFile = FileFactory.getCarbonFile(tempWriteFilePath, fileType);
        tempFile.delete();
      }
      FileFactory.createNewFile(tempWriteFilePath, fileType);
      tempFile = FileFactory.getCarbonFile(tempWriteFilePath, fileType);
      byte[] buff = new byte[bufferSize];
      dataInputStream = FileFactory.getDataInputStream(fileName, fileType);
      int read = dataInputStream.read(buff, 0, buff.length);
      dataOutputStream = FileFactory.getDataOutputStream(tempWriteFilePath, fileType);
      dataOutputStream.write(buff, 0, read);
      long remaining = validDataEndOffset - read;
      while (remaining > 0) {
        if (remaining > bufferSize) {
          buff = new byte[bufferSize];
        } else {
          buff = new byte[(int) remaining];
        }
        read = dataInputStream.read(buff, 0, buff.length);
        dataOutputStream.write(buff, 0, read);
        remaining = remaining - read;
      }
      CarbonUtil.closeStreams(dataInputStream, dataOutputStream);
      tempFile.renameForce(fileName);
      fileTruncatedSuccessfully = true;
    } catch (IOException e) {
      LOGGER.error("Exception occurred while truncating the file " + e.getMessage());
    } finally {
      CarbonUtil.closeStreams(dataOutputStream, dataInputStream);
    }
    return fileTruncatedSuccessfully;
  }
@ Override public boolean truncate ( String fileName , long validDataEndOffset ) { DataOutputStream dataOutputStream = null ; DataInputStream dataInputStream = null ; boolean fileTruncatedSuccessfully = false ; int bufferSize = validDataEndOffset > CarbonCommonConstants . BYTE_TO_KB_CONVERSION_FACTOR ? CarbonCommonConstants . BYTE_TO_KB_CONVERSION_FACTOR : ( int ) validDataEndOffset ; String tempWriteFilePath = fileName + CarbonCommonConstants . TEMPWRITEFILEEXTENSION ; FileFactory . FileType fileType = FileFactory . getFileType ( fileName ) ; try { CarbonFile tempFile ; if ( FileFactory . isFileExist ( tempWriteFilePath , fileType ) ) { tempFile = FileFactory . getCarbonFile ( tempWriteFilePath , fileType ) ; tempFile . delete ( ) ; } FileFactory . createNewFile ( tempWriteFilePath , fileType ) ; tempFile = FileFactory . getCarbonFile ( tempWriteFilePath , fileType ) ; byte [ ] buff = new byte [ bufferSize ] ; dataInputStream = FileFactory . getDataInputStream ( fileName , fileType ) ; int read = dataInputStream . read ( buff , 0 , buff . length ) ; dataOutputStream = FileFactory . getDataOutputStream ( tempWriteFilePath , fileType ) ; dataOutputStream . write ( buff , 0 , read ) ; long remaining = validDataEndOffset - read ; while ( remaining > 0 ) { if ( remaining > bufferSize ) { buff = new byte [ bufferSize ] ; } else { buff = new byte [ ( int ) remaining ] ; } read = dataInputStream . read ( buff , 0 , buff . length ) ; dataOutputStream . write ( buff , 0 , read ) ; remaining = remaining - read ; } CarbonUtil . closeStreams ( dataInputStream , dataOutputStream ) ; tempFile . renameForce ( fileName ) ; fileTruncatedSuccessfully = true ; } catch ( IOException e ) { LOGGER . error ( "Exception occurred while truncating the file " + e . getMessage ( ) ) ; } finally { CarbonUtil . closeStreams ( dataOutputStream , dataInputStream ) ; } return fileTruncatedSuccessfully ; }
This method will delete the data in file data from a given offset
This method will delete the data in file data from a given offset
// if bytes to read less than 1024 then buffer size should be equal to the given offset // temporary file name // delete temporary file if it already exists at a given path // create new temporary file // read the data // anytime we should not cross the offset to be read // rename the temp file to original file