• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / 2407

10 Jun 2024 05:10PM UTC coverage: 90.951% (-0.02%) from 90.975%
2407

push

Evergreen

web-flow
RCORE-2149 Restore behavior of Sync_UploadDownloadProgress_1 test (#7773)

102066 of 180372 branches covered (56.59%)

52 of 52 new or added lines in 1 file covered. (100.0%)

113 existing lines in 10 files now uncovered.

214568 of 235916 relevant lines covered (90.95%)

6161477.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.47
/src/realm/sync/noinst/changeset_index.cpp
1
#include <realm/sync/noinst/changeset_index.hpp>
2
#include <realm/sync/object_id.hpp>
3

4
#include <iterator> // std::distance, std::advance
5

6
using namespace realm::sync;
7

8
namespace realm {
9
namespace _impl {
10

11
#if REALM_DEBUG
12
static bool compare_ranges(const Changeset::Range& left, const Changeset::Range& right)
13
{
3,436✔
14
    return left.begin < right.begin;
3,436✔
15
}
3,436✔
16

17
template <class InputIterator>
18
static bool check_ranges(InputIterator begin, InputIterator end)
19
{
77,892✔
20
    if (!std::is_sorted(begin, end, compare_ranges))
77,892✔
21
        return false;
×
22

23
    // Check that there are no overlaps
24
    if (begin != end) {
77,894✔
25
        auto last_end = begin->end;
77,888✔
26
        for (auto i = begin + 1; i != end; ++i) {
81,324✔
27
            if (last_end > i->begin)
3,436✔
28
                return false;
×
29
            last_end = i->end;
3,436✔
30
        }
3,436✔
31
    }
77,888✔
32
    return true;
77,892✔
33
}
77,892✔
34

35
static bool check_ranges(const ChangesetIndex::Ranges& ranges)
36
{
286,164✔
37
    for (auto& pair : ranges) {
286,164✔
38
        if (!check_ranges(pair.second.begin(), pair.second.end()))
77,894✔
39
            return false;
×
40
    }
77,894✔
41
    return true;
286,164✔
42
}
286,164✔
43
#endif // REALM_DEBUG
44

45

46
void ChangesetIndex::clear() noexcept
47
{
4,434✔
48
    m_object_instructions.clear();
4,434✔
49
    m_schema_instructions.clear();
4,434✔
50
    m_conflict_groups_owner.clear();
4,434✔
51
    m_num_conflict_groups = 0;
4,434✔
52
}
4,434✔
53

54

55
void ChangesetIndex::scan_changeset(Changeset& changeset)
56
{
610,840✔
57
    if (m_contains_destructive_schema_changes)
610,840✔
58
        return;
74,862✔
59

60
#if defined(REALM_DEBUG) // LCOV_EXCL_START
535,978✔
61
    for (auto& confict_group : m_conflict_groups_owner) {
5,879,832✔
62
        // Check that add_changeset() has not been called yet.
63
        REALM_ASSERT(confict_group.ranges.empty());
5,879,832✔
64
    }
5,879,832✔
65
#endif // REALM_DEBUG LCOV_EXCL_STOP
535,978✔
66

67
    using Instruction = realm::sync::Instruction;
535,978✔
68

69
    for (auto it = changeset.begin(); it != changeset.end(); ++it) {
1,163,034✔
70
        if (!*it)
631,490✔
71
            continue;
1,654✔
72

73
        const auto& instr = **it;
629,836✔
74

75
        if (auto add_table_instr = instr.get_if<Instruction::AddTable>()) {
629,836✔
76
            auto& p = *add_table_instr;
15,696✔
77
            schema_conflict_group(changeset.get_string(p.table));
15,696✔
78
        }
15,696✔
79
        else if (instr.get_if<Instruction::EraseTable>()) {
614,140✔
80
            m_contains_destructive_schema_changes = true;
4,434✔
81
            clear();
4,434✔
82
            return;
4,434✔
83
        }
4,434✔
84
        else if (auto add_column_instr = instr.get_if<Instruction::AddColumn>()) {
609,706✔
85
            auto& p = *add_column_instr;
38,162✔
86
            StringData table_name = changeset.get_string(p.table);
38,162✔
87
            ConflictGroup& cg = schema_conflict_group(table_name);
38,162✔
88
            if (p.type == Instruction::Payload::Type::Link) {
38,162✔
89
                StringData target_table = changeset.get_string(p.link_target_table);
4,564✔
90
                ConflictGroup& cg2 = schema_conflict_group(target_table);
4,564✔
91
                merge_conflict_groups(cg, cg2);
4,564✔
92
            }
4,564✔
93
        }
38,162✔
94
        else if (instr.get_if<Instruction::EraseColumn>()) {
571,544✔
95
            m_contains_destructive_schema_changes = true;
×
96
            clear();
×
97
            return;
×
98
        }
×
99
        else {
571,544✔
100
            GlobalID ids[2];
571,544✔
101
            size_t num_ids = get_object_ids_in_instruction(changeset, instr, ids, 2);
571,544✔
102
            REALM_ASSERT(num_ids >= 1);
571,544✔
103
            REALM_ASSERT(num_ids <= 2);
571,544✔
104

105
            ConflictGroup& cg = object_conflict_group(ids[0]);
571,544✔
106
            for (size_t i = 1; i < num_ids; ++i) {
571,900✔
107
                ConflictGroup& cg2 = object_conflict_group(ids[1]);
356✔
108
                merge_conflict_groups(cg, cg2);
356✔
109
            }
356✔
110
        }
571,544✔
111
    }
629,836✔
112
}
535,978✔
113

114

115
void ChangesetIndex::merge_conflict_groups(ConflictGroup& into, ConflictGroup& from)
116
{
4,936✔
117
    if (&into == &from)
4,936✔
118
        return;
3,252✔
119

120
    if (from.size > into.size) {
1,684✔
121
        // This is an optimization. The time it takes to merge two conflict
122
        // groups is proportional to the size of the incoming group (in number
123
        // of objects touched). If the incoming group is larger, merge the other
124
        // way.
125
        merge_conflict_groups(from, into);
16✔
126
        return;
16✔
127
    }
16✔
128

129
    REALM_ASSERT(into.ranges.empty());
1,668✔
130
    REALM_ASSERT(from.ranges.empty());
1,668✔
131

132
    for (auto& class_name : from.schemas) {
1,668✔
133
        m_schema_instructions[class_name] = &into;
1,344✔
134
    }
1,344✔
135
    into.schemas.insert(into.schemas.end(), from.schemas.begin(), from.schemas.end());
1,668✔
136

137
    for (auto& pair : from.objects) {
1,668✔
138
        auto& objset = into.objects[pair.first];
324✔
139
        auto& objinstr = m_object_instructions[pair.first];
324✔
140
        for (auto& object : pair.second) {
324✔
141
            objinstr[object] = &into;
324✔
142
        }
324✔
143
        objset.insert(objset.end(), pair.second.begin(), pair.second.end());
324✔
144
    }
324✔
145
    into.size += from.size;
1,668✔
146

147
    m_conflict_groups_owner.erase(from.self_it);
1,668✔
148
    --m_num_conflict_groups;
1,668✔
149
}
1,668✔
150

151

152
void ChangesetIndex::add_changeset(Changeset& log)
153
{
71,350✔
154
    if (!log.empty())
71,350✔
155
        m_everything[&log] = std::vector<Changeset::Range>(1, Changeset::Range{log.begin(), log.end()});
65,982✔
156

157
    if (m_contains_destructive_schema_changes)
71,350✔
158
        return; // Just add to everything.
4,434✔
159

160
    using Instruction = realm::sync::Instruction;
66,916✔
161

162
    // Iterate over all instructions (skipping tombstones), and add them to the
163
    // index.
164
    for (auto it = log.begin(); it != log.end(); ++it) {
190,368✔
165
        if (!*it)
123,452✔
166
            continue;
×
167

168
        const auto& instr = **it;
123,452✔
169

170
        if (auto add_table_instr = instr.get_if<Instruction::AddTable>()) {
123,452✔
171
            StringData table = log.get_string(add_table_instr->table);
4,438✔
172
            auto& cg = schema_conflict_group(table);
4,438✔
173
            add_instruction_at(cg.ranges, log, it);
4,438✔
174
        }
4,438✔
175
        else if (instr.get_if<Instruction::EraseTable>()) {
119,014✔
176
            REALM_TERMINATE("Call scan_changeset() before add_changeset().");
177
        }
×
178
        else if (auto add_column_instr = instr.get_if<Instruction::AddColumn>()) {
119,014✔
179
            auto& p = *add_column_instr;
10,812✔
180
            StringData table = log.get_string(p.table);
10,812✔
181
            auto& cg = schema_conflict_group(table);
10,812✔
182
            if (p.type == Instruction::Payload::Type::Link) {
10,812✔
183
                REALM_ASSERT(&cg == &schema_conflict_group(log.get_string(p.link_target_table)));
2,282✔
184
            }
2,282✔
185
            add_instruction_at(cg.ranges, log, it);
10,812✔
186
        }
10,812✔
187
        else if (instr.get_if<Instruction::EraseColumn>()) {
108,202✔
188
            REALM_TERMINATE("Call scan_changeset() before add_changeset().");
189
        }
×
190
        else {
108,202✔
191
            GlobalID ids[2];
108,202✔
192
            size_t num_ids = get_object_ids_in_instruction(log, instr, ids, 2);
108,202✔
193
            REALM_ASSERT(num_ids >= 1);
108,202✔
194
            REALM_ASSERT(num_ids <= 2);
108,202✔
195

196
            auto& cg = object_conflict_group(ids[0]);
108,202✔
197
            for (size_t i = 1; i < num_ids; ++i) {
108,410✔
198
                REALM_ASSERT(&cg == &object_conflict_group(ids[i]));
208✔
199
            }
208✔
200
            add_instruction_at(cg.ranges, log, it);
108,202✔
201
        }
108,202✔
202
    }
123,452✔
203
}
66,916✔
204

205
size_t get_object_ids_in_instruction(const Changeset& changeset, const sync::Instruction& instr,
206
                                     ChangesetIndex::GlobalID* ids, size_t max_num_ids)
207
{
1,305,444✔
208
    REALM_ASSERT_RELEASE(max_num_ids >= 2);
1,305,444✔
209

210
    using Instruction = realm::sync::Instruction;
1,305,444✔
211

212
    if (auto obj_instr = instr.get_if<Instruction::ObjectInstruction>()) {
1,305,444✔
213
        ids[0] = {changeset.get_string(obj_instr->table), changeset.get_key(obj_instr->object)};
1,304,274✔
214

215
        if (auto set_instr = instr.get_if<Instruction::Update>()) {
1,304,274✔
216
            auto& p = *set_instr;
639,998✔
217
            if (p.value.type == Instruction::Payload::Type::Link) {
639,998✔
218
                ids[1] = {changeset.get_string(p.value.data.link.target_table),
268✔
219
                          changeset.get_key(p.value.data.link.target)};
268✔
220
                return 2;
268✔
221
            }
268✔
222
        }
639,998✔
223
        else if (auto insert_instr = instr.get_if<Instruction::ArrayInsert>()) {
664,276✔
224
            auto& p = *insert_instr;
85,858✔
225
            if (p.value.type == Instruction::Payload::Type::Link) {
85,858✔
226
                ids[1] = {changeset.get_string(p.value.data.link.target_table),
636✔
227
                          changeset.get_key(p.value.data.link.target)};
636✔
228
                return 2;
636✔
229
            }
636✔
230
        }
85,858✔
231

232
        return 1;
1,303,370✔
233
    }
1,304,274✔
234

235
    return 0;
1,170✔
236
}
1,305,444✔
237

238
auto ChangesetIndex::get_schema_changes_for_class(StringData class_name) const -> const Ranges*
239
{
1,018✔
240
    return const_cast<ChangesetIndex*>(this)->get_schema_changes_for_class(class_name);
1,018✔
241
}
1,018✔
242

243
auto ChangesetIndex::get_schema_changes_for_class(StringData class_name) -> Ranges*
244
{
1,018✔
245
    if (m_contains_destructive_schema_changes)
1,018✔
246
        return &m_everything;
×
247
    auto it = m_schema_instructions.find(class_name);
1,018✔
248
    if (it == m_schema_instructions.end())
1,018✔
249
        return &m_empty;
×
250
    return &it->second->ranges;
1,018✔
251
}
1,018✔
252

253
auto ChangesetIndex::get_modifications_for_object(GlobalID id) -> Ranges*
254
{
729,624✔
255
    if (m_contains_destructive_schema_changes)
729,624✔
256
        return &m_everything;
115,548✔
257
    auto it = m_object_instructions.find(id.table_name);
614,076✔
258
    if (it == m_object_instructions.end())
614,076✔
259
        return &m_empty;
×
260

261
    auto& object_instructions = it->second;
614,076✔
262
    auto it2 = object_instructions.find(id.object_id);
614,076✔
263
    if (it2 == object_instructions.end())
614,076✔
264
        return &m_empty;
×
265
    return &it2->second->ranges;
614,076✔
266
}
614,076✔
267

268
auto ChangesetIndex::get_modifications_for_object(GlobalID id) const -> const Ranges*
269
{
206,674✔
270
    return const_cast<ChangesetIndex*>(this)->get_modifications_for_object(id);
206,674✔
271
}
206,674✔
272

273
auto ChangesetIndex::schema_conflict_group(StringData class_name) -> ConflictGroup&
274
{
75,954✔
275
    auto& conflict_group = m_schema_instructions[class_name];
75,954✔
276
    if (conflict_group == nullptr) {
75,954✔
277
        m_conflict_groups_owner.emplace_back();
23,928✔
278
        ++m_num_conflict_groups;
23,928✔
279
        auto new_cg = std::prev(m_conflict_groups_owner.end());
23,928✔
280
        new_cg->schemas.push_back(class_name);
23,928✔
281
        new_cg->size = 1;
23,928✔
282
        new_cg->self_it = new_cg;
23,928✔
283
        conflict_group = &*new_cg;
23,928✔
284
    }
23,928✔
285
    return *conflict_group;
75,954✔
286
}
75,954✔
287

288
auto ChangesetIndex::object_conflict_group(const GlobalID& object_id) -> ConflictGroup&
289
{
679,936✔
290
    auto& objects_for_table = m_object_instructions[object_id.table_name];
679,936✔
291
    auto& conflict_group = objects_for_table[object_id.object_id];
679,936✔
292
    if (conflict_group == nullptr) {
679,936✔
293
        m_conflict_groups_owner.emplace_back();
306,380✔
294
        ++m_num_conflict_groups;
306,380✔
295
        auto new_cg = std::prev(m_conflict_groups_owner.end());
306,380✔
296
        new_cg->objects[object_id.table_name].push_back(object_id.object_id);
306,380✔
297
        new_cg->size = 1;
306,380✔
298
        new_cg->self_it = new_cg;
306,380✔
299
        conflict_group = &*new_cg;
306,380✔
300
    }
306,380✔
301
    return *conflict_group;
679,936✔
302
}
679,936✔
303

304
auto ChangesetIndex::erase_instruction(RangeIterator pos) -> RangeIterator
305
{
20,238✔
306
    Changeset* changeset = pos.m_outer->first;
20,238✔
307
    pos.check();
20,238✔
308
    auto new_pos = pos;
20,238✔
309
    new_pos.m_pos = changeset->erase_stable(pos.m_pos);
20,238✔
310

311
    if (new_pos.m_pos >= new_pos.m_inner->end) {
20,238✔
312
        // erased the last instruction in the range, move to the next range.
313
        REALM_ASSERT(new_pos.m_inner < new_pos.m_outer->second.end());
8,820✔
314
        ++new_pos.m_inner;
8,820✔
315
        if (new_pos.m_inner == new_pos.m_outer->second.end()) {
8,820✔
316
            REALM_ASSERT(new_pos.m_outer != new_pos.m_ranges->end());
8,818✔
317
            ++new_pos.m_outer;
8,818✔
318
            if (new_pos.m_outer == new_pos.m_ranges->end()) {
8,818✔
319
                return RangeIterator{new_pos.m_ranges, RangeIterator::end_tag{}}; // avoid new_pos.check()
7,142✔
320
            }
7,142✔
321
            else {
1,676✔
322
                new_pos.m_inner = new_pos.m_outer->second.begin();
1,676✔
323
                REALM_ASSERT(new_pos.m_inner != new_pos.m_outer->second.end());
1,676✔
324
                new_pos.m_pos = new_pos.m_inner->begin;
1,676✔
325
            }
1,676✔
326
        }
8,818✔
327
        else {
2✔
328
            new_pos.m_pos = new_pos.m_inner->begin;
2✔
329
            REALM_ASSERT(new_pos.m_pos != new_pos.m_inner->end); // empty ranges not allowed
2✔
330
        }
2✔
331
    }
8,820✔
332
    new_pos.check();
13,096✔
333
    return new_pos;
13,096✔
334
}
20,238✔
335

336
#if REALM_DEBUG
337
static std::ostream& operator<<(std::ostream& os, GlobalID gid)
338
{
×
339
    return os << gid.table_name << "/" << format_pk(gid.object_id);
×
340
}
×
341

342
void ChangesetIndex::print(std::ostream& os) const
343
{
×
344
    auto print_ranges = [&](auto& subjects, const Ranges& ranges) {
×
345
        std::sort(subjects.begin(), subjects.end());
×
346
        subjects.erase(std::unique(subjects.begin(), subjects.end()), subjects.end());
×
347

348
        os << "[";
×
349
        for (auto it = subjects.begin(); it != subjects.end();) {
×
350
            os << *it;
×
351
            auto next = it;
×
352
            ++next;
×
353
            if (next != subjects.end())
×
354
                os << ", ";
×
355
            it = next;
×
356
        }
×
357
        os << "]: ";
×
358
        for (auto it = ranges.begin(); it != ranges.end();) {
×
359
            os << "Changeset" << std::dec << it->first->version << "(";
×
360
            for (auto it2 = it->second.begin(); it2 != it->second.end(); ++it2) {
×
361
                auto offset = std::distance(it->first->begin(), it2->begin);
×
362
                auto length = std::distance(it2->begin, it2->end);
×
363
                os << "[" << std::dec << offset << "+" << length << "]";
×
364
                if (it2 + 1 != it->second.end())
×
365
                    os << ", ";
×
366
            }
×
367
            os << ")";
×
368
            auto next = it;
×
369
            ++next;
×
370
            if (next != ranges.end())
×
371
                os << ", ";
×
372
            it = next;
×
373
        }
×
374
    };
×
375

376
    std::map<Ranges*, std::vector<StringData>> schema_modifications;
×
377
    std::map<Ranges*, std::vector<GlobalID>> object_modifications;
×
378

379
    for (auto& pair : m_schema_instructions) {
×
380
        schema_modifications[&pair.second->ranges].push_back(pair.first);
×
381
    }
×
382

383
    for (auto& pair : m_object_instructions) {
×
384
        for (auto& pair2 : pair.second) {
×
385
            object_modifications[&pair2.second->ranges].push_back(GlobalID{pair.first, pair2.first});
×
386
        }
×
387
    }
×
388

389
    if (schema_modifications.size()) {
×
390
        os << "SCHEMA MODIFICATIONS:\n";
×
391
        for (auto& pair : schema_modifications) {
×
392
            print_ranges(pair.second, *pair.first);
×
393
            os << "\n";
×
394
        }
×
395
        os << "\n";
×
396
    }
×
397

398
    if (object_modifications.size()) {
×
399
        os << "OBJECT MODIFICATIONS:\n";
×
400
        for (auto& pair : object_modifications) {
×
401
            print_ranges(pair.second, *pair.first);
×
402
            os << "\n";
×
403
        }
×
404
        os << "\n";
×
405
    }
×
406
}
×
407

408
void ChangesetIndex::verify() const
409
{
49,194✔
410
    REALM_ASSERT(m_num_conflict_groups == m_conflict_groups_owner.size());
49,194✔
411

412
    // Verify that there are no stray pointers.
413
    for (auto& pair : m_object_instructions) {
94,924✔
414
        for (auto& pair2 : pair.second) {
268,652✔
415
            REALM_ASSERT(&*pair2.second->self_it == pair2.second);
268,652✔
416
            REALM_ASSERT(std::any_of(m_conflict_groups_owner.begin(), m_conflict_groups_owner.end(), [&](auto& cg) {
268,652✔
417
                return &cg == pair2.second;
268,652✔
418
            }));
268,652✔
419
        }
268,652✔
420
    }
94,924✔
421

422
    for (auto& pair : m_schema_instructions) {
49,194✔
423
        REALM_ASSERT(&*pair.second->self_it == pair.second);
19,158✔
424
        REALM_ASSERT(std::any_of(m_conflict_groups_owner.begin(), m_conflict_groups_owner.end(), [&](auto& cg) {
19,158✔
425
            return &cg == pair.second;
19,158✔
426
        }));
19,158✔
427
    }
19,158✔
428

429
    // Collect all changesets
430
    std::vector<Changeset*> changesets;
49,194✔
431
    for (auto& cg : m_conflict_groups_owner) {
286,164✔
432
        check_ranges(cg.ranges);
286,164✔
433

434
        for (auto& ranges : cg.ranges) {
286,164✔
435
            changesets.push_back(ranges.first);
77,898✔
436
        }
77,898✔
437
    }
286,164✔
438
    std::sort(changesets.begin(), changesets.end(), std::less<>());
49,194✔
439
    changesets.erase(std::unique(changesets.begin(), changesets.end()), changesets.end());
49,194✔
440

441
    // Run through all instructions in each changeset and check that
442
    // instructions are correctly covered by the index.
443
    for (auto changeset : changesets) {
62,676✔
444
        auto& log = *changeset;
62,676✔
445

446
        using Instruction = realm::sync::Instruction;
62,676✔
447

448
        // Iterate over all instructions (skipping tombstones), and verify that
449
        // the index covers any objects mentioned in that instruction.
450
        for (auto it = log.begin(); it != log.end(); ++it) {
185,978✔
451
            if (!*it)
123,302✔
452
                continue;
19,048✔
453

454
            const auto& instr = **it;
104,254✔
455

456
            if (auto add_table_instr = instr.get_if<Instruction::AddTable>()) {
104,254✔
457
                StringData table = log.get_string(add_table_instr->table);
340✔
458
                auto ranges = *get_schema_changes_for_class(table);
340✔
459
                REALM_ASSERT(ranges_cover(ranges, log, it));
340✔
460
            }
340✔
461
            else if (auto erase_table_instr = instr.get_if<Instruction::EraseTable>()) {
103,914✔
462
                StringData table = log.get_string(erase_table_instr->table);
×
463
                auto ranges = *get_schema_changes_for_class(table);
×
464
                REALM_ASSERT(ranges_cover(ranges, log, it));
×
465
            }
×
466
            else if (auto add_column_instr = instr.get_if<Instruction::AddColumn>()) {
103,914✔
467
                StringData table = log.get_string(add_column_instr->table);
678✔
468
                auto ranges = *get_schema_changes_for_class(table);
678✔
469
                REALM_ASSERT(ranges_cover(ranges, log, it));
678✔
470
            }
678✔
471
            else if (auto erase_column_instr = instr.get_if<Instruction::EraseColumn>()) {
103,236✔
472
                StringData table = log.get_string(erase_column_instr->table);
×
473
                auto ranges = *get_schema_changes_for_class(table);
×
474
                REALM_ASSERT(ranges_cover(ranges, log, it));
×
475
            }
×
476
            else {
103,236✔
477
                GlobalID ids[2];
103,236✔
478
                size_t num_ids = get_object_ids_in_instruction(log, instr, ids, 2);
103,236✔
479
                REALM_ASSERT(num_ids >= 1);
103,236✔
480
                REALM_ASSERT(num_ids <= 2);
103,236✔
481
                auto& ranges_first = *get_modifications_for_object(ids[0]);
103,236✔
482

483
                for (size_t i = 0; i < num_ids; ++i) {
206,670✔
484
                    auto& ranges = *get_modifications_for_object(ids[i]);
103,434✔
485
                    REALM_ASSERT(&ranges == &ranges_first);
103,434✔
486
                    REALM_ASSERT(ranges_cover(ranges, log, it));
103,434✔
487
                }
103,434✔
488
            }
103,236✔
489
        }
104,254✔
490
    }
62,676✔
491
}
49,194✔
492

493
bool ChangesetIndex::ranges_cover(const Ranges& ranges, Changeset& log, Changeset::const_iterator it) const
494
{
104,440✔
495
    auto outer = ranges.find(&log);
104,440✔
496
    if (outer == ranges.end())
104,440✔
497
        return false;
×
498
    for (auto& range : outer->second) {
107,848✔
499
        if (it >= range.begin && it < range.end)
107,848✔
500
            return true;
104,440✔
501
    }
107,848✔
UNCOV
502
    return false;
×
503
}
104,440✔
504

505
#endif // REALM_DEBUG
506

507
void ChangesetIndex::add_instruction_at(Ranges& ranges, Changeset& changeset, Changeset::iterator pos)
508
{
123,428✔
509
    auto& ranges_for_changeset = ranges[&changeset];
123,428✔
510

511
    REALM_ASSERT(pos != changeset.end());
123,428✔
512
    auto next = pos;
123,428✔
513
    ++next;
123,428✔
514

515
    Changeset::Range incoming{pos, next};
123,428✔
516

517
    auto cmp = [](const Changeset::Range& range_a, const Changeset::Range& range_b) {
123,428✔
518
        return range_a.begin < range_b.begin;
46,276✔
519
    };
46,276✔
520

521
    auto it = std::lower_bound(ranges_for_changeset.begin(), ranges_for_changeset.end(), incoming, cmp);
123,428✔
522

523
    it = ranges_for_changeset.insert(it, incoming);
123,428✔
524
    if (it != ranges_for_changeset.begin())
123,428✔
525
        --it;
45,504✔
526

527
    // Merge adjacent overlapping ranges
528
    while (it + 1 != ranges_for_changeset.end()) {
168,930✔
529
        auto next_it = it + 1;
45,502✔
530
        if (it->end >= next_it->begin) {
45,502✔
531
            it->end = std::max(it->end, next_it->end);
42,064✔
532
            next_it = ranges_for_changeset.erase(next_it);
42,064✔
533
            it = next_it - 1;
42,064✔
534
        }
42,064✔
535
        else {
3,438✔
536
            ++it;
3,438✔
537
        }
3,438✔
538
    }
45,502✔
539
}
123,428✔
540

541

542
} // namespace _impl
543
} // namespace realm
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc