Skip to content

Commit

Permalink
Pass journal file iterator in params for testing
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksandr Ivanov <[email protected]>
  • Loading branch information
alexander-e1off committed Dec 11, 2023
1 parent e9bdf04 commit d9224b5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 88 deletions.
13 changes: 13 additions & 0 deletions src/applications/bmqstoragetool/m_bmqstoragetool_parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ Parameters::Parameters(const CommandLineArguments& arguments,
(!d_dataFile.path().empty() && !d_dataFile.resetIterator(ss))) {
throw bsl::runtime_error(ss.str());
}
// TODO: used for testing, consider better way
if (d_journalFile.path().empty())
d_journalFile.setIterator(nullptr);
if (d_dataFile.path().empty())
d_dataFile.setIterator(nullptr);
}

void Parameters::print(bsl::ostream& ss) const
Expand Down Expand Up @@ -370,5 +375,13 @@ ITER* Parameters::FileHandler<ITER>::iterator()
return &d_iter;
}

// TODO: used for testing, consider better way
template <typename ITER>
void Parameters::FileHandler<ITER>::setIterator(ITER* iter)
{
if (iterator() != nullptr && iter != nullptr)
d_iter = *iter;
}

} // close package namespace
} // close enterprise namespace
3 changes: 3 additions & 0 deletions src/applications/bmqstoragetool/m_bmqstoragetool_parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ class Parameters {
// MANIPULATORS
/// iterator resetter
bool resetIterator(bsl::ostream& errorDescription);

// TODO: used for testing, consider better way
void setIterator(ITER* iter);

/// Mapped file iterator
ITER* iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,6 @@ bool resetIterator(mqbs::MappedFileDescriptor* mfd,

// CREATORS

SearchProcessor::SearchProcessor(const bsl::shared_ptr<Parameters>& params,
mqbs::JournalFileIterator* journalFileIter,
bslma::Allocator* allocator)
: CommandProcessor(params)
, d_dataFile(allocator)
, d_journalFile(allocator)
, d_journalFileIter(journalFileIter)
, d_allocator_p(bslma::Default::allocator(allocator))
{
// NOTHING
}

SearchProcessor::SearchProcessor(const bsl::shared_ptr<Parameters>& params,
bslma::Allocator* allocator)
: CommandProcessor(params)
Expand All @@ -117,20 +105,6 @@ SearchProcessor::SearchProcessor(const bsl::shared_ptr<Parameters>& params,

void SearchProcessor::process(bsl::ostream& ostream)
{
// ostream << "SearchProcessor::process()\n";
// d_parameters->print(ostream);

// TODO: remove - Initialize journal file iterator from real file
if (!d_journalFileIter->isValid()) {
if (!resetIterator(&d_journalFd,
d_journalFileIter,
d_journalFile.c_str(),
ostream)) {
return; // RETURN
}
ostream << "Created Journal iterator successfully" << bsl::endl;
}

// TODO: why unique_ptr doesn't support deleter in reset()
// bsl::unique_ptr<SearchResult> searchResult_p;
bsl::shared_ptr<SearchResult> searchResult_p;
Expand Down Expand Up @@ -176,7 +150,7 @@ void SearchProcessor::process(bsl::ostream& ostream)
bool stopSearch = false;

// Iterate through all Journal file records
mqbs::JournalFileIterator* iter = d_journalFileIter;
mqbs::JournalFileIterator* iter = d_parameters->journalFile()->iterator();
while (true) {
if (stopSearch || !iter->hasRecordSizeRemaining()) {
searchResult_p->outputResult();
Expand Down
41 changes: 0 additions & 41 deletions src/applications/bmqstoragetool/m_bmqstoragetool_searchprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,54 +35,13 @@ namespace m_bmqstoragetool {

class SearchProcessor : public CommandProcessor {
private:
// enum SearchMode {
// k_ALL, // search all messages in journal file
// k_LIST, // search messages by given GUIDs in journal file
// k_OUTSTANDING, // search outstanding (not deleted) messages in
// journal
// // file
// k_CONFIRMED, // search confirmed by all consumers (deleted)
// messages
// // in journal file
// k_PARTIALLY_CONFIRMED // search confirmed at least by one consumer
// // messages in journal file
// };

// TODO: refactor to class, move to separate file for sharing.
// VST representing message details.
// struct MessageDetails {
// mqbs::MessageRecord messageRecord;
// bsl::vector<mqbs::ConfirmRecord> confirmRecords;
// mqbs::DeletionRecordFlag::Enum deleteRecordFlag;
// bool partiallyConfirmed; // There is at least one confirmation
// message
// };

// typedef bsl::unordered_map<bmqt::MessageGUID, MessageDetails>
// MessagesDetails;

// DATA
bsl::string d_dataFile;

bsl::string d_journalFile;

mqbs::MappedFileDescriptor d_dataFd;

mqbs::MappedFileDescriptor d_journalFd;

mqbs::DataFileIterator* d_dataFileIter;

mqbs::JournalFileIterator* d_journalFileIter;

bslma::Allocator* d_allocator_p;

// MANIPULATORS

public:
// CREATORS
explicit SearchProcessor(const bsl::shared_ptr<Parameters>& params,
mqbs::JournalFileIterator* journalFileIter,
bslma::Allocator* allocator);
explicit SearchProcessor(const bsl::shared_ptr<Parameters>& params,
bslma::Allocator* allocator);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,9 @@ static void test1_breathingTest()
CommandLineArguments arguments;
bsl::unique_ptr<Parameters> params =
bsl::make_unique<Parameters>(arguments, s_allocator_p);
params->journalFile()->setIterator(&it);

auto searchProcessor = SearchProcessor(bsl::move(params),
&it,
s_allocator_p);
auto searchProcessor = SearchProcessor(bsl::move(params), s_allocator_p);

bsl::ostringstream resultStream(s_allocator_p);
searchProcessor.process(resultStream);
Expand Down Expand Up @@ -625,10 +624,9 @@ static void test2_searchGuidTest()
}
bsl::unique_ptr<Parameters> params =
bsl::make_unique<Parameters>(arguments, s_allocator_p);
params->journalFile()->setIterator(&it);

auto searchProcessor = SearchProcessor(bsl::move(params),
&it,
s_allocator_p);
auto searchProcessor = SearchProcessor(bsl::move(params), s_allocator_p);

bsl::ostringstream resultStream(s_allocator_p);
searchProcessor.process(resultStream);
Expand All @@ -643,8 +641,6 @@ static void test2_searchGuidTest()

ASSERT_EQ(resultStream.str(), expectedStream.str());

// ASSERT(searchProcessor.getJournalFileIter().isValid());

s_allocator_p->deallocate(p);
}

Expand Down Expand Up @@ -707,10 +703,9 @@ static void test3_searchNonExistingGuidTest()

bsl::unique_ptr<Parameters> params =
bsl::make_unique<Parameters>(arguments, s_allocator_p);
params->journalFile()->setIterator(&it);

auto searchProcessor = SearchProcessor(bsl::move(params),
&it,
s_allocator_p);
auto searchProcessor = SearchProcessor(bsl::move(params), s_allocator_p);

bsl::ostringstream resultStream(s_allocator_p);
searchProcessor.process(resultStream);
Expand Down Expand Up @@ -773,10 +768,9 @@ static void test4_searchOutstandingMessagesTest()
arguments.d_outstanding = true;
bsl::unique_ptr<Parameters> params =
bsl::make_unique<Parameters>(arguments, s_allocator_p);
params->journalFile()->setIterator(&it);

auto searchProcessor = SearchProcessor(bsl::move(params),
&it,
s_allocator_p);
auto searchProcessor = SearchProcessor(bsl::move(params), s_allocator_p);

bsl::ostringstream resultStream(s_allocator_p);
searchProcessor.process(resultStream);
Expand Down Expand Up @@ -848,10 +842,9 @@ static void test5_searchConfirmedMessagesTest()
arguments.d_confirmed = true;
bsl::unique_ptr<Parameters> params =
bsl::make_unique<Parameters>(arguments, s_allocator_p);
params->journalFile()->setIterator(&it);

auto searchProcessor = SearchProcessor(bsl::move(params),
&it,
s_allocator_p);
auto searchProcessor = SearchProcessor(bsl::move(params), s_allocator_p);

bsl::ostringstream resultStream(s_allocator_p);
searchProcessor.process(resultStream);
Expand Down Expand Up @@ -924,10 +917,9 @@ static void test6_searchPartiallyConfirmedMessagesTest()
arguments.d_partiallyConfirmed = true;
bsl::unique_ptr<Parameters> params =
bsl::make_unique<Parameters>(arguments, s_allocator_p);
params->journalFile()->setIterator(&it);

auto searchProcessor = SearchProcessor(bsl::move(params),
&it,
s_allocator_p);
auto searchProcessor = SearchProcessor(bsl::move(params), s_allocator_p);

bsl::ostringstream resultStream(s_allocator_p);
searchProcessor.process(resultStream);
Expand Down

0 comments on commit d9224b5

Please sign in to comment.