Skip to content

Commit 88fe620

Browse files
Fix[Storagetool]: simplify CompositeSequenceNumber class (#562)
1 parent a75a915 commit 88fe620

4 files changed

+129
-135
lines changed

src/applications/bmqstoragetool/m_bmqstoragetool_compositesequencenumber.cpp

+58-52
Original file line numberDiff line numberDiff line change
@@ -28,75 +28,86 @@ namespace m_bmqstoragetool {
2828
// =============================
2929

3030
CompositeSequenceNumber::CompositeSequenceNumber()
31-
: d_leaseId(0)
32-
, d_seqNumber(0)
33-
, d_isSet(false)
31+
: d_compositeSequenceNumber(0, 0)
3432
{
3533
// NOTHING
3634
}
3735

3836
CompositeSequenceNumber::CompositeSequenceNumber(
39-
const unsigned int leaseId,
40-
const bsls::Types::Uint64 sequenceNumber)
41-
: d_leaseId(leaseId)
42-
, d_seqNumber(sequenceNumber)
37+
unsigned int leaseId,
38+
bsls::Types::Uint64 sequenceNumber)
39+
: d_compositeSequenceNumber(leaseId, sequenceNumber)
4340
{
44-
BSLS_ASSERT(d_leaseId > 0 && d_seqNumber > 0);
45-
d_isSet = d_leaseId > 0 && d_seqNumber > 0;
41+
// NOTHING
4642
}
4743

4844
CompositeSequenceNumber&
49-
CompositeSequenceNumber::fromString(bsl::ostream& errorDescription,
45+
CompositeSequenceNumber::fromString(bool* success,
46+
bsl::ostream& errorDescription,
5047
const bsl::string& seqNumString)
5148
{
52-
d_isSet = false;
53-
54-
if (seqNumString.empty()) {
55-
errorDescription << "Invalid input: empty string.";
56-
return *this; // RETURN
57-
}
58-
59-
// Find the position of the separator
60-
const size_t separatorPos = seqNumString.find('-');
61-
if (separatorPos == bsl::string::npos) {
62-
errorDescription << "Invalid format: no '-' separator found.";
63-
return *this; // RETURN
64-
}
49+
// PRECONDITION
50+
BSLS_ASSERT(success);
6551

66-
// Extract parts
67-
const bsl::string firstPart = seqNumString.substr(0, separatorPos);
68-
const bsl::string secondPart = seqNumString.substr(separatorPos + 1);
52+
do {
53+
if (seqNumString.empty()) {
54+
errorDescription << "Invalid input: empty string.";
55+
break; // BREAK
56+
}
6957

70-
// Convert parts to numbers
71-
try {
72-
size_t posFirst, posSecond;
58+
// Find the position of the separator
59+
const size_t separatorPos = seqNumString.find('-');
60+
if (separatorPos == bsl::string::npos) {
61+
errorDescription << "Invalid format: no '-' separator found.";
62+
break; // BREAK
63+
}
7364

74-
unsigned long uLong = bsl::stoul(firstPart, &posFirst);
75-
d_seqNumber = bsl::stoul(secondPart, &posSecond);
65+
// Extract parts
66+
const bsl::string firstPart = seqNumString.substr(0, separatorPos);
67+
const bsl::string secondPart = seqNumString.substr(separatorPos + 1);
68+
69+
// Convert parts to numbers
70+
size_t posFirst, posSecond;
71+
unsigned long uLong;
72+
bsls::Types::Uint64 seqNumber;
73+
try {
74+
uLong = bsl::stoul(firstPart, &posFirst);
75+
seqNumber = bsl::stoul(secondPart, &posSecond);
76+
}
77+
catch (const bsl::invalid_argument& e) {
78+
errorDescription
79+
<< "Invalid input: non-numeric values encountered.";
80+
break; // BREAK
81+
}
82+
catch (const bsl::out_of_range& e) {
83+
errorDescription << "Invalid input: number out of range.";
84+
break; // BREAK
85+
}
7686

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

81-
d_leaseId = static_cast<unsigned int>(uLong);
82-
if (uLong != d_leaseId) {
83-
throw bsl::out_of_range(""); // THROW
93+
unsigned int leaseId = static_cast<unsigned int>(uLong);
94+
if (uLong != leaseId) {
95+
errorDescription << "Invalid input: number out of range.";
96+
break; // BREAK
8497
}
8598

86-
if (d_leaseId == 0 || d_seqNumber == 0) {
99+
if (leaseId == 0 || seqNumber == 0) {
87100
errorDescription << "Invalid input: zero values encountered.";
88-
return *this; // RETURN
101+
break; // BREAK
89102
}
90103

91-
d_isSet = true;
92-
}
93-
catch (const bsl::invalid_argument& e) {
94-
errorDescription << "Invalid input: non-numeric values encountered.";
95-
}
96-
catch (const bsl::out_of_range& e) {
97-
errorDescription << "Invalid input: number out of range.";
98-
}
104+
d_compositeSequenceNumber = bsl::make_pair(leaseId, seqNumber);
99105

106+
*success = true;
107+
return *this; // RETURN
108+
} while (false);
109+
110+
*success = false;
100111
return *this;
101112
}
102113

@@ -110,13 +121,8 @@ bsl::ostream& CompositeSequenceNumber::print(bsl::ostream& stream,
110121

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

113-
if (isSet()) {
114-
stream << "leaseId: " << leaseId()
115-
<< ", sequenceNumber: " << sequenceNumber();
116-
}
117-
else {
118-
stream << "** UNSET **";
119-
}
124+
stream << "leaseId: " << leaseId()
125+
<< ", sequenceNumber: " << sequenceNumber();
120126

121127
if (spacesPerLevel >= 0) {
122128
stream << '\n';

src/applications/bmqstoragetool/m_bmqstoragetool_compositesequencenumber.h

+28-51
Original file line numberDiff line numberDiff line change
@@ -44,48 +44,47 @@ namespace m_bmqstoragetool {
4444

4545
class CompositeSequenceNumber {
4646
private:
47-
// DATA
48-
unsigned int d_leaseId;
49-
// Primary Lease Id
50-
bsls::Types::Uint64 d_seqNumber;
51-
// Sequence Number
52-
bool d_isSet;
53-
// Set to `true` if the value of this object is set
47+
// PRIVATE DATA
48+
49+
/// Pair of primary lease Id and sequence number
50+
bsl::pair<unsigned int, bsls::Types::Uint64> d_compositeSequenceNumber;
5451

5552
public:
5653
// CREATORS
5754

58-
/// Create an un-initialized CompositeSequenceNumber. Note that
59-
/// `isSet()` would return false.
55+
/// Create CompositeSequenceNumber with zero initialized values.
6056
CompositeSequenceNumber();
6157

6258
/// Create CompositeSequenceNumber from the specified `leaseId` and
6359
/// `sequenceNumber`
64-
CompositeSequenceNumber(const unsigned int leaseId,
65-
const bsls::Types::Uint64 sequenceNumber);
60+
CompositeSequenceNumber(unsigned int leaseId,
61+
bsls::Types::Uint64 sequenceNumber);
6662

6763
// MANIPULATORS
6864

6965
/// Initialize this CompositeSequenceNumber from the specified
7066
/// `seqNumString` representation in format `<leaseId>-<sequenceNumber>`.
7167
/// Return a reference offering modifiable access to this object. If
72-
/// convertion is successfull, `isSet()` would return `true`. Otherwise,
73-
/// `isSet()` would return `false` and specified `errorDescription` is
68+
/// convertion is successfull, `success` value is set to `true`. Otherwise,
69+
/// `success` value is set to `false` and specified `errorDescription` is
7470
/// filled with error description.
75-
CompositeSequenceNumber& fromString(bsl::ostream& errorDescription,
71+
CompositeSequenceNumber& fromString(bool* success,
72+
bsl::ostream& errorDescription,
7673
const bsl::string& seqNumString);
7774

7875
// ACCESSORS
7976

80-
/// Return `true` if the value of this object is not set.
81-
bool isSet() const;
82-
83-
/// Return Primary Lease Id value.
77+
/// Return primary Lease Id value.
8478
unsigned int leaseId() const;
8579

86-
/// Return Sequence Number value.
80+
/// Return sequence number value.
8781
bsls::Types::Uint64 sequenceNumber() const;
8882

83+
/// Return the const reference to composite sequence number as a pair of
84+
/// primary lease Id and sequence number.
85+
const bsl::pair<unsigned int, bsls::Types::Uint64>&
86+
compositeSequenceNumber() const;
87+
8988
/// Write the value of this object to the specified output `stream` in a
9089
/// human-readable format, and return a reference to `stream`.
9190
/// Optionally specify an initial indentation `level`. If `level` is
@@ -139,19 +138,20 @@ bool operator<=(const CompositeSequenceNumber& lhs,
139138

140139
// ACCESSORS
141140

142-
inline bool CompositeSequenceNumber::isSet() const
141+
inline unsigned int CompositeSequenceNumber::leaseId() const
143142
{
144-
return d_isSet;
143+
return d_compositeSequenceNumber.first;
145144
}
146145

147-
inline unsigned int CompositeSequenceNumber::leaseId() const
146+
inline bsls::Types::Uint64 CompositeSequenceNumber::sequenceNumber() const
148147
{
149-
return d_leaseId;
148+
return d_compositeSequenceNumber.second;
150149
}
151150

152-
inline bsls::Types::Uint64 CompositeSequenceNumber::sequenceNumber() const
151+
inline const bsl::pair<unsigned int, bsls::Types::Uint64>&
152+
CompositeSequenceNumber::compositeSequenceNumber() const
153153
{
154-
return d_seqNumber;
154+
return d_compositeSequenceNumber;
155155
}
156156

157157
} // close package namespace
@@ -166,51 +166,28 @@ inline bsl::ostream& m_bmqstoragetool::operator<<(
166166
bsl::ostream& stream,
167167
const m_bmqstoragetool::CompositeSequenceNumber& rhs)
168168
{
169-
// PRECONDITIONS
170-
BSLS_ASSERT(rhs.isSet());
171-
172169
return rhs.print(stream, 0, -1);
173170
}
174171

175172
inline bool m_bmqstoragetool::operator==(
176173
const m_bmqstoragetool::CompositeSequenceNumber& lhs,
177174
const m_bmqstoragetool::CompositeSequenceNumber& rhs)
178175
{
179-
// PRECONDITIONS
180-
BSLS_ASSERT(lhs.isSet() && rhs.isSet());
181-
182-
return (lhs.leaseId() == rhs.leaseId() &&
183-
lhs.sequenceNumber() == rhs.sequenceNumber());
176+
return lhs.compositeSequenceNumber() == rhs.compositeSequenceNumber();
184177
}
185178

186179
inline bool m_bmqstoragetool::operator<(
187180
const m_bmqstoragetool::CompositeSequenceNumber& lhs,
188181
const m_bmqstoragetool::CompositeSequenceNumber& rhs)
189182
{
190-
// PRECONDITIONS
191-
BSLS_ASSERT(lhs.isSet() && rhs.isSet());
192-
193-
// Check leaseId first
194-
if (lhs.leaseId() < rhs.leaseId()) {
195-
return true; // RETURN
196-
}
197-
else if (lhs.leaseId() == rhs.leaseId()) {
198-
if (lhs.sequenceNumber() < rhs.sequenceNumber()) {
199-
return true; // RETURN
200-
}
201-
}
202-
203-
return false;
183+
return lhs.compositeSequenceNumber() < rhs.compositeSequenceNumber();
204184
}
205185

206186
inline bool m_bmqstoragetool::operator<=(
207187
const m_bmqstoragetool::CompositeSequenceNumber& lhs,
208188
const m_bmqstoragetool::CompositeSequenceNumber& rhs)
209189
{
210-
// PRECONDITIONS
211-
BSLS_ASSERT(lhs.isSet() && rhs.isSet());
212-
213-
return (lhs < rhs || lhs == rhs);
190+
return lhs.compositeSequenceNumber() <= rhs.compositeSequenceNumber();
214191
}
215192

216193
} // close enterprise namespace

0 commit comments

Comments
 (0)