From 4e3fddb5b660be7e4646fd44cab817e4309253ae Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 22 May 2016 17:52:21 +0100 Subject: Create file processors per run maintenance run --- gentoobrowse-api/service/maintenanceimpl.cpp | 60 ++++++++++++++++------------ gentoobrowse-api/service/maintenanceimpl.h | 20 ++++++---- 2 files changed, 46 insertions(+), 34 deletions(-) diff --git a/gentoobrowse-api/service/maintenanceimpl.cpp b/gentoobrowse-api/service/maintenanceimpl.cpp index 0957dda..bbece65 100644 --- a/gentoobrowse-api/service/maintenanceimpl.cpp +++ b/gentoobrowse-api/service/maintenanceimpl.cpp @@ -51,58 +51,62 @@ namespace Gentoo { { } + template + Maintenance::FileProcessorPtr + Maintenance::createFileProessor() + { + return FileProcessorPtr(new T()); + } + Maintenance::Maintenance(IceTray::DatabasePoolPtr d) : IceTray::AbstractDatabaseClient(d) { - fps[CategoryMetaProcessor::FILETYPEID] = new CategoryMetaProcessor(); - fps[PackageManifestProcessor::FILETYPEID] = new PackageManifestProcessor(); - fps[PackageMetaProcessor::FILETYPEID] = new PackageMetaProcessor(); - fps[EbuildMetaProcessor::FILETYPEID] = new EbuildMetaProcessor(); - fps[UseGlobalProcessor::FILETYPEID] = new UseGlobalProcessor(); - fps[UseLocalProcessor::FILETYPEID] = new UseLocalProcessor(); - fps[UseGroupProcessor::FILETYPEID] = new UseGroupProcessor(); - fps[MasksProcessor::FILETYPEID] = new MasksProcessor(); + fpfs[CategoryMetaProcessor::FILETYPEID] = &createFileProessor; + fpfs[PackageManifestProcessor::FILETYPEID] = &createFileProessor; + fpfs[PackageMetaProcessor::FILETYPEID] = &createFileProessor; + fpfs[EbuildMetaProcessor::FILETYPEID] = &createFileProessor; + fpfs[UseGlobalProcessor::FILETYPEID] = &createFileProessor; + fpfs[UseLocalProcessor::FILETYPEID] = &createFileProessor; + fpfs[UseGroupProcessor::FILETYPEID] = &createFileProessor; + fpfs[MasksProcessor::FILETYPEID] = &createFileProessor; } Maintenance::~Maintenance() { - for(const auto & fp : fps) { - delete fp.second; - } } void - Maintenance::fileDeleted(DB::Connection * dbc, const boost::filesystem::path & tmp, DB::SelectCommandPtr s) + Maintenance::fileDeleted(DB::Connection * dbc, const FileProcessors * fps, const boost::filesystem::path & tmp, DB::SelectCommandPtr s) { // b.filename, b.filesize, b.filetypeid, b.moddate, b.pathparts, b.repoid - s->forEachRow([dbc,&tmp,this](auto fn, auto, auto ft, auto, auto, auto) { - this->fileHandle(ft, boost::bind(&FileProcessor::deleted, _1, dbc, fn)); + s->forEachRow([dbc,&tmp,this,fps](auto fn, auto, auto ft, auto, auto, auto) { + this->fileHandle(ft, fps, boost::bind(&FileProcessor::deleted, _1, dbc, fn)); }); } void - Maintenance::fileChanged(DB::Connection * dbc, const boost::filesystem::path & tmp, DB::SelectCommandPtr s) + Maintenance::fileChanged(DB::Connection * dbc, const FileProcessors * fps, const boost::filesystem::path & tmp, DB::SelectCommandPtr s) { // a.filename, a.repoid, a.filesize old_filesize, a.filetypeid old_filetypeid, a.moddate old_moddate, a.pathparts old_pathparts, b.filesize new_filesize, b.filetypeid new_filetypeid, b.moddate new_moddate, b.pathparts new_pathparts - s->forEachRow([dbc,&tmp,this](auto fn, auto, auto, auto ft, auto, auto, auto, auto, auto, auto) { - this->fileHandle(ft, boost::bind(&FileProcessor::modified, _1, dbc, fn, tmp / fn)); + s->forEachRow([dbc,&tmp,this,fps](auto fn, auto, auto, auto ft, auto, auto, auto, auto, auto, auto) { + this->fileHandle(ft, fps, boost::bind(&FileProcessor::modified, _1, dbc, fn, tmp / fn)); }); } void - Maintenance::fileCreated(DB::Connection * dbc, const boost::filesystem::path & tmp, DB::SelectCommandPtr s) + Maintenance::fileCreated(DB::Connection * dbc, const FileProcessors * fps, const boost::filesystem::path & tmp, DB::SelectCommandPtr s) { // b.filename, b.filesize, b.filetypeid, b.moddate, b.pathparts, b.repoid - s->forEachRow([dbc,&tmp,this](auto fn, auto, auto ft, auto, auto, auto) { - this->fileHandle(ft, boost::bind(&FileProcessor::created, _1, dbc, fn, tmp / fn)); + s->forEachRow([dbc,&tmp,this,fps](auto fn, auto, auto ft, auto, auto, auto) { + this->fileHandle(ft, fps, boost::bind(&FileProcessor::created, _1, dbc, fn, tmp / fn)); }); } void - Maintenance::fileHandle(int64_t ft, const FileHandleFunc & f) const + Maintenance::fileHandle(int64_t ft, const FileProcessors * fps, const FileHandleFunc & f) const { - auto i = fps.find(ft); - if (i != fps.end()) { + auto i = fps->find(ft); + if (i != fps->end()) { f(i->second); } } @@ -131,15 +135,19 @@ namespace Gentoo { void Maintenance::processChanges(DB::Connection * dbc, const boost::filesystem::path & tmp) { + FileProcessors fps; + for (const auto & fpf : fpfs) { + fps[fpf.first] = fpf.second(); + } DB::StaticSqlWriter src(sql::maintenance::fileList.getSql()); DB::TablePatch tp; tp.srcExpr = &src; tp.dest = "gentoobrowse.files"; tp.pk = {"repoid", "filename"}; tp.cols = {"repoid", "filename", "filetypeid", "pathparts", "filesize", "moddate"}; - tp.beforeDelete = boost::bind(&Maintenance::fileDeleted, this, dbc, tmp, _1); - tp.beforeUpdate = boost::bind(&Maintenance::fileChanged, this, dbc, tmp, _1); - tp.beforeInsert = boost::bind(&Maintenance::fileCreated, this, dbc, tmp, _1); + tp.beforeDelete = boost::bind(&Maintenance::fileDeleted, this, dbc, &fps, tmp, _1); + tp.beforeUpdate = boost::bind(&Maintenance::fileChanged, this, dbc, &fps, tmp, _1); + tp.beforeInsert = boost::bind(&Maintenance::fileCreated, this, dbc, &fps, tmp, _1); DB::StaticSqlWriter obpo("processOrder NULLS LAST"); tp.order = &obpo; dbc->patchTable(&tp); diff --git a/gentoobrowse-api/service/maintenanceimpl.h b/gentoobrowse-api/service/maintenanceimpl.h index 846507b..378cbd1 100644 --- a/gentoobrowse-api/service/maintenanceimpl.h +++ b/gentoobrowse-api/service/maintenanceimpl.h @@ -19,9 +19,11 @@ namespace Gentoo { virtual void modified(DB::Connection * dbc, const boost::filesystem::path & fn, const boost::filesystem::path & path) const; virtual void deleted(DB::Connection * dbc, const boost::filesystem::path & fn) const; }; + typedef boost::shared_ptr FileProcessorPtr; - typedef std::map FileProcessors; - typedef boost::function FileHandleFunc; + typedef std::map> FileProcessorFactories; + typedef std::map FileProcessors; + typedef boost::function FileHandleFunc; Maintenance(IceTray::DatabasePoolPtr d); ~Maintenance(); @@ -31,12 +33,14 @@ namespace Gentoo { private: static void createTempFileList(DB::Connection *, const boost::filesystem::path &); void processChanges(DB::Connection *, const boost::filesystem::path &); - void fileDeleted(DB::Connection * dbc, const boost::filesystem::path &, DB::SelectCommandPtr s); - void fileChanged(DB::Connection * dbc, const boost::filesystem::path &, DB::SelectCommandPtr s); - void fileCreated(DB::Connection * dbc, const boost::filesystem::path & tmp, DB::SelectCommandPtr s); - void fileHandle(int64_t filetypeId, const FileHandleFunc &) const; - - FileProcessors fps; + void fileDeleted(DB::Connection * dbc, const FileProcessors *, const boost::filesystem::path &, DB::SelectCommandPtr s); + void fileChanged(DB::Connection * dbc, const FileProcessors *, const boost::filesystem::path &, DB::SelectCommandPtr s); + void fileCreated(DB::Connection * dbc, const FileProcessors *, const boost::filesystem::path & tmp, DB::SelectCommandPtr s); + void fileHandle(int64_t filetypeId, const FileProcessors *, const FileHandleFunc &) const; + + template + static FileProcessorPtr createFileProessor(); + FileProcessorFactories fpfs; }; } } -- cgit v1.2.3