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

realm / realm-core / michael.wilkersonbarker_971

23 Feb 2024 04:24AM UTC coverage: 91.835% (-0.03%) from 91.865%
michael.wilkersonbarker_971

Pull #7365

Evergreen

michael-wb
First round of updates from review
Pull Request #7365: RCORE-1982: Opening realm with cached user while offline results in fatal error and session does not retry connection

93112 of 171580 branches covered (54.27%)

209 of 226 new or added lines in 9 files covered. (92.48%)

126 existing lines in 18 files now uncovered.

235370 of 256297 relevant lines covered (91.83%)

6232257.12 hits per line

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

63.07
/src/realm/sync/transform.cpp
1
#include <realm/sync/transform.hpp>
2

3
#include <realm/sync/noinst/changeset_index.hpp>
4
#include <realm/sync/noinst/protocol_codec.hpp>
5

6
#if REALM_DEBUG
7
#include <sstream>
8
#include <iostream> // std::cerr used for debug tracing
9
#include <mutex>    // std::unique_lock used for debug tracing
10
#endif              // REALM_DEBUG
11

12
using namespace realm;
13
using namespace realm::sync;
14
using namespace realm::util;
15

16
namespace {
17

18
#if REALM_DEBUG
19
#if defined(_MSC_VER)
20
#define TERM_RED ""
21
#define TERM_YELLOW ""
22
#define TERM_CYAN ""
23
#define TERM_MAGENTA ""
24
#define TERM_GREEN ""
25
#define TERM_BOLD ""
26
#define TERM_RESET ""
27
#else
28
#define TERM_RED "\x1b[31;22m"
×
29
#define TERM_YELLOW "\x1b[33;22m"
×
30
#define TERM_CYAN "\x1b[36;22m"
×
31
#define TERM_MAGENTA "\x1b[35;22m"
×
32
#define TERM_GREEN "\x1b[32;22m"
33
#define TERM_BOLD "\x1b[1m"
34
#define TERM_RESET "\x1b[39;49;22m"
×
35
#endif
36
#endif
37

38
struct Discriminant {
39
    timestamp_type timestamp;
40
    file_ident_type client_file_ident;
41
    Discriminant(timestamp_type t, file_ident_type p)
42
        : timestamp(t)
43
        , client_file_ident(p)
44
    {
916,188✔
45
    }
916,188✔
46

47
    Discriminant(const Discriminant&) = default;
48
    Discriminant& operator=(const Discriminant&) = default;
49

50
    bool operator<(const Discriminant& other) const
51
    {
7,676✔
52
        return timestamp == other.timestamp ? (client_file_ident < other.client_file_ident)
4,100✔
53
                                            : timestamp < other.timestamp;
7,402✔
54
    }
7,676✔
55

56
    bool operator==(const Discriminant& other) const
57
    {
×
58
        return timestamp == other.timestamp && client_file_ident == other.client_file_ident;
×
59
    }
×
60
    bool operator!=(const Discriminant& other) const
61
    {
×
62
        return !((*this) == other);
×
63
    }
×
64
};
65

66
struct TransformerImpl;
67

68
struct Side {
69
    TransformerImpl& m_transformer;
70
    Changeset* m_changeset = nullptr;
71
    Discriminant m_discriminant;
72

73
    bool was_discarded = false;
74
    bool was_replaced = false;
75
    size_t m_path_len = 0;
76

77
    Side(TransformerImpl& transformer)
78
        : m_transformer(transformer)
79
        , m_discriminant(0, 0)
80
    {
89,054✔
81
    }
89,054✔
82

83
    virtual void skip_tombstones() noexcept = 0;
84
    virtual void next_instruction() noexcept = 0;
85
    virtual Instruction& get() noexcept = 0;
86

87
    void substitute(const Instruction& instr)
88
    {
×
89
        was_replaced = true;
×
90
        get() = instr;
×
91
    }
×
92

93
    StringData get_string(StringBufferRange range) const
94
    {
32✔
95
        // Relying on the transaction parser to only provide valid StringBufferRanges.
16✔
96
        return m_changeset->get_string(range);
32✔
97
    }
32✔
98

99
    StringData get_string(InternString intern_string) const
100
    {
25,264✔
101
        // Rely on the parser having checked the consistency of the interned strings
12,554✔
102
        return m_changeset->get_string(intern_string);
25,264✔
103
    }
25,264✔
104

105
    InternString intern_string(StringData data) const
106
    {
×
107
        return m_changeset->intern_string(data);
×
108
    }
×
109

110
    const Discriminant& timestamp() const
111
    {
15,352✔
112
        return m_discriminant;
15,352✔
113
    }
15,352✔
114

115
    InternString adopt_string(const Side& other_side, InternString other_string)
116
    {
×
117
        // FIXME: This needs to change if we choose to compare strings through a
118
        // mapping of InternStrings.
119
        StringData string = other_side.get_string(other_string);
×
120
        return intern_string(string);
×
121
    }
×
122

123
protected:
124
    void init_with_instruction(const Instruction& instr) noexcept
125
    {
826,834✔
126
        was_discarded = false;
826,834✔
127
        was_replaced = false;
826,834✔
128
        m_path_len = instr.path_length();
826,834✔
129
    }
826,834✔
130
};
131

132
struct MajorSide : Side {
133
    MajorSide(TransformerImpl& transformer)
134
        : Side(transformer)
135
    {
44,528✔
136
    }
44,528✔
137

138
    void set_next_changeset(Changeset* changeset) noexcept;
139
    void discard();
140
    void prepend(Instruction operation);
141
    template <class InputIterator>
142
    void prepend(InputIterator begin, InputIterator end);
143

144
    void init_with_instruction(Changeset::iterator position) noexcept
145
    {
546,568✔
146
        REALM_ASSERT(position >= m_changeset->begin());
546,568✔
147
        REALM_ASSERT(position != m_changeset->end());
546,568✔
148
        m_position = position;
546,568✔
149
        skip_tombstones();
546,568✔
150
        REALM_ASSERT(position != m_changeset->end());
546,568✔
151

285,378✔
152
        m_discriminant = Discriminant{m_changeset->origin_timestamp, m_changeset->origin_file_ident};
546,568✔
153

285,378✔
154
        Side::init_with_instruction(get());
546,568✔
155
    }
546,568✔
156

157
    void skip_tombstones() noexcept final
158
    {
2,143,848✔
159
        while (m_position != m_changeset->end() && !*m_position) {
2,145,288✔
160
            ++m_position;
1,440✔
161
        }
1,440✔
162
    }
2,143,848✔
163

164
    void next_instruction() noexcept final
165
    {
527,754✔
166
        REALM_ASSERT(m_position != m_changeset->end());
527,754✔
167
        do {
527,892✔
168
            ++m_position;
527,892✔
169
        } while (m_position != m_changeset->end() && !*m_position);
527,892✔
170
    }
527,754✔
171

172
    Instruction& get() noexcept final
173
    {
3,009,540✔
174
        return **m_position;
3,009,540✔
175
    }
3,009,540✔
176

177
    size_t get_object_ids_in_current_instruction(_impl::ChangesetIndex::GlobalID* ids, size_t max_ids)
178
    {
494,104✔
179
        return _impl::get_object_ids_in_instruction(*m_changeset, get(), ids, max_ids);
494,104✔
180
    }
494,104✔
181

182
    Changeset::iterator m_position;
183
};
184

185
struct MinorSide : Side {
186
    using Position = _impl::ChangesetIndex::RangeIterator;
187

188
    MinorSide(TransformerImpl& transformer)
189
        : Side(transformer)
190
    {
44,528✔
191
    }
44,528✔
192

193
    void discard();
194
    void prepend(Instruction operation);
195
    template <class InputIterator>
196
    void prepend(InputIterator begin, InputIterator end);
197

198
    void substitute(const Instruction& instr)
199
    {
×
200
        was_replaced = true;
×
201
        get() = instr;
×
202
    }
×
203

204
    Position begin() noexcept
205
    {
546,676✔
206
        return Position{m_conflict_ranges};
546,676✔
207
    }
546,676✔
208

209
    Position end() noexcept
210
    {
4,638,358✔
211
        return Position{m_conflict_ranges, Position::end_tag{}};
4,638,358✔
212
    }
4,638,358✔
213

214
    void update_changeset_pointer() noexcept
215
    {
1,190,248✔
216
        if (REALM_LIKELY(m_position != end())) {
1,190,248✔
217
            m_changeset = m_position.m_outer->first;
468,102✔
218
        }
468,102✔
219
        else {
722,146✔
220
            m_changeset = nullptr;
722,146✔
221
        }
722,146✔
222
    }
1,190,248✔
223

224
    void skip_tombstones() noexcept final
225
    {
1,345,364✔
226
        if (m_position != end() && *m_position)
1,345,364✔
227
            return;
710,928✔
228
        skip_tombstones_slow();
634,436✔
229
    }
634,436✔
230

231
    REALM_NOINLINE void skip_tombstones_slow() noexcept
232
    {
634,794✔
233
        while (m_position != end() && !*m_position) {
743,838✔
234
            ++m_position;
109,044✔
235
        }
109,044✔
236
        update_changeset_pointer();
634,794✔
237
    }
634,794✔
238

239
    void next_instruction() noexcept final
240
    {
256,822✔
241
        REALM_ASSERT(m_position != end());
256,822✔
242
        ++m_position;
256,822✔
243
        update_changeset_pointer();
256,822✔
244
        skip_tombstones();
256,822✔
245
    }
256,822✔
246

247
    Instruction& get() noexcept final
248
    {
1,705,450✔
249
        Instruction* instr = *m_position;
1,705,450✔
250
        REALM_ASSERT(instr != nullptr);
1,705,450✔
251
        return *instr;
1,705,450✔
252
    }
1,705,450✔
253

254
    void init_with_instruction(Position position)
255
    {
280,696✔
256
        // REALM_ASSERT(position >= Position(m_conflict_ranges));
136,238✔
257
        REALM_ASSERT(position != end());
280,696✔
258
        m_position = position;
280,696✔
259
        update_changeset_pointer();
280,696✔
260
        skip_tombstones();
280,696✔
261
        REALM_ASSERT(position != end());
280,696✔
262

136,238✔
263
        m_discriminant = Discriminant{m_changeset->origin_timestamp, m_changeset->origin_file_ident};
280,696✔
264

136,238✔
265
        Side::init_with_instruction(get());
280,696✔
266
    }
280,696✔
267

268
    _impl::ChangesetIndex::RangeIterator m_position;
269
    _impl::ChangesetIndex* m_changeset_index = nullptr;
270
    _impl::ChangesetIndex::Ranges* m_conflict_ranges = nullptr;
271
};
272

273
#if defined(REALM_DEBUG) // LCOV_EXCL_START Debug utilities
274

275
struct MergeTracer {
276
public:
277
    Side& m_minor;
278
    Side& m_major;
279
    const Changeset& m_minor_log;
280
    const Changeset& m_major_log;
281
    Instruction minor_before;
282
    Instruction major_before;
283

284
    // field => pair(original_value, change)
285
    struct Diff {
286
        std::map<std::string, std::pair<int64_t, int64_t>> numbers;
287
        std::map<std::string, std::pair<std::string, std::string>> strings;
288

289
        bool empty() const noexcept
290
        {
×
291
            return numbers.empty() && strings.empty();
×
292
        }
×
293
    };
294

295
    explicit MergeTracer(Side& minor, Side& major)
296
        : m_minor(minor)
297
        , m_major(major)
298
        , m_minor_log(*minor.m_changeset)
299
        , m_major_log(*major.m_changeset)
300
        , minor_before(minor.get())
301
        , major_before(major.get())
302
    {
×
303
    }
×
304

305
    struct FieldTracer : sync::Changeset::Reflector::Tracer {
306
        std::string m_name;
307
        std::map<std::string, std::string, std::less<>> m_fields;
308

309
        const Changeset* m_changeset = nullptr;
310

311
        void set_changeset(const Changeset* changeset) override
312
        {
×
313
            m_changeset = changeset;
×
314
        }
×
315

316
        StringData get_string(InternString str)
317
        {
×
318
            return m_changeset->get_string(str);
×
319
        }
×
320

321
        void name(StringData n) override
322
        {
×
323
            m_name = n;
×
324
        }
×
325

326
        void path(StringData n, InternString table, const Instruction::PrimaryKey& pk,
327
                  util::Optional<InternString> field, const Instruction::Path* path) override
328
        {
×
329
            std::stringstream ss;
×
330
            m_changeset->print_path(ss, table, pk, field, path);
×
331
            m_fields.emplace(n, ss.str());
×
332
        }
×
333

334
        void field(StringData n, InternString str) override
335
        {
×
336
            m_fields.emplace(n, get_string(str));
×
337
        }
×
338

339
        void field(StringData n, Instruction::Payload::Type type) override
340
        {
×
341
            m_fields.emplace(n, get_type_name(type));
×
342
        }
×
343

344
        void field(StringData n, Instruction::AddColumn::CollectionType type) override
345
        {
×
346
            m_fields.emplace(n, get_collection_type(type));
×
347
        }
×
348

349
        void field(StringData n, const Instruction::PrimaryKey& key) override
350
        {
×
351
            auto real_key = m_changeset->get_key(key);
×
352
            std::stringstream ss;
×
353
            ss << format_pk(real_key);
×
354
            m_fields.emplace(n, ss.str());
×
355
        }
×
356

357
        void field(StringData n, const Instruction::Payload& value) override
358
        {
×
359
            std::stringstream ss;
×
360
            m_changeset->print_value(ss, value);
×
361
            m_fields.emplace(n, ss.str());
×
362
        }
×
363

364
        void field(StringData n, const Instruction::Path& value) override
365
        {
×
366
            std::stringstream ss;
×
367
            m_changeset->print_path(ss, value);
×
368
            m_fields.emplace(n, ss.str());
×
369
        }
×
370

371
        void field(StringData n, uint32_t value) override
372
        {
×
373
            std::stringstream ss;
×
374
            ss << value;
×
375
            m_fields.emplace(n, ss.str());
×
376
        }
×
377
    };
378

379
    struct PrintDiffTracer : sync::Changeset::Reflector::Tracer {
380
        std::ostream& m_os;
381
        const FieldTracer& m_before;
382
        bool m_first = true;
383
        const Changeset* m_changeset = nullptr;
384

385
        PrintDiffTracer(std::ostream& os, const FieldTracer& before)
386
            : m_os(os)
387
            , m_before(before)
388
        {
×
389
        }
×
390

391
        void set_changeset(const Changeset* changeset) override
392
        {
×
393
            m_changeset = changeset;
×
394
        }
×
395

396
        StringData get_string(InternString str) const noexcept
397
        {
×
398
            return m_changeset->get_string(str);
×
399
        }
×
400

401
        void name(StringData n) override
402
        {
×
403
            m_os << std::left << std::setw(16) << std::string(n);
×
404
        }
×
405

406
        void path(StringData n, InternString table, const Instruction::PrimaryKey& pk,
407
                  util::Optional<InternString> field, const Instruction::Path* path) override
408
        {
×
409
            std::stringstream ss;
×
410
            m_changeset->print_path(ss, table, pk, field, path);
×
411
            diff_field(n, ss.str());
×
412
        }
×
413

414
        void field(StringData n, InternString str) override
415
        {
×
416
            diff_field(n, get_string(str));
×
417
        }
×
418

419
        void field(StringData n, Instruction::Payload::Type type) override
420
        {
×
421
            diff_field(n, get_type_name(type));
×
422
        }
×
423

424
        void field(StringData n, Instruction::AddColumn::CollectionType type) override
425
        {
×
426
            diff_field(n, get_collection_type(type));
×
427
        }
×
428

429
        void field(StringData n, const Instruction::PrimaryKey& value) override
430
        {
×
431
            std::stringstream ss;
×
432
            ss << format_pk(m_changeset->get_key(value));
×
433
            diff_field(n, ss.str());
×
434
        }
×
435

436
        void field(StringData n, const Instruction::Payload& value) override
437
        {
×
438
            std::stringstream ss;
×
439
            m_changeset->print_value(ss, value);
×
440
            diff_field(n, ss.str());
×
441
        }
×
442

443
        void field(StringData n, const Instruction::Path& value) override
444
        {
×
445
            std::stringstream ss;
×
446
            m_changeset->print_path(ss, value);
×
447
            diff_field(n, ss.str());
×
448
        }
×
449

450
        void field(StringData n, uint32_t value) override
451
        {
×
452
            std::stringstream ss;
×
453
            ss << value;
×
454
            diff_field(n, ss.str());
×
455
        }
×
456

457
        void diff_field(StringData name, std::string value)
458
        {
×
459
            std::stringstream ss;
×
460
            ss << name << "=";
×
461
            auto it = m_before.m_fields.find(name);
×
462
            if (it == m_before.m_fields.end() || it->second == value) {
×
463
                ss << value;
×
464
            }
×
465
            else {
×
466
                ss << it->second << "->" << value;
×
467
            }
×
468
            if (!m_first) {
×
469
                m_os << ", ";
×
470
            }
×
471
            m_os << ss.str();
×
472
            m_first = false;
×
473
        }
×
474
    };
475

476
    static void print_instr(std::ostream& os, const Instruction& instr, const Changeset& changeset)
477
    {
×
478
        Changeset::Printer printer{os};
×
479
        Changeset::Reflector reflector{printer, changeset};
×
480
        instr.visit(reflector);
×
481
    }
×
482

483
    bool print_diff(std::ostream& os, bool print_unmodified, const Instruction& before, const Changeset& before_log,
484
                    Side& side) const
485
    {
×
486
        if (side.was_discarded) {
×
487
            print_instr(os, before, before_log);
×
488
            os << " (DISCARDED)";
×
489
        }
×
490
        else if (side.was_replaced) {
×
491
            print_instr(os, before, before_log);
×
492
            os << " (REPLACED)";
×
493
        }
×
494
        else {
×
495
            Instruction after = side.get();
×
496
            if (print_unmodified || (before != after)) {
×
497
                FieldTracer before_tracer;
×
498
                before_tracer.set_changeset(&before_log);
×
499
                PrintDiffTracer after_tracer{os, before_tracer};
×
500
                Changeset::Reflector before_reflector{before_tracer, *side.m_changeset};
×
501
                Changeset::Reflector after_reflector{after_tracer, *side.m_changeset};
×
502
                before.visit(before_reflector);
×
503
                after.visit(after_reflector); // This prints the diff'ed instruction
×
504
            }
×
505
            else {
×
506
                os << "(=)";
×
507
            }
×
508
        }
×
509
        return true;
×
510
    }
×
511

512
    void print_diff(std::ostream& os, bool print_unmodified) const
513
    {
×
514
        bool must_print_minor = m_minor.was_discarded || m_minor.was_replaced;
×
515
        if (!must_print_minor) {
×
516
            Instruction minor_after = m_minor.get();
×
517
            must_print_minor = (minor_before != minor_after);
×
518
        }
×
519
        bool must_print_major = m_major.was_discarded || m_major.was_replaced;
×
520
        if (!must_print_major) {
×
521
            Instruction major_after = m_major.get();
×
522
            must_print_major = (major_before != major_after);
×
523
        }
×
524
        bool must_print = (print_unmodified || must_print_minor || must_print_major);
×
525
        if (must_print) {
×
526
            std::stringstream ss_minor;
×
527
            std::stringstream ss_major;
×
528

529
            print_diff(ss_minor, true, minor_before, m_minor_log, m_minor);
×
530
            print_diff(ss_major, print_unmodified, major_before, m_major_log, m_major);
×
531

532
            os << std::left << std::setw(80) << ss_minor.str();
×
533
            os << ss_major.str() << "\n";
×
534
        }
×
535
    }
×
536

537
    void pad_or_ellipsis(std::ostream& os, const std::string& str, int width) const
538
    {
×
539
        // FIXME: Does not work with UTF-8.
×
540
        if (str.size() > size_t(width)) {
×
541
            os << str.substr(0, width - 1) << "~";
×
542
        }
×
543
        else {
×
544
            os << std::left << std::setw(width) << str;
×
545
        }
×
546
    }
×
547
};
548
#endif // LCOV_EXCL_STOP REALM_DEBUG
549

550
struct TransformerImpl {
551
    MajorSide m_major_side;
552
    MinorSide m_minor_side;
553
    MinorSide::Position m_minor_end;
554
    bool m_trace;
555

556
    TransformerImpl(bool trace)
557
        : m_major_side{*this}
558
        , m_minor_side{*this}
559
        , m_trace{trace}
560
    {
44,526✔
561
    }
44,526✔
562

563
    void transform()
564
    {
525,974✔
565
        m_major_side.m_position = m_major_side.m_changeset->begin();
525,974✔
566
        m_major_side.skip_tombstones();
525,974✔
567

270,770✔
568
        while (m_major_side.m_position != m_major_side.m_changeset->end()) {
1,072,550✔
569
            m_major_side.init_with_instruction(m_major_side.m_position);
546,576✔
570

285,378✔
571
            set_conflict_ranges();
546,576✔
572
            m_minor_end = m_minor_side.end();
546,576✔
573
            m_minor_side.m_position = m_minor_side.begin();
546,576✔
574
            transform_major();
546,576✔
575

285,378✔
576
            if (!m_major_side.was_discarded)
546,576✔
577
                // Discarding the instruction moves to the next one.
276,038✔
578
                m_major_side.next_instruction();
527,764✔
579
            m_major_side.skip_tombstones();
546,576✔
580
        }
546,576✔
581
    }
525,974✔
582

583
    _impl::ChangesetIndex::Ranges* get_conflict_ranges_for_instruction(const Instruction& instr)
584
    {
546,418✔
585
        _impl::ChangesetIndex& index = *m_minor_side.m_changeset_index;
546,418✔
586

285,350✔
587
        if (_impl::is_schema_change(instr)) {
546,418✔
588
            ///
24,582✔
589
            /// CONFLICT GROUP: Everything touching that class
24,582✔
590
            ///
24,582✔
591
            auto ranges = index.get_everything();
52,478✔
592
            if (!ranges->empty()) {
52,478✔
593
#if REALM_DEBUG // LCOV_EXCL_START
19,140✔
594
                if (m_trace) {
40,914✔
595
                    std::cerr << TERM_RED << "Conflict group: Everything (due to schema change)\n" << TERM_RESET;
×
596
                }
×
597
#endif // REALM_DEBUG LCOV_EXCL_STOP
40,914✔
598
            }
40,914✔
599
            return ranges;
52,478✔
600
        }
52,478✔
601
        else {
493,940✔
602
            ///
260,768✔
603
            /// CONFLICT GROUP: Everything touching the involved objects,
260,768✔
604
            /// including schema changes.
260,768✔
605
            ///
260,768✔
606
            _impl::ChangesetIndex::GlobalID major_ids[2];
493,940✔
607
            size_t num_major_ids = m_major_side.get_object_ids_in_current_instruction(major_ids, 2);
493,940✔
608
            REALM_ASSERT(num_major_ids <= 2);
493,940✔
609
            REALM_ASSERT(num_major_ids >= 1);
493,940✔
610
#if REALM_DEBUG // LCOV_EXCL_START
260,768✔
611
            if (m_trace) {
493,940✔
612
                std::cerr << TERM_RED << "Conflict group: ";
×
613
                if (num_major_ids == 0) {
×
614
                    std::cerr << "(nothing - no object references)";
×
615
                }
×
616
                for (size_t i = 0; i < num_major_ids; ++i) {
×
617
                    std::cerr << major_ids[i].table_name << "[" << format_pk(major_ids[i].object_id) << "]";
×
618
                    if (i + 1 != num_major_ids)
×
619
                        std::cerr << ", ";
×
620
                }
×
621
                std::cerr << "\n" << TERM_RESET;
×
622
            }
×
623
#endif // REALM_DEBUG LCOV_EXCL_STOP
493,940✔
624
            auto ranges = index.get_modifications_for_object(major_ids[0]);
493,940✔
625
            if (num_major_ids == 2) {
493,940✔
626
                // Check that the index has correctly joined the ranges for the
74✔
627
                // two object IDs.
74✔
628
                REALM_ASSERT(ranges == index.get_modifications_for_object(major_ids[1]));
148✔
629
            }
148✔
630
            return ranges;
493,940✔
631
        }
493,940✔
632
    }
546,418✔
633

634
    void set_conflict_ranges()
635
    {
546,488✔
636
        const auto& major_instr = m_major_side.get();
546,488✔
637
        m_minor_side.m_conflict_ranges = get_conflict_ranges_for_instruction(major_instr);
546,488✔
638
        /* m_minor_side.m_changeset_index->verify(); */
285,352✔
639
    }
546,488✔
640

641
    void set_next_major_changeset(Changeset* changeset) noexcept
642
    {
526,134✔
643
        m_major_side.m_changeset = changeset;
526,134✔
644
        m_major_side.m_position = changeset->begin();
526,134✔
645
        m_major_side.skip_tombstones();
526,134✔
646
    }
526,134✔
647

648
    void discard_major()
649
    {
18,844✔
650
        m_major_side.m_position = m_major_side.m_changeset->erase_stable(m_major_side.m_position);
18,844✔
651
        m_major_side.was_discarded = true; // This terminates the loop in transform_major();
18,844✔
652
        m_major_side.m_changeset->set_dirty(true);
18,844✔
653
    }
18,844✔
654

655
    void discard_minor()
656
    {
18,316✔
657
        m_minor_side.was_discarded = true;
18,316✔
658
        m_minor_side.m_position = m_minor_side.m_changeset_index->erase_instruction(m_minor_side.m_position);
18,316✔
659
        m_minor_side.m_changeset->set_dirty(true);
18,316✔
660
        m_minor_side.update_changeset_pointer();
18,316✔
661
    }
18,316✔
662

663
    template <class InputIterator>
664
    void prepend_major(InputIterator instr_begin, InputIterator instr_end)
665
    {
×
666
        REALM_ASSERT(*m_major_side.m_position); // cannot prepend a tombstone
×
667
        auto insert_position = m_major_side.m_position;
×
668
        m_major_side.m_position = m_major_side.m_changeset->insert_stable(insert_position, instr_begin, instr_end);
×
669
        m_major_side.m_changeset->set_dirty(true);
×
670
        size_t num_prepended = instr_end - instr_begin;
×
671
        transform_prepended_major(num_prepended);
×
672
    }
×
673

674
    void prepend_major(Instruction instr)
675
    {
×
676
        const Instruction* begin = &instr;
×
677
        const Instruction* end = begin + 1;
×
678
        prepend_major(begin, end);
×
679
    }
×
680

681
    template <class InputIterator>
682
    void prepend_minor(InputIterator instr_begin, InputIterator instr_end)
683
    {
×
684
        REALM_ASSERT(*m_minor_side.m_position); // cannot prepend a tombstone
×
685
        auto insert_position = m_minor_side.m_position.m_pos;
×
686
        m_minor_side.m_position.m_pos =
×
687
            m_minor_side.m_changeset->insert_stable(insert_position, instr_begin, instr_end);
×
688
        m_minor_side.m_changeset->set_dirty(true);
×
689
        size_t num_prepended = instr_end - instr_begin;
×
690
        // Go back to the instruction that initiated this prepend
691
        for (size_t i = 0; i < num_prepended; ++i) {
×
692
            ++m_minor_side.m_position;
×
693
        }
×
694
        REALM_ASSERT(m_minor_end == m_minor_side.end());
×
695
    }
×
696

697
    void prepend_minor(Instruction instr)
698
    {
×
699
        const Instruction* begin = &instr;
×
700
        const Instruction* end = begin + 1;
×
701
        prepend_minor(begin, end);
×
702
    }
×
703

704
    void transform_prepended_major(size_t num_prepended)
705
    {
×
706
        auto orig_major_was_discarded = m_major_side.was_discarded;
×
707
        auto orig_major_path_len = m_major_side.m_path_len;
×
708

709
        // Reset 'was_discarded', as it should refer to the prepended
710
        // instructions in the below, not the instruction that instigated the
711
        // prepend.
712
        m_major_side.was_discarded = false;
×
713
        REALM_ASSERT(m_major_side.m_position != m_major_side.m_changeset->end());
×
714

715
#if defined(REALM_DEBUG) // LCOV_EXCL_START
×
716
        if (m_trace) {
×
717
            std::cerr << std::setw(80) << " ";
×
718
            MergeTracer::print_instr(std::cerr, m_major_side.get(), *m_major_side.m_changeset);
×
719
            std::cerr << " (PREPENDED " << num_prepended << ")\n";
×
720
        }
×
721
#endif // REALM_DEBUG LCOV_EXCL_STOP
×
722

723
        for (size_t i = 0; i < num_prepended; ++i) {
×
724
            auto orig_minor_index = m_minor_side.m_position;
×
725
            auto orig_minor_was_discarded = m_minor_side.was_discarded;
×
726
            auto orig_minor_was_replaced = m_minor_side.was_replaced;
×
727
            auto orig_minor_path_len = m_minor_side.m_path_len;
×
728

729
            // Skip the instruction that initiated this prepend.
730
            if (!m_minor_side.was_discarded) {
×
731
                // Discarding an instruction moves to the next.
732
                m_minor_side.next_instruction();
×
733
            }
×
734

735
            REALM_ASSERT(m_major_side.m_position != m_major_side.m_changeset->end());
×
736
            m_major_side.init_with_instruction(m_major_side.m_position);
×
737
            REALM_ASSERT(!m_major_side.was_discarded);
×
738
            REALM_ASSERT(m_major_side.m_position != m_major_side.m_changeset->end());
×
739
            transform_major();
×
740
            if (!m_major_side.was_discarded) {
×
741
                // Discarding an instruction moves to the next.
742
                m_major_side.next_instruction();
×
743
            }
×
744
            REALM_ASSERT(m_major_side.m_position != m_major_side.m_changeset->end());
×
745

746
            m_minor_side.m_position = orig_minor_index;
×
747
            m_minor_side.was_discarded = orig_minor_was_discarded;
×
748
            m_minor_side.was_replaced = orig_minor_was_replaced;
×
749
            m_minor_side.m_path_len = orig_minor_path_len;
×
750
            m_minor_side.update_changeset_pointer();
×
751
        }
×
752

753
#if defined(REALM_DEBUG) // LCOV_EXCL_START
×
754
        if (m_trace) {
×
755
            std::cerr << TERM_CYAN << "(end transform of prepended major)\n" << TERM_RESET;
×
756
        }
×
757
#endif // REALM_DEBUG LCOV_EXCL_STOP
×
758

759
        m_major_side.was_discarded = orig_major_was_discarded;
×
760
        m_major_side.m_path_len = orig_major_path_len;
×
761
    }
×
762

763
    void transform_major()
764
    {
546,674✔
765
        m_minor_side.skip_tombstones();
546,674✔
766

285,442✔
767
#if defined(REALM_DEBUG) // LCOV_EXCL_START Debug tracing
546,674✔
768
        const bool print_noop_merges = false;
546,674✔
769
        bool new_major = true; // print an instruction every time we go to the next major regardless
546,674✔
770
#endif                         // LCOV_EXCL_STOP REALM_DEBUG
546,674✔
771

285,442✔
772
        while (m_minor_side.m_position != m_minor_end) {
808,510✔
773
            m_minor_side.init_with_instruction(m_minor_side.m_position);
280,680✔
774

136,216✔
775
#if defined(REALM_DEBUG) // LCOV_EXCL_START Debug tracing
280,680✔
776
            if (m_trace) {
280,680✔
777
                MergeTracer tracer{m_minor_side, m_major_side};
×
778
                merge_instructions(m_major_side, m_minor_side);
×
779
                if (new_major)
×
780
                    std::cerr << TERM_CYAN << "\n(new major round)\n" << TERM_RESET;
×
781
                tracer.print_diff(std::cerr, new_major || print_noop_merges);
×
782
                new_major = false;
×
783
            }
×
784
            else {
280,680✔
785
#endif // LCOV_EXCL_STOP REALM_DEBUG
280,680✔
786
                merge_instructions(m_major_side, m_minor_side);
280,680✔
787
#if defined(REALM_DEBUG) // LCOV_EXCL_START
280,680✔
788
            }
280,680✔
789
#endif // LCOV_EXCL_STOP REALM_DEBUG
280,680✔
790

136,216✔
791
            if (m_major_side.was_discarded)
280,680✔
792
                break;
18,844✔
793
            if (!m_minor_side.was_discarded)
261,836✔
794
                // Discarding an instruction moves to the next one.
124,322✔
795
                m_minor_side.next_instruction();
256,826✔
796
            m_minor_side.skip_tombstones();
261,836✔
797
        }
261,836✔
798
    }
546,674✔
799

800
    void merge_instructions(MajorSide& left, MinorSide& right);
801
};
802

803
void MajorSide::set_next_changeset(Changeset* changeset) noexcept
804
{
526,152✔
805
    m_transformer.set_next_major_changeset(changeset);
526,152✔
806
}
526,152✔
807
void MajorSide::discard()
808
{
18,844✔
809
    m_transformer.discard_major();
18,844✔
810
}
18,844✔
811
void MajorSide::prepend(Instruction operation)
812
{
×
813
    m_transformer.prepend_major(std::move(operation));
×
814
}
×
815
template <class InputIterator>
816
void MajorSide::prepend(InputIterator begin, InputIterator end)
817
{
818
    m_transformer.prepend_major(std::move(begin), std::move(end));
819
}
820
void MinorSide::discard()
821
{
18,316✔
822
    m_transformer.discard_minor();
18,316✔
823
}
18,316✔
824
void MinorSide::prepend(Instruction operation)
825
{
×
826
    m_transformer.prepend_minor(std::move(operation));
×
827
}
×
828
template <class InputIterator>
829
void MinorSide::prepend(InputIterator begin, InputIterator end)
830
{
831
    m_transformer.prepend_minor(std::move(begin), std::move(end));
832
}
833

834
REALM_NORETURN void throw_bad_merge(std::string msg)
835
{
58✔
836
    throw sync::TransformError{std::move(msg)};
58✔
837
}
58✔
838

839
template <class... Params>
840
REALM_NORETURN void bad_merge(const char* msg, Params&&... params)
841
{
58✔
842
    throw_bad_merge(util::format(msg, std::forward<Params>(params)...));
58✔
843
}
58✔
844

845
REALM_NORETURN void bad_merge(Side& side, Instruction::PathInstruction instr, const std::string& msg)
846
{
×
847
    std::stringstream ss;
×
848
    side.m_changeset->print_path(ss, instr.table, instr.object, instr.field, &instr.path);
×
849
    bad_merge("%1 (instruction target: %2). Please contact support.", msg, ss.str());
×
850
}
×
851

852
template <class LeftInstruction, class RightInstruction, class Enable = void>
853
struct Merge;
854
template <class Outer>
855
struct MergeNested;
856

857
struct MergeUtils {
858
    MergeUtils(Side& left_side, Side& right_side)
859
        : m_left_side(left_side)
860
        , m_right_side(right_side)
861
    {
72,494✔
862
    }
72,494✔
863

864
    // CAUTION: All of these utility functions rely implicitly on the "left" and
865
    // "right" arguments corresponding to the left and right sides. If the
866
    // arguments are switched, mayhem ensues.
867

868
    bool same_string(InternString left, InternString right) const noexcept
869
    {
94,734✔
870
        // FIXME: Optimize string comparison by building a map of InternString values up front.
44,808✔
871
        return m_left_side.m_changeset->get_string(left) == m_right_side.m_changeset->get_string(right);
94,734✔
872
    }
94,734✔
873

874
    bool same_key(const Instruction::PrimaryKey& left, const Instruction::PrimaryKey& right) const noexcept
875
    {
19,810✔
876
        // FIXME: Once we can compare string by InternString map lookups,
8,778✔
877
        // compare the string components of the keys using that.
8,778✔
878
        PrimaryKey left_key = m_left_side.m_changeset->get_key(left);
19,810✔
879
        PrimaryKey right_key = m_right_side.m_changeset->get_key(right);
19,810✔
880
        return left_key == right_key;
19,810✔
881
    }
19,810✔
882

883
    bool same_payload(const Instruction::Payload& left, const Instruction::Payload& right)
884
    {
220✔
885
        using Type = Instruction::Payload::Type;
220✔
886

108✔
887
        if (left.type != right.type)
220✔
888
            return false;
160✔
889

28✔
890
        switch (left.type) {
60✔
891
            case Type::Null:
✔
892
                return true;
×
893
            case Type::Erased:
✔
894
                return true;
×
895
            case Type::Dictionary:
✔
896
                return true;
×
897
            case Type::ObjectValue:
✔
898
                return true;
×
899
            case Type::GlobalKey:
✔
900
                return left.data.key == right.data.key;
×
901
            case Type::Int:
28✔
902
                return left.data.integer == right.data.integer;
28✔
903
            case Type::Bool:
✔
904
                return left.data.boolean == right.data.boolean;
×
905
            case Type::String:
16✔
906
                return m_left_side.get_string(left.data.str) == m_right_side.get_string(right.data.str);
16✔
907
            case Type::Binary:
✔
908
                return m_left_side.get_string(left.data.binary) == m_right_side.get_string(right.data.binary);
×
909
            case Type::Timestamp:
✔
910
                return left.data.timestamp == right.data.timestamp;
×
911
            case Type::Float:
16✔
912
                return left.data.fnum == right.data.fnum;
16✔
913
            case Type::Double:
✔
914
                return left.data.dnum == right.data.dnum;
×
915
            case Type::Decimal:
✔
916
                return left.data.decimal == right.data.decimal;
×
917
            case Type::Link: {
✔
918
                if (!same_key(left.data.link.target, right.data.link.target)) {
×
919
                    return false;
×
920
                }
×
921
                auto left_target = m_left_side.get_string(left.data.link.target_table);
×
922
                auto right_target = m_right_side.get_string(right.data.link.target_table);
×
923
                return left_target == right_target;
×
924
            }
×
925
            case Type::ObjectId:
✔
926
                return left.data.object_id == right.data.object_id;
×
927
            case Type::UUID:
✔
928
                return left.data.uuid == right.data.uuid;
×
929
        }
×
930

931
        bad_merge("Invalid payload type in instruction");
×
932
    }
×
933

934
    bool same_path_element(const Instruction::Path::Element& left,
935
                           const Instruction::Path::Element& right) const noexcept
936
    {
300✔
937
        auto pred = util::overload{
300✔
938
            [](uint32_t lhs, uint32_t rhs) {
216✔
939
                return lhs == rhs;
132✔
940
            },
132✔
941
            [this](InternString lhs, InternString rhs) {
198✔
942
                return same_string(lhs, rhs);
168✔
943
            },
168✔
944
            // FIXME: Paths contain incompatible element types. Should we raise an error here?
114✔
945
            [](InternString, uint32_t) {
114✔
946
                return false;
×
947
            },
×
948
            [](uint32_t, InternString) {
114✔
949
                return false;
×
950
            },
×
951
        };
300✔
952
        return mpark::visit(pred, left, right);
300✔
953
    }
300✔
954

955
    bool same_path(const Instruction::Path& left, const Instruction::Path& right) const noexcept
956
    {
7,248✔
957
        if (left.size() == right.size()) {
7,248✔
958
            for (size_t i = 0; i < left.size(); ++i) {
7,298✔
959
                if (!same_path_element(left[i], right[i])) {
292✔
960
                    return false;
176✔
961
                }
176✔
962
            }
292✔
963
            return true;
7,060✔
964
        }
66✔
965
        return false;
66✔
966
    }
66✔
967

968
    bool same_table(const Instruction::TableInstruction& left,
969
                    const Instruction::TableInstruction& right) const noexcept
970
    {
72,036✔
971
        return same_string(left.table, right.table);
72,036✔
972
    }
72,036✔
973

974
    bool same_object(const Instruction::ObjectInstruction& left,
975
                     const Instruction::ObjectInstruction& right) const noexcept
976
    {
46,806✔
977
        return same_table(left, right) && same_key(left.object, right.object);
46,806✔
978
    }
46,806✔
979

980
    template <class Left, class Right>
981
    bool same_column(const Left& left, const Right& right) const noexcept
982
    {
14,374✔
983
        if constexpr (std::is_convertible_v<const Right&, const Instruction::PathInstruction&>) {
14,374✔
984
            const Instruction::PathInstruction& rhs = right;
14,374✔
985
            return same_table(left, right) && same_string(left.field, rhs.field);
7,070!
986
        }
×
987
        else if constexpr (std::is_convertible_v<const Right&, const Instruction::ObjectInstruction&>) {
14,374✔
988
            // CreateObject/EraseObject do not have a column built in.
7,070✔
989
            return false;
14,374✔
990
        }
14,374✔
991
        else {
14,374✔
992
            return same_table(left, right) && same_string(left.field, right.field);
14,374!
993
        }
14,374✔
994
    }
14,374✔
995

996
    bool same_field(const Instruction::PathInstruction& left,
997
                    const Instruction::PathInstruction& right) const noexcept
998
    {
14,308✔
999
        return same_object(left, right) && same_string(left.field, right.field);
14,308✔
1000
    }
14,308✔
1001

1002
    bool same_path(const Instruction::PathInstruction& left, const Instruction::PathInstruction& right) const noexcept
1003
    {
8,296✔
1004
        return same_field(left, right) && same_path(left.path, right.path);
8,296✔
1005
    }
8,296✔
1006

1007
    bool same_container(const Instruction::Path& left, const Instruction::Path& right) const noexcept
1008
    {
1,304✔
1009
        // The instructions refer to the same container if the paths have the
236✔
1010
        // same length, and elements [0..n-1] are equal (so the last element is
236✔
1011
        // disregarded). If the path length is 1, this counts as referring to
236✔
1012
        // the same container.
236✔
1013
        if (left.size() == right.size()) {
1,304✔
1014
            if (left.size() == 0)
1,280✔
1015
                return true;
×
1016

224✔
1017
            for (size_t i = 0; i < left.size() - 1; ++i) {
1,280✔
1018
                if (!same_path_element(left[i], right[i])) {
×
1019
                    return false;
×
1020
                }
×
1021
            }
×
1022
            return true;
1,280✔
1023
        }
24✔
1024
        return false;
24✔
1025
    }
24✔
1026

1027
    bool same_container(const Instruction::PathInstruction& left,
1028
                        const Instruction::PathInstruction& right) const noexcept
1029
    {
4,240✔
1030
        return same_field(left, right) && same_container(left.path, right.path);
4,240✔
1031
    }
4,240✔
1032

1033
    // NOTE: `is_prefix_of()` should only return true if the left is a strictly
1034
    // shorter path than the right, and the entire left path is the initial
1035
    // sequence of the right.
1036

1037
    bool is_prefix_of(const Instruction::AddTable& left, const Instruction::TableInstruction& right) const noexcept
1038
    {
×
1039
        return same_table(left, right);
×
1040
    }
×
1041

1042
    bool is_prefix_of(const Instruction::EraseTable& left, const Instruction::TableInstruction& right) const noexcept
1043
    {
6,308✔
1044
        return same_table(left, right);
6,308✔
1045
    }
6,308✔
1046

1047
    bool is_prefix_of(const Instruction::AddColumn&, const Instruction::TableInstruction&) const noexcept
1048
    {
×
1049
        // Right side is a schema instruction.
×
1050
        return false;
×
1051
    }
×
1052

1053
    bool is_prefix_of(const Instruction::EraseColumn&, const Instruction::TableInstruction&) const noexcept
1054
    {
×
1055
        // Right side is a schema instruction.
×
1056
        return false;
×
1057
    }
×
1058

1059
    bool is_prefix_of(const Instruction::AddColumn& left, const Instruction::ObjectInstruction& right) const noexcept
1060
    {
×
1061
        return same_column(left, right);
×
1062
    }
×
1063

1064
    bool is_prefix_of(const Instruction::EraseColumn& left,
1065
                      const Instruction::ObjectInstruction& right) const noexcept
1066
    {
×
1067
        return same_column(left, right);
×
1068
    }
×
1069

1070
    bool is_prefix_of(const Instruction::ObjectInstruction&, const Instruction::TableInstruction&) const noexcept
1071
    {
×
1072
        // Right side is a schema instruction.
1073
        return false;
×
1074
    }
×
1075

1076
    bool is_prefix_of(const Instruction::ObjectInstruction& left,
1077
                      const Instruction::PathInstruction& right) const noexcept
1078
    {
8,178✔
1079
        return same_object(left, right);
8,178✔
1080
    }
8,178✔
1081

1082
    bool is_prefix_of(const Instruction::PathInstruction&, const Instruction::TableInstruction&) const noexcept
1083
    {
×
1084
        // Path instructions can never be full prefixes of table-level instructions. Note that this also covers
1085
        // ObjectInstructions.
1086
        return false;
×
1087
    }
×
1088

1089
    bool is_prefix_of(const Instruction::PathInstruction& left,
1090
                      const Instruction::PathInstruction& right) const noexcept
1091
    {
1,748✔
1092
        if (left.path.size() < right.path.size() && same_field(left, right)) {
1,748✔
1093
            for (size_t i = 0; i < left.path.size(); ++i) {
128✔
1094
                if (!same_path_element(left.path[i], right.path[i])) {
8✔
1095
                    return false;
8✔
1096
                }
8✔
1097
            }
8✔
1098
            return true;
124✔
1099
        }
1,620✔
1100
        return false;
1,620✔
1101
    }
1,620✔
1102

1103
    // True if the left side is an instruction that touches a container within
1104
    // right's path. Equivalent to is_prefix_of, except the last element (the
1105
    // index) is not considered.
1106
    bool is_container_prefix_of(const Instruction::PathInstruction& left,
1107
                                const Instruction::PathInstruction& right) const
1108
    {
24✔
1109
        if (left.path.size() != 0 && left.path.size() < right.path.size() && same_field(left, right)) {
24✔
1110
            for (size_t i = 0; i < left.path.size() - 1; ++i) {
24✔
1111
                if (!same_path_element(left.path[i], right.path[i])) {
×
1112
                    return false;
×
1113
                }
×
1114
            }
×
1115
            return true;
24✔
1116
        }
×
1117
        return false;
×
1118
    }
×
1119

1120
    bool is_container_prefix_of(const Instruction::PathInstruction&, const Instruction::TableInstruction&) const
1121
    {
×
1122
        return false;
×
1123
    }
×
1124

1125
    // When the left side has a shorter path, get the path component on the
1126
    // right that corresponds to the last component on the left.
1127
    //
1128
    // Note that this will only be used in the context of array indices, because
1129
    // those are the only path components that are modified during OT.
1130
    uint32_t& corresponding_index_in_path(const Instruction::PathInstruction& left,
1131
                                          Instruction::PathInstruction& right) const
1132
    {
24✔
1133
        REALM_ASSERT(left.path.size() != 0);
24✔
1134
        REALM_ASSERT(left.path.size() < right.path.size());
24✔
1135
        REALM_ASSERT(mpark::holds_alternative<uint32_t>(left.path.back()));
24✔
1136
        size_t index = left.path.size() - 1;
24✔
1137
        if (!mpark::holds_alternative<uint32_t>(right.path[index])) {
24✔
1138
            bad_merge("Inconsistent paths");
×
1139
        }
×
1140
        return mpark::get<uint32_t>(right.path[index]);
24✔
1141
    }
24✔
1142

1143
    uint32_t& corresponding_index_in_path(const Instruction::PathInstruction&,
1144
                                          const Instruction::TableInstruction&) const
1145
    {
×
1146
        // A path instruction can never have a shorter path than something that
1147
        // isn't a PathInstruction.
1148
        REALM_UNREACHABLE();
1149
    }
×
1150

1151
    void merge_get_vs_move(uint32_t& get_ndx, const uint32_t& move_from_ndx,
1152
                           const uint32_t& move_to_ndx) const noexcept
1153
    {
×
1154
        if (get_ndx == move_from_ndx) {
×
1155
            // CONFLICT: Update of a moved element.
1156
            //
1157
            // RESOLUTION: On the left side, use the MOVE operation to transform the
1158
            // UPDATE operation received from the right side.
1159
            get_ndx = move_to_ndx; // --->
×
1160
        }
×
1161
        else {
×
1162
            // Right update vs left remove
1163
            if (get_ndx > move_from_ndx) {
×
1164
                get_ndx -= 1; // --->
×
1165
            }
×
1166
            // Right update vs left insert
1167
            if (get_ndx >= move_to_ndx) {
×
1168
                get_ndx += 1; // --->
×
1169
            }
×
1170
        }
×
1171
    }
×
1172

1173
protected:
1174
    Side& m_left_side;
1175
    Side& m_right_side;
1176
};
1177

1178
template <class LeftInstruction, class RightInstruction>
1179
struct MergeBase : MergeUtils {
1180
    static const Instruction::Type A = Instruction::GetInstructionType<LeftInstruction>::value;
1181
    static const Instruction::Type B = Instruction::GetInstructionType<RightInstruction>::value;
1182
    static_assert(A >= B, "A < B. Please reverse the order of instruction types. :-)");
1183

1184
    MergeBase(Side& left_side, Side& right_side)
1185
        : MergeUtils(left_side, right_side)
1186
    {
56,182✔
1187
    }
56,182✔
1188
    MergeBase(MergeBase&&) = delete;
1189
};
1190

1191
#define DEFINE_MERGE(A, B)                                                                                           \
1192
    template <>                                                                                                      \
1193
    struct Merge<A, B> {                                                                                             \
1194
        template <class LeftSide, class RightSide>                                                                   \
1195
        struct DoMerge : MergeBase<A, B> {                                                                           \
1196
            A& left;                                                                                                 \
1197
            B& right;                                                                                                \
1198
            LeftSide& left_side;                                                                                     \
1199
            RightSide& right_side;                                                                                   \
1200
            DoMerge(A& left, B& right, LeftSide& left_side, RightSide& right_side)                                   \
1201
                : MergeBase<A, B>(left_side, right_side)                                                             \
1202
                , left(left)                                                                                         \
1203
                , right(right)                                                                                       \
1204
                , left_side(left_side)                                                                               \
1205
                , right_side(right_side)                                                                             \
1206
            {                                                                                                        \
56,182✔
1207
            }                                                                                                        \
56,182✔
1208
            void do_merge();                                                                                         \
1209
        };                                                                                                           \
1210
        template <class LeftSide, class RightSide>                                                                   \
1211
        static inline void merge(A& left, B& right, LeftSide& left_side, RightSide& right_side)                      \
1212
        {                                                                                                            \
56,182✔
1213
            DoMerge<LeftSide, RightSide> do_merge{left, right, left_side, right_side};                               \
56,182✔
1214
            do_merge.do_merge();                                                                                     \
56,182✔
1215
        }                                                                                                            \
56,182✔
1216
    };                                                                                                               \
1217
    template <class LeftSide, class RightSide>                                                                       \
1218
    void Merge<A, B>::DoMerge<LeftSide, RightSide>::do_merge()
1219

1220
#define DEFINE_MERGE_NOOP(A, B)                                                                                      \
1221
    template <>                                                                                                      \
1222
    struct Merge<A, B> {                                                                                             \
1223
        static const Instruction::Type left_type = Instruction::GetInstructionType<A>::value;                        \
1224
        static const Instruction::Type right_type = Instruction::GetInstructionType<B>::value;                       \
1225
        static_assert(left_type >= right_type,                                                                       \
1226
                      "left_type < right_type. Please reverse the order of instruction types. :-)");                 \
1227
        template <class LeftSide, class RightSide>                                                                   \
1228
        static inline void merge(A&, B&, LeftSide&, RightSide&)                                                      \
1229
        { /* Do nothing */                                                                                           \
222,002✔
1230
        }                                                                                                            \
222,002✔
1231
    }
1232

1233

1234
#define DEFINE_NESTED_MERGE(A)                                                                                       \
1235
    template <>                                                                                                      \
1236
    struct MergeNested<A> {                                                                                          \
1237
        template <class B, class OuterSide, class InnerSide>                                                         \
1238
        struct DoMerge : MergeUtils {                                                                                \
1239
            A& outer;                                                                                                \
1240
            B& inner;                                                                                                \
1241
            OuterSide& outer_side;                                                                                   \
1242
            InnerSide& inner_side;                                                                                   \
1243
            DoMerge(A& outer, B& inner, OuterSide& outer_side, InnerSide& inner_side)                                \
1244
                : MergeUtils(outer_side, inner_side)                                                                 \
1245
                , outer(outer)                                                                                       \
1246
                , inner(inner)                                                                                       \
1247
                , outer_side(outer_side)                                                                             \
1248
                , inner_side(inner_side)                                                                             \
1249
            {                                                                                                        \
16,314✔
1250
            }                                                                                                        \
16,314✔
1251
            void do_merge();                                                                                         \
1252
        };                                                                                                           \
1253
        template <class B, class OuterSide, class InnerSide>                                                         \
1254
        static inline void merge(A& outer, B& inner, OuterSide& outer_side, InnerSide& inner_side)                   \
1255
        {                                                                                                            \
16,314✔
1256
            DoMerge<B, OuterSide, InnerSide> do_merge{outer, inner, outer_side, inner_side};                         \
16,314✔
1257
            do_merge.do_merge();                                                                                     \
16,314✔
1258
        }                                                                                                            \
16,314✔
1259
    };                                                                                                               \
1260
    template <class B, class OuterSide, class InnerSide>                                                             \
1261
    void MergeNested<A>::DoMerge<B, OuterSide, InnerSide>::do_merge()
1262

1263
#define DEFINE_NESTED_MERGE_NOOP(A)                                                                                  \
1264
    template <>                                                                                                      \
1265
    struct MergeNested<A> {                                                                                          \
1266
        template <class B, class OuterSide, class InnerSide>                                                         \
1267
        static inline void merge(const A&, const B&, const OuterSide&, const InnerSide&)                             \
1268
        { /* Do nothing */                                                                                           \
28,736✔
1269
        }                                                                                                            \
28,736✔
1270
    }
1271

1272
// Implementation that reverses order.
1273
template <class A, class B>
1274
struct Merge<A, B,
1275
             typename std::enable_if<(Instruction::GetInstructionType<A>::value <
1276
                                      Instruction::GetInstructionType<B>::value)>::type> {
1277
    template <class LeftSide, class RightSide>
1278
    static void merge(A& left, B& right, LeftSide& left_side, RightSide& right_side)
1279
    {
39,546✔
1280
        Merge<B, A>::merge(right, left, right_side, left_side);
39,546✔
1281
    }
39,546✔
1282
};
1283

1284
///
1285
///  GET READY!
1286
///
1287
///  Realm supports 14 instructions at the time of this writing. Each
1288
///  instruction type needs one rule for each other instruction type. We only
1289
///  define one rule to handle each combination (A vs B and B vs A are handle by
1290
///  a single rule).
1291
///
1292
///  This gives (14 * (14 + 1)) / 2 = 105 merge rules below.
1293
///
1294
///  Merge rules are ordered such that the second instruction type is always of
1295
///  a lower enum value than the first.
1296
///
1297
///  Nested merge rules apply when one instruction has a strictly longer path
1298
///  than another. All instructions that have a path of the same length will
1299
///  meet each other through regular merge rules, regardless of whether they
1300
///  share a prefix.
1301
///
1302

1303

1304
/// AddTable rules
1305

1306
DEFINE_NESTED_MERGE_NOOP(Instruction::AddTable);
1307

1308
DEFINE_MERGE(Instruction::AddTable, Instruction::AddTable)
1309
{
4,354✔
1310
    if (same_table(left, right)) {
4,354✔
1311
        StringData left_name = left_side.get_string(left.table);
4,026✔
1312
        if (auto left_spec = mpark::get_if<Instruction::AddTable::TopLevelTable>(&left.type)) {
4,026✔
1313
            if (auto right_spec = mpark::get_if<Instruction::AddTable::TopLevelTable>(&right.type)) {
3,866✔
1314
                StringData left_pk_name = left_side.get_string(left_spec->pk_field);
3,866✔
1315
                StringData right_pk_name = right_side.get_string(right_spec->pk_field);
3,866✔
1316
                if (left_pk_name != right_pk_name) {
3,866✔
1317
                    bad_merge(
8✔
1318
                        "Schema mismatch: '%1' has primary key '%2' on one side, but primary key '%3' on the other.",
8✔
1319
                        left_name, left_pk_name, right_pk_name);
8✔
1320
                }
8✔
1321

1,918✔
1322
                if (left_spec->pk_type != right_spec->pk_type) {
3,866✔
1323
                    bad_merge("Schema mismatch: '%1' has primary key '%2', which is of type %3 on one side and type "
8✔
1324
                              "%4 on the other.",
8✔
1325
                              left_name, left_pk_name, get_type_name(left_spec->pk_type),
8✔
1326
                              get_type_name(right_spec->pk_type));
8✔
1327
                }
8✔
1328

1,918✔
1329
                if (left_spec->pk_nullable != right_spec->pk_nullable) {
3,866✔
1330
                    bad_merge("Schema mismatch: '%1' has primary key '%2', which is nullable on one side, but not "
8✔
1331
                              "the other.",
8✔
1332
                              left_name, left_pk_name);
8✔
1333
                }
8✔
1334

1,918✔
1335
                if (left_spec->is_asymmetric != right_spec->is_asymmetric) {
3,866✔
1336
                    bad_merge("Schema mismatch: '%1' is asymmetric on one side, but not on the other.", left_name);
4✔
1337
                }
4✔
1338
            }
3,866✔
UNCOV
1339
            else {
×
UNCOV
1340
                bad_merge("Schema mismatch: '%1' has a primary key on one side, but not on the other.", left_name);
×
UNCOV
1341
            }
×
1342
        }
3,866✔
1343
        else if (mpark::get_if<Instruction::AddTable::EmbeddedTable>(&left.type)) {
160✔
1344
            if (!mpark::get_if<Instruction::AddTable::EmbeddedTable>(&right.type)) {
160✔
1345
                bad_merge("Schema mismatch: '%1' is an embedded table on one side, but not the other.", left_name);
×
1346
            }
×
1347
        }
160✔
1348

1,998✔
1349
        // Names are the same, PK presence is the same, and if there is a primary
1,998✔
1350
        // key, its name, type, and nullability are the same. Discard both sides.
1,998✔
1351
        left_side.discard();
4,026✔
1352
        right_side.discard();
4,026✔
1353
        return;
4,026✔
1354
    }
4,026✔
1355
}
4,354✔
1356

1357
DEFINE_MERGE(Instruction::EraseTable, Instruction::AddTable)
1358
{
148✔
1359
    if (same_table(left, right)) {
148✔
1360
        right_side.discard();
×
1361
    }
×
1362
}
148✔
1363

1364
DEFINE_MERGE_NOOP(Instruction::CreateObject, Instruction::AddTable);
1365
DEFINE_MERGE_NOOP(Instruction::EraseObject, Instruction::AddTable);
1366
DEFINE_MERGE_NOOP(Instruction::Update, Instruction::AddTable);
1367
DEFINE_MERGE_NOOP(Instruction::AddInteger, Instruction::AddTable);
1368
DEFINE_MERGE_NOOP(Instruction::AddColumn, Instruction::AddTable);
1369
DEFINE_MERGE_NOOP(Instruction::EraseColumn, Instruction::AddTable);
1370
DEFINE_MERGE_NOOP(Instruction::ArrayInsert, Instruction::AddTable);
1371
DEFINE_MERGE_NOOP(Instruction::ArrayMove, Instruction::AddTable);
1372
DEFINE_MERGE_NOOP(Instruction::ArrayErase, Instruction::AddTable);
1373
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::AddTable);
1374
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::AddTable);
1375
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::AddTable);
1376

1377
/// EraseTable rules
1378

1379
DEFINE_NESTED_MERGE(Instruction::EraseTable)
1380
{
6,308✔
1381
    if (is_prefix_of(outer, inner)) {
6,308!
1382
        inner_side.discard();
1,228✔
1383
    }
1,228✔
1384
}
6,308✔
1385

1386
DEFINE_MERGE(Instruction::EraseTable, Instruction::EraseTable)
1387
{
48✔
1388
    if (same_table(left, right)) {
48✔
1389
        left_side.discard();
12✔
1390
        right_side.discard();
12✔
1391
    }
12✔
1392
}
48✔
1393

1394
// Handled by nesting rule.
1395
DEFINE_MERGE_NOOP(Instruction::CreateObject, Instruction::EraseTable);
1396
DEFINE_MERGE_NOOP(Instruction::EraseObject, Instruction::EraseTable);
1397
DEFINE_MERGE_NOOP(Instruction::Update, Instruction::EraseTable);
1398
DEFINE_MERGE_NOOP(Instruction::AddInteger, Instruction::EraseTable);
1399

1400
DEFINE_MERGE(Instruction::AddColumn, Instruction::EraseTable)
1401
{
404✔
1402
    // AddColumn on an erased table handled by nesting.
180✔
1403

180✔
1404
    if (left.type == Instruction::Payload::Type::Link && same_string(left.link_target_table, right.table)) {
404!
1405
        // Erase of a table where the left side adds a link column targeting it.
1406
        Instruction::EraseColumn erase_column;
×
1407
        erase_column.table = right_side.adopt_string(left_side, left.table);
×
1408
        erase_column.field = right_side.adopt_string(left_side, left.field);
×
1409
        right_side.prepend(erase_column);
×
1410
        left_side.discard();
×
1411
    }
×
1412
}
404✔
1413

1414
// Handled by nested rule.
1415
DEFINE_MERGE_NOOP(Instruction::EraseColumn, Instruction::EraseTable);
1416
DEFINE_MERGE_NOOP(Instruction::ArrayInsert, Instruction::EraseTable);
1417
DEFINE_MERGE_NOOP(Instruction::ArrayMove, Instruction::EraseTable);
1418
DEFINE_MERGE_NOOP(Instruction::ArrayErase, Instruction::EraseTable);
1419
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::EraseTable);
1420
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::EraseTable);
1421
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::EraseTable);
1422

1423

1424
/// CreateObject rules
1425

1426
// CreateObject cannot interfere with instructions that have a longer path.
1427
DEFINE_NESTED_MERGE_NOOP(Instruction::CreateObject);
1428

1429
// CreateObject is idempotent.
1430
DEFINE_MERGE_NOOP(Instruction::CreateObject, Instruction::CreateObject);
1431

1432
DEFINE_MERGE(Instruction::EraseObject, Instruction::CreateObject)
1433
{
18,056✔
1434
    if (same_object(left, right)) {
18,056✔
1435
        // CONFLICT: Create and Erase of the same object.
486✔
1436
        //
486✔
1437
        // RESOLUTION: Erase always wins.
486✔
1438
        right_side.discard();
762✔
1439
    }
762✔
1440
}
18,056✔
1441

1442
DEFINE_MERGE_NOOP(Instruction::Update, Instruction::CreateObject);
1443
DEFINE_MERGE_NOOP(Instruction::AddInteger, Instruction::CreateObject);
1444
DEFINE_MERGE_NOOP(Instruction::AddColumn, Instruction::CreateObject);
1445
DEFINE_MERGE_NOOP(Instruction::EraseColumn, Instruction::CreateObject);
1446
DEFINE_MERGE_NOOP(Instruction::ArrayInsert, Instruction::CreateObject);
1447
DEFINE_MERGE_NOOP(Instruction::ArrayMove, Instruction::CreateObject);
1448
DEFINE_MERGE_NOOP(Instruction::ArrayErase, Instruction::CreateObject);
1449
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::CreateObject);
1450
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::CreateObject);
1451
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::CreateObject);
1452

1453

1454
/// Erase rules
1455

1456
DEFINE_NESTED_MERGE(Instruction::EraseObject)
1457
{
8,178✔
1458
    if (is_prefix_of(outer, inner)) {
8,178!
1459
        // Erase always wins.
422✔
1460
        inner_side.discard();
1,178✔
1461
    }
1,178✔
1462
}
8,178✔
1463

1464
DEFINE_MERGE(Instruction::EraseObject, Instruction::EraseObject)
1465
{
6,262✔
1466
    if (same_object(left, right)) {
6,262✔
1467
        // We keep the most recent erase. This prevents the situation where a
372✔
1468
        // high number of EraseObject instructions in the past trumps a
372✔
1469
        // Erase-Create pair in the future.
372✔
1470
        if (right_side.timestamp() < left_side.timestamp()) {
612✔
1471
            right_side.discard();
306✔
1472
        }
306✔
1473
        else {
306✔
1474
            left_side.discard();
306✔
1475
        }
306✔
1476
    }
612✔
1477
}
6,262✔
1478

1479
// Handled by nested merge.
1480
DEFINE_MERGE_NOOP(Instruction::Update, Instruction::EraseObject);
1481
DEFINE_MERGE_NOOP(Instruction::AddInteger, Instruction::EraseObject);
1482
DEFINE_MERGE_NOOP(Instruction::AddColumn, Instruction::EraseObject);
1483
DEFINE_MERGE_NOOP(Instruction::EraseColumn, Instruction::EraseObject);
1484
DEFINE_MERGE_NOOP(Instruction::ArrayInsert, Instruction::EraseObject);
1485
DEFINE_MERGE_NOOP(Instruction::ArrayMove, Instruction::EraseObject);
1486
DEFINE_MERGE_NOOP(Instruction::ArrayErase, Instruction::EraseObject);
1487
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::EraseObject);
1488
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::EraseObject);
1489
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::EraseObject);
1490

1491

1492
/// Set rules
1493

1494
DEFINE_NESTED_MERGE(Instruction::Update)
1495
{
1,764✔
1496
    using Type = Instruction::Payload::Type;
1,764✔
1497

440✔
1498
    if (outer.value.type == Type::ObjectValue || outer.value.type == Type::Dictionary) {
1,764!
1499
        // Creating an embedded object or a dictionary is an idempotent
32✔
1500
        // operation, and should not eliminate updates to the subtree.
32✔
1501
        return;
64✔
1502
    }
64✔
1503

408✔
1504
    // Setting a value higher up in the hierarchy overwrites any modification to
408✔
1505
    // the inner value, regardless of when this happened.
408✔
1506
    if (is_prefix_of(outer, inner)) {
1,700!
1507
        inner_side.discard();
80✔
1508
    }
80✔
1509
}
1,700✔
1510

1511
DEFINE_MERGE(Instruction::Update, Instruction::Update)
1512
{
7,592✔
1513
    // The two instructions are at the same level of nesting.
3,652✔
1514

3,652✔
1515
    using Type = Instruction::Payload::Type;
7,592✔
1516

3,652✔
1517
    if (same_path(left, right)) {
7,592✔
1518
        bool left_is_default = false;
6,470✔
1519
        bool right_is_default = false;
6,470✔
1520
        if (!(left.is_array_update() == right.is_array_update())) {
6,470✔
1521
            bad_merge(left_side, left, "Merge error: left.is_array_update() == right.is_array_update()");
×
1522
        }
×
1523

3,242✔
1524
        if (!left.is_array_update()) {
6,470✔
1525
            if (right.is_array_update()) {
6,452✔
1526
                bad_merge(right_side, right, "Merge error: !right.is_array_update()");
×
1527
            }
×
1528
            left_is_default = left.is_default;
6,452✔
1529
            right_is_default = right.is_default;
6,452✔
1530
        }
6,452✔
1531
        else if (!(left.prior_size == right.prior_size)) {
18✔
1532
            bad_merge(left_side, left, "Merge error: left.prior_size == right.prior_size");
×
1533
        }
×
1534

3,242✔
1535
        if (left.value.type != right.value.type) {
6,470✔
1536
            // Embedded object / dictionary creation should always lose to an
48✔
1537
            // Update(value), because these structures are nested, and we need to
48✔
1538
            // discard any update inside the structure.
48✔
1539
            if (left.value.type == Type::Dictionary || left.value.type == Type::ObjectValue) {
104✔
1540
                left_side.discard();
24✔
1541
                return;
24✔
1542
            }
24✔
1543
            else if (right.value.type == Type::Dictionary || right.value.type == Type::ObjectValue) {
80✔
1544
                right_side.discard();
24✔
1545
                return;
24✔
1546
            }
24✔
1547
        }
6,422✔
1548

3,218✔
1549
        // CONFLICT: Two updates of the same element.
3,218✔
1550
        //
3,218✔
1551
        // RESOLUTION: Suppress the effect of the UPDATE operation with the lower
3,218✔
1552
        // timestamp. Note that the timestamps can never be equal. This is
3,218✔
1553
        // achieved on both sides by discarding the received UPDATE operation if
3,218✔
1554
        // it has a lower timestamp than the previously applied UPDATE operation.
3,218✔
1555
        if (left_is_default == right_is_default) {
6,422✔
1556
            if (left_side.timestamp() < right_side.timestamp()) {
6,352✔
1557
                left_side.discard(); // --->
3,362✔
1558
            }
3,362✔
1559
            else {
2,990✔
1560
                right_side.discard(); // <---
2,990✔
1561
            }
2,990✔
1562
        }
6,352✔
1563
        else {
70✔
1564
            if (left_is_default) {
70✔
1565
                left_side.discard();
36✔
1566
            }
36✔
1567
            else {
34✔
1568
                right_side.discard();
34✔
1569
            }
34✔
1570
        }
70✔
1571
    }
6,422✔
1572
}
7,592✔
1573

1574
DEFINE_MERGE(Instruction::AddInteger, Instruction::Update)
1575
{
446✔
1576
    // The two instructions are at the same level of nesting.
148✔
1577

148✔
1578
    if (same_path(left, right)) {
446✔
1579
        // CONFLICT: Add vs Set of the same element.
148✔
1580
        //
148✔
1581
        // RESOLUTION: If the Add was later than the Set, add its value to
148✔
1582
        // the payload of the Set instruction. Otherwise, discard it.
148✔
1583

148✔
1584
        if (!(right.value.type == Instruction::Payload::Type::Int || right.value.is_null())) {
292✔
1585
            bad_merge(right_side, right,
×
1586
                      "Merge error: right.value.type == Instruction::Payload::Type::Int || right.value.is_null()");
×
1587
        }
×
1588

148✔
1589
        bool right_is_default = !right.is_array_update() && right.is_default;
292✔
1590

148✔
1591
        // Note: AddInteger survives SetDefault, regardless of timestamp.
148✔
1592
        if (right_side.timestamp() < left_side.timestamp() || right_is_default) {
292✔
1593
            if (right.value.is_null()) {
272✔
1594
                // The AddInteger happened "after" the Set(null). This becomes a
28✔
1595
                // no-op, but if the server later integrates a Set(int) that
28✔
1596
                // came-before the AddInteger, it will be taken into account again.
28✔
1597
                return;
56✔
1598
            }
56✔
1599

108✔
1600
            // Wrapping add
108✔
1601
            uint64_t ua = uint64_t(right.value.data.integer);
216✔
1602
            uint64_t ub = uint64_t(left.value);
216✔
1603
            right.value.data.integer = int64_t(ua + ub);
216✔
1604
        }
216✔
1605
        else {
20✔
1606
            left_side.discard();
20✔
1607
        }
20✔
1608
    }
292✔
1609
}
446✔
1610

1611
DEFINE_MERGE_NOOP(Instruction::AddColumn, Instruction::Update);
1612

1613
DEFINE_MERGE(Instruction::EraseColumn, Instruction::Update)
1614
{
×
1615
    if (same_column(left, right)) {
×
1616
        right_side.discard();
×
1617
    }
×
1618
}
×
1619

1620
DEFINE_MERGE(Instruction::ArrayInsert, Instruction::Update)
1621
{
1,882✔
1622
    if (same_container(left, right)) {
1,882✔
1623
        REALM_ASSERT(right.is_array_update());
312✔
1624
        if (!(left.prior_size == right.prior_size)) {
312✔
1625
            bad_merge(left_side, left, "Merge error: left.prior_size == right.prior_size");
×
1626
        }
×
1627
        if (!(left.index() <= left.prior_size)) {
312✔
1628
            bad_merge(left_side, left, "Merge error: left.index() <= left.prior_size");
×
1629
        }
×
1630
        if (!(right.index() < right.prior_size)) {
312✔
1631
            bad_merge(right_side, right, "Merge error: right.index() < right.prior_size");
×
1632
        }
×
1633
        right.prior_size += 1;
312✔
1634
        if (right.index() >= left.index()) {
312✔
1635
            right.index() += 1; // --->
120✔
1636
        }
120✔
1637
    }
312✔
1638
}
1,882✔
1639

1640
DEFINE_MERGE(Instruction::ArrayMove, Instruction::Update)
1641
{
24✔
1642
    if (same_container(left, right)) {
24✔
1643
        REALM_ASSERT(right.is_array_update());
×
1644

1645
        if (!(left.index() < left.prior_size)) {
×
1646
            bad_merge(left_side, left, "Merge error: left.index() < left.prior_size");
×
1647
        }
×
1648
        if (!(right.index() < right.prior_size)) {
×
1649
            bad_merge(right_side, right, "Merge error: right.index() < right.prior_size");
×
1650
        }
×
1651

1652
        // FIXME: This marks both sides as dirty, even when they are unmodified.
1653
        merge_get_vs_move(right.index(), left.index(), left.ndx_2);
×
1654
    }
×
1655
}
24✔
1656

1657
DEFINE_MERGE(Instruction::ArrayErase, Instruction::Update)
1658
{
224✔
1659
    if (same_container(left, right)) {
224✔
1660
        REALM_ASSERT(right.is_array_update());
80✔
1661
        if (!(left.prior_size == right.prior_size)) {
80✔
1662
            bad_merge(left_side, left, "Merge error: left.prior_size == right.prior_size");
×
1663
        }
×
1664
        if (!(left.index() < left.prior_size)) {
80✔
1665
            bad_merge(left_side, left, "Merge error: left.index() < left.prior_size");
×
1666
        }
×
1667
        if (!(right.index() < right.prior_size)) {
80✔
1668
            bad_merge(right_side, right, "Merge error: right.index() < right.prior_size");
×
1669
        }
×
1670

1671
        // CONFLICT: Update of a removed element.
1672
        //
1673
        // RESOLUTION: Discard the UPDATE operation received on the right side.
1674
        right.prior_size -= 1;
80✔
1675

1676
        if (left.index() == right.index()) {
80✔
1677
            // CONFLICT: Update of a removed element.
1678
            //
1679
            // RESOLUTION: Discard the UPDATE operation received on the right side.
1680
            right_side.discard();
20✔
1681
        }
20✔
1682
        else if (right.index() > left.index()) {
60✔
1683
            right.index() -= 1;
24✔
1684
        }
24✔
1685
    }
80✔
1686
}
224✔
1687

1688
// Handled by nested rule
1689
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::Update);
1690
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::Update);
1691
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::Update);
1692

1693

1694
/// AddInteger rules
1695

1696
DEFINE_NESTED_MERGE_NOOP(Instruction::AddInteger);
1697
DEFINE_MERGE_NOOP(Instruction::AddInteger, Instruction::AddInteger);
1698
DEFINE_MERGE_NOOP(Instruction::AddColumn, Instruction::AddInteger);
1699
DEFINE_MERGE_NOOP(Instruction::EraseColumn, Instruction::AddInteger);
1700
DEFINE_MERGE_NOOP(Instruction::ArrayInsert, Instruction::AddInteger);
1701
DEFINE_MERGE_NOOP(Instruction::ArrayMove, Instruction::AddInteger);
1702
DEFINE_MERGE_NOOP(Instruction::ArrayErase, Instruction::AddInteger);
1703
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::AddInteger);
1704
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::AddInteger);
1705
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::AddInteger);
1706

1707

1708
/// AddColumn rules
1709

1710
DEFINE_NESTED_MERGE_NOOP(Instruction::AddColumn);
1711

1712
DEFINE_MERGE(Instruction::AddColumn, Instruction::AddColumn)
1713
{
14,374✔
1714
    if (same_column(left, right)) {
14,374✔
1715
        StringData left_name = left_side.get_string(left.field);
9,350✔
1716
        if (left.type != right.type) {
9,350✔
1717
            bad_merge(
8✔
1718
                "Schema mismatch: Property '%1' in class '%2' is of type %3 on one side and type %4 on the other.",
8✔
1719
                left_name, left_side.get_string(left.table), get_type_name(left.type), get_type_name(right.type));
8✔
1720
        }
8✔
1721

4,644✔
1722
        if (left.nullable != right.nullable) {
9,350✔
1723
            bad_merge("Schema mismatch: Property '%1' in class '%2' is nullable on one side and not on the other.",
14✔
1724
                      left_name, left_side.get_string(left.table));
14✔
1725
        }
14✔
1726

4,644✔
1727
        if (left.collection_type != right.collection_type) {
9,350✔
1728
            auto collection_type_name = [](Instruction::AddColumn::CollectionType type) -> const char* {
×
1729
                switch (type) {
×
1730
                    case Instruction::AddColumn::CollectionType::Single:
×
1731
                        return "single value";
×
1732
                    case Instruction::AddColumn::CollectionType::List:
×
1733
                        return "list";
×
1734
                    case Instruction::AddColumn::CollectionType::Dictionary:
×
1735
                        return "dictionary";
×
1736
                    case Instruction::AddColumn::CollectionType::Set:
×
1737
                        return "set";
×
1738
                }
×
1739
                REALM_TERMINATE("");
1740
            };
×
1741

1742
            std::stringstream ss;
×
1743
            const char* left_type = collection_type_name(left.collection_type);
×
1744
            const char* right_type = collection_type_name(right.collection_type);
×
1745
            bad_merge("Schema mismatch: Property '%1' in class '%2' is a %3 on one side, and a %4 on the other.",
×
1746
                      left_name, left_side.get_string(left.table), left_type, right_type);
×
1747
        }
×
1748

4,644✔
1749
        if (left.type == Instruction::Payload::Type::Link) {
9,350✔
1750
            StringData left_target = left_side.get_string(left.link_target_table);
2,064✔
1751
            StringData right_target = right_side.get_string(right.link_target_table);
2,064✔
1752
            if (left_target != right_target) {
2,064✔
1753
                bad_merge("Schema mismatch: Link property '%1' in class '%2' points to class '%3' on one side and to "
8✔
1754
                          "'%4' on the other.",
8✔
1755
                          left_name, left_side.get_string(left.table), left_target, right_target);
8✔
1756
            }
8✔
1757
        }
2,064✔
1758

4,644✔
1759
        // Name, type, nullability and link targets match -- discard both
4,644✔
1760
        // sides and proceed.
4,644✔
1761
        left_side.discard();
9,350✔
1762
        right_side.discard();
9,350✔
1763
    }
9,350✔
1764
}
14,374✔
1765

1766
DEFINE_MERGE(Instruction::EraseColumn, Instruction::AddColumn)
1767
{
×
1768
    if (same_column(left, right)) {
×
1769
        right_side.discard();
×
1770
    }
×
1771
}
×
1772

1773
DEFINE_MERGE_NOOP(Instruction::ArrayInsert, Instruction::AddColumn);
1774
DEFINE_MERGE_NOOP(Instruction::ArrayMove, Instruction::AddColumn);
1775
DEFINE_MERGE_NOOP(Instruction::ArrayErase, Instruction::AddColumn);
1776
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::AddColumn);
1777
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::AddColumn);
1778
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::AddColumn);
1779

1780

1781
/// EraseColumn rules
1782

1783
DEFINE_NESTED_MERGE_NOOP(Instruction::EraseColumn);
1784

1785
DEFINE_MERGE(Instruction::EraseColumn, Instruction::EraseColumn)
1786
{
×
1787
    if (same_column(left, right)) {
×
1788
        left_side.discard();
×
1789
        right_side.discard();
×
1790
    }
×
1791
}
×
1792

1793
DEFINE_MERGE_NOOP(Instruction::ArrayInsert, Instruction::EraseColumn);
1794
DEFINE_MERGE_NOOP(Instruction::ArrayMove, Instruction::EraseColumn);
1795
DEFINE_MERGE_NOOP(Instruction::ArrayErase, Instruction::EraseColumn);
1796
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::EraseColumn);
1797
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::EraseColumn);
1798
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::EraseColumn);
1799

1800
/// ArrayInsert rules
1801

1802
DEFINE_NESTED_MERGE(Instruction::ArrayInsert)
1803
{
16✔
1804
    if (is_container_prefix_of(outer, inner)) {
16!
1805
        auto& index = corresponding_index_in_path(outer, inner);
16✔
1806
        if (index >= outer.index()) {
16!
1807
            index += 1;
8✔
1808
        }
8✔
1809
    }
16✔
1810
}
16✔
1811

1812
DEFINE_MERGE(Instruction::ArrayInsert, Instruction::ArrayInsert)
1813
{
1,600✔
1814
    if (same_container(left, right)) {
1,600✔
1815
        if (!(left.prior_size == right.prior_size)) {
644✔
1816
            bad_merge(right_side, right, "Exception: left.prior_size == right.prior_size");
×
1817
        }
×
1818
        left.prior_size++;
644✔
1819
        right.prior_size++;
644✔
1820

146✔
1821
        if (left.index() > right.index()) {
644✔
1822
            left.index() += 1; // --->
142✔
1823
        }
142✔
1824
        else if (left.index() < right.index()) {
502✔
1825
            right.index() += 1; // <---
146✔
1826
        }
146✔
1827
        else { // left index == right index
356✔
1828
            // CONFLICT: Two element insertions at the same position.
92✔
1829
            //
92✔
1830
            // Resolution: Place the inserted elements in order of increasing
92✔
1831
            // timestamp. Note that the timestamps can never be equal.
92✔
1832
            if (left_side.timestamp() < right_side.timestamp()) {
356✔
1833
                right.index() += 1;
186✔
1834
            }
186✔
1835
            else {
170✔
1836
                left.index() += 1;
170✔
1837
            }
170✔
1838
        }
356✔
1839
    }
644✔
1840
}
1,600✔
1841

1842
DEFINE_MERGE(Instruction::ArrayMove, Instruction::ArrayInsert)
1843
{
×
1844
    if (same_container(left, right)) {
×
1845
        left.prior_size += 1;
×
1846

1847
        // Left insertion vs right removal
1848
        if (right.index() > left.index()) {
×
1849
            right.index() -= 1; // --->
×
1850
        }
×
1851
        else {
×
1852
            left.index() += 1; // <---
×
1853
        }
×
1854

1855
        // Left insertion vs left insertion
1856
        if (right.index() < left.ndx_2) {
×
1857
            left.ndx_2 += 1; // <---
×
1858
        }
×
1859
        else if (right.index() > left.ndx_2) {
×
1860
            right.index() += 1; // --->
×
1861
        }
×
1862
        else { // right.index() == left.ndx_2
×
1863
            // CONFLICT: Insertion and movement to same position.
1864
            //
1865
            // RESOLUTION: Place the two elements in order of increasing
1866
            // timestamp. Note that the timestamps can never be equal.
1867
            if (left_side.timestamp() < right_side.timestamp()) {
×
1868
                left.ndx_2 += 1; // <---
×
1869
            }
×
1870
            else {
×
1871
                right.index() += 1; // --->
×
1872
            }
×
1873
        }
×
1874
    }
×
1875
}
×
1876

1877
DEFINE_MERGE(Instruction::ArrayErase, Instruction::ArrayInsert)
1878
{
456✔
1879
    if (same_container(left, right)) {
456✔
1880
        if (!(left.prior_size == right.prior_size)) {
192✔
1881
            bad_merge(left_side, left, "Merge error: left.prior_size == right.prior_size");
×
1882
        }
×
1883
        if (!(left.index() < left.prior_size)) {
192✔
1884
            bad_merge(left_side, left, "Merge error: left.index() < left.prior_size");
×
1885
        }
×
1886
        if (!(right.index() <= right.prior_size)) {
192✔
1887
            bad_merge(left_side, left, "Merge error: right.index() <= right.prior_size");
×
1888
        }
×
1889

38✔
1890
        left.prior_size++;
192✔
1891
        right.prior_size--;
192✔
1892
        if (right.index() <= left.index()) {
192✔
1893
            left.index() += 1; // --->
72✔
1894
        }
72✔
1895
        else {
120✔
1896
            right.index() -= 1; // <---
120✔
1897
        }
120✔
1898
    }
192✔
1899
}
456✔
1900

1901
// Handled by nested rules
1902
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::ArrayInsert);
1903
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::ArrayInsert);
1904
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::ArrayInsert);
1905

1906

1907
/// ArrayMove rules
1908

1909
DEFINE_NESTED_MERGE(Instruction::ArrayMove)
1910
{
×
1911
    if (is_container_prefix_of(outer, inner)) {
×
1912
        auto& index = corresponding_index_in_path(outer, inner);
×
1913
        merge_get_vs_move(outer.index(), index, outer.ndx_2);
×
1914
    }
×
1915
}
×
1916

1917
DEFINE_MERGE(Instruction::ArrayMove, Instruction::ArrayMove)
1918
{
28✔
1919
    if (same_container(left, right)) {
28✔
1920
        if (!(left.prior_size == right.prior_size)) {
28✔
1921
            bad_merge(left_side, left, "Merge error: left.prior_size == right.prior_size");
×
1922
        }
×
1923
        if (!(left.index() < left.prior_size)) {
28✔
1924
            bad_merge(left_side, left, "Merge error: left.index() < left.prior_size");
×
1925
        }
×
1926
        if (!(right.index() < right.prior_size)) {
28✔
1927
            bad_merge(right_side, right, "Merge error: right.index() < right.prior_size");
×
1928
        }
×
1929
        if (!(left.ndx_2 < left.prior_size)) {
28✔
1930
            bad_merge(left_side, left, "Merge error: left.ndx_2 < left.prior_size");
×
1931
        }
×
1932
        if (!(right.ndx_2 < right.prior_size)) {
28✔
1933
            bad_merge(right_side, right, "Merge error: right.ndx_2 < right.prior_size");
×
1934
        }
×
1935

14✔
1936
        if (left.index() < right.index()) {
28✔
1937
            right.index() -= 1; // <---
4✔
1938
        }
4✔
1939
        else if (left.index() > right.index()) {
24✔
1940
            left.index() -= 1; // --->
4✔
1941
        }
4✔
1942
        else {
20✔
1943
            // CONFLICT: Two movements of same element.
10✔
1944
            //
10✔
1945
            // RESOLUTION: Respect the MOVE operation associated with the higher
10✔
1946
            // timestamp. If the previously applied MOVE operation has the higher
10✔
1947
            // timestamp, discard the received MOVE operation, otherwise use the
10✔
1948
            // previously applied MOVE operation to transform the received MOVE
10✔
1949
            // operation. Note that the timestamps are never equal.
10✔
1950
            if (left_side.timestamp() < right_side.timestamp()) {
20✔
1951
                right.index() = left.ndx_2; // <---
12✔
1952
                left_side.discard();        // --->
12✔
1953
                if (right.index() == right.ndx_2) {
12✔
1954
                    right_side.discard(); // <---
×
1955
                }
×
1956
            }
12✔
1957
            else {
8✔
1958
                left.index() = right.ndx_2; // --->
8✔
1959
                if (left.index() == left.ndx_2) {
8✔
1960
                    left_side.discard(); // --->
×
1961
                }
×
1962
                right_side.discard(); // <---
8✔
1963
            }
8✔
1964
            return;
20✔
1965
        }
20✔
1966

4✔
1967
        // Left insertion vs right removal
4✔
1968
        if (left.ndx_2 > right.index()) {
8✔
1969
            left.ndx_2 -= 1; // --->
4✔
1970
        }
4✔
1971
        else {
4✔
1972
            right.index() += 1; // <---
4✔
1973
        }
4✔
1974

4✔
1975
        // Left removal vs right insertion
4✔
1976
        if (left.index() < right.ndx_2) {
8✔
1977
            right.ndx_2 -= 1; // <---
4✔
1978
        }
4✔
1979
        else {
4✔
1980
            left.index() += 1; // --->
4✔
1981
        }
4✔
1982

4✔
1983
        // Left insertion vs right insertion
4✔
1984
        if (left.ndx_2 < right.ndx_2) {
8✔
1985
            right.ndx_2 += 1; // <---
4✔
1986
        }
4✔
1987
        else if (left.ndx_2 > right.ndx_2) {
4✔
1988
            left.ndx_2 += 1; // --->
4✔
1989
        }
4✔
1990
        else { // left.ndx_2 == right.ndx_2
×
1991
            // CONFLICT: Two elements moved to the same position
1992
            //
1993
            // RESOLUTION: Place the moved elements in order of increasing
1994
            // timestamp. Note that the timestamps can never be equal.
1995
            if (left_side.timestamp() < right_side.timestamp()) {
×
1996
                right.ndx_2 += 1; // <---
×
1997
            }
×
1998
            else {
×
1999
                left.ndx_2 += 1; // --->
×
2000
            }
×
2001
        }
×
2002

4✔
2003
        if (left.index() == left.ndx_2) {
8✔
2004
            left_side.discard(); // --->
×
2005
        }
×
2006
        if (right.index() == right.ndx_2) {
8✔
2007
            right_side.discard(); // <---
×
2008
        }
×
2009
    }
8✔
2010
}
28✔
2011

2012
DEFINE_MERGE(Instruction::ArrayErase, Instruction::ArrayMove)
2013
{
×
2014
    if (same_container(left, right)) {
×
2015
        if (!(left.prior_size == right.prior_size)) {
×
2016
            bad_merge(left_side, left, "Merge error: left.prior_size == right.prior_size");
×
2017
        }
×
2018
        if (!(left.index() < left.prior_size)) {
×
2019
            bad_merge(left_side, left, "Merge error: left.index() < left.prior_size");
×
2020
        }
×
2021
        if (!(right.index() < right.prior_size)) {
×
2022
            bad_merge(right_side, right, "Merge error: right.index() < right.prior_size");
×
2023
        }
×
2024

2025
        right.prior_size -= 1;
×
2026

2027
        if (left.index() == right.index()) {
×
2028
            // CONFLICT: Removal of a moved element.
2029
            //
2030
            // RESOLUTION: Discard the received MOVE operation on the left side, and
2031
            // use the previously applied MOVE operation to transform the received
2032
            // REMOVE operation on the right side.
2033
            left.index() = right.ndx_2; // --->
×
2034
            right_side.discard();       // <---
×
2035
        }
×
2036
        else {
×
2037
            // Left removal vs right removal
2038
            if (left.index() > right.index()) {
×
2039
                left.index() -= 1; // --->
×
2040
            }
×
2041
            else {                  // left.index() < right.index()
×
2042
                right.index() -= 1; // <---
×
2043
            }
×
2044
            // Left removal vs right insertion
2045
            if (left.index() >= right.ndx_2) {
×
2046
                left.index() += 1; // --->
×
2047
            }
×
2048
            else {
×
2049
                right.ndx_2 -= 1; // <---
×
2050
            }
×
2051

2052
            if (right.index() == right.ndx_2) {
×
2053
                right_side.discard(); // <---
×
2054
            }
×
2055
        }
×
2056
    }
×
2057
}
×
2058

2059
// Handled by nested rule.
2060
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::ArrayMove);
2061
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::ArrayMove);
2062
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::ArrayMove);
2063

2064

2065
/// ArrayErase rules
2066

2067
DEFINE_NESTED_MERGE(Instruction::ArrayErase)
2068
{
8✔
2069
    if (is_prefix_of(outer, inner)) {
8!
2070
        // Erase of subtree - inner instruction touches the subtree.
2071
        inner_side.discard();
×
2072
    }
×
2073
    else if (is_container_prefix_of(outer, inner)) {
8!
2074
        // Erase of a sibling element in the container - adjust the path.
4✔
2075
        auto& index = corresponding_index_in_path(outer, inner);
8✔
2076
        if (outer.index() < index) {
8!
2077
            index -= 1;
8✔
2078
        }
8✔
2079
        else {
×
2080
            REALM_ASSERT(index != outer.index());
×
2081
        }
×
2082
    }
8✔
2083
}
8✔
2084

2085
DEFINE_MERGE(Instruction::ArrayErase, Instruction::ArrayErase)
2086
{
26✔
2087
    if (same_container(left, right)) {
26✔
2088
        if (!(left.prior_size == right.prior_size)) {
24✔
2089
            bad_merge(left_side, left, "Merge error: left.prior_size == right.prior_size");
×
2090
        }
×
2091
        if (!(left.index() < left.prior_size)) {
24✔
2092
            bad_merge(left_side, left, "Merge error: left.index() < left.prior_size");
×
2093
        }
×
2094
        if (!(right.index() < right.prior_size)) {
24✔
2095
            bad_merge(right_side, right, "Merge error: right.index() < right.prior_size");
×
2096
        }
×
2097

2✔
2098
        left.prior_size -= 1;
24✔
2099
        right.prior_size -= 1;
24✔
2100

2✔
2101
        if (left.index() > right.index()) {
24✔
2102
            left.index() -= 1; // --->
6✔
2103
        }
6✔
2104
        else if (left.index() < right.index()) {
18✔
2105
            right.index() -= 1; // <---
10✔
2106
        }
10✔
2107
        else { // left.index() == right.index()
8✔
2108
            // CONFLICT: Two removals of the same element.
2109
            //
2110
            // RESOLUTION: On each side, discard the received REMOVE operation.
2111
            left_side.discard();  // --->
8✔
2112
            right_side.discard(); // <---
8✔
2113
        }
8✔
2114
    }
24✔
2115
}
26✔
2116

2117
// Handled by nested rules.
2118
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::ArrayErase);
2119
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::ArrayErase);
2120
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::ArrayErase);
2121

2122

2123
/// Clear rules
2124

2125
DEFINE_NESTED_MERGE(Instruction::Clear)
2126
{
40✔
2127
    // Note: Clear instructions do not have an index in their path.
20✔
2128
    if (is_prefix_of(outer, inner)) {
40!
2129
        inner_side.discard();
40✔
2130
    }
40✔
2131
}
40✔
2132

2133
DEFINE_MERGE(Instruction::Clear, Instruction::Clear)
2134
{
16✔
2135
    if (same_path(left, right)) {
16✔
2136
        // CONFLICT: Two clears of the same container.
8✔
2137
        //
8✔
2138
        // RESOLUTION: Discard the clear with the lower timestamp. This has the
8✔
2139
        // effect of preserving insertions that came after the clear from the
8✔
2140
        // side that has the higher timestamp.
8✔
2141
        if (left_side.timestamp() < right_side.timestamp()) {
16✔
2142
            left_side.discard();
12✔
2143
        }
12✔
2144
        else {
4✔
2145
            right_side.discard();
4✔
2146
        }
4✔
2147
    }
16✔
2148
}
16✔
2149

2150
DEFINE_MERGE(Instruction::SetInsert, Instruction::Clear)
2151
{
8✔
2152
    if (same_path(left, right)) {
8!
2153
        left_side.discard();
4✔
2154
    }
4✔
2155
}
8✔
2156

2157
DEFINE_MERGE(Instruction::SetErase, Instruction::Clear)
2158
{
8✔
2159
    if (same_path(left, right)) {
8!
2160
        left_side.discard();
4✔
2161
    }
4✔
2162
}
8✔
2163

2164

2165
/// SetInsert rules
2166

2167
DEFINE_NESTED_MERGE_NOOP(Instruction::SetInsert);
2168

2169
DEFINE_MERGE(Instruction::SetInsert, Instruction::SetInsert)
2170
{
156✔
2171
    if (same_path(left, right)) {
156✔
2172
        // CONFLICT: Two inserts into the same set.
76✔
2173
        //
76✔
2174
        // RESOLUTION: If the values are the same, discard the insertion with the lower timestamp. Otherwise,
76✔
2175
        // do nothing.
76✔
2176
        //
76✔
2177
        // NOTE: Set insertion is idempotent. Keeping the instruction with the higher timestamp is necessary
76✔
2178
        // because we want to maintain associativity in the case where intermittent erases (as ordered by
76✔
2179
        // timestamp) arrive at a later point in time.
76✔
2180
        if (same_payload(left.value, right.value)) {
152✔
2181
            if (left_side.timestamp() < right_side.timestamp()) {
24✔
2182
                left_side.discard();
12✔
2183
            }
12✔
2184
            else {
12✔
2185
                right_side.discard();
12✔
2186
            }
12✔
2187
        }
24✔
2188
    }
152✔
2189
}
156✔
2190

2191
DEFINE_MERGE(Instruction::SetErase, Instruction::SetInsert)
2192
{
68✔
2193
    if (same_path(left, right)) {
68✔
2194
        // CONFLICT: Insertion and erase in the same set.
32✔
2195
        //
32✔
2196
        // RESOLUTION: If the inserted/erased values are the same, discard the instruction with the lower
32✔
2197
        // timestamp. Otherwise, do nothing.
32✔
2198
        //
32✔
2199
        // Note: Set insertion and erase are both idempotent. Keeping the instruction with the higher
32✔
2200
        // timestamp is necessary because we want to maintain associativity.
32✔
2201
        if (same_payload(left.value, right.value)) {
68✔
2202
            if (left_side.timestamp() < right_side.timestamp()) {
4✔
2203
                left_side.discard();
4✔
2204
            }
4✔
2205
            else {
×
2206
                right_side.discard();
×
2207
            }
×
2208
        }
4✔
2209
    }
68✔
2210
}
68✔
2211

2212

2213
/// SetErase rules.
2214

2215
DEFINE_NESTED_MERGE_NOOP(Instruction::SetErase);
2216

2217
DEFINE_MERGE(Instruction::SetErase, Instruction::SetErase)
2218
{
×
2219
    if (same_path(left, right)) {
×
2220
        // CONFLICT: Two erases in the same set.
2221
        //
2222
        // RESOLUTION: If the values are the same, discard the instruction with the lower timestamp.
2223
        // Otherwise, do nothing.
2224
        if (left.value == right.value) {
×
2225
            if (left_side.timestamp() < right_side.timestamp()) {
×
2226
                left_side.discard();
×
2227
            }
×
2228
            else {
×
2229
                right_side.discard();
×
2230
            }
×
2231
        }
×
2232
    }
×
2233
}
×
2234

2235

2236
///
2237
/// END OF MERGE RULES!
2238
///
2239

2240
template <class Left, class Right>
2241
void merge_instructions_2(Left& left, Right& right, MajorSide& left_side, MinorSide& right_side)
2242
{
278,184✔
2243
    Merge<Left, Right>::merge(left, right, left_side, right_side);
278,184✔
2244
}
278,184✔
2245

2246
template <class Outer, class Inner, class OuterSide, class InnerSide>
2247
void merge_nested_2(Outer& outer, Inner& inner, OuterSide& outer_side, InnerSide& inner_side)
2248
{
45,050✔
2249
    MergeNested<Outer>::merge(outer, inner, outer_side, inner_side);
45,050✔
2250
}
45,050✔
2251

2252
template <class OuterSide, class InnerSide>
2253
void merge_nested(OuterSide& outer_side, InnerSide& inner_side)
2254
{
45,052✔
2255
    outer_side.get().visit([&](auto& outer) {
45,052✔
2256
        inner_side.get().visit([&](auto& inner) {
45,050✔
2257
            merge_nested_2(outer, inner, outer_side, inner_side);
45,050✔
2258
        });
45,050✔
2259
    });
45,052✔
2260
}
45,052✔
2261

2262
void TransformerImpl::merge_instructions(MajorSide& their_side, MinorSide& our_side)
2263
{
280,658✔
2264
    // FIXME: Find a way to avoid heap-copies of the path.
136,208✔
2265
    Instruction their_before = their_side.get();
280,658✔
2266
    Instruction our_before = our_side.get();
280,658✔
2267

136,208✔
2268
    if (their_side.get().get_if<Instruction::Update>()) {
280,658✔
2269
        REALM_ASSERT(their_side.m_path_len > 2);
14,690✔
2270
    }
14,690✔
2271
    if (our_side.get().get_if<Instruction::Update>()) {
280,658✔
2272
        REALM_ASSERT(our_side.m_path_len > 2);
17,442✔
2273
    }
17,442✔
2274
    if (their_side.get().get_if<Instruction::EraseObject>()) {
280,658✔
2275
        REALM_ASSERT(their_side.m_path_len == 2);
23,312✔
2276
    }
23,312✔
2277
    if (our_side.get().get_if<Instruction::EraseObject>()) {
280,658✔
2278
        REALM_ASSERT(our_side.m_path_len == 2);
24,856✔
2279
    }
24,856✔
2280

136,208✔
2281
    // Update selections on the major side (outer loop) according to events on
136,208✔
2282
    // the minor side (inner loop). The selection may only be impacted if the
136,208✔
2283
    // instruction level is lower (i.e. at a higher point in the hierarchy).
136,208✔
2284
    if (our_side.m_path_len < their_side.m_path_len) {
280,658✔
2285
        merge_nested(our_side, their_side);
17,426✔
2286
        if (their_side.was_discarded)
17,426✔
2287
            return;
1,312✔
2288
    }
263,232✔
2289
    else if (our_side.m_path_len > their_side.m_path_len) {
263,232✔
2290
        merge_nested(their_side, our_side);
27,626✔
2291
        if (our_side.was_discarded)
27,626✔
2292
            return;
1,214✔
2293
    }
278,132✔
2294

135,090✔
2295
    if (!their_side.was_discarded && !our_side.was_discarded) {
278,142✔
2296
        // Even if the instructions were nested, we must still perform a regular
135,086✔
2297
        // merge, because link-related instructions contain information from higher
135,086✔
2298
        // levels (both rows, columns, and tables).
135,086✔
2299
        //
135,086✔
2300
        // FIXME: This condition goes away when dangling links are implemented.
135,086✔
2301
        their_side.get().visit([&](auto& their_instruction) {
278,180✔
2302
            our_side.get().visit([&](auto& our_instruction) {
278,184✔
2303
                merge_instructions_2(their_instruction, our_instruction, their_side, our_side);
278,184✔
2304
            });
278,184✔
2305
        });
278,180✔
2306
    }
278,138✔
2307

135,090✔
2308
    // Note: `left` and/or `right` may be dangling at this point due to
135,090✔
2309
    // discard/prepend. However, if they were not discarded, their iterators are
135,090✔
2310
    // required to point to an instruction of the same type.
135,090✔
2311
    if (!their_side.was_discarded && !their_side.was_replaced) {
278,132✔
2312
        const auto& their_after = their_side.get();
260,578✔
2313
        if (!(their_after == their_before)) {
260,578✔
2314
            their_side.m_changeset->set_dirty(true);
1,162✔
2315
        }
1,162✔
2316
    }
260,578✔
2317

135,090✔
2318
    if (!our_side.was_discarded && !our_side.was_replaced) {
278,132✔
2319
        const auto& our_after = our_side.get();
261,012✔
2320
        if (!(our_after == our_before)) {
261,012✔
2321
            our_side.m_changeset->set_dirty(true);
1,166✔
2322
        }
1,166✔
2323
    }
261,012✔
2324
}
278,132✔
2325

2326
} // anonymous namespace
2327

2328
namespace realm::sync {
2329
void Transformer::merge_changesets(file_ident_type local_file_ident, util::Span<Changeset> their_changesets,
2330
                                   util::Span<Changeset*> our_changesets, util::Logger& logger)
2331
{
44,526✔
2332
    REALM_ASSERT(our_changesets.size() != 0);
44,526✔
2333
    REALM_ASSERT(their_changesets.size() != 0);
44,526✔
2334
    bool trace = false;
44,526✔
2335
#if REALM_DEBUG && !REALM_UWP
44,526✔
2336
    // FIXME: Not thread-safe (use config parameter instead and confine environment reading to test/test_all.cpp)
24,424✔
2337
    const char* trace_p = ::getenv("UNITTEST_TRACE_TRANSFORM");
44,526✔
2338
    trace = (trace_p && StringData{trace_p} != "no");
44,526!
2339
    static std::mutex trace_mutex;
44,526✔
2340
    util::Optional<std::unique_lock<std::mutex>> l;
44,526✔
2341
    if (trace) {
44,526✔
2342
        l = std::unique_lock<std::mutex>{trace_mutex};
×
2343
    }
×
2344
#endif
44,526✔
2345
    TransformerImpl transformer{trace};
44,526✔
2346

24,424✔
2347
    _impl::ChangesetIndex their_index;
44,526✔
2348
    size_t their_num_instructions = 0;
44,526✔
2349
    size_t our_num_instructions = 0;
44,526✔
2350

24,424✔
2351
    // Loop through all instructions on both sides and build conflict groups.
24,424✔
2352
    // This causes the index to merge ranges that are connected by instructions
24,424✔
2353
    // on the left side, but which aren't connected on the right side.
24,424✔
2354
    // FIXME: The conflict groups can be persisted as part of the changeset to
24,424✔
2355
    // skip this step in the future.
24,424✔
2356
    for (size_t i = 0; i < their_changesets.size(); ++i) {
112,310✔
2357
        size_t num_instructions = their_changesets[i].size();
67,784✔
2358
        their_num_instructions += num_instructions;
67,784✔
2359
        logger.trace("Scanning incoming changeset [%1/%2] (%3 instructions)", i + 1, their_changesets.size(),
67,784✔
2360
                     num_instructions);
67,784✔
2361

35,050✔
2362
        their_index.scan_changeset(their_changesets[i]);
67,784✔
2363
    }
67,784✔
2364
    for (size_t i = 0; i < our_changesets.size(); ++i) {
570,590✔
2365
        Changeset& our_changeset = *our_changesets[i];
526,064✔
2366
        size_t num_instructions = our_changeset.size();
526,064✔
2367
        our_num_instructions += num_instructions;
526,064✔
2368
        logger.trace("Scanning local changeset [%1/%2] (%3 instructions)", i + 1, our_changesets.size(),
526,064✔
2369
                     num_instructions);
526,064✔
2370

270,778✔
2371
        their_index.scan_changeset(our_changeset);
526,064✔
2372
    }
526,064✔
2373

24,424✔
2374
    // Build the index.
24,424✔
2375
    for (size_t i = 0; i < their_changesets.size(); ++i) {
112,302✔
2376
        logger.trace("Indexing incoming changeset [%1/%2] (%3 instructions)", i + 1, their_changesets.size(),
67,776✔
2377
                     their_changesets[i].size());
67,776✔
2378
        their_index.add_changeset(their_changesets[i]);
67,776✔
2379
    }
67,776✔
2380

24,424✔
2381
    logger.debug("Finished changeset indexing (incoming: %1 changeset(s) / %2 instructions, local: %3 "
44,526✔
2382
                 "changeset(s) / %4 instructions, conflict group(s): %5)",
44,526✔
2383
                 their_changesets.size(), their_num_instructions, our_changesets.size(), our_num_instructions,
44,526✔
2384
                 their_index.get_num_conflict_groups());
44,526✔
2385

24,424✔
2386
#if REALM_DEBUG // LCOV_EXCL_START
24,424✔
2387
    if (trace) {
44,526✔
2388
        std::cerr << TERM_YELLOW << "\n=> PEER " << std::hex << local_file_ident
×
2389
                  << " merging "
×
2390
                     "changeset(s)/from peer(s):\n";
×
2391
        for (size_t i = 0; i < their_changesets.size(); ++i) {
×
2392
            std::cerr << "Changeset version " << std::dec << their_changesets[i].version << " from peer "
×
2393
                      << their_changesets[i].origin_file_ident << " at timestamp "
×
2394
                      << their_changesets[i].origin_timestamp << "\n";
×
2395
        }
×
2396
        std::cerr << "Transforming through local changeset(s):\n";
×
2397
        for (size_t i = 0; i < our_changesets.size(); ++i) {
×
2398
            std::cerr << "Changeset version " << our_changesets[i]->version << " from peer "
×
2399
                      << our_changesets[i]->origin_file_ident << " at timestamp "
×
2400
                      << our_changesets[i]->origin_timestamp << "\n";
×
2401
        }
×
2402

2403
        for (size_t i = 0; i < our_changesets.size(); ++i) {
×
2404
            std::cerr << TERM_RED << "\nLOCAL (RECIPROCAL) CHANGESET BEFORE MERGE:\n" << TERM_RESET;
×
2405
            our_changesets[i]->print(std::cerr);
×
2406
        }
×
2407

2408
        for (size_t i = 0; i < their_changesets.size(); ++i) {
×
2409
            std::cerr << TERM_RED << "\nINCOMING CHANGESET BEFORE MERGE:\n" << TERM_RESET;
×
2410
            their_changesets[i].print(std::cerr);
×
2411
        }
×
2412

2413
        std::cerr << TERM_MAGENTA << "\nINCOMING CHANGESET INDEX:\n" << TERM_RESET;
×
2414
        their_index.print(std::cerr);
×
2415
        std::cerr << '\n';
×
2416
        their_index.verify();
×
2417

2418
        std::cerr << TERM_YELLOW << std::setw(80) << std::left << "MERGE TRACE (incoming):"
×
2419
                  << "MERGE TRACE (local):\n"
×
2420
                  << TERM_RESET;
×
2421
    }
×
2422
#else
2423
    static_cast<void>(local_file_ident);
2424
#endif // REALM_DEBUG LCOV_EXCL_STOP
2425

24,424✔
2426
    for (size_t i = 0; i < our_changesets.size(); ++i) {
570,624✔
2427
        logger.trace(
526,098✔
2428
            "Transforming local changeset [%1/%2] through %3 incoming changeset(s) with %4 conflict group(s)", i + 1,
526,098✔
2429
            our_changesets.size(), their_changesets.size(), their_index.get_num_conflict_groups());
526,098✔
2430
        Changeset* our_changeset = our_changesets[i];
526,098✔
2431

270,792✔
2432
        transformer.m_major_side.set_next_changeset(our_changeset);
526,098✔
2433
        // MinorSide uses the index to find the Changeset.
270,792✔
2434
        transformer.m_minor_side.m_changeset_index = &their_index;
526,098✔
2435
        transformer.transform(); // Throws
526,098✔
2436
    }
526,098✔
2437

24,424✔
2438
    logger.debug("Finished transforming %1 local changesets through %2 incoming changesets (%3 vs %4 "
44,526✔
2439
                 "instructions, in %5 conflict groups)",
44,526✔
2440
                 our_changesets.size(), their_changesets.size(), our_num_instructions, their_num_instructions,
44,526✔
2441
                 their_index.get_num_conflict_groups());
44,526✔
2442

24,424✔
2443
#if REALM_DEBUG // LCOV_EXCL_START
24,424✔
2444
    // Check that the index is still valid after transformation.
24,424✔
2445
    their_index.verify();
44,526✔
2446
#endif // REALM_DEBUG LCOV_EXCL_STOP
44,526✔
2447

24,424✔
2448
#if REALM_DEBUG // LCOV_EXCL_START
24,424✔
2449
    if (trace) {
44,526✔
2450
        for (size_t i = 0; i < our_changesets.size(); ++i) {
×
2451
            std::cerr << TERM_CYAN << "\nRECIPROCAL CHANGESET AFTER MERGE:\n" << TERM_RESET;
×
2452
            our_changesets[i]->print(std::cerr);
×
2453
            std::cerr << '\n';
×
2454
        }
×
2455
        for (size_t i = 0; i < their_changesets.size(); ++i) {
×
2456
            std::cerr << TERM_CYAN << "INCOMING CHANGESET AFTER MERGE:\n" << TERM_RESET;
×
2457
            their_changesets[i].print(std::cerr);
×
2458
            std::cerr << '\n';
×
2459
        }
×
2460
    }
×
2461
#endif // LCOV_EXCL_STOP REALM_DEBUG
44,526✔
2462
}
44,526✔
2463

2464
size_t Transformer::transform_remote_changesets(TransformHistory& history, file_ident_type local_file_ident,
2465
                                                version_type current_local_version,
2466
                                                util::Span<Changeset> parsed_changesets,
2467
                                                util::FunctionRef<bool(const Changeset*)> changeset_applier,
2468
                                                util::Logger& logger)
2469
{
60,582✔
2470
    REALM_ASSERT(local_file_ident != 0);
60,582✔
2471

31,966✔
2472
    std::vector<Changeset*> our_changesets;
60,582✔
2473

31,966✔
2474
    // p points to the beginning of a range of changesets that share the same
31,966✔
2475
    // "base", i.e. are based on the same local version.
31,966✔
2476
    auto p = parsed_changesets.begin();
60,582✔
2477
    auto parsed_changesets_end = parsed_changesets.end();
60,582✔
2478

31,966✔
2479
    while (p != parsed_changesets_end) {
126,418✔
2480
        // Find the range of incoming changesets that share the same
34,988✔
2481
        // last_integrated_local_version, which means we can merge them in one go.
34,988✔
2482
        auto same_base_range_end = std::find_if(p + 1, parsed_changesets_end, [&](auto& changeset) {
54,832✔
2483
            return p->last_integrated_remote_version != changeset.last_integrated_remote_version;
36,258✔
2484
        });
36,258✔
2485

34,988✔
2486
        version_type begin_version = p->last_integrated_remote_version;
65,836✔
2487
        version_type end_version = current_local_version;
65,836✔
2488
        for (;;) {
591,894✔
2489
            HistoryEntry history_entry;
591,894✔
2490
            version_type version = history.find_history_entry(begin_version, end_version, history_entry);
591,894✔
2491
            if (version == 0)
591,894✔
2492
                break; // No more local changesets
65,838✔
2493

270,784✔
2494
            Changeset& our_changeset = get_reciprocal_transform(history, local_file_ident, version,
526,056✔
2495
                                                                history_entry); // Throws
526,056✔
2496
            our_changesets.push_back(&our_changeset);
526,056✔
2497

270,784✔
2498
            begin_version = version;
526,056✔
2499
        }
526,056✔
2500

34,988✔
2501
        bool must_apply_all = false;
65,836✔
2502

34,988✔
2503
        if (!our_changesets.empty()) {
65,836✔
2504
            merge_changesets(local_file_ident, {&*p, same_base_range_end}, our_changesets, logger); // Throws
44,528✔
2505
            // We need to apply all transformed changesets if at least one reciprocal changeset was modified
24,426✔
2506
            // during OT.
24,426✔
2507
            must_apply_all = std::any_of(our_changesets.begin(), our_changesets.end(), [](const Changeset* c) {
469,090✔
2508
                return c->is_dirty();
469,090✔
2509
            });
469,090✔
2510
        }
44,528✔
2511

34,988✔
2512
        auto continue_applying = true;
65,836✔
2513
        for (; p != same_base_range_end && continue_applying; ++p) {
162,624✔
2514
            // It is safe to stop applying the changesets if:
48,360✔
2515
            //      1. There are no reciprocal changesets
48,360✔
2516
            //      2. No reciprocal changeset was modified
48,360✔
2517
            continue_applying = changeset_applier(p) || must_apply_all;
96,788!
2518
        }
96,788✔
2519
        if (!continue_applying) {
65,836✔
2520
            break;
×
2521
        }
×
2522

34,988✔
2523
        our_changesets.clear(); // deliberately not releasing memory
65,836✔
2524
    }
65,836✔
2525

31,966✔
2526
    // NOTE: Any exception thrown during flushing *MUST* lead to rollback of
31,966✔
2527
    // the current transaction.
31,966✔
2528
    flush_reciprocal_transform_cache(history); // Throws
60,582✔
2529

31,966✔
2530
    return p - parsed_changesets.begin();
60,582✔
2531
}
60,582✔
2532

2533

2534
Changeset& Transformer::get_reciprocal_transform(TransformHistory& history, file_ident_type local_file_ident,
2535
                                                 version_type version, const HistoryEntry& history_entry)
2536
{
526,048✔
2537
    auto& changeset = m_reciprocal_transform_cache[version]; // Throws
526,048✔
2538
    if (changeset.empty()) {
526,048✔
2539
        bool is_compressed = false;
495,884✔
2540
        ChunkedBinaryData data = history.get_reciprocal_transform(version, is_compressed);
495,884✔
2541
        ChunkedBinaryInputStream in{data};
495,884✔
2542
        if (is_compressed) {
495,884✔
2543
            size_t total_size;
48,102✔
2544
            auto decompressed = util::compression::decompress_nonportable_input_stream(in, total_size);
48,102✔
2545
            REALM_ASSERT(decompressed);
48,102✔
2546
            sync::parse_changeset(*decompressed, changeset); // Throws
48,102✔
2547
        }
48,102✔
2548
        else {
447,782✔
2549
            sync::parse_changeset(in, changeset); // Throws
447,782✔
2550
        }
447,782✔
2551

251,986✔
2552
        changeset.version = version;
495,884✔
2553
        changeset.last_integrated_remote_version = history_entry.remote_version;
495,884✔
2554
        changeset.origin_timestamp = history_entry.origin_timestamp;
495,884✔
2555
        file_ident_type origin_file_ident = history_entry.origin_file_ident;
495,884✔
2556
        if (origin_file_ident == 0)
495,884✔
2557
            origin_file_ident = local_file_ident;
280,534✔
2558
        changeset.origin_file_ident = origin_file_ident;
495,884✔
2559
    }
495,884✔
2560
    return changeset;
526,048✔
2561
}
526,048✔
2562

2563

2564
void Transformer::flush_reciprocal_transform_cache(TransformHistory& history)
2565
{
60,518✔
2566
    auto changesets = std::move(m_reciprocal_transform_cache);
60,518✔
2567
    m_reciprocal_transform_cache.clear();
60,518✔
2568
    ChangesetEncoder::Buffer output_buffer;
60,518✔
2569
    for (const auto& [version, changeset] : changesets) {
494,798✔
2570
        if (changeset.is_dirty()) {
494,798✔
2571
            encode_changeset(changeset, output_buffer); // Throws
8,054✔
2572
            BinaryData data{output_buffer.data(), output_buffer.size()};
8,054✔
2573
            history.set_reciprocal_transform(version, data); // Throws
8,054✔
2574
            output_buffer.clear();
8,054✔
2575
        }
8,054✔
2576
    }
494,798✔
2577
}
60,518✔
2578

2579
void parse_remote_changeset(const RemoteChangeset& remote_changeset, Changeset& parsed_changeset)
2580
{
96,852✔
2581
    // origin_file_ident = 0 is currently used to indicate an entry of local
48,380✔
2582
    // origin.
48,380✔
2583
    REALM_ASSERT(remote_changeset.origin_file_ident != 0);
96,852✔
2584
    REALM_ASSERT(remote_changeset.remote_version != 0);
96,852✔
2585

48,380✔
2586
    ChunkedBinaryInputStream remote_in{remote_changeset.data};
96,852✔
2587
    parse_changeset(remote_in, parsed_changeset); // Throws
96,852✔
2588

48,380✔
2589
    parsed_changeset.version = remote_changeset.remote_version;
96,852✔
2590
    parsed_changeset.last_integrated_remote_version = remote_changeset.last_integrated_local_version;
96,852✔
2591
    parsed_changeset.origin_timestamp = remote_changeset.origin_timestamp;
96,852✔
2592
    parsed_changeset.origin_file_ident = remote_changeset.origin_file_ident;
96,852✔
2593
    parsed_changeset.original_changeset_size = remote_changeset.original_changeset_size;
96,852✔
2594
}
96,852✔
2595

2596
} // namespace realm::sync
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

© 2025 Coveralls, Inc