Skip to content

Commit fa18b1c

Browse files
committed
fix
1 parent 9528c0b commit fa18b1c

11 files changed

Lines changed: 115 additions & 13 deletions

File tree

‎include/paimon/catalog/table.h‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class TableScan;
3737
class TableRead;
3838

3939
/// A table provides basic abstraction for table type.
40-
class PAIMON_EXPORT Table {
40+
class PAIMON_EXPORT Table : public std::enable_shared_from_this<Table> {
4141
public:
4242
/// Creates a table by loading the latest schema from the specified table path.
4343
///
@@ -84,27 +84,35 @@ class PAIMON_EXPORT Table {
8484
}
8585

8686
/// Creates a scan of this table from a finished scan context, standing in for
87-
/// `TableScan::Create`.
87+
/// `TableScan::Create`. The scan is handed the table's shared owner, so it shares the
88+
/// caller's hold on the table.
8889
/// @param context The finished scan context.
89-
/// @return The scan, or the error `TableScan::Create` reports.
90+
/// @return The scan, or the error `TableScan::Create` reports. A table no `shared_ptr` holds
91+
/// cannot hand itself over and reports that.
9092
Result<std::unique_ptr<TableScan>> NewScan(std::unique_ptr<ScanContext> context) const;
9193

9294
/// Creates a read of this table from a finished read context, standing in for
93-
/// `TableRead::Create`.
95+
/// `TableRead::Create`. The read is handed the table's shared owner, so it shares the
96+
/// caller's hold on the table.
9497
/// @param context The finished read context.
95-
/// @return The read, or the error `TableRead::Create` reports.
98+
/// @return The read, or the error `TableRead::Create` reports. A table no `shared_ptr` holds
99+
/// cannot hand itself over and reports that.
96100
Result<std::unique_ptr<TableRead>> NewRead(std::unique_ptr<ReadContext> context) const;
97101

98102
/// Creates a write of this table from a finished write context, standing in for
99-
/// `FileStoreWrite::Create`.
103+
/// `FileStoreWrite::Create`. The write is handed the table's shared owner, so it shares the
104+
/// caller's hold on the table.
100105
/// @param context The finished write context.
101-
/// @return The write, or the error `FileStoreWrite::Create` reports.
106+
/// @return The write, or the error `FileStoreWrite::Create` reports. A table no `shared_ptr`
107+
/// holds cannot hand itself over and reports that.
102108
Result<std::unique_ptr<FileStoreWrite>> NewWrite(std::unique_ptr<WriteContext> context) const;
103109

104110
/// Creates a commit of this table from a finished commit context, standing in for
105-
/// `FileStoreCommit::Create`.
111+
/// `FileStoreCommit::Create`. The commit is handed the table's shared owner, so it shares
112+
/// the caller's hold on the table.
106113
/// @param context The finished commit context.
107-
/// @return The commit, or the error `FileStoreCommit::Create` reports.
114+
/// @return The commit, or the error `FileStoreCommit::Create` reports. A table no
115+
/// `shared_ptr` holds cannot hand itself over and reports that.
108116
Result<std::unique_ptr<FileStoreCommit>> NewCommit(
109117
std::unique_ptr<CommitContext> context) const;
110118

‎include/paimon/file_store_commit.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
namespace paimon {
4040
class CommitContext;
4141
class CommitMessage;
42+
class Table;
4243

4344
/// Interface for commit operations in a file store.
4445
///
@@ -57,6 +58,15 @@ class PAIMON_EXPORT FileStoreCommit {
5758
/// @return A Result containing a unique pointer to the `FileStoreCommit` instance.
5859
static Result<std::unique_ptr<FileStoreCommit>> Create(std::unique_ptr<CommitContext> context);
5960

61+
/// Create an instance of `FileStoreCommit` for a table the caller already opened.
62+
///
63+
/// @param context A unique pointer to the `CommitContext` used for commit operations.
64+
/// @param table The opened table, or null when the caller has no table to hand over.
65+
///
66+
/// @return A Result containing a unique pointer to the `FileStoreCommit` instance.
67+
static Result<std::unique_ptr<FileStoreCommit>> Create(
68+
std::unique_ptr<CommitContext> context, const std::shared_ptr<const Table>& table);
69+
6070
virtual ~FileStoreCommit() = default;
6171

6272
/// Commit changes to the file store.

‎include/paimon/file_store_write.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
namespace paimon {
3939
class RecordBatch;
40+
class Table;
4041
class WriteContext;
4142

4243
/// Interface for write operations in a file store.
@@ -49,6 +50,15 @@ class PAIMON_EXPORT FileStoreWrite {
4950
/// @return A Result containing a unique pointer to the `FileStoreWrite` instance.
5051
static Result<std::unique_ptr<FileStoreWrite>> Create(std::unique_ptr<WriteContext> context);
5152

53+
/// Create an instance of `FileStoreWrite` for a table the caller already opened.
54+
///
55+
/// @param context A unique pointer to the `WriteContext` used for write operations.
56+
/// @param table The opened table, or null when the caller has no table to hand over.
57+
///
58+
/// @return A Result containing a unique pointer to the `FileStoreWrite` instance.
59+
static Result<std::unique_ptr<FileStoreWrite>> Create(
60+
std::unique_ptr<WriteContext> context, const std::shared_ptr<const Table>& table);
61+
5262
virtual ~FileStoreWrite() = default;
5363

5464
/// Support write an input `RecordBatch` to internal buffer or file.

‎include/paimon/table/source/table_read.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
namespace paimon {
3434
class ReadContext;
35+
class Table;
3536

3637
/// Given a `Split` or a list of `Split`, generate a reader for batch reading.
3738
///
@@ -48,6 +49,14 @@ class PAIMON_EXPORT TableRead {
4849
/// @return A Result containing a unique pointer to the `TableRead` instance.
4950
static Result<std::unique_ptr<TableRead>> Create(std::unique_ptr<ReadContext> context);
5051

52+
/// Create an instance of `TableRead` for a table the caller already opened.
53+
///
54+
/// @param context A unique pointer to the `ReadContext` used for read operations.
55+
/// @param table The opened table, or null when the caller has no table to hand over.
56+
/// @return A Result containing a unique pointer to the `TableRead` instance.
57+
static Result<std::unique_ptr<TableRead>> Create(std::unique_ptr<ReadContext> context,
58+
const std::shared_ptr<const Table>& table);
59+
5160
/// Creates a `BatchReader` instance for reading data.
5261
///
5362
/// This method creates a BatchReader that will be responsible for reading data from the

‎include/paimon/table/source/table_scan.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
namespace paimon {
3434
class ScanContext;
35+
class Table;
3536

3637
/// A scanner interface for reading table's meta and create a plan.
3738
class PAIMON_EXPORT TableScan {
@@ -42,6 +43,14 @@ class PAIMON_EXPORT TableScan {
4243
/// @return A Result containing a unique pointer to the `TableScan` instance.
4344
static Result<std::unique_ptr<TableScan>> Create(std::unique_ptr<ScanContext> context);
4445

46+
/// Create an instance of `TableScan` for a table the caller already opened.
47+
///
48+
/// @param context A unique pointer to the `ScanContext` used for scan operations.
49+
/// @param table The opened table, or null when the caller has no table to hand over.
50+
/// @return A Result containing a unique pointer to the `TableScan` instance.
51+
static Result<std::unique_ptr<TableScan>> Create(std::unique_ptr<ScanContext> context,
52+
const std::shared_ptr<const Table>& table);
53+
4554
virtual ~TableScan() = default;
4655

4756
/// Create a scan plan.

‎src/paimon/core/operation/file_store_commit.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ Result<std::unique_ptr<FileStoreCommit>> NewFormatTableCommit(
174174

175175
Result<std::unique_ptr<FileStoreCommit>> FileStoreCommit::Create(
176176
std::unique_ptr<CommitContext> ctx) {
177+
return Create(std::move(ctx), nullptr);
178+
}
179+
180+
Result<std::unique_ptr<FileStoreCommit>> FileStoreCommit::Create(
181+
std::unique_ptr<CommitContext> ctx,
182+
[[maybe_unused]] const std::shared_ptr<const Table>& table) {
177183
if (ctx == nullptr) {
178184
return Status::Invalid("commit context is null pointer");
179185
}

‎src/paimon/core/operation/file_store_write.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ Status FileStoreWrite::RefreshCommittedSnapshot(int64_t) {
164164
}
165165

166166
Result<std::unique_ptr<FileStoreWrite>> FileStoreWrite::Create(std::unique_ptr<WriteContext> ctx) {
167+
return Create(std::move(ctx), nullptr);
168+
}
169+
170+
Result<std::unique_ptr<FileStoreWrite>> FileStoreWrite::Create(
171+
std::unique_ptr<WriteContext> ctx, [[maybe_unused]] const std::shared_ptr<const Table>& table) {
167172
if (ctx == nullptr) {
168173
return Status::Invalid("write context is null pointer");
169174
}

‎src/paimon/core/table/source/table_read.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ Result<std::unique_ptr<TableRead>> NewFormatTableRead(const std::shared_ptr<Form
170170
} // namespace
171171

172172
Result<std::unique_ptr<TableRead>> TableRead::Create(std::unique_ptr<ReadContext> ctx) {
173+
return Create(std::move(ctx), nullptr);
174+
}
175+
176+
Result<std::unique_ptr<TableRead>> TableRead::Create(
177+
std::unique_ptr<ReadContext> ctx, [[maybe_unused]] const std::shared_ptr<const Table>& table) {
173178
std::shared_ptr<ReadContext> context = std::move(ctx);
174179
if (context == nullptr) {
175180
return Status::Invalid("read context is null pointer");

‎src/paimon/core/table/source/table_scan.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,12 @@ Result<std::vector<std::map<std::string, std::string>>> TableScan::ListPartition
253253
}
254254

255255
Result<std::unique_ptr<TableScan>> TableScan::Create(std::unique_ptr<ScanContext> context) {
256+
return Create(std::move(context), nullptr);
257+
}
258+
259+
Result<std::unique_ptr<TableScan>> TableScan::Create(
260+
std::unique_ptr<ScanContext> context,
261+
[[maybe_unused]] const std::shared_ptr<const Table>& table) {
256262
if (context == nullptr) {
257263
return Status::Invalid("scan context is null pointer");
258264
}

‎src/paimon/core/table/table.cpp‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@
3737

3838
namespace paimon {
3939

40+
namespace {
41+
42+
/// The table's shared owner, which a `NewScan()`, `NewRead()`, `NewWrite()` or `NewCommit()` call
43+
/// hands to the operator it creates. A table no `shared_ptr` holds has no owner to hand over, and
44+
/// `shared_from_this()` would throw rather than say so, so the failure is stood up as a status.
45+
Result<std::shared_ptr<const Table>> SharedOwnerOf(const Table* table) {
46+
if (std::shared_ptr<const Table> owner = table->weak_from_this().lock()) {
47+
return owner;
48+
}
49+
return Status::Invalid("table is not held by a shared_ptr");
50+
}
51+
52+
} // namespace
53+
4054
Result<std::shared_ptr<Table>> Table::Create(const std::shared_ptr<FileSystem>& file_system,
4155
const std::string& table_path,
4256
const Identifier& identifier) {
@@ -64,21 +78,25 @@ Result<std::shared_ptr<Table>> Table::Create(const std::shared_ptr<FileSystem>&
6478
}
6579

6680
Result<std::unique_ptr<TableScan>> Table::NewScan(std::unique_ptr<ScanContext> context) const {
67-
return TableScan::Create(std::move(context));
81+
PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<const Table> table, SharedOwnerOf(this));
82+
return TableScan::Create(std::move(context), table);
6883
}
6984

7085
Result<std::unique_ptr<TableRead>> Table::NewRead(std::unique_ptr<ReadContext> context) const {
71-
return TableRead::Create(std::move(context));
86+
PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<const Table> table, SharedOwnerOf(this));
87+
return TableRead::Create(std::move(context), table);
7288
}
7389

7490
Result<std::unique_ptr<FileStoreWrite>> Table::NewWrite(
7591
std::unique_ptr<WriteContext> context) const {
76-
return FileStoreWrite::Create(std::move(context));
92+
PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<const Table> table, SharedOwnerOf(this));
93+
return FileStoreWrite::Create(std::move(context), table);
7794
}
7895

7996
Result<std::unique_ptr<FileStoreCommit>> Table::NewCommit(
8097
std::unique_ptr<CommitContext> context) const {
81-
return FileStoreCommit::Create(std::move(context));
98+
PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<const Table> table, SharedOwnerOf(this));
99+
return FileStoreCommit::Create(std::move(context), table);
82100
}
83101

84102
} // namespace paimon

0 commit comments

Comments
 (0)