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

realm / realm-core / 2214

10 Apr 2024 11:21PM UTC coverage: 91.813% (-0.8%) from 92.623%
2214

push

Evergreen

web-flow
Add missing availability checks for SecCopyErrorMessageString (#7577)

This requires iOS 11.3 and we currently target iOS 11.

94848 of 175770 branches covered (53.96%)

7 of 22 new or added lines in 2 files covered. (31.82%)

1815 existing lines in 77 files now uncovered.

242945 of 264608 relevant lines covered (91.81%)

6136478.37 hits per line

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

64.67
/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
    {
900,666✔
45
    }
900,666✔
46

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

50
    bool operator<(const Discriminant& other) const
51
    {
9,392✔
52
        return timestamp == other.timestamp ? (client_file_ident < other.client_file_ident)
6,002✔
53
                                            : timestamp < other.timestamp;
9,154✔
54
    }
9,392✔
55

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

66
struct TransformerImpl;
67

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

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

77
    Side(TransformerImpl& transformer)
78
        : m_transformer(transformer)
79
        , m_discriminant(0, 0)
80
    {
89,052✔
81
    }
89,052✔
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,036✔
101
        // Rely on the parser having checked the consistency of the interned strings
12,396✔
102
        return m_changeset->get_string(intern_string);
25,036✔
103
    }
25,036✔
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,784✔
112
        return m_discriminant;
18,784✔
113
    }
18,784✔
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
    {
811,430✔
126
        was_discarded = false;
811,430✔
127
        was_replaced = false;
811,430✔
128
        m_path_len = instr.path_length();
811,430✔
129
    }
811,430✔
130
};
131

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

278,216✔
152
        m_discriminant = Discriminant{m_changeset->origin_timestamp, m_changeset->origin_file_ident};
545,688✔
153

278,216✔
154
        Side::init_with_instruction(get());
545,688✔
155
    }
545,688✔
156

157
    void skip_tombstones() noexcept final
158
    {
2,110,446✔
159
        while (m_position != m_changeset->end() && !*m_position) {
2,111,674✔
160
            ++m_position;
1,228✔
161
        }
1,228✔
162
    }
2,110,446✔
163

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

172
    Instruction& get() noexcept final
173
    {
2,940,752✔
174
        return **m_position;
2,940,752✔
175
    }
2,940,752✔
176

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

182
    Changeset::iterator m_position;
183
};
184

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

188
    MinorSide(TransformerImpl& transformer)
189
        : Side(transformer)
190
    {
44,526✔
191
    }
44,526✔
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
    {
545,820✔
206
        return Position{m_conflict_ranges};
545,820✔
207
    }
545,820✔
208

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

214
    void update_changeset_pointer() noexcept
215
    {
1,142,106✔
216
        if (REALM_LIKELY(m_position != end())) {
1,142,106✔
217
            m_changeset = m_position.m_outer->first;
454,798✔
218
        }
454,798✔
219
        else {
687,308✔
220
            m_changeset = nullptr;
687,308✔
221
        }
687,308✔
222
    }
1,142,106✔
223

224
    void skip_tombstones() noexcept final
225
    {
1,299,068✔
226
        if (m_position != end() && *m_position)
1,299,068✔
227
            return;
682,762✔
228
        skip_tombstones_slow();
616,306✔
229
    }
616,306✔
230

231
    REALM_NOINLINE void skip_tombstones_slow() noexcept
232
    {
616,396✔
233
        while (m_position != end() && !*m_position) {
716,274✔
234
            ++m_position;
99,878✔
235
        }
99,878✔
236
        update_changeset_pointer();
616,396✔
237
    }
616,396✔
238

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

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

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

129,156✔
263
        m_discriminant = Discriminant{m_changeset->origin_timestamp, m_changeset->origin_file_ident};
266,010✔
264

129,156✔
265
        Side::init_with_instruction(get());
266,010✔
266
    }
266,010✔
267

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

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

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

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

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

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

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

309
        const Changeset* m_changeset = nullptr;
310

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

259,602✔
568
        while (m_major_side.m_position != m_major_side.m_changeset->end()) {
1,055,646✔
569
            m_major_side.init_with_instruction(m_major_side.m_position);
545,694✔
570

278,218✔
571
            set_conflict_ranges();
545,694✔
572
            m_minor_end = m_minor_side.end();
545,694✔
573
            m_minor_side.m_position = m_minor_side.begin();
545,694✔
574
            transform_major();
545,694✔
575

278,218✔
576
            if (!m_major_side.was_discarded)
545,694✔
577
                // Discarding the instruction moves to the next one.
268,228✔
578
                m_major_side.next_instruction();
526,358✔
579
            m_major_side.skip_tombstones();
545,694✔
580
        }
545,694✔
581
    }
509,952✔
582

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

278,218✔
587
        if (_impl::is_schema_change(instr)) {
545,658✔
588
            ///
20,834✔
589
            /// CONFLICT GROUP: Everything touching that class
20,834✔
590
            ///
20,834✔
591
            auto ranges = index.get_everything();
44,510✔
592
            if (!ranges->empty()) {
44,510✔
593
#if REALM_DEBUG // LCOV_EXCL_START
16,608✔
594
                if (m_trace) {
35,146✔
595
                    std::cerr << TERM_RED << "Conflict group: Everything (due to schema change)\n" << TERM_RESET;
×
596
                }
×
597
#endif // REALM_DEBUG LCOV_EXCL_STOP
35,146✔
598
            }
35,146✔
599
            return ranges;
44,510✔
600
        }
44,510✔
601
        else {
501,148✔
602
            ///
257,384✔
603
            /// CONFLICT GROUP: Everything touching the involved objects,
257,384✔
604
            /// including schema changes.
257,384✔
605
            ///
257,384✔
606
            _impl::ChangesetIndex::GlobalID major_ids[2];
501,148✔
607
            size_t num_major_ids = m_major_side.get_object_ids_in_current_instruction(major_ids, 2);
501,148✔
608
            REALM_ASSERT(num_major_ids <= 2);
501,148✔
609
            REALM_ASSERT(num_major_ids >= 1);
501,148✔
610
#if REALM_DEBUG // LCOV_EXCL_START
257,384✔
611
            if (m_trace) {
501,148✔
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
501,148✔
624
            auto ranges = index.get_modifications_for_object(major_ids[0]);
501,148✔
625
            if (num_major_ids == 2) {
501,148✔
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]));
154✔
629
            }
154✔
630
            return ranges;
501,148✔
631
        }
501,148✔
632
    }
545,658✔
633

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

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

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

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

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

278,330✔
772
        while (m_minor_side.m_position != m_minor_end) {
792,392✔
773
            m_minor_side.init_with_instruction(m_minor_side.m_position);
265,984✔
774

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

129,126✔
791
            if (m_major_side.was_discarded)
265,984✔
792
                break;
19,380✔
793
            if (!m_minor_side.was_discarded)
246,604✔
794
                // Discarding an instruction moves to the next one.
115,812✔
795
                m_minor_side.next_instruction();
240,994✔
796
            m_minor_side.skip_tombstones();
246,604✔
797
        }
246,604✔
798
    }
545,788✔
799

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

803
void MajorSide::set_next_changeset(Changeset* changeset) noexcept
804
{
510,032✔
805
    m_transformer.set_next_major_changeset(changeset);
510,032✔
806
}
510,032✔
807
void MajorSide::discard()
808
{
19,380✔
809
    m_transformer.discard_major();
19,380✔
810
}
19,380✔
811
void MajorSide::prepend(Instruction operation)
812
{
×
813
    m_transformer.prepend_major(std::move(operation));
×
814
}
×
815
template <class InputIterator>
816
void MajorSide::prepend(InputIterator begin, InputIterator end)
817
{
818
    m_transformer.prepend_major(std::move(begin), std::move(end));
819
}
820
void MinorSide::discard()
821
{
18,878✔
822
    m_transformer.discard_minor();
18,878✔
823
}
18,878✔
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
{
48✔
836
    throw sync::TransformError{std::move(msg)};
48✔
837
}
48✔
838

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

874
    bool same_key(const Instruction::PrimaryKey& left, const Instruction::PrimaryKey& right) const noexcept
875
    {
22,712✔
876
        // FIXME: Once we can compare string by InternString map lookups,
13,008✔
877
        // compare the string components of the keys using that.
13,008✔
878
        PrimaryKey left_key = m_left_side.m_changeset->get_key(left);
22,712✔
879
        PrimaryKey right_key = m_right_side.m_changeset->get_key(right);
22,712✔
880
        return left_key == right_key;
22,712✔
881
    }
22,712✔
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
    {
1,792✔
941
        auto pred = util::overload{
1,792✔
942
            [](uint32_t lhs, uint32_t rhs) {
1,248✔
943
                return lhs == rhs;
704✔
944
            },
704✔
945
            [this](InternString lhs, InternString rhs) {
1,434✔
946
                return same_string(lhs, rhs);
1,088✔
947
            },
1,088✔
948
            // FIXME: Paths contain incompatible element types. Should we raise an error here?
890✔
949
            [](InternString, uint32_t) {
890✔
950
                return false;
×
951
            },
×
952
            [](uint32_t, InternString) {
890✔
953
                return false;
×
954
            },
×
955
        };
1,792✔
956
        return mpark::visit(pred, left, right);
1,792✔
957
    }
1,792✔
958

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

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

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

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

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

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

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

2,088✔
1021
            for (size_t i = 0; i < left.size() - 1; ++i) {
3,572✔
1022
                if (!same_path_element(left[i], right[i])) {
208✔
1023
                    return false;
×
1024
                }
×
1025
            }
208✔
1026
            return true;
3,364✔
1027
        }
224✔
1028
        return false;
224✔
1029
    }
224✔
1030

1031
    bool same_container(const Instruction::PathInstruction& left,
1032
                        const Instruction::PathInstruction& right) const noexcept
1033
    {
7,634✔
1034
        return same_field(left, right) && same_container(left.path, right.path);
7,634✔
1035
    }
7,634✔
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::ObjectInstruction&, const Instruction::TableInstruction&) const noexcept
1042
    {
×
1043
        // Right side is a schema instruction.
1044
        return false;
×
1045
    }
×
1046

1047
    bool is_prefix_of(const Instruction::ObjectInstruction& left,
1048
                      const Instruction::PathInstruction& right) const noexcept
1049
    {
8,314✔
1050
        return same_object(left, right);
8,314✔
1051
    }
8,314✔
1052

1053
    // Returns the next path element if the first path is a parent of the second path.
1054
    // Example:
1055
    //  * is_prefix_of(field1.123.field2, field1.123.field2.456) = 456
1056
    //  * is_prefix_of(field1.123.field2, field1.123.field3.456) = {}
1057

1058
    std::optional<Instruction::Path::Element> is_prefix_of(const Instruction::PathInstruction&,
1059
                                                           const Instruction::TableInstruction&) const noexcept
1060
    {
×
1061
        // Path instructions can never be full prefixes of table-level instructions. Note that this also covers
1062
        // ObjectInstructions.
1063
        return {};
×
1064
    }
×
1065

1066
    std::optional<Instruction::Path::Element> is_prefix_of(const Instruction::PathInstruction& left,
1067
                                                           const Instruction::PathInstruction& right) const noexcept
1068
    {
2,372✔
1069
        if (left.path.size() < right.path.size() && same_field(left, right)) {
2,372✔
1070
            for (size_t i = 0; i < left.path.size(); ++i) {
1,512✔
1071
                if (!same_path_element(left.path[i], right.path[i])) {
728✔
1072
                    return {};
32✔
1073
                }
32✔
1074
            }
728✔
1075
            return right.path[left.path.size()];
800✔
1076
        }
1,556✔
1077
        return {};
1,556✔
1078
    }
1,556✔
1079

1080
    // True if the left side is an instruction that touches a container within
1081
    // right's path. Equivalent to is_prefix_of, except the last element (the
1082
    // index) is not considered.
1083
    bool is_container_prefix_of(const Instruction::PathInstruction& left,
1084
                                const Instruction::PathInstruction& right) const
1085
    {
32✔
1086
        if (left.path.size() != 0 && left.path.size() < right.path.size() && same_field(left, right)) {
32✔
1087
            for (size_t i = 0; i < left.path.size() - 1; ++i) {
32✔
1088
                if (!same_path_element(left.path[i], right.path[i])) {
×
1089
                    return false;
×
1090
                }
×
1091
            }
×
1092
            return true;
32✔
1093
        }
×
1094
        return false;
×
1095
    }
×
1096

1097
    bool is_container_prefix_of(const Instruction::PathInstruction&, const Instruction::TableInstruction&) const
1098
    {
×
1099
        return false;
×
1100
    }
×
1101

1102
    // When the left side has a shorter path, get the path component on the
1103
    // right that corresponds to the last component on the left.
1104
    //
1105
    // Note that this will only be used in the context of array indices, because
1106
    // those are the only path components that are modified during OT.
1107
    uint32_t& corresponding_index_in_path(const Instruction::PathInstruction& left,
1108
                                          Instruction::PathInstruction& right) const
1109
    {
32✔
1110
        REALM_ASSERT(left.path.size() != 0);
32✔
1111
        REALM_ASSERT(left.path.size() < right.path.size());
32✔
1112
        REALM_ASSERT(mpark::holds_alternative<uint32_t>(left.path.back()));
32✔
1113
        size_t index = left.path.size() - 1;
32✔
1114
        if (!mpark::holds_alternative<uint32_t>(right.path[index])) {
32✔
1115
            bad_merge("Inconsistent paths");
×
1116
        }
×
1117
        return mpark::get<uint32_t>(right.path[index]);
32✔
1118
    }
32✔
1119

1120
    uint32_t& corresponding_index_in_path(const Instruction::PathInstruction&,
1121
                                          const Instruction::TableInstruction&) const
1122
    {
×
1123
        // A path instruction can never have a shorter path than something that
1124
        // isn't a PathInstruction.
1125
        REALM_UNREACHABLE();
1126
    }
×
1127

1128
    void merge_get_vs_move(uint32_t& get_ndx, const uint32_t& move_from_ndx,
1129
                           const uint32_t& move_to_ndx) const noexcept
1130
    {
×
1131
        if (get_ndx == move_from_ndx) {
×
1132
            // CONFLICT: Update of a moved element.
1133
            //
1134
            // RESOLUTION: On the left side, use the MOVE operation to transform the
1135
            // UPDATE operation received from the right side.
1136
            get_ndx = move_to_ndx; // --->
×
1137
        }
×
1138
        else {
×
1139
            // Right update vs left remove
1140
            if (get_ndx > move_from_ndx) {
×
1141
                get_ndx -= 1; // --->
×
1142
            }
×
1143
            // Right update vs left insert
1144
            if (get_ndx >= move_to_ndx) {
×
1145
                get_ndx += 1; // --->
×
1146
            }
×
1147
        }
×
1148
    }
×
1149

1150
protected:
1151
    Side& m_left_side;
1152
    Side& m_right_side;
1153
};
1154

1155
template <class LeftInstruction, class RightInstruction>
1156
struct MergeBase : MergeUtils {
1157
    static const Instruction::Type A = Instruction::GetInstructionType<LeftInstruction>::value;
1158
    static const Instruction::Type B = Instruction::GetInstructionType<RightInstruction>::value;
1159
    static_assert(A >= B, "A < B. Please reverse the order of instruction types. :-)");
1160

1161
    MergeBase(Side& left_side, Side& right_side)
1162
        : MergeUtils(left_side, right_side)
1163
    {
51,504✔
1164
    }
51,504✔
1165
    MergeBase(MergeBase&&) = delete;
1166
};
1167

1168
#define DEFINE_MERGE(A, B)                                                                                           \
1169
    template <>                                                                                                      \
1170
    struct Merge<A, B> {                                                                                             \
1171
        template <class LeftSide, class RightSide>                                                                   \
1172
        struct DoMerge : MergeBase<A, B> {                                                                           \
1173
            A& left;                                                                                                 \
1174
            B& right;                                                                                                \
1175
            LeftSide& left_side;                                                                                     \
1176
            RightSide& right_side;                                                                                   \
1177
            DoMerge(A& left, B& right, LeftSide& left_side, RightSide& right_side)                                   \
1178
                : MergeBase<A, B>(left_side, right_side)                                                             \
1179
                , left(left)                                                                                         \
1180
                , right(right)                                                                                       \
1181
                , left_side(left_side)                                                                               \
1182
                , right_side(right_side)                                                                             \
1183
            {                                                                                                        \
51,494✔
1184
            }                                                                                                        \
51,494✔
1185
            void do_merge();                                                                                         \
1186
        };                                                                                                           \
1187
        template <class LeftSide, class RightSide>                                                                   \
1188
        static inline void merge(A& left, B& right, LeftSide& left_side, RightSide& right_side)                      \
1189
        {                                                                                                            \
51,490✔
1190
            DoMerge<LeftSide, RightSide> do_merge{left, right, left_side, right_side};                               \
51,490✔
1191
            do_merge.do_merge();                                                                                     \
51,490✔
1192
        }                                                                                                            \
51,490✔
1193
    };                                                                                                               \
1194
    template <class LeftSide, class RightSide>                                                                       \
1195
    void Merge<A, B>::DoMerge<LeftSide, RightSide>::do_merge()
1196

1197
#define DEFINE_MERGE_NOOP(A, B)                                                                                      \
1198
    template <>                                                                                                      \
1199
    struct Merge<A, B> {                                                                                             \
1200
        static const Instruction::Type left_type = Instruction::GetInstructionType<A>::value;                        \
1201
        static const Instruction::Type right_type = Instruction::GetInstructionType<B>::value;                       \
1202
        static_assert(left_type >= right_type,                                                                       \
1203
                      "left_type < right_type. Please reverse the order of instruction types. :-)");                 \
1204
        template <class LeftSide, class RightSide>                                                                   \
1205
        static inline void merge(A&, B&, LeftSide&, RightSide&)                                                      \
1206
        { /* Do nothing */                                                                                           \
212,078✔
1207
        }                                                                                                            \
212,078✔
1208
    }
1209

1210

1211
#define DEFINE_NESTED_MERGE(A)                                                                                       \
1212
    template <>                                                                                                      \
1213
    struct MergeNested<A> {                                                                                          \
1214
        template <class B, class OuterSide, class InnerSide>                                                         \
1215
        struct DoMerge : MergeUtils {                                                                                \
1216
            A& outer;                                                                                                \
1217
            B& inner;                                                                                                \
1218
            OuterSide& outer_side;                                                                                   \
1219
            InnerSide& inner_side;                                                                                   \
1220
            DoMerge(A& outer, B& inner, OuterSide& outer_side, InnerSide& inner_side)                                \
1221
                : MergeUtils(outer_side, inner_side)                                                                 \
1222
                , outer(outer)                                                                                       \
1223
                , inner(inner)                                                                                       \
1224
                , outer_side(outer_side)                                                                             \
1225
                , inner_side(inner_side)                                                                             \
1226
            {                                                                                                        \
15,698✔
1227
            }                                                                                                        \
15,698✔
1228
            void do_merge();                                                                                         \
1229
        };                                                                                                           \
1230
        template <class B, class OuterSide, class InnerSide>                                                         \
1231
        static inline void merge(A& outer, B& inner, OuterSide& outer_side, InnerSide& inner_side)                   \
1232
        {                                                                                                            \
15,698✔
1233
            DoMerge<B, OuterSide, InnerSide> do_merge{outer, inner, outer_side, inner_side};                         \
15,698✔
1234
            do_merge.do_merge();                                                                                     \
15,698✔
1235
        }                                                                                                            \
15,698✔
1236
    };                                                                                                               \
1237
    template <class B, class OuterSide, class InnerSide>                                                             \
1238
    void MergeNested<A>::DoMerge<B, OuterSide, InnerSide>::do_merge()
1239

1240
#define DEFINE_NESTED_MERGE_NOOP(A)                                                                                  \
1241
    template <>                                                                                                      \
1242
    struct MergeNested<A> {                                                                                          \
1243
        template <class B, class OuterSide, class InnerSide>                                                         \
1244
        static inline void merge(const A&, const B&, const OuterSide&, const InnerSide&)                             \
1245
        { /* Do nothing */                                                                                           \
28,478✔
1246
        }                                                                                                            \
28,478✔
1247
    }
1248

1249
// Implementation that reverses order.
1250
template <class A, class B>
1251
struct Merge<A, B,
1252
             typename std::enable_if<(Instruction::GetInstructionType<A>::value <
1253
                                      Instruction::GetInstructionType<B>::value)>::type> {
1254
    template <class LeftSide, class RightSide>
1255
    static void merge(A& left, B& right, LeftSide& left_side, RightSide& right_side)
1256
    {
34,400✔
1257
        Merge<B, A>::merge(right, left, right_side, left_side);
34,400✔
1258
    }
34,400✔
1259
};
1260

1261
///
1262
///  GET READY!
1263
///
1264
///  Realm supports 14 instructions at the time of this writing. Each
1265
///  instruction type needs one rule for each other instruction type. We only
1266
///  define one rule to handle each combination (A vs B and B vs A are handle by
1267
///  a single rule).
1268
///
1269
///  This gives (14 * (14 + 1)) / 2 = 105 merge rules below.
1270
///
1271
///  Merge rules are ordered such that the second instruction type is always of
1272
///  a lower enum value than the first.
1273
///
1274
///  Nested merge rules apply when one instruction has a strictly longer path
1275
///  than another. All instructions that have a path of the same length will
1276
///  meet each other through regular merge rules, regardless of whether they
1277
///  share a prefix.
1278
///
1279

1280

1281
/// AddTable rules
1282

1283
DEFINE_NESTED_MERGE_NOOP(Instruction::AddTable);
1284

1285
DEFINE_MERGE(Instruction::AddTable, Instruction::AddTable)
1286
{
4,256✔
1287
    if (same_table(left, right)) {
4,256✔
1288
        StringData left_name = left_side.get_string(left.table);
3,944✔
1289
        if (auto left_spec = mpark::get_if<Instruction::AddTable::TopLevelTable>(&left.type)) {
3,944✔
1290
            if (auto right_spec = mpark::get_if<Instruction::AddTable::TopLevelTable>(&right.type)) {
3,784✔
1291
                StringData left_pk_name = left_side.get_string(left_spec->pk_field);
3,784✔
1292
                StringData right_pk_name = right_side.get_string(right_spec->pk_field);
3,784✔
1293
                if (left_pk_name != right_pk_name) {
3,784✔
1294
                    bad_merge(
8✔
1295
                        "Schema mismatch: '%1' has primary key '%2' on one side, but primary key '%3' on the other.",
8✔
1296
                        left_name, left_pk_name, right_pk_name);
8✔
1297
                }
8✔
1298

1,860✔
1299
                if (left_spec->pk_type != right_spec->pk_type) {
3,784✔
1300
                    bad_merge("Schema mismatch: '%1' has primary key '%2', which is of type %3 on one side and type "
8✔
1301
                              "%4 on the other.",
8✔
1302
                              left_name, left_pk_name, get_type_name(left_spec->pk_type),
8✔
1303
                              get_type_name(right_spec->pk_type));
8✔
1304
                }
8✔
1305

1,860✔
1306
                if (left_spec->pk_nullable != right_spec->pk_nullable) {
3,784✔
1307
                    bad_merge("Schema mismatch: '%1' has primary key '%2', which is nullable on one side, but not "
8✔
1308
                              "the other.",
8✔
1309
                              left_name, left_pk_name);
8✔
1310
                }
8✔
1311

1,860✔
1312
                if (left_spec->is_asymmetric != right_spec->is_asymmetric) {
3,784✔
1313
                    bad_merge("Schema mismatch: '%1' is asymmetric on one side, but not on the other.", left_name);
4✔
1314
                }
4✔
1315
            }
3,784✔
1316
            else {
×
1317
                bad_merge("Schema mismatch: '%1' has a primary key on one side, but not on the other.", left_name);
×
1318
            }
×
1319
        }
3,784✔
1320
        else if (mpark::get_if<Instruction::AddTable::EmbeddedTable>(&left.type)) {
160✔
1321
            if (!mpark::get_if<Instruction::AddTable::EmbeddedTable>(&right.type)) {
160✔
1322
                bad_merge("Schema mismatch: '%1' is an embedded table on one side, but not the other.", left_name);
×
1323
            }
×
1324
        }
160✔
1325

1,940✔
1326
        // Names are the same, PK presence is the same, and if there is a primary
1,940✔
1327
        // key, its name, type, and nullability are the same. Discard both sides.
1,940✔
1328
        left_side.discard();
3,944✔
1329
        right_side.discard();
3,944✔
1330
        return;
3,944✔
1331
    }
3,944✔
1332
}
4,256✔
1333

1334
DEFINE_MERGE(Instruction::EraseTable, Instruction::AddTable)
1335
{
120✔
1336
    if (same_table(left, right)) {
120✔
1337
        right_side.discard();
×
1338
    }
×
1339
}
120✔
1340

1341
DEFINE_MERGE_NOOP(Instruction::CreateObject, Instruction::AddTable);
1342
DEFINE_MERGE_NOOP(Instruction::EraseObject, Instruction::AddTable);
1343
DEFINE_MERGE_NOOP(Instruction::Update, Instruction::AddTable);
1344
DEFINE_MERGE_NOOP(Instruction::AddInteger, Instruction::AddTable);
1345
DEFINE_MERGE_NOOP(Instruction::AddColumn, Instruction::AddTable);
1346
DEFINE_MERGE_NOOP(Instruction::EraseColumn, Instruction::AddTable);
1347
DEFINE_MERGE_NOOP(Instruction::ArrayInsert, Instruction::AddTable);
1348
DEFINE_MERGE_NOOP(Instruction::ArrayMove, Instruction::AddTable);
1349
DEFINE_MERGE_NOOP(Instruction::ArrayErase, Instruction::AddTable);
1350
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::AddTable);
1351
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::AddTable);
1352
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::AddTable);
1353

1354
/// EraseTable rules
1355

1356
DEFINE_NESTED_MERGE(Instruction::EraseTable)
1357
{
4,924✔
1358
    if (same_table(outer, inner)) {
4,924!
1359
        inner_side.discard();
988✔
1360
    }
988✔
1361
}
4,924✔
1362

1363
DEFINE_MERGE(Instruction::EraseTable, Instruction::EraseTable)
1364
{
24✔
1365
    if (same_table(left, right)) {
24✔
1366
        left_side.discard();
12✔
1367
        right_side.discard();
12✔
1368
    }
12✔
1369
}
24✔
1370

1371
// Handled by nesting rule.
1372
DEFINE_MERGE_NOOP(Instruction::CreateObject, Instruction::EraseTable);
1373
DEFINE_MERGE_NOOP(Instruction::EraseObject, Instruction::EraseTable);
1374
DEFINE_MERGE_NOOP(Instruction::Update, Instruction::EraseTable);
1375
DEFINE_MERGE_NOOP(Instruction::AddInteger, Instruction::EraseTable);
1376

1377
DEFINE_MERGE(Instruction::AddColumn, Instruction::EraseTable)
1378
{
204✔
1379
    // AddColumn on an erased table handled by nesting.
72✔
1380

72✔
1381
    if (left.type == Instruction::Payload::Type::Link && same_string(left.link_target_table, right.table)) {
204!
1382
        // Erase of a table where the left side adds a link column targeting it.
1383
        Instruction::EraseColumn erase_column;
×
1384
        erase_column.table = right_side.adopt_string(left_side, left.table);
×
1385
        erase_column.field = right_side.adopt_string(left_side, left.field);
×
1386
        right_side.prepend(erase_column);
×
1387
        left_side.discard();
×
1388
    }
×
1389
}
204✔
1390

1391
// Handled by nested rule.
1392
DEFINE_MERGE_NOOP(Instruction::EraseColumn, Instruction::EraseTable);
1393
DEFINE_MERGE_NOOP(Instruction::ArrayInsert, Instruction::EraseTable);
1394
DEFINE_MERGE_NOOP(Instruction::ArrayMove, Instruction::EraseTable);
1395
DEFINE_MERGE_NOOP(Instruction::ArrayErase, Instruction::EraseTable);
1396
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::EraseTable);
1397
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::EraseTable);
1398
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::EraseTable);
1399

1400

1401
/// CreateObject rules
1402

1403
// CreateObject cannot interfere with instructions that have a longer path.
1404
DEFINE_NESTED_MERGE_NOOP(Instruction::CreateObject);
1405

1406
// CreateObject is idempotent.
1407
DEFINE_MERGE_NOOP(Instruction::CreateObject, Instruction::CreateObject);
1408

1409
DEFINE_MERGE(Instruction::EraseObject, Instruction::CreateObject)
1410
{
11,352✔
1411
    if (same_object(left, right)) {
11,352✔
1412
        // CONFLICT: Create and Erase of the same object.
382✔
1413
        //
382✔
1414
        // RESOLUTION: Erase always wins.
382✔
1415
        right_side.discard();
708✔
1416
    }
708✔
1417
}
11,352✔
1418

1419
DEFINE_MERGE_NOOP(Instruction::Update, Instruction::CreateObject);
1420
DEFINE_MERGE_NOOP(Instruction::AddInteger, Instruction::CreateObject);
1421
DEFINE_MERGE_NOOP(Instruction::AddColumn, Instruction::CreateObject);
1422
DEFINE_MERGE_NOOP(Instruction::EraseColumn, Instruction::CreateObject);
1423
DEFINE_MERGE_NOOP(Instruction::ArrayInsert, Instruction::CreateObject);
1424
DEFINE_MERGE_NOOP(Instruction::ArrayMove, Instruction::CreateObject);
1425
DEFINE_MERGE_NOOP(Instruction::ArrayErase, Instruction::CreateObject);
1426
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::CreateObject);
1427
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::CreateObject);
1428
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::CreateObject);
1429

1430

1431
/// Erase rules
1432

1433
DEFINE_NESTED_MERGE(Instruction::EraseObject)
1434
{
8,314✔
1435
    if (is_prefix_of(outer, inner)) {
8,314!
1436
        // Erase always wins.
442✔
1437
        inner_side.discard();
976✔
1438
    }
976✔
1439
}
8,314✔
1440

1441
DEFINE_MERGE(Instruction::EraseObject, Instruction::EraseObject)
1442
{
4,046✔
1443
    if (same_object(left, right)) {
4,046✔
1444
        // We keep the most recent erase. This prevents the situation where a
348✔
1445
        // high number of EraseObject instructions in the past trumps a
348✔
1446
        // Erase-Create pair in the future.
348✔
1447
        if (right_side.timestamp() < left_side.timestamp()) {
672✔
1448
            right_side.discard();
336✔
1449
        }
336✔
1450
        else {
336✔
1451
            left_side.discard();
336✔
1452
        }
336✔
1453
    }
672✔
1454
}
4,046✔
1455

1456
// Handled by nested merge.
1457
DEFINE_MERGE_NOOP(Instruction::Update, Instruction::EraseObject);
1458
DEFINE_MERGE_NOOP(Instruction::AddInteger, Instruction::EraseObject);
1459
DEFINE_MERGE_NOOP(Instruction::AddColumn, Instruction::EraseObject);
1460
DEFINE_MERGE_NOOP(Instruction::EraseColumn, Instruction::EraseObject);
1461
DEFINE_MERGE_NOOP(Instruction::ArrayInsert, Instruction::EraseObject);
1462
DEFINE_MERGE_NOOP(Instruction::ArrayMove, Instruction::EraseObject);
1463
DEFINE_MERGE_NOOP(Instruction::ArrayErase, Instruction::EraseObject);
1464
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::EraseObject);
1465
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::EraseObject);
1466
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::EraseObject);
1467

1468

1469
/// Set rules
1470

1471
DEFINE_NESTED_MERGE(Instruction::Update)
1472
{
2,168✔
1473
    using Type = Instruction::Payload::Type;
2,168✔
1474

884✔
1475
    if (outer.value.type == Type::ObjectValue) {
2,168!
1476
        // Creating an embedded object is an idempotent operation, and should
32✔
1477
        // not eliminate updates to the subtree.
32✔
1478
        return;
64✔
1479
    }
64✔
1480

852✔
1481
    // Setting a value higher up in the hierarchy overwrites any modification to
852✔
1482
    // the inner value, regardless of when this happened.
852✔
1483
    if (auto next_element = is_prefix_of(outer, inner)) {
2,104!
1484
        //  If this is a collection in mixed, we will allow the inner instruction
324✔
1485
        //  to pass so long as it references the proper type (list or dictionary).
324✔
1486
        if (outer.value.type == Type::List && mpark::holds_alternative<uint32_t>(*next_element)) {
648!
1487
            return;
200✔
1488
        }
200✔
1489
        else if (outer.value.type == Type::Dictionary && mpark::holds_alternative<InternString>(*next_element)) {
448!
1490
            return;
128✔
1491
        }
128✔
1492
        inner_side.discard();
320✔
1493
    }
320✔
1494
}
2,104✔
1495

1496
DEFINE_MERGE(Instruction::Update, Instruction::Update)
1497
{
9,202✔
1498
    // The two instructions are at the same level of nesting.
5,376✔
1499

5,376✔
1500
    using Type = Instruction::Payload::Type;
9,202✔
1501

5,376✔
1502
    if (same_path(left, right)) {
9,202✔
1503
        bool left_is_default = false;
7,818✔
1504
        bool right_is_default = false;
7,818✔
1505
        if (!(left.is_array_update() == right.is_array_update())) {
7,818✔
1506
            bad_merge(left_side, left, "Merge error: left.is_array_update() == right.is_array_update()");
×
1507
        }
×
1508

4,848✔
1509
        if (!left.is_array_update()) {
7,818✔
1510
            if (right.is_array_update()) {
7,778✔
1511
                bad_merge(right_side, right, "Merge error: !right.is_array_update()");
×
1512
            }
×
1513
            left_is_default = left.is_default;
7,778✔
1514
            right_is_default = right.is_default;
7,778✔
1515
        }
7,778✔
1516
        else if (!(left.prior_size == right.prior_size)) {
40✔
1517
            bad_merge(left_side, left, "Merge error: left.prior_size == right.prior_size");
×
1518
        }
×
1519

4,848✔
1520
        if (left.value.type != right.value.type) {
7,818✔
1521
            // Embedded object creation should always lose to an Update(value),
96✔
1522
            // because these structures are nested, and we need to discard any
96✔
1523
            // update inside the structure.
96✔
1524
            if (left.value.type == Type::ObjectValue) {
196✔
1525
                left_side.discard();
24✔
1526
                return;
24✔
1527
            }
24✔
1528
            else if (right.value.type == Type::ObjectValue) {
172✔
1529
                right_side.discard();
24✔
1530
                return;
24✔
1531
            }
24✔
1532
        }
7,770✔
1533

4,824✔
1534
        // Updates to List or Dictionary are idempotent. If both sides are setting to the same value,
4,824✔
1535
        // let them both pass through. It is important that the instruction application rules reflect this.
4,824✔
1536
        // If it is not two lists or dictionaries, then the normal last-writer-wins rules will take effect below.
4,824✔
1537
        if (left.value.type == right.value.type &&
7,770✔
1538
            (left.value.type == Type::List || left.value.type == Type::Dictionary)) {
7,694✔
1539
            return;
120✔
1540
        }
120✔
1541

4,764✔
1542
        // CONFLICT: Two updates of the same element.
4,764✔
1543
        //
4,764✔
1544
        // RESOLUTION: Suppress the effect of the UPDATE operation with the lower
4,764✔
1545
        // timestamp. Note that the timestamps can never be equal. This is
4,764✔
1546
        // achieved on both sides by discarding the received UPDATE operation if
4,764✔
1547
        // it has a lower timestamp than the previously applied UPDATE operation.
4,764✔
1548
        if (left_is_default == right_is_default) {
7,650✔
1549
            if (left_side.timestamp() < right_side.timestamp()) {
7,590✔
1550
                left_side.discard(); // --->
3,974✔
1551
            }
3,974✔
1552
            else {
3,616✔
1553
                right_side.discard(); // <---
3,616✔
1554
            }
3,616✔
1555
        }
7,590✔
1556
        else {
60✔
1557
            if (left_is_default) {
60✔
1558
                left_side.discard();
36✔
1559
            }
36✔
1560
            else {
24✔
1561
                right_side.discard();
24✔
1562
            }
24✔
1563
        }
60✔
1564
    }
7,650✔
1565
}
9,202✔
1566

1567
DEFINE_MERGE(Instruction::AddInteger, Instruction::Update)
1568
{
358✔
1569
    // The two instructions are at the same level of nesting.
172✔
1570

172✔
1571
    if (same_path(left, right)) {
358✔
1572
        // CONFLICT: Add vs Set of the same element.
160✔
1573
        //
160✔
1574
        // RESOLUTION: If the Add was later than the Set, add its value to
160✔
1575
        // the payload of the Set instruction. Otherwise, discard it.
160✔
1576

160✔
1577
        bool right_is_default = !right.is_array_update() && right.is_default;
328✔
1578

160✔
1579
        // Five Cases Here:
160✔
1580
        // 1. AddInteger is after Update and Update is of a non-integer type
160✔
1581
        //     - Discard the AddInteger; AddInteger to a mixed field is a no-op
160✔
1582
        // 2: AddInteger is after the Update and the Update instruction contains an integer payload:
160✔
1583
        //     - We increment the Update instruction payload
160✔
1584
        // 3: AddInteger is after Update and Update is null:
160✔
1585
        //     - No conflict
160✔
1586
        // 4: Update is after AddInteger and Update.default is false
160✔
1587
        //     - Discard the AddInteger
160✔
1588
        // 5: Update is after AddInteger and Update.default is true
160✔
1589
        //     - Treat the Update as if it were before the AddInteger instruction
160✔
1590

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

112✔
1600
            // The AddInteger happened after an Update with a non int type
112✔
1601
            // This must be operating on a mixed field. Discard the AddInteger
112✔
1602
            if (right.value.type != Instruction::Payload::Type::Int) {
228✔
1603
                left_side.discard();
8✔
1604
                return;
8✔
1605
            }
8✔
1606

108✔
1607
            // Wrapping add
108✔
1608
            uint64_t ua = uint64_t(right.value.data.integer);
220✔
1609
            uint64_t ub = uint64_t(left.value);
220✔
1610
            right.value.data.integer = int64_t(ua + ub);
220✔
1611
        }
220✔
1612
        else {
44✔
1613
            left_side.discard();
44✔
1614
        }
44✔
1615
    }
328✔
1616
}
358✔
1617

1618
DEFINE_MERGE_NOOP(Instruction::AddColumn, Instruction::Update);
1619

1620
DEFINE_MERGE(Instruction::EraseColumn, Instruction::Update)
1621
{
×
1622
    if (same_column(left, right)) {
×
1623
        right_side.discard();
×
1624
    }
×
1625
}
×
1626

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

1647
DEFINE_MERGE(Instruction::ArrayMove, Instruction::Update)
1648
{
24✔
1649
    if (same_container(left, right)) {
24✔
1650
        REALM_ASSERT(right.is_array_update());
×
1651

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

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

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

148✔
1678
        // CONFLICT: Update of a removed element.
148✔
1679
        //
148✔
1680
        // RESOLUTION: Discard the UPDATE operation received on the right side.
148✔
1681
        right.prior_size -= 1;
244✔
1682

148✔
1683
        if (left.index() == right.index()) {
244✔
1684
            // CONFLICT: Update of a removed element.
12✔
1685
            //
12✔
1686
            // RESOLUTION: Discard the UPDATE operation received on the right side.
12✔
1687
            right_side.discard();
24✔
1688
        }
24✔
1689
        else if (right.index() > left.index()) {
220✔
1690
            right.index() -= 1;
80✔
1691
        }
80✔
1692
    }
244✔
1693
}
502✔
1694

1695
DEFINE_MERGE(Instruction::Clear, Instruction::Update)
1696
{
160✔
1697
    using Type = Instruction::Payload::Type;
160✔
1698

80✔
1699
    // The two instructions are at the same level of nesting.
80✔
1700
    if (same_path(left, right)) {
160✔
1701
        // TODO: We could make it so a Clear instruction does not win against setting a property or
56✔
1702
        // collection item to a different collection.
56✔
1703
        if (right.value.type != Type::List && right.value.type != Type::Dictionary) {
112✔
1704
            left_side.discard();
16✔
1705
        }
16✔
1706
    }
112✔
1707
}
160✔
1708

1709
// Handled by nested rule
1710
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::Update);
1711
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::Update);
1712

1713

1714
/// AddInteger rules
1715

1716
DEFINE_NESTED_MERGE(Instruction::AddInteger)
1717
{
116✔
1718
    if (is_prefix_of(outer, inner)) {
116!
1719
        inner_side.discard();
×
1720
    }
×
1721
}
116✔
1722

1723
DEFINE_MERGE_NOOP(Instruction::AddInteger, Instruction::AddInteger);
1724
DEFINE_MERGE_NOOP(Instruction::AddColumn, Instruction::AddInteger);
1725
DEFINE_MERGE_NOOP(Instruction::EraseColumn, Instruction::AddInteger);
1726
DEFINE_MERGE_NOOP(Instruction::ArrayInsert, Instruction::AddInteger);
1727
DEFINE_MERGE_NOOP(Instruction::ArrayMove, Instruction::AddInteger);
1728
DEFINE_MERGE_NOOP(Instruction::ArrayErase, Instruction::AddInteger);
1729
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::AddInteger);
1730
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::AddInteger);
1731
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::AddInteger);
1732

1733

1734
/// AddColumn rules
1735

1736
DEFINE_NESTED_MERGE_NOOP(Instruction::AddColumn);
1737

1738
DEFINE_MERGE(Instruction::AddColumn, Instruction::AddColumn)
1739
{
13,896✔
1740
    if (same_column(left, right)) {
13,896✔
1741
        StringData left_name = left_side.get_string(left.field);
9,376✔
1742
        if (left.type != right.type) {
9,376✔
1743
            bad_merge(
6✔
1744
                "Schema mismatch: Property '%1' in class '%2' is of type %3 on one side and type %4 on the other.",
6✔
1745
                left_name, left_side.get_string(left.table), get_type_name(left.type), get_type_name(right.type));
6✔
1746
        }
6✔
1747

4,662✔
1748
        if (left.nullable != right.nullable) {
9,376✔
1749
            bad_merge("Schema mismatch: Property '%1' in class '%2' is nullable on one side and not on the other.",
6✔
1750
                      left_name, left_side.get_string(left.table));
6✔
1751
        }
6✔
1752

4,662✔
1753
        if (left.collection_type != right.collection_type) {
9,376✔
1754
            auto collection_type_name = [](Instruction::AddColumn::CollectionType type) -> const char* {
×
1755
                switch (type) {
×
1756
                    case Instruction::AddColumn::CollectionType::Single:
×
1757
                        return "single value";
×
1758
                    case Instruction::AddColumn::CollectionType::List:
×
1759
                        return "list";
×
1760
                    case Instruction::AddColumn::CollectionType::Dictionary:
×
1761
                        return "dictionary";
×
1762
                    case Instruction::AddColumn::CollectionType::Set:
×
1763
                        return "set";
×
1764
                }
×
1765
                REALM_TERMINATE("");
1766
            };
×
1767

1768
            std::stringstream ss;
×
1769
            const char* left_type = collection_type_name(left.collection_type);
×
1770
            const char* right_type = collection_type_name(right.collection_type);
×
1771
            bad_merge("Schema mismatch: Property '%1' in class '%2' is a %3 on one side, and a %4 on the other.",
×
1772
                      left_name, left_side.get_string(left.table), left_type, right_type);
×
1773
        }
×
1774

4,662✔
1775
        if (left.type == Instruction::Payload::Type::Link) {
9,376✔
1776
            StringData left_target = left_side.get_string(left.link_target_table);
2,064✔
1777
            StringData right_target = right_side.get_string(right.link_target_table);
2,064✔
1778
            if (left_target != right_target) {
2,064✔
1779
                bad_merge("Schema mismatch: Link property '%1' in class '%2' points to class '%3' on one side and to "
8✔
1780
                          "'%4' on the other.",
8✔
1781
                          left_name, left_side.get_string(left.table), left_target, right_target);
8✔
1782
            }
8✔
1783
        }
2,064✔
1784

4,662✔
1785
        // Name, type, nullability and link targets match -- discard both
4,662✔
1786
        // sides and proceed.
4,662✔
1787
        left_side.discard();
9,376✔
1788
        right_side.discard();
9,376✔
1789
    }
9,376✔
1790
}
13,896✔
1791

1792
DEFINE_MERGE(Instruction::EraseColumn, Instruction::AddColumn)
1793
{
×
1794
    if (same_column(left, right)) {
×
1795
        right_side.discard();
×
1796
    }
×
1797
}
×
1798

1799
DEFINE_MERGE_NOOP(Instruction::ArrayInsert, Instruction::AddColumn);
1800
DEFINE_MERGE_NOOP(Instruction::ArrayMove, Instruction::AddColumn);
1801
DEFINE_MERGE_NOOP(Instruction::ArrayErase, Instruction::AddColumn);
1802
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::AddColumn);
1803
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::AddColumn);
1804
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::AddColumn);
1805

1806

1807
/// EraseColumn rules
1808

1809
DEFINE_NESTED_MERGE_NOOP(Instruction::EraseColumn);
1810

1811
DEFINE_MERGE(Instruction::EraseColumn, Instruction::EraseColumn)
1812
{
×
1813
    if (same_column(left, right)) {
×
1814
        left_side.discard();
×
1815
        right_side.discard();
×
1816
    }
×
1817
}
×
1818

1819
DEFINE_MERGE_NOOP(Instruction::ArrayInsert, Instruction::EraseColumn);
1820
DEFINE_MERGE_NOOP(Instruction::ArrayMove, Instruction::EraseColumn);
1821
DEFINE_MERGE_NOOP(Instruction::ArrayErase, Instruction::EraseColumn);
1822
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::EraseColumn);
1823
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::EraseColumn);
1824
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::EraseColumn);
1825

1826
/// ArrayInsert rules
1827

1828
DEFINE_NESTED_MERGE(Instruction::ArrayInsert)
1829
{
24✔
1830
    if (is_container_prefix_of(outer, inner)) {
24!
1831
        auto& index = corresponding_index_in_path(outer, inner);
24✔
1832
        if (index >= outer.index()) {
24!
1833
            index += 1;
16✔
1834
        }
16✔
1835
    }
24✔
1836
}
24✔
1837

1838
DEFINE_MERGE(Instruction::ArrayInsert, Instruction::ArrayInsert)
1839
{
3,004✔
1840
    if (same_container(left, right)) {
3,004✔
1841
        if (!(left.prior_size == right.prior_size)) {
1,488✔
1842
            bad_merge(right_side, right, "Exception: left.prior_size == right.prior_size");
×
1843
        }
×
1844
        left.prior_size++;
1,488✔
1845
        right.prior_size++;
1,488✔
1846

942✔
1847
        if (left.index() > right.index()) {
1,488✔
1848
            left.index() += 1; // --->
368✔
1849
        }
368✔
1850
        else if (left.index() < right.index()) {
1,120✔
1851
            right.index() += 1; // <---
372✔
1852
        }
372✔
1853
        else { // left index == right index
748✔
1854
            // CONFLICT: Two element insertions at the same position.
492✔
1855
            //
492✔
1856
            // Resolution: Place the inserted elements in order of increasing
492✔
1857
            // timestamp. Note that the timestamps can never be equal.
492✔
1858
            if (left_side.timestamp() < right_side.timestamp()) {
748✔
1859
                right.index() += 1;
382✔
1860
            }
382✔
1861
            else {
366✔
1862
                left.index() += 1;
366✔
1863
            }
366✔
1864
        }
748✔
1865
    }
1,488✔
1866
}
3,004✔
1867

1868
DEFINE_MERGE(Instruction::ArrayMove, Instruction::ArrayInsert)
1869
{
×
1870
    if (same_container(left, right)) {
×
1871
        left.prior_size += 1;
×
1872

1873
        // Left insertion vs right removal
1874
        if (right.index() > left.index()) {
×
1875
            right.index() -= 1; // --->
×
1876
        }
×
1877
        else {
×
1878
            left.index() += 1; // <---
×
1879
        }
×
1880

1881
        // Left insertion vs left insertion
1882
        if (right.index() < left.ndx_2) {
×
1883
            left.ndx_2 += 1; // <---
×
1884
        }
×
1885
        else if (right.index() > left.ndx_2) {
×
1886
            right.index() += 1; // --->
×
1887
        }
×
1888
        else { // right.index() == left.ndx_2
×
1889
            // CONFLICT: Insertion and movement to same position.
1890
            //
1891
            // RESOLUTION: Place the two elements in order of increasing
1892
            // timestamp. Note that the timestamps can never be equal.
1893
            if (left_side.timestamp() < right_side.timestamp()) {
×
1894
                left.ndx_2 += 1; // <---
×
1895
            }
×
1896
            else {
×
1897
                right.index() += 1; // --->
×
1898
            }
×
1899
        }
×
1900
    }
×
1901
}
×
1902

1903
DEFINE_MERGE(Instruction::ArrayErase, Instruction::ArrayInsert)
1904
{
1,220✔
1905
    if (same_container(left, right)) {
1,220✔
1906
        if (!(left.prior_size == right.prior_size)) {
644✔
1907
            bad_merge(left_side, left, "Merge error: left.prior_size == right.prior_size");
×
1908
        }
×
1909
        if (!(left.index() < left.prior_size)) {
644✔
1910
            bad_merge(left_side, left, "Merge error: left.index() < left.prior_size");
×
1911
        }
×
1912
        if (!(right.index() <= right.prior_size)) {
644✔
1913
            bad_merge(left_side, left, "Merge error: right.index() <= right.prior_size");
×
1914
        }
×
1915

446✔
1916
        left.prior_size++;
644✔
1917
        right.prior_size--;
644✔
1918
        if (right.index() <= left.index()) {
644✔
1919
            left.index() += 1; // --->
344✔
1920
        }
344✔
1921
        else {
300✔
1922
            right.index() -= 1; // <---
300✔
1923
        }
300✔
1924
    }
644✔
1925
}
1,220✔
1926

1927
// Handled by nested rules
1928
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::ArrayInsert);
1929
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::ArrayInsert);
1930
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::ArrayInsert);
1931

1932

1933
/// ArrayMove rules
1934

1935
DEFINE_NESTED_MERGE(Instruction::ArrayMove)
1936
{
×
1937
    if (is_container_prefix_of(outer, inner)) {
×
1938
        auto& index = corresponding_index_in_path(outer, inner);
×
1939
        merge_get_vs_move(outer.index(), index, outer.ndx_2);
×
1940
    }
×
1941
}
×
1942

1943
DEFINE_MERGE(Instruction::ArrayMove, Instruction::ArrayMove)
1944
{
28✔
1945
    if (same_container(left, right)) {
28✔
1946
        if (!(left.prior_size == right.prior_size)) {
28✔
1947
            bad_merge(left_side, left, "Merge error: left.prior_size == right.prior_size");
×
1948
        }
×
1949
        if (!(left.index() < left.prior_size)) {
28✔
1950
            bad_merge(left_side, left, "Merge error: left.index() < left.prior_size");
×
1951
        }
×
1952
        if (!(right.index() < right.prior_size)) {
28✔
1953
            bad_merge(right_side, right, "Merge error: right.index() < right.prior_size");
×
1954
        }
×
1955
        if (!(left.ndx_2 < left.prior_size)) {
28✔
1956
            bad_merge(left_side, left, "Merge error: left.ndx_2 < left.prior_size");
×
1957
        }
×
1958
        if (!(right.ndx_2 < right.prior_size)) {
28✔
1959
            bad_merge(right_side, right, "Merge error: right.ndx_2 < right.prior_size");
×
1960
        }
×
1961

14✔
1962
        if (left.index() < right.index()) {
28✔
1963
            right.index() -= 1; // <---
4✔
1964
        }
4✔
1965
        else if (left.index() > right.index()) {
24✔
1966
            left.index() -= 1; // --->
4✔
1967
        }
4✔
1968
        else {
20✔
1969
            // CONFLICT: Two movements of same element.
10✔
1970
            //
10✔
1971
            // RESOLUTION: Respect the MOVE operation associated with the higher
10✔
1972
            // timestamp. If the previously applied MOVE operation has the higher
10✔
1973
            // timestamp, discard the received MOVE operation, otherwise use the
10✔
1974
            // previously applied MOVE operation to transform the received MOVE
10✔
1975
            // operation. Note that the timestamps are never equal.
10✔
1976
            if (left_side.timestamp() < right_side.timestamp()) {
20✔
1977
                right.index() = left.ndx_2; // <---
12✔
1978
                left_side.discard();        // --->
12✔
1979
                if (right.index() == right.ndx_2) {
12✔
1980
                    right_side.discard(); // <---
×
1981
                }
×
1982
            }
12✔
1983
            else {
8✔
1984
                left.index() = right.ndx_2; // --->
8✔
1985
                if (left.index() == left.ndx_2) {
8✔
1986
                    left_side.discard(); // --->
×
1987
                }
×
1988
                right_side.discard(); // <---
8✔
1989
            }
8✔
1990
            return;
20✔
1991
        }
20✔
1992

4✔
1993
        // Left insertion vs right removal
4✔
1994
        if (left.ndx_2 > right.index()) {
8✔
1995
            left.ndx_2 -= 1; // --->
4✔
1996
        }
4✔
1997
        else {
4✔
1998
            right.index() += 1; // <---
4✔
1999
        }
4✔
2000

4✔
2001
        // Left removal vs right insertion
4✔
2002
        if (left.index() < right.ndx_2) {
8✔
2003
            right.ndx_2 -= 1; // <---
4✔
2004
        }
4✔
2005
        else {
4✔
2006
            left.index() += 1; // --->
4✔
2007
        }
4✔
2008

4✔
2009
        // Left insertion vs right insertion
4✔
2010
        if (left.ndx_2 < right.ndx_2) {
8✔
2011
            right.ndx_2 += 1; // <---
4✔
2012
        }
4✔
2013
        else if (left.ndx_2 > right.ndx_2) {
4✔
2014
            left.ndx_2 += 1; // --->
4✔
2015
        }
4✔
2016
        else { // left.ndx_2 == right.ndx_2
×
2017
            // CONFLICT: Two elements moved to the same position
2018
            //
2019
            // RESOLUTION: Place the moved elements in order of increasing
2020
            // timestamp. Note that the timestamps can never be equal.
2021
            if (left_side.timestamp() < right_side.timestamp()) {
×
2022
                right.ndx_2 += 1; // <---
×
2023
            }
×
2024
            else {
×
2025
                left.ndx_2 += 1; // --->
×
2026
            }
×
2027
        }
×
2028

4✔
2029
        if (left.index() == left.ndx_2) {
8✔
2030
            left_side.discard(); // --->
×
2031
        }
×
2032
        if (right.index() == right.ndx_2) {
8✔
2033
            right_side.discard(); // <---
×
2034
        }
×
2035
    }
8✔
2036
}
28✔
2037

2038
DEFINE_MERGE(Instruction::ArrayErase, Instruction::ArrayMove)
2039
{
×
2040
    if (same_container(left, right)) {
×
2041
        if (!(left.prior_size == right.prior_size)) {
×
2042
            bad_merge(left_side, left, "Merge error: left.prior_size == right.prior_size");
×
2043
        }
×
2044
        if (!(left.index() < left.prior_size)) {
×
2045
            bad_merge(left_side, left, "Merge error: left.index() < left.prior_size");
×
2046
        }
×
2047
        if (!(right.index() < right.prior_size)) {
×
2048
            bad_merge(right_side, right, "Merge error: right.index() < right.prior_size");
×
2049
        }
×
2050

2051
        right.prior_size -= 1;
×
2052

2053
        if (left.index() == right.index()) {
×
2054
            // CONFLICT: Removal of a moved element.
2055
            //
2056
            // RESOLUTION: Discard the received MOVE operation on the left side, and
2057
            // use the previously applied MOVE operation to transform the received
2058
            // REMOVE operation on the right side.
2059
            left.index() = right.ndx_2; // --->
×
2060
            right_side.discard();       // <---
×
2061
        }
×
2062
        else {
×
2063
            // Left removal vs right removal
2064
            if (left.index() > right.index()) {
×
2065
                left.index() -= 1; // --->
×
2066
            }
×
2067
            else {                  // left.index() < right.index()
×
2068
                right.index() -= 1; // <---
×
2069
            }
×
2070
            // Left removal vs right insertion
2071
            if (left.index() >= right.ndx_2) {
×
2072
                left.index() += 1; // --->
×
2073
            }
×
2074
            else {
×
2075
                right.ndx_2 -= 1; // <---
×
2076
            }
×
2077

2078
            if (right.index() == right.ndx_2) {
×
2079
                right_side.discard(); // <---
×
2080
            }
×
2081
        }
×
2082
    }
×
2083
}
×
2084

2085
// Handled by nested rule.
2086
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::ArrayMove);
2087
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::ArrayMove);
2088
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::ArrayMove);
2089

2090

2091
/// ArrayErase rules
2092

2093
DEFINE_NESTED_MERGE(Instruction::ArrayErase)
2094
{
8✔
2095
    if (is_prefix_of(outer, inner)) {
8!
2096
        // Erase of subtree - inner instruction touches the subtree.
2097
        inner_side.discard();
×
2098
    }
×
2099
    else if (is_container_prefix_of(outer, inner)) {
8!
2100
        // Erase of a sibling element in the container - adjust the path.
4✔
2101
        auto& index = corresponding_index_in_path(outer, inner);
8✔
2102
        if (outer.index() < index) {
8!
2103
            index -= 1;
8✔
2104
        }
8✔
2105
        else {
×
2106
            REALM_ASSERT(index != outer.index());
×
2107
        }
×
2108
    }
8✔
2109
}
8✔
2110

2111
DEFINE_MERGE(Instruction::ArrayErase, Instruction::ArrayErase)
2112
{
132✔
2113
    if (same_container(left, right)) {
132✔
2114
        if (!(left.prior_size == right.prior_size)) {
68✔
2115
            bad_merge(left_side, left, "Merge error: left.prior_size == right.prior_size");
×
2116
        }
×
2117
        if (!(left.index() < left.prior_size)) {
68✔
2118
            bad_merge(left_side, left, "Merge error: left.index() < left.prior_size");
×
2119
        }
×
2120
        if (!(right.index() < right.prior_size)) {
68✔
2121
            bad_merge(right_side, right, "Merge error: right.index() < right.prior_size");
×
2122
        }
×
2123

50✔
2124
        left.prior_size -= 1;
68✔
2125
        right.prior_size -= 1;
68✔
2126

50✔
2127
        if (left.index() > right.index()) {
68✔
2128
            left.index() -= 1; // --->
28✔
2129
        }
28✔
2130
        else if (left.index() < right.index()) {
40✔
2131
            right.index() -= 1; // <---
32✔
2132
        }
32✔
2133
        else { // left.index() == right.index()
8✔
2134
            // CONFLICT: Two removals of the same element.
4✔
2135
            //
4✔
2136
            // RESOLUTION: On each side, discard the received REMOVE operation.
4✔
2137
            left_side.discard();  // --->
8✔
2138
            right_side.discard(); // <---
8✔
2139
        }
8✔
2140
    }
68✔
2141
}
132✔
2142

2143
// Handled by nested rules.
2144
DEFINE_MERGE_NOOP(Instruction::Clear, Instruction::ArrayErase);
2145
DEFINE_MERGE_NOOP(Instruction::SetInsert, Instruction::ArrayErase);
2146
DEFINE_MERGE_NOOP(Instruction::SetErase, Instruction::ArrayErase);
2147

2148

2149
/// Clear rules
2150

2151
DEFINE_NESTED_MERGE(Instruction::Clear)
2152
{
144✔
2153
    // Note: Clear instructions do not have an index in their path.
72✔
2154
    if (is_prefix_of(outer, inner)) {
144!
2155
        inner_side.discard();
136✔
2156
    }
136✔
2157
}
144✔
2158

2159
DEFINE_MERGE(Instruction::Clear, Instruction::Clear)
2160
{
16✔
2161
    if (same_path(left, right)) {
16✔
2162
        // CONFLICT: Two clears of the same container.
8✔
2163
        //
8✔
2164
        // RESOLUTION: Discard the clear with the lower timestamp. This has the
8✔
2165
        // effect of preserving insertions that came after the clear from the
8✔
2166
        // side that has the higher timestamp.
8✔
2167
        if (left_side.timestamp() < right_side.timestamp()) {
16✔
2168
            left_side.discard();
12✔
2169
        }
12✔
2170
        else {
4✔
2171
            right_side.discard();
4✔
2172
        }
4✔
2173
    }
16✔
2174
}
16✔
2175

2176
DEFINE_MERGE(Instruction::SetInsert, Instruction::Clear)
2177
{
8✔
2178
    if (same_path(left, right)) {
8!
2179
        left_side.discard();
4✔
2180
    }
4✔
2181
}
8✔
2182

2183
DEFINE_MERGE(Instruction::SetErase, Instruction::Clear)
2184
{
8✔
2185
    if (same_path(left, right)) {
8!
2186
        left_side.discard();
4✔
2187
    }
4✔
2188
}
8✔
2189

2190

2191
/// SetInsert rules
2192

2193
DEFINE_NESTED_MERGE_NOOP(Instruction::SetInsert);
2194

2195
DEFINE_MERGE(Instruction::SetInsert, Instruction::SetInsert)
2196
{
156✔
2197
    if (same_path(left, right)) {
156✔
2198
        // CONFLICT: Two inserts into the same set.
76✔
2199
        //
76✔
2200
        // RESOLUTION: If the values are the same, discard the insertion with the lower timestamp. Otherwise,
76✔
2201
        // do nothing.
76✔
2202
        //
76✔
2203
        // NOTE: Set insertion is idempotent. Keeping the instruction with the higher timestamp is necessary
76✔
2204
        // because we want to maintain associativity in the case where intermittent erases (as ordered by
76✔
2205
        // timestamp) arrive at a later point in time.
76✔
2206
        if (same_payload(left.value, right.value)) {
152✔
2207
            if (left_side.timestamp() < right_side.timestamp()) {
24✔
2208
                left_side.discard();
12✔
2209
            }
12✔
2210
            else {
12✔
2211
                right_side.discard();
12✔
2212
            }
12✔
2213
        }
24✔
2214
    }
152✔
2215
}
156✔
2216

2217
DEFINE_MERGE(Instruction::SetErase, Instruction::SetInsert)
2218
{
64✔
2219
    if (same_path(left, right)) {
64✔
2220
        // CONFLICT: Insertion and erase in the same set.
32✔
2221
        //
32✔
2222
        // RESOLUTION: If the inserted/erased values are the same, discard the instruction with the lower
32✔
2223
        // timestamp. Otherwise, do nothing.
32✔
2224
        //
32✔
2225
        // Note: Set insertion and erase are both idempotent. Keeping the instruction with the higher
32✔
2226
        // timestamp is necessary because we want to maintain associativity.
32✔
2227
        if (same_payload(left.value, right.value)) {
64✔
UNCOV
2228
            if (left_side.timestamp() < right_side.timestamp()) {
×
UNCOV
2229
                left_side.discard();
×
UNCOV
2230
            }
×
2231
            else {
×
2232
                right_side.discard();
×
2233
            }
×
UNCOV
2234
        }
×
2235
    }
64✔
2236
}
64✔
2237

2238

2239
/// SetErase rules.
2240

2241
DEFINE_NESTED_MERGE_NOOP(Instruction::SetErase);
2242

2243
DEFINE_MERGE(Instruction::SetErase, Instruction::SetErase)
2244
{
×
2245
    if (same_path(left, right)) {
×
2246
        // CONFLICT: Two erases in the same set.
2247
        //
2248
        // RESOLUTION: If the values are the same, discard the instruction with the lower timestamp.
2249
        // Otherwise, do nothing.
2250
        if (left.value == right.value) {
×
2251
            if (left_side.timestamp() < right_side.timestamp()) {
×
2252
                left_side.discard();
×
2253
            }
×
2254
            else {
×
2255
                right_side.discard();
×
2256
            }
×
2257
        }
×
2258
    }
×
2259
}
×
2260

2261

2262
///
2263
/// END OF MERGE RULES!
2264
///
2265

2266
template <class Left, class Right>
2267
void merge_instructions_2(Left& left, Right& right, MajorSide& left_side, MinorSide& right_side)
2268
{
263,568✔
2269
    Merge<Left, Right>::merge(left, right, left_side, right_side);
263,568✔
2270
}
263,568✔
2271

2272
template <class Outer, class Inner, class OuterSide, class InnerSide>
2273
void merge_nested_2(Outer& outer, Inner& inner, OuterSide& outer_side, InnerSide& inner_side)
2274
{
44,174✔
2275
    MergeNested<Outer>::merge(outer, inner, outer_side, inner_side);
44,174✔
2276
}
44,174✔
2277

2278
template <class OuterSide, class InnerSide>
2279
void merge_nested(OuterSide& outer_side, InnerSide& inner_side)
2280
{
44,182✔
2281
    outer_side.get().visit([&](auto& outer) {
44,182✔
2282
        inner_side.get().visit([&](auto& inner) {
44,178✔
2283
            merge_nested_2(outer, inner, outer_side, inner_side);
44,174✔
2284
        });
44,174✔
2285
    });
44,178✔
2286
}
44,182✔
2287

2288
void TransformerImpl::merge_instructions(MajorSide& their_side, MinorSide& our_side)
2289
{
265,970✔
2290
    // FIXME: Find a way to avoid heap-copies of the path.
129,116✔
2291
    Instruction their_before = their_side.get();
265,970✔
2292
    Instruction our_before = our_side.get();
265,970✔
2293

129,116✔
2294
    if (their_side.get().get_if<Instruction::Update>()) {
265,970✔
2295
        REALM_ASSERT(their_side.m_path_len > 2);
17,692✔
2296
    }
17,692✔
2297
    if (our_side.get().get_if<Instruction::Update>()) {
265,970✔
2298
        REALM_ASSERT(our_side.m_path_len > 2);
19,114✔
2299
    }
19,114✔
2300
    if (their_side.get().get_if<Instruction::EraseObject>()) {
265,970✔
2301
        REALM_ASSERT(their_side.m_path_len == 2);
16,264✔
2302
    }
16,264✔
2303
    if (our_side.get().get_if<Instruction::EraseObject>()) {
265,970✔
2304
        REALM_ASSERT(our_side.m_path_len == 2);
18,698✔
2305
    }
18,698✔
2306

129,116✔
2307
    // Update selections on the major side (outer loop) according to events on
129,116✔
2308
    // the minor side (inner loop). The selection may only be impacted if the
129,116✔
2309
    // instruction level is lower (i.e. at a higher point in the hierarchy).
129,116✔
2310
    if (our_side.m_path_len < their_side.m_path_len) {
265,970✔
2311
        merge_nested(our_side, their_side);
17,570✔
2312
        if (their_side.was_discarded)
17,570✔
2313
            return;
1,256✔
2314
    }
248,400✔
2315
    else if (our_side.m_path_len > their_side.m_path_len) {
248,400✔
2316
        merge_nested(their_side, our_side);
26,612✔
2317
        if (our_side.was_discarded)
26,612✔
2318
            return;
1,164✔
2319
    }
263,550✔
2320

127,994✔
2321
    if (!their_side.was_discarded && !our_side.was_discarded) {
263,550✔
2322
        // Even if the instructions were nested, we must still perform a regular
127,978✔
2323
        // merge, because link-related instructions contain information from higher
127,978✔
2324
        // levels (both rows, columns, and tables).
127,978✔
2325
        //
127,978✔
2326
        // FIXME: This condition goes away when dangling links are implemented.
127,978✔
2327
        their_side.get().visit([&](auto& their_instruction) {
263,580✔
2328
            our_side.get().visit([&](auto& our_instruction) {
263,582✔
2329
                merge_instructions_2(their_instruction, our_instruction, their_side, our_side);
263,568✔
2330
            });
263,568✔
2331
        });
263,580✔
2332
    }
263,534✔
2333

127,994✔
2334
    // Note: `left` and/or `right` may be dangling at this point due to
127,994✔
2335
    // discard/prepend. However, if they were not discarded, their iterators are
127,994✔
2336
    // required to point to an instruction of the same type.
127,994✔
2337
    if (!their_side.was_discarded && !their_side.was_replaced) {
263,550✔
2338
        const auto& their_after = their_side.get();
245,404✔
2339
        if (!(their_after == their_before)) {
245,404✔
2340
            their_side.m_changeset->set_dirty(true);
2,878✔
2341
        }
2,878✔
2342
    }
245,404✔
2343

127,994✔
2344
    if (!our_side.was_discarded && !our_side.was_replaced) {
263,550✔
2345
        const auto& our_after = our_side.get();
245,830✔
2346
        if (!(our_after == our_before)) {
245,830✔
2347
            our_side.m_changeset->set_dirty(true);
2,882✔
2348
        }
2,882✔
2349
    }
245,830✔
2350
}
263,550✔
2351

2352
} // anonymous namespace
2353

2354
namespace realm::sync {
2355
void Transformer::merge_changesets(file_ident_type local_file_ident, util::Span<Changeset> their_changesets,
2356
                                   util::Span<Changeset*> our_changesets, util::Logger& logger)
2357
{
44,532✔
2358
    REALM_ASSERT(our_changesets.size() != 0);
44,532✔
2359
    REALM_ASSERT(their_changesets.size() != 0);
44,532✔
2360
    bool trace = false;
44,532✔
2361
#if REALM_DEBUG && !REALM_UWP
44,532✔
2362
    // FIXME: Not thread-safe (use config parameter instead and confine environment reading to test/test_all.cpp)
23,972✔
2363
    const char* trace_p = ::getenv("UNITTEST_TRACE_TRANSFORM");
44,532✔
2364
    trace = (trace_p && StringData{trace_p} != "no");
44,532!
2365
    static std::mutex trace_mutex;
44,532✔
2366
    util::Optional<std::unique_lock<std::mutex>> l;
44,532✔
2367
    if (trace) {
44,532✔
2368
        l = std::unique_lock<std::mutex>{trace_mutex};
×
2369
    }
×
2370
#endif
44,532✔
2371
    TransformerImpl transformer{trace};
44,532✔
2372

23,972✔
2373
    _impl::ChangesetIndex their_index;
44,532✔
2374
    size_t their_num_instructions = 0;
44,532✔
2375
    size_t our_num_instructions = 0;
44,532✔
2376

23,972✔
2377
    // Loop through all instructions on both sides and build conflict groups.
23,972✔
2378
    // This causes the index to merge ranges that are connected by instructions
23,972✔
2379
    // on the left side, but which aren't connected on the right side.
23,972✔
2380
    // FIXME: The conflict groups can be persisted as part of the changeset to
23,972✔
2381
    // skip this step in the future.
23,972✔
2382
    for (size_t i = 0; i < their_changesets.size(); ++i) {
111,824✔
2383
        size_t num_instructions = their_changesets[i].size();
67,292✔
2384
        their_num_instructions += num_instructions;
67,292✔
2385
        logger.trace("Scanning incoming changeset [%1/%2] (%3 instructions)", i + 1, their_changesets.size(),
67,292✔
2386
                     num_instructions);
67,292✔
2387

34,860✔
2388
        their_index.scan_changeset(their_changesets[i]);
67,292✔
2389
    }
67,292✔
2390
    for (size_t i = 0; i < our_changesets.size(); ++i) {
554,506✔
2391
        Changeset& our_changeset = *our_changesets[i];
509,974✔
2392
        size_t num_instructions = our_changeset.size();
509,974✔
2393
        our_num_instructions += num_instructions;
509,974✔
2394
        logger.trace(util::LogCategory::changeset, "Scanning local changeset [%1/%2] (%3 instructions)", i + 1,
509,974✔
2395
                     our_changesets.size(), num_instructions);
509,974✔
2396

259,618✔
2397
        their_index.scan_changeset(our_changeset);
509,974✔
2398
    }
509,974✔
2399

23,972✔
2400
    // Build the index.
23,972✔
2401
    for (size_t i = 0; i < their_changesets.size(); ++i) {
111,838✔
2402
        logger.trace(util::LogCategory::changeset, "Indexing incoming changeset [%1/%2] (%3 instructions)", i + 1,
67,306✔
2403
                     their_changesets.size(), their_changesets[i].size());
67,306✔
2404
        their_index.add_changeset(their_changesets[i]);
67,306✔
2405
    }
67,306✔
2406

23,972✔
2407
    logger.debug(util::LogCategory::changeset,
44,532✔
2408
                 "Finished changeset indexing (incoming: %1 changeset(s) / %2 instructions, local: %3 "
44,532✔
2409
                 "changeset(s) / %4 instructions, conflict group(s): %5)",
44,532✔
2410
                 their_changesets.size(), their_num_instructions, our_changesets.size(), our_num_instructions,
44,532✔
2411
                 their_index.get_num_conflict_groups());
44,532✔
2412

23,972✔
2413
#if REALM_DEBUG // LCOV_EXCL_START
23,972✔
2414
    if (trace) {
44,532✔
2415
        std::cerr << TERM_YELLOW << "\n=> PEER " << std::hex << local_file_ident
×
2416
                  << " merging "
×
2417
                     "changeset(s)/from peer(s):\n";
×
2418
        for (size_t i = 0; i < their_changesets.size(); ++i) {
×
2419
            std::cerr << "Changeset version " << std::dec << their_changesets[i].version << " from peer "
×
2420
                      << their_changesets[i].origin_file_ident << " at timestamp "
×
2421
                      << their_changesets[i].origin_timestamp << "\n";
×
2422
        }
×
2423
        std::cerr << "Transforming through local changeset(s):\n";
×
2424
        for (size_t i = 0; i < our_changesets.size(); ++i) {
×
2425
            std::cerr << "Changeset version " << our_changesets[i]->version << " from peer "
×
2426
                      << our_changesets[i]->origin_file_ident << " at timestamp "
×
2427
                      << our_changesets[i]->origin_timestamp << "\n";
×
2428
        }
×
2429

2430
        for (size_t i = 0; i < our_changesets.size(); ++i) {
×
2431
            std::cerr << TERM_RED << "\nLOCAL (RECIPROCAL) CHANGESET BEFORE MERGE:\n" << TERM_RESET;
×
2432
            our_changesets[i]->print(std::cerr);
×
2433
        }
×
2434

2435
        for (size_t i = 0; i < their_changesets.size(); ++i) {
×
2436
            std::cerr << TERM_RED << "\nINCOMING CHANGESET BEFORE MERGE:\n" << TERM_RESET;
×
2437
            their_changesets[i].print(std::cerr);
×
2438
        }
×
2439

2440
        std::cerr << TERM_MAGENTA << "\nINCOMING CHANGESET INDEX:\n" << TERM_RESET;
×
2441
        their_index.print(std::cerr);
×
2442
        std::cerr << '\n';
×
2443
        their_index.verify();
×
2444

2445
        std::cerr << TERM_YELLOW << std::setw(80) << std::left << "MERGE TRACE (incoming):"
×
2446
                  << "MERGE TRACE (local):\n"
×
2447
                  << TERM_RESET;
×
2448
    }
×
2449
#else
2450
    static_cast<void>(local_file_ident);
2451
#endif // REALM_DEBUG LCOV_EXCL_STOP
2452

23,972✔
2453
    for (size_t i = 0; i < our_changesets.size(); ++i) {
554,526✔
2454
        logger.trace(
509,994✔
2455
            util::LogCategory::changeset,
509,994✔
2456
            "Transforming local changeset [%1/%2] through %3 incoming changeset(s) with %4 conflict group(s)", i + 1,
509,994✔
2457
            our_changesets.size(), their_changesets.size(), their_index.get_num_conflict_groups());
509,994✔
2458
        Changeset* our_changeset = our_changesets[i];
509,994✔
2459

259,634✔
2460
        transformer.m_major_side.set_next_changeset(our_changeset);
509,994✔
2461
        // MinorSide uses the index to find the Changeset.
259,634✔
2462
        transformer.m_minor_side.m_changeset_index = &their_index;
509,994✔
2463
        transformer.transform(); // Throws
509,994✔
2464
    }
509,994✔
2465

23,972✔
2466
    logger.debug(util::LogCategory::changeset,
44,532✔
2467
                 "Finished transforming %1 local changesets through %2 incoming changesets (%3 vs %4 "
44,532✔
2468
                 "instructions, in %5 conflict groups)",
44,532✔
2469
                 our_changesets.size(), their_changesets.size(), our_num_instructions, their_num_instructions,
44,532✔
2470
                 their_index.get_num_conflict_groups());
44,532✔
2471

23,972✔
2472
#if REALM_DEBUG // LCOV_EXCL_START
23,972✔
2473
    // Check that the index is still valid after transformation.
23,972✔
2474
    their_index.verify();
44,532✔
2475
#endif // REALM_DEBUG LCOV_EXCL_STOP
44,532✔
2476

23,972✔
2477
#if REALM_DEBUG // LCOV_EXCL_START
23,972✔
2478
    if (trace) {
44,532✔
2479
        for (size_t i = 0; i < our_changesets.size(); ++i) {
×
2480
            std::cerr << TERM_CYAN << "\nRECIPROCAL CHANGESET AFTER MERGE:\n" << TERM_RESET;
×
2481
            our_changesets[i]->print(std::cerr);
×
2482
            std::cerr << '\n';
×
2483
        }
×
2484
        for (size_t i = 0; i < their_changesets.size(); ++i) {
×
2485
            std::cerr << TERM_CYAN << "INCOMING CHANGESET AFTER MERGE:\n" << TERM_RESET;
×
2486
            their_changesets[i].print(std::cerr);
×
2487
            std::cerr << '\n';
×
2488
        }
×
2489
    }
×
2490
#endif // LCOV_EXCL_STOP REALM_DEBUG
44,532✔
2491
}
44,532✔
2492

2493
size_t Transformer::transform_remote_changesets(TransformHistory& history, file_ident_type local_file_ident,
2494
                                                version_type current_local_version,
2495
                                                util::Span<Changeset> parsed_changesets,
2496
                                                util::FunctionRef<bool(const Changeset*)> changeset_applier,
2497
                                                util::Logger& logger)
2498
{
61,710✔
2499
    REALM_ASSERT(local_file_ident != 0);
61,710✔
2500

31,942✔
2501
    std::vector<Changeset*> our_changesets;
61,710✔
2502

31,942✔
2503
    // p points to the beginning of a range of changesets that share the same
31,942✔
2504
    // "base", i.e. are based on the same local version.
31,942✔
2505
    auto p = parsed_changesets.begin();
61,710✔
2506
    auto parsed_changesets_end = parsed_changesets.end();
61,710✔
2507

31,942✔
2508
    while (p != parsed_changesets_end) {
128,786✔
2509
        // Find the range of incoming changesets that share the same
34,886✔
2510
        // last_integrated_local_version, which means we can merge them in one go.
34,886✔
2511
        auto same_base_range_end = std::find_if(p + 1, parsed_changesets_end, [&](auto& changeset) {
53,978✔
2512
            return p->last_integrated_remote_version != changeset.last_integrated_remote_version;
35,802✔
2513
        });
35,802✔
2514

34,886✔
2515
        version_type begin_version = p->last_integrated_remote_version;
67,076✔
2516
        version_type end_version = current_local_version;
67,076✔
2517
        for (;;) {
577,050✔
2518
            HistoryEntry history_entry;
577,050✔
2519
            version_type version = history.find_history_entry(begin_version, end_version, history_entry);
577,050✔
2520
            if (version == 0)
577,050✔
2521
                break; // No more local changesets
67,078✔
2522

259,646✔
2523
            Changeset& our_changeset = get_reciprocal_transform(history, local_file_ident, version,
509,972✔
2524
                                                                history_entry); // Throws
509,972✔
2525
            our_changesets.push_back(&our_changeset);
509,972✔
2526

259,646✔
2527
            begin_version = version;
509,972✔
2528
        }
509,972✔
2529

34,886✔
2530
        bool must_apply_all = false;
67,076✔
2531

34,886✔
2532
        if (!our_changesets.empty()) {
67,076✔
2533
            merge_changesets(local_file_ident, {&*p, same_base_range_end}, our_changesets, logger); // Throws
44,534✔
2534
            // We need to apply all transformed changesets if at least one reciprocal changeset was modified
23,974✔
2535
            // during OT.
23,974✔
2536
            must_apply_all = std::any_of(our_changesets.begin(), our_changesets.end(), [](const Changeset* c) {
444,928✔
2537
                return c->is_dirty();
444,928✔
2538
            });
444,928✔
2539
        }
44,534✔
2540

34,886✔
2541
        auto continue_applying = true;
67,076✔
2542
        for (; p != same_base_range_end && continue_applying; ++p) {
164,560✔
2543
            // It is safe to stop applying the changesets if:
48,622✔
2544
            //      1. There are no reciprocal changesets
48,622✔
2545
            //      2. No reciprocal changeset was modified
48,622✔
2546
            continue_applying = changeset_applier(p) || must_apply_all;
97,484!
2547
        }
97,484✔
2548
        if (!continue_applying) {
67,076✔
2549
            break;
×
2550
        }
×
2551

34,886✔
2552
        our_changesets.clear(); // deliberately not releasing memory
67,076✔
2553
    }
67,076✔
2554

31,942✔
2555
    // NOTE: Any exception thrown during flushing *MUST* lead to rollback of
31,942✔
2556
    // the current transaction.
31,942✔
2557
    flush_reciprocal_transform_cache(history); // Throws
61,710✔
2558

31,942✔
2559
    return p - parsed_changesets.begin();
61,710✔
2560
}
61,710✔
2561

2562

2563
Changeset& Transformer::get_reciprocal_transform(TransformHistory& history, file_ident_type local_file_ident,
2564
                                                 version_type version, const HistoryEntry& history_entry)
2565
{
509,974✔
2566
    auto& changeset = m_reciprocal_transform_cache[version]; // Throws
509,974✔
2567
    if (changeset.empty()) {
509,974✔
2568
        bool is_compressed = false;
481,306✔
2569
        ChunkedBinaryData data = history.get_reciprocal_transform(version, is_compressed);
481,306✔
2570
        ChunkedBinaryInputStream in{data};
481,306✔
2571
        if (is_compressed) {
481,306✔
2572
            size_t total_size;
46,350✔
2573
            auto decompressed = util::compression::decompress_nonportable_input_stream(in, total_size);
46,350✔
2574
            REALM_ASSERT(decompressed);
46,350✔
2575
            sync::parse_changeset(*decompressed, changeset); // Throws
46,350✔
2576
        }
46,350✔
2577
        else {
434,956✔
2578
            sync::parse_changeset(in, changeset); // Throws
434,956✔
2579
        }
434,956✔
2580

244,952✔
2581
        changeset.version = version;
481,306✔
2582
        changeset.last_integrated_remote_version = history_entry.remote_version;
481,306✔
2583
        changeset.origin_timestamp = history_entry.origin_timestamp;
481,306✔
2584
        file_ident_type origin_file_ident = history_entry.origin_file_ident;
481,306✔
2585
        if (origin_file_ident == 0)
481,306✔
2586
            origin_file_ident = local_file_ident;
268,388✔
2587
        changeset.origin_file_ident = origin_file_ident;
481,306✔
2588
    }
481,306✔
2589
    return changeset;
509,974✔
2590
}
509,974✔
2591

2592

2593
void Transformer::flush_reciprocal_transform_cache(TransformHistory& history)
2594
{
61,660✔
2595
    auto changesets = std::move(m_reciprocal_transform_cache);
61,660✔
2596
    m_reciprocal_transform_cache.clear();
61,660✔
2597
    ChangesetEncoder::Buffer output_buffer;
61,660✔
2598
    for (const auto& [version, changeset] : changesets) {
480,280✔
2599
        if (changeset.is_dirty()) {
480,280✔
2600
            encode_changeset(changeset, output_buffer); // Throws
10,204✔
2601
            BinaryData data{output_buffer.data(), output_buffer.size()};
10,204✔
2602
            history.set_reciprocal_transform(version, data); // Throws
10,204✔
2603
            output_buffer.clear();
10,204✔
2604
        }
10,204✔
2605
    }
480,280✔
2606
}
61,660✔
2607

2608
void parse_remote_changeset(const RemoteChangeset& remote_changeset, Changeset& parsed_changeset)
2609
{
97,422✔
2610
    // origin_file_ident = 0 is currently used to indicate an entry of local
48,502✔
2611
    // origin.
48,502✔
2612
    REALM_ASSERT(remote_changeset.origin_file_ident != 0);
97,422✔
2613
    REALM_ASSERT(remote_changeset.remote_version != 0);
97,422✔
2614

48,502✔
2615
    ChunkedBinaryInputStream remote_in{remote_changeset.data};
97,422✔
2616
    parse_changeset(remote_in, parsed_changeset); // Throws
97,422✔
2617

48,502✔
2618
    parsed_changeset.version = remote_changeset.remote_version;
97,422✔
2619
    parsed_changeset.last_integrated_remote_version = remote_changeset.last_integrated_local_version;
97,422✔
2620
    parsed_changeset.origin_timestamp = remote_changeset.origin_timestamp;
97,422✔
2621
    parsed_changeset.origin_file_ident = remote_changeset.origin_file_ident;
97,422✔
2622
    parsed_changeset.original_changeset_size = remote_changeset.original_changeset_size;
97,422✔
2623
}
97,422✔
2624

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