Skip to content

Commit 1319f52

Browse files
committed
simplify status code
Signed-off-by: Junwang Zhao <[email protected]>
1 parent e25c533 commit 1319f52

File tree

12 files changed

+17
-264
lines changed

12 files changed

+17
-264
lines changed

api/iceberg/status.h

Lines changed: 10 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
#pragma once
1616

1717
#include <cstring>
18+
#include <format>
1819
#include <iosfwd>
1920
#include <memory>
2021
#include <string>
2122
#include <utility>
2223

2324
#include "util/compare.h"
2425
#include "util/macros.h"
25-
#include "util/string_builder.h"
26+
#include "util/to_string_ostreamable.h"
2627
#include "util/visibility.h"
2728

2829
#ifdef ICEBERG_EXTRA_ERROR_CONTEXT
@@ -86,16 +87,10 @@ namespace iceberg {
8687
enum class StatusCode : char {
8788
OK = 0,
8889
OutOfMemory = 1,
89-
KeyError = 2,
90-
TypeError = 3,
91-
Invalid = 4,
92-
IOError = 5,
93-
CapacityError = 6,
94-
IndexError = 7,
95-
Cancelled = 8,
96-
NotImplemented = 9,
97-
SerializationError = 10,
98-
AlreadyExists = 11,
90+
TypeError = 2,
91+
Invalid = 3,
92+
IOError = 4,
93+
NotImplemented = 5,
9994
UnknownError = 127
10095
};
10196

@@ -160,14 +155,14 @@ class ICEBERG_EXPORT [[nodiscard]] Status : public util::EqualityComparable<Stat
160155
static Status OK() { return Status(); }
161156

162157
template <typename... Args>
163-
static Status FromArgs(StatusCode code, Args&&... args) {
164-
return Status(code, util::StringBuilder(std::forward<Args>(args)...));
158+
static Status FromArgs(StatusCode code, std::string_view fmt, Args&&... args) {
159+
return Status(code, std::vformat(fmt, std::make_format_args(args...)));
165160
}
166161

167162
template <typename... Args>
168163
static Status FromDetailAndArgs(StatusCode code, std::shared_ptr<StatusDetail> detail,
169-
Args&&... args) {
170-
return Status(code, util::StringBuilder(std::forward<Args>(args)...),
164+
std::string_view fmt, Args&&... args) {
165+
return Status(code, std::vformat(fmt, std::make_format_args(args...)),
171166
std::move(detail));
172167
}
173168

@@ -177,12 +172,6 @@ class ICEBERG_EXPORT [[nodiscard]] Status : public util::EqualityComparable<Stat
177172
return Status::FromArgs(StatusCode::OutOfMemory, std::forward<Args>(args)...);
178173
}
179174

180-
/// Return an error status for failed key lookups (e.g. column name in a table)
181-
template <typename... Args>
182-
static Status KeyError(Args&&... args) {
183-
return Status::FromArgs(StatusCode::KeyError, std::forward<Args>(args)...);
184-
}
185-
186175
/// Return an error status for type errors (such as mismatching data types)
187176
template <typename... Args>
188177
static Status TypeError(Args&&... args) {
@@ -201,43 +190,13 @@ class ICEBERG_EXPORT [[nodiscard]] Status : public util::EqualityComparable<Stat
201190
return Status::FromArgs(StatusCode::IOError, std::forward<Args>(args)...);
202191
}
203192

204-
/// Return an error status when a container's capacity would exceed its limits
205-
template <typename... Args>
206-
static Status CapacityError(Args&&... args) {
207-
return Status::FromArgs(StatusCode::CapacityError, std::forward<Args>(args)...);
208-
}
209-
210-
/// Return an error status when an index is out of bounds
211-
template <typename... Args>
212-
static Status IndexError(Args&&... args) {
213-
return Status::FromArgs(StatusCode::IndexError, std::forward<Args>(args)...);
214-
}
215-
216-
/// Return an error status for cancelled operation
217-
template <typename... Args>
218-
static Status Cancelled(Args&&... args) {
219-
return Status::FromArgs(StatusCode::Cancelled, std::forward<Args>(args)...);
220-
}
221-
222193
/// Return an error status when an operation or a combination of operation and
223194
/// data types is unimplemented
224195
template <typename... Args>
225196
static Status NotImplemented(Args&&... args) {
226197
return Status::FromArgs(StatusCode::NotImplemented, std::forward<Args>(args)...);
227198
}
228199

229-
/// Return an error status when some (de)serialization operation failed
230-
template <typename... Args>
231-
static Status SerializationError(Args&&... args) {
232-
return Status::FromArgs(StatusCode::SerializationError, std::forward<Args>(args)...);
233-
}
234-
235-
/// Return an error status for already exists errors
236-
template <typename... Args>
237-
static Status AlreadyExists(Args&&... args) {
238-
return Status::FromArgs(StatusCode::AlreadyExists, std::forward<Args>(args)...);
239-
}
240-
241200
/// Return an error status for unknown errors
242201
template <typename... Args>
243202
static Status UnknownError(Args&&... args) {
@@ -249,27 +208,14 @@ class ICEBERG_EXPORT [[nodiscard]] Status : public util::EqualityComparable<Stat
249208

250209
/// Return true iff the status indicates an out-of-memory error.
251210
constexpr bool IsOutOfMemory() const { return code() == StatusCode::OutOfMemory; }
252-
/// Return true iff the status indicates a key lookup error.
253-
constexpr bool IsKeyError() const { return code() == StatusCode::KeyError; }
254211
/// Return true iff the status indicates a type error.
255212
constexpr bool IsTypeError() const { return code() == StatusCode::TypeError; }
256213
/// Return true iff the status indicates invalid data.
257214
constexpr bool IsInvalid() const { return code() == StatusCode::Invalid; }
258215
/// Return true iff the status indicates an IO-related failure.
259216
constexpr bool IsIOError() const { return code() == StatusCode::IOError; }
260-
/// Return true iff the status indicates a container reaching capacity limits.
261-
constexpr bool IsCapacityError() const { return code() == StatusCode::CapacityError; }
262-
/// Return true iff the status indicates an out of bounds index.
263-
constexpr bool IsIndexError() const { return code() == StatusCode::IndexError; }
264-
/// Return true iff the status indicates a cancelled operation.
265-
constexpr bool IsCancelled() const { return code() == StatusCode::Cancelled; }
266217
/// Return true iff the status indicates an unimplemented operation.
267218
constexpr bool IsNotImplemented() const { return code() == StatusCode::NotImplemented; }
268-
/// Return true iff the status indicates a (de)serialization failure
269-
constexpr bool IsSerializationError() const {
270-
return code() == StatusCode::SerializationError;
271-
}
272-
constexpr bool IsAlreadyExists() const { return code() == StatusCode::AlreadyExists; }
273219
/// Return true iff the status indicates an unknown error.
274220
constexpr bool IsUnknownError() const { return code() == StatusCode::UnknownError; }
275221

@@ -278,12 +224,6 @@ class ICEBERG_EXPORT [[nodiscard]] Status : public util::EqualityComparable<Stat
278224
/// The string "OK" is returned for success.
279225
std::string ToString() const;
280226

281-
/// \brief Return a string representation of this status without
282-
/// context lines suitable for printing.
283-
///
284-
/// The string "OK" is returned for success.
285-
std::string ToStringWithoutContextLines() const;
286-
287227
/// \brief Return a string representation of the status code, without the message
288228
/// text or POSIX code information.
289229
std::string CodeAsString() const;
@@ -320,9 +260,6 @@ class ICEBERG_EXPORT [[nodiscard]] Status : public util::EqualityComparable<Stat
320260
void Warn() const;
321261
void Warn(const std::string& message) const;
322262

323-
[[noreturn]] void Abort() const;
324-
[[noreturn]] void Abort(const std::string& message) const;
325-
326263
#ifdef ICEBERG_EXTRA_ERROR_CONTEXT
327264
void AddContextLine(const char* filename, int line, const char* expr);
328265
#endif

api/iceberg/util/macros.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,6 @@
178178
# define ICEBERG_DISABLE_UBSAN(feature)
179179
#endif
180180

181-
// ----------------------------------------------------------------------
182-
// Machine information
183-
184-
#if INTPTR_MAX == INT64_MAX
185-
# define ICEBERG_BITNESS 64
186-
#elif INTPTR_MAX == INT32_MAX
187-
# define ICEBERG_BITNESS 32
188-
#else
189-
# error Unexpected INTPTR_MAX
190-
#endif
191181

192182
// ----------------------------------------------------------------------
193183
// From googletest

api/iceberg/util/string_builder.h renamed to api/iceberg/util/to_string_ostreamable.h

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,40 +28,6 @@ namespace iceberg {
2828

2929
namespace util {
3030

31-
namespace detail {
32-
33-
class ICEBERG_EXPORT StringStreamWrapper {
34-
public:
35-
StringStreamWrapper();
36-
~StringStreamWrapper();
37-
38-
std::ostream& stream() { return ostream_; }
39-
std::string str();
40-
41-
protected:
42-
std::unique_ptr<std::ostringstream> sstream_;
43-
std::ostream& ostream_;
44-
};
45-
} // namespace detail
46-
47-
/// Variadic templates
48-
template <typename Head>
49-
void StringBuilderRecursive(std::ostream& os, Head&& head) {
50-
os << head;
51-
}
52-
template <typename Head, typename... Tail>
53-
void StringBuilderRecursive(std::ostream& os, Head&& head, Tail&&... tail) {
54-
StringBuilderRecursive(os, std::forward<Head>(head));
55-
StringBuilderRecursive(os, std::forward<Tail>(tail)...);
56-
}
57-
58-
template <typename... Args>
59-
std::string StringBuilder(Args&&... args) {
60-
detail::StringStreamWrapper ss;
61-
StringBuilderRecursive(ss.stream(), std::forward<Args>(args)...);
62-
return ss.str();
63-
}
64-
6531
/// CRTP helper for declaring string representaion. Defines operator<<
6632
template <typename T>
6733
class ToStringOstreamable {

src/CMakeLists.txt

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

18-
add_subdirectory(common)
1918
add_subdirectory(core)
2019
add_subdirectory(puffin)

src/common/CMakeLists.txt

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/common/util/string_builder.cc

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/core/CMakeLists.txt

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

18-
set(ICEBERG_CORE_SOURCES demo_table.cc)
18+
set(ICEBERG_CORE_SOURCES demo_table.cc status.cc)
1919
set(ICEBERG_CORE_INCLUDES "${ICEBERG_API_DIR}")
20-
set(ICEBERG_CORE_STATIC_LINK_LIBS iceberg_common_static)
21-
set(ICEBERG_CORE_SHARED_LINK_LIBS iceberg_common_shared)
2220

2321
add_iceberg_lib(iceberg_core
2422
CMAKE_PACKAGE_NAME
2523
iceberg
2624
SOURCES
2725
${ICEBERG_CORE_SOURCES}
28-
STATIC_LINK_LIBS
29-
${ICEBERG_CORE_STATIC_LINK_LIBS}
30-
SHARED_LINK_LIBS
31-
${ICEBERG_CORE_SHARED_LINK_LIBS}
3226
PRIVATE_INCLUDES
3327
${ICEBERG_CORE_INCLUDES})

src/common/status.cc renamed to src/core/status.cc

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ std::string Status::CodeAsString(StatusCode code) {
5959
case StatusCode::OutOfMemory:
6060
type = "Out of memory";
6161
break;
62-
case StatusCode::KeyError:
63-
type = "Key error";
64-
break;
6562
case StatusCode::TypeError:
6663
type = "Type error";
6764
break;
@@ -71,24 +68,9 @@ std::string Status::CodeAsString(StatusCode code) {
7168
case StatusCode::IOError:
7269
type = "IOError";
7370
break;
74-
case StatusCode::CapacityError:
75-
type = "Capacity error";
76-
break;
77-
case StatusCode::IndexError:
78-
type = "Index error";
79-
break;
80-
case StatusCode::Cancelled:
81-
type = "Cancelled";
82-
break;
8371
case StatusCode::NotImplemented:
8472
type = "NotImplemented";
8573
break;
86-
case StatusCode::SerializationError:
87-
type = "Serialization error";
88-
break;
89-
case StatusCode::AlreadyExists:
90-
type = "Already Exists";
91-
break;
9274
case StatusCode::UnknownError:
9375
type = "Unknown error";
9476
break;
@@ -114,34 +96,6 @@ std::string Status::ToString() const {
11496
return result;
11597
}
11698

117-
std::string Status::ToStringWithoutContextLines() const {
118-
auto message = ToString();
119-
#ifdef ICEBERG_EXTRA_ERROR_CONTEXT
120-
while (true) {
121-
auto last_new_line_position = message.rfind("\n");
122-
if (last_new_line_position == std::string::npos) {
123-
break;
124-
}
125-
if (message.find(":", last_new_line_position) == std::string::npos) {
126-
break;
127-
}
128-
message = message.substr(0, last_new_line_position);
129-
}
130-
#endif
131-
return message;
132-
}
133-
134-
void Status::Abort() const { Abort(std::string()); }
135-
136-
void Status::Abort(const std::string& message) const {
137-
std::cerr << "-- Iceberg Fatal Error --\n";
138-
if (!message.empty()) {
139-
std::cerr << message << "\n";
140-
}
141-
std::cerr << ToString() << std::endl;
142-
std::abort();
143-
}
144-
14599
void Status::Warn() const { std::cerr << ToString(); }
146100

147101
void Status::Warn(const std::string& message) const {

test/CMakeLists.txt

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

18-
add_subdirectory(common)
18+
add_subdirectory(core)

0 commit comments

Comments
 (0)