MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
ob_file_writer.h
1/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2miniob is licensed under Mulan PSL v2.
3You can use this software according to the terms and conditions of the Mulan PSL v2.
4You may obtain a copy of Mulan PSL v2 at:
5 http://license.coscl.org.cn/MulanPSL2
6THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
7EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
8MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9See the Mulan PSL v2 for more details. */
10
11#pragma once
12
13#include "common/lang/fstream.h"
14#include "common/lang/string.h"
15#include "common/lang/string_view.h"
16#include "common/lang/memory.h"
17#include "common/sys/rc.h"
18
19namespace oceanbase {
20
32{
33public:
44 ObFileWriter(const string &filename, bool append = false) : filename_(filename), append_(append) {}
45
47
57 RC open_file();
58
65 void close_file();
66
75 RC write(const string_view &data);
76
85 RC flush();
86
92 bool is_open() const { return file_.is_open(); }
93
99 string file_name() const { return filename_; }
100
111 static unique_ptr<ObFileWriter> create_file_writer(const string &filename, bool append);
112
113private:
117 string filename_;
118
126
130 ofstream file_;
131};
132} // namespace oceanbase
A utility class for writing data to files.
Definition: ob_file_writer.h:32
RC write(const string_view &data)
Writes data to the file.
Definition: ob_file_writer.cpp:17
ObFileWriter(const string &filename, bool append=false)
Constructs an ObFileWriter object with the specified file name and mode.
Definition: ob_file_writer.h:44
string file_name() const
Returns the name of the file being written to.
Definition: ob_file_writer.h:99
bool append_
Indicates whether the file should be opened in append mode.
Definition: ob_file_writer.h:125
RC flush()
Flushes buffered data to disk.
Definition: ob_file_writer.cpp:27
RC open_file()
Opens the file for writing.
Definition: ob_file_writer.cpp:37
string filename_
The name of the file to be written to.
Definition: ob_file_writer.h:117
void close_file()
Closes the file if it is currently open.
Definition: ob_file_writer.cpp:54
bool is_open() const
Checks if the file is currently open.
Definition: ob_file_writer.h:92
static unique_ptr< ObFileWriter > create_file_writer(const string &filename, bool append)
Creates a new ObFileWriter instance.
Definition: ob_file_writer.cpp:62
ofstream file_
The file stream used for writing data.
Definition: ob_file_writer.h:130