From 76081a7329f35a85d42cbf081709e6a05538093a Mon Sep 17 00:00:00 2001 From: Luke DiGiovanna <43895803+lukedigiovanna@users.noreply.github.com> Date: Tue, 23 Jul 2024 11:02:28 -0400 Subject: [PATCH] mqba_domainmanager: refactor code into locateOrCreateDomain function (#368) Co-authored-by: Evgeny Malygin <678098@protonmail.com> Signed-off-by: Luke DiGiovanna <43895803+lukedigiovanna@users.noreply.github.com> --- src/groups/mqb/mqba/mqba_domainmanager.cpp | 79 ++++++++++------------ src/groups/mqb/mqba/mqba_domainmanager.h | 7 ++ 2 files changed, 44 insertions(+), 42 deletions(-) diff --git a/src/groups/mqb/mqba/mqba_domainmanager.cpp b/src/groups/mqb/mqba/mqba_domainmanager.cpp index d02c8edaa..fb2ff12ef 100644 --- a/src/groups/mqb/mqba/mqba_domainmanager.cpp +++ b/src/groups/mqb/mqba/mqba_domainmanager.cpp @@ -599,6 +599,33 @@ int DomainManager::locateDomain(DomainSp* domain, return rc_SUCCESS; } +int DomainManager::locateOrCreateDomain(DomainSp* domain, + const bsl::string& domainName) +{ + if (0 != locateDomain(domain, domainName)) { + BALL_LOG_WARN + << "Domain '" << domainName + << "' is not opened, trying to initialize from configuration"; + + bslmt::Latch latch(1); + createDomain(domainName, + bdlf::BindUtil::bind(&onDomain, + bdlf::PlaceHolders::_1, // status + bdlf::PlaceHolders::_2, // domain* + &latch)); + + // To return a result from command execution, we need to + // synchronize with the domain creation attempt + latch.wait(); + + if (0 != locateDomain(domain, domainName)) { + return -1; // RETURN + } + } + + return 0; // RETURN +} + int DomainManager::processCommand(mqbcmd::DomainsResult* result, const mqbcmd::DomainsCommand& command) { @@ -620,27 +647,11 @@ int DomainManager::processCommand(mqbcmd::DomainsResult* result, DomainSp domainSp; - if (0 != locateDomain(&domainSp, name)) { - BALL_LOG_WARN << "Domain '" << name << "' is not opened," - << " trying to initialize from configuration"; - - bslmt::Latch latch(1); - createDomain( - name, - bdlf::BindUtil::bind(&onDomain, - bdlf::PlaceHolders::_1, // status - bdlf::PlaceHolders::_2, // domain* - &latch)); - // To return a result from command execution, we need to - // synchronize with the domain creation attempt - latch.wait(); - - if (0 != locateDomain(&domainSp, name)) { - mwcu::MemOutStream os; - os << "Domain '" << name << "' doesn't exist"; - result->makeError().message() = os.str(); - return -1; // RETURN - } + if (0 != locateOrCreateDomain(&domainSp, name)) { + mwcu::MemOutStream os; + os << "Domain '" << name << "' doesn't exist"; + result->makeError().message() = os.str(); + return -1; // RETURN } mqbcmd::DomainResult domainResult; @@ -663,27 +674,11 @@ int DomainManager::processCommand(mqbcmd::DomainsResult* result, DomainSp domainSp; - if (0 != locateDomain(&domainSp, name)) { - BALL_LOG_WARN << "Domain '" << name << "' is not opened," - << " trying to initialize from configuration"; - - bslmt::Latch latch(1); - createDomain( - name, - bdlf::BindUtil::bind(&onDomain, - bdlf::PlaceHolders::_1, // status - bdlf::PlaceHolders::_2, // domain* - &latch)); - // To return a result from command execution, we need to - // synchronize with the domain creation attempt - latch.wait(); - - if (0 != locateDomain(&domainSp, name)) { - mwcu::MemOutStream os; - os << "Domain '" << name << "' doesn't exist"; - result->makeError().message() = os.str(); - return -1; // RETURN - } + if (0 != locateOrCreateDomain(&domainSp, name)) { + mwcu::MemOutStream os; + os << "Domain '" << name << "' doesn't exist"; + result->makeError().message() = os.str(); + return -1; // RETURN } DecodeAndUpsertValue configureResult; diff --git a/src/groups/mqb/mqba/mqba_domainmanager.h b/src/groups/mqb/mqba/mqba_domainmanager.h index 1bb66b4aa..bd2984e2f 100644 --- a/src/groups/mqb/mqba/mqba_domainmanager.h +++ b/src/groups/mqb/mqba/mqba_domainmanager.h @@ -332,6 +332,13 @@ class DomainManager BSLS_CPP11_FINAL : public mqbi::DomainFactory { /// specified `errorDescription` otherwise. int locateDomain(DomainSp* domain, const bsl::string& domainName); + /// Load into the specified `domainSp` the domain corresponding to the + /// specified `domainName`, if found. If not found then attempt to create + /// the domain corresponding to `domainName` and load the result into the + /// specified `domainSp`. Return 0 on success, or a non-zero return code on + /// failure. + int locateOrCreateDomain(DomainSp* domain, const bsl::string& domainName); + /// Process the specified `command` and load the result of the command /// in the specified `result`. Return zero on success or a nonzero /// value otherwise.