From f527b0f4fc7ffbd820b29508b1e686b670e5e341 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 6 Mar 2022 20:28:11 +0000 Subject: Users service with rw/ro pools using new IceTray features --- gentoobrowse-api/service/main.cpp | 2 +- .../service/maintenanceGitOperations.cpp | 3 +- gentoobrowse-api/service/usersimpl.cpp | 63 ++++++++-------------- gentoobrowse-api/service/usersimpl.h | 14 +++-- gentoobrowse-api/unittests/mockDefs.cpp | 1 + gentoobrowse-api/unittests/mockDefs.h | 2 +- gentoobrowse-api/unittests/testUsers.cpp | 3 +- 7 files changed, 40 insertions(+), 48 deletions(-) diff --git a/gentoobrowse-api/service/main.cpp b/gentoobrowse-api/service/main.cpp index 64c826b..ef21dce 100644 --- a/gentoobrowse-api/service/main.cpp +++ b/gentoobrowse-api/service/main.cpp @@ -51,7 +51,7 @@ namespace Gentoo::Service { auto props = ic->getProperties(); IceTray::Cube::addObject(adp, "portage", db); IceTray::Cube::addObject(adp, "maintenance", dbp, ic, props); - IceTray::Cube::addObject(adp, "users", dbp); + IceTray::Cube::addObject(adp, "users", db, dbp); IceTray::Cube::add(); IceTray::Cube::add( props->getPropertyWithDefault("GentooBrowseAPI.MailServer", "localhost:25")); diff --git a/gentoobrowse-api/service/maintenanceGitOperations.cpp b/gentoobrowse-api/service/maintenanceGitOperations.cpp index 0b4a290..a91116b 100644 --- a/gentoobrowse-api/service/maintenanceGitOperations.cpp +++ b/gentoobrowse-api/service/maintenanceGitOperations.cpp @@ -3,13 +3,14 @@ #include "maintenance.h" #include "maintenance/abstractFileProcessor.h" #include "maintenanceimpl.h" -#include "resourcePool.h" // IWYU pragma: keep #include "staticSqlSource.h" #include "utils/git.h" #include #include #include #include +#include +#include #include #include #include diff --git a/gentoobrowse-api/service/usersimpl.cpp b/gentoobrowse-api/service/usersimpl.cpp index 76d1eea..e7abdab 100644 --- a/gentoobrowse-api/service/usersimpl.cpp +++ b/gentoobrowse-api/service/usersimpl.cpp @@ -6,15 +6,13 @@ #include #include #include -#include +#include #include #include #include #include -#include #include #include -#include // IWYU pragma: keep #include #include #include @@ -28,50 +26,53 @@ #include #include #include +#include // IWYU pragma: no_include "resourcePool.impl.h" namespace Gentoo::Service { - Users::Users(const DB::ConnectionPoolPtr & d) : IceTray::AbstractCachingDatabaseClient(d) { } + Users::Users(const DB::ConnectionPoolPtr & ro, const DB::ConnectionPoolPtr & rw) : rodb {ro}, rwdb {rw} { } Gentoo::UserPtr Users::authenticate(const std::string_view username, const std::string_view password, const Ice::Current &) { - return fetchCache(sql::users::authenticate, 30, username, password); + return rodb.fetchCache(sql::users::authenticate, 30, username, password); } Gentoo::UserPtr Users::verify(const std::string_view username, const std::string_view verifyguid, const Ice::Current &) { - return fetch(sql::users::verify, username, verifyguid); + return rwdb.fetch(sql::users::verify, username, verifyguid); } Gentoo::UserPtr Users::get(Ice::Int id, const Ice::Current &) { - return fetch(sql::users::get, id); + return rodb.fetch(sql::users::get, id); } Gentoo::NewUserPtr Users::getNew(const std::string_view username, const std::string_view password, const Ice::Current &) { - return fetch(sql::users::getNew, username, password); + return rodb.fetch(sql::users::getNew, username, password); } Gentoo::UserPtr Users::find(const std::string_view username, const Ice::Current &) { - return fetch(sql::users::find, username); + return rodb.fetch(sql::users::find, username); } + template Gentoo::NewUserPtr - Users::authOrCreate(const std::string_view & username, const std::string_view & password, - const std::string_view & realname, const std::string_view & email) + Users::authOrCreate(IceTray::TransactionalDatabaseClient & db, const std::string_view & username, + const std::string_view & password, const std::string_view & realname, const std::string_view & email) { - auto existing = fetch>(sql::users::getNew, username, password); + auto existing + = db.template fetch>(sql::users::getNew, username, password); if (existing && *existing) { return *existing; } - return fetch(sql::users::create, username, password, realname, email); + return db.template fetch(sql::users::create, username, password, realname, email); } Gentoo::NewUserPtr @@ -95,10 +96,8 @@ namespace Gentoo::Service { check(realname, validRealname, invalid); check(email, validEmail, invalid); - auto dbc = db->get(); - DB::TransactionScope tx(*dbc.get()); - - auto newUser = authOrCreate(username, password, realname, email); + auto tx = rwdb.transactional(); + auto newUser = authOrCreate(tx, username, password, realname, email); auto mail = notifications->getSignup(newUser); mailServer->sendEmail(mail); @@ -108,20 +107,13 @@ namespace Gentoo::Service { void Users::mailshotsent(Ice::Int id, const Ice::Current &) { - auto dbc = db->get(); - auto upd = sql::users::mailshotsent.modify(dbc.get()); - upd->bindParamI(0, id); - upd->execute(); + rwdb.modify(sql::users::mailshotsent, id); } void Users::remove(Ice::Int id, const std::string_view password, const Ice::Current &) { - auto dbc = db->get(); - auto del = sql::users::safeDelete.modify(dbc.get()); - del->bindParamI(0, id); - del->bindParamS(1, password); - del->execute(); + rwdb.modify(sql::users::safeDelete, id, password); } void @@ -129,35 +121,24 @@ namespace Gentoo::Service { { auto properties = current.adapter->getCommunicator()->getProperties(); auto prunePeriod = properties->getPropertyWithDefault("GentooBrowseAPI.Users.PrunePeriod", "8 weeks"); - auto dbc = db->get(); - auto prune = sql::users::prune.modify(dbc.get()); - prune->bindParamS(0, prunePeriod); - prune->execute(); + rwdb.modify(sql::users::prune, prunePeriod); } void Users::track(Ice::Int userId, Ice::Int packageId, const Ice::Current &) { - auto dbc = db->get(); - auto track = sql::users::track.modify(dbc.get()); - track->bindParamI(0, userId); - track->bindParamI(1, packageId); - track->execute(); + rwdb.modify(sql::users::track, userId, packageId); } void Users::untrack(Ice::Int userId, Ice::Int packageId, const Ice::Current &) { - auto dbc = db->get(); - auto untrack = sql::users::untrack.modify(dbc.get()); - untrack->bindParamI(0, userId); - untrack->bindParamI(1, packageId); - untrack->execute(); + rwdb.modify(sql::users::untrack, userId, packageId); } Gentoo::PackageIds Users::tracked(Ice::Int userId, const Ice::Current &) { - return fetchCache(sql::users::tracked, 10, userId); + return rodb.fetchCache(sql::users::tracked, 10, userId); } } diff --git a/gentoobrowse-api/service/usersimpl.h b/gentoobrowse-api/service/usersimpl.h index 64b610b..5b8d1af 100644 --- a/gentoobrowse-api/service/usersimpl.h +++ b/gentoobrowse-api/service/usersimpl.h @@ -4,6 +4,7 @@ #include "user-models.h" #include #include +#include #include #include #include @@ -11,11 +12,14 @@ namespace Ice { struct Current; } +namespace IceTray { + template class TransactionalDatabaseClient; +} namespace Gentoo::Service { - class DLL_PUBLIC Users : public Gentoo::Users, IceTray::AbstractCachingDatabaseClient { + class DLL_PUBLIC Users : public Gentoo::Users { public: - explicit Users(const DB::ConnectionPoolPtr & d); + explicit Users(const DB::ConnectionPoolPtr & ro, const DB::ConnectionPoolPtr & rw); Gentoo::UserPtr authenticate(const std::string_view, const std::string_view, const Ice::Current &) override; Gentoo::UserPtr verify(const std::string_view, const std::string_view, const Ice::Current &) override; @@ -34,8 +38,12 @@ namespace Gentoo::Service { Gentoo::PackageIds tracked(Ice::Int, const Ice::Current &) override; private: - DLL_PRIVATE Gentoo::NewUserPtr authOrCreate( + template + DLL_PRIVATE static Gentoo::NewUserPtr authOrCreate(IceTray::TransactionalDatabaseClient &, const std::string_view &, const std::string_view &, const std::string_view &, const std::string_view &); + + IceTray::AbstractCachingDatabaseClient rodb; + IceTray::AbstractDatabaseClient rwdb; }; } diff --git a/gentoobrowse-api/unittests/mockDefs.cpp b/gentoobrowse-api/unittests/mockDefs.cpp index 883f84d..4f0ad67 100644 --- a/gentoobrowse-api/unittests/mockDefs.cpp +++ b/gentoobrowse-api/unittests/mockDefs.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include diff --git a/gentoobrowse-api/unittests/mockDefs.h b/gentoobrowse-api/unittests/mockDefs.h index 8284f11..32284fd 100644 --- a/gentoobrowse-api/unittests/mockDefs.h +++ b/gentoobrowse-api/unittests/mockDefs.h @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/gentoobrowse-api/unittests/testUsers.cpp b/gentoobrowse-api/unittests/testUsers.cpp index 6d1ce02..29443ee 100644 --- a/gentoobrowse-api/unittests/testUsers.cpp +++ b/gentoobrowse-api/unittests/testUsers.cpp @@ -12,12 +12,13 @@ #include #include #include -#include #include #include namespace Slicer { class NoRowsReturned; } +// IWYU pragma: no_include +// IWYU pragma: no_include BOOST_GLOBAL_FIXTURE(Service); -- cgit v1.2.3