Repository navigation
feat(catalog): add pluggable CatalogFactory #617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Joob1n
wants to merge
1
commit into
apache:main
Choose a base branch
from
Joob1n:feat/catalog-factory
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <map> | ||
| #include <memory> | ||
| #include <string> | ||
|
|
||
| #include "paimon/factories/factory.h" | ||
| #include "paimon/result.h" | ||
| #include "paimon/visibility.h" | ||
|
|
||
| namespace paimon { | ||
|
|
||
| class Catalog; | ||
| class FileSystem; | ||
|
|
||
| /// A factory for creating `Catalog` instances. | ||
| /// | ||
| /// Register an implementation with `REGISTER_PAIMON_FACTORY` so `Catalog::Create` can resolve a | ||
| /// custom `CatalogOptions::METASTORE` identifier through `FactoryCreator`. `Catalog::Create` | ||
| /// lowercases `CatalogOptions::METASTORE` before lookup, so custom factories should register | ||
| /// lowercase identifiers. | ||
| /// | ||
| /// @note All factories share one identifier space, so avoid colliding with built-in catalog, | ||
| /// file system, format, or index identifiers such as `"filesystem"`, `"rest"`, `"local"`, `"oss"`, | ||
| /// `"s3"`, or `"parquet"`. | ||
| class PAIMON_EXPORT CatalogFactory : public Factory { | ||
| public: | ||
| ~CatalogFactory() override = default; | ||
|
|
||
| /// Creates a `Catalog` instance from the configuration options and optional file system. | ||
| /// | ||
| /// @param root_path Path to the root directory where the catalog is located, or catalog | ||
| /// warehouse/instance identifier. | ||
| /// @param options Configuration options for catalog initialization. | ||
| /// @param file_system Optional caller-supplied file system for file operations. | ||
| /// @param fs_scheme_to_identifier_map Maps a URI scheme (like "oss") to the registered file | ||
| /// system identifier that serves it when `file_system` is nullptr. | ||
| /// @return A result containing a unique pointer to a `Catalog` instance, or an error status. | ||
| virtual Result<std::unique_ptr<Catalog>> Create( | ||
| const std::string& root_path, const std::map<std::string, std::string>& options, | ||
| const std::shared_ptr<FileSystem>& file_system = nullptr, | ||
| const std::map<std::string, std::string>& fs_scheme_to_identifier_map = {}) const = 0; | ||
|
|
||
| /// Gets a `Catalog` instance using the factory registered under `identifier`. | ||
| /// @pre Factory is already registered. | ||
| static Result<std::unique_ptr<Catalog>> Get( | ||
| const std::string& identifier, const std::string& root_path, | ||
| const std::map<std::string, std::string>& options, | ||
| const std::shared_ptr<FileSystem>& file_system = nullptr, | ||
| const std::map<std::string, std::string>& fs_scheme_to_identifier_map = {}); | ||
| }; | ||
|
|
||
| } // namespace paimon | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "paimon/catalog/catalog_factory.h" | ||
|
|
||
| #include <map> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <utility> | ||
|
|
||
| #include "fmt/format.h" | ||
| #include "paimon/catalog/catalog.h" | ||
| #include "paimon/core/catalog/file_system_catalog.h" | ||
| #include "paimon/core/core_options.h" | ||
| #include "paimon/factories/factory_creator.h" | ||
| #include "paimon/status.h" | ||
| #ifdef PAIMON_ENABLE_REST | ||
| #include "paimon/rest/rest_catalog.h" | ||
| #endif | ||
|
|
||
| namespace paimon { | ||
| namespace { | ||
|
|
||
| class FileSystemCatalogFactory : public CatalogFactory { | ||
| public: | ||
| static constexpr const char kIdentifier[] = "filesystem"; | ||
|
|
||
| const char* Identifier() const override { | ||
| return kIdentifier; | ||
| } | ||
|
|
||
| Result<std::unique_ptr<Catalog>> Create( | ||
| const std::string& root_path, const std::map<std::string, std::string>& options, | ||
| const std::shared_ptr<FileSystem>& file_system, | ||
| const std::map<std::string, std::string>& fs_scheme_to_identifier_map) const override { | ||
| PAIMON_ASSIGN_OR_RAISE( | ||
| CoreOptions core_options, | ||
| CoreOptions::FromMap(options, file_system, fs_scheme_to_identifier_map)); | ||
| return std::make_unique<FileSystemCatalog>(core_options.GetFileSystem(), root_path, | ||
| options); | ||
| } | ||
| }; | ||
|
|
||
| #ifdef PAIMON_ENABLE_REST | ||
| class RestCatalogFactory : public CatalogFactory { | ||
| public: | ||
| static constexpr const char kIdentifier[] = "rest"; | ||
|
|
||
| const char* Identifier() const override { | ||
| return kIdentifier; | ||
| } | ||
|
|
||
| Result<std::unique_ptr<Catalog>> Create( | ||
| const std::string& root_path, const std::map<std::string, std::string>& options, | ||
| const std::shared_ptr<FileSystem>& file_system, | ||
| const std::map<std::string, std::string>& fs_scheme_to_identifier_map) const override { | ||
| return RestCatalog::Create(root_path, options, file_system, RestHttpClient::Config(), | ||
| fs_scheme_to_identifier_map); | ||
| } | ||
| }; | ||
| #endif | ||
|
|
||
| } // namespace | ||
|
|
||
| Result<std::unique_ptr<Catalog>> CatalogFactory::Get( | ||
| const std::string& identifier, const std::string& root_path, | ||
| const std::map<std::string, std::string>& options, | ||
| const std::shared_ptr<FileSystem>& file_system, | ||
| const std::map<std::string, std::string>& fs_scheme_to_identifier_map) { | ||
| FactoryCreator* factory_creator = FactoryCreator::GetInstance(); | ||
| if (factory_creator == nullptr) { | ||
| return Status::Invalid("factory creator is null pointer"); | ||
| } | ||
| Factory* factory = factory_creator->Create(identifier); | ||
| if (factory == nullptr) { | ||
| #ifndef PAIMON_ENABLE_REST | ||
| if (identifier == "rest") { | ||
| return Status::NotImplemented( | ||
| "the rest catalog requires building paimon with PAIMON_ENABLE_REST=ON"); | ||
| } | ||
| #endif | ||
| return Status::Invalid(fmt::format( | ||
| "unsupported metastore: '{}' (catalog factory not registered)", identifier)); | ||
| } | ||
| auto* catalog_factory = dynamic_cast<CatalogFactory*>(factory); | ||
| if (catalog_factory == nullptr) { | ||
| return Status::Invalid( | ||
| fmt::format("Failed to cast catalog factory with identifier '{}'.", identifier)); | ||
| } | ||
| PAIMON_ASSIGN_OR_RAISE( | ||
| std::unique_ptr<Catalog> catalog, | ||
| catalog_factory->Create(root_path, options, file_system, fs_scheme_to_identifier_map)); | ||
| if (catalog == nullptr) { | ||
| return Status::Invalid( | ||
| fmt::format("Catalog factory '{}' created a null catalog.", identifier)); | ||
| } | ||
| return catalog; | ||
| } | ||
|
|
||
| // Keep built-in registration with the lookup entry point for static linking. | ||
| REGISTER_PAIMON_FACTORY(FileSystemCatalogFactory); | ||
| #ifdef PAIMON_ENABLE_REST | ||
| REGISTER_PAIMON_FACTORY(RestCatalogFactory); | ||
| #endif | ||
|
|
||
| } // namespace paimon |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a suggestion: if it doesn’t affect interface compatibility, could we remove the default parameters?