Jinchul81 commented on code in PR #296: URL: https://github.com/apache/iceberg-cpp/pull/296#discussion_r2554132979
########## src/iceberg/catalog/rest/catalog.cc: ########## @@ -0,0 +1,200 @@ +/* + * 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 "iceberg/catalog/rest/catalog.h" + +#include <memory> +#include <utility> + +#include <cpr/cpr.h> + +#include "iceberg/catalog/rest/config.h" +#include "iceberg/catalog/rest/constant.h" +#include "iceberg/catalog/rest/endpoint_util.h" +#include "iceberg/catalog/rest/http_client_interal.h" +#include "iceberg/catalog/rest/json_internal.h" +#include "iceberg/catalog/rest/types.h" +#include "iceberg/json_internal.h" +#include "iceberg/result.h" +#include "iceberg/table.h" +#include "iceberg/util/macros.h" + +namespace iceberg::rest { + +Result<std::unique_ptr<RestCatalog>> RestCatalog::Make(const RestCatalogConfig& config) { + // Create ResourcePaths and validate URI + ICEBERG_ASSIGN_OR_RAISE(auto paths, ResourcePaths::Make(config)); + + ICEBERG_ASSIGN_OR_RAISE(auto tmp_client, HttpClient::Make(config)); + + const std::string endpoint = paths->V1Config(); + cpr::Parameters params; + ICEBERG_ASSIGN_OR_RAISE(const auto& response, tmp_client->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto server_config, CatalogConfigFromJson(json)); + // Merge server config into client config, server config overrides > client config + // properties > server config defaults + auto final_props = std::move(server_config.defaults); + for (const auto& kv : config.configs()) { + final_props.insert_or_assign(kv.first, kv.second); + } + + for (const auto& kv : server_config.overrides) { + final_props.insert_or_assign(kv.first, kv.second); + } + auto final_config = RestCatalogConfig::FromMap(final_props); + ICEBERG_ASSIGN_OR_RAISE(auto client, HttpClient::Make(*final_config)); + ICEBERG_ASSIGN_OR_RAISE(auto final_paths, ResourcePaths::Make(*final_config)); + return std::unique_ptr<RestCatalog>(new RestCatalog( + std::move(final_config), std::move(client), std::move(*final_paths))); + }; + default: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +RestCatalog::RestCatalog(std::unique_ptr<RestCatalogConfig> config, + std::unique_ptr<HttpClient> client, ResourcePaths paths) + : config_(std::move(config)), client_(std::move(client)), paths_(std::move(paths)) {} + +std::string_view RestCatalog::name() const { + auto it = config_->configs().find(std::string(RestCatalogConfig::kName)); + if (it == config_->configs().end() || it->second.empty()) { + return {""}; + } + return std::string_view(it->second); +} + +Result<std::vector<Namespace>> RestCatalog::ListNamespaces(const Namespace& ns) const { + const std::string endpoint = paths_.V1Namespaces(); + std::vector<Namespace> result; + std::string next_token; + while (true) { + cpr::Parameters params; + if (!ns.levels.empty()) { + params.Add({std::string(kQueryParamParent), EncodeNamespaceForUrl(ns)}); + } + if (!next_token.empty()) { + params.Add({std::string(kQueryParamPageToken), next_token}); + } + ICEBERG_ASSIGN_OR_RAISE(const auto& response, client_->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ListNamespacesResponseFromJson(json)); + result.insert(result.end(), list_response.namespaces.begin(), + list_response.namespaces.end()); + if (list_response.next_page_token.empty()) { + return result; + } + next_token = list_response.next_page_token; + continue; Review Comment: I am not sure the code always exit the infinite loop. Eventually `list_response.next_page_token` will return empty? ########## src/iceberg/catalog/rest/catalog.cc: ########## @@ -0,0 +1,200 @@ +/* + * 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 "iceberg/catalog/rest/catalog.h" + +#include <memory> +#include <utility> + +#include <cpr/cpr.h> + +#include "iceberg/catalog/rest/config.h" +#include "iceberg/catalog/rest/constant.h" +#include "iceberg/catalog/rest/endpoint_util.h" +#include "iceberg/catalog/rest/http_client_interal.h" +#include "iceberg/catalog/rest/json_internal.h" +#include "iceberg/catalog/rest/types.h" +#include "iceberg/json_internal.h" +#include "iceberg/result.h" +#include "iceberg/table.h" +#include "iceberg/util/macros.h" + +namespace iceberg::rest { + +Result<std::unique_ptr<RestCatalog>> RestCatalog::Make(const RestCatalogConfig& config) { + // Create ResourcePaths and validate URI + ICEBERG_ASSIGN_OR_RAISE(auto paths, ResourcePaths::Make(config)); + + ICEBERG_ASSIGN_OR_RAISE(auto tmp_client, HttpClient::Make(config)); + + const std::string endpoint = paths->V1Config(); + cpr::Parameters params; + ICEBERG_ASSIGN_OR_RAISE(const auto& response, tmp_client->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto server_config, CatalogConfigFromJson(json)); + // Merge server config into client config, server config overrides > client config + // properties > server config defaults + auto final_props = std::move(server_config.defaults); + for (const auto& kv : config.configs()) { + final_props.insert_or_assign(kv.first, kv.second); + } + + for (const auto& kv : server_config.overrides) { + final_props.insert_or_assign(kv.first, kv.second); + } + auto final_config = RestCatalogConfig::FromMap(final_props); + ICEBERG_ASSIGN_OR_RAISE(auto client, HttpClient::Make(*final_config)); + ICEBERG_ASSIGN_OR_RAISE(auto final_paths, ResourcePaths::Make(*final_config)); + return std::unique_ptr<RestCatalog>(new RestCatalog( + std::move(final_config), std::move(client), std::move(*final_paths))); + }; + default: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +RestCatalog::RestCatalog(std::unique_ptr<RestCatalogConfig> config, + std::unique_ptr<HttpClient> client, ResourcePaths paths) + : config_(std::move(config)), client_(std::move(client)), paths_(std::move(paths)) {} + +std::string_view RestCatalog::name() const { + auto it = config_->configs().find(std::string(RestCatalogConfig::kName)); + if (it == config_->configs().end() || it->second.empty()) { + return {""}; + } + return std::string_view(it->second); +} + +Result<std::vector<Namespace>> RestCatalog::ListNamespaces(const Namespace& ns) const { + const std::string endpoint = paths_.V1Namespaces(); + std::vector<Namespace> result; + std::string next_token; + while (true) { + cpr::Parameters params; + if (!ns.levels.empty()) { + params.Add({std::string(kQueryParamParent), EncodeNamespaceForUrl(ns)}); + } + if (!next_token.empty()) { + params.Add({std::string(kQueryParamPageToken), next_token}); + } + ICEBERG_ASSIGN_OR_RAISE(const auto& response, client_->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ListNamespacesResponseFromJson(json)); + result.insert(result.end(), list_response.namespaces.begin(), + list_response.namespaces.end()); + if (list_response.next_page_token.empty()) { + return result; + } + next_token = list_response.next_page_token; + continue; + } + case cpr::status::HTTP_NOT_FOUND: { + return NoSuchNamespace("Namespace not found"); + } + default: + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +Status RestCatalog::CreateNamespace( + const Namespace& ns, const std::unordered_map<std::string, std::string>& properties) { Review Comment: If the parameters are currently used in the function, remove the names or add `[[maybe_unused]]`. ``` Status RestCatalog::CreateNamespace( const Namespace&, const std::unordered_map<std::string, std::string>&) ``` Or ``` Status RestCatalog::CreateNamespace( [[maybe_unused]] const Namespace&, [[maybe_unused]] const std::unordered_map<std::string, std::string>&) ``` ########## src/iceberg/catalog/rest/catalog.cc: ########## @@ -0,0 +1,200 @@ +/* + * 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 "iceberg/catalog/rest/catalog.h" + +#include <memory> +#include <utility> + +#include <cpr/cpr.h> + +#include "iceberg/catalog/rest/config.h" +#include "iceberg/catalog/rest/constant.h" +#include "iceberg/catalog/rest/endpoint_util.h" +#include "iceberg/catalog/rest/http_client_interal.h" +#include "iceberg/catalog/rest/json_internal.h" +#include "iceberg/catalog/rest/types.h" +#include "iceberg/json_internal.h" +#include "iceberg/result.h" +#include "iceberg/table.h" +#include "iceberg/util/macros.h" + +namespace iceberg::rest { + +Result<std::unique_ptr<RestCatalog>> RestCatalog::Make(const RestCatalogConfig& config) { + // Create ResourcePaths and validate URI + ICEBERG_ASSIGN_OR_RAISE(auto paths, ResourcePaths::Make(config)); + + ICEBERG_ASSIGN_OR_RAISE(auto tmp_client, HttpClient::Make(config)); + + const std::string endpoint = paths->V1Config(); + cpr::Parameters params; + ICEBERG_ASSIGN_OR_RAISE(const auto& response, tmp_client->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto server_config, CatalogConfigFromJson(json)); + // Merge server config into client config, server config overrides > client config + // properties > server config defaults + auto final_props = std::move(server_config.defaults); + for (const auto& kv : config.configs()) { + final_props.insert_or_assign(kv.first, kv.second); + } + + for (const auto& kv : server_config.overrides) { + final_props.insert_or_assign(kv.first, kv.second); + } + auto final_config = RestCatalogConfig::FromMap(final_props); + ICEBERG_ASSIGN_OR_RAISE(auto client, HttpClient::Make(*final_config)); + ICEBERG_ASSIGN_OR_RAISE(auto final_paths, ResourcePaths::Make(*final_config)); + return std::unique_ptr<RestCatalog>(new RestCatalog( + std::move(final_config), std::move(client), std::move(*final_paths))); + }; + default: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +RestCatalog::RestCatalog(std::unique_ptr<RestCatalogConfig> config, + std::unique_ptr<HttpClient> client, ResourcePaths paths) + : config_(std::move(config)), client_(std::move(client)), paths_(std::move(paths)) {} + +std::string_view RestCatalog::name() const { + auto it = config_->configs().find(std::string(RestCatalogConfig::kName)); + if (it == config_->configs().end() || it->second.empty()) { + return {""}; + } + return std::string_view(it->second); +} + +Result<std::vector<Namespace>> RestCatalog::ListNamespaces(const Namespace& ns) const { + const std::string endpoint = paths_.V1Namespaces(); + std::vector<Namespace> result; + std::string next_token; + while (true) { + cpr::Parameters params; + if (!ns.levels.empty()) { + params.Add({std::string(kQueryParamParent), EncodeNamespaceForUrl(ns)}); + } + if (!next_token.empty()) { + params.Add({std::string(kQueryParamPageToken), next_token}); + } + ICEBERG_ASSIGN_OR_RAISE(const auto& response, client_->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ListNamespacesResponseFromJson(json)); + result.insert(result.end(), list_response.namespaces.begin(), + list_response.namespaces.end()); + if (list_response.next_page_token.empty()) { + return result; + } + next_token = list_response.next_page_token; + continue; + } + case cpr::status::HTTP_NOT_FOUND: { + return NoSuchNamespace("Namespace not found"); + } + default: + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +Status RestCatalog::CreateNamespace( + const Namespace& ns, const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unordered_map<std::string, std::string>> RestCatalog::GetNamespaceProperties( + const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::DropNamespace(const Namespace& ns) { + return NotImplemented("Not implemented"); +} + +Result<bool> RestCatalog::NamespaceExists(const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::UpdateNamespaceProperties( + const Namespace& ns, const std::unordered_map<std::string, std::string>& updates, + const std::unordered_set<std::string>& removals) { + return NotImplemented("Not implemented"); +} + +Result<std::vector<TableIdentifier>> RestCatalog::ListTables(const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Result<std::unique_ptr<Table>> RestCatalog::CreateTable( + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, + const std::string& location, + const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unique_ptr<Table>> RestCatalog::UpdateTable( + const TableIdentifier& identifier, + const std::vector<std::unique_ptr<TableRequirement>>& requirements, + const std::vector<std::unique_ptr<TableUpdate>>& updates) { + return NotImplemented("Not implemented"); +} + +Result<std::shared_ptr<Transaction>> RestCatalog::StageCreateTable( + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, + const std::string& location, + const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::DropTable(const TableIdentifier& identifier, bool purge) { + return NotImplemented("Not implemented"); +} + +Result<bool> RestCatalog::TableExists(const TableIdentifier& identifier) const { Review Comment: Remove the names or add `[[maybe_unused]]`. ########## src/iceberg/catalog/rest/catalog.cc: ########## @@ -0,0 +1,200 @@ +/* + * 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 "iceberg/catalog/rest/catalog.h" + +#include <memory> +#include <utility> + +#include <cpr/cpr.h> + +#include "iceberg/catalog/rest/config.h" +#include "iceberg/catalog/rest/constant.h" +#include "iceberg/catalog/rest/endpoint_util.h" +#include "iceberg/catalog/rest/http_client_interal.h" +#include "iceberg/catalog/rest/json_internal.h" +#include "iceberg/catalog/rest/types.h" +#include "iceberg/json_internal.h" +#include "iceberg/result.h" +#include "iceberg/table.h" +#include "iceberg/util/macros.h" + +namespace iceberg::rest { + +Result<std::unique_ptr<RestCatalog>> RestCatalog::Make(const RestCatalogConfig& config) { + // Create ResourcePaths and validate URI + ICEBERG_ASSIGN_OR_RAISE(auto paths, ResourcePaths::Make(config)); + + ICEBERG_ASSIGN_OR_RAISE(auto tmp_client, HttpClient::Make(config)); + + const std::string endpoint = paths->V1Config(); + cpr::Parameters params; + ICEBERG_ASSIGN_OR_RAISE(const auto& response, tmp_client->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto server_config, CatalogConfigFromJson(json)); + // Merge server config into client config, server config overrides > client config + // properties > server config defaults + auto final_props = std::move(server_config.defaults); + for (const auto& kv : config.configs()) { + final_props.insert_or_assign(kv.first, kv.second); + } + + for (const auto& kv : server_config.overrides) { + final_props.insert_or_assign(kv.first, kv.second); + } + auto final_config = RestCatalogConfig::FromMap(final_props); + ICEBERG_ASSIGN_OR_RAISE(auto client, HttpClient::Make(*final_config)); + ICEBERG_ASSIGN_OR_RAISE(auto final_paths, ResourcePaths::Make(*final_config)); + return std::unique_ptr<RestCatalog>(new RestCatalog( + std::move(final_config), std::move(client), std::move(*final_paths))); + }; + default: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +RestCatalog::RestCatalog(std::unique_ptr<RestCatalogConfig> config, + std::unique_ptr<HttpClient> client, ResourcePaths paths) + : config_(std::move(config)), client_(std::move(client)), paths_(std::move(paths)) {} + +std::string_view RestCatalog::name() const { + auto it = config_->configs().find(std::string(RestCatalogConfig::kName)); + if (it == config_->configs().end() || it->second.empty()) { + return {""}; + } + return std::string_view(it->second); +} + +Result<std::vector<Namespace>> RestCatalog::ListNamespaces(const Namespace& ns) const { + const std::string endpoint = paths_.V1Namespaces(); + std::vector<Namespace> result; + std::string next_token; + while (true) { + cpr::Parameters params; + if (!ns.levels.empty()) { + params.Add({std::string(kQueryParamParent), EncodeNamespaceForUrl(ns)}); + } + if (!next_token.empty()) { + params.Add({std::string(kQueryParamPageToken), next_token}); + } + ICEBERG_ASSIGN_OR_RAISE(const auto& response, client_->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ListNamespacesResponseFromJson(json)); + result.insert(result.end(), list_response.namespaces.begin(), + list_response.namespaces.end()); + if (list_response.next_page_token.empty()) { + return result; + } + next_token = list_response.next_page_token; + continue; + } + case cpr::status::HTTP_NOT_FOUND: { + return NoSuchNamespace("Namespace not found"); + } + default: + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +Status RestCatalog::CreateNamespace( + const Namespace& ns, const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unordered_map<std::string, std::string>> RestCatalog::GetNamespaceProperties( + const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::DropNamespace(const Namespace& ns) { + return NotImplemented("Not implemented"); +} + +Result<bool> RestCatalog::NamespaceExists(const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::UpdateNamespaceProperties( + const Namespace& ns, const std::unordered_map<std::string, std::string>& updates, + const std::unordered_set<std::string>& removals) { + return NotImplemented("Not implemented"); +} + +Result<std::vector<TableIdentifier>> RestCatalog::ListTables(const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Result<std::unique_ptr<Table>> RestCatalog::CreateTable( + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, + const std::string& location, + const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unique_ptr<Table>> RestCatalog::UpdateTable( + const TableIdentifier& identifier, + const std::vector<std::unique_ptr<TableRequirement>>& requirements, + const std::vector<std::unique_ptr<TableUpdate>>& updates) { + return NotImplemented("Not implemented"); +} + +Result<std::shared_ptr<Transaction>> RestCatalog::StageCreateTable( + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, + const std::string& location, + const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::DropTable(const TableIdentifier& identifier, bool purge) { + return NotImplemented("Not implemented"); +} + +Result<bool> RestCatalog::TableExists(const TableIdentifier& identifier) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::RenameTable(const TableIdentifier& from, const TableIdentifier& to) { + return NotImplemented("Not implemented"); +} + +Result<std::unique_ptr<Table>> RestCatalog::LoadTable(const TableIdentifier& identifier) { + return NotImplemented("Not implemented"); +} + +Result<std::shared_ptr<Table>> RestCatalog::RegisterTable( + const TableIdentifier& identifier, const std::string& metadata_file_location) { Review Comment: Remove the names or add `[[maybe_unused]]`. ########## src/iceberg/catalog/rest/catalog.h: ########## @@ -0,0 +1,110 @@ +/* + * 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 <memory> +#include <string> + +#include "iceberg/catalog.h" +#include "iceberg/catalog/rest/config.h" +#include "iceberg/catalog/rest/http_client.h" +#include "iceberg/catalog/rest/iceberg_rest_export.h" +#include "iceberg/catalog/rest/resource_paths.h" +#include "iceberg/result.h" + +/// \file iceberg/catalog/rest/catalog.h +/// RestCatalog implementation for Iceberg REST API. + +namespace iceberg::rest { + +class ICEBERG_REST_EXPORT RestCatalog : public Catalog { + public: + RestCatalog(const RestCatalog&) = delete; + RestCatalog& operator=(const RestCatalog&) = delete; + RestCatalog(RestCatalog&&) = delete; + RestCatalog& operator=(RestCatalog&&) = delete; + + /// \brief Create a RestCatalog instance + /// + /// \param config the configuration for the RestCatalog + /// \return a unique_ptr to RestCatalog instance + static Result<std::unique_ptr<RestCatalog>> Make(const RestCatalogConfig& config); + + std::string_view name() const override; + + Result<std::vector<Namespace>> ListNamespaces(const Namespace& ns) const override; + + Status CreateNamespace( + const Namespace& ns, + const std::unordered_map<std::string, std::string>& properties) override; + + Result<std::unordered_map<std::string, std::string>> GetNamespaceProperties( + const Namespace& ns) const override; + + Status DropNamespace(const Namespace& ns) override; + + Result<bool> NamespaceExists(const Namespace& ns) const override; + + Status UpdateNamespaceProperties( + const Namespace& ns, const std::unordered_map<std::string, std::string>& updates, + const std::unordered_set<std::string>& removals) override; + + Result<std::vector<TableIdentifier>> ListTables(const Namespace& ns) const override; + + Result<std::unique_ptr<Table>> CreateTable( + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, + const std::string& location, + const std::unordered_map<std::string, std::string>& properties) override; + + Result<std::unique_ptr<Table>> UpdateTable( + const TableIdentifier& identifier, + const std::vector<std::unique_ptr<TableRequirement>>& requirements, + const std::vector<std::unique_ptr<TableUpdate>>& updates) override; + + Result<std::shared_ptr<Transaction>> StageCreateTable( + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, + const std::string& location, + const std::unordered_map<std::string, std::string>& properties) override; + + Result<bool> TableExists(const TableIdentifier& identifier) const override; + + Status RenameTable(const TableIdentifier& from, const TableIdentifier& to) override; + + Status DropTable(const TableIdentifier& identifier, bool purge) override; + + Result<std::unique_ptr<Table>> LoadTable(const TableIdentifier& identifier) override; + + Result<std::shared_ptr<Table>> RegisterTable( + const TableIdentifier& identifier, + const std::string& metadata_file_location) override; + + std::unique_ptr<RestCatalog::TableBuilder> BuildTable( + const TableIdentifier& identifier, const Schema& schema) const override; + + private: + RestCatalog(std::unique_ptr<RestCatalogConfig> config, + std::unique_ptr<HttpClient> client, ResourcePaths paths); Review Comment: @wgtmac 1. Is there a way to pass the ownership if we still use `unique_ptr` instead of `shared_ptr`? ########## src/iceberg/catalog/rest/catalog.cc: ########## @@ -0,0 +1,200 @@ +/* + * 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 "iceberg/catalog/rest/catalog.h" + +#include <memory> +#include <utility> + +#include <cpr/cpr.h> + +#include "iceberg/catalog/rest/config.h" +#include "iceberg/catalog/rest/constant.h" +#include "iceberg/catalog/rest/endpoint_util.h" +#include "iceberg/catalog/rest/http_client_interal.h" +#include "iceberg/catalog/rest/json_internal.h" +#include "iceberg/catalog/rest/types.h" +#include "iceberg/json_internal.h" +#include "iceberg/result.h" +#include "iceberg/table.h" +#include "iceberg/util/macros.h" + +namespace iceberg::rest { + +Result<std::unique_ptr<RestCatalog>> RestCatalog::Make(const RestCatalogConfig& config) { + // Create ResourcePaths and validate URI + ICEBERG_ASSIGN_OR_RAISE(auto paths, ResourcePaths::Make(config)); + + ICEBERG_ASSIGN_OR_RAISE(auto tmp_client, HttpClient::Make(config)); + + const std::string endpoint = paths->V1Config(); + cpr::Parameters params; + ICEBERG_ASSIGN_OR_RAISE(const auto& response, tmp_client->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto server_config, CatalogConfigFromJson(json)); + // Merge server config into client config, server config overrides > client config + // properties > server config defaults + auto final_props = std::move(server_config.defaults); + for (const auto& kv : config.configs()) { + final_props.insert_or_assign(kv.first, kv.second); + } + + for (const auto& kv : server_config.overrides) { + final_props.insert_or_assign(kv.first, kv.second); + } + auto final_config = RestCatalogConfig::FromMap(final_props); + ICEBERG_ASSIGN_OR_RAISE(auto client, HttpClient::Make(*final_config)); + ICEBERG_ASSIGN_OR_RAISE(auto final_paths, ResourcePaths::Make(*final_config)); + return std::unique_ptr<RestCatalog>(new RestCatalog( + std::move(final_config), std::move(client), std::move(*final_paths))); + }; + default: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +RestCatalog::RestCatalog(std::unique_ptr<RestCatalogConfig> config, + std::unique_ptr<HttpClient> client, ResourcePaths paths) + : config_(std::move(config)), client_(std::move(client)), paths_(std::move(paths)) {} + +std::string_view RestCatalog::name() const { + auto it = config_->configs().find(std::string(RestCatalogConfig::kName)); + if (it == config_->configs().end() || it->second.empty()) { + return {""}; + } + return std::string_view(it->second); +} + +Result<std::vector<Namespace>> RestCatalog::ListNamespaces(const Namespace& ns) const { + const std::string endpoint = paths_.V1Namespaces(); + std::vector<Namespace> result; + std::string next_token; + while (true) { + cpr::Parameters params; + if (!ns.levels.empty()) { + params.Add({std::string(kQueryParamParent), EncodeNamespaceForUrl(ns)}); + } + if (!next_token.empty()) { + params.Add({std::string(kQueryParamPageToken), next_token}); + } + ICEBERG_ASSIGN_OR_RAISE(const auto& response, client_->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ListNamespacesResponseFromJson(json)); + result.insert(result.end(), list_response.namespaces.begin(), + list_response.namespaces.end()); + if (list_response.next_page_token.empty()) { + return result; + } + next_token = list_response.next_page_token; + continue; + } + case cpr::status::HTTP_NOT_FOUND: { + return NoSuchNamespace("Namespace not found"); + } + default: + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +Status RestCatalog::CreateNamespace( + const Namespace& ns, const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unordered_map<std::string, std::string>> RestCatalog::GetNamespaceProperties( + const Namespace& ns) const { Review Comment: Remove the names or add `[[maybe_unused]]`. ########## src/iceberg/catalog/rest/catalog.cc: ########## @@ -0,0 +1,200 @@ +/* + * 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 "iceberg/catalog/rest/catalog.h" + +#include <memory> +#include <utility> + +#include <cpr/cpr.h> + +#include "iceberg/catalog/rest/config.h" +#include "iceberg/catalog/rest/constant.h" +#include "iceberg/catalog/rest/endpoint_util.h" +#include "iceberg/catalog/rest/http_client_interal.h" +#include "iceberg/catalog/rest/json_internal.h" +#include "iceberg/catalog/rest/types.h" +#include "iceberg/json_internal.h" +#include "iceberg/result.h" +#include "iceberg/table.h" +#include "iceberg/util/macros.h" + +namespace iceberg::rest { + +Result<std::unique_ptr<RestCatalog>> RestCatalog::Make(const RestCatalogConfig& config) { + // Create ResourcePaths and validate URI + ICEBERG_ASSIGN_OR_RAISE(auto paths, ResourcePaths::Make(config)); + + ICEBERG_ASSIGN_OR_RAISE(auto tmp_client, HttpClient::Make(config)); + + const std::string endpoint = paths->V1Config(); + cpr::Parameters params; + ICEBERG_ASSIGN_OR_RAISE(const auto& response, tmp_client->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto server_config, CatalogConfigFromJson(json)); + // Merge server config into client config, server config overrides > client config + // properties > server config defaults + auto final_props = std::move(server_config.defaults); + for (const auto& kv : config.configs()) { + final_props.insert_or_assign(kv.first, kv.second); + } + + for (const auto& kv : server_config.overrides) { + final_props.insert_or_assign(kv.first, kv.second); + } + auto final_config = RestCatalogConfig::FromMap(final_props); + ICEBERG_ASSIGN_OR_RAISE(auto client, HttpClient::Make(*final_config)); + ICEBERG_ASSIGN_OR_RAISE(auto final_paths, ResourcePaths::Make(*final_config)); + return std::unique_ptr<RestCatalog>(new RestCatalog( + std::move(final_config), std::move(client), std::move(*final_paths))); + }; + default: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +RestCatalog::RestCatalog(std::unique_ptr<RestCatalogConfig> config, + std::unique_ptr<HttpClient> client, ResourcePaths paths) + : config_(std::move(config)), client_(std::move(client)), paths_(std::move(paths)) {} + +std::string_view RestCatalog::name() const { + auto it = config_->configs().find(std::string(RestCatalogConfig::kName)); + if (it == config_->configs().end() || it->second.empty()) { + return {""}; + } + return std::string_view(it->second); +} + +Result<std::vector<Namespace>> RestCatalog::ListNamespaces(const Namespace& ns) const { + const std::string endpoint = paths_.V1Namespaces(); + std::vector<Namespace> result; + std::string next_token; + while (true) { + cpr::Parameters params; + if (!ns.levels.empty()) { + params.Add({std::string(kQueryParamParent), EncodeNamespaceForUrl(ns)}); + } + if (!next_token.empty()) { + params.Add({std::string(kQueryParamPageToken), next_token}); + } + ICEBERG_ASSIGN_OR_RAISE(const auto& response, client_->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ListNamespacesResponseFromJson(json)); + result.insert(result.end(), list_response.namespaces.begin(), + list_response.namespaces.end()); + if (list_response.next_page_token.empty()) { + return result; + } + next_token = list_response.next_page_token; + continue; + } + case cpr::status::HTTP_NOT_FOUND: { + return NoSuchNamespace("Namespace not found"); + } + default: + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +Status RestCatalog::CreateNamespace( + const Namespace& ns, const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unordered_map<std::string, std::string>> RestCatalog::GetNamespaceProperties( + const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::DropNamespace(const Namespace& ns) { + return NotImplemented("Not implemented"); +} + +Result<bool> RestCatalog::NamespaceExists(const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::UpdateNamespaceProperties( + const Namespace& ns, const std::unordered_map<std::string, std::string>& updates, + const std::unordered_set<std::string>& removals) { Review Comment: Remove the names or add `[[maybe_unused]]`. ########## src/iceberg/catalog/rest/catalog.cc: ########## @@ -0,0 +1,200 @@ +/* + * 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 "iceberg/catalog/rest/catalog.h" + +#include <memory> +#include <utility> + +#include <cpr/cpr.h> + +#include "iceberg/catalog/rest/config.h" +#include "iceberg/catalog/rest/constant.h" +#include "iceberg/catalog/rest/endpoint_util.h" +#include "iceberg/catalog/rest/http_client_interal.h" +#include "iceberg/catalog/rest/json_internal.h" +#include "iceberg/catalog/rest/types.h" +#include "iceberg/json_internal.h" +#include "iceberg/result.h" +#include "iceberg/table.h" +#include "iceberg/util/macros.h" + +namespace iceberg::rest { + +Result<std::unique_ptr<RestCatalog>> RestCatalog::Make(const RestCatalogConfig& config) { + // Create ResourcePaths and validate URI + ICEBERG_ASSIGN_OR_RAISE(auto paths, ResourcePaths::Make(config)); + + ICEBERG_ASSIGN_OR_RAISE(auto tmp_client, HttpClient::Make(config)); + + const std::string endpoint = paths->V1Config(); + cpr::Parameters params; + ICEBERG_ASSIGN_OR_RAISE(const auto& response, tmp_client->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto server_config, CatalogConfigFromJson(json)); + // Merge server config into client config, server config overrides > client config + // properties > server config defaults + auto final_props = std::move(server_config.defaults); + for (const auto& kv : config.configs()) { + final_props.insert_or_assign(kv.first, kv.second); + } + + for (const auto& kv : server_config.overrides) { + final_props.insert_or_assign(kv.first, kv.second); + } + auto final_config = RestCatalogConfig::FromMap(final_props); + ICEBERG_ASSIGN_OR_RAISE(auto client, HttpClient::Make(*final_config)); + ICEBERG_ASSIGN_OR_RAISE(auto final_paths, ResourcePaths::Make(*final_config)); + return std::unique_ptr<RestCatalog>(new RestCatalog( + std::move(final_config), std::move(client), std::move(*final_paths))); + }; + default: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +RestCatalog::RestCatalog(std::unique_ptr<RestCatalogConfig> config, + std::unique_ptr<HttpClient> client, ResourcePaths paths) + : config_(std::move(config)), client_(std::move(client)), paths_(std::move(paths)) {} + +std::string_view RestCatalog::name() const { Review Comment: Add `constexpr` to the return type. ########## src/iceberg/catalog/rest/catalog.cc: ########## @@ -0,0 +1,200 @@ +/* + * 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 "iceberg/catalog/rest/catalog.h" + +#include <memory> +#include <utility> + +#include <cpr/cpr.h> + +#include "iceberg/catalog/rest/config.h" +#include "iceberg/catalog/rest/constant.h" +#include "iceberg/catalog/rest/endpoint_util.h" +#include "iceberg/catalog/rest/http_client_interal.h" +#include "iceberg/catalog/rest/json_internal.h" +#include "iceberg/catalog/rest/types.h" +#include "iceberg/json_internal.h" +#include "iceberg/result.h" +#include "iceberg/table.h" +#include "iceberg/util/macros.h" + +namespace iceberg::rest { + +Result<std::unique_ptr<RestCatalog>> RestCatalog::Make(const RestCatalogConfig& config) { + // Create ResourcePaths and validate URI + ICEBERG_ASSIGN_OR_RAISE(auto paths, ResourcePaths::Make(config)); + + ICEBERG_ASSIGN_OR_RAISE(auto tmp_client, HttpClient::Make(config)); + + const std::string endpoint = paths->V1Config(); + cpr::Parameters params; + ICEBERG_ASSIGN_OR_RAISE(const auto& response, tmp_client->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto server_config, CatalogConfigFromJson(json)); + // Merge server config into client config, server config overrides > client config + // properties > server config defaults + auto final_props = std::move(server_config.defaults); + for (const auto& kv : config.configs()) { + final_props.insert_or_assign(kv.first, kv.second); + } + + for (const auto& kv : server_config.overrides) { + final_props.insert_or_assign(kv.first, kv.second); + } + auto final_config = RestCatalogConfig::FromMap(final_props); + ICEBERG_ASSIGN_OR_RAISE(auto client, HttpClient::Make(*final_config)); + ICEBERG_ASSIGN_OR_RAISE(auto final_paths, ResourcePaths::Make(*final_config)); + return std::unique_ptr<RestCatalog>(new RestCatalog( + std::move(final_config), std::move(client), std::move(*final_paths))); + }; + default: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +RestCatalog::RestCatalog(std::unique_ptr<RestCatalogConfig> config, + std::unique_ptr<HttpClient> client, ResourcePaths paths) + : config_(std::move(config)), client_(std::move(client)), paths_(std::move(paths)) {} + +std::string_view RestCatalog::name() const { + auto it = config_->configs().find(std::string(RestCatalogConfig::kName)); + if (it == config_->configs().end() || it->second.empty()) { + return {""}; + } + return std::string_view(it->second); +} + +Result<std::vector<Namespace>> RestCatalog::ListNamespaces(const Namespace& ns) const { + const std::string endpoint = paths_.V1Namespaces(); + std::vector<Namespace> result; + std::string next_token; + while (true) { + cpr::Parameters params; + if (!ns.levels.empty()) { + params.Add({std::string(kQueryParamParent), EncodeNamespaceForUrl(ns)}); + } + if (!next_token.empty()) { + params.Add({std::string(kQueryParamPageToken), next_token}); + } + ICEBERG_ASSIGN_OR_RAISE(const auto& response, client_->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ListNamespacesResponseFromJson(json)); + result.insert(result.end(), list_response.namespaces.begin(), + list_response.namespaces.end()); + if (list_response.next_page_token.empty()) { + return result; + } + next_token = list_response.next_page_token; + continue; + } + case cpr::status::HTTP_NOT_FOUND: { + return NoSuchNamespace("Namespace not found"); + } + default: + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +Status RestCatalog::CreateNamespace( + const Namespace& ns, const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unordered_map<std::string, std::string>> RestCatalog::GetNamespaceProperties( + const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::DropNamespace(const Namespace& ns) { + return NotImplemented("Not implemented"); +} + +Result<bool> RestCatalog::NamespaceExists(const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::UpdateNamespaceProperties( + const Namespace& ns, const std::unordered_map<std::string, std::string>& updates, + const std::unordered_set<std::string>& removals) { + return NotImplemented("Not implemented"); +} + +Result<std::vector<TableIdentifier>> RestCatalog::ListTables(const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Result<std::unique_ptr<Table>> RestCatalog::CreateTable( + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, + const std::string& location, + const std::unordered_map<std::string, std::string>& properties) { Review Comment: Remove the names or add `[[maybe_unused]]`. ########## src/iceberg/catalog/rest/catalog.cc: ########## @@ -0,0 +1,200 @@ +/* + * 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 "iceberg/catalog/rest/catalog.h" + +#include <memory> +#include <utility> + +#include <cpr/cpr.h> + +#include "iceberg/catalog/rest/config.h" +#include "iceberg/catalog/rest/constant.h" +#include "iceberg/catalog/rest/endpoint_util.h" +#include "iceberg/catalog/rest/http_client_interal.h" +#include "iceberg/catalog/rest/json_internal.h" +#include "iceberg/catalog/rest/types.h" +#include "iceberg/json_internal.h" +#include "iceberg/result.h" +#include "iceberg/table.h" +#include "iceberg/util/macros.h" + +namespace iceberg::rest { + +Result<std::unique_ptr<RestCatalog>> RestCatalog::Make(const RestCatalogConfig& config) { + // Create ResourcePaths and validate URI + ICEBERG_ASSIGN_OR_RAISE(auto paths, ResourcePaths::Make(config)); + + ICEBERG_ASSIGN_OR_RAISE(auto tmp_client, HttpClient::Make(config)); + + const std::string endpoint = paths->V1Config(); + cpr::Parameters params; + ICEBERG_ASSIGN_OR_RAISE(const auto& response, tmp_client->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto server_config, CatalogConfigFromJson(json)); + // Merge server config into client config, server config overrides > client config + // properties > server config defaults + auto final_props = std::move(server_config.defaults); + for (const auto& kv : config.configs()) { + final_props.insert_or_assign(kv.first, kv.second); + } + + for (const auto& kv : server_config.overrides) { + final_props.insert_or_assign(kv.first, kv.second); + } + auto final_config = RestCatalogConfig::FromMap(final_props); + ICEBERG_ASSIGN_OR_RAISE(auto client, HttpClient::Make(*final_config)); + ICEBERG_ASSIGN_OR_RAISE(auto final_paths, ResourcePaths::Make(*final_config)); + return std::unique_ptr<RestCatalog>(new RestCatalog( + std::move(final_config), std::move(client), std::move(*final_paths))); + }; + default: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +RestCatalog::RestCatalog(std::unique_ptr<RestCatalogConfig> config, + std::unique_ptr<HttpClient> client, ResourcePaths paths) + : config_(std::move(config)), client_(std::move(client)), paths_(std::move(paths)) {} + +std::string_view RestCatalog::name() const { + auto it = config_->configs().find(std::string(RestCatalogConfig::kName)); + if (it == config_->configs().end() || it->second.empty()) { + return {""}; + } + return std::string_view(it->second); +} + +Result<std::vector<Namespace>> RestCatalog::ListNamespaces(const Namespace& ns) const { + const std::string endpoint = paths_.V1Namespaces(); + std::vector<Namespace> result; + std::string next_token; + while (true) { + cpr::Parameters params; + if (!ns.levels.empty()) { + params.Add({std::string(kQueryParamParent), EncodeNamespaceForUrl(ns)}); + } + if (!next_token.empty()) { + params.Add({std::string(kQueryParamPageToken), next_token}); + } + ICEBERG_ASSIGN_OR_RAISE(const auto& response, client_->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ListNamespacesResponseFromJson(json)); + result.insert(result.end(), list_response.namespaces.begin(), + list_response.namespaces.end()); + if (list_response.next_page_token.empty()) { + return result; + } + next_token = list_response.next_page_token; + continue; + } + case cpr::status::HTTP_NOT_FOUND: { + return NoSuchNamespace("Namespace not found"); + } + default: + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +Status RestCatalog::CreateNamespace( + const Namespace& ns, const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unordered_map<std::string, std::string>> RestCatalog::GetNamespaceProperties( + const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::DropNamespace(const Namespace& ns) { Review Comment: Remove the names or add `[[maybe_unused]]`. ########## src/iceberg/catalog/rest/catalog.cc: ########## @@ -0,0 +1,200 @@ +/* + * 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 "iceberg/catalog/rest/catalog.h" + +#include <memory> +#include <utility> + +#include <cpr/cpr.h> + +#include "iceberg/catalog/rest/config.h" +#include "iceberg/catalog/rest/constant.h" +#include "iceberg/catalog/rest/endpoint_util.h" +#include "iceberg/catalog/rest/http_client_interal.h" +#include "iceberg/catalog/rest/json_internal.h" +#include "iceberg/catalog/rest/types.h" +#include "iceberg/json_internal.h" +#include "iceberg/result.h" +#include "iceberg/table.h" +#include "iceberg/util/macros.h" + +namespace iceberg::rest { + +Result<std::unique_ptr<RestCatalog>> RestCatalog::Make(const RestCatalogConfig& config) { + // Create ResourcePaths and validate URI + ICEBERG_ASSIGN_OR_RAISE(auto paths, ResourcePaths::Make(config)); + + ICEBERG_ASSIGN_OR_RAISE(auto tmp_client, HttpClient::Make(config)); + + const std::string endpoint = paths->V1Config(); + cpr::Parameters params; + ICEBERG_ASSIGN_OR_RAISE(const auto& response, tmp_client->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto server_config, CatalogConfigFromJson(json)); + // Merge server config into client config, server config overrides > client config + // properties > server config defaults + auto final_props = std::move(server_config.defaults); + for (const auto& kv : config.configs()) { + final_props.insert_or_assign(kv.first, kv.second); + } + + for (const auto& kv : server_config.overrides) { + final_props.insert_or_assign(kv.first, kv.second); + } + auto final_config = RestCatalogConfig::FromMap(final_props); + ICEBERG_ASSIGN_OR_RAISE(auto client, HttpClient::Make(*final_config)); + ICEBERG_ASSIGN_OR_RAISE(auto final_paths, ResourcePaths::Make(*final_config)); + return std::unique_ptr<RestCatalog>(new RestCatalog( + std::move(final_config), std::move(client), std::move(*final_paths))); + }; + default: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +RestCatalog::RestCatalog(std::unique_ptr<RestCatalogConfig> config, + std::unique_ptr<HttpClient> client, ResourcePaths paths) + : config_(std::move(config)), client_(std::move(client)), paths_(std::move(paths)) {} + +std::string_view RestCatalog::name() const { + auto it = config_->configs().find(std::string(RestCatalogConfig::kName)); + if (it == config_->configs().end() || it->second.empty()) { + return {""}; + } + return std::string_view(it->second); +} + +Result<std::vector<Namespace>> RestCatalog::ListNamespaces(const Namespace& ns) const { + const std::string endpoint = paths_.V1Namespaces(); + std::vector<Namespace> result; + std::string next_token; + while (true) { + cpr::Parameters params; + if (!ns.levels.empty()) { + params.Add({std::string(kQueryParamParent), EncodeNamespaceForUrl(ns)}); + } + if (!next_token.empty()) { + params.Add({std::string(kQueryParamPageToken), next_token}); + } + ICEBERG_ASSIGN_OR_RAISE(const auto& response, client_->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ListNamespacesResponseFromJson(json)); + result.insert(result.end(), list_response.namespaces.begin(), + list_response.namespaces.end()); + if (list_response.next_page_token.empty()) { + return result; + } + next_token = list_response.next_page_token; + continue; + } + case cpr::status::HTTP_NOT_FOUND: { + return NoSuchNamespace("Namespace not found"); + } + default: + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +Status RestCatalog::CreateNamespace( + const Namespace& ns, const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unordered_map<std::string, std::string>> RestCatalog::GetNamespaceProperties( + const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::DropNamespace(const Namespace& ns) { + return NotImplemented("Not implemented"); +} + +Result<bool> RestCatalog::NamespaceExists(const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::UpdateNamespaceProperties( + const Namespace& ns, const std::unordered_map<std::string, std::string>& updates, + const std::unordered_set<std::string>& removals) { + return NotImplemented("Not implemented"); +} + +Result<std::vector<TableIdentifier>> RestCatalog::ListTables(const Namespace& ns) const { Review Comment: Remove the names or add `[[maybe_unused]]`. ########## src/iceberg/catalog/rest/catalog.cc: ########## @@ -0,0 +1,200 @@ +/* + * 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 "iceberg/catalog/rest/catalog.h" + +#include <memory> +#include <utility> + +#include <cpr/cpr.h> + +#include "iceberg/catalog/rest/config.h" +#include "iceberg/catalog/rest/constant.h" +#include "iceberg/catalog/rest/endpoint_util.h" +#include "iceberg/catalog/rest/http_client_interal.h" +#include "iceberg/catalog/rest/json_internal.h" +#include "iceberg/catalog/rest/types.h" +#include "iceberg/json_internal.h" +#include "iceberg/result.h" +#include "iceberg/table.h" +#include "iceberg/util/macros.h" + +namespace iceberg::rest { + +Result<std::unique_ptr<RestCatalog>> RestCatalog::Make(const RestCatalogConfig& config) { + // Create ResourcePaths and validate URI + ICEBERG_ASSIGN_OR_RAISE(auto paths, ResourcePaths::Make(config)); + + ICEBERG_ASSIGN_OR_RAISE(auto tmp_client, HttpClient::Make(config)); + + const std::string endpoint = paths->V1Config(); + cpr::Parameters params; + ICEBERG_ASSIGN_OR_RAISE(const auto& response, tmp_client->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto server_config, CatalogConfigFromJson(json)); + // Merge server config into client config, server config overrides > client config + // properties > server config defaults + auto final_props = std::move(server_config.defaults); + for (const auto& kv : config.configs()) { + final_props.insert_or_assign(kv.first, kv.second); + } + + for (const auto& kv : server_config.overrides) { + final_props.insert_or_assign(kv.first, kv.second); + } + auto final_config = RestCatalogConfig::FromMap(final_props); + ICEBERG_ASSIGN_OR_RAISE(auto client, HttpClient::Make(*final_config)); + ICEBERG_ASSIGN_OR_RAISE(auto final_paths, ResourcePaths::Make(*final_config)); + return std::unique_ptr<RestCatalog>(new RestCatalog( + std::move(final_config), std::move(client), std::move(*final_paths))); + }; + default: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +RestCatalog::RestCatalog(std::unique_ptr<RestCatalogConfig> config, + std::unique_ptr<HttpClient> client, ResourcePaths paths) + : config_(std::move(config)), client_(std::move(client)), paths_(std::move(paths)) {} + +std::string_view RestCatalog::name() const { + auto it = config_->configs().find(std::string(RestCatalogConfig::kName)); + if (it == config_->configs().end() || it->second.empty()) { + return {""}; + } + return std::string_view(it->second); +} + +Result<std::vector<Namespace>> RestCatalog::ListNamespaces(const Namespace& ns) const { + const std::string endpoint = paths_.V1Namespaces(); + std::vector<Namespace> result; + std::string next_token; + while (true) { + cpr::Parameters params; + if (!ns.levels.empty()) { + params.Add({std::string(kQueryParamParent), EncodeNamespaceForUrl(ns)}); + } + if (!next_token.empty()) { + params.Add({std::string(kQueryParamPageToken), next_token}); + } + ICEBERG_ASSIGN_OR_RAISE(const auto& response, client_->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ListNamespacesResponseFromJson(json)); + result.insert(result.end(), list_response.namespaces.begin(), + list_response.namespaces.end()); + if (list_response.next_page_token.empty()) { + return result; + } + next_token = list_response.next_page_token; + continue; + } + case cpr::status::HTTP_NOT_FOUND: { + return NoSuchNamespace("Namespace not found"); + } + default: + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +Status RestCatalog::CreateNamespace( + const Namespace& ns, const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unordered_map<std::string, std::string>> RestCatalog::GetNamespaceProperties( + const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::DropNamespace(const Namespace& ns) { + return NotImplemented("Not implemented"); +} + +Result<bool> RestCatalog::NamespaceExists(const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::UpdateNamespaceProperties( + const Namespace& ns, const std::unordered_map<std::string, std::string>& updates, + const std::unordered_set<std::string>& removals) { + return NotImplemented("Not implemented"); +} + +Result<std::vector<TableIdentifier>> RestCatalog::ListTables(const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Result<std::unique_ptr<Table>> RestCatalog::CreateTable( + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, + const std::string& location, + const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unique_ptr<Table>> RestCatalog::UpdateTable( + const TableIdentifier& identifier, + const std::vector<std::unique_ptr<TableRequirement>>& requirements, + const std::vector<std::unique_ptr<TableUpdate>>& updates) { Review Comment: Remove the names or add `[[maybe_unused]]`. ########## src/iceberg/catalog/rest/catalog.cc: ########## @@ -0,0 +1,200 @@ +/* + * 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 "iceberg/catalog/rest/catalog.h" + +#include <memory> +#include <utility> + +#include <cpr/cpr.h> + +#include "iceberg/catalog/rest/config.h" +#include "iceberg/catalog/rest/constant.h" +#include "iceberg/catalog/rest/endpoint_util.h" +#include "iceberg/catalog/rest/http_client_interal.h" +#include "iceberg/catalog/rest/json_internal.h" +#include "iceberg/catalog/rest/types.h" +#include "iceberg/json_internal.h" +#include "iceberg/result.h" +#include "iceberg/table.h" +#include "iceberg/util/macros.h" + +namespace iceberg::rest { + +Result<std::unique_ptr<RestCatalog>> RestCatalog::Make(const RestCatalogConfig& config) { + // Create ResourcePaths and validate URI + ICEBERG_ASSIGN_OR_RAISE(auto paths, ResourcePaths::Make(config)); + + ICEBERG_ASSIGN_OR_RAISE(auto tmp_client, HttpClient::Make(config)); + + const std::string endpoint = paths->V1Config(); + cpr::Parameters params; + ICEBERG_ASSIGN_OR_RAISE(const auto& response, tmp_client->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto server_config, CatalogConfigFromJson(json)); + // Merge server config into client config, server config overrides > client config + // properties > server config defaults + auto final_props = std::move(server_config.defaults); + for (const auto& kv : config.configs()) { + final_props.insert_or_assign(kv.first, kv.second); + } + + for (const auto& kv : server_config.overrides) { + final_props.insert_or_assign(kv.first, kv.second); + } + auto final_config = RestCatalogConfig::FromMap(final_props); + ICEBERG_ASSIGN_OR_RAISE(auto client, HttpClient::Make(*final_config)); + ICEBERG_ASSIGN_OR_RAISE(auto final_paths, ResourcePaths::Make(*final_config)); + return std::unique_ptr<RestCatalog>(new RestCatalog( + std::move(final_config), std::move(client), std::move(*final_paths))); + }; + default: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +RestCatalog::RestCatalog(std::unique_ptr<RestCatalogConfig> config, + std::unique_ptr<HttpClient> client, ResourcePaths paths) + : config_(std::move(config)), client_(std::move(client)), paths_(std::move(paths)) {} + +std::string_view RestCatalog::name() const { + auto it = config_->configs().find(std::string(RestCatalogConfig::kName)); + if (it == config_->configs().end() || it->second.empty()) { + return {""}; + } + return std::string_view(it->second); +} + +Result<std::vector<Namespace>> RestCatalog::ListNamespaces(const Namespace& ns) const { + const std::string endpoint = paths_.V1Namespaces(); + std::vector<Namespace> result; + std::string next_token; + while (true) { + cpr::Parameters params; + if (!ns.levels.empty()) { + params.Add({std::string(kQueryParamParent), EncodeNamespaceForUrl(ns)}); + } + if (!next_token.empty()) { + params.Add({std::string(kQueryParamPageToken), next_token}); + } + ICEBERG_ASSIGN_OR_RAISE(const auto& response, client_->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ListNamespacesResponseFromJson(json)); + result.insert(result.end(), list_response.namespaces.begin(), + list_response.namespaces.end()); + if (list_response.next_page_token.empty()) { + return result; + } + next_token = list_response.next_page_token; + continue; + } + case cpr::status::HTTP_NOT_FOUND: { + return NoSuchNamespace("Namespace not found"); + } + default: + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +Status RestCatalog::CreateNamespace( + const Namespace& ns, const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unordered_map<std::string, std::string>> RestCatalog::GetNamespaceProperties( + const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::DropNamespace(const Namespace& ns) { + return NotImplemented("Not implemented"); +} + +Result<bool> RestCatalog::NamespaceExists(const Namespace& ns) const { Review Comment: Remove the names or add `[[maybe_unused]]`. ########## src/iceberg/catalog/rest/catalog.cc: ########## @@ -0,0 +1,200 @@ +/* + * 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 "iceberg/catalog/rest/catalog.h" + +#include <memory> +#include <utility> + +#include <cpr/cpr.h> + +#include "iceberg/catalog/rest/config.h" +#include "iceberg/catalog/rest/constant.h" +#include "iceberg/catalog/rest/endpoint_util.h" +#include "iceberg/catalog/rest/http_client_interal.h" +#include "iceberg/catalog/rest/json_internal.h" +#include "iceberg/catalog/rest/types.h" +#include "iceberg/json_internal.h" +#include "iceberg/result.h" +#include "iceberg/table.h" +#include "iceberg/util/macros.h" + +namespace iceberg::rest { + +Result<std::unique_ptr<RestCatalog>> RestCatalog::Make(const RestCatalogConfig& config) { + // Create ResourcePaths and validate URI + ICEBERG_ASSIGN_OR_RAISE(auto paths, ResourcePaths::Make(config)); + + ICEBERG_ASSIGN_OR_RAISE(auto tmp_client, HttpClient::Make(config)); + + const std::string endpoint = paths->V1Config(); + cpr::Parameters params; + ICEBERG_ASSIGN_OR_RAISE(const auto& response, tmp_client->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto server_config, CatalogConfigFromJson(json)); + // Merge server config into client config, server config overrides > client config + // properties > server config defaults + auto final_props = std::move(server_config.defaults); + for (const auto& kv : config.configs()) { + final_props.insert_or_assign(kv.first, kv.second); + } + + for (const auto& kv : server_config.overrides) { + final_props.insert_or_assign(kv.first, kv.second); + } + auto final_config = RestCatalogConfig::FromMap(final_props); + ICEBERG_ASSIGN_OR_RAISE(auto client, HttpClient::Make(*final_config)); + ICEBERG_ASSIGN_OR_RAISE(auto final_paths, ResourcePaths::Make(*final_config)); + return std::unique_ptr<RestCatalog>(new RestCatalog( + std::move(final_config), std::move(client), std::move(*final_paths))); + }; + default: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +RestCatalog::RestCatalog(std::unique_ptr<RestCatalogConfig> config, + std::unique_ptr<HttpClient> client, ResourcePaths paths) + : config_(std::move(config)), client_(std::move(client)), paths_(std::move(paths)) {} + +std::string_view RestCatalog::name() const { + auto it = config_->configs().find(std::string(RestCatalogConfig::kName)); + if (it == config_->configs().end() || it->second.empty()) { + return {""}; + } + return std::string_view(it->second); +} + +Result<std::vector<Namespace>> RestCatalog::ListNamespaces(const Namespace& ns) const { + const std::string endpoint = paths_.V1Namespaces(); + std::vector<Namespace> result; + std::string next_token; + while (true) { + cpr::Parameters params; + if (!ns.levels.empty()) { + params.Add({std::string(kQueryParamParent), EncodeNamespaceForUrl(ns)}); + } + if (!next_token.empty()) { + params.Add({std::string(kQueryParamPageToken), next_token}); + } + ICEBERG_ASSIGN_OR_RAISE(const auto& response, client_->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ListNamespacesResponseFromJson(json)); + result.insert(result.end(), list_response.namespaces.begin(), + list_response.namespaces.end()); + if (list_response.next_page_token.empty()) { + return result; + } + next_token = list_response.next_page_token; + continue; + } + case cpr::status::HTTP_NOT_FOUND: { + return NoSuchNamespace("Namespace not found"); + } + default: + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +Status RestCatalog::CreateNamespace( + const Namespace& ns, const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unordered_map<std::string, std::string>> RestCatalog::GetNamespaceProperties( + const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::DropNamespace(const Namespace& ns) { + return NotImplemented("Not implemented"); +} + +Result<bool> RestCatalog::NamespaceExists(const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::UpdateNamespaceProperties( + const Namespace& ns, const std::unordered_map<std::string, std::string>& updates, + const std::unordered_set<std::string>& removals) { + return NotImplemented("Not implemented"); +} + +Result<std::vector<TableIdentifier>> RestCatalog::ListTables(const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Result<std::unique_ptr<Table>> RestCatalog::CreateTable( + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, + const std::string& location, + const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unique_ptr<Table>> RestCatalog::UpdateTable( + const TableIdentifier& identifier, + const std::vector<std::unique_ptr<TableRequirement>>& requirements, + const std::vector<std::unique_ptr<TableUpdate>>& updates) { + return NotImplemented("Not implemented"); +} + +Result<std::shared_ptr<Transaction>> RestCatalog::StageCreateTable( + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, + const std::string& location, + const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::DropTable(const TableIdentifier& identifier, bool purge) { + return NotImplemented("Not implemented"); +} + +Result<bool> RestCatalog::TableExists(const TableIdentifier& identifier) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::RenameTable(const TableIdentifier& from, const TableIdentifier& to) { Review Comment: Remove the names or add `[[maybe_unused]]`. ########## src/iceberg/catalog/rest/catalog.cc: ########## @@ -0,0 +1,200 @@ +/* + * 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 "iceberg/catalog/rest/catalog.h" + +#include <memory> +#include <utility> + +#include <cpr/cpr.h> + +#include "iceberg/catalog/rest/config.h" +#include "iceberg/catalog/rest/constant.h" +#include "iceberg/catalog/rest/endpoint_util.h" +#include "iceberg/catalog/rest/http_client_interal.h" +#include "iceberg/catalog/rest/json_internal.h" +#include "iceberg/catalog/rest/types.h" +#include "iceberg/json_internal.h" +#include "iceberg/result.h" +#include "iceberg/table.h" +#include "iceberg/util/macros.h" + +namespace iceberg::rest { + +Result<std::unique_ptr<RestCatalog>> RestCatalog::Make(const RestCatalogConfig& config) { + // Create ResourcePaths and validate URI + ICEBERG_ASSIGN_OR_RAISE(auto paths, ResourcePaths::Make(config)); + + ICEBERG_ASSIGN_OR_RAISE(auto tmp_client, HttpClient::Make(config)); + + const std::string endpoint = paths->V1Config(); + cpr::Parameters params; + ICEBERG_ASSIGN_OR_RAISE(const auto& response, tmp_client->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto server_config, CatalogConfigFromJson(json)); + // Merge server config into client config, server config overrides > client config + // properties > server config defaults + auto final_props = std::move(server_config.defaults); + for (const auto& kv : config.configs()) { + final_props.insert_or_assign(kv.first, kv.second); + } + + for (const auto& kv : server_config.overrides) { + final_props.insert_or_assign(kv.first, kv.second); + } + auto final_config = RestCatalogConfig::FromMap(final_props); + ICEBERG_ASSIGN_OR_RAISE(auto client, HttpClient::Make(*final_config)); + ICEBERG_ASSIGN_OR_RAISE(auto final_paths, ResourcePaths::Make(*final_config)); + return std::unique_ptr<RestCatalog>(new RestCatalog( + std::move(final_config), std::move(client), std::move(*final_paths))); + }; + default: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +RestCatalog::RestCatalog(std::unique_ptr<RestCatalogConfig> config, + std::unique_ptr<HttpClient> client, ResourcePaths paths) + : config_(std::move(config)), client_(std::move(client)), paths_(std::move(paths)) {} + +std::string_view RestCatalog::name() const { + auto it = config_->configs().find(std::string(RestCatalogConfig::kName)); + if (it == config_->configs().end() || it->second.empty()) { + return {""}; + } + return std::string_view(it->second); +} + +Result<std::vector<Namespace>> RestCatalog::ListNamespaces(const Namespace& ns) const { + const std::string endpoint = paths_.V1Namespaces(); + std::vector<Namespace> result; + std::string next_token; + while (true) { + cpr::Parameters params; + if (!ns.levels.empty()) { + params.Add({std::string(kQueryParamParent), EncodeNamespaceForUrl(ns)}); + } + if (!next_token.empty()) { + params.Add({std::string(kQueryParamPageToken), next_token}); + } + ICEBERG_ASSIGN_OR_RAISE(const auto& response, client_->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ListNamespacesResponseFromJson(json)); + result.insert(result.end(), list_response.namespaces.begin(), + list_response.namespaces.end()); + if (list_response.next_page_token.empty()) { + return result; + } + next_token = list_response.next_page_token; + continue; + } + case cpr::status::HTTP_NOT_FOUND: { + return NoSuchNamespace("Namespace not found"); + } + default: + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +Status RestCatalog::CreateNamespace( + const Namespace& ns, const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unordered_map<std::string, std::string>> RestCatalog::GetNamespaceProperties( + const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::DropNamespace(const Namespace& ns) { + return NotImplemented("Not implemented"); +} + +Result<bool> RestCatalog::NamespaceExists(const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::UpdateNamespaceProperties( + const Namespace& ns, const std::unordered_map<std::string, std::string>& updates, + const std::unordered_set<std::string>& removals) { + return NotImplemented("Not implemented"); +} + +Result<std::vector<TableIdentifier>> RestCatalog::ListTables(const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Result<std::unique_ptr<Table>> RestCatalog::CreateTable( + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, + const std::string& location, + const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unique_ptr<Table>> RestCatalog::UpdateTable( + const TableIdentifier& identifier, + const std::vector<std::unique_ptr<TableRequirement>>& requirements, + const std::vector<std::unique_ptr<TableUpdate>>& updates) { + return NotImplemented("Not implemented"); +} + +Result<std::shared_ptr<Transaction>> RestCatalog::StageCreateTable( + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, + const std::string& location, + const std::unordered_map<std::string, std::string>& properties) { Review Comment: Remove the names or add `[[maybe_unused]]`. ########## src/iceberg/catalog/rest/catalog.h: ########## @@ -0,0 +1,110 @@ +/* + * 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 <memory> +#include <string> + +#include "iceberg/catalog.h" +#include "iceberg/catalog/rest/config.h" +#include "iceberg/catalog/rest/http_client_interal.h" +#include "iceberg/catalog/rest/iceberg_rest_export.h" +#include "iceberg/catalog/rest/resource_paths.h" +#include "iceberg/result.h" + +/// \file iceberg/catalog/rest/catalog.h +/// RestCatalog implementation for Iceberg REST API. + +namespace iceberg::rest { + +class ICEBERG_REST_EXPORT RestCatalog : public Catalog { + public: + RestCatalog(const RestCatalog&) = delete; + RestCatalog& operator=(const RestCatalog&) = delete; + RestCatalog(RestCatalog&&) = delete; + RestCatalog& operator=(RestCatalog&&) = delete; + + /// \brief Create a RestCatalog instance + /// + /// \param config the configuration for the RestCatalog + /// \return a unique_ptr to RestCatalog instance + static Result<std::unique_ptr<RestCatalog>> Make(const RestCatalogConfig& config); + + std::string_view name() const override; + + Result<std::vector<Namespace>> ListNamespaces(const Namespace& ns) const override; + + Status CreateNamespace( + const Namespace& ns, + const std::unordered_map<std::string, std::string>& properties) override; + + Result<std::unordered_map<std::string, std::string>> GetNamespaceProperties( + const Namespace& ns) const override; + + Status DropNamespace(const Namespace& ns) override; + + Result<bool> NamespaceExists(const Namespace& ns) const override; + + Status UpdateNamespaceProperties( + const Namespace& ns, const std::unordered_map<std::string, std::string>& updates, + const std::unordered_set<std::string>& removals) override; + + Result<std::vector<TableIdentifier>> ListTables(const Namespace& ns) const override; + + Result<std::unique_ptr<Table>> CreateTable( + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, + const std::string& location, + const std::unordered_map<std::string, std::string>& properties) override; + + Result<std::unique_ptr<Table>> UpdateTable( + const TableIdentifier& identifier, + const std::vector<std::unique_ptr<TableRequirement>>& requirements, + const std::vector<std::unique_ptr<TableUpdate>>& updates) override; + + Result<std::shared_ptr<Transaction>> StageCreateTable( + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, + const std::string& location, + const std::unordered_map<std::string, std::string>& properties) override; + + Result<bool> TableExists(const TableIdentifier& identifier) const override; + + Status RenameTable(const TableIdentifier& from, const TableIdentifier& to) override; + + Status DropTable(const TableIdentifier& identifier, bool purge) override; + + Result<std::unique_ptr<Table>> LoadTable(const TableIdentifier& identifier) override; + + Result<std::shared_ptr<Table>> RegisterTable( + const TableIdentifier& identifier, + const std::string& metadata_file_location) override; + + std::unique_ptr<RestCatalog::TableBuilder> BuildTable( + const TableIdentifier& identifier, const Schema& schema) const override; + + private: + RestCatalog(std::unique_ptr<RestCatalogConfig> config, Review Comment: Why do we put the ctor to the private section? Please add a comment for this reason (e.g. using builder pattern). ########## src/iceberg/catalog/rest/catalog.cc: ########## @@ -0,0 +1,200 @@ +/* + * 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 "iceberg/catalog/rest/catalog.h" + +#include <memory> +#include <utility> + +#include <cpr/cpr.h> + +#include "iceberg/catalog/rest/config.h" +#include "iceberg/catalog/rest/constant.h" +#include "iceberg/catalog/rest/endpoint_util.h" +#include "iceberg/catalog/rest/http_client_interal.h" +#include "iceberg/catalog/rest/json_internal.h" +#include "iceberg/catalog/rest/types.h" +#include "iceberg/json_internal.h" +#include "iceberg/result.h" +#include "iceberg/table.h" +#include "iceberg/util/macros.h" + +namespace iceberg::rest { + +Result<std::unique_ptr<RestCatalog>> RestCatalog::Make(const RestCatalogConfig& config) { + // Create ResourcePaths and validate URI + ICEBERG_ASSIGN_OR_RAISE(auto paths, ResourcePaths::Make(config)); + + ICEBERG_ASSIGN_OR_RAISE(auto tmp_client, HttpClient::Make(config)); + + const std::string endpoint = paths->V1Config(); + cpr::Parameters params; + ICEBERG_ASSIGN_OR_RAISE(const auto& response, tmp_client->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto server_config, CatalogConfigFromJson(json)); + // Merge server config into client config, server config overrides > client config + // properties > server config defaults + auto final_props = std::move(server_config.defaults); + for (const auto& kv : config.configs()) { + final_props.insert_or_assign(kv.first, kv.second); + } + + for (const auto& kv : server_config.overrides) { + final_props.insert_or_assign(kv.first, kv.second); + } + auto final_config = RestCatalogConfig::FromMap(final_props); + ICEBERG_ASSIGN_OR_RAISE(auto client, HttpClient::Make(*final_config)); + ICEBERG_ASSIGN_OR_RAISE(auto final_paths, ResourcePaths::Make(*final_config)); + return std::unique_ptr<RestCatalog>(new RestCatalog( + std::move(final_config), std::move(client), std::move(*final_paths))); + }; + default: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +RestCatalog::RestCatalog(std::unique_ptr<RestCatalogConfig> config, + std::unique_ptr<HttpClient> client, ResourcePaths paths) + : config_(std::move(config)), client_(std::move(client)), paths_(std::move(paths)) {} + +std::string_view RestCatalog::name() const { + auto it = config_->configs().find(std::string(RestCatalogConfig::kName)); + if (it == config_->configs().end() || it->second.empty()) { + return {""}; + } + return std::string_view(it->second); +} + +Result<std::vector<Namespace>> RestCatalog::ListNamespaces(const Namespace& ns) const { + const std::string endpoint = paths_.V1Namespaces(); + std::vector<Namespace> result; + std::string next_token; + while (true) { + cpr::Parameters params; + if (!ns.levels.empty()) { + params.Add({std::string(kQueryParamParent), EncodeNamespaceForUrl(ns)}); + } + if (!next_token.empty()) { + params.Add({std::string(kQueryParamPageToken), next_token}); + } + ICEBERG_ASSIGN_OR_RAISE(const auto& response, client_->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ListNamespacesResponseFromJson(json)); + result.insert(result.end(), list_response.namespaces.begin(), + list_response.namespaces.end()); + if (list_response.next_page_token.empty()) { + return result; + } + next_token = list_response.next_page_token; + continue; + } + case cpr::status::HTTP_NOT_FOUND: { + return NoSuchNamespace("Namespace not found"); + } + default: + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +Status RestCatalog::CreateNamespace( + const Namespace& ns, const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unordered_map<std::string, std::string>> RestCatalog::GetNamespaceProperties( + const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::DropNamespace(const Namespace& ns) { + return NotImplemented("Not implemented"); +} + +Result<bool> RestCatalog::NamespaceExists(const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::UpdateNamespaceProperties( + const Namespace& ns, const std::unordered_map<std::string, std::string>& updates, + const std::unordered_set<std::string>& removals) { + return NotImplemented("Not implemented"); +} + +Result<std::vector<TableIdentifier>> RestCatalog::ListTables(const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Result<std::unique_ptr<Table>> RestCatalog::CreateTable( + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, + const std::string& location, + const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unique_ptr<Table>> RestCatalog::UpdateTable( + const TableIdentifier& identifier, + const std::vector<std::unique_ptr<TableRequirement>>& requirements, + const std::vector<std::unique_ptr<TableUpdate>>& updates) { + return NotImplemented("Not implemented"); +} + +Result<std::shared_ptr<Transaction>> RestCatalog::StageCreateTable( + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, + const std::string& location, + const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::DropTable(const TableIdentifier& identifier, bool purge) { Review Comment: Remove the names or add `[[maybe_unused]]`. ########## src/iceberg/catalog/rest/catalog.cc: ########## @@ -0,0 +1,200 @@ +/* + * 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 "iceberg/catalog/rest/catalog.h" + +#include <memory> +#include <utility> + +#include <cpr/cpr.h> + +#include "iceberg/catalog/rest/config.h" +#include "iceberg/catalog/rest/constant.h" +#include "iceberg/catalog/rest/endpoint_util.h" +#include "iceberg/catalog/rest/http_client_interal.h" +#include "iceberg/catalog/rest/json_internal.h" +#include "iceberg/catalog/rest/types.h" +#include "iceberg/json_internal.h" +#include "iceberg/result.h" +#include "iceberg/table.h" +#include "iceberg/util/macros.h" + +namespace iceberg::rest { + +Result<std::unique_ptr<RestCatalog>> RestCatalog::Make(const RestCatalogConfig& config) { + // Create ResourcePaths and validate URI + ICEBERG_ASSIGN_OR_RAISE(auto paths, ResourcePaths::Make(config)); + + ICEBERG_ASSIGN_OR_RAISE(auto tmp_client, HttpClient::Make(config)); + + const std::string endpoint = paths->V1Config(); + cpr::Parameters params; + ICEBERG_ASSIGN_OR_RAISE(const auto& response, tmp_client->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto server_config, CatalogConfigFromJson(json)); + // Merge server config into client config, server config overrides > client config + // properties > server config defaults + auto final_props = std::move(server_config.defaults); + for (const auto& kv : config.configs()) { + final_props.insert_or_assign(kv.first, kv.second); + } + + for (const auto& kv : server_config.overrides) { + final_props.insert_or_assign(kv.first, kv.second); + } + auto final_config = RestCatalogConfig::FromMap(final_props); + ICEBERG_ASSIGN_OR_RAISE(auto client, HttpClient::Make(*final_config)); + ICEBERG_ASSIGN_OR_RAISE(auto final_paths, ResourcePaths::Make(*final_config)); + return std::unique_ptr<RestCatalog>(new RestCatalog( + std::move(final_config), std::move(client), std::move(*final_paths))); + }; + default: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +RestCatalog::RestCatalog(std::unique_ptr<RestCatalogConfig> config, + std::unique_ptr<HttpClient> client, ResourcePaths paths) + : config_(std::move(config)), client_(std::move(client)), paths_(std::move(paths)) {} + +std::string_view RestCatalog::name() const { + auto it = config_->configs().find(std::string(RestCatalogConfig::kName)); + if (it == config_->configs().end() || it->second.empty()) { + return {""}; + } + return std::string_view(it->second); +} + +Result<std::vector<Namespace>> RestCatalog::ListNamespaces(const Namespace& ns) const { + const std::string endpoint = paths_.V1Namespaces(); + std::vector<Namespace> result; + std::string next_token; + while (true) { + cpr::Parameters params; + if (!ns.levels.empty()) { + params.Add({std::string(kQueryParamParent), EncodeNamespaceForUrl(ns)}); + } + if (!next_token.empty()) { + params.Add({std::string(kQueryParamPageToken), next_token}); + } + ICEBERG_ASSIGN_OR_RAISE(const auto& response, client_->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ListNamespacesResponseFromJson(json)); + result.insert(result.end(), list_response.namespaces.begin(), + list_response.namespaces.end()); + if (list_response.next_page_token.empty()) { + return result; + } + next_token = list_response.next_page_token; + continue; + } + case cpr::status::HTTP_NOT_FOUND: { + return NoSuchNamespace("Namespace not found"); + } + default: + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +Status RestCatalog::CreateNamespace( + const Namespace& ns, const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unordered_map<std::string, std::string>> RestCatalog::GetNamespaceProperties( + const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::DropNamespace(const Namespace& ns) { + return NotImplemented("Not implemented"); +} + +Result<bool> RestCatalog::NamespaceExists(const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::UpdateNamespaceProperties( + const Namespace& ns, const std::unordered_map<std::string, std::string>& updates, + const std::unordered_set<std::string>& removals) { + return NotImplemented("Not implemented"); +} + +Result<std::vector<TableIdentifier>> RestCatalog::ListTables(const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Result<std::unique_ptr<Table>> RestCatalog::CreateTable( + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, + const std::string& location, + const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unique_ptr<Table>> RestCatalog::UpdateTable( + const TableIdentifier& identifier, + const std::vector<std::unique_ptr<TableRequirement>>& requirements, + const std::vector<std::unique_ptr<TableUpdate>>& updates) { + return NotImplemented("Not implemented"); +} + +Result<std::shared_ptr<Transaction>> RestCatalog::StageCreateTable( + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, + const std::string& location, + const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::DropTable(const TableIdentifier& identifier, bool purge) { + return NotImplemented("Not implemented"); +} + +Result<bool> RestCatalog::TableExists(const TableIdentifier& identifier) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::RenameTable(const TableIdentifier& from, const TableIdentifier& to) { + return NotImplemented("Not implemented"); +} + +Result<std::unique_ptr<Table>> RestCatalog::LoadTable(const TableIdentifier& identifier) { Review Comment: Remove the names or add `[[maybe_unused]]`. ########## src/iceberg/catalog/rest/catalog.cc: ########## @@ -0,0 +1,200 @@ +/* + * 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 "iceberg/catalog/rest/catalog.h" + +#include <memory> +#include <utility> + +#include <cpr/cpr.h> + +#include "iceberg/catalog/rest/config.h" +#include "iceberg/catalog/rest/constant.h" +#include "iceberg/catalog/rest/endpoint_util.h" +#include "iceberg/catalog/rest/http_client_interal.h" +#include "iceberg/catalog/rest/json_internal.h" +#include "iceberg/catalog/rest/types.h" +#include "iceberg/json_internal.h" +#include "iceberg/result.h" +#include "iceberg/table.h" +#include "iceberg/util/macros.h" + +namespace iceberg::rest { + +Result<std::unique_ptr<RestCatalog>> RestCatalog::Make(const RestCatalogConfig& config) { + // Create ResourcePaths and validate URI + ICEBERG_ASSIGN_OR_RAISE(auto paths, ResourcePaths::Make(config)); + + ICEBERG_ASSIGN_OR_RAISE(auto tmp_client, HttpClient::Make(config)); + + const std::string endpoint = paths->V1Config(); + cpr::Parameters params; + ICEBERG_ASSIGN_OR_RAISE(const auto& response, tmp_client->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto server_config, CatalogConfigFromJson(json)); + // Merge server config into client config, server config overrides > client config + // properties > server config defaults + auto final_props = std::move(server_config.defaults); + for (const auto& kv : config.configs()) { + final_props.insert_or_assign(kv.first, kv.second); + } + + for (const auto& kv : server_config.overrides) { + final_props.insert_or_assign(kv.first, kv.second); + } + auto final_config = RestCatalogConfig::FromMap(final_props); + ICEBERG_ASSIGN_OR_RAISE(auto client, HttpClient::Make(*final_config)); + ICEBERG_ASSIGN_OR_RAISE(auto final_paths, ResourcePaths::Make(*final_config)); + return std::unique_ptr<RestCatalog>(new RestCatalog( + std::move(final_config), std::move(client), std::move(*final_paths))); + }; + default: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +RestCatalog::RestCatalog(std::unique_ptr<RestCatalogConfig> config, + std::unique_ptr<HttpClient> client, ResourcePaths paths) + : config_(std::move(config)), client_(std::move(client)), paths_(std::move(paths)) {} + +std::string_view RestCatalog::name() const { + auto it = config_->configs().find(std::string(RestCatalogConfig::kName)); + if (it == config_->configs().end() || it->second.empty()) { + return {""}; + } + return std::string_view(it->second); +} + +Result<std::vector<Namespace>> RestCatalog::ListNamespaces(const Namespace& ns) const { + const std::string endpoint = paths_.V1Namespaces(); + std::vector<Namespace> result; + std::string next_token; + while (true) { + cpr::Parameters params; + if (!ns.levels.empty()) { + params.Add({std::string(kQueryParamParent), EncodeNamespaceForUrl(ns)}); + } + if (!next_token.empty()) { + params.Add({std::string(kQueryParamPageToken), next_token}); + } + ICEBERG_ASSIGN_OR_RAISE(const auto& response, client_->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ListNamespacesResponseFromJson(json)); + result.insert(result.end(), list_response.namespaces.begin(), + list_response.namespaces.end()); + if (list_response.next_page_token.empty()) { + return result; + } + next_token = list_response.next_page_token; + continue; + } + case cpr::status::HTTP_NOT_FOUND: { + return NoSuchNamespace("Namespace not found"); + } + default: + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +Status RestCatalog::CreateNamespace( + const Namespace& ns, const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unordered_map<std::string, std::string>> RestCatalog::GetNamespaceProperties( + const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::DropNamespace(const Namespace& ns) { + return NotImplemented("Not implemented"); +} + +Result<bool> RestCatalog::NamespaceExists(const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::UpdateNamespaceProperties( + const Namespace& ns, const std::unordered_map<std::string, std::string>& updates, + const std::unordered_set<std::string>& removals) { + return NotImplemented("Not implemented"); +} + +Result<std::vector<TableIdentifier>> RestCatalog::ListTables(const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Result<std::unique_ptr<Table>> RestCatalog::CreateTable( + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, + const std::string& location, + const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unique_ptr<Table>> RestCatalog::UpdateTable( + const TableIdentifier& identifier, + const std::vector<std::unique_ptr<TableRequirement>>& requirements, + const std::vector<std::unique_ptr<TableUpdate>>& updates) { + return NotImplemented("Not implemented"); +} + +Result<std::shared_ptr<Transaction>> RestCatalog::StageCreateTable( + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, + const std::string& location, + const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::DropTable(const TableIdentifier& identifier, bool purge) { + return NotImplemented("Not implemented"); +} + +Result<bool> RestCatalog::TableExists(const TableIdentifier& identifier) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::RenameTable(const TableIdentifier& from, const TableIdentifier& to) { + return NotImplemented("Not implemented"); +} + +Result<std::unique_ptr<Table>> RestCatalog::LoadTable(const TableIdentifier& identifier) { + return NotImplemented("Not implemented"); +} + +Result<std::shared_ptr<Table>> RestCatalog::RegisterTable( + const TableIdentifier& identifier, const std::string& metadata_file_location) { + return NotImplemented("Not implemented"); +} + +std::unique_ptr<RestCatalog::TableBuilder> RestCatalog::BuildTable( + const TableIdentifier& identifier, const Schema& schema) const { Review Comment: Remove the names or add `[[maybe_unused]]`. ########## src/iceberg/catalog/rest/catalog.cc: ########## @@ -0,0 +1,200 @@ +/* + * 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 "iceberg/catalog/rest/catalog.h" + +#include <memory> +#include <utility> + +#include <cpr/cpr.h> + +#include "iceberg/catalog/rest/config.h" +#include "iceberg/catalog/rest/constant.h" +#include "iceberg/catalog/rest/endpoint_util.h" +#include "iceberg/catalog/rest/http_client_interal.h" +#include "iceberg/catalog/rest/json_internal.h" +#include "iceberg/catalog/rest/types.h" +#include "iceberg/json_internal.h" +#include "iceberg/result.h" +#include "iceberg/table.h" +#include "iceberg/util/macros.h" + +namespace iceberg::rest { + +Result<std::unique_ptr<RestCatalog>> RestCatalog::Make(const RestCatalogConfig& config) { + // Create ResourcePaths and validate URI + ICEBERG_ASSIGN_OR_RAISE(auto paths, ResourcePaths::Make(config)); + + ICEBERG_ASSIGN_OR_RAISE(auto tmp_client, HttpClient::Make(config)); + + const std::string endpoint = paths->V1Config(); + cpr::Parameters params; + ICEBERG_ASSIGN_OR_RAISE(const auto& response, tmp_client->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto server_config, CatalogConfigFromJson(json)); + // Merge server config into client config, server config overrides > client config + // properties > server config defaults + auto final_props = std::move(server_config.defaults); + for (const auto& kv : config.configs()) { + final_props.insert_or_assign(kv.first, kv.second); + } + + for (const auto& kv : server_config.overrides) { + final_props.insert_or_assign(kv.first, kv.second); + } + auto final_config = RestCatalogConfig::FromMap(final_props); + ICEBERG_ASSIGN_OR_RAISE(auto client, HttpClient::Make(*final_config)); + ICEBERG_ASSIGN_OR_RAISE(auto final_paths, ResourcePaths::Make(*final_config)); + return std::unique_ptr<RestCatalog>(new RestCatalog( + std::move(final_config), std::move(client), std::move(*final_paths))); + }; + default: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +RestCatalog::RestCatalog(std::unique_ptr<RestCatalogConfig> config, + std::unique_ptr<HttpClient> client, ResourcePaths paths) + : config_(std::move(config)), client_(std::move(client)), paths_(std::move(paths)) {} + +std::string_view RestCatalog::name() const { + auto it = config_->configs().find(std::string(RestCatalogConfig::kName)); + if (it == config_->configs().end() || it->second.empty()) { + return {""}; + } + return std::string_view(it->second); +} + +Result<std::vector<Namespace>> RestCatalog::ListNamespaces(const Namespace& ns) const { + const std::string endpoint = paths_.V1Namespaces(); + std::vector<Namespace> result; + std::string next_token; + while (true) { + cpr::Parameters params; + if (!ns.levels.empty()) { + params.Add({std::string(kQueryParamParent), EncodeNamespaceForUrl(ns)}); + } + if (!next_token.empty()) { + params.Add({std::string(kQueryParamPageToken), next_token}); + } + ICEBERG_ASSIGN_OR_RAISE(const auto& response, client_->Get(endpoint, params)); + switch (response.status_code) { + case cpr::status::HTTP_OK: { + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ListNamespacesResponseFromJson(json)); + result.insert(result.end(), list_response.namespaces.begin(), + list_response.namespaces.end()); + if (list_response.next_page_token.empty()) { + return result; + } + next_token = list_response.next_page_token; + continue; + } + case cpr::status::HTTP_NOT_FOUND: { + return NoSuchNamespace("Namespace not found"); + } + default: + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); + return UnknownError("Error listing namespaces: {}", list_response.error.message); + } + } +} + +Status RestCatalog::CreateNamespace( + const Namespace& ns, const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unordered_map<std::string, std::string>> RestCatalog::GetNamespaceProperties( + const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::DropNamespace(const Namespace& ns) { + return NotImplemented("Not implemented"); +} + +Result<bool> RestCatalog::NamespaceExists(const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::UpdateNamespaceProperties( + const Namespace& ns, const std::unordered_map<std::string, std::string>& updates, + const std::unordered_set<std::string>& removals) { + return NotImplemented("Not implemented"); +} + +Result<std::vector<TableIdentifier>> RestCatalog::ListTables(const Namespace& ns) const { + return NotImplemented("Not implemented"); +} + +Result<std::unique_ptr<Table>> RestCatalog::CreateTable( + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, + const std::string& location, + const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Result<std::unique_ptr<Table>> RestCatalog::UpdateTable( + const TableIdentifier& identifier, + const std::vector<std::unique_ptr<TableRequirement>>& requirements, + const std::vector<std::unique_ptr<TableUpdate>>& updates) { + return NotImplemented("Not implemented"); +} + +Result<std::shared_ptr<Transaction>> RestCatalog::StageCreateTable( + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, + const std::string& location, + const std::unordered_map<std::string, std::string>& properties) { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::DropTable(const TableIdentifier& identifier, bool purge) { + return NotImplemented("Not implemented"); +} + +Result<bool> RestCatalog::TableExists(const TableIdentifier& identifier) const { + return NotImplemented("Not implemented"); +} + +Status RestCatalog::RenameTable(const TableIdentifier& from, const TableIdentifier& to) { + return NotImplemented("Not implemented"); +} + +Result<std::unique_ptr<Table>> RestCatalog::LoadTable(const TableIdentifier& identifier) { + return NotImplemented("Not implemented"); +} + +Result<std::shared_ptr<Table>> RestCatalog::RegisterTable( + const TableIdentifier& identifier, const std::string& metadata_file_location) { + return NotImplemented("Not implemented"); +} + +std::unique_ptr<RestCatalog::TableBuilder> RestCatalog::BuildTable( + const TableIdentifier& identifier, const Schema& schema) const { + return nullptr; Review Comment: Why does it always return null? ########## src/iceberg/catalog/rest/catalog.h: ########## @@ -0,0 +1,110 @@ +/* + * 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 <memory> +#include <string> + +#include "iceberg/catalog.h" +#include "iceberg/catalog/rest/config.h" +#include "iceberg/catalog/rest/http_client_interal.h" +#include "iceberg/catalog/rest/iceberg_rest_export.h" +#include "iceberg/catalog/rest/resource_paths.h" +#include "iceberg/result.h" + +/// \file iceberg/catalog/rest/catalog.h +/// RestCatalog implementation for Iceberg REST API. + +namespace iceberg::rest { + +class ICEBERG_REST_EXPORT RestCatalog : public Catalog { Review Comment: Add a description for this class. ########## src/iceberg/catalog/rest/config.h: ########## @@ -0,0 +1,69 @@ +/* + * 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 <memory> +#include <string> +#include <unordered_map> + +#include <cpr/cprtypes.h> + +#include "iceberg/catalog/rest/iceberg_rest_export.h" +#include "iceberg/result.h" +#include "iceberg/util/config.h" + +/// \file iceberg/catalog/rest/config.h +/// \brief RestCatalogConfig implementation for Iceberg REST API. + +namespace iceberg::rest { + +/// \brief Configuration class for a REST Catalog. +class ICEBERG_REST_EXPORT RestCatalogConfig : public ConfigBase<RestCatalogConfig> { + public: + template <typename T> + using Entry = const ConfigBase<RestCatalogConfig>::Entry<T>; + + /// \brief The URI of the REST catalog server. + inline static std::string_view kUri{"uri"}; + + /// \brief The name of the catalog. + inline static std::string_view kName{"name"}; Review Comment: add `constexpr` e.g. ``` inline static constexpr std::string_view ``` ########## src/iceberg/catalog/rest/resource_paths.cc: ########## @@ -0,0 +1,135 @@ +/* + * 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 "iceberg/catalog/rest/resource_paths.h" + +#include <format> + +#include "iceberg/catalog/rest/config.h" +#include "iceberg/catalog/rest/constant.h" +#include "iceberg/catalog/rest/endpoint_util.h" +#include "iceberg/result.h" +#include "iceberg/util/macros.h" + +namespace iceberg::rest { + +Result<std::unique_ptr<ResourcePaths>> ResourcePaths::Make( + const RestCatalogConfig& config) { + // Validate and extract URI + auto it = config.configs().find(std::string(RestCatalogConfig::kUri)); + if (it == config.configs().end() || it->second.empty()) { Review Comment: add `[[unlikely]]`. ########## src/iceberg/catalog/rest/config.cc: ########## @@ -0,0 +1,62 @@ +/* + * 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 "iceberg/catalog/rest/config.h" + +#include "iceberg/catalog/rest/constant.h" + +namespace iceberg::rest { + +std::unique_ptr<RestCatalogConfig> RestCatalogConfig::default_properties() { + return std::make_unique<RestCatalogConfig>(); +} + +std::unique_ptr<RestCatalogConfig> RestCatalogConfig::FromMap( + const std::unordered_map<std::string, std::string>& properties) { + auto rest_catalog_config = std::make_unique<RestCatalogConfig>(); + rest_catalog_config->configs_ = properties; + return rest_catalog_config; +} + +Result<cpr::Header> RestCatalogConfig::GetExtraHeaders() const { + cpr::Header headers; + + headers[std::string(kHeaderContentType)] = std::string(kMimeTypeApplicationJson); + headers[std::string(kHeaderUserAgent)] = std::string(kUserAgent); + headers[std::string(kHeaderXClientVersion)] = std::string(kClientVersion); + + constexpr std::string_view prefix = "header."; + for (const auto& [key, value] : configs_) { + if (key.starts_with(prefix)) { + std::string_view header_name = std::string_view(key).substr(prefix.length()); + + if (header_name.empty()) { Review Comment: Add `[[unlikely]]` ``` if (header_name.empty()) [[unlikely]] { ``` ########## src/iceberg/catalog/rest/config.cc: ########## @@ -0,0 +1,62 @@ +/* + * 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 "iceberg/catalog/rest/config.h" + +#include "iceberg/catalog/rest/constant.h" + +namespace iceberg::rest { + +std::unique_ptr<RestCatalogConfig> RestCatalogConfig::default_properties() { + return std::make_unique<RestCatalogConfig>(); +} + +std::unique_ptr<RestCatalogConfig> RestCatalogConfig::FromMap( + const std::unordered_map<std::string, std::string>& properties) { + auto rest_catalog_config = std::make_unique<RestCatalogConfig>(); + rest_catalog_config->configs_ = properties; + return rest_catalog_config; +} + +Result<cpr::Header> RestCatalogConfig::GetExtraHeaders() const { + cpr::Header headers; + + headers[std::string(kHeaderContentType)] = std::string(kMimeTypeApplicationJson); + headers[std::string(kHeaderUserAgent)] = std::string(kUserAgent); + headers[std::string(kHeaderXClientVersion)] = std::string(kClientVersion); + + constexpr std::string_view prefix = "header."; + for (const auto& [key, value] : configs_) { + if (key.starts_with(prefix)) { + std::string_view header_name = std::string_view(key).substr(prefix.length()); + + if (header_name.empty()) { + return InvalidArgument("Header name cannot be empty after '{}' prefix", prefix); + } + + if (value.empty()) { Review Comment: Add `[[unlikely]]`. ########## src/iceberg/catalog/rest/config.h: ########## @@ -0,0 +1,69 @@ +/* + * 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 <memory> +#include <string> +#include <unordered_map> + +#include <cpr/cprtypes.h> + +#include "iceberg/catalog/rest/iceberg_rest_export.h" +#include "iceberg/result.h" +#include "iceberg/util/config.h" + +/// \file iceberg/catalog/rest/config.h +/// \brief RestCatalogConfig implementation for Iceberg REST API. + +namespace iceberg::rest { + +/// \brief Configuration class for a REST Catalog. +class ICEBERG_REST_EXPORT RestCatalogConfig : public ConfigBase<RestCatalogConfig> { + public: + template <typename T> + using Entry = const ConfigBase<RestCatalogConfig>::Entry<T>; + + /// \brief The URI of the REST catalog server. + inline static std::string_view kUri{"uri"}; + + /// \brief The name of the catalog. + inline static std::string_view kName{"name"}; + + /// \brief The warehouse path. + inline static std::string_view kWarehouse{"warehouse"}; Review Comment: add `constexpr` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
