Attempt to lazy load distro_series for spph

main
Simon Quigley 1 month ago
parent 6870125249
commit 943f1b913b

@ -0,0 +1,45 @@
// Copyright (C) 2024 Simon Quigley <tsimonq2@ubuntu.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef LAZY_OPTIONAL_H
#define LAZY_OPTIONAL_H
#include <optional>
#include <utility>
template <typename T, typename Loader>
class lazy_optional {
public:
lazy_optional(Loader loader) : loader_(std::move(loader)) {}
T& operator*() { load(); return *value_; }
const T& operator*() const { load(); return *value_; }
T* operator->() { load(); return &*value_; }
const T* operator->() const { load(); return &*value_; }
explicit operator bool() const { load(); return value_.has_value(); }
private:
mutable std::optional<T> value_;
[[no_unique_address]] Loader loader_;
// Removed constexpr
void load() const {
if (!value_) value_ = loader_();
}
};
#endif // LAZY_OPTIONAL_H

@ -21,7 +21,27 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
source_package_publishing_history::source_package_publishing_history() source_package_publishing_history::source_package_publishing_history()
: source_package_name(""), source_package_version(""), self_link(""), lp(nullptr) {} : source_package_name(""),
source_package_version(""),
self_link(""),
lp(nullptr),
distro_series([this]() -> std::optional<class distro_series> {
if (distro_series_link.empty()) return std::nullopt;
auto response = lp ? lp->api_get(distro_series_link) : std::nullopt;
if (!response) return std::nullopt;
auto data = nlohmann::json::parse(response.value());
auto ds = distro_series::parse(data.dump());
if (ds) {
ds->set_lp(lp);
return ds;
}
return std::nullopt;
})
{}
source_package_publishing_history::~source_package_publishing_history() {} source_package_publishing_history::~source_package_publishing_history() {}
@ -81,19 +101,6 @@ void source_package_publishing_history::set_lp(launchpad* lp_ptr) {
lp = lp_ptr; lp = lp_ptr;
} }
const std::optional<class distro_series> source_package_publishing_history::getDistroSeries() {
if (distro_series_link.empty()) return std::nullopt;
auto response = lp->api_get(distro_series_link);
if (!response) return std::nullopt;
auto data = nlohmann::json::parse(response.value());
auto ds = distro_series::parse(data.dump());
if (ds) {
ds->set_lp(lp);
return ds;
}
return std::nullopt;
}
std::generator<binary_package_publishing_history> source_package_publishing_history::getPublishedBinaries(bool active_binaries_only) const { std::generator<binary_package_publishing_history> source_package_publishing_history::getPublishedBinaries(bool active_binaries_only) const {
std::map<std::string,std::string> params; std::map<std::string,std::string> params;
params["ws.op"] = "getPublishedBinaries"; params["ws.op"] = "getPublishedBinaries";

@ -20,6 +20,7 @@
#include <optional> #include <optional>
#include <generator> #include <generator>
#include "distro_series.h" #include "distro_series.h"
#include "lazy_optional.h"
class launchpad; class launchpad;
class build; class build;
@ -42,7 +43,7 @@ public:
void parse_json(const std::string& json_data); void parse_json(const std::string& json_data);
std::generator<build> getBuilds() const; std::generator<build> getBuilds() const;
std::optional<class distro_series> distro_series = getDistroSeries(); lazy_optional<class distro_series, std::function<std::optional<class distro_series>()>> distro_series;
std::generator<binary_package_publishing_history> getPublishedBinaries(bool active_binaries_only = false) const; std::generator<binary_package_publishing_history> getPublishedBinaries(bool active_binaries_only = false) const;
bool requestDeletion(const std::string& removal_comment = ""); bool requestDeletion(const std::string& removal_comment = "");
@ -56,7 +57,6 @@ public:
private: private:
launchpad* lp; launchpad* lp;
const std::optional<class distro_series> getDistroSeries();
}; };
#endif // SOURCE_PACKAGE_PUBLISHING_HISTORY #endif // SOURCE_PACKAGE_PUBLISHING_HISTORY

Loading…
Cancel
Save