From cb6e86bb6a7b7d866582fb6f1a1fb16673ea9e5e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 18 Apr 2016 23:57:22 +0100 Subject: Set up basic file processing from table patch audit callbacks --- gentoobrowse-api/service/maintenanceimpl.cpp | 63 +++++++++++++++------- gentoobrowse-api/service/maintenanceimpl.h | 5 +- .../sql/maintenance/createTempFileListView.sql | 2 +- gentoobrowse-api/unittests/testMaintenance.cpp | 9 +++- 4 files changed, 54 insertions(+), 25 deletions(-) diff --git a/gentoobrowse-api/service/maintenanceimpl.cpp b/gentoobrowse-api/service/maintenanceimpl.cpp index b9a83b4..eddf2b2 100644 --- a/gentoobrowse-api/service/maintenanceimpl.cpp +++ b/gentoobrowse-api/service/maintenanceimpl.cpp @@ -21,10 +21,39 @@ namespace Gentoo { return FilePtr(popen(find, "r"), pclose); } + static void - Maintenance::createTempFileList(DB::Connection * dbc) + fileDeleted(DB::Connection * dbc, const boost::filesystem::path &, DB::SelectCommandPtr s) + { + // b.filename, b.filesize, b.filetypeid, b.moddate, b.pathparts, b.repoid + s->forEachRow([dbc](auto fn, auto, auto, auto, auto, auto) { + fprintf(stderr, "deleted file: %s\n", fn.c_str()); + }); + } + + static + void + fileChanged(DB::Connection * dbc, const boost::filesystem::path &, 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](auto fn, auto, auto, auto, auto, auto, auto, auto, auto, auto) { + fprintf(stderr, "updated file: %s\n", fn.c_str()); + }); + } + + static + void + fileCreated(DB::Connection * dbc, const boost::filesystem::path &, DB::SelectCommandPtr s) + { + // b.filename, b.filesize, b.filetypeid, b.moddate, b.pathparts, b.repoid + s->forEachRow([dbc](auto fn, auto, auto, auto, auto, auto) { + fprintf(stderr, "new file: %s\n", fn.c_str()); + }); + } + + void + Maintenance::createTempFileList(DB::Connection * dbc, const boost::filesystem::path & tmp) { - auto tmp = boost::filesystem::temp_directory_path() / "import"; boost::filesystem::remove_all(tmp); boost::filesystem::create_directories(tmp); dbc->select("SELECT name, path FROM gentoobrowse.repos")->forEachRow([&tmp](auto n, auto p) { @@ -34,41 +63,35 @@ namespace Gentoo { dbc->beginBulkUpload("filelist", ""); dbc->bulkUploadData(findInPath(tmp.string()).get()); dbc->endBulkUpload(nullptr); - boost::filesystem::remove_all(tmp); - } - - void - Maintenance::createTempFileListView(DB::Connection * dbc) - { dbc->execute(sql::maintenance::createTempFileListView::sql); } void - Maintenance::mergeTempFileList(DB::Connection * dbc) + Maintenance::processChanges(DB::Connection * dbc, const boost::filesystem::path & tmp) { DB::TablePatch tp; tp.src = "tmp"; tp.dest = "gentoobrowse.files"; - tp.pk.insert("repoid"); - tp.pk.insert("filename"); - tp.cols.insert("repoid"); - tp.cols.insert("filename"); - tp.cols.insert("filetypeid"); - tp.cols.insert("pathparts"); - tp.cols.insert("filesize"); - tp.cols.insert("moddate"); + tp.pk = {"repoid", "filename"}; + tp.cols = {"repoid", "filename", "filetypeid", "pathparts", "filesize", "moddate"}; + tp.beforeDelete = boost::bind(&fileDeleted, dbc, tmp, _1); + tp.beforeUpdate = boost::bind(&fileChanged, dbc, tmp, _1); + tp.beforeInsert = boost::bind(&fileCreated, dbc, tmp, _1); dbc->patchTable(&tp); + boost::filesystem::remove_all(tmp); + dbc->execute("DROP VIEW tmp"); + dbc->execute("DROP TABLE filelist"); } void Maintenance::refreshPackageTree(const Ice::Current &) { + auto tmp = boost::filesystem::temp_directory_path() / "import"; auto dbc = db->get(); dbc->execute("SET search_path = gentoobrowse, pg_catalog"); DB::TransactionScope tx(dbc.get()); - createTempFileList(dbc.get()); - createTempFileListView(dbc.get()); - mergeTempFileList(dbc.get()); + createTempFileList(dbc.get(), tmp); + processChanges(dbc.get(), tmp); dbc->execute("SET search_path = public, pg_catalog"); } } diff --git a/gentoobrowse-api/service/maintenanceimpl.h b/gentoobrowse-api/service/maintenanceimpl.h index b7f3b3e..c5ad6b7 100644 --- a/gentoobrowse-api/service/maintenanceimpl.h +++ b/gentoobrowse-api/service/maintenanceimpl.h @@ -16,9 +16,8 @@ namespace Gentoo { void refreshPackageTree(const Ice::Current &) override; private: - static void createTempFileList(DB::Connection *); - static void createTempFileListView(DB::Connection *); - static void mergeTempFileList(DB::Connection *); + static void createTempFileList(DB::Connection *, const boost::filesystem::path &); + static void processChanges(DB::Connection *, const boost::filesystem::path &); static FilePtr findInPath(const std::string &); }; } diff --git a/gentoobrowse-api/service/sql/maintenance/createTempFileListView.sql b/gentoobrowse-api/service/sql/maintenance/createTempFileListView.sql index 91dd062..f40473e 100644 --- a/gentoobrowse-api/service/sql/maintenance/createTempFileListView.sql +++ b/gentoobrowse-api/service/sql/maintenance/createTempFileListView.sql @@ -5,7 +5,7 @@ SELECT ft.filetypeid, (STRING_TO_ARRAY(fl.filename, '/'))[2:100] pathparts, fl.filesize, - fl.filemtime AT TIME ZONE 'utc' moddate + fl.filemtime moddate FROM filelist fl, gentoobrowse.repos r, gentoobrowse.filetypes ft WHERE (STRING_TO_ARRAY(fl.filename, '/'))[1] = r.name AND (STRING_TO_ARRAY(fl.filename, '/'))[2:100] ~ ft.spec diff --git a/gentoobrowse-api/unittests/testMaintenance.cpp b/gentoobrowse-api/unittests/testMaintenance.cpp index 38fb258..bb8de21 100644 --- a/gentoobrowse-api/unittests/testMaintenance.cpp +++ b/gentoobrowse-api/unittests/testMaintenance.cpp @@ -42,18 +42,25 @@ BOOST_FIXTURE_TEST_SUITE(tp, TestClient) BOOST_AUTO_TEST_CASE( refreshPackageTree ) { SampleData sd; - sd.extract("4156eb45cf3b0ce1d7125b84efd8688c2d6e831d", "gentoo"); auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("GentooBrowseAPI")); auto insRepo = db->modify("INSERT INTO gentoobrowse.repos(name, path) VALUES(?, ?)"); insRepo->bindParamS(0, "gentoo"); insRepo->bindParamS(1, (treeDir / "gentoo").string()); insRepo->execute(); + sd.extract("4156eb45cf3b0ce1d7125b84efd8688c2d6e831d", "gentoo"); m->refreshPackageTree(); + sd.clean(); db->select("SELECT COUNT(*) FROM gentoobrowse.files")->forEachRow([](auto n) { BOOST_REQUIRE_EQUAL(2084, n); }); + + sd.extract("756569aa764177340726dd3d40b41d89b11b20c7", "gentoo"); + m->refreshPackageTree(); + sd.clean(); + + m->refreshPackageTree(); } BOOST_AUTO_TEST_SUITE_END(); -- cgit v1.2.3