Skip to content

Commit 3669b05

Browse files
authored
refactor: Restructure catalog module (#230)
Reorganizes the catalog module structure to better support the upcoming REST catalog implementation. Changes: - Create a new `memory` subdirectory within `iceberg-cpp/src/iceberg/catalog/`. Move the existing `in_memory_catalog.cc` and `in_memory_catalog.h` files into this new memory subdirectory. - Add `json` dependencies for the rest package to support serialization and deserialization of data. - Add the `cpp-httplib` dependency to the rest test code. This library will be used to simply simulate an Iceberg REST server, enabling tests of whether the RestCatalog client can communicate with it correctly and handle success and failure scenarios. Hint: - This PR depends on the dependencies introduced in and should be merged after #236. closes #229 .
1 parent 622a934 commit 3669b05

File tree

9 files changed

+152
-5
lines changed

9 files changed

+152
-5
lines changed

example/demo_example.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
#include "iceberg/arrow/arrow_file_io.h"
2323
#include "iceberg/avro/avro_register.h"
24-
#include "iceberg/catalog/in_memory_catalog.h"
24+
#include "iceberg/catalog/memory/in_memory_catalog.h"
2525
#include "iceberg/parquet/parquet_register.h"
2626
#include "iceberg/table.h"
2727
#include "iceberg/table_scan.h"

src/iceberg/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
set(ICEBERG_INCLUDES "$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/src>"
1919
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>")
2020
set(ICEBERG_SOURCES
21-
catalog/in_memory_catalog.cc
21+
catalog/memory/in_memory_catalog.cc
2222
expression/expression.cc
2323
expression/expressions.cc
2424
expression/literal.cc

src/iceberg/catalog/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
iceberg_install_all_headers(iceberg/catalog)
1919

20+
add_subdirectory(memory)
21+
2022
if(ICEBERG_BUILD_REST)
2123
add_subdirectory(rest)
2224
endif()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
iceberg_install_all_headers(iceberg/catalog/memory)

src/iceberg/catalog/in_memory_catalog.cc renamed to src/iceberg/catalog/memory/in_memory_catalog.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* under the License.
1818
*/
1919

20-
#include "iceberg/catalog/in_memory_catalog.h"
20+
#include "iceberg/catalog/memory/in_memory_catalog.h"
2121

2222
#include <algorithm>
2323
#include <iterator> // IWYU pragma: keep

src/iceberg/test/CMakeLists.txt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,23 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18+
fetchcontent_declare(cpp-httplib
19+
GIT_REPOSITORY https://github.com/yhirose/cpp-httplib.git
20+
GIT_TAG 89c932f313c6437c38f2982869beacc89c2f2246 #release-0.26.0
21+
)
22+
1823
fetchcontent_declare(googletest
1924
GIT_REPOSITORY https://github.com/google/googletest.git
2025
GIT_TAG b514bdc898e2951020cbdca1304b75f5950d1f59 # release-1.15.2
2126
FIND_PACKAGE_ARGS
2227
NAMES
2328
GTest)
24-
fetchcontent_makeavailable(googletest)
29+
30+
if(ICEBERG_BUILD_REST)
31+
fetchcontent_makeavailable(cpp-httplib googletest)
32+
else()
33+
fetchcontent_makeavailable(googletest)
34+
endif()
2535

2636
set(ICEBERG_TEST_RESOURCES "${CMAKE_SOURCE_DIR}/src/iceberg/test/resources")
2737

@@ -136,3 +146,9 @@ if(ICEBERG_BUILD_BUNDLE)
136146
add_iceberg_test(scan_test USE_BUNDLE SOURCES file_scan_task_test.cc)
137147

138148
endif()
149+
150+
if(ICEBERG_BUILD_REST)
151+
add_iceberg_test(rest_catalog_test SOURCES rest_catalog_test.cc)
152+
target_link_libraries(rest_catalog_test PRIVATE iceberg_rest_static)
153+
target_include_directories(rest_catalog_test PRIVATE ${cpp-httplib_SOURCE_DIR})
154+
endif()

src/iceberg/test/in_memory_catalog_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* under the License.
1818
*/
1919

20-
#include "iceberg/catalog/in_memory_catalog.h"
20+
#include "iceberg/catalog/memory/in_memory_catalog.h"
2121

2222
#include <filesystem>
2323

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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/rest_catalog.h"
21+
22+
#include <httplib.h>
23+
24+
#include <memory>
25+
#include <thread>
26+
27+
#include <gmock/gmock.h>
28+
#include <gtest/gtest.h>
29+
#include <nlohmann/json.hpp>
30+
31+
namespace iceberg::catalog::rest {
32+
33+
class RestCatalogIntegrationTest : public ::testing::Test {
34+
protected:
35+
void SetUp() override {
36+
server_ = std::make_unique<httplib::Server>();
37+
port_ = server_->bind_to_any_port("127.0.0.1");
38+
39+
server_thread_ = std::thread([this]() { server_->listen_after_bind(); });
40+
}
41+
42+
void TearDown() override {
43+
server_->stop();
44+
if (server_thread_.joinable()) {
45+
server_thread_.join();
46+
}
47+
}
48+
49+
std::unique_ptr<httplib::Server> server_;
50+
int port_ = -1;
51+
std::thread server_thread_;
52+
};
53+
54+
TEST_F(RestCatalogIntegrationTest, GetConfigSuccessfully) {
55+
server_->Get("/v1/config", [](const httplib::Request&, httplib::Response& res) {
56+
res.status = 200;
57+
res.set_content(R"({"warehouse": "s3://test-bucket"})", "application/json");
58+
});
59+
60+
std::string base_uri = "http://127.0.0.1:" + std::to_string(port_);
61+
RestCatalog catalog(base_uri);
62+
cpr::Response response = catalog.GetConfig();
63+
64+
ASSERT_EQ(response.error.code, cpr::ErrorCode::OK);
65+
ASSERT_EQ(response.status_code, 200);
66+
67+
auto json_body = nlohmann::json::parse(response.text);
68+
EXPECT_EQ(json_body["warehouse"], "s3://test-bucket");
69+
}
70+
71+
TEST_F(RestCatalogIntegrationTest, ListNamespacesReturnsMultipleResults) {
72+
server_->Get("/v1/namespaces", [](const httplib::Request&, httplib::Response& res) {
73+
res.status = 200;
74+
res.set_content(R"({
75+
"namespaces": [
76+
["accounting", "db"],
77+
["production", "db"]
78+
]
79+
})",
80+
"application/json");
81+
});
82+
83+
std::string base_uri = "http://127.0.0.1:" + std::to_string(port_);
84+
RestCatalog catalog(base_uri);
85+
cpr::Response response = catalog.ListNamespaces();
86+
87+
ASSERT_EQ(response.error.code, cpr::ErrorCode::OK);
88+
ASSERT_EQ(response.status_code, 200);
89+
90+
auto json_body = nlohmann::json::parse(response.text);
91+
ASSERT_TRUE(json_body.contains("namespaces"));
92+
EXPECT_EQ(json_body["namespaces"].size(), 2);
93+
EXPECT_THAT(json_body["namespaces"][0][0], "accounting");
94+
}
95+
96+
TEST_F(RestCatalogIntegrationTest, HandlesServerError) {
97+
server_->Get("/v1/config", [](const httplib::Request&, httplib::Response& res) {
98+
res.status = 500;
99+
res.set_content("Internal Server Error", "text/plain");
100+
});
101+
102+
std::string base_uri = "http://127.0.0.1:" + std::to_string(port_);
103+
RestCatalog catalog(base_uri);
104+
cpr::Response response = catalog.GetConfig();
105+
106+
ASSERT_EQ(response.error.code, cpr::ErrorCode::OK);
107+
ASSERT_EQ(response.status_code, 500);
108+
ASSERT_EQ(response.text, "Internal Server Error");
109+
}
110+
111+
} // namespace iceberg::catalog::rest

0 commit comments

Comments
 (0)