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

Fix[Storagetool]: simplify CompositeSequenceNumber class #562

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,75 +28,86 @@ 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)
unsigned int leaseId,
bsls::Types::Uint64 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;

if (seqNumString.empty()) {
errorDescription << "Invalid input: empty string.";
return *this; // RETURN
}

// Find the position of the separator
const size_t separatorPos = seqNumString.find('-');
if (separatorPos == bsl::string::npos) {
errorDescription << "Invalid format: no '-' separator found.";
return *this; // RETURN
}
// PRECONDITION
BSLS_ASSERT(success);

// Extract parts
const bsl::string firstPart = seqNumString.substr(0, separatorPos);
const bsl::string secondPart = seqNumString.substr(separatorPos + 1);
do {
if (seqNumString.empty()) {
errorDescription << "Invalid input: empty string.";
break; // BREAK
}

// Convert parts to numbers
try {
size_t posFirst, posSecond;
// Find the position of the separator
const size_t separatorPos = seqNumString.find('-');
if (separatorPos == bsl::string::npos) {
errorDescription << "Invalid format: no '-' separator found.";
break; // BREAK
}

unsigned long uLong = bsl::stoul(firstPart, &posFirst);
d_seqNumber = bsl::stoul(secondPart, &posSecond);
// Extract parts
const bsl::string firstPart = seqNumString.substr(0, separatorPos);
const bsl::string secondPart = seqNumString.substr(separatorPos + 1);

// Convert parts to numbers
size_t posFirst, posSecond;
unsigned long uLong;
bsls::Types::Uint64 seqNumber;
try {
uLong = bsl::stoul(firstPart, &posFirst);
seqNumber = bsl::stoul(secondPart, &posSecond);
}
catch (const bsl::invalid_argument& e) {
errorDescription
<< "Invalid input: non-numeric values encountered.";
break; // BREAK
}
catch (const bsl::out_of_range& e) {
errorDescription << "Invalid input: number out of range.";
break; // BREAK
}

if (posFirst != firstPart.size() || posSecond != secondPart.size()) {
throw bsl::invalid_argument(""); // THROW
errorDescription
<< "Invalid input: non-numeric values encountered.";
break; // BREAK
}

d_leaseId = static_cast<unsigned int>(uLong);
if (uLong != d_leaseId) {
throw bsl::out_of_range(""); // THROW
unsigned int leaseId = static_cast<unsigned int>(uLong);
if (uLong != leaseId) {
errorDescription << "Invalid input: number out of range.";
break; // BREAK
}

if (d_leaseId == 0 || d_seqNumber == 0) {
if (leaseId == 0 || seqNumber == 0) {
errorDescription << "Invalid input: zero values encountered.";
return *this; // RETURN
break; // BREAK
}
678098 marked this conversation as resolved.
Show resolved Hide resolved

d_isSet = true;
}
catch (const bsl::invalid_argument& e) {
errorDescription << "Invalid input: non-numeric values encountered.";
}
catch (const bsl::out_of_range& e) {
errorDescription << "Invalid input: number out of range.";
}
d_compositeSequenceNumber = bsl::make_pair(leaseId, seqNumber);

*success = true;
678098 marked this conversation as resolved.
Show resolved Hide resolved
return *this; // RETURN
} while (false);

*success = false;
return *this;
}

Expand All @@ -110,13 +121,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,48 +44,47 @@ 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
/// `sequenceNumber`
CompositeSequenceNumber(const unsigned int leaseId,
const bsls::Types::Uint64 sequenceNumber);
CompositeSequenceNumber(unsigned int leaseId,
bsls::Types::Uint64 sequenceNumber);

// MANIPULATORS

/// 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 const reference to composite sequence number as a pair of
/// primary lease Id and sequence number.
const 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 const 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
Loading
Loading