Skip to content

Commit

Permalink
mqba_domainmanager: refactor code into locateOrCreateDomain function (#…
Browse files Browse the repository at this point in the history
…368)


Co-authored-by: Evgeny Malygin <678098@protonmail.com>
Signed-off-by: Luke DiGiovanna <43895803+lukedigiovanna@users.noreply.github.com>
  • Loading branch information
lukedigiovanna and 678098 authored Jul 23, 2024
1 parent e1621ff commit 76081a7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 42 deletions.
79 changes: 37 additions & 42 deletions src/groups/mqb/mqba/mqba_domainmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
Expand All @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions src/groups/mqb/mqba/mqba_domainmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 76081a7

Please sign in to comment.