From 7c950e5ede352e45177a2ab3da5b2bbda334bd0b Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 1 Jun 2016 09:06:05 +0100 Subject: Parallel extract and lookup --- gentoobrowse-api/client/helpers.h | 39 ++++++++++++++++++++++++++++++++++++++ gentoobrowse-api/client/search.cpp | 10 ++++------ 2 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 gentoobrowse-api/client/helpers.h 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 +#include +#include + +template +std::map +asyncMapRelated(const Collection & collection, Proxy p, + Key Collection::value_type::element_type::* member, + Ice::AsyncResultPtr (Proxy::element_type::*f)(Key, + const ::IceInternal::Function &response, + const ::IceInternal::Function &exception, + const ::IceInternal::Function & )) +{ + std::map rtn; + std::map 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 #include #include "main.h" +#include "helpers.h" class search : public Module { public: void run(Gentoo::PortagePrx p, const std::vector & a) const override { auto pkgs = p->getPackagesSearch(boost::algorithm::join(a, " ")); - std::map 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); } } }; -- cgit v1.2.3