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

realm / realm-core / github_pull_request_281922

31 Oct 2023 09:13AM UTC coverage: 90.445% (-0.08%) from 90.528%
github_pull_request_281922

Pull #7039

Evergreen

jedelbo
Merge branch 'next-major' into je/global-key
Pull Request #7039: Remove ability to synchronize objects without primary key

95324 of 175822 branches covered (0.0%)

101 of 105 new or added lines in 13 files covered. (96.19%)

238 existing lines in 19 files now uncovered.

232657 of 257235 relevant lines covered (90.45%)

6351359.67 hits per line

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

64.05
/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
{
4✔
12
    throw BadChangesetError{std::move(msg)};
4✔
13
}
4✔
14

15
} // namespace
16

17
REALM_NORETURN void InstructionApplier::bad_transaction_log(const std::string& msg) const
18
{
4✔
19
    if (m_last_object_key) {
4✔
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) {
4✔
37
        // We should have a changeset if we have a table name defined.
2✔
38
        REALM_ASSERT(m_log);
4✔
39
        throw_bad_transaction_log(
4✔
40
            util::format("%1 (instruction table: %2, version: %3, last_integrated_remote_version: %4, "
4✔
41
                         "origin_file_ident: %5, timestamp: %6)",
4✔
42
                         msg, m_log->get_string(m_last_table_name), m_log->version,
4✔
43
                         m_log->last_integrated_remote_version, m_log->origin_file_ident, m_log->origin_timestamp));
4✔
44
    }
4✔
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));
4✔
53
}
4✔
54

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

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

72
StringData InstructionApplier::get_string(StringBufferRange range) const
73
{
172,024✔
74
    auto string = m_log->try_get_string(range);
172,024✔
75
    if (!string)
172,024✔
76
        bad_transaction_log("string read error");
×
77
    return *string;
172,024✔
78
}
172,024✔
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_table_name_length - 6)
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
    {
38,956✔
102
        std::swap(target, backup);
38,956✔
103
    }
38,956✔
104

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

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

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

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

6,436✔
121
    auto add_table = util::overload{
12,928✔
122
        [&](const Instruction::AddTable::TopLevelTable& spec) {
12,818✔
123
            auto table_type = (spec.is_asymmetric ? Table::Type::TopLevelAsymmetric : Table::Type::TopLevel);
12,696✔
124
            if (spec.pk_type == Instruction::Payload::Type::GlobalKey) {
12,708✔
UNCOV
125
                m_transaction.get_or_add_table(table_name, table_type);
×
UNCOV
126
            }
×
127
            else {
12,708✔
128
                if (!is_valid_key_type(spec.pk_type)) {
12,708✔
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);
12,708✔
133
                StringData pk_field = get_string(spec.pk_field);
12,708✔
134
                bool nullable = spec.pk_nullable;
12,708✔
135

6,326✔
136
                if (!m_transaction.get_or_add_table_with_primary_key(table_name, pk_type, pk_field, nullable,
12,708✔
137
                                                                     table_type)) {
6,326✔
138
                    bad_transaction_log("AddTable: The existing table '%1' has different properties", table_name);
×
139
                }
×
140
            }
12,708✔
141
        },
12,708✔
142
        [&](const Instruction::AddTable::EmbeddedTable&) {
6,546✔
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
    };
12,928✔
153

6,436✔
154
    mpark::visit(std::move(add_table), instr.type);
12,928✔
155
}
12,928✔
156

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

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

2,010✔
168
    m_transaction.remove_table(table_name);
3,848✔
169
}
3,848✔
170

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

67,300✔
177
    mpark::visit(
136,252✔
178
        util::overload{
136,252✔
179
            [&](mpark::monostate) {
67,316✔
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) {
123,072✔
189
                if (!pk_col) {
111,212✔
190
                    bad_transaction_log("CreateObject(Int) on table without a primary key");
×
191
                }
×
192
                if (table->get_column_type(pk_col) != type_Int) {
111,212✔
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);
111,212✔
197
            },
111,212✔
198
            [&](InternString pk) {
76,544✔
199
                if (!pk_col) {
17,952✔
200
                    bad_transaction_log("CreateObject(String) on table without a primary key");
×
201
                }
×
202
                if (table->get_column_type(pk_col) != type_String) {
17,952✔
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);
17,952✔
207
                m_last_object = table->create_object_with_primary_key(str);
17,952✔
208
            },
17,952✔
209
            [&](const ObjectId& id) {
71,196✔
210
                if (!pk_col) {
7,010✔
211
                    bad_transaction_log("CreateObject(ObjectId) on table without a primary key");
×
212
                }
×
213
                if (table->get_column_type(pk_col) != type_ObjectId) {
7,010✔
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,010✔
218
            },
7,010✔
219
            [&](const UUID& id) {
67,324✔
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) {
67,300✔
NEW
230
                bad_transaction_log("CreateObject(GlobalKey) not supported");
×
UNCOV
231
            },
×
232
        },
136,252✔
233
        instr.object);
136,252✔
234
}
136,252✔
235

236
void InstructionApplier::operator()(const Instruction::EraseObject& instr)
237
{
57,398✔
238
    // FIXME: Log actions.
28,356✔
239
    // Note: EraseObject is idempotent.
28,356✔
240
    if (auto obj = get_top_object(instr, "EraseObject")) {
57,398✔
241
        // This call will prevent incoming links to be nullified/deleted
20,830✔
242
        obj->invalidate();
42,588✔
243
    }
42,588✔
244
    m_last_object.reset();
57,398✔
245
}
57,398✔
246

247
template <class F>
248
void InstructionApplier::visit_payload(const Instruction::Payload& payload, F&& visitor)
249
{
636,814✔
250
    using Type = Instruction::Payload::Type;
636,814✔
251

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

310
void InstructionApplier::operator()(const Instruction::Update& instr)
311
{
474,606✔
312
    struct UpdateResolver : public PathResolver {
474,606✔
313
        UpdateResolver(InstructionApplier* applier, const Instruction::Update& instr)
474,606✔
314
            : PathResolver(applier, instr, "Update")
474,606✔
315
            , m_instr(instr)
474,606✔
316
        {
476,114✔
317
        }
476,114✔
318
        void on_property(Obj& obj, ColKey col) override
474,606✔
319
        {
471,148✔
320
            // Update of object field.
226,270✔
321

226,270✔
322
            auto table = obj.get_table();
467,756✔
323
            auto table_name = table->get_name();
467,756✔
324
            auto field_name = table->get_column_name(col);
467,756✔
325
            auto data_type = DataType(col.get_type());
467,756✔
326

226,270✔
327
            auto visitor = [&](const mpark::variant<ObjLink, Mixed, Instruction::Payload::ObjectValue,
467,756✔
328
                                                    Instruction::Payload::Dictionary, Instruction::Payload::List,
467,756✔
329
                                                    Instruction::Payload::Set, Instruction::Payload::Erased>& arg) {
467,772✔
330
                if (const auto link_ptr = mpark::get_if<ObjLink>(&arg)) {
467,084✔
331
                    if (data_type == type_Mixed || data_type == type_TypedLink) {
80✔
332
                        obj.set_any(col, *link_ptr, m_instr.is_default);
×
333
                    }
×
334
                    else if (data_type == type_Link) {
80✔
335
                        // Validate target table.
40✔
336
                        auto target_table = table->get_link_target(col);
80✔
337
                        if (target_table->get_key() != link_ptr->get_table_key()) {
80✔
338
                            m_applier->bad_transaction_log(
×
339
                                "Update: Target table mismatch (expected %1, got %2)", target_table->get_name(),
×
340
                                m_applier->m_transaction.get_table(link_ptr->get_table_key())->get_name());
×
341
                        }
×
342
                        obj.set<ObjKey>(col, link_ptr->get_obj_key(), m_instr.is_default);
80✔
343
                    }
80✔
344
                    else {
×
345
                        m_applier->bad_transaction_log("Update: Type mismatch in '%2.%1' (expected %3, got %4)",
×
346
                                                       field_name, table_name, col.get_type(), type_Link);
×
347
                    }
×
348
                }
80✔
349
                else if (const auto mixed_ptr = mpark::get_if<Mixed>(&arg)) {
469,284✔
350
                    if (mixed_ptr->is_null()) {
468,988✔
351
                        if (col.is_nullable()) {
788✔
352
                            obj.set_null(col, m_instr.is_default);
788✔
353
                        }
788✔
354
                        else {
×
355
                            m_applier->bad_transaction_log("Update: NULL in non-nullable field '%2.%1'", field_name,
×
356
                                                           table_name);
×
357
                        }
×
358
                    }
788✔
359
                    else if (data_type == type_Mixed || mixed_ptr->get_type() == data_type) {
468,200✔
360
                        obj.set_any(col, *mixed_ptr, m_instr.is_default);
465,722✔
361
                    }
465,722✔
362
                    else {
2,478✔
363
                        m_applier->bad_transaction_log("Update: Type mismatch in '%2.%1' (expected %3, got %4)",
2,478✔
364
                                                       field_name, table_name, col.get_type(), mixed_ptr->get_type());
2,478✔
365
                    }
2,478✔
366
                }
468,988✔
367
                else if (const auto obj_val_ptr = mpark::get_if<Instruction::Payload::ObjectValue>(&arg)) {
2,147,483,943✔
368
                    if (obj.is_null(col)) {
584✔
369
                        obj.create_and_set_linked_object(col);
556✔
370
                    }
556✔
371
                }
584✔
372
                else if (const auto erase_ptr = mpark::get_if<Instruction::Payload::Erased>(&arg)) {
2,147,483,651✔
373
                    m_applier->bad_transaction_log("Update: Dictionary erase at object field");
×
374
                }
×
375
                else if (mpark::get_if<Instruction::Payload::Dictionary>(&arg)) {
2,147,483,651✔
376
                    obj.set_collection(col, CollectionType::Dictionary);
×
377
                }
×
378
                else if (mpark::get_if<Instruction::Payload::List>(&arg)) {
2,147,483,651✔
379
                    obj.set_collection(col, CollectionType::List);
×
380
                }
×
381
                else if (mpark::get_if<Instruction::Payload::Set>(&arg)) {
2,147,483,651✔
382
                    obj.set_collection(col, CollectionType::Set);
×
383
                }
×
384
            };
467,084✔
385

226,270✔
386
            m_applier->visit_payload(m_instr.value, visitor);
467,756✔
387
        }
467,756✔
388
        Status on_list_index(LstBase& list, uint32_t index) override
474,606✔
389
        {
231,792✔
390
            // Update of list element.
2,648✔
391

2,648✔
392
            auto col = list.get_col_key();
4,778✔
393
            auto data_type = DataType(col.get_type());
4,778✔
394
            auto table = list.get_table();
4,778✔
395
            auto table_name = table->get_name();
4,778✔
396
            auto field_name = table->get_column_name(col);
4,778✔
397

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

2,648✔
467
            m_applier->visit_payload(m_instr.value, visitor);
4,778✔
468
            return Status::Pending;
4,778✔
469
        }
4,778✔
470
        Status on_dictionary_key(Dictionary& dict, Mixed key) override
474,606✔
471
        {
230,964✔
472
            // Update (insert) of dictionary element.
1,302✔
473

1,302✔
474
            auto visitor = util::overload{
2,604✔
475
                [&](Mixed value) {
2,326✔
476
                    if (value.is_null()) {
2,048✔
477
                        // FIXME: Separate handling of NULL is needed because
36✔
478
                        // `Mixed::get_type()` asserts on NULL.
36✔
479
                        dict.insert(key, value);
72✔
480
                    }
72✔
481
                    else if (value.get_type() == type_Link) {
1,976✔
482
                        m_applier->bad_transaction_log("Update: Untyped links are not supported in dictionaries.");
×
483
                    }
×
484
                    else {
1,976✔
485
                        dict.insert(key, value);
1,976✔
486
                    }
1,976✔
487
                },
2,048✔
488
                [&](const Instruction::Payload::Erased&) {
1,400✔
489
                    dict.erase(key);
196✔
490
                },
196✔
491
                [&](const Instruction::Payload::ObjectValue&) {
1,482✔
492
                    dict.create_and_insert_linked_object(key);
360✔
493
                },
360✔
494
                [&](const Instruction::Payload::Dictionary&) {
1,302✔
495
                    dict.insert_collection(key.get_string(), CollectionType::Dictionary);
×
496
                },
×
497
                [&](const Instruction::Payload::List&) {
1,302✔
498
                    dict.insert_collection(key.get_string(), CollectionType::List);
×
499
                },
×
500
                [&](const Instruction::Payload::Set&) {
1,302✔
501
                    dict.insert_collection(key.get_string(), CollectionType::Set);
×
502
                },
×
503
            };
2,604✔
504

1,302✔
505
            m_applier->visit_payload(m_instr.value, visitor);
2,604✔
506
            return Status::Pending;
2,604✔
507
        }
2,604✔
508

229,662✔
509
    private:
474,606✔
510
        const Instruction::Update& m_instr;
474,606✔
511
    };
474,606✔
512
    UpdateResolver resolver(this, instr);
474,606✔
513
    resolver.resolve();
474,606✔
514
}
474,606✔
515

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

1,694✔
540
    private:
3,606✔
541
        const Instruction::AddInteger& m_instr;
3,606✔
542
    };
3,606✔
543
    AddIntegerResolver resolver(this, instr);
3,606✔
544
    resolver.resolve();
3,606✔
545
}
3,606✔
546

547
void InstructionApplier::operator()(const Instruction::AddColumn& instr)
548
{
22,180✔
549
    using Type = Instruction::Payload::Type;
22,180✔
550
    using CollectionType = Instruction::AddColumn::CollectionType;
22,180✔
551

10,600✔
552
    // Temporarily swap out the last object key so it doesn't get included in error messages
10,600✔
553
    TemporarySwapOut<decltype(m_last_object_key)> last_object_key_guard(m_last_object_key);
22,180✔
554

10,600✔
555
    auto table = get_table(instr, "AddColumn");
22,180✔
556
    auto col_name = get_string(instr.field);
22,180✔
557

10,600✔
558
    if (ColKey existing_key = table->get_column_key(col_name)) {
22,180✔
559
        DataType new_type = get_data_type(instr.type);
304✔
560
        ColumnType existing_type = existing_key.get_type();
304✔
561
        if (existing_type == col_type_LinkList) {
304✔
562
            existing_type = col_type_Link;
×
563
        }
×
564
        if (existing_type != ColumnType(new_type)) {
304✔
565
            bad_transaction_log("AddColumn: Schema mismatch for existing column in '%1.%2' (expected %3, got %4)",
4✔
566
                                table->get_name(), col_name, existing_type, new_type);
4✔
567
        }
4✔
568
        bool existing_is_list = existing_key.is_list();
304✔
569
        if ((instr.collection_type == CollectionType::List) != existing_is_list) {
304✔
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();
304✔
575
        if ((instr.collection_type == CollectionType::Set) != existing_is_set) {
304✔
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();
304✔
581
        if ((instr.collection_type == CollectionType::Dictionary) != existing_is_dict) {
304✔
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) {
304✔
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;
304✔
596
    }
304✔
597

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

10,448✔
603
    if (instr.type != Type::Link) {
21,876✔
604
        DataType type = get_data_type(instr.type);
20,752✔
605
        switch (instr.collection_type) {
20,752✔
606
            case CollectionType::Single: {
14,730✔
607
                table->add_column(type, col_name, instr.nullable);
14,730✔
608
                break;
14,730✔
609
            }
×
610
            case CollectionType::List: {
5,790✔
611
                table->add_column_list(type, col_name, instr.nullable);
5,790✔
612
                break;
5,790✔
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,124✔
623
        }
1,124✔
624
    }
1,124✔
625
    else {
1,124✔
626
        Group::TableNameBuffer buffer;
1,124✔
627
        auto target_table_name = get_string(instr.link_target_table);
1,124✔
628
        if (target_table_name.size() != 0) {
1,124✔
629
            TableRef target = m_transaction.get_table(Group::class_name_to_table_name(target_table_name, buffer));
1,124✔
630
            if (!target) {
1,124✔
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,124✔
635
                table->add_column_list(*target, col_name);
580✔
636
            }
580✔
637
            else if (instr.collection_type == CollectionType::Set) {
544✔
638
                table->add_column_set(*target, col_name);
8✔
639
            }
8✔
640
            else if (instr.collection_type == CollectionType::Dictionary) {
536✔
641
                table->add_column_dictionary(*target, col_name);
32✔
642
            }
32✔
643
            else {
504✔
644
                REALM_ASSERT(instr.collection_type == CollectionType::Single);
504✔
645
                table->add_column(*target, col_name);
504✔
646
            }
504✔
647
        }
1,124✔
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,124✔
658
}
21,876✔
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
{
163,076✔
678
    struct ArrayInsertResolver : public PathResolver {
163,076✔
679
        ArrayInsertResolver(InstructionApplier* applier, const Instruction::ArrayInsert& instr)
163,076✔
680
            : PathResolver(applier, instr, "ArrayInsert")
163,076✔
681
            , m_instr(instr)
163,076✔
682
        {
163,076✔
683
        }
163,076✔
684
        Status on_list_index(LstBase& list, uint32_t index) override
163,076✔
685
        {
163,076✔
686
            auto data_type = list.get_data_type();
163,076✔
687
            auto table = list.get_table();
163,076✔
688
            auto table_name = table->get_name();
163,076✔
689
            auto field_name = [&] {
81,362✔
690
                return table->get_column_name(list.get_col_key());
×
691
            };
×
692

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

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

81,362✔
702
            if (m_instr.prior_size != list.size()) {
163,076✔
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,362✔
707
            auto inserter = util::overload{
163,076✔
708
                [&](const ObjLink& link) {
81,942✔
709
                    if (data_type == type_TypedLink) {
1,160✔
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,160✔
715
                        REALM_ASSERT(dynamic_cast<Lst<Mixed>*>(&list));
132✔
716
                        auto& mixed_list = static_cast<Lst<Mixed>&>(list);
132✔
717
                        mixed_list.insert(index, link);
132✔
718
                    }
132✔
719
                    else if (data_type == type_Link) {
1,028✔
720
                        REALM_ASSERT(dynamic_cast<Lst<ObjKey>*>(&list));
1,028✔
721
                        auto& link_list = static_cast<Lst<ObjKey>&>(list);
1,028✔
722
                        // Validate the target.
514✔
723
                        auto target_table = table->get_link_target(list.get_col_key());
1,028✔
724
                        if (target_table->get_key() != link.get_table_key()) {
1,028✔
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,028✔
731
                    }
1,028✔
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,160✔
738
                [&](Mixed value) {
161,666✔
739
                    if (data_type == type_Mixed) {
160,256✔
740
                        list.insert_any(index, value);
28✔
741
                    }
28✔
742
                    else if (value.is_null()) {
160,228✔
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 {
160,180✔
752
                        if (value.get_type() == data_type) {
160,180✔
753
                            list.insert_any(index, value);
160,180✔
754
                        }
160,180✔
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
                    }
160,180✔
761
                },
160,256✔
762
                [&](const Instruction::Payload::ObjectValue&) {
82,192✔
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,362✔
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,362✔
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,362✔
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,362✔
798
                    m_applier->bad_transaction_log("Dictionary erase payload for ArrayInsert");
×
799
                },
×
800
            };
163,076✔
801

81,362✔
802
            m_applier->visit_payload(m_instr.value, inserter);
163,076✔
803
            return Status::Pending;
163,076✔
804
        }
163,076✔
805

81,362✔
806
    private:
163,076✔
807
        const Instruction::ArrayInsert& m_instr;
163,076✔
808
    };
163,076✔
809
    ArrayInsertResolver(this, instr).resolve();
163,076✔
810
}
163,076✔
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
{
4,448✔
849
    struct ArrayEraseResolver : public PathResolver {
4,448✔
850
        ArrayEraseResolver(InstructionApplier* applier, const Instruction::ArrayErase& instr)
4,448✔
851
            : PathResolver(applier, instr, "ArrayErase")
4,448✔
852
            , m_instr(instr)
4,448✔
853
        {
4,448✔
854
        }
4,448✔
855
        Status on_list_index(LstBase& list, uint32_t index) override
4,448✔
856
        {
4,448✔
857
            if (index >= m_instr.prior_size) {
4,448✔
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()) {
4,448✔
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()) {
4,448✔
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);
4,448✔
869
            return Status::Pending;
4,448✔
870
        }
4,448✔
871

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

878
void InstructionApplier::operator()(const Instruction::Clear& instr)
879
{
274✔
880
    struct ClearResolver : public PathResolver {
274✔
881
        ClearResolver(InstructionApplier* applier, const Instruction::Clear& instr)
274✔
882
            : PathResolver(applier, instr, "Clear")
274✔
883
        {
274✔
884
        }
274✔
885
        void on_list(LstBase& list) override
274✔
886
        {
218✔
887
            list.clear();
162✔
888
        }
162✔
889
        void on_dictionary(Dictionary& dict) override
274✔
890
        {
176✔
891
            dict.clear();
48✔
892
        }
48✔
893
        void on_set(SetBase& set) override
274✔
894
        {
184✔
895
            set.clear();
64✔
896
        }
64✔
897
        void on_property(Obj& obj, ColKey col_key) override
274✔
898
        {
152✔
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
    };
274✔
921
    ClearResolver(this, instr).resolve();
274✔
922
}
274✔
923

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

8✔
959
    private:
16✔
960
        bool m_allows_nulls;
16✔
961
    };
16✔
962
    return AllowsNullsResolver(this, instr, instr_name).allows_nulls();
16✔
963
}
16✔
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
{
21,872✔
975
    bool valid_payload = true;
21,872✔
976
    using Type = Instruction::Payload::Type;
21,872✔
977
    if (payload.type == Type::Link) {
21,872✔
978
        StringData class_name = get_string(payload.data.link.target_table);
500✔
979
        Group::TableNameBuffer buffer;
500✔
980
        StringData target_table_name = Group::class_name_to_table_name(class_name, buffer);
500✔
981
        TableRef target_table = m_transaction.get_table(target_table_name);
500✔
982
        if (!target_table) {
500✔
983
            bad_transaction_log("Link with invalid target table '%1'", target_table_name);
×
984
        }
×
985
        if (target_table->is_embedded()) {
500✔
986
            bad_transaction_log("Link to embedded table '%1'", target_table_name);
×
987
        }
×
988
        Mixed linked_pk =
500✔
989
            mpark::visit(util::overload{[&](mpark::monostate) {
262✔
990
                                            return Mixed{}; // the link exists and the pk is null
24✔
991
                                        },
24✔
992
                                        [&](int64_t pk) {
486✔
993
                                            return Mixed{pk};
472✔
994
                                        },
472✔
995
                                        [&](InternString interned_pk) {
250✔
996
                                            return Mixed{get_string(interned_pk)};
×
997
                                        },
×
998
                                        [&](GlobalKey) {
250✔
999
                                            bad_transaction_log(
×
1000
                                                "Unexpected link to embedded object while validating a primary key");
×
1001
                                            return Mixed{}; // appease the compiler; visitors must have a single
×
1002
                                                            // return type
1003
                                        },
×
1004
                                        [&](ObjectId pk) {
252✔
1005
                                            return Mixed{pk};
4✔
1006
                                        },
4✔
1007
                                        [&](UUID pk) {
250✔
1008
                                            return Mixed{pk};
×
1009
                                        }},
×
1010
                         payload.data.link.target);
500✔
1011

250✔
1012
        if (!target_table->find_primary_key(linked_pk)) {
500✔
1013
            valid_payload = false;
48✔
1014
        }
48✔
1015
    }
500✔
1016
    return valid_payload;
21,872✔
1017
}
21,872✔
1018

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

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

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

848✔
1104
            m_applier->visit_payload(m_instr.value, inserter);
1,696✔
1105
        }
1,696✔
1106

848✔
1107
    private:
1,696✔
1108
        const Instruction::SetInsert& m_instr;
1,696✔
1109
    };
1,696✔
1110
    SetInsertResolver(this, instr).resolve();
1,696✔
1111
}
1,696✔
1112

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

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

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

228✔
1198
            m_applier->visit_payload(m_instr.value, inserter);
456✔
1199
        }
456✔
1200

228✔
1201
    private:
456✔
1202
        const Instruction::SetErase& m_instr;
456✔
1203
    };
456✔
1204
    SetEraseResolver(this, instr).resolve();
456✔
1205
}
456✔
1206

1207
StringData InstructionApplier::get_table_name(const Instruction::TableInstruction& instr,
1208
                                              const std::string_view& name)
1209
{
312,346✔
1210
    if (auto class_name = m_log->try_get_string(instr.table)) {
312,352✔
1211
        return Group::class_name_to_table_name(*class_name, m_table_name_buffer);
312,350✔
1212
    }
312,350✔
1213
    else {
2,147,483,649✔
1214
        bad_transaction_log("Corrupt table name in %1 instruction", name);
2,147,483,649✔
1215
    }
2,147,483,649✔
1216
}
312,346✔
1217

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

1239
util::Optional<Obj> InstructionApplier::get_top_object(const Instruction::ObjectInstruction& instr,
1240
                                                       const std::string_view& name)
1241
{
721,838✔
1242
    if (m_last_table_name == instr.table && m_last_object_key && m_last_object &&
721,838✔
1243
        *m_last_object_key == instr.object) {
642,270✔
1244
        // We have already found the object, reuse it.
115,224✔
1245
        return *m_last_object;
243,980✔
1246
    }
243,980✔
1247
    else {
477,858✔
1248
        TableRef table = get_table(instr, name);
477,858✔
1249
        ObjKey key = get_object_key(*table, instr.object, name);
477,858✔
1250
        if (!key) {
477,858✔
1251
            return util::none;
×
1252
        }
×
1253
        if (!table->is_valid(key)) {
477,858✔
1254
            // Check if the object is deleted or is a tombstone.
7,642✔
1255
            return util::none;
15,042✔
1256
        }
15,042✔
1257

225,686✔
1258
        Obj obj = table->get_object(key);
462,816✔
1259
        m_last_object_key = instr.object;
462,816✔
1260
        m_last_object = obj;
462,816✔
1261
        return obj;
462,816✔
1262
    }
462,816✔
1263
}
721,838✔
1264

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

1288
InstructionApplier::PathResolver::PathResolver(InstructionApplier* applier, const Instruction::PathInstruction& instr,
1289
                                               const std::string_view& instr_name)
1290
    : m_applier(applier)
1291
    , m_path_instr(instr)
1292
    , m_instr_name(instr_name)
1293
{
663,586✔
1294
}
663,586✔
1295

1296
InstructionApplier::PathResolver::~PathResolver()
1297
{
666,652✔
1298
    on_finish();
666,652✔
1299
}
666,652✔
1300

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

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

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

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

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

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

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

1338
void InstructionApplier::PathResolver::on_column_advance(ColKey col)
1339
{
659,808✔
1340
    m_applier->m_last_field = col;
659,808✔
1341
}
659,808✔
1342

1343
void InstructionApplier::PathResolver::on_dict_key_advance(StringData) {}
2,092✔
1344

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

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

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

1363
void InstructionApplier::PathResolver::on_finish()
1364
{
670,376✔
1365
    m_applier->m_current_path.reset();
670,376✔
1366
    m_applier->m_last_field_name = InternString{};
670,376✔
1367
    m_applier->m_last_field = ColKey{};
670,376✔
1368
}
670,376✔
1369

1370
StringData InstructionApplier::PathResolver::get_string(InternString interned)
1371
{
697,246✔
1372
    return m_applier->get_string(interned);
697,246✔
1373
}
697,246✔
1374

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

320,036✔
1388
    m_it_begin = m_path_instr.path.begin();
664,112✔
1389
    m_it_end = m_path_instr.path.end();
664,112✔
1390
    Status status = resolve_field(*obj, m_path_instr.field);
664,112✔
1391
    return status == Status::Pending ? Status::Success : status;
661,876✔
1392
}
664,112✔
1393

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

336,466✔
1405
    if (m_it_begin == m_it_end) {
688,380✔
1406
        if (col.is_list()) {
487,820✔
1407
            auto list = obj.get_listbase_ptr(col);
198✔
1408
            on_list(*list);
198✔
1409
        }
198✔
1410
        else if (col.is_dictionary()) {
487,622✔
1411
            auto dict = obj.get_dictionary(col);
80✔
1412
            on_dictionary(dict);
80✔
1413
        }
80✔
1414
        else if (col.is_set()) {
487,542✔
1415
            SetBasePtr set;
3,484✔
1416
            if (col.get_type() == col_type_Link) {
3,484✔
1417
                // We are interested in using non-condensed indexes - as for Lists below
152✔
1418
                set = obj.get_set_ptr<ObjKey>(col);
304✔
1419
            }
304✔
1420
            else {
3,180✔
1421
                set = obj.get_setbase_ptr(col);
3,180✔
1422
            }
3,180✔
1423
            on_set(*set);
3,484✔
1424
        }
3,484✔
1425
        else {
484,058✔
1426
            on_property(obj, col);
484,058✔
1427
        }
484,058✔
1428
        return Status::Pending;
487,820✔
1429
    }
487,820✔
1430

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

1499
InstructionApplier::PathResolver::Status InstructionApplier::PathResolver::resolve_list_element(LstBase& list,
1500
                                                                                                uint32_t index)
1501
{
185,466✔
1502
    if (m_it_begin == m_it_end) {
185,466✔
1503
        return on_list_index(list, index);
175,294✔
1504
    }
175,294✔
1505

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

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

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

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

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

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

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

2,038✔
1573
    on_dict_key_advance(string_key);
4,076✔
1574

2,038✔
1575
    auto col = dict.get_col_key();
4,076✔
1576
    auto table = dict.get_table();
4,076✔
1577
    auto field_name = table->get_column_name(col);
4,076✔
1578

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

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

1627

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

12✔
1652
                ObjKey key = table.get_objkey_from_primary_key(realm::util::none);
24✔
1653
                return key;
24✔
1654
            },
24✔
1655
            [&](int64_t pk) {
473,038✔
1656
                if (!pk_col) {
467,746✔
1657
                    bad_transaction_log("%1 instruction with integer primary key (%2), but table '%3' does not have "
×
1658
                                        "a primary key column",
×
1659
                                        name, pk, table_name);
×
1660
                }
×
1661
                if (pk_type != type_Int) {
467,746✔
1662
                    bad_transaction_log(
×
1663
                        "%1 instruction with integer primary key (%2), but '%3.%4' has primary keys of type '%5'",
×
1664
                        name, pk, table_name, pk_name, pk_type);
×
1665
                }
×
1666
                ObjKey key = table.get_objkey_from_primary_key(pk);
467,746✔
1667
                return key;
467,746✔
1668
            },
467,746✔
1669
            [&](InternString interned_pk) {
248,430✔
1670
                auto pk = get_string(interned_pk);
17,286✔
1671
                if (!pk_col) {
17,286✔
1672
                    bad_transaction_log("%1 instruction with string primary key (\"%2\"), but table '%3' does not "
×
1673
                                        "have a primary key column",
×
1674
                                        name, pk, table_name);
×
1675
                }
×
1676
                if (pk_type != type_String) {
17,286✔
1677
                    bad_transaction_log(
×
1678
                        "%1 instruction with string primary key (\"%2\"), but '%3.%4' has primary keys of type '%5'",
×
1679
                        name, pk, table_name, pk_name, pk_type);
×
1680
                }
×
1681
                ObjKey key = table.get_objkey_from_primary_key(pk);
17,286✔
1682
                return key;
17,286✔
1683
            },
17,286✔
1684
            [&](GlobalKey) {
239,238✔
NEW
1685
                bad_transaction_log("%1 instruction without primary key not supported", name);
×
NEW
1686
                return ObjKey();
×
UNCOV
1687
            },
×
1688
            [&](ObjectId pk) {
241,674✔
1689
                if (!pk_col) {
4,876✔
1690
                    bad_transaction_log("%1 instruction with ObjectId primary key (\"%2\"), but table '%3' does not "
×
1691
                                        "have a primary key column",
×
1692
                                        name, pk, table_name);
×
1693
                }
×
1694
                if (pk_type != type_ObjectId) {
4,876✔
1695
                    bad_transaction_log(
×
1696
                        "%1 instruction with ObjectId primary key (%2), but '%3.%4' has primary keys of type '%5'",
×
1697
                        name, pk, table_name, pk_name, pk_type);
×
1698
                }
×
1699
                ObjKey key = table.get_objkey_from_primary_key(pk);
4,876✔
1700
                return key;
4,876✔
1701
            },
4,876✔
1702
            [&](UUID pk) {
239,248✔
1703
                if (!pk_col) {
20✔
1704
                    bad_transaction_log("%1 instruction with UUID primary key (\"%2\"), but table '%3' does not "
×
1705
                                        "have a primary key column",
×
1706
                                        name, pk, table_name);
×
1707
                }
×
1708
                if (pk_type != type_UUID) {
20✔
1709
                    bad_transaction_log(
×
1710
                        "%1 instruction with UUID primary key (%2), but '%3.%4' has primary keys of type '%5'", name,
×
1711
                        pk, table_name, pk_name, pk_type);
×
1712
                }
×
1713
                ObjKey key = table.get_objkey_from_primary_key(pk);
20✔
1714
                return key;
20✔
1715
            }},
20✔
1716
        primary_key);
484,694✔
1717
}
484,694✔
1718

1719

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

© 2025 Coveralls, Inc