|
| 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/http_client_interal.h" |
| 29 | +#include "iceberg/catalog/rest/json_internal.h" |
| 30 | +#include "iceberg/catalog/rest/types.h" |
| 31 | +#include "iceberg/catalog/rest/util.h" |
| 32 | +#include "iceberg/json_internal.h" |
| 33 | +#include "iceberg/result.h" |
| 34 | +#include "iceberg/table.h" |
| 35 | +#include "iceberg/util/macros.h" |
| 36 | + |
| 37 | +namespace iceberg::rest { |
| 38 | + |
| 39 | +Result<RestCatalog> RestCatalog::Make(RestCatalogConfig config) { |
| 40 | + // Validate that uri is not empty |
| 41 | + if (config.uri.empty()) { |
| 42 | + return InvalidArgument("Rest catalog configuration property 'uri' is required."); |
| 43 | + } |
| 44 | + ICEBERG_ASSIGN_OR_RAISE(auto tmp_client, HttpClient::Make(config)); |
| 45 | + const std::string endpoint = config.GetConfigEndpoint(); |
| 46 | + cpr::Parameters params; |
| 47 | + if (config.warehouse.has_value()) { |
| 48 | + params.Add({"warehouse", config.warehouse.value()}); |
| 49 | + } |
| 50 | + ICEBERG_ASSIGN_OR_RAISE(const auto& response, tmp_client->Get(endpoint, params)); |
| 51 | + switch (response.status_code) { |
| 52 | + case cpr::status::HTTP_OK: { |
| 53 | + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); |
| 54 | + ICEBERG_ASSIGN_OR_RAISE(auto server_config, CatalogConfigFromJson(json)); |
| 55 | + // Merge server config into client config, server config overrides > client config |
| 56 | + // properties > server config defaults |
| 57 | + auto final_props = std::move(server_config.defaults); |
| 58 | + for (const auto& kv : config.properties_) { |
| 59 | + final_props.insert_or_assign(kv.first, kv.second); |
| 60 | + } |
| 61 | + |
| 62 | + for (const auto& kv : server_config.overrides) { |
| 63 | + final_props.insert_or_assign(kv.first, kv.second); |
| 64 | + } |
| 65 | + RestCatalogConfig final_config = { |
| 66 | + .uri = config.uri, |
| 67 | + .name = config.name, |
| 68 | + .warehouse = config.warehouse, |
| 69 | + .properties_ = std::move(final_props), |
| 70 | + }; |
| 71 | + ICEBERG_ASSIGN_OR_RAISE(auto client, HttpClient::Make(final_config)); |
| 72 | + return RestCatalog(std::make_shared<RestCatalogConfig>(std::move(final_config)), |
| 73 | + std::move(client)); |
| 74 | + }; |
| 75 | + default: { |
| 76 | + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); |
| 77 | + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); |
| 78 | + return UnknownError("Error listing namespaces: {}", list_response.error.message); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +RestCatalog::RestCatalog(std::shared_ptr<RestCatalogConfig> config, |
| 84 | + std::unique_ptr<HttpClient> client) |
| 85 | + : config_(std::move(config)), client_(std::move(client)) {} |
| 86 | + |
| 87 | +std::string_view RestCatalog::name() const { |
| 88 | + return config_->name.has_value() ? std::string_view(*config_->name) |
| 89 | + : std::string_view(""); |
| 90 | +} |
| 91 | + |
| 92 | +Result<std::vector<Namespace>> RestCatalog::ListNamespaces(const Namespace& ns) const { |
| 93 | + const std::string endpoint = config_->GetNamespacesEndpoint(); |
| 94 | + std::vector<Namespace> result; |
| 95 | + std::string next_token; |
| 96 | + while (true) { |
| 97 | + cpr::Parameters params; |
| 98 | + if (!ns.levels.empty()) { |
| 99 | + params.Add({"parent", EncodeNamespaceForUrl(ns)}); |
| 100 | + } |
| 101 | + if (!next_token.empty()) { |
| 102 | + params.Add({"page_token", next_token}); |
| 103 | + } |
| 104 | + ICEBERG_ASSIGN_OR_RAISE(const auto& response, client_->Get(endpoint, params)); |
| 105 | + switch (response.status_code) { |
| 106 | + case cpr::status::HTTP_OK: { |
| 107 | + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); |
| 108 | + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ListNamespacesResponseFromJson(json)); |
| 109 | + result.insert(result.end(), list_response.namespaces.begin(), |
| 110 | + list_response.namespaces.end()); |
| 111 | + if (list_response.next_page_token.empty()) { |
| 112 | + return result; |
| 113 | + } |
| 114 | + next_token = list_response.next_page_token; |
| 115 | + continue; |
| 116 | + } |
| 117 | + case cpr::status::HTTP_NOT_FOUND: { |
| 118 | + return NoSuchNamespace("Namespace not found"); |
| 119 | + } |
| 120 | + default: |
| 121 | + ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(response.text)); |
| 122 | + ICEBERG_ASSIGN_OR_RAISE(auto list_response, ErrorResponseFromJson(json)); |
| 123 | + return UnknownError("Error listing namespaces: {}", list_response.error.message); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +Status RestCatalog::CreateNamespace( |
| 129 | + const Namespace& ns, const std::unordered_map<std::string, std::string>& properties) { |
| 130 | + return NotImplemented("Not implemented"); |
| 131 | +} |
| 132 | + |
| 133 | +Result<std::unordered_map<std::string, std::string>> RestCatalog::GetNamespaceProperties( |
| 134 | + const Namespace& ns) const { |
| 135 | + return NotImplemented("Not implemented"); |
| 136 | +} |
| 137 | + |
| 138 | +Status RestCatalog::DropNamespace(const Namespace& ns) { |
| 139 | + return NotImplemented("Not implemented"); |
| 140 | +} |
| 141 | + |
| 142 | +Result<bool> RestCatalog::NamespaceExists(const Namespace& ns) const { |
| 143 | + return NotImplemented("Not implemented"); |
| 144 | +} |
| 145 | + |
| 146 | +Status RestCatalog::UpdateNamespaceProperties( |
| 147 | + const Namespace& ns, const std::unordered_map<std::string, std::string>& updates, |
| 148 | + const std::unordered_set<std::string>& removals) { |
| 149 | + return NotImplemented("Not implemented"); |
| 150 | +} |
| 151 | + |
| 152 | +Result<std::vector<TableIdentifier>> RestCatalog::ListTables(const Namespace& ns) const { |
| 153 | + return NotImplemented("Not implemented"); |
| 154 | +} |
| 155 | + |
| 156 | +Result<std::unique_ptr<Table>> RestCatalog::CreateTable( |
| 157 | + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, |
| 158 | + const std::string& location, |
| 159 | + const std::unordered_map<std::string, std::string>& properties) { |
| 160 | + return NotImplemented("Not implemented"); |
| 161 | +} |
| 162 | + |
| 163 | +Result<std::unique_ptr<Table>> RestCatalog::UpdateTable( |
| 164 | + const TableIdentifier& identifier, |
| 165 | + const std::vector<std::unique_ptr<TableRequirement>>& requirements, |
| 166 | + const std::vector<std::unique_ptr<TableUpdate>>& updates) { |
| 167 | + return NotImplemented("Not implemented"); |
| 168 | +} |
| 169 | + |
| 170 | +Result<std::shared_ptr<Transaction>> RestCatalog::StageCreateTable( |
| 171 | + const TableIdentifier& identifier, const Schema& schema, const PartitionSpec& spec, |
| 172 | + const std::string& location, |
| 173 | + const std::unordered_map<std::string, std::string>& properties) { |
| 174 | + return NotImplemented("Not implemented"); |
| 175 | +} |
| 176 | + |
| 177 | +Status RestCatalog::DropTable(const TableIdentifier& identifier, bool purge) { |
| 178 | + return NotImplemented("Not implemented"); |
| 179 | +} |
| 180 | + |
| 181 | +Result<bool> RestCatalog::TableExists(const TableIdentifier& identifier) const { |
| 182 | + return NotImplemented("Not implemented"); |
| 183 | +} |
| 184 | + |
| 185 | +Result<std::unique_ptr<Table>> RestCatalog::LoadTable(const TableIdentifier& identifier) { |
| 186 | + return NotImplemented("Not implemented"); |
| 187 | +} |
| 188 | + |
| 189 | +Result<std::shared_ptr<Table>> RestCatalog::RegisterTable( |
| 190 | + const TableIdentifier& identifier, const std::string& metadata_file_location) { |
| 191 | + return NotImplemented("Not implemented"); |
| 192 | +} |
| 193 | + |
| 194 | +std::unique_ptr<RestCatalog::TableBuilder> RestCatalog::BuildTable( |
| 195 | + const TableIdentifier& identifier, const Schema& schema) const { |
| 196 | + return nullptr; |
| 197 | +} |
| 198 | + |
| 199 | +} // namespace iceberg::rest |
0 commit comments