MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
|
A utility class for writing data to files. 更多...
#include <ob_file_writer.h>
Public 成员函数 | |
ObFileWriter (const string &filename, bool append=false) | |
Constructs an ObFileWriter object with the specified file name and mode. 更多... | |
RC | open_file () |
Opens the file for writing. 更多... | |
void | close_file () |
Closes the file if it is currently open. 更多... | |
RC | write (const string_view &data) |
Writes data to the file. 更多... | |
RC | flush () |
Flushes buffered data to disk. 更多... | |
bool | is_open () const |
Checks if the file is currently open. 更多... | |
string | file_name () const |
Returns the name of the file being written to. 更多... | |
静态 Public 成员函数 | |
static unique_ptr< ObFileWriter > | create_file_writer (const string &filename, bool append) |
Creates a new ObFileWriter instance. 更多... | |
Private 属性 | |
string | filename_ |
The name of the file to be written to. | |
bool | append_ |
Indicates whether the file should be opened in append mode. 更多... | |
ofstream | file_ |
The file stream used for writing data. | |
A utility class for writing data to files.
The ObFileWriter
class provides a convenient interface for writing data to a file. It supports creating and opening files for writing, appending data to existing files, and flushing buffered data to disk. The class ensures proper resource management by providing methods for explicitly closing the file. TODO: use posix
|
inline |
Constructs an ObFileWriter
object with the specified file name and mode.
The constructor initializes the file writer with the given file name. The file is not opened until open_file()
is called. The append
parameter determines whether the file should be opened in append mode or overwrite mode.
filename | The name of the file to write to. |
append | Whether to open the file in append mode (default: false ). |
void oceanbase::ObFileWriter::close_file | ( | ) |
Closes the file if it is currently open.
This method releases the resources associated with the file. After calling this method, the file can no longer be written to until it is reopened.
|
static |
Creates a new ObFileWriter
instance.
This static factory method constructs a new ObFileWriter
object with the specified file name and append mode.
filename | The name of the file to write to. |
append | Whether to open the file in append mode (default: false ). |
unique_ptr
to the created ObFileWriter
object.
|
inline |
Returns the name of the file being written to.
RC oceanbase::ObFileWriter::flush | ( | ) |
Flushes buffered data to disk.
Ensures that all buffered data is written to the file system. This method is useful for ensuring data integrity in cases where the program may terminate unexpectedly.
|
inline |
Checks if the file is currently open.
true
if the file is open, false
otherwise. RC oceanbase::ObFileWriter::open_file | ( | ) |
Opens the file for writing.
This method attempts to open the file specified during the construction of the object. If the file is successfully opened, further write operations can be performed.
RC oceanbase::ObFileWriter::write | ( | const string_view & | data | ) |
Writes data to the file.
Appends the provided data to the file. If the file is not open, the operation will fail.
data | The data to write to the file, provided as a string_view . |
|
private |
Indicates whether the file should be opened in append mode.
If true
, data will be appended to the existing file. If false
, the existing file (if any) will be overwritten when the file is opened.