Skip to content

Commit 60ade03

Browse files
committed
Started converting to using CompositeKey instead of string keys.
1 parent d087009 commit 60ade03

File tree

2 files changed

+175
-46
lines changed

2 files changed

+175
-46
lines changed

core/modules/loader/StringRange.cc

+117
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,108 @@ std::ostream& operator<<(std::ostream& os, StringRange const& strRange) {
6363
}
6464

6565

66+
void StringRange::setAllInclusiveRange() {
67+
_min = CompositeKey(0,"");
68+
_maxE = CompositeKey(CompositeKey::maxIntVal(), "");
69+
_unlimited = true;
70+
setValid();
71+
}
72+
73+
74+
/* &&&
75+
bool StringRange::setMin(std::string const& val) {
76+
if (not _unlimited && val >= _maxE) {
77+
_min = decrementString(_maxE);
78+
return false;
79+
}
80+
_min = val;
81+
return true;
82+
}
83+
*/
84+
bool StringRange::setMin(CompositeKey const& val) {
85+
if (not _unlimited && val >= _maxE) {
86+
_min = decrement(_maxE);
87+
return false;
88+
}
89+
_min = val;
90+
return true;
91+
}
92+
93+
/* &&&
94+
95+
bool StringRange::setMax(std::string const& val, bool unlimited=false) {
96+
_unlimited = unlimited;
97+
if (unlimited) {
98+
if (val > _maxE) { _maxE = val; }
99+
return true;
100+
}
101+
if (val < _min) {
102+
_maxE = incrementString(_min);
103+
return false;
104+
}
105+
_maxE = val;
106+
return true;
107+
}
108+
109+
*/
110+
bool StringRange::setMax(CompositeKey const& val, bool unlimited=false) {
111+
_unlimited = unlimited;
112+
if (unlimited) {
113+
if (val > _maxE) { _maxE = val; }
114+
return true;
115+
}
116+
if (val < _min) {
117+
_maxE = increment(_min);
118+
return false;
119+
}
120+
_maxE = val;
121+
return true;
122+
}
123+
124+
125+
/* &&&
126+
bool StringRange::setMinMax(std::string const& vMin, std::string const& vMax, bool unlimited=false) {
127+
_unlimited = unlimited;
128+
if (!unlimited && vMin > vMax) {
129+
return false;
130+
}
131+
_unlimited = unlimited;
132+
if (_unlimited) {
133+
_min = vMin;
134+
_maxE = std::max(vMax, _min); // max is irrelevant at this point
135+
} else {
136+
_min = vMin;
137+
_maxE = vMax;
138+
}
139+
setValid();
140+
return true;
141+
}
142+
*/
143+
bool StringRange::setMinMax(CompositeKey const& vMin, CompositeKey const& vMax, bool unlimited=false) {
144+
_unlimited = unlimited;
145+
if (!unlimited && vMin > vMax) {
146+
return false;
147+
}
148+
_unlimited = unlimited;
149+
if (_unlimited) {
150+
_min = vMin;
151+
_maxE = std::max(vMax, _min); // max is irrelevant at this point
152+
} else {
153+
_min = vMin;
154+
_maxE = vMax;
155+
}
156+
setValid();
157+
return true;
158+
}
159+
160+
161+
162+
66163
std::string StringRange::incrementString(std::string const& str, char appendChar) {
67164
std::string output(str);
165+
if (output.empty()) {
166+
output += appendChar;
167+
}
68168
size_t pos = output.size() - 1;
69169
char lastChar = output[pos];
70170
if (lastChar < 'z') {
@@ -77,6 +177,12 @@ std::string StringRange::incrementString(std::string const& str, char appendChar
77177
}
78178

79179

180+
CompositeKey StringRange::increment(CompositeKey const& key, char appendChar) {
181+
CompositeKey outKey(key.kInt, incrementString(key.kStr, appendChar));
182+
return outKey;
183+
}
184+
185+
80186
std::string StringRange::decrementString(std::string const& str, char minChar) {
81187
if (str.empty()) {
82188
return std::string();
@@ -95,6 +201,17 @@ std::string StringRange::decrementString(std::string const& str, char minChar) {
95201
}
96202

97203

204+
CompositeKey StringRange::decrement(CompositeKey const& key, char minChar='0') {
205+
CompositeKey outK(key);
206+
if (outK.kStr.empty()) {
207+
if (outK.kInt > 0) --outK.kInt;
208+
return outK;
209+
}
210+
outK.kStr = decrementString(outK.kStr, minChar);
211+
return outK;
212+
}
213+
214+
98215
void ProtoHelper::workerKeysInfoExtractor(BufferUdp& data, uint32_t& wId, NeighborsInfo& nInfo, StringRange& strRange) {
99216
auto funcName = "CentralWorker::_workerKeysInfoExtractor";
100217
LOGS(_log, LOG_LVL_DEBUG, funcName);

core/modules/loader/StringRange.h

+58-46
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,55 @@ namespace lsst {
3636
namespace qserv {
3737
namespace loader {
3838

39+
40+
/// &&&
41+
class CompositeKey {
42+
public:
43+
CompositeKey(uint64_t ki, std::string const& ks) : kInt(ki), kStr(ks) {}
44+
CompositeKey(uint64_t ki) : CompositeKey(ki, "") {}
45+
CompositeKey(std::string const& ks) : CompositeKey(0, ks) {}
46+
CompositeKey(CompositeKey const& ck) : CompositeKey(ck.kInt, ck.kStr) {}
47+
CompositeKey() : CompositeKey(0, "") {}
48+
~CompositeKey() = default;
49+
50+
static uint64_t maxIntVal() const { return UINT64_MAX; }
51+
52+
CompositeKey& operator=(CompositeKey const& other) {
53+
kInt = other.kInt;
54+
kStr = other.kStr;
55+
}
56+
57+
bool operator<(CompositeKey const& other) const {
58+
if (kInt < other.kInt) return true;
59+
if (kInt > other.kInt) return false;
60+
if (kStr < other.kStr) return true;
61+
return false;
62+
}
63+
64+
bool operator>(CompositeKey const& other) const {
65+
return other < *this;
66+
}
67+
68+
bool operator=(CompositeKey const& other) const {
69+
return (kInt == other.kInt) && (kStr == other.kStr);
70+
}
71+
72+
uint64_t kInt;
73+
std::string kStr;
74+
};
75+
3976
/// Class for storing the range of a single worker.
4077
/// This is likely to become a template class, hence lots in the header.
4178
/// It tries to keep its state consistent, _min < _max, but depends on
42-
/// other classes to eventually get the correct values for _min and _max
79+
/// other classes to eventually get the correct values for _min and _max.
80+
///
81+
/// When new workers are activated, they need placeholder values for
82+
/// for their ranges, as the new worker will have no keys. increment(...)
83+
/// and decrement(...) try to create reasonable key values for the ranges
84+
/// but true ranges cannot be established until the worker and its
85+
/// right neighbor (if there is one) each have at least one key. The worker
86+
/// ranges should eventually reach the master, then the other workers
87+
/// and clients.
4388
class StringRange {
4489
public:
4590
using Ptr = std::shared_ptr<StringRange>;
@@ -50,51 +95,11 @@ class StringRange {
5095

5196
~StringRange() = default;
5297

53-
void setAllInclusiveRange() {
54-
_min = "";
55-
_unlimited = true;
56-
setValid();
57-
}
58-
59-
bool setMin(std::string const& val) {
60-
if (not _unlimited && val >= _maxE) {
61-
_min = decrementString(_maxE);
62-
return false;
63-
}
64-
_min = val;
65-
return true;
66-
}
67-
68-
bool setMax(std::string const& val, bool unlimited=false) {
69-
_unlimited = unlimited;
70-
if (unlimited) {
71-
if (val > _maxE) { _maxE = val; }
72-
return true;
73-
}
74-
if (val < _min) {
75-
_maxE = incrementString(_min);
76-
return false;
77-
}
78-
_maxE = val;
79-
return true;
80-
}
98+
void setAllInclusiveRange();
8199

82-
bool setMinMax(std::string const& vMin, std::string const& vMax, bool unlimited=false) {
83-
_unlimited = unlimited;
84-
if (!unlimited && vMin > vMax) {
85-
return false;
86-
}
87-
_unlimited = unlimited;
88-
if (_unlimited) {
89-
_min = vMin;
90-
_maxE = std::max(vMax, _min); // max is irrelevant at this point
91-
} else {
92-
_min = vMin;
93-
_maxE = vMax;
94-
}
95-
setValid();
96-
return true;
97-
}
100+
bool setMin(CompositeKey const& val);
101+
bool setMax(CompositeKey const& val, bool unlimited=false);
102+
bool setMinMax(CompositeKey const& vMin, CompositeKey const& vMax, bool unlimited=false);
98103

99104
bool setValid() {
100105
_valid = (_min <= _maxE );
@@ -142,19 +147,26 @@ class StringRange {
142147
/// Return a string that would slightly follow the value of the input string 'str'
143148
/// appendChar is the character appended to a string ending with a character > 'z'
144149
static std::string incrementString(std::string const& str, char appendChar='0');
150+
/// Return a CompositeKey slightly higher value than 'key'.
151+
static CompositeKey increment(CompositeKey const& key, char appendChar='0');
145152

146153
// Return a string that would come slightly before 'str'. 'minChar' is the
147154
// smallest acceptable value for the last character before just erasing the last character.
148155
static std::string decrementString(std::string const& str, char minChar='0');
149-
156+
/// Return a CompositeKey slightly higher lower than 'key'.
157+
static CompositeKey decrement(CompositeKey const& str, char minChar='0');
150158

151159
friend std::ostream& operator<<(std::ostream&, StringRange const&);
152160

153161
private:
154162
bool _valid{false}; ///< true if range is valid
155163
bool _unlimited{false}; ///< true if the range includes largest possible values.
164+
CompositeKey _min; ///< Smallest value = ""
165+
CompositeKey _maxE; ///< maximum value exclusive
166+
/* &&&
156167
std::string _min; ///< Smallest value = ""
157168
std::string _maxE; ///< maximum value exclusive
169+
*/
158170

159171
};
160172

0 commit comments

Comments
 (0)