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

realm / realm-core / 2494

16 Jul 2024 09:26AM UTC coverage: 91.001% (+0.02%) from 90.982%
2494

push

Evergreen

web-flow
Add get local schema version to the C-API (#7873)

* Add method to retrieve persisted schema version

* suggested fix + basic tests

* code review

* changelog + cr

---------

Co-authored-by: Clemente <clemente.tort@mongodb.com>

102384 of 180596 branches covered (56.69%)

14 of 17 new or added lines in 2 files covered. (82.35%)

48 existing lines in 12 files now uncovered.

215446 of 236750 relevant lines covered (91.0%)

5859770.57 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,844✔
14
    return left.begin < right.begin;
3,844✔
15
}
3,844✔
16

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

23
    // Check that there are no overlaps
24
    if (begin != end) {
79,222✔
25
        auto last_end = begin->end;
79,220✔
26
        for (auto i = begin + 1; i != end; ++i) {
83,064✔
27
            if (last_end > i->begin)
3,844✔
28
                return false;
×
29
            last_end = i->end;
3,844✔
30
        }
3,844✔
31
    }
79,220✔
32
    return true;
79,216✔
33
}
79,216✔
34

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

45

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

54

55
void ChangesetIndex::scan_changeset(Changeset& changeset)
56
{
628,856✔
57
    if (m_contains_destructive_schema_changes)
628,856✔
58
        return;
71,864✔
59

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

67
    using Instruction = realm::sync::Instruction;
556,992✔
68

69
    for (auto it = changeset.begin(); it != changeset.end(); ++it) {
1,208,442✔
70
        if (!*it)
655,230✔
71
            continue;
3,214✔
72

73
        const auto& instr = **it;
652,016✔
74

75
        if (auto add_table_instr = instr.get_if<Instruction::AddTable>()) {
652,016✔
76
            auto& p = *add_table_instr;
15,860✔
77
            schema_conflict_group(changeset.get_string(p.table));
15,860✔
78
        }
15,860✔
79
        else if (instr.get_if<Instruction::EraseTable>()) {
636,156✔
80
            m_contains_destructive_schema_changes = true;
3,780✔
81
            clear();
3,780✔
82
            return;
3,780✔
83
        }
3,780✔
84
        else if (auto add_column_instr = instr.get_if<Instruction::AddColumn>()) {
632,376✔
85
            auto& p = *add_column_instr;
38,950✔
86
            StringData table_name = changeset.get_string(p.table);
38,950✔
87
            ConflictGroup& cg = schema_conflict_group(table_name);
38,950✔
88
            if (p.type == Instruction::Payload::Type::Link) {
38,950✔
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,950✔
94
        else if (instr.get_if<Instruction::EraseColumn>()) {
593,426✔
95
            m_contains_destructive_schema_changes = true;
×
96
            clear();
×
97
            return;
×
98
        }
×
99
        else {
593,426✔
100
            GlobalID ids[2];
593,426✔
101
            size_t num_ids = get_object_ids_in_instruction(changeset, instr, ids, 2);
593,426✔
102
            REALM_ASSERT(num_ids >= 1);
593,426✔
103
            REALM_ASSERT(num_ids <= 2);
593,426✔
104

105
            ConflictGroup& cg = object_conflict_group(ids[0]);
593,426✔
106
            for (size_t i = 1; i < num_ids; ++i) {
593,818✔
107
                ConflictGroup& cg2 = object_conflict_group(ids[1]);
392✔
108
                merge_conflict_groups(cg, cg2);
392✔
109
            }
392✔
110
        }
593,426✔
111
    }
652,016✔
112
}
556,992✔
113

114

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

120
    if (from.size > into.size) {
1,720✔
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,704✔
130
    REALM_ASSERT(from.ranges.empty());
1,704✔
131

132
    for (auto& class_name : from.schemas) {
1,704✔
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,704✔
136

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

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

151

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

157
    if (m_contains_destructive_schema_changes)
71,498✔
158
        return; // Just add to everything.
3,780✔
159

160
    using Instruction = realm::sync::Instruction;
67,718✔
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) {
193,740✔
165
        if (!*it)
126,022✔
166
            continue;
×
167

168
        const auto& instr = **it;
126,022✔
169

170
        if (auto add_table_instr = instr.get_if<Instruction::AddTable>()) {
126,022✔
171
            StringData table = log.get_string(add_table_instr->table);
4,424✔
172
            auto& cg = schema_conflict_group(table);
4,424✔
173
            add_instruction_at(cg.ranges, log, it);
4,424✔
174
        }
4,424✔
175
        else if (instr.get_if<Instruction::EraseTable>()) {
121,598✔
176
            REALM_TERMINATE("Call scan_changeset() before add_changeset().");
177
        }
×
178
        else if (auto add_column_instr = instr.get_if<Instruction::AddColumn>()) {
121,598✔
179
            auto& p = *add_column_instr;
10,822✔
180
            StringData table = log.get_string(p.table);
10,822✔
181
            auto& cg = schema_conflict_group(table);
10,822✔
182
            if (p.type == Instruction::Payload::Type::Link) {
10,822✔
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,822✔
186
        }
10,822✔
187
        else if (instr.get_if<Instruction::EraseColumn>()) {
110,776✔
188
            REALM_TERMINATE("Call scan_changeset() before add_changeset().");
189
        }
×
190
        else {
110,776✔
191
            GlobalID ids[2];
110,776✔
192
            size_t num_ids = get_object_ids_in_instruction(log, instr, ids, 2);
110,776✔
193
            REALM_ASSERT(num_ids >= 1);
110,776✔
194
            REALM_ASSERT(num_ids <= 2);
110,776✔
195

196
            auto& cg = object_conflict_group(ids[0]);
110,776✔
197
            for (size_t i = 1; i < num_ids; ++i) {
111,002✔
198
                REALM_ASSERT(&cg == &object_conflict_group(ids[i]));
226✔
199
            }
226✔
200
            add_instruction_at(cg.ranges, log, it);
110,776✔
201
        }
110,776✔
202
    }
126,022✔
203
}
67,718✔
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,346,696✔
208
    REALM_ASSERT_RELEASE(max_num_ids >= 2);
1,346,696✔
209

210
    using Instruction = realm::sync::Instruction;
1,346,696✔
211

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

215
        if (auto set_instr = instr.get_if<Instruction::Update>()) {
1,345,224✔
216
            auto& p = *set_instr;
674,622✔
217
            if (p.value.type == Instruction::Payload::Type::Link) {
674,622✔
218
                ids[1] = {changeset.get_string(p.value.data.link.target_table),
298✔
219
                          changeset.get_key(p.value.data.link.target)};
298✔
220
                return 2;
298✔
221
            }
298✔
222
        }
674,622✔
223
        else if (auto insert_instr = instr.get_if<Instruction::ArrayInsert>()) {
670,602✔
224
            auto& p = *insert_instr;
80,986✔
225
            if (p.value.type == Instruction::Payload::Type::Link) {
80,986✔
226
                ids[1] = {changeset.get_string(p.value.data.link.target_table),
696✔
227
                          changeset.get_key(p.value.data.link.target)};
696✔
228
                return 2;
696✔
229
            }
696✔
230
        }
80,986✔
231

232
        return 1;
1,344,230✔
233
    }
1,345,224✔
234

235
    return 0;
1,472✔
236
}
1,346,696✔
237

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

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

253
auto ChangesetIndex::get_modifications_for_object(GlobalID id) -> Ranges*
254
{
749,032✔
255
    if (m_contains_destructive_schema_changes)
749,032✔
256
        return &m_everything;
98,442✔
257
    auto it = m_object_instructions.find(id.table_name);
650,590✔
258
    if (it == m_object_instructions.end())
650,590✔
259
        return &m_empty;
×
260

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

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

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

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

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

311
    if (new_pos.m_pos >= new_pos.m_inner->end) {
20,486✔
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());
9,030✔
314
        ++new_pos.m_inner;
9,030✔
315
        if (new_pos.m_inner == new_pos.m_outer->second.end()) {
9,030✔
316
            REALM_ASSERT(new_pos.m_outer != new_pos.m_ranges->end());
9,022✔
317
            ++new_pos.m_outer;
9,022✔
318
            if (new_pos.m_outer == new_pos.m_ranges->end()) {
9,022✔
319
                return RangeIterator{new_pos.m_ranges, RangeIterator::end_tag{}}; // avoid new_pos.check()
7,182✔
320
            }
7,182✔
321
            else {
1,840✔
322
                new_pos.m_inner = new_pos.m_outer->second.begin();
1,840✔
323
                REALM_ASSERT(new_pos.m_inner != new_pos.m_outer->second.end());
1,840✔
324
                new_pos.m_pos = new_pos.m_inner->begin;
1,840✔
325
            }
1,840✔
326
        }
9,022✔
327
        else {
8✔
328
            new_pos.m_pos = new_pos.m_inner->begin;
8✔
329
            REALM_ASSERT(new_pos.m_pos != new_pos.m_inner->end); // empty ranges not allowed
8✔
330
        }
8✔
331
    }
9,030✔
332
    new_pos.check();
13,304✔
333
    return new_pos;
13,304✔
334
}
20,486✔
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
{
48,018✔
410
    REALM_ASSERT(m_num_conflict_groups == m_conflict_groups_owner.size());
48,018✔
411

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

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

429
    // Collect all changesets
430
    std::vector<Changeset*> changesets;
48,018✔
431
    for (auto& cg : m_conflict_groups_owner) {
305,810✔
432
        check_ranges(cg.ranges);
305,810✔
433

434
        for (auto& ranges : cg.ranges) {
305,810✔
435
            changesets.push_back(ranges.first);
79,218✔
436
        }
79,218✔
437
    }
305,810✔
438
    std::sort(changesets.begin(), changesets.end(), std::less<>());
48,018✔
439
    changesets.erase(std::unique(changesets.begin(), changesets.end()), changesets.end());
48,018✔
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,942✔
444
        auto& log = *changeset;
62,942✔
445

446
        using Instruction = realm::sync::Instruction;
62,942✔
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) {
188,848✔
451
            if (!*it)
125,906✔
452
                continue;
19,342✔
453

454
            const auto& instr = **it;
106,564✔
455

456
            if (auto add_table_instr = instr.get_if<Instruction::AddTable>()) {
106,564✔
457
                StringData table = log.get_string(add_table_instr->table);
330✔
458
                auto ranges = *get_schema_changes_for_class(table);
330✔
459
                REALM_ASSERT(ranges_cover(ranges, log, it));
330✔
460
            }
330✔
461
            else if (auto erase_table_instr = instr.get_if<Instruction::EraseTable>()) {
106,234✔
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>()) {
106,234✔
467
                StringData table = log.get_string(add_column_instr->table);
666✔
468
                auto ranges = *get_schema_changes_for_class(table);
666✔
469
                REALM_ASSERT(ranges_cover(ranges, log, it));
666✔
470
            }
666✔
471
            else if (auto erase_column_instr = instr.get_if<Instruction::EraseColumn>()) {
105,568✔
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 {
105,568✔
477
                GlobalID ids[2];
105,568✔
478
                size_t num_ids = get_object_ids_in_instruction(log, instr, ids, 2);
105,568✔
479
                REALM_ASSERT(num_ids >= 1);
105,568✔
480
                REALM_ASSERT(num_ids <= 2);
105,568✔
481
                auto& ranges_first = *get_modifications_for_object(ids[0]);
105,568✔
482

483
                for (size_t i = 0; i < num_ids; ++i) {
211,348✔
484
                    auto& ranges = *get_modifications_for_object(ids[i]);
105,780✔
485
                    REALM_ASSERT(&ranges == &ranges_first);
105,780✔
486
                    REALM_ASSERT(ranges_cover(ranges, log, it));
105,780✔
487
                }
105,780✔
488
            }
105,568✔
489
        }
106,564✔
490
    }
62,942✔
491
}
48,018✔
492

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

505
#endif // REALM_DEBUG
506

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

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

515
    Changeset::Range incoming{pos, next};
126,024✔
516

517
    auto cmp = [](const Changeset::Range& range_a, const Changeset::Range& range_b) {
126,024✔
518
        return range_a.begin < range_b.begin;
47,534✔
519
    };
47,534✔
520

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

523
    it = ranges_for_changeset.insert(it, incoming);
126,024✔
524
    if (it != ranges_for_changeset.begin())
126,024✔
525
        --it;
46,762✔
526

527
    // Merge adjacent overlapping ranges
528
    while (it + 1 != ranges_for_changeset.end()) {
172,786✔
529
        auto next_it = it + 1;
46,762✔
530
        if (it->end >= next_it->begin) {
46,762✔
531
            it->end = std::max(it->end, next_it->end);
42,912✔
532
            next_it = ranges_for_changeset.erase(next_it);
42,912✔
533
            it = next_it - 1;
42,912✔
534
        }
42,912✔
535
        else {
3,850✔
536
            ++it;
3,850✔
537
        }
3,850✔
538
    }
46,762✔
539
}
126,024✔
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