Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ci/scripts/build_example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mkdir ${build_dir}
pushd ${build_dir}

is_windows() {
[[ "${OSTYPE}" == "msys" || "${OSTYPE}" == "win32" ]]
[[ "${OSTYPE}" == "msys" || "${OSTYPE}" == "win32" || "${OSTYPE}" == "cygwin" ]]
}

CMAKE_ARGS=(
Expand Down
2 changes: 1 addition & 1 deletion ci/scripts/build_iceberg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mkdir ${build_dir}
pushd ${build_dir}

is_windows() {
[[ "${OSTYPE}" == "msys" || "${OSTYPE}" == "win32" ]]
[[ "${OSTYPE}" == "msys" || "${OSTYPE}" == "win32" || "${OSTYPE}" == "cygwin" ]]
}

CMAKE_ARGS=(
Expand Down
1 change: 1 addition & 0 deletions src/iceberg/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ add_iceberg_test(util_test
decimal_test.cc
endian_test.cc
formatter_test.cc
lazy_test.cc
Comment thread
HuaHuaY marked this conversation as resolved.
location_util_test.cc
Comment thread
HuaHuaY marked this conversation as resolved.
roaring_position_bitmap_test.cc
position_delete_index_test.cc
Expand Down
68 changes: 68 additions & 0 deletions src/iceberg/test/lazy_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include "iceberg/util/lazy.h"

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "iceberg/result.h"
#include "iceberg/test/matchers.h"

namespace {

struct NonDefaultConstructible {
explicit NonDefaultConstructible(int value) : value(value) {}
NonDefaultConstructible() = delete;

int value;
};

iceberg::Result<NonDefaultConstructible> InitNonDefaultConstructible(int value) {
return NonDefaultConstructible(value);
}

iceberg::Result<int> InitAlwaysFails() { return iceberg::Invalid("init failed"); }

} // namespace

TEST(LazyTest, SupportsNonDefaultConstructibleValues) {
const iceberg::Lazy<InitNonDefaultConstructible> lazy;

auto first = lazy.Get(42);
ASSERT_THAT(first, iceberg::IsOk());
EXPECT_EQ(first->get().value, 42);

auto second = lazy.Get(13);
ASSERT_THAT(second, iceberg::IsOk());
EXPECT_EQ(second->get().value, 42);
EXPECT_EQ(&first->get(), &second->get());
}

TEST(LazyTest, ReusesInitializationError) {
const iceberg::Lazy<InitAlwaysFails> lazy;

auto first = lazy.Get();
EXPECT_THAT(first, iceberg::IsError(iceberg::ErrorKind::kInvalid));
EXPECT_THAT(first, iceberg::HasErrorMessage("init failed"));

auto second = lazy.Get();
EXPECT_THAT(second, iceberg::IsError(iceberg::ErrorKind::kInvalid));
EXPECT_THAT(second, iceberg::HasErrorMessage("init failed"));
}
1 change: 1 addition & 0 deletions src/iceberg/test/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ iceberg_tests = {
'decimal_test.cc',
'endian_test.cc',
'formatter_test.cc',
'lazy_test.cc',
'location_util_test.cc',
'position_delete_index_test.cc',
'retry_util_test.cc',
Expand Down
16 changes: 6 additions & 10 deletions src/iceberg/util/lazy.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <concepts>
#include <functional>
#include <mutex>
#include <utility>

#include "iceberg/result.h"
#include "iceberg/util/macros.h"
Expand All @@ -48,19 +49,14 @@ class Lazy {
requires std::invocable<decltype(InitFunc), Args...> &&
std::same_as<std::invoke_result_t<decltype(InitFunc), Args...>, Result<T>>
Result<std::reference_wrapper<T>> Get(Args&&... args) const {
Result<T> result;
std::call_once(flag_, [&result, this, &args...]() {
result = InitFunc(std::forward<Args>(args)...);
if (result) {
this->value_ = std::move(result.value());
}
});
ICEBERG_RETURN_UNEXPECTED(result);
return std::ref(value_);
std::call_once(
flag_, [this, &args...]() { value_ = InitFunc(std::forward<Args>(args)...); });
ICEBERG_RETURN_UNEXPECTED(value_);
Comment thread
HuaHuaY marked this conversation as resolved.
return std::ref(*value_);
}

private:
mutable T value_;
mutable Result<T> value_ = Invalid("Lazy value has not been initialized");
mutable std::once_flag flag_;
};

Expand Down
Loading