Skip to content

Commit 89b0b75

Browse files
committed
Merge branch 'master' into upgrade-to-the-latest-storage-version
2 parents 6dac040 + 97a1047 commit 89b0b75

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

convert_old_db/convert.cc

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,74 @@ std::string GenericUpdate(const std::chrono::microseconds timestamp, const std::
7070
return converted_json;
7171
}
7272

73+
template <>
74+
std::string GenericUpdate<new_ctfo::Card, Persisted_CardUpdated>(const std::chrono::microseconds timestamp,
75+
const std::vector<std::string>& tsv) {
76+
assert(tsv.size() == 3u);
77+
78+
current::storage::Transaction<T_PERSISTED_VARIANT> transaction;
79+
80+
transaction.meta.timestamp = timestamp;
81+
transaction.mutations.emplace_back(Persisted_CardUpdated());
82+
83+
std::string json = JSON(transaction);
84+
std::string subjson = "{\"data\":" + JSON(new_ctfo::Card()) + "}";
85+
const size_t offset = json.find(subjson);
86+
assert(offset != std::string::npos);
87+
88+
std::string converted_json = json.substr(0u, offset) + tsv[2] + json.substr(offset + subjson.length());
89+
const std::string sub_ms = "\"ms\":";
90+
const size_t offset_ms = converted_json.find(sub_ms, offset);
91+
if (offset != std::string::npos) {
92+
const size_t offset_ms_start = offset_ms + sub_ms.length();
93+
const size_t offset_ms_end = converted_json.find(',', offset_ms);
94+
assert(offset_ms_end != std::string::npos);
95+
std::string ms_value = converted_json.substr(offset_ms_start, offset_ms_end - offset_ms_start);
96+
std::string sub_us = "\"us\":" + ms_value + "000"; // convert `ms` to `us` by adding 000 to the end
97+
converted_json = converted_json.replace(offset_ms, offset_ms_end - offset_ms, sub_us);
98+
}
99+
100+
// Now, the real thing: Detect those moments the counters go down.
101+
using T_TRANSACTION = current::storage::Transaction<T_PERSISTED_VARIANT>;
102+
auto result = ParseJSON<T_TRANSACTION>(converted_json);
103+
auto& data = Value<Persisted_CardUpdated>(result.mutations[0]).data;
104+
auto cid = data.cid;
105+
106+
static std::map<CID, size_t> ctfo_count_map;
107+
static std::map<CID, size_t> tfu_count_map;
108+
static std::map<CID, size_t> skip_count_map;
109+
110+
size_t& ctfo_count = ctfo_count_map[cid];
111+
size_t& tfu_count = tfu_count_map[cid];
112+
size_t& skip_count = skip_count_map[cid];
113+
114+
if (ctfo_count && data.ctfo_count <= ctfo_count) {
115+
std::cerr << "ctfo_count: " << ctfo_count << " => " << data.ctfo_count;
116+
std::cerr << ", CID= " << static_cast<uint64_t>(cid) << std::endl;
117+
data.ctfo_count = ++ctfo_count;
118+
} else {
119+
ctfo_count = data.ctfo_count;
120+
}
121+
122+
if (tfu_count && data.tfu_count <= tfu_count) {
123+
std::cerr << "tfu_count: " << tfu_count << " => " << data.tfu_count;
124+
std::cerr << ", CID= " << static_cast<uint64_t>(cid) << std::endl;
125+
data.tfu_count = ++tfu_count;
126+
} else {
127+
tfu_count = data.tfu_count;
128+
}
129+
130+
if (skip_count && data.skip_count <= skip_count) {
131+
std::cerr << "skip_count: " << skip_count << " => " << data.skip_count;
132+
std::cerr << ", CID= " << static_cast<uint64_t>(cid) << std::endl;
133+
data.skip_count = ++skip_count;
134+
} else {
135+
skip_count = data.skip_count;
136+
}
137+
138+
return JSON(result);
139+
}
140+
73141
// Special case to convert `UIDAndCID` into `std::pair<UID, CID>`.
74142
template <typename T_RECORD, typename T_PERSISTED_RECORD>
75143
std::string StarredNotificationsSentDictionaryUpdate(const std::chrono::microseconds timestamp,

0 commit comments

Comments
 (0)