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

realm / realm-core / 1737

06 Oct 2023 12:38PM UTC coverage: 91.567% (-0.02%) from 91.591%
1737

push

Evergreen

web-flow
Enable `REALM_ENABLE_GEOSPATIAL`, geoWithin on points for SPM Installation (#7032)

94320 of 173524 branches covered (0.0%)

230508 of 251736 relevant lines covered (91.57%)

6802705.57 hits per line

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

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

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

122,794✔
23
    // Check that there are no overlaps
122,794✔
24
    if (begin != end) {
242,410✔
25
        auto last_end = begin->end;
242,408✔
26
        for (auto i = begin + 1; i != end; ++i) {
245,800✔
27
            if (last_end > i->begin)
3,392✔
28
                return false;
×
29
            last_end = i->end;
3,392✔
30
        }
3,392✔
31
    }
242,408✔
32
    return true;
242,404✔
33
}
242,404✔
34

35
static bool check_ranges(const ChangesetIndex::Ranges& ranges)
36
{
3,388,074✔
37
    for (auto& pair : ranges) {
1,821,760✔
38
        if (!check_ranges(pair.second.begin(), pair.second.end()))
242,402✔
39
            return false;
×
40
    }
242,402✔
41
    return true;
3,388,074✔
42
}
3,388,074✔
43
#endif // REALM_DEBUG
44

45

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

54

55
void ChangesetIndex::scan_changeset(Changeset& changeset)
56
{
10,219,302✔
57
    if (m_contains_destructive_schema_changes)
10,219,302✔
58
        return;
2,078,188✔
59

4,068,656✔
60
#if defined(REALM_DEBUG) // LCOV_EXCL_START
8,141,114✔
61
    for (auto& confict_group : m_conflict_groups_owner) {
72,059,210✔
62
        // Check that add_changeset() has not been called yet.
35,747,894✔
63
        REALM_ASSERT(confict_group.ranges.empty());
72,059,210✔
64
    }
72,059,210✔
65
#endif // REALM_DEBUG LCOV_EXCL_STOP
8,141,114✔
66

4,068,656✔
67
    using Instruction = realm::sync::Instruction;
8,141,114✔
68

4,068,656✔
69
    for (auto it = changeset.begin(); it != changeset.end(); ++it) {
14,925,076✔
70
        if (!*it)
6,899,954✔
71
            continue;
668✔
72

3,432,824✔
73
        const auto& instr = **it;
6,899,286✔
74

3,432,824✔
75
        if (auto add_table_instr = instr.get_if<Instruction::AddTable>()) {
6,899,286✔
76
            auto& p = *add_table_instr;
182,346✔
77
            schema_conflict_group(changeset.get_string(p.table));
182,346✔
78
        }
182,346✔
79
        else if (instr.get_if<Instruction::EraseTable>()) {
6,716,940✔
80
            m_contains_destructive_schema_changes = true;
115,992✔
81
            clear();
115,992✔
82
            return;
115,992✔
83
        }
115,992✔
84
        else if (auto add_column_instr = instr.get_if<Instruction::AddColumn>()) {
6,600,948✔
85
            auto& p = *add_column_instr;
444,382✔
86
            StringData table_name = changeset.get_string(p.table);
444,382✔
87
            ConflictGroup& cg = schema_conflict_group(table_name);
444,382✔
88
            if (p.type == Instruction::Payload::Type::Link) {
444,382✔
89
                StringData target_table = changeset.get_string(p.link_target_table);
3,936✔
90
                ConflictGroup& cg2 = schema_conflict_group(target_table);
3,936✔
91
                merge_conflict_groups(cg, cg2);
3,936✔
92
            }
3,936✔
93
        }
444,382✔
94
        else if (instr.get_if<Instruction::EraseColumn>()) {
6,156,566✔
95
            m_contains_destructive_schema_changes = true;
×
96
            clear();
×
97
            return;
×
98
        }
×
99
        else {
6,156,566✔
100
            GlobalID ids[2];
6,156,566✔
101
            size_t num_ids = get_object_ids_in_instruction(changeset, instr, ids, 2);
6,156,566✔
102
            REALM_ASSERT(num_ids >= 1);
6,156,566✔
103
            REALM_ASSERT(num_ids <= 2);
6,156,566✔
104

3,067,684✔
105
            ConflictGroup& cg = object_conflict_group(ids[0]);
6,156,566✔
106
            for (size_t i = 1; i < num_ids; ++i) {
6,156,934✔
107
                ConflictGroup& cg2 = object_conflict_group(ids[1]);
368✔
108
                merge_conflict_groups(cg, cg2);
368✔
109
            }
368✔
110
        }
6,156,566✔
111
    }
6,899,286✔
112
}
8,141,114✔
113

114

115
void ChangesetIndex::merge_conflict_groups(ConflictGroup& into, ConflictGroup& from)
116
{
4,320✔
117
    if (&into == &from)
4,320✔
118
        return;
2,856✔
119

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

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

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

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

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

151

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

221,160✔
157
    if (m_contains_destructive_schema_changes)
438,766✔
158
        return; // Just add to everything.
115,992✔
159

163,740✔
160
    using Instruction = realm::sync::Instruction;
322,774✔
161

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

144,222✔
168
        const auto& instr = **it;
285,864✔
169

144,222✔
170
        if (auto add_table_instr = instr.get_if<Instruction::AddTable>()) {
285,864✔
171
            StringData table = log.get_string(add_table_instr->table);
13,568✔
172
            auto& cg = schema_conflict_group(table);
13,568✔
173
            add_instruction_at(cg.ranges, log, it);
13,568✔
174
        }
13,568✔
175
        else if (instr.get_if<Instruction::EraseTable>()) {
272,296✔
176
            REALM_TERMINATE("Call scan_changeset() before add_changeset().");
×
177
        }
×
178
        else if (auto add_column_instr = instr.get_if<Instruction::AddColumn>()) {
272,296✔
179
            auto& p = *add_column_instr;
21,262✔
180
            StringData table = log.get_string(p.table);
21,262✔
181
            auto& cg = schema_conflict_group(table);
21,262✔
182
            if (p.type == Instruction::Payload::Type::Link) {
21,262✔
183
                REALM_ASSERT(&cg == &schema_conflict_group(log.get_string(p.link_target_table)));
1,968✔
184
            }
1,968✔
185
            add_instruction_at(cg.ranges, log, it);
21,262✔
186
        }
21,262✔
187
        else if (instr.get_if<Instruction::EraseColumn>()) {
251,034✔
188
            REALM_TERMINATE("Call scan_changeset() before add_changeset().");
×
189
        }
×
190
        else {
251,034✔
191
            GlobalID ids[2];
251,034✔
192
            size_t num_ids = get_object_ids_in_instruction(log, instr, ids, 2);
251,034✔
193
            REALM_ASSERT(num_ids >= 1);
251,034✔
194
            REALM_ASSERT(num_ids <= 2);
251,034✔
195

126,910✔
196
            auto& cg = object_conflict_group(ids[0]);
251,034✔
197
            for (size_t i = 1; i < num_ids; ++i) {
251,254✔
198
                REALM_ASSERT(&cg == &object_conflict_group(ids[i]));
220✔
199
            }
220✔
200
            add_instruction_at(cg.ranges, log, it);
251,034✔
201
        }
251,034✔
202
    }
285,864✔
203
}
322,774✔
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
{
14,123,530✔
208
    REALM_ASSERT_RELEASE(max_num_ids >= 2);
14,123,530✔
209

7,051,530✔
210
    using Instruction = realm::sync::Instruction;
14,123,530✔
211

7,051,530✔
212
    if (auto obj_instr = instr.get_if<Instruction::ObjectInstruction>()) {
14,123,530✔
213
        ids[0] = {changeset.get_string(obj_instr->table), changeset.get_key(obj_instr->object)};
14,122,972✔
214

7,051,208✔
215
        if (auto set_instr = instr.get_if<Instruction::Update>()) {
14,122,972✔
216
            auto& p = *set_instr;
1,785,266✔
217
            if (p.value.type == Instruction::Payload::Type::Link) {
1,785,266✔
218
                ids[1] = {changeset.get_string(p.value.data.link.target_table),
304✔
219
                          changeset.get_key(p.value.data.link.target)};
304✔
220
                return 2;
304✔
221
            }
304✔
222
        }
12,337,706✔
223
        else if (auto insert_instr = instr.get_if<Instruction::ArrayInsert>()) {
12,337,706✔
224
            auto& p = *insert_instr;
2,039,804✔
225
            if (p.value.type == Instruction::Payload::Type::Link) {
2,039,804✔
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
        }
14,122,032✔
231

7,050,774✔
232
        return 1;
14,122,032✔
233
    }
14,122,032✔
234

322✔
235
    return 0;
558✔
236
}
558✔
237

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

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

253
auto ChangesetIndex::get_modifications_for_object(GlobalID id) -> Ranges*
254
{
7,946,556✔
255
    if (m_contains_destructive_schema_changes)
7,946,556✔
256
        return &m_everything;
2,981,636✔
257
    auto it = m_object_instructions.find(id.table_name);
4,964,920✔
258
    if (it == m_object_instructions.end())
4,964,920✔
259
        return &m_empty;
×
260

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

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

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

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

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

35,384✔
311
    if (new_pos.m_pos >= new_pos.m_inner->end) {
69,034✔
312
        // erased the last instruction in the range, move to the next range.
30,250✔
313
        REALM_ASSERT(new_pos.m_inner < new_pos.m_outer->second.end());
58,696✔
314
        ++new_pos.m_inner;
58,696✔
315
        if (new_pos.m_inner == new_pos.m_outer->second.end()) {
58,696✔
316
            REALM_ASSERT(new_pos.m_outer != new_pos.m_ranges->end());
58,696✔
317
            ++new_pos.m_outer;
58,696✔
318
            if (new_pos.m_outer == new_pos.m_ranges->end()) {
58,696✔
319
                return RangeIterator{new_pos.m_ranges, RangeIterator::end_tag{}}; // avoid new_pos.check()
56,062✔
320
            }
56,062✔
321
            else {
2,634✔
322
                new_pos.m_inner = new_pos.m_outer->second.begin();
2,634✔
323
                REALM_ASSERT(new_pos.m_inner != new_pos.m_outer->second.end());
2,634✔
324
                new_pos.m_pos = new_pos.m_inner->begin;
2,634✔
325
            }
2,634✔
326
        }
58,696✔
327
        else {
×
328
            new_pos.m_pos = new_pos.m_inner->begin;
×
329
            REALM_ASSERT(new_pos.m_pos != new_pos.m_inner->end); // empty ranges not allowed
✔
330
        }
×
331
    }
58,696✔
332
    new_pos.check();
42,234✔
333
    return new_pos;
12,972✔
334
}
69,034✔
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
{
414,698✔
410
    REALM_ASSERT(m_num_conflict_groups == m_conflict_groups_owner.size());
414,698✔
411

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

210,190✔
422
    for (auto& pair : m_schema_instructions) {
403,648✔
423
        REALM_ASSERT(&*pair.second->self_it == pair.second);
379,448✔
424
        REALM_ASSERT(std::any_of(m_conflict_groups_owner.begin(), m_conflict_groups_owner.end(), [&](auto& cg) {
379,448✔
425
            return &cg == pair.second;
379,448✔
426
        }));
379,448✔
427
    }
379,448✔
428

210,190✔
429
    // Collect all changesets
210,190✔
430
    std::vector<Changeset*> changesets;
414,698✔
431
    for (auto& cg : m_conflict_groups_owner) {
3,388,070✔
432
        check_ranges(cg.ranges);
3,388,070✔
433

1,702,152✔
434
        for (auto& ranges : cg.ranges) {
1,821,768✔
435
            changesets.push_back(ranges.first);
242,408✔
436
        }
242,408✔
437
    }
3,388,070✔
438
    std::sort(changesets.begin(), changesets.end(), std::less<>());
414,698✔
439
    changesets.erase(std::unique(changesets.begin(), changesets.end()), changesets.end());
414,698✔
440

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

115,202✔
446
        using Instruction = realm::sync::Instruction;
227,550✔
447

115,202✔
448
        // Iterate over all instructions (skipping tombstones), and verify that
115,202✔
449
        // the index covers any objects mentioned in that instruction.
115,202✔
450
        for (auto it = log.begin(); it != log.end(); ++it) {
513,324✔
451
            if (!*it)
285,774✔
452
                continue;
36,986✔
453

125,222✔
454
            const auto& instr = **it;
248,788✔
455

125,222✔
456
            if (auto add_table_instr = instr.get_if<Instruction::AddTable>()) {
248,788✔
457
                StringData table = log.get_string(add_table_instr->table);
7,322✔
458
                auto ranges = *get_schema_changes_for_class(table);
7,322✔
459
                REALM_ASSERT(ranges_cover(ranges, log, it));
7,322✔
460
            }
7,322✔
461
            else if (auto erase_table_instr = instr.get_if<Instruction::EraseTable>()) {
241,466✔
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>()) {
241,466✔
467
                StringData table = log.get_string(add_column_instr->table);
11,594✔
468
                auto ranges = *get_schema_changes_for_class(table);
11,594✔
469
                REALM_ASSERT(ranges_cover(ranges, log, it));
11,594✔
470
            }
11,594✔
471
            else if (auto erase_column_instr = instr.get_if<Instruction::EraseColumn>()) {
229,872✔
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 {
229,872✔
477
                GlobalID ids[2];
229,872✔
478
                size_t num_ids = get_object_ids_in_instruction(log, instr, ids, 2);
229,872✔
479
                REALM_ASSERT(num_ids >= 1);
229,872✔
480
                REALM_ASSERT(num_ids <= 2);
229,872✔
481
                auto& ranges_first = *get_modifications_for_object(ids[0]);
229,872✔
482

115,862✔
483
                for (size_t i = 0; i < num_ids; ++i) {
459,970✔
484
                    auto& ranges = *get_modifications_for_object(ids[i]);
230,098✔
485
                    REALM_ASSERT(&ranges == &ranges_first);
230,098✔
486
                    REALM_ASSERT(ranges_cover(ranges, log, it));
230,098✔
487
                }
230,098✔
488
            }
229,872✔
489
        }
248,788✔
490
    }
227,550✔
491
}
414,698✔
492

493
bool ChangesetIndex::ranges_cover(const Ranges& ranges, Changeset& log, Changeset::const_iterator it) const
494
{
249,016✔
495
    auto outer = ranges.find(&log);
249,016✔
496
    if (outer == ranges.end())
249,016✔
497
        return false;
×
498
    for (auto& range : outer->second) {
252,426✔
499
        if (it >= range.begin && it < range.end)
252,426✔
500
            return true;
249,016✔
501
    }
252,426✔
502
    return false;
2,147,608,983✔
503
}
249,016✔
504

505
#endif // REALM_DEBUG
506

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

144,274✔
511
    REALM_ASSERT(pos != changeset.end());
285,922✔
512
    auto next = pos;
285,922✔
513
    ++next;
285,922✔
514

144,274✔
515
    Changeset::Range incoming{pos, next};
285,922✔
516

144,274✔
517
    auto cmp = [](const Changeset::Range& range_a, const Changeset::Range& range_b) {
166,574✔
518
        return range_a.begin < range_b.begin;
44,074✔
519
    };
44,074✔
520

144,274✔
521
    auto it = std::lower_bound(ranges_for_changeset.begin(), ranges_for_changeset.end(), incoming, cmp);
285,922✔
522

144,274✔
523
    it = ranges_for_changeset.insert(it, incoming);
285,922✔
524
    if (it != ranges_for_changeset.begin())
285,922✔
525
        --it;
43,482✔
526

144,274✔
527
    // Merge adjacent overlapping ranges
144,274✔
528
    while (it + 1 != ranges_for_changeset.end()) {
329,402✔
529
        auto next_it = it + 1;
43,480✔
530
        if (it->end >= next_it->begin) {
43,480✔
531
            it->end = std::max(it->end, next_it->end);
40,088✔
532
            next_it = ranges_for_changeset.erase(next_it);
40,088✔
533
            it = next_it - 1;
40,088✔
534
        }
40,088✔
535
        else {
3,392✔
536
            ++it;
3,392✔
537
        }
3,392✔
538
    }
43,480✔
539
}
285,922✔
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