diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2022-03-06 20:28:11 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2022-03-06 20:28:11 +0000 |
commit | f527b0f4fc7ffbd820b29508b1e686b670e5e341 (patch) | |
tree | c8d3ea19b74b34a8225d9b3a097ab5ae51887f66 | |
parent | Revert accidental clang-format of config file (diff) | |
download | gentoobrowse-api-f527b0f4fc7ffbd820b29508b1e686b670e5e341.tar.bz2 gentoobrowse-api-f527b0f4fc7ffbd820b29508b1e686b670e5e341.tar.xz gentoobrowse-api-f527b0f4fc7ffbd820b29508b1e686b670e5e341.zip |
Users service with rw/ro pools using new IceTray featuresgentoobrowse-api-0.12
-rw-r--r-- | gentoobrowse-api/service/main.cpp | 2 | ||||
-rw-r--r-- | gentoobrowse-api/service/maintenanceGitOperations.cpp | 3 | ||||
-rw-r--r-- | gentoobrowse-api/service/usersimpl.cpp | 63 | ||||
-rw-r--r-- | gentoobrowse-api/service/usersimpl.h | 14 | ||||
-rw-r--r-- | gentoobrowse-api/unittests/mockDefs.cpp | 1 | ||||
-rw-r--r-- | gentoobrowse-api/unittests/mockDefs.h | 2 | ||||
-rw-r--r-- | 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<Gentoo::Portage, Portage>(adp, "portage", db); IceTray::Cube::addObject<Gentoo::Maintenance, Maintenance>(adp, "maintenance", dbp, ic, props); - IceTray::Cube::addObject<Gentoo::Users, Users>(adp, "users", dbp); + IceTray::Cube::addObject<Gentoo::Users, Users>(adp, "users", db, dbp); IceTray::Cube::add<Gentoo::Notifications, Notifications>(); IceTray::Cube::add<IceTray::Mail::MailServer, IceTray::Mail::LibesmtpMailServer>( 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 <Ice/Communicator.h> #include <Ice/Current.h> #include <Ice/ObjectAdapter.h> #include <Ice/Properties.h> +#include <abstractDatabaseClient.h> +#include <basicDataClient.h> #include <boost/date_time/posix_time/conversion.hpp> #include <compileTimeFormatter.h> #include <connection.h> 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 <Ice/Optional.h> #include <Ice/Properties.h> #include <abstractCachingDatabaseClient.h> -#include <connection.h> +#include <abstractDatabaseClient.h> #include <connectionPool.h> #include <icecube.h> #include <mail.h> #include <memory> -#include <modifycommand.h> #include <notifications.h> #include <regex> -#include <resourcePool.h> // IWYU pragma: keep #include <sql/users/authenticate.sql.h> #include <sql/users/create.sql.h> #include <sql/users/find.sql.h> @@ -28,50 +26,53 @@ #include <sql/users/untrack.sql.h> #include <sql/users/verify.sql.h> #include <staticSqlSource.h> +#include <vector> // 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<Gentoo::UserPtr>(sql::users::authenticate, 30, username, password); + return rodb.fetchCache<Gentoo::UserPtr>(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<Gentoo::UserPtr>(sql::users::verify, username, verifyguid); + return rwdb.fetch<Gentoo::UserPtr>(sql::users::verify, username, verifyguid); } Gentoo::UserPtr Users::get(Ice::Int id, const Ice::Current &) { - return fetch<Gentoo::UserPtr>(sql::users::get, id); + return rodb.fetch<Gentoo::UserPtr>(sql::users::get, id); } Gentoo::NewUserPtr Users::getNew(const std::string_view username, const std::string_view password, const Ice::Current &) { - return fetch<Gentoo::NewUserPtr>(sql::users::getNew, username, password); + return rodb.fetch<Gentoo::NewUserPtr>(sql::users::getNew, username, password); } Gentoo::UserPtr Users::find(const std::string_view username, const Ice::Current &) { - return fetch<Gentoo::UserPtr>(sql::users::find, username); + return rodb.fetch<Gentoo::UserPtr>(sql::users::find, username); } + template<typename C> 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<C> & db, const std::string_view & username, + const std::string_view & password, const std::string_view & realname, const std::string_view & email) { - auto existing = fetch<IceUtil::Optional<Gentoo::NewUserPtr>>(sql::users::getNew, username, password); + auto existing + = db.template fetch<IceUtil::Optional<Gentoo::NewUserPtr>>(sql::users::getNew, username, password); if (existing && *existing) { return *existing; } - return fetch<Gentoo::NewUserPtr>(sql::users::create, username, password, realname, email); + return db.template fetch<Gentoo::NewUserPtr>(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<Gentoo::PackageIds>(sql::users::tracked, 10, userId); + return rodb.fetchCache<Gentoo::PackageIds>(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 <Ice/Config.h> #include <abstractCachingDatabaseClient.h> +#include <abstractDatabaseClient.h> #include <connectionPool.h> #include <string_view> #include <users.h> @@ -11,11 +12,14 @@ namespace Ice { struct Current; } +namespace IceTray { + template<typename Connection> 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<typename C> + DLL_PRIVATE static Gentoo::NewUserPtr authOrCreate(IceTray::TransactionalDatabaseClient<C> &, 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 <mime.h> #include <mimeImpl.h> #include <mockDatabase.h> +#include <mockMail.h> #include <mockMailServer.h> #include <notifications.h> #include <plugins.h> 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 <maintenance.h> #include <memory> #include <mockDatabase.h> -#include <mockMailServer.h> +#include <mockMail.h> #include <notifications.h> #include <portage.h> #include <selectcommandUtil.impl.h> 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 <mail.h> #include <memory> #include <mockMail.h> -#include <mockMailServer.h> #include <string> #include <vector> namespace Slicer { class NoRowsReturned; } +// IWYU pragma: no_include <boost/core/enable_if.hpp> +// IWYU pragma: no_include <boost/mpl/identity.hpp> BOOST_GLOBAL_FIXTURE(Service); |