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

realm / realm-core / 2092

02 Mar 2024 12:43AM UTC coverage: 90.936% (+0.02%) from 90.92%
2092

push

Evergreen

web-flow
Use updated curl on evergreen windows hosts (#7409)

93932 of 173116 branches covered (54.26%)

238423 of 262189 relevant lines covered (90.94%)

5915346.18 hits per line

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

62.65
/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
    {
943,330✔
45
    }
943,330✔
46

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

50
    bool operator<(const Discriminant& other) const
51
    {
9,434✔
52
        return timestamp == other.timestamp ? (client_file_ident < other.client_file_ident)
6,062✔
53
                                            : timestamp < other.timestamp;
9,048✔
54
    }
9,434✔
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
    {
90,104✔
81
    }
90,104✔
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,220✔
101
        // Rely on the parser having checked the consistency of the interned strings
12,604✔
102
        return m_changeset->get_string(intern_string);
25,220✔
103
    }
25,220✔
104

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

110
    const Discriminant& timestamp() const
111
    {
18,868✔
112
        return m_discriminant;
18,868✔
113
    }
18,868✔
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
    {
853,100✔
126
        was_discarded = false;
853,100✔
127
        was_replaced = false;
853,100✔
128
        m_path_len = instr.path_length();
853,100✔
129
    }
853,100✔
130
};
131

132
struct MajorSide : Side {
133
    MajorSide(TransformerImpl& transformer)
134
        : Side(transformer)
135
    {
45,052✔
136
    }
45,052✔
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
    {
576,116✔
146
        REALM_ASSERT(position >= m_changeset->begin());
576,116✔
147
        REALM_ASSERT(position != m_changeset->end());
576,116✔
148
        m_position = position;
576,116✔
149
        skip_tombstones();
576,116✔
150
        REALM_ASSERT(position != m_changeset->end());
576,116✔
151

323,324✔
152
        m_discriminant = Discriminant{m_changeset->origin_timestamp, m_changeset->origin_file_ident};
576,116✔
153

323,324✔
154
        Side::init_with_instruction(get());
576,116✔
155
    }
576,116✔
156

157
    void skip_tombstones() noexcept final
158
    {
2,254,846✔
159
        while (m_position != m_changeset->end() && !*m_position) {
2,260,498✔
160
            ++m_position;
5,652✔
161
        }
5,652✔
162
    }
2,254,846✔
163

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

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

177
    size_t get_object_ids_in_current_instruction(_impl::ChangesetIndex::GlobalID* ids, size_t max_ids)
178
    {
523,206✔
179
        return _impl::get_object_ids_in_instruction(*m_changeset, get(), ids, max_ids);
523,206✔
180
    }
523,206✔
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
    {
45,052✔
191
    }
45,052✔
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
    {
576,248✔
206
        return Position{m_conflict_ranges};
576,248✔
207
    }
576,248✔
208

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

214
    void update_changeset_pointer() noexcept
215
    {
1,204,948✔
216
        if (REALM_LIKELY(m_position != end())) {
1,204,948✔
217
            m_changeset = m_position.m_outer->first;
465,344✔
218
        }
465,344✔
219
        else {
739,604✔
220
            m_changeset = nullptr;
739,604✔
221
        }
739,604✔
222
    }
1,204,948✔
223

224
    void skip_tombstones() noexcept final
225
    {
1,361,628✔
226
        if (m_position != end() && *m_position)
1,361,628✔
227
            return;
704,448✔
228
        skip_tombstones_slow();
657,180✔
229
    }
657,180✔
230

231
    REALM_NOINLINE void skip_tombstones_slow() noexcept
232
    {
657,446✔
233
        while (m_position != end() && !*m_position) {
766,054✔
234
            ++m_position;
108,608✔
235
        }
108,608✔
236
        update_changeset_pointer();
657,446✔
237
    }
657,446✔
238

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

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

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

136,490✔
263
        m_discriminant = Discriminant{m_changeset->origin_timestamp, m_changeset->origin_file_ident};
277,254✔
264

136,490✔
265
        Side::init_with_instruction(get());
277,254✔
266
    }
277,254✔
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
    {
45,052✔
561
    }
45,052✔
562

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

306,272✔
568
        while (m_major_side.m_position != m_major_side.m_changeset->end()) {
1,127,996✔
569
            m_major_side.init_with_instruction(m_major_side.m_position);
576,110✔
570

323,326✔
571
            set_conflict_ranges();
576,110✔
572
            m_minor_end = m_minor_side.end();
576,110✔
573
            m_minor_side.m_position = m_minor_side.begin();
576,110✔
574
            transform_major();
576,110✔
575

323,326✔
576
            if (!m_major_side.was_discarded)
576,110✔
577
                // Discarding the instruction moves to the next one.
312,874✔
578
                m_major_side.next_instruction();
556,164✔
579
            m_major_side.skip_tombstones();
576,110✔
580
        }
576,110✔
581
    }
551,886✔
582

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

323,344✔
587
        if (_impl::is_schema_change(instr)) {
576,028✔
588
            ///
25,382✔
589
            /// CONFLICT GROUP: Everything touching that class
25,382✔
590
            ///
25,382✔
591
            auto ranges = index.get_everything();
52,910✔
592
            if (!ranges->empty()) {
52,910✔
593
#if REALM_DEBUG // LCOV_EXCL_START
18,632✔
594
                if (m_trace) {
40,158✔
595
                    std::cerr << TERM_RED << "Conflict group: Everything (due to schema change)\n" << TERM_RESET;
×
596
                }
×
597
#endif // REALM_DEBUG LCOV_EXCL_STOP
40,158✔
598
            }
40,158✔
599
            return ranges;
52,910✔
600
        }
52,910✔
601
        else {
523,118✔
602
            ///
297,962✔
603
            /// CONFLICT GROUP: Everything touching the involved objects,
297,962✔
604
            /// including schema changes.
297,962✔
605
            ///
297,962✔
606
            _impl::ChangesetIndex::GlobalID major_ids[2];
523,118✔
607
            size_t num_major_ids = m_major_side.get_object_ids_in_current_instruction(major_ids, 2);
523,118✔
608
            REALM_ASSERT(num_major_ids <= 2);
523,118✔
609
            REALM_ASSERT(num_major_ids >= 1);
523,118✔
610
#if REALM_DEBUG // LCOV_EXCL_START
297,962✔
611
            if (m_trace) {
523,118✔
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
523,118✔
624
            auto ranges = index.get_modifications_for_object(major_ids[0]);
523,118✔
625
            if (num_major_ids == 2) {
523,118✔
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;
523,118✔
631
        }
523,118✔
632
    }
576,028✔
633

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

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

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

655
    void discard_minor()
656
    {
19,080✔
657
        m_minor_side.was_discarded = true;
19,080✔
658
        m_minor_side.m_position = m_minor_side.m_changeset_index->erase_instruction(m_minor_side.m_position);
19,080✔
659
        m_minor_side.m_changeset->set_dirty(true);
19,080✔
660
        m_minor_side.update_changeset_pointer();
19,080✔
661
    }
19,080✔
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
    {
576,206✔
765
        m_minor_side.skip_tombstones();
576,206✔
766

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

323,420✔
772
        while (m_minor_side.m_position != m_minor_end) {
833,466✔
773
            m_minor_side.init_with_instruction(m_minor_side.m_position);
277,254✔
774

136,490✔
775
#if defined(REALM_DEBUG) // LCOV_EXCL_START Debug tracing
277,254✔
776
            if (m_trace) {
277,254✔
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 {
277,254✔
785
#endif // LCOV_EXCL_STOP REALM_DEBUG
277,254✔
786
                merge_instructions(m_major_side, m_minor_side);
277,254✔
787
#if defined(REALM_DEBUG) // LCOV_EXCL_START
277,254✔
788
            }
277,254✔
789
#endif // LCOV_EXCL_STOP REALM_DEBUG
277,254✔
790

136,490✔
791
            if (m_major_side.was_discarded)
277,254✔
792
                break;
19,994✔
793
            if (!m_minor_side.was_discarded)
257,260✔
794
                // Discarding an instruction moves to the next one.
122,686✔
795
                m_minor_side.next_instruction();
251,440✔
796
            m_minor_side.skip_tombstones();
257,260✔
797
        }
257,260✔
798
    }
576,206✔
799

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

803
void MajorSide::set_next_changeset(Changeset* changeset) noexcept
804
{
551,962✔
805
    m_transformer.set_next_major_changeset(changeset);
551,962✔
806
}
551,962✔
807
void MajorSide::discard()
808
{
19,994✔
809
    m_transformer.discard_major();
19,994✔
810
}
19,994✔
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
{
19,080✔
822
    m_transformer.discard_minor();
19,080✔
823
}
19,080✔
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
{
56✔
836
    throw sync::TransformError{std::move(msg)};
56✔
837
}
56✔
838

839
template <class... Params>
840
REALM_NORETURN void bad_merge(const char* msg, Params&&... params)
841
{
56✔
842
    throw_bad_merge(util::format(msg, std::forward<Params>(params)...));
56✔
843
}
56✔
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
    {
70,432✔
862
    }
70,432✔
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,168✔
870
        // FIXME: Optimize string comparison by building a map of InternString values up front.
48,472✔
871
        return m_left_side.m_changeset->get_string(left) == m_right_side.m_changeset->get_string(right);
94,168✔
872
    }
94,168✔
873

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

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

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

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

935
        bad_merge("Invalid payload type in instruction");
×
936
    }
×
937

938
    bool same_path_element(const Instruction::Path::Element& left,
939
                           const Instruction::Path::Element& right) const noexcept
940
    {
244✔
941
        auto pred = util::overload{
244✔
942
            [](uint32_t lhs, uint32_t rhs) {
160✔
943
                return lhs == rhs;
76✔
944
            },
76✔
945
            [this](InternString lhs, InternString rhs) {
206✔
946
                return same_string(lhs, rhs);
168✔
947
            },
168✔
948
            // FIXME: Paths contain incompatible element types. Should we raise an error here?
122✔
949
            [](InternString, uint32_t) {
122✔
950
                return false;
×
951
            },
×
952
            [](uint32_t, InternString) {
122✔
953
                return false;
×
954
            },
×
955
        };
244✔
956
        return mpark::visit(pred, left, right);
244✔
957
    }
244✔
958

959
    bool same_path(const Instruction::Path& left, const Instruction::Path& right) const noexcept
960
    {
8,982✔
961
        if (left.size() == right.size()) {
8,982✔
962
            for (size_t i = 0; i < left.size(); ++i) {
9,034✔
963
                if (!same_path_element(left[i], right[i])) {
236✔
964
                    return false;
120✔
965
                }
120✔
966
            }
236✔
967
            return true;
8,860✔
968
        }
64✔
969
        return false;
64✔
970
    }
64✔
971

972
    bool same_table(const Instruction::TableInstruction& left,
973
                    const Instruction::TableInstruction& right) const noexcept
974
    {
69,962✔
975
        return same_string(left.table, right.table);
69,962✔
976
    }
69,962✔
977

978
    bool same_object(const Instruction::ObjectInstruction& left,
979
                     const Instruction::ObjectInstruction& right) const noexcept
980
    {
43,788✔
981
        return same_table(left, right) && same_key(left.object, right.object);
43,788✔
982
    }
43,788✔
983

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

1000
    bool same_field(const Instruction::PathInstruction& left,
1001
                    const Instruction::PathInstruction& right) const noexcept
1002
    {
17,068✔
1003
        return same_object(left, right) && same_string(left.field, right.field);
17,068✔
1004
    }
17,068✔
1005

1006
    bool same_path(const Instruction::PathInstruction& left, const Instruction::PathInstruction& right) const noexcept
1007
    {
9,974✔
1008
        return same_field(left, right) && same_path(left.path, right.path);
9,974✔
1009
    }
9,974✔
1010

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

420✔
1021
            for (size_t i = 0; i < left.size() - 1; ++i) {
1,212✔
1022
                if (!same_path_element(left[i], right[i])) {
×
1023
                    return false;
×
1024
                }
×
1025
            }
×
1026
            return true;
1,212✔
1027
        }
24✔
1028
        return false;
24✔
1029
    }
24✔
1030

1031
    bool same_container(const Instruction::PathInstruction& left,
1032
                        const Instruction::PathInstruction& right) const noexcept
1033
    {
4,846✔
1034
        return same_field(left, right) && same_container(left.path, right.path);
4,846✔
1035
    }
4,846✔
1036

1037
    // NOTE: `is_prefix_of()` should only return true if the left is a strictly
1038
    // shorter path than the right, and the entire left path is the initial
1039
    // sequence of the right.
1040

1041
    bool is_prefix_of(const Instruction::AddTable& left, const Instruction::TableInstruction& right) const noexcept
1042
    {
×
1043
        return same_table(left, right);
×
1044
    }
×
1045

1046
    bool is_prefix_of(const Instruction::EraseTable& left, const Instruction::TableInstruction& right) const noexcept
1047
    {
7,312✔
1048
        return same_table(left, right);
7,312✔
1049
    }
7,312✔
1050

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

1057
    bool is_prefix_of(const Instruction::EraseColumn&, const Instruction::TableInstruction&) const noexcept
1058
    {
×
1059
        // Right side is a schema instruction.
×
1060
        return false;
×
1061
    }
×
1062

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

1068
    bool is_prefix_of(const Instruction::EraseColumn& left,
1069
                      const Instruction::ObjectInstruction& right) const noexcept
1070
    {
×
1071
        return same_column(left, right);
×
1072
    }
×
1073

1074
    bool is_prefix_of(const Instruction::ObjectInstruction&, const Instruction::TableInstruction&) const noexcept
1075
    {
×
1076
        // Right side is a schema instruction.
1077
        return false;
×
1078
    }
×
1079

1080
    bool is_prefix_of(const Instruction::ObjectInstruction& left,
1081
                      const Instruction::PathInstruction& right) const noexcept
1082
    {
7,378✔
1083
        return same_object(left, right);
7,378✔
1084
    }
7,378✔
1085

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

1093
    bool is_prefix_of(const Instruction::PathInstruction& left,
1094
                      const Instruction::PathInstruction& right) const noexcept
1095
    {
2,224✔
1096
        if (left.path.size() < right.path.size() && same_field(left, right)) {
2,224✔
1097
            for (size_t i = 0; i < left.path.size(); ++i) {
128✔
1098
                if (!same_path_element(left.path[i], right.path[i])) {
8✔
1099
                    return false;
8✔
1100
                }
8✔
1101
            }
8✔
1102
            return true;
124✔
1103
        }
2,096✔
1104
        return false;
2,096✔
1105
    }
2,096✔
1106

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

1124
    bool is_container_prefix_of(const Instruction::PathInstruction&, const Instruction::TableInstruction&) const
1125
    {
×
1126
        return false;
×
1127
    }
×
1128

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

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

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

1177
protected:
1178
    Side& m_left_side;
1179
    Side& m_right_side;
1180
};
1181

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

1188
    MergeBase(Side& left_side, Side& right_side)
1189
        : MergeUtils(left_side, right_side)
1190
    {
53,438✔
1191
    }
53,438✔
1192
    MergeBase(MergeBase&&) = delete;
1193
};
1194

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

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

1237

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

1267
#define DEFINE_NESTED_MERGE_NOOP(A)                                                                                  \
1268
    template <>                                                                                                      \
1269
    struct MergeNested<A> {                                                                                          \
1270
        template <class B, class OuterSide, class InnerSide>                                                         \
1271
        static inline void merge(const A&, const B&, const OuterSide&, const InnerSide&)                             \
1272
        { /* Do nothing */                                                                                           \
32,480✔
1273
        }                                                                                                            \
32,480✔
1274
    }
1275

1276
// Implementation that reverses order.
1277
template <class A, class B>
1278
struct Merge<A, B,
1279
             typename std::enable_if<(Instruction::GetInstructionType<A>::value <
1280
                                      Instruction::GetInstructionType<B>::value)>::type> {
1281
    template <class LeftSide, class RightSide>
1282
    static void merge(A& left, B& right, LeftSide& left_side, RightSide& right_side)
1283
    {
37,040✔
1284
        Merge<B, A>::merge(right, left, right_side, left_side);
37,040✔
1285
    }
37,040✔
1286
};
1287

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

1307

1308
/// AddTable rules
1309

1310
DEFINE_NESTED_MERGE_NOOP(Instruction::AddTable);
1311

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

1,934✔
1326
                if (left_spec->pk_type != right_spec->pk_type) {
3,856✔
1327
                    bad_merge("Schema mismatch: '%1' has primary key '%2', which is of type %3 on one side and type "
8✔
1328
                              "%4 on the other.",
8✔
1329
                              left_name, left_pk_name, get_type_name(left_spec->pk_type),
8✔
1330
                              get_type_name(right_spec->pk_type));
8✔
1331
                }
8✔
1332

1,934✔
1333
                if (left_spec->pk_nullable != right_spec->pk_nullable) {
3,856✔
1334
                    bad_merge("Schema mismatch: '%1' has primary key '%2', which is nullable on one side, but not "
6✔
1335
                              "the other.",
6✔
1336
                              left_name, left_pk_name);
6✔
1337
                }
6✔
1338

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

2,014✔
1353
        // Names are the same, PK presence is the same, and if there is a primary
2,014✔
1354
        // key, its name, type, and nullability are the same. Discard both sides.
2,014✔
1355
        left_side.discard();
4,016✔
1356
        right_side.discard();
4,016✔
1357
        return;
4,016✔
1358
    }
4,016✔
1359
}
4,452✔
1360

1361
DEFINE_MERGE(Instruction::EraseTable, Instruction::AddTable)
1362
{
316✔
1363
    if (same_table(left, right)) {
316✔
1364
        right_side.discard();
×
1365
    }
×
1366
}
316✔
1367

1368
DEFINE_MERGE_NOOP(Instruction::CreateObject, Instruction::AddTable);
1369
DEFINE_MERGE_NOOP(Instruction::EraseObject, Instruction::AddTable);
1370
DEFINE_MERGE_NOOP(Instruction::Update, Instruction::AddTable);
1371
DEFINE_MERGE_NOOP(Instruction::AddInteger, Instruction::AddTable);
1372
DEFINE_MERGE_NOOP(Instruction::AddColumn, Instruction::AddTable);
1373
DEFINE_MERGE_NOOP(Instruction::EraseColumn, Instruction::AddTable);
1374
DEFINE_MERGE_NOOP(Instruction::ArrayInsert, Instruction::AddTable);
1375
DEFINE_MERGE_NOOP(Instruction::ArrayMove, Instruction::AddTable);
1376
DEFINE_MERGE_NOOP(Instruction::ArrayErase, Instruction::AddTable);
1377
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::AddTable);
1378
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::AddTable);
1379
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::AddTable);
1380

1381
/// EraseTable rules
1382

1383
DEFINE_NESTED_MERGE(Instruction::EraseTable)
1384
{
7,312✔
1385
    if (is_prefix_of(outer, inner)) {
7,312!
1386
        inner_side.discard();
1,636✔
1387
    }
1,636✔
1388
}
7,312✔
1389

1390
DEFINE_MERGE(Instruction::EraseTable, Instruction::EraseTable)
1391
{
88✔
1392
    if (same_table(left, right)) {
88✔
1393
        left_side.discard();
4✔
1394
        right_side.discard();
4✔
1395
    }
4✔
1396
}
88✔
1397

1398
// Handled by nesting rule.
1399
DEFINE_MERGE_NOOP(Instruction::CreateObject, Instruction::EraseTable);
1400
DEFINE_MERGE_NOOP(Instruction::EraseObject, Instruction::EraseTable);
1401
DEFINE_MERGE_NOOP(Instruction::Update, Instruction::EraseTable);
1402
DEFINE_MERGE_NOOP(Instruction::AddInteger, Instruction::EraseTable);
1403

1404
DEFINE_MERGE(Instruction::AddColumn, Instruction::EraseTable)
1405
{
412✔
1406
    // AddColumn on an erased table handled by nesting.
172✔
1407

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

1418
// Handled by nested rule.
1419
DEFINE_MERGE_NOOP(Instruction::EraseColumn, Instruction::EraseTable);
1420
DEFINE_MERGE_NOOP(Instruction::ArrayInsert, Instruction::EraseTable);
1421
DEFINE_MERGE_NOOP(Instruction::ArrayMove, Instruction::EraseTable);
1422
DEFINE_MERGE_NOOP(Instruction::ArrayErase, Instruction::EraseTable);
1423
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::EraseTable);
1424
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::EraseTable);
1425
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::EraseTable);
1426

1427

1428
/// CreateObject rules
1429

1430
// CreateObject cannot interfere with instructions that have a longer path.
1431
DEFINE_NESTED_MERGE_NOOP(Instruction::CreateObject);
1432

1433
// CreateObject is idempotent.
1434
DEFINE_MERGE_NOOP(Instruction::CreateObject, Instruction::CreateObject);
1435

1436
DEFINE_MERGE(Instruction::EraseObject, Instruction::CreateObject)
1437
{
14,740✔
1438
    if (same_object(left, right)) {
14,740✔
1439
        // CONFLICT: Create and Erase of the same object.
366✔
1440
        //
366✔
1441
        // RESOLUTION: Erase always wins.
366✔
1442
        right_side.discard();
694✔
1443
    }
694✔
1444
}
14,740✔
1445

1446
DEFINE_MERGE_NOOP(Instruction::Update, Instruction::CreateObject);
1447
DEFINE_MERGE_NOOP(Instruction::AddInteger, Instruction::CreateObject);
1448
DEFINE_MERGE_NOOP(Instruction::AddColumn, Instruction::CreateObject);
1449
DEFINE_MERGE_NOOP(Instruction::EraseColumn, Instruction::CreateObject);
1450
DEFINE_MERGE_NOOP(Instruction::ArrayInsert, Instruction::CreateObject);
1451
DEFINE_MERGE_NOOP(Instruction::ArrayMove, Instruction::CreateObject);
1452
DEFINE_MERGE_NOOP(Instruction::ArrayErase, Instruction::CreateObject);
1453
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::CreateObject);
1454
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::CreateObject);
1455
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::CreateObject);
1456

1457

1458
/// Erase rules
1459

1460
DEFINE_NESTED_MERGE(Instruction::EraseObject)
1461
{
7,378✔
1462
    if (is_prefix_of(outer, inner)) {
7,378!
1463
        // Erase always wins.
510✔
1464
        inner_side.discard();
1,090✔
1465
    }
1,090✔
1466
}
7,378✔
1467

1468
DEFINE_MERGE(Instruction::EraseObject, Instruction::EraseObject)
1469
{
4,602✔
1470
    if (same_object(left, right)) {
4,602✔
1471
        // We keep the most recent erase. This prevents the situation where a
300✔
1472
        // high number of EraseObject instructions in the past trumps a
300✔
1473
        // Erase-Create pair in the future.
300✔
1474
        if (right_side.timestamp() < left_side.timestamp()) {
584✔
1475
            right_side.discard();
292✔
1476
        }
292✔
1477
        else {
292✔
1478
            left_side.discard();
292✔
1479
        }
292✔
1480
    }
584✔
1481
}
4,602✔
1482

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

1495

1496
/// Set rules
1497

1498
DEFINE_NESTED_MERGE(Instruction::Update)
1499
{
2,240✔
1500
    using Type = Instruction::Payload::Type;
2,240✔
1501

1,102✔
1502
    if (outer.value.type == Type::ObjectValue || outer.value.type == Type::Dictionary) {
2,240!
1503
        // Creating an embedded object or a dictionary is an idempotent
32✔
1504
        // operation, and should not eliminate updates to the subtree.
32✔
1505
        return;
64✔
1506
    }
64✔
1507

1,070✔
1508
    // Setting a value higher up in the hierarchy overwrites any modification to
1,070✔
1509
    // the inner value, regardless of when this happened.
1,070✔
1510
    if (is_prefix_of(outer, inner)) {
2,176!
1511
        inner_side.discard();
80✔
1512
    }
80✔
1513
}
2,176✔
1514

1515
DEFINE_MERGE(Instruction::Update, Instruction::Update)
1516
{
9,312✔
1517
    // The two instructions are at the same level of nesting.
5,632✔
1518

5,632✔
1519
    using Type = Instruction::Payload::Type;
9,312✔
1520

5,632✔
1521
    if (same_path(left, right)) {
9,312✔
1522
        bool left_is_default = false;
8,266✔
1523
        bool right_is_default = false;
8,266✔
1524
        if (!(left.is_array_update() == right.is_array_update())) {
8,266✔
1525
            bad_merge(left_side, left, "Merge error: left.is_array_update() == right.is_array_update()");
×
1526
        }
×
1527

5,118✔
1528
        if (!left.is_array_update()) {
8,266✔
1529
            if (right.is_array_update()) {
8,246✔
1530
                bad_merge(right_side, right, "Merge error: !right.is_array_update()");
×
1531
            }
×
1532
            left_is_default = left.is_default;
8,246✔
1533
            right_is_default = right.is_default;
8,246✔
1534
        }
8,246✔
1535
        else if (!(left.prior_size == right.prior_size)) {
20✔
1536
            bad_merge(left_side, left, "Merge error: left.prior_size == right.prior_size");
×
1537
        }
×
1538

5,118✔
1539
        if (left.value.type != right.value.type) {
8,266✔
1540
            // Embedded object / dictionary creation should always lose to an
52✔
1541
            // Update(value), because these structures are nested, and we need to
52✔
1542
            // discard any update inside the structure.
52✔
1543
            if (left.value.type == Type::Dictionary || left.value.type == Type::ObjectValue) {
100✔
1544
                left_side.discard();
24✔
1545
                return;
24✔
1546
            }
24✔
1547
            else if (right.value.type == Type::Dictionary || right.value.type == Type::ObjectValue) {
76✔
1548
                right_side.discard();
24✔
1549
                return;
24✔
1550
            }
24✔
1551
        }
8,218✔
1552

5,094✔
1553
        // CONFLICT: Two updates of the same element.
5,094✔
1554
        //
5,094✔
1555
        // RESOLUTION: Suppress the effect of the UPDATE operation with the lower
5,094✔
1556
        // timestamp. Note that the timestamps can never be equal. This is
5,094✔
1557
        // achieved on both sides by discarding the received UPDATE operation if
5,094✔
1558
        // it has a lower timestamp than the previously applied UPDATE operation.
5,094✔
1559
        if (left_is_default == right_is_default) {
8,218✔
1560
            if (left_side.timestamp() < right_side.timestamp()) {
8,140✔
1561
                left_side.discard(); // --->
4,450✔
1562
            }
4,450✔
1563
            else {
3,690✔
1564
                right_side.discard(); // <---
3,690✔
1565
            }
3,690✔
1566
        }
8,140✔
1567
        else {
78✔
1568
            if (left_is_default) {
78✔
1569
                left_side.discard();
38✔
1570
            }
38✔
1571
            else {
40✔
1572
                right_side.discard();
40✔
1573
            }
40✔
1574
        }
78✔
1575
    }
8,218✔
1576
}
9,312✔
1577

1578
DEFINE_MERGE(Instruction::AddInteger, Instruction::Update)
1579
{
410✔
1580
    // The two instructions are at the same level of nesting.
210✔
1581

210✔
1582
    if (same_path(left, right)) {
410✔
1583
        // CONFLICT: Add vs Set of the same element.
148✔
1584
        //
148✔
1585
        // RESOLUTION: If the Add was later than the Set, add its value to
148✔
1586
        // the payload of the Set instruction. Otherwise, discard it.
148✔
1587

148✔
1588
        if (!(right.value.type == Instruction::Payload::Type::Int || right.value.is_null())) {
292✔
1589
            bad_merge(right_side, right,
×
1590
                      "Merge error: right.value.type == Instruction::Payload::Type::Int || right.value.is_null()");
×
1591
        }
×
1592

148✔
1593
        bool right_is_default = !right.is_array_update() && right.is_default;
292✔
1594

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

112✔
1604
            // Wrapping add
112✔
1605
            uint64_t ua = uint64_t(right.value.data.integer);
220✔
1606
            uint64_t ub = uint64_t(left.value);
220✔
1607
            right.value.data.integer = int64_t(ua + ub);
220✔
1608
        }
220✔
1609
        else {
16✔
1610
            left_side.discard();
16✔
1611
        }
16✔
1612
    }
292✔
1613
}
410✔
1614

1615
DEFINE_MERGE_NOOP(Instruction::AddColumn, Instruction::Update);
1616

1617
DEFINE_MERGE(Instruction::EraseColumn, Instruction::Update)
1618
{
×
1619
    if (same_column(left, right)) {
×
1620
        right_side.discard();
×
1621
    }
×
1622
}
×
1623

1624
DEFINE_MERGE(Instruction::ArrayInsert, Instruction::Update)
1625
{
2,192✔
1626
    if (same_container(left, right)) {
2,192✔
1627
        REALM_ASSERT(right.is_array_update());
308✔
1628
        if (!(left.prior_size == right.prior_size)) {
308✔
1629
            bad_merge(left_side, left, "Merge error: left.prior_size == right.prior_size");
×
1630
        }
×
1631
        if (!(left.index() <= left.prior_size)) {
308✔
1632
            bad_merge(left_side, left, "Merge error: left.index() <= left.prior_size");
×
1633
        }
×
1634
        if (!(right.index() < right.prior_size)) {
308✔
1635
            bad_merge(right_side, right, "Merge error: right.index() < right.prior_size");
×
1636
        }
×
1637
        right.prior_size += 1;
308✔
1638
        if (right.index() >= left.index()) {
308✔
1639
            right.index() += 1; // --->
100✔
1640
        }
100✔
1641
    }
308✔
1642
}
2,192✔
1643

1644
DEFINE_MERGE(Instruction::ArrayMove, Instruction::Update)
1645
{
24✔
1646
    if (same_container(left, right)) {
24✔
1647
        REALM_ASSERT(right.is_array_update());
×
1648

1649
        if (!(left.index() < left.prior_size)) {
×
1650
            bad_merge(left_side, left, "Merge error: left.index() < left.prior_size");
×
1651
        }
×
1652
        if (!(right.index() < right.prior_size)) {
×
1653
            bad_merge(right_side, right, "Merge error: right.index() < right.prior_size");
×
1654
        }
×
1655

1656
        // FIXME: This marks both sides as dirty, even when they are unmodified.
1657
        merge_get_vs_move(right.index(), left.index(), left.ndx_2);
×
1658
    }
×
1659
}
24✔
1660

1661
DEFINE_MERGE(Instruction::ArrayErase, Instruction::Update)
1662
{
416✔
1663
    if (same_container(left, right)) {
416✔
1664
        REALM_ASSERT(right.is_array_update());
52✔
1665
        if (!(left.prior_size == right.prior_size)) {
52✔
1666
            bad_merge(left_side, left, "Merge error: left.prior_size == right.prior_size");
×
1667
        }
×
1668
        if (!(left.index() < left.prior_size)) {
52✔
1669
            bad_merge(left_side, left, "Merge error: left.index() < left.prior_size");
×
1670
        }
×
1671
        if (!(right.index() < right.prior_size)) {
52✔
1672
            bad_merge(right_side, right, "Merge error: right.index() < right.prior_size");
×
1673
        }
×
1674

1675
        // CONFLICT: Update of a removed element.
1676
        //
1677
        // RESOLUTION: Discard the UPDATE operation received on the right side.
1678
        right.prior_size -= 1;
52✔
1679

1680
        if (left.index() == right.index()) {
52✔
1681
            // CONFLICT: Update of a removed element.
1682
            //
1683
            // RESOLUTION: Discard the UPDATE operation received on the right side.
1684
            right_side.discard();
×
1685
        }
×
1686
        else if (right.index() > left.index()) {
52✔
1687
            right.index() -= 1;
8✔
1688
        }
8✔
1689
    }
52✔
1690
}
416✔
1691

1692
// Handled by nested rule
1693
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::Update);
1694
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::Update);
1695
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::Update);
1696

1697

1698
/// AddInteger rules
1699

1700
DEFINE_NESTED_MERGE_NOOP(Instruction::AddInteger);
1701
DEFINE_MERGE_NOOP(Instruction::AddInteger, Instruction::AddInteger);
1702
DEFINE_MERGE_NOOP(Instruction::AddColumn, Instruction::AddInteger);
1703
DEFINE_MERGE_NOOP(Instruction::EraseColumn, Instruction::AddInteger);
1704
DEFINE_MERGE_NOOP(Instruction::ArrayInsert, Instruction::AddInteger);
1705
DEFINE_MERGE_NOOP(Instruction::ArrayMove, Instruction::AddInteger);
1706
DEFINE_MERGE_NOOP(Instruction::ArrayErase, Instruction::AddInteger);
1707
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::AddInteger);
1708
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::AddInteger);
1709
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::AddInteger);
1710

1711

1712
/// AddColumn rules
1713

1714
DEFINE_NESTED_MERGE_NOOP(Instruction::AddColumn);
1715

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

4,642✔
1726
        if (left.nullable != right.nullable) {
9,332✔
1727
            bad_merge("Schema mismatch: Property '%1' in class '%2' is nullable on one side and not on the other.",
16✔
1728
                      left_name, left_side.get_string(left.table));
16✔
1729
        }
16✔
1730

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

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

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

4,642✔
1763
        // Name, type, nullability and link targets match -- discard both
4,642✔
1764
        // sides and proceed.
4,642✔
1765
        left_side.discard();
9,332✔
1766
        right_side.discard();
9,332✔
1767
    }
9,332✔
1768
}
14,008✔
1769

1770
DEFINE_MERGE(Instruction::EraseColumn, Instruction::AddColumn)
1771
{
×
1772
    if (same_column(left, right)) {
×
1773
        right_side.discard();
×
1774
    }
×
1775
}
×
1776

1777
DEFINE_MERGE_NOOP(Instruction::ArrayInsert, Instruction::AddColumn);
1778
DEFINE_MERGE_NOOP(Instruction::ArrayMove, Instruction::AddColumn);
1779
DEFINE_MERGE_NOOP(Instruction::ArrayErase, Instruction::AddColumn);
1780
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::AddColumn);
1781
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::AddColumn);
1782
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::AddColumn);
1783

1784

1785
/// EraseColumn rules
1786

1787
DEFINE_NESTED_MERGE_NOOP(Instruction::EraseColumn);
1788

1789
DEFINE_MERGE(Instruction::EraseColumn, Instruction::EraseColumn)
1790
{
×
1791
    if (same_column(left, right)) {
×
1792
        left_side.discard();
×
1793
        right_side.discard();
×
1794
    }
×
1795
}
×
1796

1797
DEFINE_MERGE_NOOP(Instruction::ArrayInsert, Instruction::EraseColumn);
1798
DEFINE_MERGE_NOOP(Instruction::ArrayMove, Instruction::EraseColumn);
1799
DEFINE_MERGE_NOOP(Instruction::ArrayErase, Instruction::EraseColumn);
1800
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::EraseColumn);
1801
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::EraseColumn);
1802
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::EraseColumn);
1803

1804
/// ArrayInsert rules
1805

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

1816
DEFINE_MERGE(Instruction::ArrayInsert, Instruction::ArrayInsert)
1817
{
1,554✔
1818
    if (same_container(left, right)) {
1,554✔
1819
        if (!(left.prior_size == right.prior_size)) {
628✔
1820
            bad_merge(right_side, right, "Exception: left.prior_size == right.prior_size");
×
1821
        }
×
1822
        left.prior_size++;
628✔
1823
        right.prior_size++;
628✔
1824

282✔
1825
        if (left.index() > right.index()) {
628✔
1826
            left.index() += 1; // --->
134✔
1827
        }
134✔
1828
        else if (left.index() < right.index()) {
494✔
1829
            right.index() += 1; // <---
138✔
1830
        }
138✔
1831
        else { // left index == right index
356✔
1832
            // CONFLICT: Two element insertions at the same position.
144✔
1833
            //
144✔
1834
            // Resolution: Place the inserted elements in order of increasing
144✔
1835
            // timestamp. Note that the timestamps can never be equal.
144✔
1836
            if (left_side.timestamp() < right_side.timestamp()) {
356✔
1837
                right.index() += 1;
186✔
1838
            }
186✔
1839
            else {
170✔
1840
                left.index() += 1;
170✔
1841
            }
170✔
1842
        }
356✔
1843
    }
628✔
1844
}
1,554✔
1845

1846
DEFINE_MERGE(Instruction::ArrayMove, Instruction::ArrayInsert)
1847
{
×
1848
    if (same_container(left, right)) {
×
1849
        left.prior_size += 1;
×
1850

1851
        // Left insertion vs right removal
1852
        if (right.index() > left.index()) {
×
1853
            right.index() -= 1; // --->
×
1854
        }
×
1855
        else {
×
1856
            left.index() += 1; // <---
×
1857
        }
×
1858

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

1881
DEFINE_MERGE(Instruction::ArrayErase, Instruction::ArrayInsert)
1882
{
572✔
1883
    if (same_container(left, right)) {
572✔
1884
        if (!(left.prior_size == right.prior_size)) {
184✔
1885
            bad_merge(left_side, left, "Merge error: left.prior_size == right.prior_size");
×
1886
        }
×
1887
        if (!(left.index() < left.prior_size)) {
184✔
1888
            bad_merge(left_side, left, "Merge error: left.index() < left.prior_size");
×
1889
        }
×
1890
        if (!(right.index() <= right.prior_size)) {
184✔
1891
            bad_merge(left_side, left, "Merge error: right.index() <= right.prior_size");
×
1892
        }
×
1893

42✔
1894
        left.prior_size++;
184✔
1895
        right.prior_size--;
184✔
1896
        if (right.index() <= left.index()) {
184✔
1897
            left.index() += 1; // --->
88✔
1898
        }
88✔
1899
        else {
96✔
1900
            right.index() -= 1; // <---
96✔
1901
        }
96✔
1902
    }
184✔
1903
}
572✔
1904

1905
// Handled by nested rules
1906
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::ArrayInsert);
1907
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::ArrayInsert);
1908
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::ArrayInsert);
1909

1910

1911
/// ArrayMove rules
1912

1913
DEFINE_NESTED_MERGE(Instruction::ArrayMove)
1914
{
×
1915
    if (is_container_prefix_of(outer, inner)) {
×
1916
        auto& index = corresponding_index_in_path(outer, inner);
×
1917
        merge_get_vs_move(outer.index(), index, outer.ndx_2);
×
1918
    }
×
1919
}
×
1920

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

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

4✔
1971
        // Left insertion vs right removal
4✔
1972
        if (left.ndx_2 > right.index()) {
8✔
1973
            left.ndx_2 -= 1; // --->
4✔
1974
        }
4✔
1975
        else {
4✔
1976
            right.index() += 1; // <---
4✔
1977
        }
4✔
1978

4✔
1979
        // Left removal vs right insertion
4✔
1980
        if (left.index() < right.ndx_2) {
8✔
1981
            right.ndx_2 -= 1; // <---
4✔
1982
        }
4✔
1983
        else {
4✔
1984
            left.index() += 1; // --->
4✔
1985
        }
4✔
1986

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

4✔
2007
        if (left.index() == left.ndx_2) {
8✔
2008
            left_side.discard(); // --->
×
2009
        }
×
2010
        if (right.index() == right.ndx_2) {
8✔
2011
            right_side.discard(); // <---
×
2012
        }
×
2013
    }
8✔
2014
}
28✔
2015

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

2029
        right.prior_size -= 1;
×
2030

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

2056
            if (right.index() == right.ndx_2) {
×
2057
                right_side.discard(); // <---
×
2058
            }
×
2059
        }
×
2060
    }
×
2061
}
×
2062

2063
// Handled by nested rule.
2064
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::ArrayMove);
2065
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::ArrayMove);
2066
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::ArrayMove);
2067

2068

2069
/// ArrayErase rules
2070

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

2089
DEFINE_MERGE(Instruction::ArrayErase, Instruction::ArrayErase)
2090
{
60✔
2091
    if (same_container(left, right)) {
60✔
2092
        if (!(left.prior_size == right.prior_size)) {
12✔
2093
            bad_merge(left_side, left, "Merge error: left.prior_size == right.prior_size");
×
2094
        }
×
2095
        if (!(left.index() < left.prior_size)) {
12✔
2096
            bad_merge(left_side, left, "Merge error: left.index() < left.prior_size");
×
2097
        }
×
2098
        if (!(right.index() < right.prior_size)) {
12✔
2099
            bad_merge(right_side, right, "Merge error: right.index() < right.prior_size");
×
2100
        }
×
2101

2✔
2102
        left.prior_size -= 1;
12✔
2103
        right.prior_size -= 1;
12✔
2104

2✔
2105
        if (left.index() > right.index()) {
12✔
2106
            left.index() -= 1; // --->
2✔
2107
        }
2✔
2108
        else if (left.index() < right.index()) {
10✔
2109
            right.index() -= 1; // <---
6✔
2110
        }
6✔
2111
        else { // left.index() == right.index()
4✔
2112
            // CONFLICT: Two removals of the same element.
2113
            //
2114
            // RESOLUTION: On each side, discard the received REMOVE operation.
2115
            left_side.discard();  // --->
4✔
2116
            right_side.discard(); // <---
4✔
2117
        }
4✔
2118
    }
12✔
2119
}
60✔
2120

2121
// Handled by nested rules.
2122
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::ArrayErase);
2123
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::ArrayErase);
2124
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::ArrayErase);
2125

2126

2127
/// Clear rules
2128

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

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

2154
DEFINE_MERGE(Instruction::SetInsert, Instruction::Clear)
2155
{
8✔
2156
    if (same_path(left, right)) {
8!
2157
        left_side.discard();
4✔
2158
    }
4✔
2159
}
8✔
2160

2161
DEFINE_MERGE(Instruction::SetErase, Instruction::Clear)
2162
{
8✔
2163
    if (same_path(left, right)) {
8!
2164
        left_side.discard();
4✔
2165
    }
4✔
2166
}
8✔
2167

2168

2169
/// SetInsert rules
2170

2171
DEFINE_NESTED_MERGE_NOOP(Instruction::SetInsert);
2172

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

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

2216

2217
/// SetErase rules.
2218

2219
DEFINE_NESTED_MERGE_NOOP(Instruction::SetErase);
2220

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

2239

2240
///
2241
/// END OF MERGE RULES!
2242
///
2243

2244
template <class Left, class Right>
2245
void merge_instructions_2(Left& left, Right& right, MajorSide& left_side, MinorSide& right_side)
2246
{
274,426✔
2247
    Merge<Left, Right>::merge(left, right, left_side, right_side);
274,426✔
2248
}
274,426✔
2249

2250
template <class Outer, class Inner, class OuterSide, class InnerSide>
2251
void merge_nested_2(Outer& outer, Inner& inner, OuterSide& outer_side, InnerSide& inner_side)
2252
{
49,474✔
2253
    MergeNested<Outer>::merge(outer, inner, outer_side, inner_side);
49,474✔
2254
}
49,474✔
2255

2256
template <class OuterSide, class InnerSide>
2257
void merge_nested(OuterSide& outer_side, InnerSide& inner_side)
2258
{
49,468✔
2259
    outer_side.get().visit([&](auto& outer) {
49,474✔
2260
        inner_side.get().visit([&](auto& inner) {
49,474✔
2261
            merge_nested_2(outer, inner, outer_side, inner_side);
49,474✔
2262
        });
49,474✔
2263
    });
49,474✔
2264
}
49,468✔
2265

2266
void TransformerImpl::merge_instructions(MajorSide& their_side, MinorSide& our_side)
2267
{
277,230✔
2268
    // FIXME: Find a way to avoid heap-copies of the path.
136,486✔
2269
    Instruction their_before = their_side.get();
277,230✔
2270
    Instruction our_before = our_side.get();
277,230✔
2271

136,486✔
2272
    if (their_side.get().get_if<Instruction::Update>()) {
277,230✔
2273
        REALM_ASSERT(their_side.m_path_len > 2);
17,892✔
2274
    }
17,892✔
2275
    if (our_side.get().get_if<Instruction::Update>()) {
277,230✔
2276
        REALM_ASSERT(our_side.m_path_len > 2);
19,360✔
2277
    }
19,360✔
2278
    if (their_side.get().get_if<Instruction::EraseObject>()) {
277,230✔
2279
        REALM_ASSERT(their_side.m_path_len == 2);
17,918✔
2280
    }
17,918✔
2281
    if (our_side.get().get_if<Instruction::EraseObject>()) {
277,230✔
2282
        REALM_ASSERT(our_side.m_path_len == 2);
21,454✔
2283
    }
21,454✔
2284

136,486✔
2285
    // Update selections on the major side (outer loop) according to events on
136,486✔
2286
    // the minor side (inner loop). The selection may only be impacted if the
136,486✔
2287
    // instruction level is lower (i.e. at a higher point in the hierarchy).
136,486✔
2288
    if (our_side.m_path_len < their_side.m_path_len) {
277,230✔
2289
        merge_nested(our_side, their_side);
21,700✔
2290
        if (their_side.was_discarded)
21,700✔
2291
            return;
1,472✔
2292
    }
255,530✔
2293
    else if (our_side.m_path_len > their_side.m_path_len) {
255,530✔
2294
        merge_nested(their_side, our_side);
27,770✔
2295
        if (our_side.was_discarded)
27,770✔
2296
            return;
1,374✔
2297
    }
274,384✔
2298

135,164✔
2299
    if (!their_side.was_discarded && !our_side.was_discarded) {
274,402✔
2300
        // Even if the instructions were nested, we must still perform a regular
135,160✔
2301
        // merge, because link-related instructions contain information from higher
135,160✔
2302
        // levels (both rows, columns, and tables).
135,160✔
2303
        //
135,160✔
2304
        // FIXME: This condition goes away when dangling links are implemented.
135,160✔
2305
        their_side.get().visit([&](auto& their_instruction) {
274,422✔
2306
            our_side.get().visit([&](auto& our_instruction) {
274,426✔
2307
                merge_instructions_2(their_instruction, our_instruction, their_side, our_side);
274,426✔
2308
            });
274,426✔
2309
        });
274,422✔
2310
    }
274,398✔
2311

135,164✔
2312
    // Note: `left` and/or `right` may be dangling at this point due to
135,164✔
2313
    // discard/prepend. However, if they were not discarded, their iterators are
135,164✔
2314
    // required to point to an instruction of the same type.
135,164✔
2315
    if (!their_side.was_discarded && !their_side.was_replaced) {
274,384✔
2316
        const auto& their_after = their_side.get();
255,840✔
2317
        if (!(their_after == their_before)) {
255,840✔
2318
            their_side.m_changeset->set_dirty(true);
1,126✔
2319
        }
1,126✔
2320
    }
255,840✔
2321

135,164✔
2322
    if (!our_side.was_discarded && !our_side.was_replaced) {
274,384✔
2323
        const auto& our_after = our_side.get();
256,658✔
2324
        if (!(our_after == our_before)) {
256,658✔
2325
            our_side.m_changeset->set_dirty(true);
1,130✔
2326
        }
1,130✔
2327
    }
256,658✔
2328
}
274,384✔
2329

2330
} // anonymous namespace
2331

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

25,434✔
2351
    _impl::ChangesetIndex their_index;
45,054✔
2352
    size_t their_num_instructions = 0;
45,054✔
2353
    size_t our_num_instructions = 0;
45,054✔
2354

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

35,704✔
2366
        their_index.scan_changeset(their_changesets[i]);
68,830✔
2367
    }
68,830✔
2368
    for (size_t i = 0; i < our_changesets.size(); ++i) {
596,936✔
2369
        Changeset& our_changeset = *our_changesets[i];
551,882✔
2370
        size_t num_instructions = our_changeset.size();
551,882✔
2371
        our_num_instructions += num_instructions;
551,882✔
2372
        logger.trace(util::LogCategory::changeset, "Scanning local changeset [%1/%2] (%3 instructions)", i + 1,
551,882✔
2373
                     our_changesets.size(), num_instructions);
551,882✔
2374

306,230✔
2375
        their_index.scan_changeset(our_changeset);
551,882✔
2376
    }
551,882✔
2377

25,434✔
2378
    // Build the index.
25,434✔
2379
    for (size_t i = 0; i < their_changesets.size(); ++i) {
113,890✔
2380
        logger.trace(util::LogCategory::changeset, "Indexing incoming changeset [%1/%2] (%3 instructions)", i + 1,
68,836✔
2381
                     their_changesets.size(), their_changesets[i].size());
68,836✔
2382
        their_index.add_changeset(their_changesets[i]);
68,836✔
2383
    }
68,836✔
2384

25,434✔
2385
    logger.debug(util::LogCategory::changeset,
45,054✔
2386
                 "Finished changeset indexing (incoming: %1 changeset(s) / %2 instructions, local: %3 "
45,054✔
2387
                 "changeset(s) / %4 instructions, conflict group(s): %5)",
45,054✔
2388
                 their_changesets.size(), their_num_instructions, our_changesets.size(), our_num_instructions,
45,054✔
2389
                 their_index.get_num_conflict_groups());
45,054✔
2390

25,434✔
2391
#if REALM_DEBUG // LCOV_EXCL_START
25,434✔
2392
    if (trace) {
45,054✔
2393
        std::cerr << TERM_YELLOW << "\n=> PEER " << std::hex << local_file_ident
×
2394
                  << " merging "
×
2395
                     "changeset(s)/from peer(s):\n";
×
2396
        for (size_t i = 0; i < their_changesets.size(); ++i) {
×
2397
            std::cerr << "Changeset version " << std::dec << their_changesets[i].version << " from peer "
×
2398
                      << their_changesets[i].origin_file_ident << " at timestamp "
×
2399
                      << their_changesets[i].origin_timestamp << "\n";
×
2400
        }
×
2401
        std::cerr << "Transforming through local changeset(s):\n";
×
2402
        for (size_t i = 0; i < our_changesets.size(); ++i) {
×
2403
            std::cerr << "Changeset version " << our_changesets[i]->version << " from peer "
×
2404
                      << our_changesets[i]->origin_file_ident << " at timestamp "
×
2405
                      << our_changesets[i]->origin_timestamp << "\n";
×
2406
        }
×
2407

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

2413
        for (size_t i = 0; i < their_changesets.size(); ++i) {
×
2414
            std::cerr << TERM_RED << "\nINCOMING CHANGESET BEFORE MERGE:\n" << TERM_RESET;
×
2415
            their_changesets[i].print(std::cerr);
×
2416
        }
×
2417

2418
        std::cerr << TERM_MAGENTA << "\nINCOMING CHANGESET INDEX:\n" << TERM_RESET;
×
2419
        their_index.print(std::cerr);
×
2420
        std::cerr << '\n';
×
2421
        their_index.verify();
×
2422

2423
        std::cerr << TERM_YELLOW << std::setw(80) << std::left << "MERGE TRACE (incoming):"
×
2424
                  << "MERGE TRACE (local):\n"
×
2425
                  << TERM_RESET;
×
2426
    }
×
2427
#else
2428
    static_cast<void>(local_file_ident);
2429
#endif // REALM_DEBUG LCOV_EXCL_STOP
2430

25,434✔
2431
    for (size_t i = 0; i < our_changesets.size(); ++i) {
596,952✔
2432
        logger.trace(
551,898✔
2433
            util::LogCategory::changeset,
551,898✔
2434
            "Transforming local changeset [%1/%2] through %3 incoming changeset(s) with %4 conflict group(s)", i + 1,
551,898✔
2435
            our_changesets.size(), their_changesets.size(), their_index.get_num_conflict_groups());
551,898✔
2436
        Changeset* our_changeset = our_changesets[i];
551,898✔
2437

306,264✔
2438
        transformer.m_major_side.set_next_changeset(our_changeset);
551,898✔
2439
        // MinorSide uses the index to find the Changeset.
306,264✔
2440
        transformer.m_minor_side.m_changeset_index = &their_index;
551,898✔
2441
        transformer.transform(); // Throws
551,898✔
2442
    }
551,898✔
2443

25,434✔
2444
    logger.debug(util::LogCategory::changeset,
45,054✔
2445
                 "Finished transforming %1 local changesets through %2 incoming changesets (%3 vs %4 "
45,054✔
2446
                 "instructions, in %5 conflict groups)",
45,054✔
2447
                 our_changesets.size(), their_changesets.size(), our_num_instructions, their_num_instructions,
45,054✔
2448
                 their_index.get_num_conflict_groups());
45,054✔
2449

25,434✔
2450
#if REALM_DEBUG // LCOV_EXCL_START
25,434✔
2451
    // Check that the index is still valid after transformation.
25,434✔
2452
    their_index.verify();
45,054✔
2453
#endif // REALM_DEBUG LCOV_EXCL_STOP
45,054✔
2454

25,434✔
2455
#if REALM_DEBUG // LCOV_EXCL_START
25,434✔
2456
    if (trace) {
45,054✔
2457
        for (size_t i = 0; i < our_changesets.size(); ++i) {
×
2458
            std::cerr << TERM_CYAN << "\nRECIPROCAL CHANGESET AFTER MERGE:\n" << TERM_RESET;
×
2459
            our_changesets[i]->print(std::cerr);
×
2460
            std::cerr << '\n';
×
2461
        }
×
2462
        for (size_t i = 0; i < their_changesets.size(); ++i) {
×
2463
            std::cerr << TERM_CYAN << "INCOMING CHANGESET AFTER MERGE:\n" << TERM_RESET;
×
2464
            their_changesets[i].print(std::cerr);
×
2465
            std::cerr << '\n';
×
2466
        }
×
2467
    }
×
2468
#endif // LCOV_EXCL_STOP REALM_DEBUG
45,054✔
2469
}
45,054✔
2470

2471
size_t Transformer::transform_remote_changesets(TransformHistory& history, file_ident_type local_file_ident,
2472
                                                version_type current_local_version,
2473
                                                util::Span<Changeset> parsed_changesets,
2474
                                                util::FunctionRef<bool(const Changeset*)> changeset_applier,
2475
                                                util::Logger& logger)
2476
{
60,790✔
2477
    REALM_ASSERT(local_file_ident != 0);
60,790✔
2478

32,374✔
2479
    std::vector<Changeset*> our_changesets;
60,790✔
2480

32,374✔
2481
    // p points to the beginning of a range of changesets that share the same
32,374✔
2482
    // "base", i.e. are based on the same local version.
32,374✔
2483
    auto p = parsed_changesets.begin();
60,790✔
2484
    auto parsed_changesets_end = parsed_changesets.end();
60,790✔
2485

32,374✔
2486
    while (p != parsed_changesets_end) {
127,152✔
2487
        // Find the range of incoming changesets that share the same
35,830✔
2488
        // last_integrated_local_version, which means we can merge them in one go.
35,830✔
2489
        auto same_base_range_end = std::find_if(p + 1, parsed_changesets_end, [&](auto& changeset) {
55,690✔
2490
            return p->last_integrated_remote_version != changeset.last_integrated_remote_version;
36,374✔
2491
        });
36,374✔
2492

35,830✔
2493
        version_type begin_version = p->last_integrated_remote_version;
66,362✔
2494
        version_type end_version = current_local_version;
66,362✔
2495
        for (;;) {
618,244✔
2496
            HistoryEntry history_entry;
618,244✔
2497
            version_type version = history.find_history_entry(begin_version, end_version, history_entry);
618,244✔
2498
            if (version == 0)
618,244✔
2499
                break; // No more local changesets
66,362✔
2500

306,264✔
2501
            Changeset& our_changeset = get_reciprocal_transform(history, local_file_ident, version,
551,882✔
2502
                                                                history_entry); // Throws
551,882✔
2503
            our_changesets.push_back(&our_changeset);
551,882✔
2504

306,264✔
2505
            begin_version = version;
551,882✔
2506
        }
551,882✔
2507

35,830✔
2508
        bool must_apply_all = false;
66,362✔
2509

35,830✔
2510
        if (!our_changesets.empty()) {
66,362✔
2511
            merge_changesets(local_file_ident, {&*p, same_base_range_end}, our_changesets, logger); // Throws
45,054✔
2512
            // We need to apply all transformed changesets if at least one reciprocal changeset was modified
25,434✔
2513
            // during OT.
25,434✔
2514
            must_apply_all = std::any_of(our_changesets.begin(), our_changesets.end(), [](const Changeset* c) {
468,140✔
2515
                return c->is_dirty();
468,140✔
2516
            });
468,140✔
2517
        }
45,054✔
2518

35,830✔
2519
        auto continue_applying = true;
66,362✔
2520
        for (; p != same_base_range_end && continue_applying; ++p) {
163,458✔
2521
            // It is safe to stop applying the changesets if:
48,852✔
2522
            //      1. There are no reciprocal changesets
48,852✔
2523
            //      2. No reciprocal changeset was modified
48,852✔
2524
            continue_applying = changeset_applier(p) || must_apply_all;
97,096!
2525
        }
97,096✔
2526
        if (!continue_applying) {
66,362✔
2527
            break;
×
2528
        }
×
2529

35,830✔
2530
        our_changesets.clear(); // deliberately not releasing memory
66,362✔
2531
    }
66,362✔
2532

32,374✔
2533
    // NOTE: Any exception thrown during flushing *MUST* lead to rollback of
32,374✔
2534
    // the current transaction.
32,374✔
2535
    flush_reciprocal_transform_cache(history); // Throws
60,790✔
2536

32,374✔
2537
    return p - parsed_changesets.begin();
60,790✔
2538
}
60,790✔
2539

2540

2541
Changeset& Transformer::get_reciprocal_transform(TransformHistory& history, file_ident_type local_file_ident,
2542
                                                 version_type version, const HistoryEntry& history_entry)
2543
{
551,906✔
2544
    auto& changeset = m_reciprocal_transform_cache[version]; // Throws
551,906✔
2545
    if (changeset.empty()) {
551,906✔
2546
        bool is_compressed = false;
495,220✔
2547
        ChunkedBinaryData data = history.get_reciprocal_transform(version, is_compressed);
495,220✔
2548
        ChunkedBinaryInputStream in{data};
495,220✔
2549
        if (is_compressed) {
495,220✔
2550
            size_t total_size;
56,694✔
2551
            auto decompressed = util::compression::decompress_nonportable_input_stream(in, total_size);
56,694✔
2552
            REALM_ASSERT(decompressed);
56,694✔
2553
            sync::parse_changeset(*decompressed, changeset); // Throws
56,694✔
2554
        }
56,694✔
2555
        else {
438,526✔
2556
            sync::parse_changeset(in, changeset); // Throws
438,526✔
2557
        }
438,526✔
2558

260,124✔
2559
        changeset.version = version;
495,220✔
2560
        changeset.last_integrated_remote_version = history_entry.remote_version;
495,220✔
2561
        changeset.origin_timestamp = history_entry.origin_timestamp;
495,220✔
2562
        file_ident_type origin_file_ident = history_entry.origin_file_ident;
495,220✔
2563
        if (origin_file_ident == 0)
495,220✔
2564
            origin_file_ident = local_file_ident;
281,026✔
2565
        changeset.origin_file_ident = origin_file_ident;
495,220✔
2566
    }
495,220✔
2567
    return changeset;
551,906✔
2568
}
551,906✔
2569

2570

2571
void Transformer::flush_reciprocal_transform_cache(TransformHistory& history)
2572
{
60,730✔
2573
    auto changesets = std::move(m_reciprocal_transform_cache);
60,730✔
2574
    m_reciprocal_transform_cache.clear();
60,730✔
2575
    ChangesetEncoder::Buffer output_buffer;
60,730✔
2576
    for (const auto& [version, changeset] : changesets) {
491,590✔
2577
        if (changeset.is_dirty()) {
491,590✔
2578
            encode_changeset(changeset, output_buffer); // Throws
8,972✔
2579
            BinaryData data{output_buffer.data(), output_buffer.size()};
8,972✔
2580
            history.set_reciprocal_transform(version, data); // Throws
8,972✔
2581
            output_buffer.clear();
8,972✔
2582
        }
8,972✔
2583
    }
491,590✔
2584
}
60,730✔
2585

2586
void parse_remote_changeset(const RemoteChangeset& remote_changeset, Changeset& parsed_changeset)
2587
{
97,186✔
2588
    // origin_file_ident = 0 is currently used to indicate an entry of local
48,878✔
2589
    // origin.
48,878✔
2590
    REALM_ASSERT(remote_changeset.origin_file_ident != 0);
97,186✔
2591
    REALM_ASSERT(remote_changeset.remote_version != 0);
97,186✔
2592

48,878✔
2593
    ChunkedBinaryInputStream remote_in{remote_changeset.data};
97,186✔
2594
    parse_changeset(remote_in, parsed_changeset); // Throws
97,186✔
2595

48,878✔
2596
    parsed_changeset.version = remote_changeset.remote_version;
97,186✔
2597
    parsed_changeset.last_integrated_remote_version = remote_changeset.last_integrated_local_version;
97,186✔
2598
    parsed_changeset.origin_timestamp = remote_changeset.origin_timestamp;
97,186✔
2599
    parsed_changeset.origin_file_ident = remote_changeset.origin_file_ident;
97,186✔
2600
    parsed_changeset.original_changeset_size = remote_changeset.original_changeset_size;
97,186✔
2601
}
97,186✔
2602

2603
} // 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