Skip to content

Commit

Permalink
CXXCBC-531: Transfer ownership of range_scan_orchestrator_impl to sca…
Browse files Browse the repository at this point in the history
…n_result (#645)
  • Loading branch information
DemetrisChr authored Aug 22, 2024
1 parent e6805ae commit 8be8b51
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 30 deletions.
6 changes: 3 additions & 3 deletions core/impl/collection.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -905,12 +905,12 @@ class collection_impl : public std::enable_shared_from_this<collection_impl>
core_scan_type,
orchestrator_opts);
return orchestrator.scan(
[handler = std::move(handler), orchestrator](auto ec, auto core_scan_result) mutable {
[handler = std::move(handler)](auto ec, auto core_scan_result) mutable {
if (ec) {
return handler(error(ec, "Error while starting the range scan"), {});
}
auto internal_result = std::make_shared<internal_scan_result>(
std::move(orchestrator), std::move(core_scan_result));
auto internal_result =
std::make_shared<internal_scan_result>(std::move(core_scan_result));
return handler({}, scan_result{ internal_result });
});
});
Expand Down
3 changes: 1 addition & 2 deletions core/impl/internal_scan_result.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class internal_scan_result
{

public:
internal_scan_result(core::range_scan_orchestrator orchestrator, core::scan_result core_result);
explicit internal_scan_result(core::scan_result core_result);
internal_scan_result(const internal_scan_result&) = delete;
internal_scan_result(internal_scan_result&&) noexcept = default;
auto operator=(const internal_scan_result&) -> internal_scan_result& = delete;
Expand All @@ -38,7 +38,6 @@ public:
void cancel();

private:
core::range_scan_orchestrator orchestrator_;
core::scan_result core_result_;
};
} // namespace couchbase
6 changes: 2 additions & 4 deletions core/impl/scan_result.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ to_scan_result_item(core::range_scan_item core_item) -> scan_result_item
}
} // namespace

internal_scan_result::internal_scan_result(core::range_scan_orchestrator orchestrator,
core::scan_result core_result)
: orchestrator_{ std::move(orchestrator) }
, core_result_{ std::move(core_result) }
internal_scan_result::internal_scan_result(core::scan_result core_result)
: core_result_{ std::move(core_result) }
{
}

Expand Down
8 changes: 6 additions & 2 deletions core/range_scan_orchestrator.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ class range_scan_orchestrator_impl
self->streams_[vbucket] = stream;
}
self->start_streams(self->concurrency_);
return cb({}, scan_result(self));
// Transferring ownership of the range_scan_orchestrator impl to the scan_result
return cb({}, scan_result(std::move(self)));
});
}

Expand Down Expand Up @@ -663,6 +664,9 @@ range_scan_orchestrator::scan() -> tl::expected<scan_result, std::error_code>
void
range_scan_orchestrator::scan(couchbase::core::scan_callback&& cb)
{
return impl_->scan(std::move(cb));
if (impl_) {
return impl_->scan(std::move(cb));
}
cb(errc::common::request_canceled, {});
}
} // namespace couchbase::core
25 changes: 7 additions & 18 deletions core/scan_result.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,36 @@ namespace couchbase::core
class scan_result_impl
{
public:
explicit scan_result_impl(std::weak_ptr<range_scan_item_iterator> iterator)
explicit scan_result_impl(std::shared_ptr<range_scan_item_iterator> iterator)
: iterator_{ std::move(iterator) }
{
}

[[nodiscard]] auto next() const -> tl::expected<range_scan_item, std::error_code>
{
if (auto ptr = iterator_.lock(); ptr != nullptr) {
return ptr->next().get();
}
return tl::unexpected{ errc::common::request_canceled };
return iterator_->next().get();
}

void next(utils::movable_function<void(range_scan_item, std::error_code)> callback) const
{
if (auto ptr = iterator_.lock(); ptr != nullptr) {
return ptr->next(std::move(callback));
}
callback({}, errc::common::request_canceled);
return iterator_->next(std::move(callback));
}

void cancel()
{
if (auto ptr = iterator_.lock(); ptr != nullptr) {
return ptr->cancel();
}
return iterator_->cancel();
}

[[nodiscard]] auto is_cancelled() -> bool
{
if (auto ptr = iterator_.lock(); ptr != nullptr) {
return ptr->is_cancelled();
}
return false;
return iterator_->is_cancelled();
}

private:
std::weak_ptr<range_scan_item_iterator> iterator_;
std::shared_ptr<range_scan_item_iterator> iterator_;
};

scan_result::scan_result(std::weak_ptr<range_scan_item_iterator> iterator)
scan_result::scan_result(std::shared_ptr<range_scan_item_iterator> iterator)
: impl_{ std::make_shared<scan_result_impl>(std::move(iterator)) }
{
}
Expand Down
2 changes: 1 addition & 1 deletion core/scan_result.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class scan_result
{
public:
scan_result() = default;
explicit scan_result(std::weak_ptr<range_scan_item_iterator> iterator);
explicit scan_result(std::shared_ptr<range_scan_item_iterator> iterator);
[[nodiscard]] auto next() const -> tl::expected<range_scan_item, std::error_code>;
void next(utils::movable_function<void(range_scan_item, std::error_code)> callback) const;
void cancel();
Expand Down

0 comments on commit 8be8b51

Please sign in to comment.