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

realm / realm-core / 2088

01 Mar 2024 04:55PM UTC coverage: 90.906% (+0.009%) from 90.897%
2088

push

Evergreen

web-flow
Merge pull request #7407 from realm/release/14.1.0

Release 14.1.0

93916 of 173116 branches covered (54.25%)

238350 of 262194 relevant lines covered (90.91%)

6006140.42 hits per line

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

64.2
/src/realm/sync/instruction_applier.cpp
1
#include <realm/sync/instruction_applier.hpp>
2
#include <realm/set.hpp>
3
#include <realm/util/scope_exit.hpp>
4

5
#include <realm/transaction.hpp>
6

7
namespace realm::sync {
8
namespace {
9

10
REALM_NORETURN void throw_bad_transaction_log(std::string msg)
11
{
8✔
12
    throw BadChangesetError{std::move(msg)};
8✔
13
}
8✔
14

15
} // namespace
16

17
REALM_NORETURN void InstructionApplier::bad_transaction_log(const std::string& msg) const
18
{
8✔
19
    if (m_last_object_key) {
8✔
20
        // If the last_object_key is valid then we should have a changeset and a current table
21
        REALM_ASSERT(m_log);
×
22
        REALM_ASSERT(m_last_table_name);
×
23
        std::stringstream ss;
×
24
        util::Optional<InternString> field_name;
×
25
        if (m_last_field_name) {
×
26
            field_name = m_last_field_name;
×
27
        }
×
28
        const instr::Path* cur_path = m_current_path ? &(*m_current_path) : nullptr;
×
29
        m_log->print_path(ss, m_last_table_name, *m_last_object_key, field_name, cur_path);
×
30
        throw_bad_transaction_log(
×
31
            util::format("%1 (instruction target: %2, version: %3, last_integrated_remote_version: %4, "
×
32
                         "origin_file_ident: %5, timestamp: %6)",
×
33
                         msg, ss.str(), m_log->version, m_log->last_integrated_remote_version,
×
34
                         m_log->origin_file_ident, m_log->origin_timestamp));
×
35
    }
×
36
    else if (m_last_table_name) {
8✔
37
        // We should have a changeset if we have a table name defined.
4✔
38
        REALM_ASSERT(m_log);
8✔
39
        throw_bad_transaction_log(
8✔
40
            util::format("%1 (instruction table: %2, version: %3, last_integrated_remote_version: %4, "
8✔
41
                         "origin_file_ident: %5, timestamp: %6)",
8✔
42
                         msg, m_log->get_string(m_last_table_name), m_log->version,
8✔
43
                         m_log->last_integrated_remote_version, m_log->origin_file_ident, m_log->origin_timestamp));
8✔
44
    }
8✔
45
    else if (m_log) {
×
46
        // If all we have is a changeset, then we should log whatever we can about it.
47
        throw_bad_transaction_log(util::format("%1 (version: %2, last_integrated_remote_version: %3, "
×
48
                                               "origin_file_ident: %4, timestamp: %5)",
×
49
                                               msg, m_log->version, m_log->last_integrated_remote_version,
×
50
                                               m_log->origin_file_ident, m_log->origin_timestamp));
×
51
    }
×
52
    throw_bad_transaction_log(std::move(msg));
8✔
53
}
8✔
54

55
template <class... Params>
56
REALM_NORETURN void InstructionApplier::bad_transaction_log(const char* msg, Params&&... params) const
57
{
8✔
58
    // FIXME: Avoid throwing in normal program flow (since changesets can come
4✔
59
    // in over the network, defective changesets are part of normal program
4✔
60
    // flow).
4✔
61
    bad_transaction_log(util::format(msg, std::forward<Params>(params)...));
8✔
62
}
8✔
63

64
StringData InstructionApplier::get_string(InternString str) const
65
{
700,430✔
66
    auto string = m_log->try_get_intern_string(str);
700,430✔
67
    if (REALM_UNLIKELY(!string))
700,430✔
68
        bad_transaction_log("string read fails");
343,540✔
69
    return m_log->get_string(*string);
700,430✔
70
}
700,430✔
71

72
StringData InstructionApplier::get_string(StringBufferRange range) const
73
{
175,446✔
74
    auto string = m_log->try_get_string(range);
175,446✔
75
    if (!string)
175,446✔
76
        bad_transaction_log("string read error");
×
77
    return *string;
175,446✔
78
}
175,446✔
79

80
BinaryData InstructionApplier::get_binary(StringBufferRange range) const
81
{
11,724✔
82
    auto string = m_log->try_get_string(range);
11,724✔
83
    if (!string)
11,724✔
84
        bad_transaction_log("binary read error");
×
85
    return BinaryData{string->data(), string->size()};
11,724✔
86
}
11,724✔
87

88
TableRef InstructionApplier::table_for_class_name(StringData class_name) const
89
{
56✔
90
    if (class_name.size() > Group::max_class_name_length)
56✔
91
        bad_transaction_log("class name too long");
×
92
    Group::TableNameBuffer buffer;
56✔
93
    return m_transaction.get_table(Group::class_name_to_table_name(class_name, buffer));
56✔
94
}
56✔
95

96
template <typename T>
97
struct TemporarySwapOut {
98
    explicit TemporarySwapOut(T& target)
99
        : target(target)
100
        , backup()
101
    {
11,308✔
102
        std::swap(target, backup);
11,308✔
103
    }
11,308✔
104

105
    ~TemporarySwapOut()
106
    {
11,308✔
107
        std::swap(backup, target);
11,308✔
108
    }
11,308✔
109

110
    T& target;
111
    T backup;
112
};
113

114
void InstructionApplier::operator()(const Instruction::AddTable& instr)
115
{
3,702✔
116
    auto table_name = get_table_name(instr);
3,702✔
117

1,778✔
118
    // Temporarily swap out the last object key so it doesn't get included in error messages
1,778✔
119
    TemporarySwapOut<decltype(m_last_object_key)> last_object_key_guard(m_last_object_key);
3,702✔
120

1,778✔
121
    auto add_table = util::overload{
3,702✔
122
        [&](const Instruction::AddTable::TopLevelTable& spec) {
3,592✔
123
            auto table_type = (spec.is_asymmetric ? Table::Type::TopLevelAsymmetric : Table::Type::TopLevel);
3,470✔
124
            if (spec.pk_type == Instruction::Payload::Type::GlobalKey) {
3,482✔
125
                m_transaction.get_or_add_table(table_name, table_type);
8✔
126
            }
8✔
127
            else {
3,474✔
128
                if (!is_valid_key_type(spec.pk_type)) {
3,474✔
129
                    bad_transaction_log("Invalid primary key type '%1' while adding table '%2'", int8_t(spec.pk_type),
×
130
                                        table_name);
×
131
                }
×
132
                DataType pk_type = get_data_type(spec.pk_type);
3,474✔
133
                StringData pk_field = get_string(spec.pk_field);
3,474✔
134
                bool nullable = spec.pk_nullable;
3,474✔
135

1,664✔
136
                if (!m_transaction.get_or_add_table_with_primary_key(table_name, pk_type, pk_field, nullable,
3,474✔
137
                                                                     table_type)) {
1,664✔
138
                    bad_transaction_log("AddTable: The existing table '%1' has different properties", table_name);
×
139
                }
×
140
            }
3,474✔
141
        },
3,482✔
142
        [&](const Instruction::AddTable::EmbeddedTable&) {
1,888✔
143
            if (TableRef table = m_transaction.get_table(table_name)) {
220✔
144
                if (!table->is_embedded()) {
×
145
                    bad_transaction_log("AddTable: The existing table '%1' is not embedded", table_name);
×
146
                }
×
147
            }
×
148
            else {
220✔
149
                m_transaction.add_table(table_name, Table::Type::Embedded);
220✔
150
            }
220✔
151
        },
220✔
152
    };
3,702✔
153

1,778✔
154
    mpark::visit(std::move(add_table), instr.type);
3,702✔
155
}
3,702✔
156

157
void InstructionApplier::operator()(const Instruction::EraseTable& instr)
158
{
206✔
159
    auto table_name = get_table_name(instr);
206✔
160
    // Temporarily swap out the last object key so it doesn't get included in error messages
86✔
161
    TemporarySwapOut<decltype(m_last_object_key)> last_object_key_guard(m_last_object_key);
206✔
162

86✔
163
    if (REALM_UNLIKELY(REALM_COVER_NEVER(!m_transaction.has_table(table_name)))) {
206✔
164
        // FIXME: Should EraseTable be considered idempotent?
165
        bad_transaction_log("table does not exist");
×
166
    }
×
167

86✔
168
    m_transaction.remove_table(table_name);
206✔
169
}
206✔
170

171
void InstructionApplier::operator()(const Instruction::CreateObject& instr)
172
{
45,320✔
173
    auto table = get_table(instr);
45,320✔
174
    ColKey pk_col = table->get_primary_key_column();
45,320✔
175
    m_last_object_key = instr.object;
45,320✔
176

22,140✔
177
    mpark::visit(
45,320✔
178
        util::overload{
45,320✔
179
            [&](mpark::monostate) {
22,156✔
180
                if (!pk_col) {
32✔
181
                    bad_transaction_log("CreateObject(NULL) on table without a primary key");
×
182
                }
×
183
                if (!table->is_nullable(pk_col)) {
32✔
184
                    bad_transaction_log("CreateObject(NULL) on a table with a non-nullable primary key");
×
185
                }
×
186
                m_last_object = table->create_object_with_primary_key(util::none);
32✔
187
            },
32✔
188
            [&](int64_t pk) {
40,764✔
189
                if (!pk_col) {
37,042✔
190
                    bad_transaction_log("CreateObject(Int) on table without a primary key");
×
191
                }
×
192
                if (table->get_column_type(pk_col) != type_Int) {
37,042✔
193
                    bad_transaction_log("CreateObject(Int) on a table with primary key type %1",
×
194
                                        table->get_column_type(pk_col));
×
195
                }
×
196
                m_last_object = table->create_object_with_primary_key(pk);
37,042✔
197
            },
37,042✔
198
            [&](InternString pk) {
22,558✔
199
                if (!pk_col) {
798✔
200
                    bad_transaction_log("CreateObject(String) on table without a primary key");
×
201
                }
×
202
                if (table->get_column_type(pk_col) != type_String) {
798✔
203
                    bad_transaction_log("CreateObject(String) on a table with primary key type %1",
×
204
                                        table->get_column_type(pk_col));
×
205
                }
×
206
                StringData str = get_string(pk);
798✔
207
                m_last_object = table->create_object_with_primary_key(str);
798✔
208
            },
798✔
209
            [&](const ObjectId& id) {
26,226✔
210
                if (!pk_col) {
7,380✔
211
                    bad_transaction_log("CreateObject(ObjectId) on table without a primary key");
×
212
                }
×
213
                if (table->get_column_type(pk_col) != type_ObjectId) {
7,380✔
214
                    bad_transaction_log("CreateObject(ObjectId) on a table with primary key type %1",
×
215
                                        table->get_column_type(pk_col));
×
216
                }
×
217
                m_last_object = table->create_object_with_primary_key(id);
7,380✔
218
            },
7,380✔
219
            [&](const UUID& id) {
22,164✔
220
                if (!pk_col) {
48✔
221
                    bad_transaction_log("CreateObject(UUID) on table without a primary key");
×
222
                }
×
223
                if (table->get_column_type(pk_col) != type_UUID) {
48✔
224
                    bad_transaction_log("CreateObject(UUID) on a table with primary key type %1",
×
225
                                        table->get_column_type(pk_col));
×
226
                }
×
227
                m_last_object = table->create_object_with_primary_key(id);
48✔
228
            },
48✔
229
            [&](GlobalKey key) {
22,146✔
230
                if (pk_col) {
12✔
231
                    bad_transaction_log("CreateObject(GlobalKey) on table with a primary key");
×
232
                }
×
233
                m_last_object = table->create_object(key);
12✔
234
            },
12✔
235
        },
45,320✔
236
        instr.object);
45,320✔
237
}
45,320✔
238

239
void InstructionApplier::operator()(const Instruction::EraseObject& instr)
240
{
3,470✔
241
    // FIXME: Log actions.
1,670✔
242
    // Note: EraseObject is idempotent.
1,670✔
243
    if (auto obj = get_top_object(instr, "EraseObject")) {
3,470✔
244
        // This call will prevent incoming links to be nullified/deleted
1,030✔
245
        obj->invalidate();
2,158✔
246
    }
2,158✔
247
    m_last_object.reset();
3,470✔
248
}
3,470✔
249

250
template <class F>
251
void InstructionApplier::visit_payload(const Instruction::Payload& payload, F&& visitor)
252
{
630,344✔
253
    using Type = Instruction::Payload::Type;
630,344✔
254

308,962✔
255
    const auto& data = payload.data;
630,344✔
256
    switch (payload.type) {
630,344✔
257
        case Type::ObjectValue:
2,676✔
258
            return visitor(Instruction::Payload::ObjectValue{});
2,676✔
259
        case Type::Set:
✔
260
            return visitor(Instruction::Payload::Set{});
×
261
        case Type::List:
✔
262
            return visitor(Instruction::Payload::List{});
×
263
        case Type::Dictionary:
✔
264
            return visitor(Instruction::Payload::Dictionary{});
×
265
        case Type::Erased:
196✔
266
            return visitor(Instruction::Payload::Erased{});
196✔
267
        case Type::GlobalKey:
✔
268
            return visitor(realm::util::none); // FIXME: Not sure about this
×
269
        case Type::Null:
398✔
270
            return visitor(realm::util::none);
398✔
271
        case Type::Int:
431,564✔
272
            return visitor(data.integer);
431,564✔
273
        case Type::Bool:
184✔
274
            return visitor(data.boolean);
184✔
275
        case Type::String: {
175,446✔
276
            StringData value = get_string(data.str);
175,446✔
277
            return visitor(value);
175,446✔
278
        }
×
279
        case Type::Binary: {
11,724✔
280
            BinaryData value = get_binary(data.binary);
11,724✔
281
            return visitor(value);
11,724✔
282
        }
×
283
        case Type::Timestamp:
3,086✔
284
            return visitor(data.timestamp);
3,086✔
285
        case Type::Float:
260✔
286
            return visitor(data.fnum);
260✔
287
        case Type::Double:
1,192✔
288
            return visitor(data.dnum);
1,192✔
289
        case Type::Decimal:
168✔
290
            return visitor(data.decimal);
168✔
291
        case Type::Link: {
2,036✔
292
            StringData class_name = get_string(data.link.target_table);
2,036✔
293
            Group::TableNameBuffer buffer;
2,036✔
294
            StringData target_table_name = Group::class_name_to_table_name(class_name, buffer);
2,036✔
295
            TableRef target_table = m_transaction.get_table(target_table_name);
2,036✔
296
            if (!target_table) {
2,036✔
297
                bad_transaction_log("Link with invalid target table '%1'", target_table_name);
×
298
            }
×
299
            if (target_table->is_embedded()) {
2,036✔
300
                bad_transaction_log("Link to embedded table '%1'", target_table_name);
×
301
            }
×
302
            ObjKey target = get_object_key(*target_table, data.link.target);
2,036✔
303
            ObjLink link = ObjLink{target_table->get_key(), target};
2,036✔
304
            return visitor(link);
2,036✔
305
        }
×
306
        case Type::ObjectId:
1,128✔
307
            return visitor(data.object_id);
1,128✔
308
        case Type::UUID:
304✔
309
            return visitor(data.uuid);
304✔
310
    }
630,344✔
311
}
630,344✔
312

313
void InstructionApplier::operator()(const Instruction::Update& instr)
314
{
465,808✔
315
    struct UpdateResolver : public PathResolver {
465,808✔
316
        UpdateResolver(InstructionApplier* applier, const Instruction::Update& instr)
465,808✔
317
            : PathResolver(applier, instr, "Update")
465,808✔
318
            , m_instr(instr)
465,808✔
319
        {
465,814✔
320
        }
465,814✔
321
        void on_property(Obj& obj, ColKey col) override
465,808✔
322
        {
464,308✔
323
            // Update of object field.
224,956✔
324

224,956✔
325
            auto table = obj.get_table();
462,830✔
326
            auto table_name = table->get_name();
462,830✔
327
            auto field_name = table->get_column_name(col);
462,830✔
328
            auto data_type = DataType(col.get_type());
462,830✔
329

224,956✔
330
            auto visitor = [&](const mpark::variant<ObjLink, Mixed, Instruction::Payload::ObjectValue,
462,830✔
331
                                                    Instruction::Payload::Dictionary, Instruction::Payload::List,
462,830✔
332
                                                    Instruction::Payload::Set, Instruction::Payload::Erased>& arg) {
462,930✔
333
                if (const auto link_ptr = mpark::get_if<ObjLink>(&arg)) {
462,930✔
334
                    if (data_type == type_Mixed || data_type == type_TypedLink) {
84✔
335
                        obj.set_any(col, *link_ptr, m_instr.is_default);
×
336
                    }
×
337
                    else if (data_type == type_Link) {
84✔
338
                        // Validate target table.
42✔
339
                        auto target_table = table->get_link_target(col);
84✔
340
                        if (target_table->get_key() != link_ptr->get_table_key()) {
84✔
341
                            m_applier->bad_transaction_log(
×
342
                                "Update: Target table mismatch (expected %1, got %2)", target_table->get_name(),
×
343
                                m_applier->m_transaction.get_table(link_ptr->get_table_key())->get_name());
×
344
                        }
×
345
                        obj.set<ObjKey>(col, link_ptr->get_obj_key(), m_instr.is_default);
84✔
346
                    }
84✔
347
                    else {
×
348
                        m_applier->bad_transaction_log("Update: Type mismatch in '%2.%1' (expected %3, got %4)",
×
349
                                                       field_name, table_name, col.get_type(), type_Link);
×
350
                    }
×
351
                }
84✔
352
                else if (const auto mixed_ptr = mpark::get_if<Mixed>(&arg)) {
462,846✔
353
                    if (mixed_ptr->is_null()) {
462,216✔
354
                        if (col.is_nullable()) {
218✔
355
                            obj.set_null(col, m_instr.is_default);
218✔
356
                        }
218✔
357
                        else {
×
358
                            m_applier->bad_transaction_log("Update: NULL in non-nullable field '%2.%1'", field_name,
×
359
                                                           table_name);
×
360
                        }
×
361
                    }
218✔
362
                    else if (data_type == type_Mixed || mixed_ptr->get_type() == data_type) {
462,000✔
363
                        obj.set_any(col, *mixed_ptr, m_instr.is_default);
461,960✔
364
                    }
461,960✔
365
                    else {
2,147,483,687✔
366
                        m_applier->bad_transaction_log("Update: Type mismatch in '%2.%1' (expected %3, got %4)",
2,147,483,687✔
367
                                                       field_name, table_name, col.get_type(), mixed_ptr->get_type());
2,147,483,687✔
368
                    }
2,147,483,687✔
369
                }
462,216✔
370
                else if (const auto obj_val_ptr = mpark::get_if<Instruction::Payload::ObjectValue>(&arg)) {
658✔
371
                    if (obj.is_null(col)) {
652✔
372
                        obj.create_and_set_linked_object(col);
624✔
373
                    }
624✔
374
                }
652✔
375
                else if (const auto erase_ptr = mpark::get_if<Instruction::Payload::Erased>(&arg)) {
2,147,483,653✔
376
                    m_applier->bad_transaction_log("Update: Dictionary erase at object field");
×
377
                }
×
378
                else if (mpark::get_if<Instruction::Payload::Dictionary>(&arg)) {
2,147,483,653✔
379
                    obj.set_collection(col, CollectionType::Dictionary);
×
380
                }
×
381
                else if (mpark::get_if<Instruction::Payload::List>(&arg)) {
2,147,483,653✔
382
                    obj.set_collection(col, CollectionType::List);
×
383
                }
×
384
                else if (mpark::get_if<Instruction::Payload::Set>(&arg)) {
2,147,483,653✔
385
                    obj.set_collection(col, CollectionType::Set);
×
386
                }
×
387
            };
462,930✔
388

224,956✔
389
            m_applier->visit_payload(m_instr.value, visitor);
462,830✔
390
        }
462,830✔
391
        Status on_list_index(LstBase& list, uint32_t index) override
465,808✔
392
        {
226,568✔
393
            // Update of list element.
172✔
394

172✔
395
            auto col = list.get_col_key();
306✔
396
            auto data_type = DataType(col.get_type());
306✔
397
            auto table = list.get_table();
306✔
398
            auto table_name = table->get_name();
306✔
399
            auto field_name = table->get_column_name(col);
306✔
400

172✔
401
            auto visitor = util::overload{
306✔
402
                [&](const ObjLink& link) {
250✔
403
                    if (data_type == type_TypedLink) {
156✔
404
                        REALM_ASSERT(dynamic_cast<Lst<ObjLink>*>(&list));
×
405
                        auto& link_list = static_cast<Lst<ObjLink>&>(list);
×
406
                        link_list.set(index, link);
×
407
                    }
×
408
                    else if (data_type == type_Mixed) {
156✔
409
                        REALM_ASSERT(dynamic_cast<Lst<Mixed>*>(&list));
×
410
                        auto& mixed_list = static_cast<Lst<Mixed>&>(list);
×
411
                        mixed_list.set(index, link);
×
412
                    }
×
413
                    else if (data_type == type_Link) {
156✔
414
                        REALM_ASSERT(dynamic_cast<Lst<ObjKey>*>(&list));
156✔
415
                        auto& link_list = static_cast<Lst<ObjKey>&>(list);
156✔
416
                        // Validate the target.
78✔
417
                        auto target_table = table->get_link_target(col);
156✔
418
                        if (target_table->get_key() != link.get_table_key()) {
156✔
419
                            m_applier->bad_transaction_log(
×
420
                                "Update: Target table mismatch (expected '%1', got '%2')", target_table->get_name(),
×
421
                                m_applier->m_transaction.get_table(link.get_table_key())->get_name());
×
422
                        }
×
423
                        link_list.set(index, link.get_obj_key());
156✔
424
                    }
156✔
425
                    else {
×
426
                        m_applier->bad_transaction_log(
×
427
                            "Update: Type mismatch in list at '%2.%1' (expected link type, was %3)", field_name,
×
428
                            table_name, data_type);
×
429
                    }
×
430
                },
156✔
431
                [&](Mixed value) {
228✔
432
                    if (value.is_null()) {
150✔
433
                        if (col.is_nullable()) {
×
434
                            list.set_null(index);
×
435
                        }
×
436
                        else {
×
437
                            m_applier->bad_transaction_log("Update: NULL in non-nullable list '%2.%1'", field_name,
×
438
                                                           table_name);
×
439
                        }
×
440
                    }
×
441
                    else {
150✔
442
                        if (data_type == type_Mixed || value.get_type() == data_type) {
150✔
443
                            list.set_any(index, value);
150✔
444
                        }
150✔
445
                        else {
×
446
                            m_applier->bad_transaction_log(
×
447
                                "Update: Type mismatch in list at '%2.%1' (expected %3, got %4)", field_name,
×
448
                                table_name, data_type, value.get_type());
×
449
                        }
×
450
                    }
150✔
451
                },
150✔
452
                [&](const Instruction::Payload::ObjectValue&) {
172✔
453
                    // Embedded object creation is idempotent, and link lists cannot
454
                    // contain nulls, so this is a no-op.
455
                },
×
456
                [&](const Instruction::Payload::Dictionary&) {
172✔
457
                    list.set_collection(size_t(index), CollectionType::Dictionary);
×
458
                },
×
459
                [&](const Instruction::Payload::List&) {
172✔
460
                    list.set_collection(size_t(index), CollectionType::List);
×
461
                },
×
462
                [&](const Instruction::Payload::Set&) {
172✔
463
                    list.set_collection(size_t(index), CollectionType::Set);
×
464
                },
×
465
                [&](const Instruction::Payload::Erased&) {
172✔
466
                    m_applier->bad_transaction_log("Update: Dictionary erase of list element");
×
467
                },
×
468
            };
306✔
469

172✔
470
            m_applier->visit_payload(m_instr.value, visitor);
306✔
471
            return Status::Pending;
306✔
472
        }
306✔
473
        Status on_dictionary_key(Dictionary& dict, Mixed key) override
465,808✔
474
        {
227,782✔
475
            // Update (insert) of dictionary element.
1,348✔
476

1,348✔
477
            auto visitor = util::overload{
2,696✔
478
                [&](Mixed value) {
2,416✔
479
                    if (value.is_null()) {
2,136✔
480
                        // FIXME: Separate handling of NULL is needed because
56✔
481
                        // `Mixed::get_type()` asserts on NULL.
56✔
482
                        dict.insert(key, value);
112✔
483
                    }
112✔
484
                    else if (value.get_type() == type_Link) {
2,024✔
485
                        m_applier->bad_transaction_log("Update: Untyped links are not supported in dictionaries.");
×
486
                    }
×
487
                    else {
2,024✔
488
                        dict.insert(key, value);
2,024✔
489
                    }
2,024✔
490
                },
2,136✔
491
                [&](const Instruction::Payload::Erased&) {
1,446✔
492
                    dict.erase(key);
196✔
493
                },
196✔
494
                [&](const Instruction::Payload::ObjectValue&) {
1,530✔
495
                    dict.create_and_insert_linked_object(key);
364✔
496
                },
364✔
497
                [&](const Instruction::Payload::Dictionary&) {
1,348✔
498
                    dict.insert_collection(key.get_string(), CollectionType::Dictionary);
×
499
                },
×
500
                [&](const Instruction::Payload::List&) {
1,348✔
501
                    dict.insert_collection(key.get_string(), CollectionType::List);
×
502
                },
×
503
                [&](const Instruction::Payload::Set&) {
1,348✔
504
                    dict.insert_collection(key.get_string(), CollectionType::Set);
×
505
                },
×
506
            };
2,696✔
507

1,348✔
508
            m_applier->visit_payload(m_instr.value, visitor);
2,696✔
509
            return Status::Pending;
2,696✔
510
        }
2,696✔
511

226,434✔
512
    private:
465,808✔
513
        const Instruction::Update& m_instr;
465,808✔
514
    };
465,808✔
515
    UpdateResolver resolver(this, instr);
465,808✔
516
    resolver.resolve();
465,808✔
517
}
465,808✔
518

519
void InstructionApplier::operator()(const Instruction::AddInteger& instr)
520
{
2,648✔
521
    // FIXME: Implement increments of array elements, dictionary values.
1,310✔
522
    struct AddIntegerResolver : public PathResolver {
2,648✔
523
        AddIntegerResolver(InstructionApplier* applier, const Instruction::AddInteger& instr)
2,648✔
524
            : PathResolver(applier, instr, "AddInteger")
2,648✔
525
            , m_instr(instr)
2,648✔
526
        {
2,648✔
527
        }
2,648✔
528
        void on_property(Obj& obj, ColKey col)
2,648✔
529
        {
2,648✔
530
            // Increment of object field.
1,310✔
531
            if (!obj.is_null(col)) {
2,648✔
532
                try {
2,592✔
533
                    obj.add_int(col, m_instr.value);
2,592✔
534
                }
2,592✔
535
                catch (const LogicError&) {
1,282✔
536
                    auto table = obj.get_table();
×
537
                    m_applier->bad_transaction_log("AddInteger: Not an integer field '%2.%1'",
×
538
                                                   table->get_column_name(col), table->get_name());
×
539
                }
×
540
            }
2,592✔
541
        }
2,648✔
542

1,310✔
543
    private:
2,648✔
544
        const Instruction::AddInteger& m_instr;
2,648✔
545
    };
2,648✔
546
    AddIntegerResolver resolver(this, instr);
2,648✔
547
    resolver.resolve();
2,648✔
548
}
2,648✔
549

550
void InstructionApplier::operator()(const Instruction::AddColumn& instr)
551
{
7,400✔
552
    using Type = Instruction::Payload::Type;
7,400✔
553
    using CollectionType = Instruction::AddColumn::CollectionType;
7,400✔
554

3,240✔
555
    // Temporarily swap out the last object key so it doesn't get included in error messages
3,240✔
556
    TemporarySwapOut<decltype(m_last_object_key)> last_object_key_guard(m_last_object_key);
7,400✔
557

3,240✔
558
    auto table = get_table(instr, "AddColumn");
7,400✔
559
    auto col_name = get_string(instr.field);
7,400✔
560

3,240✔
561
    if (ColKey existing_key = table->get_column_key(col_name)) {
7,400✔
562
        DataType new_type = get_data_type(instr.type);
264✔
563
        ColumnType existing_type = existing_key.get_type();
264✔
564
        if (existing_type != ColumnType(new_type)) {
264✔
565
            bad_transaction_log("AddColumn: Schema mismatch for existing column in '%1.%2' (expected %3, got %4)",
8✔
566
                                table->get_name(), col_name, existing_type, new_type);
8✔
567
        }
8✔
568
        bool existing_is_list = existing_key.is_list();
264✔
569
        if ((instr.collection_type == CollectionType::List) != existing_is_list) {
264✔
570
            bad_transaction_log(
×
571
                "AddColumn: Schema mismatch for existing column in '%1.%2' (existing is%3 a list, the other is%4)",
×
572
                table->get_name(), col_name, existing_is_list ? "" : " not", existing_is_list ? " not" : "");
×
573
        }
×
574
        bool existing_is_set = existing_key.is_set();
264✔
575
        if ((instr.collection_type == CollectionType::Set) != existing_is_set) {
264✔
576
            bad_transaction_log(
×
577
                "AddColumn: Schema mismatch for existing column in '%1.%2' (existing is%3 a set, the other is%4)",
×
578
                table->get_name(), col_name, existing_is_set ? "" : " not", existing_is_set ? " not" : "");
×
579
        }
×
580
        bool existing_is_dict = existing_key.is_dictionary();
264✔
581
        if ((instr.collection_type == CollectionType::Dictionary) != existing_is_dict) {
264✔
582
            bad_transaction_log("AddColumn: Schema mismatch for existing column in '%1.%2' (existing is%3 a "
×
583
                                "dictionary, the other is%4)",
×
584
                                table->get_name(), col_name, existing_is_dict ? "" : " not",
×
585
                                existing_is_dict ? " not" : "");
×
586
        }
×
587
        if (new_type == type_Link) {
264✔
588
            Group::TableNameBuffer buffer;
16✔
589
            auto target_table_name = Group::class_name_to_table_name(get_string(instr.link_target_table), buffer);
16✔
590
            if (target_table_name != table->get_link_target(existing_key)->get_name()) {
16✔
591
                bad_transaction_log("AddColumn: Schema mismatch for existing column in '%1.%2' (link targets differ)",
×
592
                                    table->get_name(), col_name);
×
593
            }
×
594
        }
16✔
595
        return;
264✔
596
    }
264✔
597

3,108✔
598
    if (instr.collection_type == CollectionType::Dictionary && instr.key_type != Type::String) {
7,136✔
599
        bad_transaction_log("AddColumn '%1.%3' adding dictionary column with non-string keys", table->get_name(),
×
600
                            col_name);
×
601
    }
×
602

3,108✔
603
    if (instr.type != Type::Link) {
7,136✔
604
        DataType type = get_data_type(instr.type);
5,972✔
605
        switch (instr.collection_type) {
5,972✔
606
            case CollectionType::Single: {
5,118✔
607
                table->add_column(type, col_name, instr.nullable);
5,118✔
608
                break;
5,118✔
609
            }
×
610
            case CollectionType::List: {
622✔
611
                table->add_column_list(type, col_name, instr.nullable);
622✔
612
                break;
622✔
613
            }
×
614
            case CollectionType::Dictionary: {
104✔
615
                DataType key_type = get_data_type(instr.key_type);
104✔
616
                table->add_column_dictionary(type, col_name, instr.nullable, key_type);
104✔
617
                break;
104✔
618
            }
×
619
            case CollectionType::Set: {
128✔
620
                table->add_column_set(type, col_name, instr.nullable);
128✔
621
                break;
128✔
622
            }
1,164✔
623
        }
1,164✔
624
    }
1,164✔
625
    else {
1,164✔
626
        Group::TableNameBuffer buffer;
1,164✔
627
        auto target_table_name = get_string(instr.link_target_table);
1,164✔
628
        if (target_table_name.size() != 0) {
1,164✔
629
            TableRef target = m_transaction.get_table(Group::class_name_to_table_name(target_table_name, buffer));
1,164✔
630
            if (!target) {
1,164✔
631
                bad_transaction_log("AddColumn(Link) '%1.%2' to table '%3' which doesn't exist", table->get_name(),
×
632
                                    col_name, target_table_name);
×
633
            }
×
634
            if (instr.collection_type == CollectionType::List) {
1,164✔
635
                table->add_column_list(*target, col_name);
600✔
636
            }
600✔
637
            else if (instr.collection_type == CollectionType::Set) {
564✔
638
                table->add_column_set(*target, col_name);
8✔
639
            }
8✔
640
            else if (instr.collection_type == CollectionType::Dictionary) {
556✔
641
                table->add_column_dictionary(*target, col_name);
32✔
642
            }
32✔
643
            else {
524✔
644
                REALM_ASSERT(instr.collection_type == CollectionType::Single);
524✔
645
                table->add_column(*target, col_name);
524✔
646
            }
524✔
647
        }
1,164✔
648
        else {
×
649
            if (instr.collection_type == CollectionType::List) {
×
650
                table->add_column_list(type_TypedLink, col_name);
×
651
            }
×
652
            else {
×
653
                REALM_ASSERT(instr.collection_type == CollectionType::Single);
×
654
                table->add_column(type_TypedLink, col_name);
×
655
            }
×
656
        }
×
657
    }
1,164✔
658
}
7,136✔
659

660
void InstructionApplier::operator()(const Instruction::EraseColumn& instr)
661
{
×
662
    // Temporarily swap out the last object key so it doesn't get included in error messages
663
    TemporarySwapOut<decltype(m_last_object_key)> last_object_key_guard(m_last_object_key);
×
664

665
    auto table = get_table(instr, "EraseColumn");
×
666
    auto col_name = get_string(instr.field);
×
667

668
    ColKey col = table->get_column_key(col_name);
×
669
    if (!col) {
×
670
        bad_transaction_log("EraseColumn '%1.%2' which doesn't exist", table->get_name(), col_name);
×
671
    }
×
672

673
    table->remove_column(col);
×
674
}
×
675

676
void InstructionApplier::operator()(const Instruction::ArrayInsert& instr)
677
{
162,152✔
678
    struct ArrayInsertResolver : public PathResolver {
162,152✔
679
        ArrayInsertResolver(InstructionApplier* applier, const Instruction::ArrayInsert& instr)
162,152✔
680
            : PathResolver(applier, instr, "ArrayInsert")
162,152✔
681
            , m_instr(instr)
162,152✔
682
        {
162,152✔
683
        }
162,152✔
684
        Status on_list_index(LstBase& list, uint32_t index) override
162,152✔
685
        {
162,152✔
686
            auto data_type = list.get_data_type();
162,152✔
687
            auto table = list.get_table();
162,152✔
688
            auto table_name = table->get_name();
162,152✔
689
            auto field_name = [&] {
81,254✔
690
                return table->get_column_name(list.get_col_key());
×
691
            };
×
692

81,254✔
693
            if (index > m_instr.prior_size) {
162,152✔
694
                m_applier->bad_transaction_log("ArrayInsert: Invalid insertion index (index = %1, prior_size = %2)",
×
695
                                               index, m_instr.prior_size);
×
696
            }
×
697

81,254✔
698
            if (index > list.size()) {
162,152✔
699
                m_applier->bad_transaction_log("ArrayInsert: Index out of bounds (%1 > %2)", index, list.size());
×
700
            }
×
701

81,254✔
702
            if (m_instr.prior_size != list.size()) {
162,152✔
703
                m_applier->bad_transaction_log("ArrayInsert: Invalid prior_size (list size = %1, prior_size = %2)",
×
704
                                               list.size(), m_instr.prior_size);
×
705
            }
×
706

81,254✔
707
            auto inserter = util::overload{
162,152✔
708
                [&](const ObjLink& link) {
81,880✔
709
                    if (data_type == type_TypedLink) {
1,252✔
710
                        REALM_ASSERT(dynamic_cast<Lst<ObjLink>*>(&list));
×
711
                        auto& link_list = static_cast<Lst<ObjLink>&>(list);
×
712
                        link_list.insert(index, link);
×
713
                    }
×
714
                    else if (data_type == type_Mixed) {
1,252✔
715
                        REALM_ASSERT(dynamic_cast<Lst<Mixed>*>(&list));
164✔
716
                        auto& mixed_list = static_cast<Lst<Mixed>&>(list);
164✔
717
                        mixed_list.insert(index, link);
164✔
718
                    }
164✔
719
                    else if (data_type == type_Link) {
1,088✔
720
                        REALM_ASSERT(dynamic_cast<Lst<ObjKey>*>(&list));
1,088✔
721
                        auto& link_list = static_cast<Lst<ObjKey>&>(list);
1,088✔
722
                        // Validate the target.
544✔
723
                        auto target_table = table->get_link_target(list.get_col_key());
1,088✔
724
                        if (target_table->get_key() != link.get_table_key()) {
1,088✔
725
                            m_applier->bad_transaction_log(
×
726
                                "ArrayInsert: Target table mismatch (expected '%1', got '%2')",
×
727
                                target_table->get_name(),
×
728
                                m_applier->m_transaction.get_table(link.get_table_key())->get_name());
×
729
                        }
×
730
                        link_list.insert(index, link.get_obj_key());
1,088✔
731
                    }
1,088✔
732
                    else {
×
733
                        m_applier->bad_transaction_log(
×
734
                            "ArrayInsert: Type mismatch in list at '%2.%1' (expected link type, was %3)",
×
735
                            field_name(), table_name, data_type);
×
736
                    }
×
737
                },
1,252✔
738
                [&](Mixed value) {
160,696✔
739
                    if (data_type == type_Mixed) {
159,240✔
740
                        list.insert_any(index, value);
28✔
741
                    }
28✔
742
                    else if (value.is_null()) {
159,212✔
743
                        if (list.get_col_key().is_nullable()) {
48✔
744
                            list.insert_null(index);
48✔
745
                        }
48✔
746
                        else {
×
747
                            m_applier->bad_transaction_log("ArrayInsert: NULL in non-nullable list '%2.%1'",
×
748
                                                           field_name(), table_name);
×
749
                        }
×
750
                    }
48✔
751
                    else {
159,164✔
752
                        if (value.get_type() == data_type) {
159,164✔
753
                            list.insert_any(index, value);
159,164✔
754
                        }
159,164✔
755
                        else {
×
756
                            m_applier->bad_transaction_log(
×
757
                                "ArrayInsert: Type mismatch in list at '%2.%1' (expected %3, got %4)", field_name(),
×
758
                                table_name, data_type, value.get_type());
×
759
                        }
×
760
                    }
159,164✔
761
                },
159,240✔
762
                [&](const Instruction::Payload::ObjectValue&) {
82,084✔
763
                    if (data_type == type_Link) {
1,660✔
764
                        auto target_table = list.get_table()->get_link_target(list.get_col_key());
1,660✔
765
                        if (!target_table->is_embedded()) {
1,660✔
766
                            m_applier->bad_transaction_log(
×
767
                                "ArrayInsert: Creation of embedded object of type '%1', which is not "
×
768
                                "an embedded table",
×
769
                                target_table->get_name());
×
770
                        }
×
771

830✔
772
                        REALM_ASSERT(dynamic_cast<LnkLst*>(&list));
1,660✔
773
                        auto& link_list = static_cast<LnkLst&>(list);
1,660✔
774
                        link_list.create_and_insert_linked_object(index);
1,660✔
775
                    }
1,660✔
776
                    else {
×
777
                        m_applier->bad_transaction_log(
×
778
                            "ArrayInsert: Creation of embedded object in non-link list field '%2.%1'", field_name(),
×
779
                            table_name);
×
780
                    }
×
781
                },
1,660✔
782
                [&](const Instruction::Payload::Dictionary&) {
81,254✔
783
                    REALM_ASSERT(dynamic_cast<Lst<Mixed>*>(&list));
×
784
                    auto& mixed_list = static_cast<Lst<Mixed>&>(list);
×
785
                    mixed_list.insert_collection(size_t(index), CollectionType::Dictionary);
×
786
                },
×
787
                [&](const Instruction::Payload::List&) {
81,254✔
788
                    REALM_ASSERT(dynamic_cast<Lst<Mixed>*>(&list));
×
789
                    auto& mixed_list = static_cast<Lst<Mixed>&>(list);
×
790
                    mixed_list.insert_collection(size_t(index), CollectionType::List);
×
791
                },
×
792
                [&](const Instruction::Payload::Set&) {
81,254✔
793
                    REALM_ASSERT(dynamic_cast<Lst<Mixed>*>(&list));
×
794
                    auto& mixed_list = static_cast<Lst<Mixed>&>(list);
×
795
                    mixed_list.insert_collection(size_t(index), CollectionType::Set);
×
796
                },
×
797
                [&](const Instruction::Payload::Erased&) {
81,254✔
798
                    m_applier->bad_transaction_log("Dictionary erase payload for ArrayInsert");
×
799
                },
×
800
            };
162,152✔
801

81,254✔
802
            m_applier->visit_payload(m_instr.value, inserter);
162,152✔
803
            return Status::Pending;
162,152✔
804
        }
162,152✔
805

81,254✔
806
    private:
162,152✔
807
        const Instruction::ArrayInsert& m_instr;
162,152✔
808
    };
162,152✔
809
    ArrayInsertResolver(this, instr).resolve();
162,152✔
810
}
162,152✔
811

812
void InstructionApplier::operator()(const Instruction::ArrayMove& instr)
813
{
112✔
814
    struct ArrayMoveResolver : public PathResolver {
112✔
815
        ArrayMoveResolver(InstructionApplier* applier, const Instruction::ArrayMove& instr)
112✔
816
            : PathResolver(applier, instr, "ArrayMove")
112✔
817
            , m_instr(instr)
112✔
818
        {
112✔
819
        }
112✔
820
        Status on_list_index(LstBase& list, uint32_t index) override
112✔
821
        {
112✔
822
            if (index >= list.size()) {
112✔
823
                m_applier->bad_transaction_log("ArrayMove from out of bounds (%1 >= %2)", m_instr.index(),
×
824
                                               list.size());
×
825
            }
×
826
            if (m_instr.ndx_2 >= list.size()) {
112✔
827
                m_applier->bad_transaction_log("ArrayMove to out of bounds (%1 >= %2)", m_instr.ndx_2, list.size());
×
828
            }
×
829
            if (index == m_instr.ndx_2) {
112✔
830
                // FIXME: Does this really need to be an error?
831
                m_applier->bad_transaction_log("ArrayMove to same location (%1)", m_instr.index());
×
832
            }
×
833
            if (m_instr.prior_size != list.size()) {
112✔
834
                m_applier->bad_transaction_log("ArrayMove: Invalid prior_size (list size = %1, prior_size = %2)",
×
835
                                               list.size(), m_instr.prior_size);
×
836
            }
×
837
            list.move(index, m_instr.ndx_2);
112✔
838
            return Status::Pending;
112✔
839
        }
112✔
840

56✔
841
    private:
112✔
842
        const Instruction::ArrayMove& m_instr;
112✔
843
    };
112✔
844
    ArrayMoveResolver(this, instr).resolve();
112✔
845
}
112✔
846

847
void InstructionApplier::operator()(const Instruction::ArrayErase& instr)
848
{
288✔
849
    struct ArrayEraseResolver : public PathResolver {
288✔
850
        ArrayEraseResolver(InstructionApplier* applier, const Instruction::ArrayErase& instr)
288✔
851
            : PathResolver(applier, instr, "ArrayErase")
288✔
852
            , m_instr(instr)
288✔
853
        {
288✔
854
        }
288✔
855
        Status on_list_index(LstBase& list, uint32_t index) override
288✔
856
        {
288✔
857
            if (index >= m_instr.prior_size) {
288✔
858
                m_applier->bad_transaction_log("ArrayErase: Invalid index (index = %1, prior_size = %2)", index,
×
859
                                               m_instr.prior_size);
×
860
            }
×
861
            if (index >= list.size()) {
288✔
862
                m_applier->bad_transaction_log("ArrayErase: Index out of bounds (%1 >= %2)", index, list.size());
×
863
            }
×
864
            if (m_instr.prior_size != list.size()) {
288✔
865
                m_applier->bad_transaction_log("ArrayErase: Invalid prior_size (list size = %1, prior_size = %2)",
×
866
                                               list.size(), m_instr.prior_size);
×
867
            }
×
868
            list.remove(index, index + 1);
288✔
869
            return Status::Pending;
288✔
870
        }
288✔
871

172✔
872
    private:
288✔
873
        const Instruction::ArrayErase& m_instr;
288✔
874
    };
288✔
875
    ArrayEraseResolver(this, instr).resolve();
288✔
876
}
288✔
877

878
void InstructionApplier::operator()(const Instruction::Clear& instr)
879
{
200✔
880
    struct ClearResolver : public PathResolver {
200✔
881
        ClearResolver(InstructionApplier* applier, const Instruction::Clear& instr)
200✔
882
            : PathResolver(applier, instr, "Clear")
200✔
883
        {
200✔
884
        }
200✔
885
        void on_list(LstBase& list) override
200✔
886
        {
144✔
887
            list.clear();
88✔
888
        }
88✔
889
        void on_dictionary(Dictionary& dict) override
200✔
890
        {
124✔
891
            dict.clear();
48✔
892
        }
48✔
893
        void on_set(SetBase& set) override
200✔
894
        {
132✔
895
            set.clear();
64✔
896
        }
64✔
897
        void on_property(Obj& obj, ColKey col_key) override
200✔
898
        {
100✔
899
            if (col_key.get_type() == col_type_Mixed) {
×
900
                auto val = obj.get<Mixed>(col_key);
×
901
                if (val.is_type(type_Dictionary)) {
×
902
                    Dictionary dict(obj, col_key);
×
903
                    dict.clear();
×
904
                    return;
×
905
                }
×
906
                else if (val.is_type(type_List)) {
×
907
                    Lst<Mixed> list(obj, col_key);
×
908
                    list.clear();
×
909
                    return;
×
910
                }
×
911
                else if (val.is_type(type_Set)) {
×
912
                    Set<Mixed> set(obj, col_key);
×
913
                    set.clear();
×
914
                    return;
×
915
                }
×
916
            }
×
917

918
            PathResolver::on_property(obj, col_key);
×
919
        }
×
920
    };
200✔
921
    ClearResolver(this, instr).resolve();
200✔
922
}
200✔
923

924
bool InstructionApplier::allows_null_links(const Instruction::PathInstruction& instr,
925
                                           const std::string_view& instr_name)
926
{
40✔
927
    struct AllowsNullsResolver : public PathResolver {
40✔
928
        AllowsNullsResolver(InstructionApplier* applier, const Instruction::PathInstruction& instr,
40✔
929
                            const std::string_view& instr_name)
40✔
930
            : PathResolver(applier, instr, instr_name)
40✔
931
            , m_allows_nulls(false)
40✔
932
        {
40✔
933
        }
40✔
934
        Status on_list_index(LstBase&, uint32_t) override
40✔
935
        {
20✔
936
            return Status::Pending;
×
937
        }
×
938
        void on_list(LstBase&) override {}
20✔
939
        void on_set(SetBase&) override {}
20✔
940
        void on_dictionary(Dictionary&) override
40✔
941
        {
20✔
942
            m_allows_nulls = true;
×
943
        }
×
944
        Status on_dictionary_key(Dictionary&, Mixed) override
40✔
945
        {
40✔
946
            m_allows_nulls = true;
40✔
947
            return Status::Pending;
40✔
948
        }
40✔
949
        void on_property(Obj&, ColKey) override
40✔
950
        {
20✔
951
            m_allows_nulls = true;
×
952
        }
×
953
        bool allows_nulls()
40✔
954
        {
40✔
955
            resolve();
40✔
956
            return m_allows_nulls;
40✔
957
        }
40✔
958

20✔
959
    private:
40✔
960
        bool m_allows_nulls;
40✔
961
    };
40✔
962
    return AllowsNullsResolver(this, instr, instr_name).allows_nulls();
40✔
963
}
40✔
964

965
std::string InstructionApplier::to_string(const Instruction::PathInstruction& instr) const
966
{
×
967
    REALM_ASSERT(m_log);
×
968
    std::stringstream ss;
×
969
    m_log->print_path(ss, instr.table, instr.object, instr.field, &instr.path);
×
970
    return ss.str();
×
971
}
×
972

973
bool InstructionApplier::check_links_exist(const Instruction::Payload& payload)
974
{
23,452✔
975
    bool valid_payload = true;
23,452✔
976
    using Type = Instruction::Payload::Type;
23,452✔
977
    if (payload.type == Type::Link) {
23,452✔
978
        StringData class_name = get_string(payload.data.link.target_table);
800✔
979
        Group::TableNameBuffer buffer;
800✔
980
        StringData target_table_name = Group::class_name_to_table_name(class_name, buffer);
800✔
981
        TableRef target_table = m_transaction.get_table(target_table_name);
800✔
982
        if (!target_table) {
800✔
983
            bad_transaction_log("Link with invalid target table '%1'", target_table_name);
×
984
        }
×
985
        if (target_table->is_embedded()) {
800✔
986
            bad_transaction_log("Link to embedded table '%1'", target_table_name);
×
987
        }
×
988
        Mixed linked_pk =
800✔
989
            mpark::visit(util::overload{[&](mpark::monostate) {
412✔
990
                                            return Mixed{}; // the link exists and the pk is null
24✔
991
                                        },
24✔
992
                                        [&](int64_t pk) {
780✔
993
                                            return Mixed{pk};
760✔
994
                                        },
760✔
995
                                        [&](InternString interned_pk) {
400✔
996
                                            return Mixed{get_string(interned_pk)};
×
997
                                        },
×
998
                                        [&](GlobalKey) -> Mixed {
400✔
999
                                            bad_transaction_log(
×
1000
                                                "Unexpected link to embedded object while validating a primary key");
×
1001
                                        },
×
1002
                                        [&](ObjectId pk) {
408✔
1003
                                            return Mixed{pk};
16✔
1004
                                        },
16✔
1005
                                        [&](UUID pk) {
400✔
1006
                                            return Mixed{pk};
×
1007
                                        }},
×
1008
                         payload.data.link.target);
800✔
1009

400✔
1010
        if (!target_table->find_primary_key(linked_pk)) {
800✔
1011
            valid_payload = false;
168✔
1012
        }
168✔
1013
    }
800✔
1014
    return valid_payload;
23,452✔
1015
}
23,452✔
1016

1017
void InstructionApplier::operator()(const Instruction::SetInsert& instr)
1018
{
1,750✔
1019
    struct SetInsertResolver : public PathResolver {
1,750✔
1020
        SetInsertResolver(InstructionApplier* applier, const Instruction::SetInsert& instr)
1,750✔
1021
            : PathResolver(applier, instr, "SetInsert")
1,750✔
1022
            , m_instr(instr)
1,750✔
1023
        {
1,750✔
1024
        }
1,750✔
1025
        void on_property(Obj& obj, ColKey col) override
1,750✔
1026
        {
876✔
1027
            // This better be a mixed column
1028
            REALM_ASSERT(col.get_type() == col_type_Mixed);
×
1029
            auto set = obj.get_set<Mixed>(col);
×
1030
            on_set(set);
×
1031
        }
×
1032
        void on_set(SetBase& set) override
1,750✔
1033
        {
1,750✔
1034
            auto col = set.get_col_key();
1,750✔
1035
            auto data_type = DataType(col.get_type());
1,750✔
1036
            auto table = set.get_table();
1,750✔
1037
            auto table_name = table->get_name();
1,750✔
1038
            auto field_name = table->get_column_name(col);
1,750✔
1039

876✔
1040
            auto inserter = util::overload{
1,750✔
1041
                [&](const ObjLink& link) {
972✔
1042
                    if (data_type == type_TypedLink) {
192✔
1043
                        REALM_ASSERT(dynamic_cast<Set<ObjLink>*>(&set));
×
1044
                        auto& link_set = static_cast<Set<ObjLink>&>(set);
×
1045
                        link_set.insert(link);
×
1046
                    }
×
1047
                    else if (data_type == type_Mixed) {
192✔
1048
                        REALM_ASSERT(dynamic_cast<Set<Mixed>*>(&set));
88✔
1049
                        auto& mixed_set = static_cast<Set<Mixed>&>(set);
88✔
1050
                        mixed_set.insert(link);
88✔
1051
                    }
88✔
1052
                    else if (data_type == type_Link) {
104✔
1053
                        REALM_ASSERT(dynamic_cast<Set<ObjKey>*>(&set));
104✔
1054
                        auto& link_set = static_cast<Set<ObjKey>&>(set);
104✔
1055
                        // Validate the target.
52✔
1056
                        auto target_table = table->get_link_target(col);
104✔
1057
                        if (target_table->get_key() != link.get_table_key()) {
104✔
1058
                            m_applier->bad_transaction_log(
×
1059
                                "SetInsert: Target table mismatch (expected '%1', got '%2')",
×
1060
                                target_table->get_name(), table_name);
×
1061
                        }
×
1062
                        link_set.insert(link.get_obj_key());
104✔
1063
                    }
104✔
1064
                    else {
×
1065
                        m_applier->bad_transaction_log(
×
1066
                            "SetInsert: Type mismatch in set at '%2.%1' (expected link type, was %3)", field_name,
×
1067
                            table_name, data_type);
×
1068
                    }
×
1069
                },
192✔
1070
                [&](Mixed value) {
1,654✔
1071
                    if (value.is_null() && !col.is_nullable()) {
1,558✔
1072
                        m_applier->bad_transaction_log("SetInsert: NULL in non-nullable set '%2.%1'", field_name,
×
1073
                                                       table_name);
×
1074
                    }
×
1075

780✔
1076
                    if (data_type == type_Mixed || value.is_null() || value.get_type() == data_type) {
1,558✔
1077
                        set.insert_any(value);
1,558✔
1078
                    }
1,558✔
1079
                    else {
×
1080
                        m_applier->bad_transaction_log(
×
1081
                            "SetInsert: Type mismatch in set at '%2.%1' (expected %3, got %4)", field_name,
×
1082
                            table_name, data_type, value.get_type());
×
1083
                    }
×
1084
                },
1,558✔
1085
                [&](const Instruction::Payload::ObjectValue&) {
876✔
1086
                    m_applier->bad_transaction_log("SetInsert: Sets of embedded objects are not supported.");
×
1087
                },
×
1088
                [&](const Instruction::Payload::Dictionary&) {
876✔
1089
                    m_applier->bad_transaction_log("SetInsert: Sets of dictionaries are not supported.");
×
1090
                },
×
1091
                [&](const Instruction::Payload::List&) {
876✔
1092
                    m_applier->bad_transaction_log("SetInsert: Sets of lists are not supported.");
×
1093
                },
×
1094
                [&](const Instruction::Payload::Set&) {
876✔
1095
                    m_applier->bad_transaction_log("SetInsert: Sets of sets are not supported.");
×
1096
                },
×
1097
                [&](const Instruction::Payload::Erased&) {
876✔
1098
                    m_applier->bad_transaction_log("SetInsert: Dictionary erase payload in SetInsert");
×
1099
                },
×
1100
            };
1,750✔
1101

876✔
1102
            m_applier->visit_payload(m_instr.value, inserter);
1,750✔
1103
        }
1,750✔
1104

876✔
1105
    private:
1,750✔
1106
        const Instruction::SetInsert& m_instr;
1,750✔
1107
    };
1,750✔
1108
    SetInsertResolver(this, instr).resolve();
1,750✔
1109
}
1,750✔
1110

1111
void InstructionApplier::operator()(const Instruction::SetErase& instr)
1112
{
476✔
1113
    struct SetEraseResolver : public PathResolver {
476✔
1114
        SetEraseResolver(InstructionApplier* applier, const Instruction::SetErase& instr)
476✔
1115
            : PathResolver(applier, instr, "SetErase")
476✔
1116
            , m_instr(instr)
476✔
1117
        {
476✔
1118
        }
476✔
1119
        void on_property(Obj& obj, ColKey col) override
476✔
1120
        {
240✔
1121
            // This better be a mixed column
1122
            REALM_ASSERT(col.get_type() == col_type_Mixed);
×
1123
            auto set = obj.get_set<Mixed>(col);
×
1124
            on_set(set);
×
1125
        }
×
1126
        void on_set(SetBase& set) override
476✔
1127
        {
476✔
1128
            auto col = set.get_col_key();
476✔
1129
            auto data_type = DataType(col.get_type());
476✔
1130
            auto table = set.get_table();
476✔
1131
            auto table_name = table->get_name();
476✔
1132
            auto field_name = table->get_column_name(col);
476✔
1133

240✔
1134
            auto inserter = util::overload{
476✔
1135
                [&](const ObjLink& link) {
324✔
1136
                    if (data_type == type_TypedLink) {
168✔
1137
                        REALM_ASSERT(dynamic_cast<Set<ObjLink>*>(&set));
×
1138
                        auto& link_set = static_cast<Set<ObjLink>&>(set);
×
1139
                        link_set.erase(link);
×
1140
                    }
×
1141
                    else if (data_type == type_Mixed) {
168✔
1142
                        REALM_ASSERT(dynamic_cast<Set<Mixed>*>(&set));
80✔
1143
                        auto& mixed_set = static_cast<Set<Mixed>&>(set);
80✔
1144
                        mixed_set.erase(link);
80✔
1145
                    }
80✔
1146
                    else if (data_type == type_Link) {
88✔
1147
                        REALM_ASSERT(dynamic_cast<Set<ObjKey>*>(&set));
88✔
1148
                        auto& link_set = static_cast<Set<ObjKey>&>(set);
88✔
1149
                        // Validate the target.
44✔
1150
                        auto target_table = table->get_link_target(col);
88✔
1151
                        if (target_table->get_key() != link.get_table_key()) {
88✔
1152
                            m_applier->bad_transaction_log(
×
1153
                                "SetErase: Target table mismatch (expected '%1', got '%2')", target_table->get_name(),
×
1154
                                table_name);
×
1155
                        }
×
1156
                        link_set.erase(link.get_obj_key());
88✔
1157
                    }
88✔
1158
                    else {
×
1159
                        m_applier->bad_transaction_log(
×
1160
                            "SetErase: Type mismatch in set at '%2.%1' (expected link type, was %3)", field_name,
×
1161
                            table_name, data_type);
×
1162
                    }
×
1163
                },
168✔
1164
                [&](Mixed value) {
392✔
1165
                    if (value.is_null() && !col.is_nullable()) {
308!
1166
                        m_applier->bad_transaction_log("SetErase: NULL in non-nullable set '%2.%1'", field_name,
×
1167
                                                       table_name);
×
1168
                    }
×
1169

156✔
1170
                    if (data_type == type_Mixed || value.get_type() == data_type) {
308✔
1171
                        set.erase_any(value);
308✔
1172
                    }
308✔
1173
                    else {
×
1174
                        m_applier->bad_transaction_log(
×
1175
                            "SetErase: Type mismatch in set at '%2.%1' (expected %3, got %4)", field_name, table_name,
×
1176
                            data_type, value.get_type());
×
1177
                    }
×
1178
                },
308✔
1179
                [&](const Instruction::Payload::ObjectValue&) {
240✔
1180
                    m_applier->bad_transaction_log("SetErase: Sets of embedded objects are not supported.");
×
1181
                },
×
1182
                [&](const Instruction::Payload::List&) {
240✔
1183
                    m_applier->bad_transaction_log("SetErase: Sets of lists are not supported.");
×
1184
                },
×
1185
                [&](const Instruction::Payload::Set&) {
240✔
1186
                    m_applier->bad_transaction_log("SetErase: Sets of sets are not supported.");
×
1187
                },
×
1188
                [&](const Instruction::Payload::Dictionary&) {
240✔
1189
                    m_applier->bad_transaction_log("SetErase: Sets of dictionaries are not supported.");
×
1190
                },
×
1191
                [&](const Instruction::Payload::Erased&) {
240✔
1192
                    m_applier->bad_transaction_log("SetErase: Dictionary erase payload in SetErase");
×
1193
                },
×
1194
            };
476✔
1195

240✔
1196
            m_applier->visit_payload(m_instr.value, inserter);
476✔
1197
        }
476✔
1198

240✔
1199
    private:
476✔
1200
        const Instruction::SetErase& m_instr;
476✔
1201
    };
476✔
1202
    SetEraseResolver(this, instr).resolve();
476✔
1203
}
476✔
1204

1205
StringData InstructionApplier::get_table_name(const Instruction::TableInstruction& instr,
1206
                                              const std::string_view& name)
1207
{
99,446✔
1208
    if (auto class_name = m_log->try_get_string(instr.table)) {
99,446✔
1209
        return Group::class_name_to_table_name(*class_name, m_table_name_buffer);
99,446✔
1210
    }
99,446✔
1211
    else {
×
1212
        bad_transaction_log("Corrupt table name in %1 instruction", name);
×
1213
    }
×
1214
}
99,446✔
1215

1216
TableRef InstructionApplier::get_table(const Instruction::TableInstruction& instr, const std::string_view& name)
1217
{
448,068✔
1218
    if (instr.table == m_last_table_name) {
448,068✔
1219
        return m_last_table;
352,562✔
1220
    }
352,562✔
1221
    else {
95,506✔
1222
        auto table_name = get_table_name(instr, name);
95,506✔
1223
        TableRef table = m_transaction.get_table(table_name);
95,506✔
1224
        if (!table) {
95,506✔
1225
            bad_transaction_log("%1: Table '%2' does not exist", name, table_name);
×
1226
        }
×
1227
        m_last_table = table;
95,506✔
1228
        m_last_table_name = instr.table;
95,506✔
1229
        m_last_object_key.reset();
95,506✔
1230
        m_last_object.reset();
95,506✔
1231
        m_last_field_name = InternString{};
95,506✔
1232
        m_last_field = ColKey{};
95,506✔
1233
        return table;
95,506✔
1234
    }
95,506✔
1235
}
448,068✔
1236

1237
util::Optional<Obj> InstructionApplier::get_top_object(const Instruction::ObjectInstruction& instr,
1238
                                                       const std::string_view& name)
1239
{
662,638✔
1240
    if (m_last_table_name == instr.table && m_last_object_key && m_last_object &&
662,638✔
1241
        *m_last_object_key == instr.object) {
630,788✔
1242
        // We have already found the object, reuse it.
127,068✔
1243
        return *m_last_object;
267,698✔
1244
    }
267,698✔
1245
    else {
394,940✔
1246
        TableRef table = get_table(instr, name);
394,940✔
1247
        ObjKey key = get_object_key(*table, instr.object, name);
394,940✔
1248
        if (!key) {
394,940✔
1249
            return util::none;
×
1250
        }
×
1251
        if (!table->is_valid(key)) {
394,940✔
1252
            // Check if the object is deleted or is a tombstone.
766✔
1253
            return util::none;
1,564✔
1254
        }
1,564✔
1255

197,164✔
1256
        Obj obj = table->get_object(key);
393,376✔
1257
        m_last_object_key = instr.object;
393,376✔
1258
        m_last_object = obj;
393,376✔
1259
        return obj;
393,376✔
1260
    }
393,376✔
1261
}
662,638✔
1262

1263
LstBasePtr InstructionApplier::get_list_from_path(Obj& obj, ColKey col)
1264
{
177,994✔
1265
    // For link columns, `Obj::get_listbase_ptr()` always returns an instance whose concrete type is
89,222✔
1266
    // `LnkLst`, which uses condensed indexes. However, we are interested in using non-condensed
89,222✔
1267
    // indexes, so we need to manually construct a `Lst<ObjKey>` instead for lists of non-embedded
89,222✔
1268
    // links.
89,222✔
1269
    REALM_ASSERT(col.is_list());
177,994✔
1270
    LstBasePtr list;
177,994✔
1271
    if (col.get_type() == col_type_Link) {
177,994✔
1272
        auto table = obj.get_table();
13,944✔
1273
        if (!table->get_link_target(col)->is_embedded()) {
13,944✔
1274
            list = obj.get_list_ptr<ObjKey>(col);
1,760✔
1275
        }
1,760✔
1276
        else {
12,184✔
1277
            list = obj.get_listbase_ptr(col);
12,184✔
1278
        }
12,184✔
1279
    }
13,944✔
1280
    else {
164,050✔
1281
        list = obj.get_listbase_ptr(col);
164,050✔
1282
    }
164,050✔
1283
    return list;
177,994✔
1284
}
177,994✔
1285

1286
InstructionApplier::PathResolver::PathResolver(InstructionApplier* applier, const Instruction::PathInstruction& instr,
1287
                                               const std::string_view& instr_name)
1288
    : m_applier(applier)
1289
    , m_path_instr(instr)
1290
    , m_instr_name(instr_name)
1291
{
658,922✔
1292
}
658,922✔
1293

1294
InstructionApplier::PathResolver::~PathResolver()
1295
{
658,956✔
1296
    on_finish();
658,956✔
1297
}
658,956✔
1298

1299
void InstructionApplier::PathResolver::on_property(Obj&, ColKey)
1300
{
×
1301
    m_applier->bad_transaction_log(util::format("Invalid path for %1 (object, column)", m_instr_name));
×
1302
}
×
1303

1304
void InstructionApplier::PathResolver::on_list(LstBase&)
1305
{
×
1306
    m_applier->bad_transaction_log(util::format("Invalid path for %1 (list)", m_instr_name));
×
1307
}
×
1308

1309
InstructionApplier::PathResolver::Status InstructionApplier::PathResolver::on_list_index(LstBase&, uint32_t)
1310
{
×
1311
    m_applier->bad_transaction_log(util::format("Invalid path for %1 (list, index)", m_instr_name));
×
1312
    return Status::DidNotResolve;
×
1313
}
×
1314

1315
void InstructionApplier::PathResolver::on_dictionary(Dictionary&)
1316
{
×
1317
    m_applier->bad_transaction_log(util::format("Invalid path for %1 (dictionary, key)", m_instr_name));
×
1318
}
×
1319

1320
InstructionApplier::PathResolver::Status InstructionApplier::PathResolver::on_dictionary_key(Dictionary&, Mixed)
1321
{
×
1322
    m_applier->bad_transaction_log(util::format("Invalid path for %1 (dictionary, key)", m_instr_name));
×
1323
    return Status::DidNotResolve;
×
1324
}
×
1325

1326
void InstructionApplier::PathResolver::on_set(SetBase&)
1327
{
×
1328
    m_applier->bad_transaction_log(util::format("Invalid path for %1 (set)", m_instr_name));
×
1329
}
×
1330

1331
void InstructionApplier::PathResolver::on_error(const std::string& err_msg)
1332
{
×
1333
    m_applier->bad_transaction_log(err_msg);
×
1334
}
×
1335

1336
void InstructionApplier::PathResolver::on_column_advance(ColKey col)
1337
{
646,128✔
1338
    m_applier->m_last_field = col;
646,128✔
1339
}
646,128✔
1340

1341
void InstructionApplier::PathResolver::on_dict_key_advance(StringData) {}
2,136✔
1342

1343
InstructionApplier::PathResolver::Status InstructionApplier::PathResolver::on_list_index_advance(uint32_t)
1344
{
7,452✔
1345
    return Status::Pending;
7,452✔
1346
}
7,452✔
1347

1348
InstructionApplier::PathResolver::Status InstructionApplier::PathResolver::on_null_link_advance(StringData,
1349
                                                                                                StringData)
1350
{
×
1351
    return Status::Pending;
×
1352
}
×
1353

1354
InstructionApplier::PathResolver::Status InstructionApplier::PathResolver::on_begin(const util::Optional<Obj>&)
1355
{
633,626✔
1356
    m_applier->m_current_path = m_path_instr.path;
633,626✔
1357
    m_applier->m_last_field_name = m_path_instr.field;
633,626✔
1358
    return Status::Pending;
633,626✔
1359
}
633,626✔
1360

1361
void InstructionApplier::PathResolver::on_finish()
1362
{
658,930✔
1363
    m_applier->m_current_path.reset();
658,930✔
1364
    m_applier->m_last_field_name = InternString{};
658,930✔
1365
    m_applier->m_last_field = ColKey{};
658,930✔
1366
}
658,930✔
1367

1368
StringData InstructionApplier::PathResolver::get_string(InternString interned)
1369
{
683,616✔
1370
    return m_applier->get_string(interned);
683,616✔
1371
}
683,616✔
1372

1373
InstructionApplier::PathResolver::Status InstructionApplier::PathResolver::resolve()
1374
{
658,966✔
1375
    util::Optional<Obj> obj = m_applier->get_top_object(m_path_instr, m_instr_name);
658,966✔
1376
    Status begin_status = on_begin(obj);
658,966✔
1377
    if (begin_status != Status::Pending) {
658,966✔
1378
        return begin_status;
220✔
1379
    }
220✔
1380
    if (!obj) {
658,746✔
1381
        m_applier->bad_transaction_log("%1: No such object: '%2' in class '%3'", m_instr_name,
×
1382
                                       format_pk(m_applier->m_log->get_key(m_path_instr.object)),
×
1383
                                       get_string(m_path_instr.table));
×
1384
    }
×
1385

323,120✔
1386
    m_it_begin = m_path_instr.path.begin();
658,746✔
1387
    m_it_end = m_path_instr.path.end();
658,746✔
1388
    Status status = resolve_field(*obj, m_path_instr.field);
658,746✔
1389
    return status == Status::Pending ? Status::Success : status;
656,442✔
1390
}
658,746✔
1391

1392
InstructionApplier::PathResolver::Status InstructionApplier::PathResolver::resolve_field(Obj& obj, InternString field)
1393
{
674,836✔
1394
    auto field_name = get_string(field);
674,836✔
1395
    ColKey col = obj.get_table()->get_column_key(field_name);
674,836✔
1396
    if (!col) {
674,836✔
1397
        on_error(util::format("%1: No such field: '%2' in class '%3'", m_instr_name, field_name,
×
1398
                              obj.get_table()->get_name()));
×
1399
        return Status::DidNotResolve;
×
1400
    }
×
1401
    on_column_advance(col);
674,836✔
1402

331,218✔
1403
    if (m_it_begin == m_it_end) {
674,836✔
1404
        if (col.is_list()) {
485,262✔
1405
            auto list = obj.get_listbase_ptr(col);
124✔
1406
            on_list(*list);
124✔
1407
        }
124✔
1408
        else if (col.is_dictionary()) {
485,138✔
1409
            auto dict = obj.get_dictionary(col);
80✔
1410
            on_dictionary(dict);
80✔
1411
        }
80✔
1412
        else if (col.is_set()) {
485,058✔
1413
            SetBasePtr set;
3,638✔
1414
            if (col.get_type() == col_type_Link) {
3,638✔
1415
                // We are interested in using non-condensed indexes - as for Lists below
188✔
1416
                set = obj.get_set_ptr<ObjKey>(col);
376✔
1417
            }
376✔
1418
            else {
3,262✔
1419
                set = obj.get_setbase_ptr(col);
3,262✔
1420
            }
3,262✔
1421
            on_set(*set);
3,638✔
1422
        }
3,638✔
1423
        else {
481,420✔
1424
            on_property(obj, col);
481,420✔
1425
        }
481,420✔
1426
        return Status::Pending;
485,262✔
1427
    }
485,262✔
1428

95,038✔
1429
    if (col.is_list()) {
189,574✔
1430
        if (auto pindex = mpark::get_if<uint32_t>(&*m_it_begin)) {
176,178✔
1431
            auto list = InstructionApplier::get_list_from_path(obj, col);
176,178✔
1432
            ++m_it_begin;
176,178✔
1433
            return resolve_list_element(*list, *pindex);
176,178✔
1434
        }
176,178✔
1435
        on_error(util::format("%1: List index is not an integer on field '%2' in class '%3'", m_instr_name,
×
1436
                              field_name, obj.get_table()->get_name()));
×
1437
    }
×
1438
    else if (col.is_dictionary()) {
13,396✔
1439
        if (auto pkey = mpark::get_if<InternString>(&*m_it_begin)) {
8,768✔
1440
            auto dict = obj.get_dictionary(col);
8,768✔
1441
            ++m_it_begin;
8,768✔
1442
            return resolve_dictionary_element(dict, *pkey);
8,768✔
1443
        }
8,768✔
1444
        on_error(util::format("%1: Dictionary key is not a string on field '%2' in class '%3'", m_instr_name,
×
1445
                              field_name, obj.get_table()->get_name()));
×
1446
    }
×
1447
    else if (col.get_type() == col_type_Mixed) {
4,628✔
1448
        auto val = obj.get<Mixed>(col);
×
1449
        if (val.is_type(type_Dictionary)) {
×
1450
            if (auto pkey = mpark::get_if<InternString>(&*m_it_begin)) {
×
1451
                Dictionary dict(obj, col);
×
1452
                ++m_it_begin;
×
1453
                return resolve_dictionary_element(dict, *pkey);
×
1454
            }
×
1455
        }
×
1456
        if (val.is_type(type_List)) {
×
1457
            if (auto pindex = mpark::get_if<uint32_t>(&*m_it_begin)) {
×
1458
                Lst<Mixed> list(obj, col);
×
1459
                ++m_it_begin;
×
1460
                return resolve_list_element(list, *pindex);
×
1461
            }
×
1462
        }
×
1463
        on_error(util::format("%1: Not a list or dictionary on field '%2' in class '%3'", m_instr_name, field_name,
×
1464
                              obj.get_table()->get_name()));
×
1465
    }
×
1466
    else if (col.get_type() == col_type_Link) {
4,644✔
1467
        auto target = obj.get_table()->get_link_target(col);
4,608✔
1468
        if (!target->is_embedded()) {
4,608✔
1469
            on_error(util::format("%1: Reference through non-embedded link in field '%2' in class '%3'", m_instr_name,
×
1470
                                  field_name, obj.get_table()->get_name()));
×
1471
        }
×
1472
        else if (obj.is_null(col)) {
4,608✔
1473
            Status null_status =
132✔
1474
                on_null_link_advance(obj.get_table()->get_name(), obj.get_table()->get_column_name(col));
132✔
1475
            if (null_status != Status::Pending) {
132✔
1476
                return null_status;
132✔
1477
            }
132✔
1478
            on_error(util::format("%1: Reference through NULL embedded link in field '%2' in class '%3'",
×
1479
                                  m_instr_name, field_name, obj.get_table()->get_name()));
×
1480
        }
×
1481
        else if (auto pfield = mpark::get_if<InternString>(&*m_it_begin)) {
4,476✔
1482
            auto embedded_object = obj.get_linked_object(col);
4,476✔
1483
            ++m_it_begin;
4,476✔
1484
            return resolve_field(embedded_object, *pfield);
4,476✔
1485
        }
4,476✔
1486
        else {
×
1487
            on_error(util::format("%1: Embedded object field reference is not a string", m_instr_name));
×
1488
        }
×
1489
    }
4,608✔
1490
    else {
2,147,483,683✔
1491
        on_error(util::format("%1: Resolving path through unstructured field '%3.%2' of type %4", m_instr_name,
2,147,483,683✔
1492
                              field_name, obj.get_table()->get_name(), col.get_type()));
2,147,483,683✔
1493
    }
2,147,483,683✔
1494
    return Status::DidNotResolve;
2,147,578,685✔
1495
}
189,574✔
1496

1497
InstructionApplier::PathResolver::Status InstructionApplier::PathResolver::resolve_list_element(LstBase& list,
1498
                                                                                                uint32_t index)
1499
{
176,178✔
1500
    if (m_it_begin == m_it_end) {
176,178✔
1501
        return on_list_index(list, index);
166,006✔
1502
    }
166,006✔
1503

5,086✔
1504
    auto col = list.get_col_key();
10,172✔
1505
    auto field_name = list.get_table()->get_column_name(col);
10,172✔
1506

5,086✔
1507
    if (col.get_type() == col_type_Link) {
10,172✔
1508
        auto target = list.get_table()->get_link_target(col);
10,172✔
1509
        if (!target->is_embedded()) {
10,172✔
1510
            on_error(util::format("%1: Reference through non-embedded link at '%3.%2[%4]'", m_instr_name, field_name,
×
1511
                                  list.get_table()->get_name(), index));
×
1512
            return Status::DidNotResolve;
×
1513
        }
×
1514

5,086✔
1515
        Status list_status = on_list_index_advance(index);
10,172✔
1516
        if (list_status != Status::Pending) {
10,172✔
1517
            return list_status;
2,720✔
1518
        }
2,720✔
1519

3,726✔
1520
        REALM_ASSERT(dynamic_cast<LnkLst*>(&list));
7,452✔
1521
        auto& link_list = static_cast<LnkLst&>(list);
7,452✔
1522
        if (index >= link_list.size()) {
7,452✔
1523
            on_error(util::format("%1: Out-of-bounds index through list at '%3.%2[%4]'", m_instr_name, field_name,
×
1524
                                  list.get_table()->get_name(), index));
×
1525
        }
×
1526
        else if (auto pfield = mpark::get_if<InternString>(&*m_it_begin)) {
7,452✔
1527
            auto embedded_object = link_list.get_object(index);
7,452✔
1528
            ++m_it_begin;
7,452✔
1529
            return resolve_field(embedded_object, *pfield);
7,452✔
1530
        }
7,452✔
1531
        on_error(util::format("%1: Embedded object field reference is not a string", m_instr_name));
×
1532
    }
×
1533
    else {
×
1534
        if (list.get_data_type() == type_Mixed) {
×
1535
            auto& mixed_list = static_cast<Lst<Mixed>&>(list);
×
1536
            if (index < mixed_list.size()) {
×
1537
                auto val = mixed_list.get(index);
×
1538

1539
                if (val.is_type(type_Dictionary)) {
×
1540
                    if (auto pfield = mpark::get_if<InternString>(&*m_it_begin)) {
×
1541
                        Dictionary d(mixed_list, mixed_list.get_key(index));
×
1542
                        ++m_it_begin;
×
1543
                        return resolve_dictionary_element(d, *pfield);
×
1544
                    }
×
1545
                }
×
1546
                if (val.is_type(type_List)) {
×
1547
                    if (auto pindex = mpark::get_if<uint32_t>(&*m_it_begin)) {
×
1548
                        Lst<Mixed> l(mixed_list, mixed_list.get_key(index));
×
1549
                        ++m_it_begin;
×
1550
                        return resolve_list_element(l, *pindex);
×
1551
                    }
×
1552
                }
×
1553
            }
×
1554
        }
×
1555

1556
        on_error(util::format(
×
1557
            "%1: Resolving path through unstructured list element on '%3.%2', which is a list of type '%4'",
×
1558
            m_instr_name, field_name, list.get_table()->get_name(), col.get_type()));
×
1559
    }
×
1560
    return Status::DidNotResolve;
5,086✔
1561
}
10,172✔
1562

1563
InstructionApplier::PathResolver::Status
1564
InstructionApplier::PathResolver::resolve_dictionary_element(Dictionary& dict, InternString key)
1565
{
8,768✔
1566
    StringData string_key = get_string(key);
8,768✔
1567
    if (m_it_begin == m_it_end) {
8,768✔
1568
        return on_dictionary_key(dict, Mixed{string_key});
4,604✔
1569
    }
4,604✔
1570

2,082✔
1571
    on_dict_key_advance(string_key);
4,164✔
1572

2,082✔
1573
    auto col = dict.get_col_key();
4,164✔
1574
    auto table = dict.get_table();
4,164✔
1575
    auto field_name = table->get_column_name(col);
4,164✔
1576

2,082✔
1577
    if (col.get_type() == col_type_Link) {
4,164✔
1578
        auto target = dict.get_target_table();
4,164✔
1579
        if (!target->is_embedded()) {
4,164✔
1580
            on_error(util::format("%1: Reference through non-embedded link at '%3.%2[%4]'", m_instr_name, field_name,
×
1581
                                  table->get_name(), string_key));
×
1582
            return Status::DidNotResolve;
×
1583
        }
×
1584

2,082✔
1585
        auto embedded_object = dict.get_object(string_key);
4,164✔
1586
        if (!embedded_object) {
4,164✔
1587
            Status null_link_status = on_null_link_advance(table->get_name(), string_key);
80✔
1588
            if (null_link_status != Status::Pending) {
80✔
1589
                return null_link_status;
80✔
1590
            }
80✔
1591
            on_error(util::format("%1: Unmatched key through dictionary at '%3.%2[%4]'", m_instr_name, field_name,
×
1592
                                  table->get_name(), string_key));
×
1593
        }
×
1594
        else if (auto pfield = mpark::get_if<InternString>(&*m_it_begin)) {
4,084✔
1595
            ++m_it_begin;
4,084✔
1596
            return resolve_field(embedded_object, *pfield);
4,084✔
1597
        }
4,084✔
1598
        else {
×
1599
            on_error(util::format("%1: Embedded object field reference is not a string", m_instr_name));
×
1600
        }
×
1601
    }
4,164✔
1602
    else {
×
1603
        auto val = dict.get(string_key);
×
1604
        if (val.is_type(type_Dictionary)) {
×
1605
            if (auto pfield = mpark::get_if<InternString>(&*m_it_begin)) {
×
1606
                Dictionary d(dict, dict.build_index(string_key));
×
1607
                ++m_it_begin;
×
1608
                return resolve_dictionary_element(d, *pfield);
×
1609
            }
×
1610
        }
×
1611
        if (val.is_type(type_List)) {
×
1612
            if (auto pindex = mpark::get_if<uint32_t>(&*m_it_begin)) {
×
1613
                Lst<Mixed> l(dict, dict.build_index(string_key));
×
1614
                ++m_it_begin;
×
1615
                return resolve_list_element(l, *pindex);
×
1616
            }
×
1617
        }
×
1618
        on_error(
×
1619
            util::format("%1: Resolving path through non link element on '%3.%2', which is a dictionary of type '%4'",
×
1620
                         m_instr_name, field_name, table->get_name(), col.get_type()));
×
1621
    }
×
1622
    return Status::DidNotResolve;
2,082✔
1623
}
4,164✔
1624

1625

1626
ObjKey InstructionApplier::get_object_key(Table& table, const Instruction::PrimaryKey& primary_key,
1627
                                          const std::string_view& name) const
1628
{
397,178✔
1629
    StringData table_name = table.get_name();
397,178✔
1630
    ColKey pk_col = table.get_primary_key_column();
397,178✔
1631
    StringData pk_name = "";
397,178✔
1632
    DataType pk_type;
397,178✔
1633
    if (pk_col) {
397,178✔
1634
        pk_name = table.get_column_name(pk_col);
397,158✔
1635
        pk_type = table.get_column_type(pk_col);
397,158✔
1636
    }
397,158✔
1637
    return mpark::visit(
397,178✔
1638
        util::overload{
397,178✔
1639
            [&](mpark::monostate) {
199,152✔
1640
                if (!pk_col) {
24✔
1641
                    bad_transaction_log(
×
1642
                        "%1 instruction with NULL primary key, but table '%2' does not have a primary key column",
×
1643
                        name, table_name);
×
1644
                }
×
1645
                if (!table.is_nullable(pk_col)) {
24✔
1646
                    bad_transaction_log("%1 instruction with NULL primary key, but column '%2.%3' is not nullable",
×
1647
                                        name, table_name, pk_name);
×
1648
                }
×
1649

12✔
1650
                ObjKey key = table.get_objkey_from_primary_key(realm::util::none);
24✔
1651
                return key;
24✔
1652
            },
24✔
1653
            [&](int64_t pk) {
394,172✔
1654
                if (!pk_col) {
390,960✔
1655
                    bad_transaction_log("%1 instruction with integer primary key (%2), but table '%3' does not have "
×
1656
                                        "a primary key column",
×
1657
                                        name, pk, table_name);
×
1658
                }
×
1659
                if (pk_type != type_Int) {
390,960✔
1660
                    bad_transaction_log(
×
1661
                        "%1 instruction with integer primary key (%2), but '%3.%4' has primary keys of type '%5'",
×
1662
                        name, pk, table_name, pk_name, pk_type);
×
1663
                }
×
1664
                ObjKey key = table.get_objkey_from_primary_key(pk);
390,960✔
1665
                return key;
390,960✔
1666
            },
390,960✔
1667
            [&](InternString interned_pk) {
199,498✔
1668
                auto pk = get_string(interned_pk);
870✔
1669
                if (!pk_col) {
870✔
1670
                    bad_transaction_log("%1 instruction with string primary key (\"%2\"), but table '%3' does not "
×
1671
                                        "have a primary key column",
×
1672
                                        name, pk, table_name);
×
1673
                }
×
1674
                if (pk_type != type_String) {
870✔
1675
                    bad_transaction_log(
×
1676
                        "%1 instruction with string primary key (\"%2\"), but '%3.%4' has primary keys of type '%5'",
×
1677
                        name, pk, table_name, pk_name, pk_type);
×
1678
                }
×
1679
                ObjKey key = table.get_objkey_from_primary_key(pk);
870✔
1680
                return key;
870✔
1681
            },
870✔
1682
            [&](GlobalKey id) {
199,142✔
1683
                if (pk_col) {
4✔
1684
                    bad_transaction_log(
×
1685
                        "%1 instruction without primary key, but table '%2' has a primary key column of type %3",
×
1686
                        name, table_name, pk_type);
×
1687
                }
×
1688
                ObjKey key = table.get_objkey_from_global_key(id);
4✔
1689
                return key;
4✔
1690
            },
4✔
1691
            [&](ObjectId pk) {
201,754✔
1692
                if (!pk_col) {
5,222✔
1693
                    bad_transaction_log("%1 instruction with ObjectId primary key (\"%2\"), but table '%3' does not "
×
1694
                                        "have a primary key column",
×
1695
                                        name, pk, table_name);
×
1696
                }
×
1697
                if (pk_type != type_ObjectId) {
5,222✔
1698
                    bad_transaction_log(
×
1699
                        "%1 instruction with ObjectId primary key (%2), but '%3.%4' has primary keys of type '%5'",
×
1700
                        name, pk, table_name, pk_name, pk_type);
×
1701
                }
×
1702
                ObjKey key = table.get_objkey_from_primary_key(pk);
5,222✔
1703
                return key;
5,222✔
1704
            },
5,222✔
1705
            [&](UUID pk) {
199,150✔
1706
                if (!pk_col) {
20✔
1707
                    bad_transaction_log("%1 instruction with UUID primary key (\"%2\"), but table '%3' does not "
×
1708
                                        "have a primary key column",
×
1709
                                        name, pk, table_name);
×
1710
                }
×
1711
                if (pk_type != type_UUID) {
20✔
1712
                    bad_transaction_log(
×
1713
                        "%1 instruction with UUID primary key (%2), but '%3.%4' has primary keys of type '%5'", name,
×
1714
                        pk, table_name, pk_name, pk_type);
×
1715
                }
×
1716
                ObjKey key = table.get_objkey_from_primary_key(pk);
20✔
1717
                return key;
20✔
1718
            }},
20✔
1719
        primary_key);
397,178✔
1720
}
397,178✔
1721

1722

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

© 2026 Coveralls, Inc