Class RotateFile

java.lang.Object
org.apache.hadoop.hbase.util.RotateFile

@Private public class RotateFile extends Object
A file storage which supports atomic update through two files, i.e, rotating. The implementation does not require atomic rename.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final org.apache.hadoop.fs.Path[]
     
    private final org.apache.hadoop.fs.FileSystem
     
    private static final org.slf4j.Logger
     
    private final long
     
    private int
     
    private long
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    RotateFile(org.apache.hadoop.fs.FileSystem fs, org.apache.hadoop.fs.Path dir, String name, long maxFileSize)
    Constructs a new RotateFile object with the given parameters.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Deletes the two files used for rotating data.
    byte[]
    Reads the content of the rotate file by selecting the winner file based on the timestamp of the data inside the files.
    private org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.RotateFileData
    read(org.apache.hadoop.fs.Path path)
     
    private int
    select(org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.RotateFileData[] datas)
     
    void
    write(byte[] data)
    Writes the given data to the next file in the rotation, with a timestamp calculated based on the previous timestamp and the current time to make sure it is greater than the previous timestamp.
    (package private) static void
    write(org.apache.hadoop.fs.FileSystem fs, org.apache.hadoop.fs.Path file, long timestamp, byte[] data)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • LOG

      private static final org.slf4j.Logger LOG
    • fs

      private final org.apache.hadoop.fs.FileSystem fs
    • maxFileSize

      private final long maxFileSize
    • files

      private final org.apache.hadoop.fs.Path[] files
    • prevTimestamp

      private long prevTimestamp
    • nextFile

      private int nextFile
  • Constructor Details

    • RotateFile

      public RotateFile(org.apache.hadoop.fs.FileSystem fs, org.apache.hadoop.fs.Path dir, String name, long maxFileSize)
      Constructs a new RotateFile object with the given parameters.
      Parameters:
      fs - the file system to use.
      dir - the directory where the files will be created.
      name - the base name for the files.
      maxFileSize - the maximum size of each file.
  • Method Details

    • read

      private org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.RotateFileData read(org.apache.hadoop.fs.Path path) throws IOException
      Throws:
      IOException
    • select

      private int select(org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.RotateFileData[] datas)
    • read

      public byte[] read() throws IOException
      Reads the content of the rotate file by selecting the winner file based on the timestamp of the data inside the files. It reads the content of both files and selects the one with the latest timestamp as the winner. If a file is incomplete or does not exist, it logs the error and moves on to the next file. It returns the content of the winner file as a byte array. If none of the files have valid data, it returns null.
      Returns:
      a byte array containing the data from the winner file, or null if no valid data is found.
      Throws:
      IOException - if an error occurs while reading the files.
    • write

      static void write(org.apache.hadoop.fs.FileSystem fs, org.apache.hadoop.fs.Path file, long timestamp, byte[] data) throws IOException
      Throws:
      IOException
    • write

      public void write(byte[] data) throws IOException
      Writes the given data to the next file in the rotation, with a timestamp calculated based on the previous timestamp and the current time to make sure it is greater than the previous timestamp. The method also deletes the previous file, which is no longer needed.

      Notice that, for a newly created RotateFile instance, you need to call read() first to initialize the nextFile index, before calling this method.

      Parameters:
      data - the data to be written to the file
      Throws:
      IOException - if an I/O error occurs while writing the data to the file
    • delete

      public void delete() throws IOException
      Deletes the two files used for rotating data. If any of the files cannot be deleted, an IOException is thrown.
      Throws:
      IOException - if there is an error deleting either file