Package org.apache.hadoop.hbase.util
Class RotateFile
java.lang.Object
org.apache.hadoop.hbase.util.RotateFile
A file storage which supports atomic update through two files, i.e, rotating. The implementation
does not require atomic rename.
-
Field Summary
Modifier and TypeFieldDescriptionprivate 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
ConstructorDescriptionRotateFile
(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 TypeMethodDescriptionvoid
delete()
Deletes the two files used for rotating data.byte[]
read()
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)
-
Field Details
-
LOG
-
fs
-
maxFileSize
-
files
-
prevTimestamp
-
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
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
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 createdRotateFile
instance, you need to callread()
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
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
-