Skip to content

Commit eebb3e6

Browse files
committed
Replace std::shared_ptr<File> by std::unique_ptr<File>
Shared smart pointer will introduce additional cost, and it also make the ownership complicated. Use unique pointer to make the ownership explicitly visible.
1 parent 1cbab9b commit eebb3e6

14 files changed

+155
-155
lines changed

contrib/pax_storage/src/cpp/clustering/pax_clustering_reader.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ bool PaxClusteringReader::GetNextTuple(TupleTableSlot *slot) {
6363
file->Close();
6464
}
6565

66-
std::shared_ptr<File> file;
67-
std::shared_ptr<File> toast_file;
66+
std::unique_ptr<File> file;
67+
std::unique_ptr<File> toast_file;
6868
file =
6969
file_system_->Open(meta_info.GetFileName(), pax::fs::kReadMode);
7070

@@ -75,7 +75,7 @@ bool PaxClusteringReader::GetNextTuple(TupleTableSlot *slot) {
7575
}
7676

7777
reader_ = MicroPartitionFileFactory::CreateMicroPartitionReader(
78-
options, ReaderFlags::FLAGS_EMPTY, file, toast_file);
78+
options, ReaderFlags::FLAGS_EMPTY, std::move(file), std::move(toast_file));
7979
} else {
8080
return false;
8181
}

contrib/pax_storage/src/cpp/storage/micro_partition_file_factory.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ namespace pax {
3838
std::unique_ptr<MicroPartitionReader>
3939
MicroPartitionFileFactory::CreateMicroPartitionReader(
4040
const MicroPartitionReader::ReaderOptions &options, int32 flags,
41-
std::shared_ptr<File> file, std::shared_ptr<File> toast_file) {
41+
std::unique_ptr<File> file, std::unique_ptr<File> toast_file) {
4242
std::unique_ptr<MicroPartitionReader> reader =
43-
std::make_unique<OrcReader>(file, toast_file);
43+
std::make_unique<OrcReader>(std::move(file), std::move(toast_file));
4444

4545
#ifdef VEC_BUILD
4646
if (flags & ReaderFlags::FLAGS_VECTOR_PATH) {
@@ -63,13 +63,13 @@ MicroPartitionFileFactory::CreateMicroPartitionReader(
6363
std::unique_ptr<MicroPartitionWriter>
6464
MicroPartitionFileFactory::CreateMicroPartitionWriter(
6565
const MicroPartitionWriter::WriterOptions &options,
66-
std::shared_ptr<File> file, std::shared_ptr<File> toast_file) {
66+
std::unique_ptr<File> file, std::unique_ptr<File> toast_file) {
6767
std::vector<pax::porc::proto::Type_Kind> type_kinds;
6868
type_kinds = OrcWriter::BuildSchema(
6969
options.rel_tuple_desc,
7070
options.storage_format == PaxStorageFormat::kTypeStoragePorcVec);
71-
return std::make_unique<OrcWriter>(options, std::move(type_kinds), file,
72-
toast_file);
71+
return std::make_unique<OrcWriter>(options, std::move(type_kinds),
72+
std::move(file), std::move(toast_file));
7373
}
7474

7575
} // namespace pax

contrib/pax_storage/src/cpp/storage/micro_partition_file_factory.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ class MicroPartitionFileFactory final {
5252
public:
5353
static std::unique_ptr<MicroPartitionWriter> CreateMicroPartitionWriter(
5454
const MicroPartitionWriter::WriterOptions &options,
55-
std::shared_ptr<File> file,
56-
std::shared_ptr<File> toast_file = nullptr);
55+
std::unique_ptr<File> file,
56+
std::unique_ptr<File> toast_file = nullptr);
5757

5858
static std::unique_ptr<MicroPartitionReader> CreateMicroPartitionReader(
5959
const MicroPartitionReader::ReaderOptions &options, int32 flags,
60-
std::shared_ptr<File> file,
61-
std::shared_ptr<File> toast_file = nullptr);
60+
std::unique_ptr<File> file,
61+
std::unique_ptr<File> toast_file = nullptr);
6262
};
6363

6464
} // namespace pax

contrib/pax_storage/src/cpp/storage/micro_partition_file_factory_test.cc

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ TEST_F(MicroPartitionFileFactoryTest, CreateMicroPartitionWriter) {
7272
auto local_fs = Singleton<LocalFileSystem>::GetInstance();
7373
ASSERT_NE(nullptr, local_fs);
7474

75-
std::shared_ptr<File> file_ptr = local_fs->Open(file_name_, fs::kWriteMode);
76-
EXPECT_NE(nullptr, file_ptr);
75+
std::unique_ptr<File> file_ptr = local_fs->Open(file_name_, fs::kWriteMode);
76+
EXPECT_NE(nullptr, file_ptr.get());
7777

7878
std::vector<std::tuple<ColumnEncoding_Kind, int>> types_encoding;
7979
types_encoding.emplace_back(
@@ -88,7 +88,7 @@ TEST_F(MicroPartitionFileFactoryTest, CreateMicroPartitionWriter) {
8888
writer_options.encoding_opts = types_encoding;
8989

9090
auto writer = MicroPartitionFileFactory::CreateMicroPartitionWriter(
91-
writer_options, file_ptr);
91+
writer_options, std::move(file_ptr));
9292

9393
writer->WriteTuple(tuple_slot);
9494
writer->Close();
@@ -101,8 +101,8 @@ TEST_F(MicroPartitionFileFactoryTest, CreateMicroPartitionReader) {
101101
auto local_fs = Singleton<LocalFileSystem>::GetInstance();
102102
ASSERT_NE(nullptr, local_fs);
103103

104-
std::shared_ptr<File> file_ptr = local_fs->Open(file_name_, fs::kWriteMode);
105-
EXPECT_NE(nullptr, file_ptr);
104+
std::unique_ptr<File> file_ptr = local_fs->Open(file_name_, fs::kWriteMode);
105+
EXPECT_NE(nullptr, file_ptr.get());
106106

107107
std::vector<std::tuple<ColumnEncoding_Kind, int>> types_encoding;
108108
types_encoding.emplace_back(
@@ -117,7 +117,7 @@ TEST_F(MicroPartitionFileFactoryTest, CreateMicroPartitionReader) {
117117
writer_options.encoding_opts = types_encoding;
118118

119119
auto writer = MicroPartitionFileFactory::CreateMicroPartitionWriter(
120-
writer_options, file_ptr);
120+
writer_options, std::move(file_ptr));
121121
TupleTableSlot *tuple_slot_empty = CreateTestTupleTableSlot(false);
122122

123123
writer->WriteTuple(tuple_slot);
@@ -130,7 +130,7 @@ TEST_F(MicroPartitionFileFactoryTest, CreateMicroPartitionReader) {
130130
int32 flags = FLAGS_EMPTY;
131131

132132
auto reader = MicroPartitionFileFactory::CreateMicroPartitionReader(
133-
reader_options, flags, file_ptr);
133+
reader_options, flags, std::move(file_ptr));
134134
reader->ReadTuple(tuple_slot_empty);
135135
EXPECT_TRUE(VerifyTestTupleTableSlot(tuple_slot_empty));
136136

@@ -145,8 +145,8 @@ TEST_F(MicroPartitionFileFactoryTest, OrcReadWithVisibilitymap) {
145145
auto local_fs = Singleton<LocalFileSystem>::GetInstance();
146146
ASSERT_NE(nullptr, local_fs);
147147

148-
std::shared_ptr<File> file_ptr = local_fs->Open(file_name_, fs::kWriteMode);
149-
EXPECT_NE(nullptr, file_ptr);
148+
std::unique_ptr<File> file_ptr = local_fs->Open(file_name_, fs::kWriteMode);
149+
EXPECT_NE(nullptr, file_ptr.get());
150150

151151
std::vector<std::tuple<ColumnEncoding_Kind, int>> types_encoding;
152152
types_encoding.emplace_back(
@@ -161,7 +161,7 @@ TEST_F(MicroPartitionFileFactoryTest, OrcReadWithVisibilitymap) {
161161
writer_options.encoding_opts = types_encoding;
162162

163163
auto writer = MicroPartitionFileFactory::CreateMicroPartitionWriter(
164-
writer_options, file_ptr);
164+
writer_options, std::move(file_ptr));
165165

166166
int tuple_count = 1000;
167167
for (int i = 0; i < tuple_count; i++) {
@@ -186,7 +186,7 @@ TEST_F(MicroPartitionFileFactoryTest, OrcReadWithVisibilitymap) {
186186

187187
TupleTableSlot *tuple_slot_empty = CreateTestTupleTableSlot();
188188
auto reader = MicroPartitionFileFactory::CreateMicroPartitionReader(
189-
reader_options, flags, file_ptr);
189+
reader_options, flags, std::move(file_ptr));
190190

191191
int read_tuple_count = 0;
192192
while (reader->ReadTuple(tuple_slot_empty)) {
@@ -210,8 +210,8 @@ TEST_F(MicroPartitionFileFactoryTest, VecReadWithVisibilitymap) {
210210
auto local_fs = Singleton<LocalFileSystem>::GetInstance();
211211
ASSERT_NE(nullptr, local_fs);
212212

213-
std::shared_ptr<File> file_ptr = local_fs->Open(file_name_, fs::kWriteMode);
214-
EXPECT_NE(nullptr, file_ptr);
213+
std::unique_ptr<File> file_ptr = local_fs->Open(file_name_, fs::kWriteMode);
214+
EXPECT_NE(nullptr, file_ptr.get());
215215

216216
std::vector<std::tuple<ColumnEncoding_Kind, int>> types_encoding;
217217
types_encoding.emplace_back(
@@ -226,7 +226,7 @@ TEST_F(MicroPartitionFileFactoryTest, VecReadWithVisibilitymap) {
226226
writer_options.encoding_opts = types_encoding;
227227

228228
auto writer = MicroPartitionFileFactory::CreateMicroPartitionWriter(
229-
writer_options, file_ptr);
229+
writer_options, std::move(file_ptr));
230230

231231
int tuple_count = 1000;
232232
for (int i = 0; i < tuple_count; i++) {
@@ -264,7 +264,7 @@ TEST_F(MicroPartitionFileFactoryTest, VecReadWithVisibilitymap) {
264264
CreateVecEmptyTupleSlot(tuple_slot->tts_tupleDescriptor);
265265

266266
auto reader = MicroPartitionFileFactory::CreateMicroPartitionReader(
267-
reader_options, flags, file_ptr);
267+
reader_options, flags, std::move(file_ptr));
268268

269269
auto ret = reader->ReadTuple(read_tuple_slot);
270270
ASSERT_TRUE(ret);

contrib/pax_storage/src/cpp/storage/orc/orc_dump_reader.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ OrcDumpReader::OrcDumpReader(DumpConfig *config)
9191
bool OrcDumpReader::Open() {
9292
FileSystem *fs = nullptr;
9393
std::shared_ptr<FileSystemOptions> fs_opt;
94-
std::shared_ptr<File> open_file;
95-
std::shared_ptr<File> open_toast_file;
94+
std::unique_ptr<File> open_file;
95+
std::unique_ptr<File> open_toast_file;
9696

9797
assert(config_);
9898
assert(config_->file_name);
@@ -111,7 +111,7 @@ bool OrcDumpReader::Open() {
111111
}
112112
}
113113

114-
format_reader_ = new OrcFormatReader(open_file, open_toast_file);
114+
format_reader_ = new OrcFormatReader(std::move(open_file), std::move(open_toast_file));
115115
format_reader_->Open();
116116

117117
return true;

contrib/pax_storage/src/cpp/storage/orc/orc_format_reader.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535

3636
namespace pax {
3737

38-
OrcFormatReader::OrcFormatReader(std::shared_ptr<File> file,
39-
std::shared_ptr<File> toast_file)
38+
OrcFormatReader::OrcFormatReader(std::unique_ptr<File> file,
39+
std::unique_ptr<File> toast_file)
4040
: file_(std::move(file)),
4141
toast_file_(std::move(toast_file)),
4242
reused_buffer_(nullptr),

contrib/pax_storage/src/cpp/storage/orc/orc_format_reader.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class OrcDumpReader;
3838
}
3939
class OrcFormatReader final {
4040
public:
41-
explicit OrcFormatReader(std::shared_ptr<File> file, std::shared_ptr<File> toast_file = nullptr);
41+
explicit OrcFormatReader(std::unique_ptr<File> file, std::unique_ptr<File> toast_file = nullptr);
4242

4343
~OrcFormatReader();
4444

@@ -78,8 +78,8 @@ class OrcFormatReader final {
7878
friend class OrcGroupStatsProvider;
7979
std::vector<pax::porc::proto::Type_Kind> column_types_;
8080
std::vector<std::map<std::string, std::string>> column_attrs_;
81-
std::shared_ptr<File> file_;
82-
std::shared_ptr<File> toast_file_;
81+
std::unique_ptr<File> file_;
82+
std::unique_ptr<File> toast_file_;
8383
std::shared_ptr<DataBuffer<char>> reused_buffer_;
8484
size_t num_of_stripes_;
8585
bool is_vec_;

contrib/pax_storage/src/cpp/storage/orc/orc_reader.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ class OrcGroupStatsProvider final : public ColumnStatsProvider {
102102
size_t group_index_;
103103
};
104104

105-
OrcReader::OrcReader(std::shared_ptr<File> file,
106-
std::shared_ptr<File> toast_file)
105+
OrcReader::OrcReader(std::unique_ptr<File> file,
106+
std::unique_ptr<File> toast_file)
107107
: working_group_(nullptr),
108108
cached_group_(nullptr),
109109
current_group_index_(0),
110-
format_reader_(file, toast_file),
110+
format_reader_(std::move(file), std::move(toast_file)),
111111
is_closed_(true) {}
112112

113113
std::unique_ptr<ColumnStatsProvider> OrcReader::GetGroupStatsInfo(

0 commit comments

Comments
 (0)