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

realm / realm-core / 1720

02 Oct 2023 09:01AM UTC coverage: 91.205% (-0.02%) from 91.221%
1720

push

Evergreen

jedelbo
Remove redundant call to Query::find_all

95906 of 175738 branches covered (0.0%)

2 of 2 new or added lines in 1 file covered. (100.0%)

128 existing lines in 20 files now uncovered.

232494 of 254915 relevant lines covered (91.2%)

6999487.15 hits per line

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

92.32
/src/realm/sync/instruction_replication.cpp
1
#include <realm/sync/instruction_replication.hpp>
2
#include <realm/transaction.hpp>
3
#include <realm/sync/transform.hpp> // TransformError
4
#include <realm/list.hpp>
5

6
namespace realm {
7
namespace sync {
8

9
void SyncReplication::reset()
10
{
746,026✔
11
    m_encoder.reset();
746,026✔
12

374,222✔
13
    m_last_table = nullptr;
746,026✔
14
    m_last_object = ObjKey();
746,026✔
15
    m_last_field = ColKey();
746,026✔
16
    m_last_class_name = InternString::npos;
746,026✔
17
    m_last_primary_key = Instruction::PrimaryKey();
746,026✔
18
    m_last_field_name = InternString::npos;
746,026✔
19
}
746,026✔
20

21
void SyncReplication::do_initiate_transact(Group& group, version_type current_version, bool history_updated)
22
{
711,664✔
23
    Replication::do_initiate_transact(group, current_version, history_updated);
711,664✔
24
    m_transaction = dynamic_cast<Transaction*>(&group); // FIXME: Is this safe?
711,664✔
25
    m_write_validator = make_write_validator(*m_transaction);
711,664✔
26
    reset();
711,664✔
27
}
711,664✔
28

29
Instruction::Payload SyncReplication::as_payload(Mixed value)
30
{
530,194✔
31
    if (value.is_null()) {
530,194✔
32
        return Instruction::Payload{};
×
33
    }
×
34

258,012✔
35
    switch (value.get_type()) {
530,194✔
36
        case type_Int: {
296,376✔
37
            return Instruction::Payload{value.get<int64_t>()};
296,376✔
38
        }
×
39
        case type_Bool: {
2,118✔
40
            return Instruction::Payload{value.get<bool>()};
2,118✔
41
        }
×
42
        case type_Float: {
2,240✔
43
            return Instruction::Payload{value.get<float>()};
2,240✔
44
        }
×
45
        case type_Double: {
3,112✔
46
            return Instruction::Payload{value.get<double>()};
3,112✔
47
        }
×
48
        case type_String: {
189,654✔
49
            auto str = value.get<StringData>();
189,654✔
50
            auto range = m_encoder.add_string_range(str);
189,654✔
51
            return Instruction::Payload{range};
189,654✔
52
        }
×
53
        case type_Binary: {
7,984✔
54
            auto binary = value.get<BinaryData>();
7,984✔
55
            auto range = m_encoder.add_string_range(StringData{binary.data(), binary.size()});
7,984✔
56
            const bool is_binary = true;
7,984✔
57
            return Instruction::Payload{range, is_binary};
7,984✔
58
        }
×
59
        case type_Timestamp: {
9,884✔
60
            return Instruction::Payload{value.get<Timestamp>()};
9,884✔
61
        }
×
62
        case type_Decimal: {
2,144✔
63
            return Instruction::Payload{value.get<Decimal128>()};
2,144✔
64
        }
×
65
        case type_ObjectId: {
13,352✔
66
            return Instruction::Payload{value.get<ObjectId>()};
13,352✔
67
        }
×
68
        case type_UUID: {
3,328✔
69
            return Instruction::Payload{value.get<UUID>()};
3,328✔
70
        }
×
71
        case type_TypedLink:
✔
72
            [[fallthrough]];
×
73
        case type_Link: {
✔
74
            REALM_TERMINATE("as_payload() needs table/collection for links");
×
75
            break;
×
76
        }
×
77
        case type_Mixed:
✔
78
            [[fallthrough]];
×
79
        case type_LinkList: {
✔
80
            REALM_TERMINATE("Invalid payload type");
×
81
            break;
×
82
        }
×
83
    }
×
84
    return Instruction::Payload{};
×
85
}
×
86

87
Instruction::Payload SyncReplication::as_payload(const CollectionBase& collection, Mixed value)
88
{
217,020✔
89
    return as_payload(*collection.get_table(), collection.get_col_key(), value);
217,020✔
90
}
217,020✔
91

92
Instruction::Payload SyncReplication::as_payload(const Table& table, ColKey col_key, Mixed value)
93
{
556,776✔
94
    if (value.is_null()) {
556,776✔
95
        // FIXME: `Mixed::get_type()` asserts on null.
604✔
96
        return Instruction::Payload{};
1,228✔
97
    }
1,228✔
98

270,446✔
99
    if (value.is_type(type_Link)) {
555,548✔
100
        ConstTableRef target_table = table.get_link_target(col_key);
14,942✔
101
        if (target_table->is_embedded()) {
14,942✔
102
            // FIXME: Include target table name to support Mixed of Embedded Objects.
4,670✔
103
            return Instruction::Payload::ObjectValue{};
9,398✔
104
        }
9,398✔
105

2,626✔
106
        Instruction::Payload::Link link;
5,544✔
107
        link.target_table = emit_class_name(*target_table);
5,544✔
108
        link.target = primary_key_for_object(*target_table, value.get<ObjKey>());
5,544✔
109
        return Instruction::Payload{link};
5,544✔
110
    }
5,544✔
111
    else if (value.is_type(type_TypedLink)) {
540,606✔
112
        auto obj_link = value.get<ObjLink>();
10,408✔
113
        ConstTableRef target_table = m_transaction->get_table(obj_link.get_table_key());
10,408✔
114
        REALM_ASSERT(target_table);
10,408✔
115

5,132✔
116
        if (target_table->is_embedded()) {
10,408✔
117
            ConstTableRef static_target_table = table.get_link_target(col_key);
2,312✔
118

1,144✔
119
            if (static_target_table != target_table)
2,312✔
120
                REALM_TERMINATE("Dynamically typed embedded objects not supported yet.");
1,144✔
121
            return Instruction::Payload::ObjectValue{};
2,312✔
122
        }
2,312✔
123

3,988✔
124
        Instruction::Payload::Link link;
8,096✔
125
        link.target_table = emit_class_name(*target_table);
8,096✔
126
        link.target = primary_key_for_object(*target_table, obj_link.get_obj_key());
8,096✔
127
        return Instruction::Payload{link};
8,096✔
128
    }
8,096✔
129
    else {
530,198✔
130
        return as_payload(value);
530,198✔
131
    }
530,198✔
132
}
555,548✔
133

134
InternString SyncReplication::emit_class_name(StringData table_name)
135
{
422,108✔
136
    return m_encoder.intern_string(Group::table_name_to_class_name(table_name));
422,108✔
137
}
422,108✔
138

139
InternString SyncReplication::emit_class_name(const Table& table)
140
{
380,232✔
141
    return emit_class_name(table.get_name());
380,232✔
142
}
380,232✔
143

144
Instruction::Payload::Type SyncReplication::get_payload_type(DataType type) const
145
{
150,370✔
146
    using Type = Instruction::Payload::Type;
150,370✔
147
    switch (type) {
150,370✔
148
        case type_Int:
46,612✔
149
            return Type::Int;
46,612✔
150
        case type_Bool:
4,240✔
151
            return Type::Bool;
4,240✔
152
        case type_String:
35,970✔
153
            return Type::String;
35,970✔
154
        case type_Binary:
4,424✔
155
            return Type::Binary;
4,424✔
156
        case type_Timestamp:
5,264✔
157
            return Type::Timestamp;
5,264✔
158
        case type_Float:
4,436✔
159
            return Type::Float;
4,436✔
160
        case type_Double:
4,416✔
161
            return Type::Double;
4,416✔
162
        case type_Decimal:
4,240✔
163
            return Type::Decimal;
4,240✔
164
        case type_Link:
5,636✔
165
            return Type::Link;
5,636✔
166
        case type_LinkList:
3,108✔
167
            return Type::Link;
3,108✔
168
        case type_TypedLink:
4✔
169
            return Type::Link;
4✔
170
        case type_ObjectId:
21,932✔
171
            return Type::ObjectId;
21,932✔
172
        case type_UUID:
4,384✔
173
            return Type::UUID;
4,384✔
174
        case type_Mixed:
5,704✔
175
            return Type::Null;
5,704✔
176
    }
×
177
    unsupported_instruction();
×
178
    return Type::Int; // Make compiler happy
×
179
}
×
180

181
void SyncReplication::add_class(TableKey tk, StringData name, Table::Type table_type)
182
{
19,056✔
183
    Replication::add_class(tk, name, table_type);
19,056✔
184

9,336✔
185
    bool is_class = m_transaction->table_is_public(tk);
19,056✔
186

9,336✔
187
    if (is_class && !m_short_circuit) {
19,056✔
188
        Instruction::AddTable instr;
1,712✔
189
        instr.table = emit_class_name(name);
1,712✔
190
        if (table_type == Table::Type::Embedded) {
1,712✔
191
            instr.type = Instruction::AddTable::EmbeddedTable{};
1,656✔
192
        }
1,656✔
193
        else {
56✔
194
            auto field = m_encoder.intern_string(""); // FIXME: Should this be "_id"?
56✔
195
            const bool is_nullable = false;
56✔
196
            bool is_asymmetric = (table_type == Table::Type::TopLevelAsymmetric);
56✔
197
            instr.type = Instruction::AddTable::TopLevelTable{
56✔
198
                field,
56✔
199
                Instruction::Payload::Type::GlobalKey,
56✔
200
                is_nullable,
56✔
201
                is_asymmetric,
56✔
202
            };
56✔
203
        }
56✔
204
        emit(instr);
1,712✔
205
    }
1,712✔
206
}
19,056✔
207

208
void SyncReplication::add_class_with_primary_key(TableKey tk, StringData name, DataType pk_type, StringData pk_field,
209
                                                 bool nullable, Table::Type table_type)
210
{
60,578✔
211
    Replication::add_class_with_primary_key(tk, name, pk_type, pk_field, nullable, table_type);
60,578✔
212

29,990✔
213
    bool is_class = m_transaction->table_is_public(tk);
60,578✔
214

29,990✔
215
    if (is_class && !m_short_circuit) {
60,578✔
216
        Instruction::AddTable instr;
39,038✔
217
        instr.table = emit_class_name(name);
39,038✔
218
        auto field = m_encoder.intern_string(pk_field);
39,038✔
219
        auto is_asymmetric = (table_type == Table::Type::TopLevelAsymmetric);
39,038✔
220
        auto spec = Instruction::AddTable::TopLevelTable{field, get_payload_type(pk_type), nullable, is_asymmetric};
39,038✔
221
        if (!is_valid_key_type(spec.pk_type)) {
39,038✔
222
            unsupported_instruction();
×
223
        }
×
224
        instr.type = std::move(spec);
39,038✔
225
        emit(instr);
39,038✔
226
    }
39,038✔
227
}
60,578✔
228

229
void SyncReplication::create_object(const Table* table, GlobalKey oid)
230
{
14,856✔
231
    if (table->is_embedded()) {
14,856✔
232
        unsupported_instruction(); // FIXME: TODO
×
233
    }
×
234

7,278✔
235
    Replication::create_object(table, oid);
14,856✔
236
    if (select_table(*table)) {
14,856✔
237
        if (table->get_primary_key_column()) {
72✔
238
            // Trying to create object without a primary key in a table that
239
            // has a primary key column.
240
            unsupported_instruction();
×
241
        }
×
242
        Instruction::CreateObject instr;
72✔
243
        instr.table = m_last_class_name;
72✔
244
        instr.object = oid;
72✔
245
        emit(instr);
72✔
246
    }
72✔
247
}
14,856✔
248

249
Instruction::PrimaryKey SyncReplication::as_primary_key(Mixed value)
250
{
518,168✔
251
    if (value.is_null()) {
518,168✔
252
        return mpark::monostate{};
300✔
253
    }
300✔
254
    else if (value.get_type() == type_Int) {
517,868✔
255
        return value.get<int64_t>();
397,690✔
256
    }
397,690✔
257
    else if (value.get_type() == type_String) {
120,178✔
258
        return m_encoder.intern_string(value.get<StringData>());
13,450✔
259
    }
13,450✔
260
    else if (value.get_type() == type_ObjectId) {
106,728✔
261
        return value.get<ObjectId>();
106,676✔
262
    }
106,676✔
263
    else if (value.get_type() == type_UUID) {
52✔
264
        return value.get<UUID>();
52✔
265
    }
52✔
UNCOV
266
    else {
×
267
        // Unsupported primary key type.
UNCOV
268
        unsupported_instruction();
×
UNCOV
269
    }
×
270
}
518,168✔
271

272
void SyncReplication::create_object_with_primary_key(const Table* table, ObjKey oid, Mixed value)
273
{
237,532✔
274
    if (table->is_embedded()) {
237,532✔
275
        // Trying to create an object with a primary key in an embedded table.
276
        unsupported_instruction();
×
277
    }
×
278

111,578✔
279
    Replication::create_object_with_primary_key(table, oid, value);
237,532✔
280
    if (select_table(*table)) {
237,532✔
281
        if (m_write_validator) {
111,676✔
282
            m_write_validator(*table);
1,598✔
283
        }
1,598✔
284

48,888✔
285
        auto col = table->get_primary_key_column();
111,676✔
286
        if (col && ((value.is_null() && col.is_nullable()) || DataType(col.get_type()) == value.get_type())) {
111,676✔
287
            Instruction::CreateObject instr;
111,668✔
288
            instr.table = m_last_class_name;
111,668✔
289
            instr.object = as_primary_key(value);
111,668✔
290
            emit(instr);
111,668✔
291
        }
111,668✔
292
        else {
8✔
293
            // Trying to create object with primary key in table without a
4✔
294
            // primary key column, or with wrong primary key type.
4✔
295
            unsupported_instruction();
8✔
296
        }
8✔
297
    }
111,676✔
298
}
237,532✔
299

300

301
void SyncReplication::erase_class(TableKey table_key, size_t num_tables)
302
{
4,698✔
303
    Replication::erase_class(table_key, num_tables);
4,698✔
304

2,144✔
305
    StringData table_name = m_transaction->get_table_name(table_key);
4,698✔
306

2,144✔
307
    bool is_class = m_transaction->table_is_public(table_key);
4,698✔
308

2,144✔
309
    if (is_class && !m_short_circuit) {
4,698✔
310
        Instruction::EraseTable instr;
1,122✔
311
        instr.table = emit_class_name(table_name);
1,122✔
312
        emit(instr);
1,122✔
313
    }
1,122✔
314

2,144✔
315
    m_last_table = nullptr;
4,698✔
316
}
4,698✔
317

318
void SyncReplication::rename_class(TableKey, StringData)
319
{
×
320
    unsupported_instruction();
×
321
}
×
322

323
void SyncReplication::insert_column(const Table* table, ColKey col_key, DataType type, StringData name,
324
                                    Table* target_table)
325
{
164,826✔
326
    Replication::insert_column(table, col_key, type, name, target_table);
164,826✔
327
    using CollectionType = Instruction::AddColumn::CollectionType;
164,826✔
328

80,214✔
329
    if (select_table(*table)) {
164,826✔
330
        Instruction::AddColumn instr;
88,624✔
331
        instr.table = m_last_class_name;
88,624✔
332
        instr.field = m_encoder.intern_string(name);
88,624✔
333
        instr.nullable = col_key.is_nullable();
88,624✔
334
        instr.type = get_payload_type(type);
88,624✔
335

42,818✔
336
        if (col_key.is_list()) {
88,624✔
337
            instr.collection_type = CollectionType::List;
15,932✔
338
        }
15,932✔
339
        else if (col_key.is_dictionary()) {
72,692✔
340
            instr.collection_type = CollectionType::Dictionary;
11,724✔
341
            auto key_type = table->get_dictionary_key_type(col_key);
11,724✔
342
            REALM_ASSERT(key_type == type_String);
11,724✔
343
            instr.key_type = get_payload_type(key_type);
11,724✔
344
        }
11,724✔
345
        else if (col_key.is_set()) {
60,968✔
346
            instr.collection_type = CollectionType::Set;
10,980✔
347
            auto value_type = table->get_column_type(col_key);
10,980✔
348
            REALM_ASSERT(value_type != type_LinkList);
10,980✔
349
            instr.type = get_payload_type(value_type);
10,980✔
350
            instr.key_type = Instruction::Payload::Type::Null;
10,980✔
351
        }
10,980✔
352
        else {
49,988✔
353
            REALM_ASSERT(!col_key.is_collection());
49,988✔
354
            instr.collection_type = CollectionType::Single;
49,988✔
355
            instr.key_type = Instruction::Payload::Type::Null;
49,988✔
356
        }
49,988✔
357

42,818✔
358
        // Mixed columns are always nullable.
42,818✔
359
        REALM_ASSERT(instr.type != Instruction::Payload::Type::Null || instr.nullable ||
88,624!
360
                     instr.collection_type == CollectionType::Dictionary);
88,624✔
361

42,818✔
362
        if (instr.type == Instruction::Payload::Type::Link && target_table) {
88,624✔
363
            instr.link_target_table = emit_class_name(*target_table);
8,152✔
364
        }
8,152✔
365
        else {
80,472✔
366
            instr.link_target_table = m_encoder.intern_string("");
80,472✔
367
        }
80,472✔
368
        emit(instr);
88,624✔
369
    }
88,624✔
370
}
164,826✔
371

372
void SyncReplication::erase_column(const Table* table, ColKey col_ndx)
373
{
8✔
374
    Replication::erase_column(table, col_ndx);
8✔
375

4✔
376
    if (select_table(*table)) {
8✔
377
        // Not allowed to remove PK/OID columns!
4✔
378
        REALM_ASSERT(col_ndx != table->get_primary_key_column());
8✔
379
        Instruction::EraseColumn instr;
8✔
380
        instr.table = m_last_class_name;
8✔
381
        instr.field = m_encoder.intern_string(table->get_column_name(col_ndx));
8✔
382
        emit(instr);
8✔
383
    }
8✔
384
}
8✔
385

386
void SyncReplication::rename_column(const Table*, ColKey, StringData)
387
{
×
388
    unsupported_instruction();
×
389
}
×
390

391
void SyncReplication::list_set(const CollectionBase& list, size_t ndx, Mixed value)
392
{
12,318✔
393
    Mixed prior_value = list.get_any(ndx);
12,318✔
394
    bool prior_is_unresolved =
12,318✔
395
        prior_value.is_type(type_Link, type_TypedLink) && prior_value.get<ObjKey>().is_unresolved();
12,318✔
396

6,564✔
397
    // If link is unresolved, it should not be communicated.
6,564✔
398
    if (value.is_type(type_Link, type_TypedLink) && value.get<ObjKey>().is_unresolved()) {
12,318✔
399
        // ... but reported internally as a deletion if prior value was not unresolved
78✔
400
        if (!prior_is_unresolved)
156✔
401
            Replication::list_erase(list, ndx);
156✔
402
    }
156✔
403
    else {
12,162✔
404
        if (prior_is_unresolved) {
12,162✔
405
            Replication::list_insert(list, ndx, value, 0 /* prior size not used */);
16✔
406
        }
16✔
407
        else {
12,146✔
408
            Replication::list_set(list, ndx, value);
12,146✔
409
        }
12,146✔
410
    }
12,162✔
411

6,564✔
412
    if (select_collection(list)) {
12,318✔
413
        // If this is an embedded object then we need to emit and erase/insert instruction so that the old
3,106✔
414
        // object gets cleared, otherwise you'll only see the Update ObjectValue instruction, which is idempotent,
3,106✔
415
        // and that will lead to corrupted prior size for array operations inside the embedded object during
3,106✔
416
        // changeset application.
3,106✔
417
        auto needs_insert_erase_sequence = [&] {
6,026✔
418
            if (value.is_type(type_Link)) {
6,026✔
419
                return list.get_target_table()->is_embedded();
140✔
420
            }
140✔
421
            else if (value.is_type(type_TypedLink)) {
5,886✔
422
                return m_transaction->get_table(value.get_link().get_table_key())->is_embedded();
24✔
423
            }
24✔
424
            return false;
5,862✔
425
        };
5,862✔
426
        if (needs_insert_erase_sequence()) {
6,026✔
427
            REALM_ASSERT(!list.is_null(ndx));
8✔
428
            Instruction::ArrayErase erase_instr;
8✔
429
            populate_path_instr(erase_instr, list, static_cast<uint32_t>(ndx));
8✔
430
            erase_instr.prior_size = uint32_t(list.size());
8✔
431
            emit(erase_instr);
8✔
432

4✔
433
            Instruction::ArrayInsert insert_instr;
8✔
434
            populate_path_instr(insert_instr, list, static_cast<uint32_t>(ndx));
8✔
435
            insert_instr.prior_size = erase_instr.prior_size - 1;
8✔
436
            insert_instr.value = as_payload(list, value);
8✔
437
            emit(insert_instr);
8✔
438
        }
8✔
439
        else {
6,018✔
440
            Instruction::Update instr;
6,018✔
441
            populate_path_instr(instr, list, uint32_t(ndx));
6,018✔
442
            REALM_ASSERT(instr.is_array_update());
6,018✔
443
            instr.value = as_payload(list, value);
6,018✔
444
            instr.prior_size = uint32_t(list.size());
6,018✔
445
            emit(instr);
6,018✔
446
        }
6,018✔
447
    }
6,026✔
448
}
12,318✔
449

450
void SyncReplication::list_insert(const CollectionBase& list, size_t ndx, Mixed value, size_t prior_size)
451
{
331,936✔
452
    // If link is unresolved, it should not be communicated.
167,050✔
453
    if (!(value.is_type(type_Link, type_TypedLink) && value.get<ObjKey>().is_unresolved())) {
331,936✔
454
        Replication::list_insert(list, ndx, value, prior_size);
331,900✔
455
    }
331,900✔
456

167,050✔
457
    if (select_collection(list)) {
331,936✔
458
        Instruction::ArrayInsert instr;
165,886✔
459
        populate_path_instr(instr, list, uint32_t(ndx));
165,886✔
460
        instr.value = as_payload(list, value);
165,886✔
461
        instr.prior_size = uint32_t(prior_size);
165,886✔
462
        emit(instr);
165,886✔
463
    }
165,886✔
464
}
331,936✔
465

466
void SyncReplication::add_int(const Table* table, ColKey col, ObjKey ndx, int_fast64_t value)
467
{
5,298✔
468
    Replication::add_int(table, col, ndx, value);
5,298✔
469

2,706✔
470
    if (select_table(*table)) {
5,298✔
471
        REALM_ASSERT(col != table->get_primary_key_column());
1,888✔
472

946✔
473
        Instruction::AddInteger instr;
1,888✔
474
        populate_path_instr(instr, *table, ndx, col);
1,888✔
475
        instr.value = value;
1,888✔
476
        emit(instr);
1,888✔
477
    }
1,888✔
478
}
5,298✔
479

480
void SyncReplication::set(const Table* table, ColKey col, ObjKey key, Mixed value, _impl::Instruction variant)
481
{
890,008✔
482
    Replication::set(table, col, key, value, variant);
890,008✔
483

431,322✔
484
    if (key.is_unresolved()) {
890,008✔
485
        return;
164✔
486
    }
164✔
487

431,240✔
488
    if (col == table->get_primary_key_column()) {
889,844✔
489
        return;
3,380✔
490
    }
3,380✔
491

429,584✔
492
    // If link is unresolved, it should not be communicated.
429,584✔
493
    if (value.is_type(type_Link, type_TypedLink) && value.get<ObjKey>().is_unresolved()) {
886,464✔
494
        return;
36✔
495
    }
36✔
496

429,566✔
497
    if (select_table(*table)) {
886,428✔
498
        // Omit of Update(NULL, default=true) for embedded object / dictionary
162,452✔
499
        // columns if the value is already NULL. This is a workaround for the
162,452✔
500
        // fact that erase always wins for nested structures, but we don't want
162,452✔
501
        // default values to win over later embedded object creation.
162,452✔
502
        if (variant == _impl::instr_SetDefault && value.is_null()) {
339,764✔
503
            if (col.get_type() == col_type_Link && table->get_object(key).is_null(col)) {
80✔
504
                return;
8✔
505
            }
8✔
506
            if (col.is_dictionary() && table->get_object(key).is_null(col)) {
72!
507
                // Dictionary columns cannot currently be NULL, but this is
508
                // likely to change.
509
                return;
×
510
            }
×
511
        }
339,756✔
512

162,448✔
513
        Instruction::Update instr;
339,756✔
514
        populate_path_instr(instr, *table, key, col);
339,756✔
515
        instr.value = as_payload(*table, col, value);
339,756✔
516
        instr.is_default = (variant == _impl::instr_SetDefault);
339,756✔
517
        emit(instr);
339,756✔
518
    }
339,756✔
519
}
886,428✔
520

521

522
void SyncReplication::remove_object(const Table* table, ObjKey row_ndx)
523
{
74,078✔
524
    Replication::remove_object(table, row_ndx);
74,078✔
525
    if (table->is_embedded())
74,078✔
526
        return;
6,126✔
527
    if (table->is_asymmetric())
67,952✔
528
        return;
844✔
529
    REALM_ASSERT(!row_ndx.is_unresolved());
67,108✔
530

33,468✔
531
    if (select_table(*table)) {
67,108✔
532
        Instruction::EraseObject instr;
20,984✔
533
        instr.table = m_last_class_name;
20,984✔
534
        instr.object = primary_key_for_object(*table, row_ndx);
20,984✔
535
        emit(instr);
20,984✔
536
    }
20,984✔
537
}
67,108✔
538

539

540
void SyncReplication::list_move(const CollectionBase& view, size_t from_ndx, size_t to_ndx)
541
{
340✔
542
    Replication::list_move(view, from_ndx, to_ndx);
340✔
543
    if (select_collection(view)) {
340✔
544
        Instruction::ArrayMove instr;
320✔
545
        populate_path_instr(instr, view, uint32_t(from_ndx));
320✔
546
        instr.ndx_2 = uint32_t(to_ndx);
320✔
547
        instr.prior_size = uint32_t(view.size());
320✔
548
        emit(instr);
320✔
549
    }
320✔
550
}
340✔
551

552
void SyncReplication::list_erase(const CollectionBase& list, size_t ndx)
553
{
10,010✔
554
    Mixed prior_value = list.get_any(ndx);
10,010✔
555
    // If link is unresolved, it should not be communicated.
5,166✔
556
    if (!(prior_value.is_type(type_Link, type_TypedLink) && prior_value.get<ObjKey>().is_unresolved())) {
10,010✔
557
        Replication::list_erase(list, ndx);
9,998✔
558
    }
9,998✔
559

5,166✔
560
    size_t prior_size = list.size();
10,010✔
561
    if (select_collection(list)) {
10,010✔
562
        Instruction::ArrayErase instr;
4,476✔
563
        populate_path_instr(instr, list, uint32_t(ndx));
4,476✔
564
        instr.prior_size = uint32_t(prior_size);
4,476✔
565
        emit(instr);
4,476✔
566
    }
4,476✔
567
}
10,010✔
568

569
void SyncReplication::list_clear(const CollectionBase& view)
570
{
1,752✔
571
    Replication::list_clear(view);
1,752✔
572
    if (select_collection(view)) {
1,752✔
573
        Instruction::Clear instr;
254✔
574
        populate_path_instr(instr, view);
254✔
575
        emit(instr);
254✔
576
    }
254✔
577
}
1,752✔
578

579
void SyncReplication::set_insert(const CollectionBase& set, size_t set_ndx, Mixed value)
580
{
21,444✔
581
    Replication::set_insert(set, set_ndx, value);
21,444✔
582

10,646✔
583
    if (select_collection(set)) {
21,444✔
584
        Instruction::SetInsert instr;
19,216✔
585
        populate_path_instr(instr, set);
19,216✔
586
        instr.value = as_payload(set, value);
19,216✔
587
        emit(instr);
19,216✔
588
    }
19,216✔
589
}
21,444✔
590

591
void SyncReplication::set_erase(const CollectionBase& set, size_t set_ndx, Mixed value)
592
{
3,616✔
593
    Replication::set_erase(set, set_ndx, value);
3,616✔
594

1,808✔
595
    if (select_collection(set)) {
3,616✔
596
        Instruction::SetErase instr;
2,252✔
597
        populate_path_instr(instr, set);
2,252✔
598
        instr.value = as_payload(set, value);
2,252✔
599
        emit(instr);
2,252✔
600
    }
2,252✔
601
}
3,616✔
602

603
void SyncReplication::set_clear(const CollectionBase& set)
604
{
244✔
605
    Replication::set_clear(set);
244✔
606

122✔
607
    if (select_collection(set)) {
244✔
608
        Instruction::Clear instr;
212✔
609
        populate_path_instr(instr, set);
212✔
610
        emit(instr);
212✔
611
    }
212✔
612
}
244✔
613

614
void SyncReplication::dictionary_update(const CollectionBase& dict, const Mixed& key, const Mixed& value)
615
{
26,588✔
616
    // If link is unresolved, it should not be communicated.
13,206✔
617
    if (value.is_type(type_Link, type_TypedLink) && value.get<ObjKey>().is_unresolved()) {
26,588✔
618
        return;
12✔
619
    }
12✔
620

13,200✔
621
    if (select_collection(dict)) {
26,576✔
622
        Instruction::Update instr;
23,640✔
623
        REALM_ASSERT(key.get_type() == type_String);
23,640✔
624
        populate_path_instr(instr, dict);
23,640✔
625
        StringData key_value = key.get_string();
23,640✔
626
        instr.path.push_back(m_encoder.intern_string(key_value));
23,640✔
627
        instr.value = as_payload(dict, value);
23,640✔
628
        instr.is_default = false;
23,640✔
629
        emit(instr);
23,640✔
630
    }
23,640✔
631
}
26,576✔
632

633
void SyncReplication::dictionary_insert(const CollectionBase& dict, size_t ndx, Mixed key, Mixed value)
634
{
23,092✔
635
    Replication::dictionary_insert(dict, ndx, key, value);
23,092✔
636
    dictionary_update(dict, key, value);
23,092✔
637
}
23,092✔
638

639
void SyncReplication::dictionary_set(const CollectionBase& dict, size_t ndx, Mixed key, Mixed value)
640
{
3,496✔
641
    Replication::dictionary_set(dict, ndx, key, value);
3,496✔
642
    dictionary_update(dict, key, value);
3,496✔
643
}
3,496✔
644

645
void SyncReplication::dictionary_erase(const CollectionBase& dict, size_t ndx, Mixed key)
646
{
2,292✔
647
    Replication::dictionary_erase(dict, ndx, key);
2,292✔
648

1,146✔
649
    if (select_collection(dict)) {
2,292✔
650
        Instruction::Update instr;
1,848✔
651
        REALM_ASSERT(key.get_type() == type_String);
1,848✔
652
        populate_path_instr(instr, dict);
1,848✔
653
        StringData key_value = key.get_string();
1,848✔
654
        instr.path.push_back(m_encoder.intern_string(key_value));
1,848✔
655
        instr.value = Instruction::Payload::Erased{};
1,848✔
656
        instr.is_default = false;
1,848✔
657
        emit(instr);
1,848✔
658
    }
1,848✔
659
}
2,292✔
660

661
void SyncReplication::nullify_link(const Table* table, ColKey col_ndx, ObjKey ndx)
662
{
18✔
663
    Replication::nullify_link(table, col_ndx, ndx);
18✔
664

8✔
665
    if (select_table(*table)) {
18✔
666
        Instruction::Update instr;
14✔
667
        populate_path_instr(instr, *table, ndx, col_ndx);
14✔
668
        REALM_ASSERT(!instr.is_array_update());
14✔
669
        instr.value = Instruction::Payload{realm::util::none};
14✔
670
        instr.is_default = false;
14✔
671
        emit(instr);
14✔
672
    }
14✔
673
}
18✔
674

675
void SyncReplication::link_list_nullify(const Lst<ObjKey>& view, size_t ndx)
676
{
76✔
677
    size_t prior_size = view.size();
76✔
678
    Replication::link_list_nullify(view, ndx);
76✔
679
    if (select_collection(view)) {
76✔
680
        Instruction::ArrayErase instr;
56✔
681
        populate_path_instr(instr, view, uint32_t(ndx));
56✔
682
        instr.prior_size = uint32_t(prior_size);
56✔
683
        emit(instr);
56✔
684
    }
56✔
685
}
76✔
686

687
void SyncReplication::unsupported_instruction() const
688
{
×
689
    throw realm::sync::TransformError{"Unsupported instruction"};
×
690
}
×
691

692
bool SyncReplication::select_table(const Table& table)
693
{
2,758,636✔
694
    if (is_short_circuited()) {
2,758,636✔
695
        return false;
826,674✔
696
    }
826,674✔
697

941,098✔
698
    if (&table == m_last_table) {
1,931,962✔
699
        return true;
1,401,060✔
700
    }
1,401,060✔
701

263,554✔
702
    if (!m_transaction->table_is_public(table.get_key())) {
530,902✔
703
        return false;
172,878✔
704
    }
172,878✔
705

177,770✔
706
    m_last_class_name = emit_class_name(table);
358,024✔
707
    m_last_table = &table;
358,024✔
708
    m_last_field = ColKey{};
358,024✔
709
    m_last_object = ObjKey{};
358,024✔
710
    m_last_primary_key.reset();
358,024✔
711
    return true;
358,024✔
712
}
358,024✔
713

714
bool SyncReplication::select_collection(const CollectionBase& view)
715
{
410,604✔
716
    if (view.get_owner_key().is_unresolved()) {
410,604✔
717
        return false;
×
718
    }
×
719

206,782✔
720
    return select_table(*view.get_table());
410,604✔
721
}
410,604✔
722

723
Instruction::PrimaryKey SyncReplication::primary_key_for_object(const Table& table, ObjKey key)
724
{
406,518✔
725
    bool should_emit = select_table(table);
406,518✔
726
    REALM_ASSERT(should_emit);
406,518✔
727

202,226✔
728
    if (table.get_primary_key_column()) {
406,518✔
729
        return as_primary_key(table.get_primary_key(key));
406,498✔
730
    }
406,498✔
731

10✔
732
    GlobalKey global_key = table.get_object_id(key);
20✔
733
    return global_key;
20✔
734
}
20✔
735

736
void SyncReplication::populate_path_instr(Instruction::PathInstruction& instr, const Table& table, ObjKey key,
737
                                          ColKey field)
738
{
628,100✔
739
    REALM_ASSERT(key);
628,100✔
740
    REALM_ASSERT(field);
628,100✔
741

306,714✔
742
    if (table.is_embedded()) {
628,100✔
743
        // For embedded objects, Obj::traverse_path() yields the top object
31,078✔
744
        // first, then objects in the path in order.
31,078✔
745
        auto obj = table.get_object(key);
62,238✔
746
        auto path_sizer = [&](size_t size) {
62,238✔
747
            REALM_ASSERT(size != 0);
62,238✔
748
            // Reserve 2 elements per path component, because link list entries
31,078✔
749
            // have both a field and an index.
31,078✔
750
            instr.path.m_path.reserve(size * 2);
62,238✔
751
        };
62,238✔
752

31,078✔
753
        auto visitor = [&](const Obj& path_obj, ColKey next_field, Mixed index) {
91,214✔
754
            auto element_table = path_obj.get_table();
91,214✔
755
            if (element_table->is_embedded()) {
91,214✔
756
                StringData field_name = element_table->get_column_name(next_field);
28,976✔
757
                InternString interned_field_name = m_encoder.intern_string(field_name);
28,976✔
758
                instr.path.push_back(interned_field_name);
28,976✔
759
            }
28,976✔
760
            else {
62,238✔
761
                // This is the top object, populate it the normal way.
31,078✔
762
                populate_path_instr(instr, *element_table, path_obj.get_key(), next_field);
62,238✔
763
            }
62,238✔
764

45,566✔
765
            if (next_field.is_list()) {
91,214✔
766
                instr.path.push_back(uint32_t(index.get_int()));
31,716✔
767
            }
31,716✔
768
            else if (next_field.is_dictionary()) {
59,498✔
769
                InternString interned_field_name = m_encoder.intern_string(index.get_string());
24,684✔
770
                instr.path.push_back(interned_field_name);
24,684✔
771
            }
24,684✔
772
        };
91,214✔
773

31,078✔
774
        obj.traverse_path(visitor, path_sizer);
62,238✔
775

31,078✔
776
        // The field in the embedded object is the last path component.
31,078✔
777
        StringData field_in_embedded = table.get_column_name(field);
62,238✔
778
        InternString interned_field_in_embedded = m_encoder.intern_string(field_in_embedded);
62,238✔
779
        instr.path.push_back(interned_field_in_embedded);
62,238✔
780
        return;
62,238✔
781
    }
62,238✔
782

275,636✔
783
    bool should_emit = select_table(table);
565,862✔
784
    REALM_ASSERT(should_emit);
565,862✔
785

275,636✔
786
    instr.table = m_last_class_name;
565,862✔
787

275,636✔
788
    if (m_last_object == key) {
565,862✔
789
        instr.object = *m_last_primary_key;
193,952✔
790
    }
193,952✔
791
    else {
371,910✔
792
        instr.object = primary_key_for_object(table, key);
371,910✔
793
        m_last_object = key;
371,910✔
794
        m_last_primary_key = instr.object;
371,910✔
795
    }
371,910✔
796

275,636✔
797
    if (m_last_field == field) {
565,862✔
798
        instr.field = m_last_field_name;
345,966✔
799
    }
345,966✔
800
    else {
219,896✔
801
        instr.field = m_encoder.intern_string(table.get_column_name(field));
219,896✔
802
        m_last_field = field;
219,896✔
803
        m_last_field_name = instr.field;
219,896✔
804
    }
219,896✔
805
}
565,862✔
806

807
void SyncReplication::populate_path_instr(Instruction::PathInstruction& instr, const CollectionBase& list)
808
{
224,194✔
809
    ConstTableRef source_table = list.get_table();
224,194✔
810
    ObjKey source_obj = list.get_owner_key();
224,194✔
811
    ColKey source_field = list.get_col_key();
224,194✔
812
    populate_path_instr(instr, *source_table, source_obj, source_field);
224,194✔
813
}
224,194✔
814

815
void SyncReplication::populate_path_instr(Instruction::PathInstruction& instr, const CollectionBase& list,
816
                                          uint32_t ndx)
817
{
176,772✔
818
    populate_path_instr(instr, list);
176,772✔
819
    instr.path.m_path.push_back(ndx);
176,772✔
820
}
176,772✔
821

822
} // namespace sync
823
} // namespace realm
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc