|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/catalog/rest/catalog.h" |
| 21 | + |
| 22 | +#include <memory> |
| 23 | +#include <utility> |
| 24 | + |
| 25 | +#include <cpr/cpr.h> |
| 26 | + |
| 27 | +#include "iceberg/catalog/rest/config.h" |
| 28 | +#include "iceberg/catalog/rest/constant.h" |
| 29 | +#include "iceberg/catalog/rest/endpoint_util.h" |
| 30 | +#include "iceberg/catalog/rest/http_client_interal.h" |
| 31 | +#include "iceberg/catalog/rest/json_internal.h" |
| 32 | +#include "iceberg/catalog/rest/types.h" |
| 33 | +#include "iceberg/json_internal.h" |
| 34 | +#include "iceberg/result.h" |
| 35 | +#include "iceberg/table.h" |
| 36 | +#include "iceberg/util/macros.h" |
| 37 | + |
| 38 | +namespace iceberg::rest { |
| 39 | + |
| 40 | +Result<std::unique_ptr<RestCatalog>> RestCatalog::Make(const RestCatalogConfig& config) { |
| 41 | + // Create ResourcePaths and validate URI |
| 42 | + ICEBERG_ASSIGN_OR_RAISE(auto paths, ResourcePaths::Make(config)); |
| 43 | + |
| 44 | + ICEBERG_ASSIGN_OR_RAISE(auto tmp_client, HttpClient::Make(config)); |
| 45 | + |
| 46 | + const std::string endpoint = paths->V1Config(); |
| 47 | + cpr::Parameters params; |
| 48 | + ICEBERG_ASSIGN_OR_RAISE(const auto& response, tmp_client->Get(endpoint, params)); |
| 49 | + switch (response.status_code) { |
| 50 | + case cpr::status::HTTP_OK: { |
| 51 | + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); |
| 52 | + ICEBERG_ASSIGN_OR_RAISE(auto server_config, CatalogConfigFromJson(json)); |
| 53 | + // Merge server config into client config, server config overrides > client config |
| 54 | + // properties > server config defaults |
| 55 | + auto final_props = std::move(server_config.defaults); |
| 56 | + for (const auto& kv : config.configs()) { |
| 57 | + final_props.insert_or_assign(kv.first, kv.second); |
| 58 | + } |
| 59 | + |
| 60 | + for (const auto& kv : server_config.overrides) { |
| 61 | + final_props.insert_or_assign(kv.first, kv.second); |
| 62 | + } |
| 63 | + auto final_config = RestCatalogConfig::FromMap(final_props); |
| 64 | + ICEBERG_ASSIGN_OR_RAISE(auto client, HttpClient::Make(*final_config)); |
| 65 | + ICEBERG_ASSIGN_OR_RAISE(auto final_paths, ResourcePaths::Make(*final_config)); |
| 66 | + return std::unique_ptr<RestCatalog>(new RestCatalog( |
| 67 | + std::move(final_config), std::move(client), std::move(*final_paths))); |
| 68 | + }; |
| 69 | + default: { |
| 70 | + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); |
| 71 | + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); |
| 72 | + return UnknownError("Error listing namespaces: {}", list_response.error.message); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +RestCatalog::RestCatalog(std::unique_ptr<RestCatalogConfig> config, |
| 78 | + std::unique_ptr<HttpClient> client, ResourcePaths paths) |
| 79 | + : config_(std::move(config)), client_(std::move(client)), paths_(std::move(paths)) {} |
| 80 | + |
| 81 | +std::string_view RestCatalog::name() const { |
| 82 | + auto it = config_->configs().find(std::string(RestCatalogConfig::kName)); |
| 83 | + if (it == config_->configs().end() || it->second.empty()) { |
| 84 | + return {""}; |
| 85 | + } |
| 86 | + return std::string_view(it->second); |
| 87 | +} |
| 88 | + |
| 89 | +Result<std::vector<Namespace>> RestCatalog::ListNamespaces(const Namespace& ns) const { |
| 90 | + const std::string endpoint = paths_.V1Namespaces(); |
| 91 | + std::vector<Namespace> result; |
| 92 | + std::string next_token; |
| 93 | + while (true) { |
| 94 | + cpr::Parameters params; |
| 95 | + if (!ns.levels.empty()) { |
| 96 | + params.Add({std::string(kQueryParamParent), EncodeNamespaceForUrl(ns)}); |
| 97 | + } |
| 98 | + if (!next_token.empty()) { |
| 99 | + params.Add({std::string(kQueryParamPageToken), next_token}); |
| 100 | + } |
| 101 | + ICEBERG_ASSIGN_OR_RAISE(const auto& response, client_->Get(endpoint, params)); |
| 102 | + switch (response.status_code) { |
| 103 | + case cpr::status::HTTP_OK: { |
| 104 | + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); |
| 105 | + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ListNamespacesResponseFromJson(json)); |
| 106 | + result.insert(result.end(), list_response.namespaces.begin(), |
| 107 | + list_response.namespaces.end()); |
| 108 | + if (list_response.next_page_token.empty()) { |
| 109 | + return result; |
| 110 | + } |
| 111 | + next_token = list_response.next_page_token; |
| 112 | + continue; |
| 113 | + } |
| 114 | + case cpr::status::HTTP_NOT_FOUND: { |
| 115 | + return NoSuchNamespace("Namespace not found"); |
| 116 | + } |
| 117 | + default: |
| 118 | + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); |
| 119 | + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); |
| 120 | + return UnknownError("Error listing namespaces: {}", list_response.error.message); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +Status RestCatalog::CreateNamespace( |
| 126 | + const Namespace& ns, const std::unordered_map<std::string, std::string>& properties) { |
| 127 | + return NotImplemented("Not implemented"); |
| 128 | +} |
| 129 | + |
| 130 | +Result<std::unordered_map<std::string, std::string>> RestCatalog::GetNamespaceProperties( |
| 131 | + const Namespace& ns) const { |
| 132 | + return NotImplemented("Not implemented"); |
| 133 | +} |
| 134 | + |
| 135 | +Status RestCatalog::DropNamespace(const Namespace& ns) { |
| 136 | + return NotImplemented("Not implemented"); |
| 137 | +} |
| 138 | + |
| 139 | +Result<bool> RestCatalog::NamespaceExists(const Namespace& ns) const { |
| 140 | + return NotImplemented("Not implemented"); |
| 141 | +} |
| 142 | + |
| 143 | +Status RestCatalog::UpdateNamespaceProperties( |
| 144 | + const Namespace& ns, const std::unordered_map<std::string, std::string>& updates, |
| 145 | + const std::unordered_set<std::string>& removals) { |
| 146 | + return NotImplemented("Not implemented"); |
| 147 | +} |
| 148 | + |
| 149 | +Result<std::vector<TableIdentifier>> RestCatalog::ListTables(const Namespace& ns) const { |
| 150 | + return NotImplemented("Not implemented"); |
| 151 | +} |
| 152 | + |
| 153 | +Result<std::unique_ptr<Table>> RestCatalog::CreateTable( |
| 154 | + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, |
| 155 | + const std::string& location, |
| 156 | + const std::unordered_map<std::string, std::string>& properties) { |
| 157 | + return NotImplemented("Not implemented"); |
| 158 | +} |
| 159 | + |
| 160 | +Result<std::unique_ptr<Table>> RestCatalog::UpdateTable( |
| 161 | + const TableIdentifier& identifier, |
| 162 | + const std::vector<std::unique_ptr<TableRequirement>>& requirements, |
| 163 | + const std::vector<std::unique_ptr<TableUpdate>>& updates) { |
| 164 | + return NotImplemented("Not implemented"); |
| 165 | +} |
| 166 | + |
| 167 | +Result<std::shared_ptr<Transaction>> RestCatalog::StageCreateTable( |
| 168 | + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, |
| 169 | + const std::string& location, |
| 170 | + const std::unordered_map<std::string, std::string>& properties) { |
| 171 | + return NotImplemented("Not implemented"); |
| 172 | +} |
| 173 | + |
| 174 | +Status RestCatalog::DropTable(const TableIdentifier& identifier, bool purge) { |
| 175 | + return NotImplemented("Not implemented"); |
| 176 | +} |
| 177 | + |
| 178 | +Result<bool> RestCatalog::TableExists(const TableIdentifier& identifier) const { |
| 179 | + return NotImplemented("Not implemented"); |
| 180 | +} |
| 181 | + |
| 182 | +Status RestCatalog::RenameTable(const TableIdentifier& from, const TableIdentifier& to) { |
| 183 | + return NotImplemented("Not implemented"); |
| 184 | +} |
| 185 | + |
| 186 | +Result<std::unique_ptr<Table>> RestCatalog::LoadTable(const TableIdentifier& identifier) { |
| 187 | + return NotImplemented("Not implemented"); |
| 188 | +} |
| 189 | + |
| 190 | +Result<std::shared_ptr<Table>> RestCatalog::RegisterTable( |
| 191 | + const TableIdentifier& identifier, const std::string& metadata_file_location) { |
| 192 | + return NotImplemented("Not implemented"); |
| 193 | +} |
| 194 | + |
| 195 | +std::unique_ptr<RestCatalog::TableBuilder> RestCatalog::BuildTable( |
| 196 | + const TableIdentifier& identifier, const Schema& schema) const { |
| 197 | + return nullptr; |
| 198 | +} |
| 199 | + |
| 200 | +} // namespace iceberg::rest |
0 commit comments