diff options
-rw-r--r-- | gentoobrowse-api/client/helpers.h | 39 | ||||
-rw-r--r-- | gentoobrowse-api/client/search.cpp | 10 |
2 files changed, 43 insertions, 6 deletions
diff --git a/gentoobrowse-api/client/helpers.h b/gentoobrowse-api/client/helpers.h new file mode 100644 index 0000000..5221910 --- /dev/null +++ b/gentoobrowse-api/client/helpers.h @@ -0,0 +1,39 @@ +#ifndef GENTOOBROWSE_CLIENT_HELPERS_H +#define GENTOOBROWSE_CLIENT_HELPERS_H + +#include <map> +#include <Ice/AsyncResult.h> +#include <mutex> + +template<typename Key, typename Target, typename Collection, typename Proxy> +std::map<Key, Target> +asyncMapRelated(const Collection & collection, Proxy p, + Key Collection::value_type::element_type::* member, + Ice::AsyncResultPtr (Proxy::element_type::*f)(Key, + const ::IceInternal::Function<void (const Target &)> &response, + const ::IceInternal::Function<void (const ::Ice::Exception &)> &exception, + const ::IceInternal::Function<void (bool)> & )) +{ + std::map<Key, Target> rtn; + std::map<Key, Ice::AsyncResultPtr> jobs; + std::mutex m; + for (const auto & item : collection) { + Key v = item.get()->*member; + if (jobs.find(v) == jobs.end()) { + jobs.insert({ v, (p.get()->*f)(v, [&rtn, v, &m](const Target & t) { + m.lock(); + rtn.insert({v, t}); + m.unlock(); + }, [](const ::Ice::Exception & ex) { throw ex; } , nullptr) + }); + } + } + for (const auto & j : jobs) { + j.second->waitForCompleted(); + } + return rtn; +} + + +#endif + diff --git a/gentoobrowse-api/client/search.cpp b/gentoobrowse-api/client/search.cpp index dd4baf1..7ec91be 100644 --- a/gentoobrowse-api/client/search.cpp +++ b/gentoobrowse-api/client/search.cpp @@ -2,20 +2,18 @@ #include <fprintbf.h> #include <boost/algorithm/string/join.hpp> #include "main.h" +#include "helpers.h" class search : public Module { public: void run(Gentoo::PortagePrx p, const std::vector<std::string> & a) const override { auto pkgs = p->getPackagesSearch(boost::algorithm::join(a, " ")); - std::map<int, Gentoo::CategoryPtr> cats; + auto cats = asyncMapRelated(pkgs, p, &Gentoo::Package::categoryid, + &IceProxy::Gentoo::Portage::begin_getCategory); fprintbf(stdout, "Found %d packages:\n", pkgs.size()); for (const auto & pkg : pkgs) { - auto ci = cats.find(pkg->categoryid); - if (ci == cats.end()) { - ci = cats.insert({pkg->categoryid, p->getCategory(pkg->categoryid)}).first; - } - fprintbf(stdout, "%s / %s\n\t%s\n", ci->second->name, pkg->name, pkg->description); + fprintbf(stdout, "%s / %s\n\t%s\n", cats[pkg->categoryid]->name, pkg->name, pkg->description); } } }; |