Skip to content

Commit 534d64f

Browse files
committed
add Status data structure
Adpated from Apache Arrow with lot of simplification. Signed-off-by: Junwang Zhao <[email protected]>
1 parent c8d8af9 commit 534d64f

File tree

9 files changed

+675
-2
lines changed

9 files changed

+675
-2
lines changed

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ project(Iceberg
2828
DESCRIPTION "Iceberg C++ Project"
2929
LANGUAGES CXX)
3030

31+
include(FetchContent)
32+
3133
set(CMAKE_CXX_STANDARD 20)
3234
set(CMAKE_CXX_STANDARD_REQUIRED ON)
3335
set(CMAKE_CXX_EXTENSIONS OFF)
@@ -62,6 +64,11 @@ include(GenerateExportHeader)
6264
add_subdirectory(src)
6365

6466
if(ICEBERG_BUILD_TESTS)
67+
fetchcontent_declare(googletest
68+
GIT_REPOSITORY https://github.com/google/googletest.git
69+
GIT_TAG b514bdc898e2951020cbdca1304b75f5950d1f59 # release-1.15.2
70+
)
71+
fetchcontent_makeavailable(googletest)
6572
enable_testing()
6673
add_subdirectory(test)
6774
endif()

src/iceberg/CMakeLists.txt

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

18-
set(ICEBERG_SOURCES demo_table.cc)
18+
set(ICEBERG_SOURCES demo_table.cc status.cc)
1919

2020
add_iceberg_lib(iceberg
2121
SOURCES

src/iceberg/status.cc

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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/status.h"
21+
22+
#include <cassert>
23+
#include <cstdlib>
24+
#include <iostream>
25+
#include <sstream>
26+
27+
namespace iceberg {
28+
29+
Status::Status(StatusCode code, const std::string& msg)
30+
: Status::Status(code, msg, nullptr) {}
31+
32+
Status::Status(StatusCode code, std::string msg, std::shared_ptr<StatusDetail> detail) {
33+
state_ = new State{code, std::move(msg), std::move(detail)};
34+
}
35+
36+
void Status::CopyFrom(const Status& s) {
37+
if (ICEBERG_PREDICT_FALSE(state_ != nullptr)) {
38+
DeleteState();
39+
}
40+
if (s.state_ == nullptr) {
41+
state_ = nullptr;
42+
} else {
43+
state_ = new State(*s.state_);
44+
}
45+
}
46+
47+
std::string Status::CodeAsString() const {
48+
if (state_ == nullptr) {
49+
return "OK";
50+
}
51+
return CodeAsString(code());
52+
}
53+
54+
std::string Status::CodeAsString(StatusCode code) {
55+
const char* type;
56+
switch (code) {
57+
case StatusCode::OK:
58+
type = "OK";
59+
break;
60+
case StatusCode::IOError:
61+
type = "IOError";
62+
break;
63+
case StatusCode::NotImplemented:
64+
type = "NotImplemented";
65+
break;
66+
case StatusCode::UnknownError:
67+
type = "Unknown error";
68+
break;
69+
default:
70+
type = "Unknown";
71+
break;
72+
}
73+
return std::string(type);
74+
}
75+
76+
std::string Status::ToString() const {
77+
std::ostringstream oss;
78+
oss << CodeAsString();
79+
if (state_ == nullptr) {
80+
return oss.str();
81+
}
82+
oss << ": ";
83+
oss << state_->msg;
84+
if (state_->detail != nullptr) {
85+
oss << ". Detail: " << state_->detail->ToString();
86+
}
87+
88+
return oss.str();
89+
}
90+
91+
void Status::Warn() const { std::cerr << *this; }
92+
93+
void Status::Warn(const std::string& message) const {
94+
std::cerr << message << ": " << *this;
95+
}
96+
97+
} // namespace iceberg

0 commit comments

Comments
 (0)