Skip to content

Commit

Permalink
Refactor CompositeSequenceNumber
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksandr Ivanov <[email protected]>
  • Loading branch information
alexander-e1off committed Jan 10, 2025
1 parent 4c0a4ba commit f821e67
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,28 @@ namespace m_bmqstoragetool {
// =============================

CompositeSequenceNumber::CompositeSequenceNumber()
: d_leaseId(0)
, d_seqNumber(0)
, d_isSet(false)
: d_compositeSequenceNumber(0, 0)
{
// NOTHING
}

CompositeSequenceNumber::CompositeSequenceNumber(
const unsigned int leaseId,
const bsls::Types::Uint64 sequenceNumber)
: d_leaseId(leaseId)
, d_seqNumber(sequenceNumber)
: d_compositeSequenceNumber(leaseId, sequenceNumber)
{
BSLS_ASSERT(d_leaseId > 0 && d_seqNumber > 0);
d_isSet = d_leaseId > 0 && d_seqNumber > 0;
// NOTHING
}

CompositeSequenceNumber&
CompositeSequenceNumber::fromString(bsl::ostream& errorDescription,
CompositeSequenceNumber::fromString(bool* success,
bsl::ostream& errorDescription,
const bsl::string& seqNumString)
{
d_isSet = false;
// PRECONDITION
BSLS_ASSERT(success);

*success = false;

if (seqNumString.empty()) {
errorDescription << "Invalid input: empty string.";
Expand All @@ -71,24 +71,27 @@ CompositeSequenceNumber::fromString(bsl::ostream& errorDescription,
try {
size_t posFirst, posSecond;

unsigned long uLong = bsl::stoul(firstPart, &posFirst);
d_seqNumber = bsl::stoul(secondPart, &posSecond);
const unsigned long uLong = bsl::stoul(firstPart, &posFirst);
const bsls::Types::Uint64 seqNumber = bsl::stoul(secondPart,
&posSecond);

if (posFirst != firstPart.size() || posSecond != secondPart.size()) {
throw bsl::invalid_argument(""); // THROW
}

d_leaseId = static_cast<unsigned int>(uLong);
if (uLong != d_leaseId) {
unsigned int leaseId = static_cast<unsigned int>(uLong);
if (uLong != leaseId) {
throw bsl::out_of_range(""); // THROW
}

if (d_leaseId == 0 || d_seqNumber == 0) {
if (leaseId == 0 || seqNumber == 0) {
errorDescription << "Invalid input: zero values encountered.";
return *this; // RETURN
}

d_isSet = true;
d_compositeSequenceNumber = bsl::make_pair(leaseId, seqNumber);

*success = true;
}
catch (const bsl::invalid_argument& e) {
errorDescription << "Invalid input: non-numeric values encountered.";
Expand All @@ -110,13 +113,8 @@ bsl::ostream& CompositeSequenceNumber::print(bsl::ostream& stream,

bdlb::Print::indent(stream, level, spacesPerLevel);

if (isSet()) {
stream << "leaseId: " << leaseId()
<< ", sequenceNumber: " << sequenceNumber();
}
else {
stream << "** UNSET **";
}
stream << "leaseId: " << leaseId()
<< ", sequenceNumber: " << sequenceNumber();

if (spacesPerLevel >= 0) {
stream << '\n';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,15 @@ namespace m_bmqstoragetool {

class CompositeSequenceNumber {
private:
// DATA
unsigned int d_leaseId;
// Primary Lease Id
bsls::Types::Uint64 d_seqNumber;
// Sequence Number
bool d_isSet;
// Set to `true` if the value of this object is set
// PRIVATE DATA

/// Pair of primary lease Id and sequence number
bsl::pair<unsigned int, bsls::Types::Uint64> d_compositeSequenceNumber;

public:
// CREATORS

/// Create an un-initialized CompositeSequenceNumber. Note that
/// `isSet()` would return false.
/// Create CompositeSequenceNumber with zero initialized values.
CompositeSequenceNumber();

/// Create CompositeSequenceNumber from the specified `leaseId` and
Expand All @@ -69,23 +65,26 @@ class CompositeSequenceNumber {
/// Initialize this CompositeSequenceNumber from the specified
/// `seqNumString` representation in format `<leaseId>-<sequenceNumber>`.
/// Return a reference offering modifiable access to this object. If
/// convertion is successfull, `isSet()` would return `true`. Otherwise,
/// `isSet()` would return `false` and specified `errorDescription` is
/// convertion is successfull, `success` value is set to `true`. Otherwise,
/// `success` value is set to `false` and specified `errorDescription` is
/// filled with error description.
CompositeSequenceNumber& fromString(bsl::ostream& errorDescription,
CompositeSequenceNumber& fromString(bool* success,
bsl::ostream& errorDescription,
const bsl::string& seqNumString);

// ACCESSORS

/// Return `true` if the value of this object is not set.
bool isSet() const;

/// Return Primary Lease Id value.
/// Return primary Lease Id value.
unsigned int leaseId() const;

/// Return Sequence Number value.
/// Return sequence number value.
bsls::Types::Uint64 sequenceNumber() const;

/// Return the composite sequence number as a pair of primary lease Id and
/// sequence number.
bsl::pair<unsigned int, bsls::Types::Uint64>
compositeSequenceNumber() const;

/// Write the value of this object to the specified output `stream` in a
/// human-readable format, and return a reference to `stream`.
/// Optionally specify an initial indentation `level`. If `level` is
Expand Down Expand Up @@ -139,19 +138,20 @@ bool operator<=(const CompositeSequenceNumber& lhs,

// ACCESSORS

inline bool CompositeSequenceNumber::isSet() const
inline unsigned int CompositeSequenceNumber::leaseId() const
{
return d_isSet;
return d_compositeSequenceNumber.first;
}

inline unsigned int CompositeSequenceNumber::leaseId() const
inline bsls::Types::Uint64 CompositeSequenceNumber::sequenceNumber() const
{
return d_leaseId;
return d_compositeSequenceNumber.second;
}

inline bsls::Types::Uint64 CompositeSequenceNumber::sequenceNumber() const
inline bsl::pair<unsigned int, bsls::Types::Uint64>
CompositeSequenceNumber::compositeSequenceNumber() const
{
return d_seqNumber;
return d_compositeSequenceNumber;
}

} // close package namespace
Expand All @@ -166,51 +166,28 @@ inline bsl::ostream& m_bmqstoragetool::operator<<(
bsl::ostream& stream,
const m_bmqstoragetool::CompositeSequenceNumber& rhs)
{
// PRECONDITIONS
BSLS_ASSERT(rhs.isSet());

return rhs.print(stream, 0, -1);
}

inline bool m_bmqstoragetool::operator==(
const m_bmqstoragetool::CompositeSequenceNumber& lhs,
const m_bmqstoragetool::CompositeSequenceNumber& rhs)
{
// PRECONDITIONS
BSLS_ASSERT(lhs.isSet() && rhs.isSet());

return (lhs.leaseId() == rhs.leaseId() &&
lhs.sequenceNumber() == rhs.sequenceNumber());
return lhs.compositeSequenceNumber() == rhs.compositeSequenceNumber();
}

inline bool m_bmqstoragetool::operator<(
const m_bmqstoragetool::CompositeSequenceNumber& lhs,
const m_bmqstoragetool::CompositeSequenceNumber& rhs)
{
// PRECONDITIONS
BSLS_ASSERT(lhs.isSet() && rhs.isSet());

// Check leaseId first
if (lhs.leaseId() < rhs.leaseId()) {
return true; // RETURN
}
else if (lhs.leaseId() == rhs.leaseId()) {
if (lhs.sequenceNumber() < rhs.sequenceNumber()) {
return true; // RETURN
}
}

return false;
return lhs.compositeSequenceNumber() < rhs.compositeSequenceNumber();
}

inline bool m_bmqstoragetool::operator<=(
const m_bmqstoragetool::CompositeSequenceNumber& lhs,
const m_bmqstoragetool::CompositeSequenceNumber& rhs)
{
// PRECONDITIONS
BSLS_ASSERT(lhs.isSet() && rhs.isSet());

return (lhs < rhs || lhs == rhs);
return lhs.compositeSequenceNumber() <= rhs.compositeSequenceNumber();
}

} // close enterprise namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ static void test1_breathingTest()

{
CompositeSequenceNumber compositeSeqNum;
BMQTST_ASSERT(!compositeSeqNum.isSet());
BMQTST_ASSERT(compositeSeqNum.leaseId() == 0);
BMQTST_ASSERT(compositeSeqNum.sequenceNumber() == 0);
}

{
CompositeSequenceNumber compositeSeqNum(1, 2);
BMQTST_ASSERT(compositeSeqNum.isSet());
BMQTST_ASSERT_EQ(compositeSeqNum.leaseId(), 1ul);
BMQTST_ASSERT_EQ(compositeSeqNum.sequenceNumber(), 2ul);
}
Expand All @@ -73,15 +73,17 @@ static void test2_fromStringTest()

bmqu::MemOutStream errorDescription(bmqtst::TestHelperUtil::allocator());

bool success = false;

// Valid string
{
CompositeSequenceNumber compositeSeqNum;

bsl::string inputString("123-456",
bmqtst::TestHelperUtil::allocator());

compositeSeqNum.fromString(errorDescription, inputString);
BMQTST_ASSERT(compositeSeqNum.isSet());
compositeSeqNum.fromString(&success, errorDescription, inputString);
BMQTST_ASSERT(success);
BMQTST_ASSERT_EQ(compositeSeqNum.leaseId(), 123u);
BMQTST_ASSERT_EQ(compositeSeqNum.sequenceNumber(), 456u);
BMQTST_ASSERT(errorDescription.str().empty());
Expand All @@ -94,8 +96,8 @@ static void test2_fromStringTest()
bsl::string inputString("00123-000456",
bmqtst::TestHelperUtil::allocator());

compositeSeqNum.fromString(errorDescription, inputString);
BMQTST_ASSERT(compositeSeqNum.isSet());
compositeSeqNum.fromString(&success, errorDescription, inputString);
BMQTST_ASSERT(success);
BMQTST_ASSERT_EQ(compositeSeqNum.leaseId(), 123u);
BMQTST_ASSERT_EQ(compositeSeqNum.sequenceNumber(), 456u);
BMQTST_ASSERT(errorDescription.str().empty());
Expand All @@ -107,8 +109,8 @@ static void test2_fromStringTest()

bsl::string inputString("", bmqtst::TestHelperUtil::allocator());

compositeSeqNum.fromString(errorDescription, inputString);
BMQTST_ASSERT_EQ(compositeSeqNum.isSet(), false);
compositeSeqNum.fromString(&success, errorDescription, inputString);
BMQTST_ASSERT_EQ(success, false);
BMQTST_ASSERT(!errorDescription.str().empty());
BMQTST_ASSERT_EQ(errorDescription.str(),
"Invalid input: empty string.");
Expand All @@ -121,8 +123,8 @@ static void test2_fromStringTest()
bsl::string inputString("123456", bmqtst::TestHelperUtil::allocator());
errorDescription.reset();

compositeSeqNum.fromString(errorDescription, inputString);
BMQTST_ASSERT_EQ(compositeSeqNum.isSet(), false);
compositeSeqNum.fromString(&success, errorDescription, inputString);
BMQTST_ASSERT_EQ(success, false);
BMQTST_ASSERT(!errorDescription.str().empty());
BMQTST_ASSERT_EQ(errorDescription.str(),
"Invalid format: no '-' separator found.");
Expand All @@ -136,8 +138,8 @@ static void test2_fromStringTest()
bmqtst::TestHelperUtil::allocator());
errorDescription.reset();

compositeSeqNum.fromString(errorDescription, inputString);
BMQTST_ASSERT_EQ(compositeSeqNum.isSet(), false);
compositeSeqNum.fromString(&success, errorDescription, inputString);
BMQTST_ASSERT_EQ(success, false);
BMQTST_ASSERT(!errorDescription.str().empty());
BMQTST_ASSERT_EQ(errorDescription.str(),
"Invalid format: no '-' separator found.");
Expand All @@ -151,8 +153,8 @@ static void test2_fromStringTest()
bmqtst::TestHelperUtil::allocator());
errorDescription.reset();

compositeSeqNum.fromString(errorDescription, inputString);
BMQTST_ASSERT_EQ(compositeSeqNum.isSet(), false);
compositeSeqNum.fromString(&success, errorDescription, inputString);
BMQTST_ASSERT_EQ(success, false);
BMQTST_ASSERT(!errorDescription.str().empty());
BMQTST_ASSERT_EQ(errorDescription.str(),
"Invalid input: non-numeric values encountered.");
Expand All @@ -166,8 +168,8 @@ static void test2_fromStringTest()
bmqtst::TestHelperUtil::allocator());
errorDescription.reset();

compositeSeqNum.fromString(errorDescription, inputString);
BMQTST_ASSERT_EQ(compositeSeqNum.isSet(), false);
compositeSeqNum.fromString(&success, errorDescription, inputString);
BMQTST_ASSERT_EQ(success, false);
BMQTST_ASSERT(!errorDescription.str().empty());
BMQTST_ASSERT_EQ(errorDescription.str(),
"Invalid input: non-numeric values encountered.");
Expand All @@ -180,8 +182,8 @@ static void test2_fromStringTest()
bsl::string inputString("0-456", bmqtst::TestHelperUtil::allocator());
errorDescription.reset();

compositeSeqNum.fromString(errorDescription, inputString);
BMQTST_ASSERT_EQ(compositeSeqNum.isSet(), false);
compositeSeqNum.fromString(&success, errorDescription, inputString);
BMQTST_ASSERT_EQ(success, false);
BMQTST_ASSERT(!errorDescription.str().empty());
BMQTST_ASSERT_EQ(errorDescription.str(),
"Invalid input: zero values encountered.");
Expand All @@ -194,8 +196,8 @@ static void test2_fromStringTest()
bsl::string inputString("123-0", bmqtst::TestHelperUtil::allocator());
errorDescription.reset();

compositeSeqNum.fromString(errorDescription, inputString);
BMQTST_ASSERT_EQ(compositeSeqNum.isSet(), false);
compositeSeqNum.fromString(&success, errorDescription, inputString);
BMQTST_ASSERT_EQ(success, false);
BMQTST_ASSERT(!errorDescription.str().empty());
BMQTST_ASSERT_EQ(errorDescription.str(),
"Invalid input: zero values encountered.");
Expand All @@ -210,8 +212,8 @@ static void test2_fromStringTest()
bmqtst::TestHelperUtil::allocator());
errorDescription.reset();

compositeSeqNum.fromString(errorDescription, inputString);
BMQTST_ASSERT_EQ(compositeSeqNum.isSet(), false);
compositeSeqNum.fromString(&success, errorDescription, inputString);
BMQTST_ASSERT_EQ(success, false);
BMQTST_ASSERT(!errorDescription.str().empty());
BMQTST_ASSERT_EQ(errorDescription.str(),
"Invalid input: number out of range.");
Expand All @@ -226,8 +228,8 @@ static void test2_fromStringTest()
bmqtst::TestHelperUtil::allocator());
errorDescription.reset();

compositeSeqNum.fromString(errorDescription, inputString);
BMQTST_ASSERT_EQ(compositeSeqNum.isSet(), false);
compositeSeqNum.fromString(&success, errorDescription, inputString);
BMQTST_ASSERT_EQ(success, false);
BMQTST_ASSERT(!errorDescription.str().empty());
BMQTST_ASSERT_EQ(errorDescription.str(),
"Invalid input: number out of range.");
Expand Down
Loading

0 comments on commit f821e67

Please sign in to comment.