Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mqba_domainmanager: refactor code into locateOrCreateDomain function #368

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading