Skip to content

Commit 9f513f1

Browse files
authored
feat(api): Support specifying file system for more APIs (alibaba#78)
1 parent cd35fc1 commit 9f513f1

31 files changed

Lines changed: 324 additions & 147 deletions

include/paimon/catalog/catalog.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,13 @@ class PAIMON_EXPORT Catalog {
4646
///
4747
/// @param root_path Path to the root directory where the catalog is located.
4848
/// @param options Configuration options for catalog initialization.
49+
/// @param file_system Specifies the file system for file operations.
50+
/// If not set, use default file system (configured in
51+
/// `Options::FILE_SYSTEM`)
4952
/// @return A result containing a unique pointer to a `Catalog` instance, or an error status.
5053
static Result<std::unique_ptr<Catalog>> Create(
51-
const std::string& root_path, const std::map<std::string, std::string>& options);
54+
const std::string& root_path, const std::map<std::string, std::string>& options,
55+
const std::shared_ptr<FileSystem>& file_system = nullptr);
5256

5357
virtual ~Catalog() = default;
5458

@@ -124,6 +128,16 @@ class PAIMON_EXPORT Catalog {
124128
/// @return A string representing the expected location of the table.
125129
virtual std::string GetTableLocation(const Identifier& identifier) const = 0;
126130

131+
/// Returns the root path of the catalog.
132+
///
133+
/// @return A string representing the root path of the catalog.
134+
virtual std::string GetRootPath() const = 0;
135+
136+
/// Returns the file system used by the catalog.
137+
///
138+
/// @return A shared pointer to the file system instance.
139+
virtual std::shared_ptr<FileSystem> GetFileSystem() const = 0;
140+
127141
/// Loads the latest schema of a specified table.
128142
///
129143
/// @note System tables will not be supported.

include/paimon/commit_context.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class PAIMON_EXPORT CommitContext {
3939
bool ignore_empty_commit, bool use_rest_catalog_commit,
4040
const std::shared_ptr<MemoryPool>& memory_pool,
4141
const std::shared_ptr<Executor>& executor,
42+
const std::shared_ptr<FileSystem>& specific_file_system,
4243
const std::map<std::string, std::string>& options);
4344
~CommitContext();
4445

@@ -66,6 +67,10 @@ class PAIMON_EXPORT CommitContext {
6667
return executor_;
6768
}
6869

70+
std::shared_ptr<FileSystem> GetSpecificFileSystem() const {
71+
return specific_file_system_;
72+
}
73+
6974
const std::map<std::string, std::string>& GetOptions() const {
7075
return options_;
7176
}
@@ -77,6 +82,7 @@ class PAIMON_EXPORT CommitContext {
7782
bool use_rest_catalog_commit_;
7883
std::shared_ptr<MemoryPool> memory_pool_;
7984
std::shared_ptr<Executor> executor_;
85+
std::shared_ptr<FileSystem> specific_file_system_;
8086
std::map<std::string, std::string> options_;
8187
};
8288

@@ -129,6 +135,15 @@ class PAIMON_EXPORT CommitContextBuilder {
129135
/// @return Reference to this builder for method chaining.
130136
CommitContextBuilder& WithExecutor(const std::shared_ptr<Executor>& executor);
131137

138+
/// Sets a custom file system instance to be used for all file operations in this commit
139+
/// context.
140+
/// This bypasses the global file system registry and uses the provided implementation directly.
141+
///
142+
/// @param file_system The file system to use.
143+
/// @return Reference to this builder for method chaining.
144+
/// @note If not set, use default file system (configured in `Options::FILE_SYSTEM`)
145+
CommitContextBuilder& WithFileSystem(const std::shared_ptr<FileSystem>& file_system);
146+
132147
/// Build and return a `CommitContext` instance with input validation.
133148
/// @return Result containing the constructed `CommitContext` or an error status.
134149
Result<std::unique_ptr<CommitContext>> Finish();

include/paimon/global_index/global_index_write_task.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ class PAIMON_EXPORT GlobalIndexWriteTask {
4444
/// @param options Index-specific configuration (e.g., false positive rate for bloom
4545
/// filters).
4646
/// @param pool Memory pool for temporary allocations during index construction.
47-
// If `nullptr`, the system's default memory pool will be used.
47+
/// If `nullptr`, the system's default memory pool will be used.
48+
/// @param file_system Specifies the file system for file operations.
49+
/// If `nullptr`, use default file system.
4850
/// @return A `Result` containing a shared pointer to the `CommitMessage` with index metadata,
4951
/// or an error if indexing fails (e.g., unsupported type, I/O error).
5052
static Result<std::shared_ptr<CommitMessage>> WriteIndex(
5153
const std::string& table_path, const std::string& field_name, const std::string& index_type,
5254
const std::shared_ptr<IndexedSplit>& indexed_split,
53-
const std::map<std::string, std::string>& options, const std::shared_ptr<MemoryPool>& pool);
55+
const std::map<std::string, std::string>& options, const std::shared_ptr<MemoryPool>& pool,
56+
const std::shared_ptr<FileSystem>& file_system = nullptr);
5457
};
5558

5659
} // namespace paimon

include/paimon/migrate/file_meta_utils.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,15 @@ class PAIMON_EXPORT FileMetaUtils {
5656
/// tables. Use empty map for non-partitioned tables.
5757
/// @param options Set a configuration options map to set some option entries which are not
5858
/// defined in the table schema or whose values you want to overwrite.
59+
/// @param file_system Specifies the file system for file operations.
60+
/// If `nullptr`, use default file system.
5961
/// @return Result containing a unique pointer to the generated `CommitMessage`,
6062
/// or an error status if the migration cannot be performed.
6163
static Result<std::unique_ptr<CommitMessage>> GenerateCommitMessage(
6264
const std::vector<std::string>& src_data_files, const std::string& dst_table_path,
6365
const std::map<std::string, std::string>& partition_values,
64-
const std::map<std::string, std::string>& options);
66+
const std::map<std::string, std::string>& options,
67+
const std::shared_ptr<FileSystem>& file_system = nullptr);
6568
};
6669

6770
} // namespace paimon

include/paimon/orphan_files_cleaner.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class PAIMON_EXPORT CleanContext {
4141
CleanContext(const std::string& root_path, const std::map<std::string, std::string>& options,
4242
int64_t older_than_ms, const std::shared_ptr<MemoryPool>& pool,
4343
const std::shared_ptr<Executor>& executor,
44+
const std::shared_ptr<FileSystem>& specific_file_system,
4445
std::function<bool(const std::string&)> should_be_retained);
4546
~CleanContext();
4647

@@ -64,6 +65,10 @@ class PAIMON_EXPORT CleanContext {
6465
return executor_;
6566
}
6667

68+
std::shared_ptr<FileSystem> GetSpecificFileSystem() const {
69+
return specific_file_system_;
70+
}
71+
6772
std::function<bool(const std::string&)> GetFileRetainCondition() const {
6873
return should_be_retained_;
6974
}
@@ -74,6 +79,7 @@ class PAIMON_EXPORT CleanContext {
7479
int64_t older_than_ms_;
7580
std::shared_ptr<MemoryPool> memory_pool_;
7681
std::shared_ptr<Executor> executor_;
82+
std::shared_ptr<FileSystem> specific_file_system_;
7783
std::function<bool(const std::string&)> should_be_retained_;
7884
};
7985

@@ -124,6 +130,14 @@ class PAIMON_EXPORT CleanContextBuilder {
124130
/// @return Reference to this builder for method chaining.
125131
CleanContextBuilder& WithExecutor(const std::shared_ptr<Executor>& executor);
126132

133+
/// Sets a custom file system instance to be used for all file operations in this clean context.
134+
/// This bypasses the global file system registry and uses the provided implementation directly.
135+
///
136+
/// @param file_system The file system to use.
137+
/// @return Reference to this builder for method chaining.
138+
/// @note If not set, use default file system (configured in `Options::FILE_SYSTEM`)
139+
CleanContextBuilder& WithFileSystem(const std::shared_ptr<FileSystem>& file_system);
140+
127141
/// Build and return a `CleanContext` instance with input validation.
128142
/// @return Result containing the constructed `CleanContext` or an error status.
129143
Result<std::unique_ptr<CleanContext>> Finish();

include/paimon/read_context.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,10 @@ class PAIMON_EXPORT ReadContextBuilder {
334334

335335
/// Sets a custom file system instance to be used for all file operations in this read context.
336336
/// This bypasses the global file system registry and uses the provided implementation directly.
337+
///
338+
/// @param file_system The file system to use.
339+
/// @return Reference to this builder for method chaining.
340+
/// @note If not set, use default file system (configured in `Options::FILE_SYSTEM`)
337341
ReadContextBuilder& WithFileSystem(const std::shared_ptr<FileSystem>& file_system);
338342

339343
/// Build and return a `ReadContext` instance with input validation.

include/paimon/scan_context.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class PAIMON_EXPORT ScanContext {
4848
const std::shared_ptr<GlobalIndexResult>& global_index_result,
4949
const std::shared_ptr<MemoryPool>& memory_pool,
5050
const std::shared_ptr<Executor>& executor,
51+
const std::shared_ptr<FileSystem>& specific_file_system,
5152
const std::map<std::string, std::string>& options);
5253

5354
~ScanContext();
@@ -82,6 +83,10 @@ class PAIMON_EXPORT ScanContext {
8283
return global_index_result_;
8384
}
8485

86+
std::shared_ptr<FileSystem> GetSpecificFileSystem() const {
87+
return specific_file_system_;
88+
}
89+
8590
private:
8691
std::string path_;
8792
bool is_streaming_mode_;
@@ -90,6 +95,7 @@ class PAIMON_EXPORT ScanContext {
9095
std::shared_ptr<GlobalIndexResult> global_index_result_;
9196
std::shared_ptr<MemoryPool> memory_pool_;
9297
std::shared_ptr<Executor> executor_;
98+
std::shared_ptr<FileSystem> specific_file_system_;
9399
std::map<std::string, std::string> options_;
94100
};
95101

@@ -174,6 +180,12 @@ class PAIMON_EXPORT ScanContextBuilder {
174180
/// @return Reference to this builder for method chaining.
175181
/// @note if not set, executor is default executor
176182
ScanContextBuilder& WithExecutor(const std::shared_ptr<Executor>& executor);
183+
/// Sets a custom file system instance to be used for all file operations in this scan context.
184+
/// This bypasses the global file system registry and uses the provided implementation directly.
185+
/// @param file_system The file system to use.
186+
/// @return Reference to this builder for method chaining.
187+
/// @note If not set, use default file system (configured in `Options::FILE_SYSTEM`)
188+
ScanContextBuilder& WithFileSystem(const std::shared_ptr<FileSystem>& file_system);
177189

178190
/// Build and return a `ScanContext` instance with input validation.
179191
/// @return Result containing the constructed `ScanContext` or an error status.

include/paimon/write_context.h

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class PAIMON_EXPORT WriteContext {
4444
const std::vector<std::string>& write_schema,
4545
const std::shared_ptr<MemoryPool>& memory_pool,
4646
const std::shared_ptr<Executor>& executor,
47+
const std::shared_ptr<FileSystem>& specific_file_system,
4748
const std::map<std::string, std::string>& fs_scheme_to_identifier_map,
4849
const std::map<std::string, std::string>& options);
4950

@@ -97,6 +98,10 @@ class PAIMON_EXPORT WriteContext {
9798
return executor_;
9899
}
99100

101+
std::shared_ptr<FileSystem> GetSpecificFileSystem() const {
102+
return specific_file_system_;
103+
}
104+
100105
private:
101106
std::string root_path_;
102107
std::string commit_user_;
@@ -108,6 +113,7 @@ class PAIMON_EXPORT WriteContext {
108113
std::vector<std::string> write_schema_;
109114
std::shared_ptr<MemoryPool> memory_pool_;
110115
std::shared_ptr<Executor> executor_;
116+
std::shared_ptr<FileSystem> specific_file_system_;
111117
std::map<std::string, std::string> fs_scheme_to_identifier_map_;
112118
std::map<std::string, std::string> options_;
113119
};
@@ -175,12 +181,30 @@ class PAIMON_EXPORT WriteContextBuilder {
175181
/// @return Reference to this builder for method chaining.
176182
WriteContextBuilder& WithWriteSchema(const std::vector<std::string>& write_schema);
177183

178-
/// Set the file system scheme to identifier mapping for custom file system configurations.
179-
/// This allows using different file system implementations for different URI schemes.
184+
/// Sets a custom file system instance to be used for all file operations in this write context.
185+
/// This bypasses the global file system registry and uses the provided implementation directly.
186+
///
187+
/// @param file_system The file system to use.
188+
/// @return Reference to this builder for method chaining.
189+
/// @note If not set, use default file system (configured in `Options::FILE_SYSTEM`)
190+
WriteContextBuilder& WithFileSystem(const std::shared_ptr<FileSystem>& file_system);
191+
192+
/// Sets a mapping from URI schemes (e.g., "file", "oss") to registered file system
193+
/// identifiers. This allows selecting different pre-registered file system implementations
194+
/// based on the URI scheme at runtime.
180195
///
181-
/// @param fs_scheme_to_identifier_map Map from URI scheme to file system identifier.
196+
/// @param fs_scheme_to_identifier_map Map from URI scheme (like "oss") to the corresponding
197+
/// file system identifier.
182198
/// @return Reference to this builder for method chaining.
183-
/// @note If not set, use default file system (configured in `Options::FILE_SYSTEM`).
199+
/// @note
200+
/// - This method is intended for environments where multiple file systems are pre-registered.
201+
/// - The specified identifiers must correspond to file systems that have been registered at
202+
/// compile time or initialization.
203+
/// - Cannot be used together with `WithFileSystem()`.
204+
/// - If not set, use default file system (configured in `Options::FILE_SYSTEM`).
205+
/// Example:
206+
/// builder.WithFileSystemSchemeToIdentifierMap({{"oss", "jindo"}, {"file", "local"}});
207+
///
184208
WriteContextBuilder& WithFileSystemSchemeToIdentifierMap(
185209
const std::map<std::string, std::string>& fs_scheme_to_identifier_map);
186210

src/paimon/core/catalog/catalog.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ const char Catalog::SYSTEM_TABLE_SPLITTER[] = "$";
2828
const char Catalog::DB_SUFFIX[] = ".db";
2929
const char Catalog::DB_LOCATION_PROP[] = "location";
3030

31-
Result<std::unique_ptr<Catalog>> Catalog::Create(
32-
const std::string& root_path, const std::map<std::string, std::string>& options) {
33-
PAIMON_ASSIGN_OR_RAISE(CoreOptions core_options, CoreOptions::FromMap(options));
31+
Result<std::unique_ptr<Catalog>> Catalog::Create(const std::string& root_path,
32+
const std::map<std::string, std::string>& options,
33+
const std::shared_ptr<FileSystem>& file_system) {
34+
PAIMON_ASSIGN_OR_RAISE(CoreOptions core_options, CoreOptions::FromMap(options, file_system));
3435
return std::make_unique<FileSystemCatalog>(core_options.GetFileSystem(), root_path);
3536
}
3637

src/paimon/core/catalog/catalog_test.cpp

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

1919
#include "gtest/gtest.h"
2020
#include "paimon/defs.h"
21+
#include "paimon/testing/mock/mock_file_system.h"
2122
#include "paimon/testing/utils/testharness.h"
2223

2324
namespace paimon::test {
@@ -29,4 +30,13 @@ TEST(CatalogTest, Create) {
2930
ASSERT_OK_AND_ASSIGN(auto catalog, Catalog::Create("path", options));
3031
}
3132

33+
TEST(CatalogTest, CreateWithSpecificFileSystem) {
34+
std::map<std::string, std::string> options;
35+
const std::string path = "path";
36+
const auto fs = std::make_shared<MockFileSystem>();
37+
ASSERT_OK_AND_ASSIGN(auto catalog, Catalog::Create(path, options, fs));
38+
ASSERT_EQ(path, catalog->GetRootPath());
39+
ASSERT_EQ(fs, catalog->GetFileSystem());
40+
}
41+
3242
} // namespace paimon::test

0 commit comments

Comments
 (0)