Skip to content

Commit 9eab341

Browse files
committed
Use fdb5::Key and fdb5::ListElement instead of custom types
1 parent 12b69e6 commit 9eab341

7 files changed

Lines changed: 49 additions & 135 deletions

File tree

src/fdb5/tools/compare/common/Types.cc

Lines changed: 14 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@ namespace compare {
1818

1919
//---------------------------------------------------------------------------------------------------------------------
2020

21-
std::ostream& operator<<(std::ostream& os, const KeyValueMap& km) {
22-
os << "{";
23-
for (const auto& [k, v] : km) {
24-
os << k << "=" << v << ", ";
25-
}
26-
os << "}";
27-
return os;
28-
}
29-
3021
std::ostream& operator<<(std::ostream& os, const KeyDiffMap& km) {
3122
os << "{";
3223
for (const auto& [k, v] : km) {
@@ -46,10 +37,6 @@ std::ostream& operator<<(std::ostream& os, const KeySet& km) {
4637
return os;
4738
}
4839

49-
std::ostream& operator<<(std::ostream& os, const DataLocation& loc) {
50-
return os << "(" << loc.path << ", " << loc.offset << ", " << loc.length << ")";
51-
}
52-
5340
std::ostream& operator<<(std::ostream& os, const DataIndex& idx) {
5441
for (const auto& [km, loc] : idx) {
5542
os << "Key: " << km << " -> Value: " << loc << "\n";
@@ -59,26 +46,6 @@ std::ostream& operator<<(std::ostream& os, const DataIndex& idx) {
5946

6047
//---------------------------------------------------------------------------------------------------------------------
6148

62-
63-
void parseKeyValues(KeyValueMap& container, const std::string& keyValueStr) {
64-
std::istringstream stream(keyValueStr);
65-
std::string entry;
66-
67-
while (std::getline(stream, entry, ',')) {
68-
std::istringstream pairStream(entry);
69-
std::string key, value;
70-
if (std::getline(pairStream, key, '=') && std::getline(pairStream, value)) {
71-
container[key] = value; // Update or insert key-value pair
72-
}
73-
}
74-
}
75-
76-
KeyValueMap parseKeyValues(const std::string& keyValueStr) {
77-
KeyValueMap container;
78-
parseKeyValues(container, keyValueStr);
79-
return container;
80-
}
81-
8249
void parseKeySet(KeySet& container, const std::string& keyStr) {
8350
std::istringstream stream(keyStr);
8451
std::string entry;
@@ -94,11 +61,11 @@ KeySet parseKeySet(const std::string& keyStr) {
9461
return container;
9562
}
9663

97-
compare::KeyDiffMap requestDiff(const KeyValueMap& l, const KeyValueMap& r) {
64+
compare::KeyDiffMap requestDiff(const fdb5::Key& l, const fdb5::Key& r) {
9865
compare::KeyDiffMap res;
9966
for (const auto& [lk, lv] : l) {
100-
auto search = r.find(lk);
101-
if (search != r.end()) {
67+
auto [search, isValid] = r.find(lk);
68+
if (isValid) {
10269
if (search->second != lv) {
10370
res.insert({lk, {lv, search->second}});
10471
}
@@ -109,8 +76,8 @@ compare::KeyDiffMap requestDiff(const KeyValueMap& l, const KeyValueMap& r) {
10976
}
11077

11178
for (const auto& [rk, rv] : r) {
112-
auto search = l.find(rk);
113-
if (search == l.end()) {
79+
auto [search, isValid] = l.find(rk);
80+
if (!isValid) {
11481
res.insert({rk, {{}, rv}});
11582
}
11683
}
@@ -120,21 +87,15 @@ compare::KeyDiffMap requestDiff(const KeyValueMap& l, const KeyValueMap& r) {
12087
//---------------------------------------------------------------------------------------------------------------------
12188

12289

123-
KeyValueMap applyKeyDiff(KeyValueMap k, const KeyDiffMap& diff, bool swapDiff) {
90+
fdb5::Key applyKeyDiff(fdb5::Key k, const KeyDiffMap& diff, bool swapDiff) {
12491
for (const auto& [field, val_pair] : diff) {
12592
const auto& val = swapDiff ? val_pair.first : val_pair.second;
12693
if (val) {
127-
auto it = k.find(field);
128-
if (it != k.end()) {
129-
it->second = *val; // replace value
130-
}
131-
else {
132-
k.insert({field, *val});
133-
}
94+
k.set(field, *val);
13495
}
13596
else {
13697
// Delete
137-
k.erase(field);
98+
k.unset(field);
13899
}
139100
}
140101
return k; // return modified copy
@@ -265,31 +226,26 @@ void Result::update(const Result& other) {
265226
//---------------------------------------------------------------------------------------------------------------------
266227

267228

268-
bool isSubset(const KeyValueMap& a, const KeyValueMap& b) {
229+
bool isSubset(const fdb5::Key& a, const fdb5::Key& b) {
269230
for (const auto& kv : a) {
270-
auto it = b.find(kv.first);
271-
if (it == b.end() || it->second != kv.second)
231+
auto [it, isValid] = b.find(kv.first);
232+
if (!isValid || it->second != kv.second)
272233
return false;
273234
}
274235
return true;
275236
}
276237

277238

278-
DataIndex assembleCompareMap(fdb5::FDB& fdb, const fdb5::FDBToolRequest& req, const KeyValueMap& ignore) {
239+
DataIndex assembleCompareMap(fdb5::FDB& fdb, const fdb5::FDBToolRequest& req, const fdb5::Key& ignore) {
279240
DataIndex out;
280241

281242
auto list = fdb.list(req);
282243
fdb5::ListElement elem;
283244

284245
while (list.next(elem)) {
285-
KeyValueMap km;
286-
for (const auto& bit : elem.keys()) {
287-
auto d = bit.keyDict();
288-
km.insert(d.begin(), d.end());
289-
}
246+
fdb5::Key km = elem.combinedKey();
290247
if (ignore.empty() || !isSubset(ignore, km)) {
291-
out.emplace(km, DataLocation{elem.location().uri().path(), static_cast<long long>(elem.location().offset()),
292-
static_cast<long long>(elem.location().length())});
248+
out.emplace(km, elem);
293249
}
294250
}
295251
eckit::Log::info() << "[LOG] FDB request: " << req << " resulted in " << out.size() << " entries.\n";

src/fdb5/tools/compare/common/Types.h

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#pragma once
1717
#include "fdb5/api/FDB.h"
1818
#include "fdb5/api/helpers/FDBToolRequest.h"
19+
#include "fdb5/api/helpers/ListElement.h"
1920

2021
#include <iostream>
2122
#include <map>
@@ -28,16 +29,6 @@ namespace compare {
2829

2930
//---------------------------------------------------------------------------------------------------------------------
3031

31-
/// Describes the location of a message
32-
struct DataLocation {
33-
std::string path;
34-
long long offset = 0;
35-
long long length = 0;
36-
};
37-
38-
/// String-string map which is used to store FDB keys (e.g. {class=rd, expver=1234, ...})
39-
using KeyValueMap = std::map<std::string, std::string>;
40-
4132
/// A pair describing differences between two key-value pairs in a requests, e.g. expver=1234 or expver=2345
4233
/// Each value is optional - to indicate if a key is missing in one the comparing pairs.
4334
using ValueDiff = std::pair<std::optional<std::string>, std::optional<std::string>>;
@@ -50,67 +41,46 @@ using KeySet = std::unordered_set<std::string>;
5041

5142
} // namespace compare
5243

53-
template <>
54-
struct std::hash<compare::KeyValueMap> {
55-
size_t operator()(const compare::KeyValueMap& m) const noexcept {
56-
size_t h = 0;
57-
std::hash<std::string> hs;
58-
for (const auto& kv : m) {
59-
h ^= (hs(kv.first) * 1315423911u) ^ hs(kv.second);
60-
}
61-
return h;
62-
}
63-
};
64-
65-
template <>
66-
struct std::equal_to<compare::KeyValueMap> {
67-
bool operator()(const compare::KeyValueMap& a, const compare::KeyValueMap& b) const noexcept { return a == b; }
68-
};
69-
7044
namespace compare {
7145

7246
/// Creates a map of full specificed FDB keys (as a KeyValueMap),
7347
/// mapping to a data location which describes where the message can be loaded from.
74-
using DataIndex = std::unordered_map<KeyValueMap, DataLocation>;
48+
using DataIndex = std::unordered_map<fdb5::Key, fdb5::ListElement>;
7549

7650
/// Runs a request on a fdb to create a list of filtered entries to compared on.
7751
///
7852
/// @param fdb FDB on which to operate on
7953
/// @param req Request which performs a selection on the fdb
8054
/// @param ignore Map of key-value pairs to ignore
8155
/// @return filtered DataIndex
82-
DataIndex assembleCompareMap(fdb5::FDB& fdb, const fdb5::FDBToolRequest& req, const KeyValueMap& ignore);
56+
DataIndex assembleCompareMap(fdb5::FDB& fdb, const fdb5::FDBToolRequest& req, const fdb5::Key& ignore);
8357

8458
//---------------------------------------------------------------------------------------------------------------------
8559

86-
std::ostream& operator<<(std::ostream& os, const KeyValueMap& km);
87-
8860
std::ostream& operator<<(std::ostream& os, const KeyDiffMap& km);
8961

9062
std::ostream& operator<<(std::ostream& os, const KeySet& km);
9163

92-
std::ostream& operator<<(std::ostream& os, const DataLocation& loc);
93-
9464
std::ostream& operator<<(std::ostream& os, const DataIndex& idx);
9565

9666

9767
//---------------------------------------------------------------------------------------------------------------------
9868

99-
void parseKeyValues(KeyValueMap& container, const std::string& keyValueStr);
100-
KeyValueMap parseKeyValues(const std::string& keyValueStr);
69+
// void parseKeyValues(KeyValueMap& container, const std::string& keyValueStr);
70+
// KeyValueMap parseKeyValues(const std::string& keyValueStr);
10171

10272
void parseKeySet(KeySet& container, const std::string& keyValueStr);
10373
KeySet parseKeySet(const std::string& keyValueStr);
10474

105-
compare::KeyDiffMap requestDiff(const KeyValueMap& l, const KeyValueMap& r);
75+
compare::KeyDiffMap requestDiff(const fdb5::Key& l, const fdb5::Key& r);
10676

10777

10878
/// @param km Map of keys with values
10979
/// @param diff Map of keys with differences
11080
/// @param swapPair Optional - If false the second pair of the difference is put in the resulting request.
11181
/// If true the first one is used.
11282
/// @return New KeyValueMap with differences applied
113-
KeyValueMap applyKeyDiff(KeyValueMap km, const KeyDiffMap& diff, bool swapPair = false);
83+
fdb5::Key applyKeyDiff(fdb5::Key km, const KeyDiffMap& diff, bool swapPair = false);
11484

11585

11686
//---------------------------------------------------------------------------------------------------------------------
@@ -141,14 +111,14 @@ struct Options {
141111
Method method = Method::KeyByKey;
142112

143113
// For MARS comparison:
144-
KeyValueMap ignoreMarsKeys;
114+
fdb5::Key ignoreMarsKeys;
145115

146116
// Expected differences between two requets (e.g. comparing different expver)
147117
KeyDiffMap marsReqDiff;
148118

149119
// Optional explicit requests to compare different subtrees, e.g. expver=1111 with expver=2222
150-
std::optional<KeyValueMap> referenceRequest;
151-
std::optional<KeyValueMap> testRequest;
120+
std::optional<fdb5::Key> referenceRequest;
121+
std::optional<fdb5::Key> testRequest;
152122

153123
// List of specific grib keys which are used for comparison only
154124
KeySet gribKeysSelect;

src/fdb5/tools/compare/fdb-compare.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void FDBCompare::init(const CmdArgs& args) {
139139

140140
std::string tmp = args.getString("mars-keys-ignore", "");
141141
if (!tmp.empty()) {
142-
parseKeyValues(opts_.ignoreMarsKeys, tmp);
142+
opts_.ignoreMarsKeys = fdb5::Key::parse(tmp);
143143
}
144144

145145
if (args.has("reference-request")) {
@@ -218,10 +218,10 @@ void FDBCompare::execute(const CmdArgs& args) {
218218
})();
219219

220220
if (refReqString_) {
221-
opts_.referenceRequest = parseKeyValues(*refReqString_);
221+
opts_.referenceRequest = fdb5::Key::parse(*refReqString_);
222222
}
223223
if (testReqString_) {
224-
opts_.testRequest = parseKeyValues(*testReqString_);
224+
opts_.testRequest = fdb5::Key::parse(*testReqString_);
225225
}
226226

227227
const auto pickReq = [&](const std::optional<std::string>& s) -> fdb5::FDBToolRequest {

src/fdb5/tools/compare/grib/Compare.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "Utils.h"
1717

1818
#include "eckit/exception/Exceptions.h"
19+
#include "fdb5/api/helpers/ListElement.h"
1920
#include "metkit/codes/api/CodesAPI.h"
2021

2122

@@ -27,13 +28,13 @@ namespace compare::grib {
2728
using namespace eckit;
2829

2930

30-
CompareResult gribCompareBitExact(const DataLocation& gribLocRef, const DataLocation& gribLocTest, const Options& o,
31+
CompareResult gribCompareBitExact(const fdb5::ListElement& gribLocRef, const fdb5::ListElement& gribLocTest, const Options& o,
3132
const uint8_t* bufferRef, const uint8_t* bufferTest) {
3233
if (o.verbose) {
3334
eckit::Log::info() << "[LOG] Memory comparison (Bytestream)" << std::endl;
3435
}
3536
// Bitexact comparison directly on the Bytestream begin
36-
auto res = bitComparison(bufferRef, bufferTest, (gribLocTest.length));
37+
auto res = bitComparison(bufferRef, bufferTest, gribLocTest.length());
3738
if (res == CompareResult::OtherMismatch) {
3839
std::ostringstream oss;
3940
oss << "[GRIB COMPARISON MISMATCH: BITEXACT: HEADER ] {Location,offset,length} = " << gribLocRef;
@@ -43,7 +44,7 @@ CompareResult gribCompareBitExact(const DataLocation& gribLocRef, const DataLoca
4344
}
4445

4546

46-
CompareResult gribCompareHash(const DataLocation& gribLocRef, const DataLocation& gribLocTest, const Options& o,
47+
CompareResult gribCompareHash(const fdb5::ListElement& gribLocRef, const fdb5::ListElement& gribLocTest, const Options& o,
4748
const metkit::codes::CodesHandle& hRef, const metkit::codes::CodesHandle& hTest) {
4849
if (!compareHeader(hRef, hTest)) {
4950
std::ostringstream oss;
@@ -58,7 +59,7 @@ CompareResult gribCompareHash(const DataLocation& gribLocRef, const DataLocation
5859
}
5960

6061

61-
CompareResult gribCompareKeyByKey(const DataLocation& gribLocRef, const DataLocation& gribLocTest, const Options& o,
62+
CompareResult gribCompareKeyByKey(const fdb5::ListElement& gribLocRef, const fdb5::ListElement& gribLocTest, const Options& o,
6263
const metkit::codes::CodesHandle& hRef, const metkit::codes::CodesHandle& hTest) {
6364
auto res = compareKeyByKey(hRef, hTest, o.gribKeysIgnore, o.gribKeysSelect);
6465
if (res == CompareResult::OtherMismatch) {
@@ -72,15 +73,15 @@ CompareResult gribCompareKeyByKey(const DataLocation& gribLocRef, const DataLoca
7273
}
7374

7475

75-
Result gribCompareSingleMessage(const DataLocation& gribLocRef, const DataLocation& gribLocTest, const Options& o) {
76+
Result gribCompareSingleMessage(const fdb5::ListElement& gribLocRef, const fdb5::ListElement& gribLocTest, const Options& o) {
7677
Result res;
7778
res.match = true;
7879

7980
std::unique_ptr<uint8_t[]> bufferRef = extractGribMessage(gribLocRef);
8081
std::unique_ptr<uint8_t[]> bufferTest = extractGribMessage(gribLocTest);
8182

82-
auto hRef = metkit::codes::codesHandleFromMessage({bufferRef.get(), static_cast<size_t>(gribLocRef.length)});
83-
auto hTest = metkit::codes::codesHandleFromMessage({bufferTest.get(), static_cast<size_t>(gribLocTest.length)});
83+
auto hRef = metkit::codes::codesHandleFromMessage({bufferRef.get(), static_cast<size_t>(gribLocRef.length())});
84+
auto hTest = metkit::codes::codesHandleFromMessage({bufferTest.get(), static_cast<size_t>(gribLocTest.length())});
8485

8586
// Non-numeric comparisons
8687
CompareResult methodRes = ([&]() {

0 commit comments

Comments
 (0)