Skip to content

Commit e7509f2

Browse files
authored
refactor: use ICEBERG_CHECK where possible (#244)
This will simplify the code and potentially optimize the binary due to the [[unlikely]] attribute in ICEBERG_CHECK.
1 parent 17b5b2d commit e7509f2

File tree

3 files changed

+21
-41
lines changed

3 files changed

+21
-41
lines changed

src/iceberg/avro/avro_stream_internal.cc

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,8 @@ bool AvroInputStream::next(const uint8_t** data, size_t* len) {
6666
}
6767

6868
void AvroInputStream::backup(size_t len) {
69-
if (len > buffer_pos_) {
70-
throw IcebergError(
71-
std::format("Cannot backup {} bytes, only {} bytes available", len, buffer_pos_));
72-
}
69+
ICEBERG_CHECK(len <= buffer_pos_, "Cannot backup {} bytes, only {} bytes available",
70+
len, buffer_pos_);
7371

7472
buffer_pos_ -= len;
7573
byte_count_ -= len;
@@ -90,10 +88,7 @@ size_t AvroInputStream::byteCount() const { return byte_count_; }
9088

9189
void AvroInputStream::seek(int64_t position) {
9290
auto status = input_stream_->Seek(position);
93-
if (!status.ok()) {
94-
throw IcebergError(
95-
std::format("Failed to seek to {}, got {}", position, status.ToString()));
96-
}
91+
ICEBERG_CHECK(status.ok(), "Failed to seek to {}, got {}", position, status.ToString());
9792

9893
buffer_pos_ = 0;
9994
available_bytes_ = 0;
@@ -121,10 +116,8 @@ bool AvroOutputStream::next(uint8_t** data, size_t* len) {
121116
}
122117

123118
void AvroOutputStream::backup(size_t len) {
124-
if (len > buffer_pos_) {
125-
throw IcebergError(
126-
std::format("Cannot backup {} bytes, only {} bytes available", len, buffer_pos_));
127-
}
119+
ICEBERG_CHECK(len <= buffer_pos_, "Cannot backup {} bytes, only {} bytes available",
120+
len, buffer_pos_);
128121
buffer_pos_ -= len;
129122
}
130123

@@ -133,16 +126,12 @@ uint64_t AvroOutputStream::byteCount() const { return flushed_bytes_ + buffer_po
133126
void AvroOutputStream::flush() {
134127
if (buffer_pos_ > 0) {
135128
auto status = output_stream_->Write(buffer_.data(), buffer_pos_);
136-
if (!status.ok()) {
137-
throw IcebergError(std::format("Write failed {}", status.ToString()));
138-
}
129+
ICEBERG_CHECK(status.ok(), "Write failed {}", status.ToString());
139130
flushed_bytes_ += buffer_pos_;
140131
buffer_pos_ = 0;
141132
}
142133
auto status = output_stream_->Flush();
143-
if (!status.ok()) {
144-
throw IcebergError(std::format("Flush failed {}", status.ToString()));
145-
}
134+
ICEBERG_CHECK(status.ok(), "Flush failed {}", status.ToString());
146135
}
147136

148137
const std::shared_ptr<::arrow::io::OutputStream>& AvroOutputStream::arrow_output_stream()

src/iceberg/type.cc

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,9 @@ Status StructType::InitFieldByLowerCaseName() const {
133133
}
134134

135135
ListType::ListType(SchemaField element) : element_(std::move(element)) {
136-
if (element_.name() != kElementName) {
137-
throw IcebergError(std::format("ListType: child field name should be '{}', was '{}'",
138-
kElementName, element_.name()));
139-
}
136+
ICEBERG_CHECK(element_.name() == kElementName,
137+
"ListType: child field name should be '{}', was '{}'", kElementName,
138+
element_.name());
140139
}
141140

142141
ListType::ListType(int32_t field_id, std::shared_ptr<Type> type, bool optional)
@@ -189,14 +188,12 @@ bool ListType::Equals(const Type& other) const {
189188

190189
MapType::MapType(SchemaField key, SchemaField value)
191190
: fields_{std::move(key), std::move(value)} {
192-
if (this->key().name() != kKeyName) {
193-
throw IcebergError(std::format("MapType: key field name should be '{}', was '{}'",
194-
kKeyName, this->key().name()));
195-
}
196-
if (this->value().name() != kValueName) {
197-
throw IcebergError(std::format("MapType: value field name should be '{}', was '{}'",
198-
kValueName, this->value().name()));
199-
}
191+
ICEBERG_CHECK(this->key().name() == kKeyName,
192+
"MapType: key field name should be '{}', was '{}'", kKeyName,
193+
this->key().name());
194+
ICEBERG_CHECK(this->value().name() == kValueName,
195+
"MapType: value field name should be '{}', was '{}'", kValueName,
196+
this->value().name());
200197
}
201198

202199
const SchemaField& MapType::key() const { return fields_[0]; }
@@ -278,10 +275,8 @@ bool DoubleType::Equals(const Type& other) const { return other.type_id() == kTy
278275

279276
DecimalType::DecimalType(int32_t precision, int32_t scale)
280277
: precision_(precision), scale_(scale) {
281-
if (precision < 0 || precision > kMaxPrecision) {
282-
throw IcebergError(
283-
std::format("DecimalType: precision must be in [0, 38], was {}", precision));
284-
}
278+
ICEBERG_CHECK(precision >= 0 && precision <= kMaxPrecision,
279+
"DecimalType: precision must be in [0, 38], was {}", precision);
285280
}
286281

287282
int32_t DecimalType::precision() const { return precision_; }
@@ -329,9 +324,7 @@ std::string UuidType::ToString() const { return "uuid"; }
329324
bool UuidType::Equals(const Type& other) const { return other.type_id() == kTypeId; }
330325

331326
FixedType::FixedType(int32_t length) : length_(length) {
332-
if (length < 0) {
333-
throw IcebergError(std::format("FixedType: length must be >= 0, was {}", length));
334-
}
327+
ICEBERG_CHECK(length >= 0, "FixedType: length must be >= 0, was {}", length);
335328
}
336329

337330
int32_t FixedType::length() const { return length_; }

src/iceberg/util/decimal.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,8 @@ bool RescaleWouldCauseDataLoss(const Decimal& value, int32_t delta_scale,
283283

284284
Decimal::Decimal(std::string_view str) {
285285
auto result = Decimal::FromString(str);
286-
if (!result) {
287-
throw IcebergError(std::format("Failed to parse Decimal from string: {}, error: {}",
288-
str, result.error().message));
289-
}
286+
ICEBERG_CHECK(result, "Failed to parse Decimal from string: {}, error: {}", str,
287+
result.error().message);
290288
*this = std::move(result.value());
291289
}
292290

0 commit comments

Comments
 (0)