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

realm / realm-core / 1863

24 Nov 2023 10:28AM UTC coverage: 91.688% (-0.02%) from 91.708%
1863

push

Evergreen

web-flow
Add missing install headers (#7165)

* add missing install headers

* validate installed headers on PR triggers

92392 of 169288 branches covered (0.0%)

231720 of 252727 relevant lines covered (91.69%)

6390825.2 hits per line

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

70.21
/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
{
783,410✔
66
    auto string = m_log->try_get_intern_string(str);
783,410✔
67
    if (REALM_UNLIKELY(!string))
783,410✔
68
        bad_transaction_log("string read fails");
386,388✔
69
    return m_log->get_string(*string);
783,410✔
70
}
783,410✔
71

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

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

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

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

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

6,272✔
121
    auto add_table = util::overload{
12,792✔
122
        [&](const Instruction::AddTable::TopLevelTable& spec) {
12,682✔
123
            auto table_type = (spec.is_asymmetric ? Table::Type::TopLevelAsymmetric : Table::Type::TopLevel);
12,560✔
124
            if (spec.pk_type == Instruction::Payload::Type::GlobalKey) {
12,572✔
125
                log("sync::create_table(group, \"%1\", %2);", table_name, table_type);
8✔
126
                m_transaction.get_or_add_table(table_name, table_type);
8✔
127
            }
8✔
128
            else {
12,564✔
129
                if (!is_valid_key_type(spec.pk_type)) {
12,564✔
130
                    bad_transaction_log("Invalid primary key type '%1' while adding table '%2'", int8_t(spec.pk_type),
×
131
                                        table_name);
×
132
                }
×
133
                DataType pk_type = get_data_type(spec.pk_type);
12,564✔
134
                StringData pk_field = get_string(spec.pk_field);
12,564✔
135
                bool nullable = spec.pk_nullable;
12,564✔
136

6,158✔
137
                log("group.get_or_add_table_with_primary_key(group, \"%1\", %2, \"%3\", %4, %5);", table_name,
12,564✔
138
                    pk_type, pk_field, nullable, table_type);
12,564✔
139
                if (!m_transaction.get_or_add_table_with_primary_key(table_name, pk_type, pk_field, nullable,
12,564✔
140
                                                                     table_type)) {
6,158✔
141
                    bad_transaction_log("AddTable: The existing table '%1' has different properties", table_name);
×
142
                }
×
143
            }
12,564✔
144
        },
12,572✔
145
        [&](const Instruction::AddTable::EmbeddedTable&) {
6,382✔
146
            if (TableRef table = m_transaction.get_table(table_name)) {
220✔
147
                if (!table->is_embedded()) {
×
148
                    bad_transaction_log("AddTable: The existing table '%1' is not embedded", table_name);
×
149
                }
×
150
            }
×
151
            else {
220✔
152
                log("group.add_embedded_table(\"%1\");", table_name);
220✔
153
                m_transaction.add_table(table_name, Table::Type::Embedded);
220✔
154
            }
220✔
155
        },
220✔
156
    };
12,792✔
157

6,272✔
158
    mpark::visit(std::move(add_table), instr.type);
12,792✔
159
}
12,792✔
160

161
void InstructionApplier::operator()(const Instruction::EraseTable& instr)
162
{
3,724✔
163
    auto table_name = get_table_name(instr);
3,724✔
164
    // Temporarily swap out the last object key so it doesn't get included in error messages
1,690✔
165
    TemporarySwapOut<decltype(m_last_object_key)> last_object_key_guard(m_last_object_key);
3,724✔
166

1,690✔
167
    if (REALM_UNLIKELY(REALM_COVER_NEVER(!m_transaction.has_table(table_name)))) {
3,724✔
168
        // FIXME: Should EraseTable be considered idempotent?
169
        bad_transaction_log("table does not exist");
×
170
    }
×
171

1,690✔
172
    log("sync::erase_table(m_group, \"%1\")", table_name);
3,724✔
173
    m_transaction.remove_table(table_name);
3,724✔
174
}
3,724✔
175

176
void InstructionApplier::operator()(const Instruction::CreateObject& instr)
177
{
137,596✔
178
    auto table = get_table(instr);
137,596✔
179
    ColKey pk_col = table->get_primary_key_column();
137,596✔
180
    m_last_object_key = instr.object;
137,596✔
181

68,294✔
182
    mpark::visit(
137,596✔
183
        util::overload{
137,596✔
184
            [&](mpark::monostate) {
68,310✔
185
                if (!pk_col) {
32✔
186
                    bad_transaction_log("CreateObject(NULL) on table without a primary key");
×
187
                }
×
188
                if (!table->is_nullable(pk_col)) {
32✔
189
                    bad_transaction_log("CreateObject(NULL) on a table with a non-nullable primary key");
×
190
                }
×
191
                log("sync::create_object_with_primary_key(group, get_table(\"%1\"), realm::util::none);",
32✔
192
                    table->get_name());
32✔
193
                m_last_object = table->create_object_with_primary_key(util::none);
32✔
194
            },
32✔
195
            [&](int64_t pk) {
123,922✔
196
                if (!pk_col) {
110,872✔
197
                    bad_transaction_log("CreateObject(Int) on table without a primary key");
×
198
                }
×
199
                if (table->get_column_type(pk_col) != type_Int) {
110,872✔
200
                    bad_transaction_log("CreateObject(Int) on a table with primary key type %1",
×
201
                                        table->get_column_type(pk_col));
×
202
                }
×
203
                log("sync::create_object_with_primary_key(group, get_table(\"%1\"), %2);", table->get_name(), pk);
110,872✔
204
                m_last_object = table->create_object_with_primary_key(pk);
110,872✔
205
            },
110,872✔
206
            [&](InternString pk) {
77,856✔
207
                if (!pk_col) {
19,292✔
208
                    bad_transaction_log("CreateObject(String) on table without a primary key");
×
209
                }
×
210
                if (table->get_column_type(pk_col) != type_String) {
19,292✔
211
                    bad_transaction_log("CreateObject(String) on a table with primary key type %1",
×
212
                                        table->get_column_type(pk_col));
×
213
                }
×
214
                StringData str = get_string(pk);
19,292✔
215
                log("sync::create_object_with_primary_key(group, get_table(\"%1\"), \"%2\");", table->get_name(),
19,292✔
216
                    str);
19,292✔
217
                m_last_object = table->create_object_with_primary_key(str);
19,292✔
218
            },
19,292✔
219
            [&](const ObjectId& id) {
72,360✔
220
                if (!pk_col) {
7,342✔
221
                    bad_transaction_log("CreateObject(ObjectId) on table without a primary key");
×
222
                }
×
223
                if (table->get_column_type(pk_col) != type_ObjectId) {
7,342✔
224
                    bad_transaction_log("CreateObject(ObjectId) on a table with primary key type %1",
×
225
                                        table->get_column_type(pk_col));
×
226
                }
×
227
                log("sync::create_object_with_primary_key(group, get_table(\"%1\"), %2);", table->get_name(), id);
7,342✔
228
                m_last_object = table->create_object_with_primary_key(id);
7,342✔
229
            },
7,342✔
230
            [&](const UUID& id) {
68,318✔
231
                if (!pk_col) {
48✔
232
                    bad_transaction_log("CreateObject(UUID) on table without a primary key");
×
233
                }
×
234
                if (table->get_column_type(pk_col) != type_UUID) {
48✔
235
                    bad_transaction_log("CreateObject(UUID) on a table with primary key type %1",
×
236
                                        table->get_column_type(pk_col));
×
237
                }
×
238
                log("sync::create_object_with_primary_key(group, get_table(\"%1\"), %2);", table->get_name(), id);
48✔
239
                m_last_object = table->create_object_with_primary_key(id);
48✔
240
            },
48✔
241
            [&](GlobalKey key) {
68,300✔
242
                if (pk_col) {
12✔
243
                    bad_transaction_log("CreateObject(GlobalKey) on table with a primary key");
×
244
                }
×
245
                log("sync::create_object_with_primary_key(group, get_table(\"%1\"), GlobalKey{%2, %3});",
12✔
246
                    table->get_name(), key.hi(), key.lo());
12✔
247
                m_last_object = table->create_object(key);
12✔
248
            },
12✔
249
        },
137,596✔
250
        instr.object);
137,596✔
251
}
137,596✔
252

253
void InstructionApplier::operator()(const Instruction::EraseObject& instr)
254
{
57,378✔
255
    // FIXME: Log actions.
29,308✔
256
    // Note: EraseObject is idempotent.
29,308✔
257
    if (auto obj = get_top_object(instr, "EraseObject")) {
57,378✔
258
        // This call will prevent incoming links to be nullified/deleted
22,016✔
259
        obj->invalidate();
43,192✔
260
    }
43,192✔
261
    m_last_object.reset();
57,378✔
262
}
57,378✔
263

264
template <class F>
265
void InstructionApplier::visit_payload(const Instruction::Payload& payload, F&& visitor)
266
{
648,332✔
267
    using Type = Instruction::Payload::Type;
648,332✔
268

319,094✔
269
    const auto& data = payload.data;
648,332✔
270
    switch (payload.type) {
648,332✔
271
        case Type::ObjectValue:
2,668✔
272
            return visitor(Instruction::Payload::ObjectValue{});
2,668✔
273
        case Type::Dictionary:
✔
274
            return bad_transaction_log("Nested dictionaries not supported yet");
×
275
        case Type::Erased:
252✔
276
            return visitor(Instruction::Payload::Erased{});
252✔
277
        case Type::GlobalKey:
✔
278
            return visitor(realm::util::none); // FIXME: Not sure about this
×
279
        case Type::Null:
944✔
280
            return visitor(realm::util::none);
944✔
281
        case Type::Int:
450,844✔
282
            return visitor(data.integer);
450,844✔
283
        case Type::Bool:
184✔
284
            return visitor(data.boolean);
184✔
285
        case Type::String: {
173,550✔
286
            StringData value = get_string(data.str);
173,550✔
287
            return visitor(value);
173,550✔
288
        }
×
289
        case Type::Binary: {
11,724✔
290
            BinaryData value = get_binary(data.binary);
11,724✔
291
            return visitor(value);
11,724✔
292
        }
×
293
        case Type::Timestamp:
3,082✔
294
            return visitor(data.timestamp);
3,082✔
295
        case Type::Float:
260✔
296
            return visitor(data.fnum);
260✔
297
        case Type::Double:
1,192✔
298
            return visitor(data.dnum);
1,192✔
299
        case Type::Decimal:
168✔
300
            return visitor(data.decimal);
168✔
301
        case Type::Link: {
2,052✔
302
            StringData class_name = get_string(data.link.target_table);
2,052✔
303
            Group::TableNameBuffer buffer;
2,052✔
304
            StringData target_table_name = Group::class_name_to_table_name(class_name, buffer);
2,052✔
305
            TableRef target_table = m_transaction.get_table(target_table_name);
2,052✔
306
            if (!target_table) {
2,052✔
307
                bad_transaction_log("Link with invalid target table '%1'", target_table_name);
×
308
            }
×
309
            if (target_table->is_embedded()) {
2,052✔
310
                bad_transaction_log("Link to embedded table '%1'", target_table_name);
×
311
            }
×
312
            ObjKey target = get_object_key(*target_table, data.link.target);
2,052✔
313
            ObjLink link = ObjLink{target_table->get_key(), target};
2,052✔
314
            return visitor(link);
2,052✔
315
        }
×
316
        case Type::ObjectId:
1,120✔
317
            return visitor(data.object_id);
1,120✔
318
        case Type::UUID:
304✔
319
            return visitor(data.uuid);
304✔
320
    }
648,332✔
321
}
648,332✔
322

323
void InstructionApplier::operator()(const Instruction::Update& instr)
324
{
481,448✔
325
    struct UpdateResolver : public PathResolver {
481,448✔
326
        UpdateResolver(InstructionApplier* applier, const Instruction::Update& instr)
481,448✔
327
            : PathResolver(applier, instr, "Update")
481,448✔
328
            , m_instr(instr)
481,448✔
329
        {
481,442✔
330
        }
481,442✔
331
        void on_property(Obj& obj, ColKey col) override
481,448✔
332
        {
477,698✔
333
            // Update of object field.
230,840✔
334

230,840✔
335
            auto table = obj.get_table();
473,528✔
336
            auto table_name = table->get_name();
473,528✔
337
            auto field_name = table->get_column_name(col);
473,528✔
338
            auto data_type = DataType(col.get_type());
473,528✔
339

230,840✔
340
            auto visitor = [&](const mpark::variant<ObjLink, Mixed, Instruction::Payload::ObjectValue,
473,528✔
341
                                                    Instruction::Payload::Erased>& arg) {
473,536✔
342
                if (const auto link_ptr = mpark::get_if<ObjLink>(&arg)) {
473,496✔
343
                    if (data_type == type_Mixed || data_type == type_TypedLink) {
100✔
344
                        obj.set_any(col, *link_ptr, m_instr.is_default);
16✔
345
                    }
16✔
346
                    else if (data_type == type_Link) {
84✔
347
                        // Validate target table.
42✔
348
                        auto target_table = table->get_link_target(col);
84✔
349
                        if (target_table->get_key() != link_ptr->get_table_key()) {
84✔
350
                            m_applier->bad_transaction_log(
×
351
                                "Update: Target table mismatch (expected %1, got %2)", target_table->get_name(),
×
352
                                m_applier->m_transaction.get_table(link_ptr->get_table_key())->get_name());
×
353
                        }
×
354
                        obj.set<ObjKey>(col, link_ptr->get_obj_key(), m_instr.is_default);
84✔
355
                    }
84✔
356
                    else {
×
357
                        m_applier->bad_transaction_log("Update: Type mismatch in '%2.%1' (expected %3, got %4)",
×
358
                                                       field_name, table_name, col.get_type(), type_Link);
×
359
                    }
×
360
                }
100✔
361
                else if (const auto mixed_ptr = mpark::get_if<Mixed>(&arg)) {
473,396✔
362
                    if (mixed_ptr->is_null()) {
472,774✔
363
                        if (col.is_nullable()) {
764✔
364
                            obj.set_null(col, m_instr.is_default);
764✔
365
                        }
764✔
366
                        else {
×
367
                            m_applier->bad_transaction_log("Update: NULL in non-nullable field '%2.%1'", field_name,
×
368
                                                           table_name);
×
369
                        }
×
370
                    }
764✔
371
                    else if (data_type == type_Mixed || mixed_ptr->get_type() == data_type) {
472,010✔
372
                        obj.set_any(col, *mixed_ptr, m_instr.is_default);
471,988✔
373
                    }
471,988✔
374
                    else {
22✔
375
                        m_applier->bad_transaction_log("Update: Type mismatch in '%2.%1' (expected %3, got %4)",
22✔
376
                                                       field_name, table_name, col.get_type(), mixed_ptr->get_type());
22✔
377
                    }
22✔
378
                }
472,774✔
379
                else if (const auto obj_val_ptr = mpark::get_if<Instruction::Payload::ObjectValue>(&arg)) {
662✔
380
                    if (obj.is_null(col)) {
648✔
381
                        obj.create_and_set_linked_object(col);
620✔
382
                    }
620✔
383
                }
648✔
384
                else if (const auto erase_ptr = mpark::get_if<Instruction::Payload::Erased>(&arg)) {
2,147,483,661✔
385
                    m_applier->bad_transaction_log("Update: Dictionary erase at object field");
×
386
                }
×
387
            };
473,496✔
388

230,840✔
389
            m_applier->visit_payload(m_instr.value, visitor);
473,528✔
390
        }
473,528✔
391
        Status on_list_index(LstBase& list, uint32_t index) override
481,448✔
392
        {
237,378✔
393
            // Update of list element.
2,800✔
394

2,800✔
395
            auto col = list.get_col_key();
5,168✔
396
            auto data_type = DataType(col.get_type());
5,168✔
397
            auto table = list.get_table();
5,168✔
398
            auto table_name = table->get_name();
5,168✔
399
            auto field_name = table->get_column_name(col);
5,168✔
400

2,800✔
401
            auto visitor = util::overload{
5,168✔
402
                [&](const ObjLink& link) {
2,878✔
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_LinkList || 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) {
5,090✔
432
                    if (value.is_null()) {
5,012✔
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 {
5,012✔
442
                        if (data_type == type_Mixed || value.get_type() == data_type) {
5,012✔
443
                            list.set_any(index, value);
5,012✔
444
                        }
5,012✔
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
                    }
5,012✔
451
                },
5,012✔
452
                [&](const Instruction::Payload::ObjectValue&) {
2,800✔
453
                    // Embedded object creation is idempotent, and link lists cannot
454
                    // contain nulls, so this is a no-op.
455
                },
×
456
                [&](const Instruction::Payload::Erased&) {
2,800✔
457
                    m_applier->bad_transaction_log("Update: Dictionary erase of list element");
×
458
                },
×
459
            };
5,168✔
460

2,800✔
461
            m_applier->visit_payload(m_instr.value, visitor);
5,168✔
462
            return Status::Pending;
5,168✔
463
        }
5,168✔
464
        Status on_dictionary_key(Dictionary& dict, Mixed key) override
481,448✔
465
        {
236,380✔
466
            // Update (insert) of dictionary element.
1,370✔
467

1,370✔
468
            auto visitor = util::overload{
2,740✔
469
                [&](Mixed value) {
2,434✔
470
                    if (value.is_null()) {
2,128✔
471
                        // FIXME: Separate handling of NULL is needed because
56✔
472
                        // `Mixed::get_type()` asserts on NULL.
56✔
473
                        dict.insert(key, value);
112✔
474
                    }
112✔
475
                    else if (value.get_type() == type_Link) {
2,016✔
476
                        m_applier->bad_transaction_log("Update: Untyped links are not supported in dictionaries.");
×
477
                    }
×
478
                    else {
2,016✔
479
                        dict.insert(key, value);
2,016✔
480
                    }
2,016✔
481
                },
2,128✔
482
                [&](const Instruction::Payload::Erased&) {
1,496✔
483
                    dict.erase(key);
252✔
484
                },
252✔
485
                [&](const Instruction::Payload::ObjectValue&) {
1,550✔
486
                    dict.create_and_insert_linked_object(key);
360✔
487
                },
360✔
488
            };
2,740✔
489

1,370✔
490
            m_applier->visit_payload(m_instr.value, visitor);
2,740✔
491
            return Status::Pending;
2,740✔
492
        }
2,740✔
493

235,010✔
494
    private:
481,448✔
495
        const Instruction::Update& m_instr;
481,448✔
496
    };
481,448✔
497
    UpdateResolver resolver(this, instr);
481,448✔
498
    resolver.resolve();
481,448✔
499
}
481,448✔
500

501
void InstructionApplier::operator()(const Instruction::AddInteger& instr)
502
{
3,532✔
503
    // FIXME: Implement increments of array elements, dictionary values.
1,740✔
504
    struct AddIntegerResolver : public PathResolver {
3,532✔
505
        AddIntegerResolver(InstructionApplier* applier, const Instruction::AddInteger& instr)
3,532✔
506
            : PathResolver(applier, instr, "AddInteger")
3,532✔
507
            , m_instr(instr)
3,532✔
508
        {
3,532✔
509
        }
3,532✔
510
        void on_property(Obj& obj, ColKey col)
3,532✔
511
        {
3,532✔
512
            // Increment of object field.
1,740✔
513
            if (!obj.is_null(col)) {
3,532✔
514
                try {
3,476✔
515
                    obj.add_int(col, m_instr.value);
3,476✔
516
                }
3,476✔
517
                catch (const LogicError&) {
1,712✔
518
                    auto table = obj.get_table();
×
519
                    m_applier->bad_transaction_log("AddInteger: Not an integer field '%2.%1'",
×
520
                                                   table->get_column_name(col), table->get_name());
×
521
                }
×
522
            }
3,476✔
523
        }
3,532✔
524

1,740✔
525
    private:
3,532✔
526
        const Instruction::AddInteger& m_instr;
3,532✔
527
    };
3,532✔
528
    AddIntegerResolver resolver(this, instr);
3,532✔
529
    resolver.resolve();
3,532✔
530
}
3,532✔
531

532
void InstructionApplier::operator()(const Instruction::AddColumn& instr)
533
{
22,098✔
534
    using Type = Instruction::Payload::Type;
22,098✔
535
    using CollectionType = Instruction::AddColumn::CollectionType;
22,098✔
536

10,804✔
537
    // Temporarily swap out the last object key so it doesn't get included in error messages
10,804✔
538
    TemporarySwapOut<decltype(m_last_object_key)> last_object_key_guard(m_last_object_key);
22,098✔
539

10,804✔
540
    auto table = get_table(instr, "AddColumn");
22,098✔
541
    auto col_name = get_string(instr.field);
22,098✔
542

10,804✔
543
    if (ColKey existing_key = table->get_column_key(col_name)) {
22,098✔
544
        DataType new_type = get_data_type(instr.type);
304✔
545
        ColumnType existing_type = existing_key.get_type();
304✔
546
        if (existing_type == col_type_LinkList) {
304✔
547
            existing_type = col_type_Link;
×
548
        }
×
549
        if (existing_type != ColumnType(new_type)) {
304✔
550
            bad_transaction_log("AddColumn: Schema mismatch for existing column in '%1.%2' (expected %3, got %4)",
4✔
551
                                table->get_name(), col_name, existing_type, new_type);
4✔
552
        }
4✔
553
        bool existing_is_list = existing_key.is_list();
304✔
554
        if ((instr.collection_type == CollectionType::List) != existing_is_list) {
304✔
555
            bad_transaction_log(
×
556
                "AddColumn: Schema mismatch for existing column in '%1.%2' (existing is%3 a list, the other is%4)",
×
557
                table->get_name(), col_name, existing_is_list ? "" : " not", existing_is_list ? " not" : "");
×
558
        }
×
559
        bool existing_is_set = existing_key.is_set();
304✔
560
        if ((instr.collection_type == CollectionType::Set) != existing_is_set) {
304✔
561
            bad_transaction_log(
×
562
                "AddColumn: Schema mismatch for existing column in '%1.%2' (existing is%3 a set, the other is%4)",
×
563
                table->get_name(), col_name, existing_is_set ? "" : " not", existing_is_set ? " not" : "");
×
564
        }
×
565
        bool existing_is_dict = existing_key.is_dictionary();
304✔
566
        if ((instr.collection_type == CollectionType::Dictionary) != existing_is_dict) {
304✔
567
            bad_transaction_log("AddColumn: Schema mismatch for existing column in '%1.%2' (existing is%3 a "
×
568
                                "dictionary, the other is%4)",
×
569
                                table->get_name(), col_name, existing_is_dict ? "" : " not",
×
570
                                existing_is_dict ? " not" : "");
×
571
        }
×
572
        if (new_type == type_Link) {
304✔
573
            Group::TableNameBuffer buffer;
16✔
574
            auto target_table_name = Group::class_name_to_table_name(get_string(instr.link_target_table), buffer);
16✔
575
            if (target_table_name != table->get_link_target(existing_key)->get_name()) {
16✔
576
                bad_transaction_log("AddColumn: Schema mismatch for existing column in '%1.%2' (link targets differ)",
×
577
                                    table->get_name(), col_name);
×
578
            }
×
579
        }
16✔
580
        return;
304✔
581
    }
304✔
582

10,652✔
583
    if (instr.collection_type == CollectionType::Dictionary && instr.key_type != Type::String) {
21,794✔
584
        bad_transaction_log("AddColumn '%1.%3' adding dictionary column with non-string keys", table->get_name(),
×
585
                            col_name);
×
586
    }
×
587

10,652✔
588
    if (instr.type != Type::Link) {
21,794✔
589
        DataType type = get_data_type(instr.type);
20,626✔
590
        switch (instr.collection_type) {
20,626✔
591
            case CollectionType::Single: {
14,528✔
592
                table->add_column(type, col_name, instr.nullable);
14,528✔
593
                break;
14,528✔
594
            }
×
595
            case CollectionType::List: {
5,866✔
596
                table->add_column_list(type, col_name, instr.nullable);
5,866✔
597
                break;
5,866✔
598
            }
×
599
            case CollectionType::Dictionary: {
104✔
600
                DataType key_type = get_data_type(instr.key_type);
104✔
601
                table->add_column_dictionary(type, col_name, instr.nullable, key_type);
104✔
602
                break;
104✔
603
            }
×
604
            case CollectionType::Set: {
128✔
605
                table->add_column_set(type, col_name, instr.nullable);
128✔
606
                break;
128✔
607
            }
1,168✔
608
        }
1,168✔
609
    }
1,168✔
610
    else {
1,168✔
611
        Group::TableNameBuffer buffer;
1,168✔
612
        auto target_table_name = get_string(instr.link_target_table);
1,168✔
613
        if (target_table_name.size() != 0) {
1,168✔
614
            TableRef target = m_transaction.get_table(Group::class_name_to_table_name(target_table_name, buffer));
1,160✔
615
            if (!target) {
1,160✔
616
                bad_transaction_log("AddColumn(Link) '%1.%2' to table '%3' which doesn't exist", table->get_name(),
×
617
                                    col_name, target_table_name);
×
618
            }
×
619
            if (instr.collection_type == CollectionType::List) {
1,160✔
620
                table->add_column_list(*target, col_name);
596✔
621
            }
596✔
622
            else if (instr.collection_type == CollectionType::Set) {
564✔
623
                table->add_column_set(*target, col_name);
8✔
624
            }
8✔
625
            else if (instr.collection_type == CollectionType::Dictionary) {
556✔
626
                table->add_column_dictionary(*target, col_name);
32✔
627
            }
32✔
628
            else {
524✔
629
                REALM_ASSERT(instr.collection_type == CollectionType::Single);
524✔
630
                table->add_column(*target, col_name);
524✔
631
            }
524✔
632
        }
1,160✔
633
        else {
8✔
634
            if (instr.collection_type == CollectionType::List) {
8✔
635
                table->add_column_list(type_TypedLink, col_name);
×
636
            }
×
637
            else {
8✔
638
                REALM_ASSERT(instr.collection_type == CollectionType::Single);
8✔
639
                table->add_column(type_TypedLink, col_name);
8✔
640
            }
8✔
641
        }
8✔
642
    }
1,168✔
643
}
21,794✔
644

645
void InstructionApplier::operator()(const Instruction::EraseColumn& instr)
646
{
×
647
    // Temporarily swap out the last object key so it doesn't get included in error messages
648
    TemporarySwapOut<decltype(m_last_object_key)> last_object_key_guard(m_last_object_key);
×
649

650
    auto table = get_table(instr, "EraseColumn");
×
651
    auto col_name = get_string(instr.field);
×
652

653
    ColKey col = table->get_column_key(col_name);
×
654
    if (!col) {
×
655
        bad_transaction_log("EraseColumn '%1.%2' which doesn't exist", table->get_name(), col_name);
×
656
    }
×
657

658
    table->remove_column(col);
×
659
}
×
660

661
void InstructionApplier::operator()(const Instruction::ArrayInsert& instr)
662
{
164,698✔
663
    struct ArrayInsertResolver : public PathResolver {
164,698✔
664
        ArrayInsertResolver(InstructionApplier* applier, const Instruction::ArrayInsert& instr)
164,698✔
665
            : PathResolver(applier, instr, "ArrayInsert")
164,698✔
666
            , m_instr(instr)
164,698✔
667
        {
164,698✔
668
        }
164,698✔
669
        Status on_list_index(LstBase& list, uint32_t index) override
164,698✔
670
        {
164,698✔
671
            auto col = list.get_col_key();
164,698✔
672
            auto data_type = DataType(col.get_type());
164,698✔
673
            auto table = list.get_table();
164,698✔
674
            auto table_name = table->get_name();
164,698✔
675
            auto field_name = table->get_column_name(col);
164,698✔
676

82,996✔
677
            if (index > m_instr.prior_size) {
164,698✔
678
                m_applier->bad_transaction_log("ArrayInsert: Invalid insertion index (index = %1, prior_size = %2)",
×
679
                                               index, m_instr.prior_size);
×
680
            }
×
681

82,996✔
682
            if (index > list.size()) {
164,698✔
683
                m_applier->bad_transaction_log("ArrayInsert: Index out of bounds (%1 > %2)", index, list.size());
×
684
            }
×
685

82,996✔
686
            if (m_instr.prior_size != list.size()) {
164,698✔
687
                m_applier->bad_transaction_log("ArrayInsert: Invalid prior_size (list size = %1, prior_size = %2)",
×
688
                                               list.size(), m_instr.prior_size);
×
689
            }
×
690

82,996✔
691
            auto inserter = util::overload{
164,698✔
692
                [&](const ObjLink& link) {
83,622✔
693
                    if (data_type == type_TypedLink) {
1,252✔
694
                        REALM_ASSERT(dynamic_cast<Lst<ObjLink>*>(&list));
×
695
                        auto& link_list = static_cast<Lst<ObjLink>&>(list);
×
696
                        link_list.insert(index, link);
×
697
                    }
×
698
                    else if (data_type == type_Mixed) {
1,252✔
699
                        REALM_ASSERT(dynamic_cast<Lst<Mixed>*>(&list));
164✔
700
                        auto& mixed_list = static_cast<Lst<Mixed>&>(list);
164✔
701
                        mixed_list.insert(index, link);
164✔
702
                    }
164✔
703
                    else if (data_type == type_LinkList || data_type == type_Link) {
1,088!
704
                        REALM_ASSERT(dynamic_cast<Lst<ObjKey>*>(&list));
1,088✔
705
                        auto& link_list = static_cast<Lst<ObjKey>&>(list);
1,088✔
706
                        // Validate the target.
544✔
707
                        auto target_table = table->get_link_target(col);
1,088✔
708
                        if (target_table->get_key() != link.get_table_key()) {
1,088✔
709
                            m_applier->bad_transaction_log(
×
710
                                "ArrayInsert: Target table mismatch (expected '%1', got '%2')",
×
711
                                target_table->get_name(),
×
712
                                m_applier->m_transaction.get_table(link.get_table_key())->get_name());
×
713
                        }
×
714
                        link_list.insert(index, link.get_obj_key());
1,088✔
715
                    }
1,088✔
716
                    else {
×
717
                        m_applier->bad_transaction_log(
×
718
                            "ArrayInsert: Type mismatch in list at '%2.%1' (expected link type, was %3)", field_name,
×
719
                            table_name, data_type);
×
720
                    }
×
721
                },
1,252✔
722
                [&](Mixed value) {
163,242✔
723
                    if (value.is_null()) {
161,786✔
724
                        if (col.is_nullable()) {
52✔
725
                            list.insert_null(index);
52✔
726
                        }
52✔
727
                        else {
×
728
                            m_applier->bad_transaction_log("ArrayInsert: NULL in non-nullable list '%2.%1'",
×
729
                                                           field_name, table_name);
×
730
                        }
×
731
                    }
52✔
732
                    else {
161,734✔
733
                        if (data_type == type_Mixed || value.get_type() == data_type) {
161,734✔
734
                            list.insert_any(index, value);
161,734✔
735
                        }
161,734✔
736
                        else {
×
737
                            m_applier->bad_transaction_log(
×
738
                                "ArrayInsert: Type mismatch in list at '%2.%1' (expected %3, got %4)", field_name,
×
739
                                table_name, data_type, value.get_type());
×
740
                        }
×
741
                    }
161,734✔
742
                },
161,786✔
743
                [&](const Instruction::Payload::ObjectValue&) {
83,826✔
744
                    if (col.get_type() == col_type_LinkList || col.get_type() == col_type_Link) {
1,660!
745
                        auto target_table = list.get_table()->get_link_target(col);
1,660✔
746
                        if (!target_table->is_embedded()) {
1,660✔
747
                            m_applier->bad_transaction_log(
×
748
                                "ArrayInsert: Creation of embedded object of type '%1', which is not "
×
749
                                "an embedded table",
×
750
                                target_table->get_name());
×
751
                        }
×
752

830✔
753
                        REALM_ASSERT(dynamic_cast<LnkLst*>(&list));
1,660✔
754
                        auto& link_list = static_cast<LnkLst&>(list);
1,660✔
755
                        link_list.create_and_insert_linked_object(index);
1,660✔
756
                    }
1,660✔
757
                    else {
×
758
                        m_applier->bad_transaction_log(
×
759
                            "ArrayInsert: Creation of embedded object in non-link list field '%2.%1'", field_name,
×
760
                            table_name);
×
761
                    }
×
762
                },
1,660✔
763
                [&](const Instruction::Payload::Dictionary&) {
164,698✔
764
                    m_applier->bad_transaction_log("Dictionary payload for ArrayInsert");
164,698✔
765
                },
164,698✔
766
                [&](const Instruction::Payload::Erased&) {
82,996✔
767
                    m_applier->bad_transaction_log("Dictionary erase payload for ArrayInsert");
×
768
                },
×
769
            };
164,698✔
770

82,996✔
771
            m_applier->visit_payload(m_instr.value, inserter);
164,698✔
772
            return Status::Pending;
164,698✔
773
        }
164,698✔
774

82,996✔
775
    private:
164,698✔
776
        const Instruction::ArrayInsert& m_instr;
164,698✔
777
    };
164,698✔
778
    ArrayInsertResolver(this, instr).resolve();
164,698✔
779
}
164,698✔
780

781
void InstructionApplier::operator()(const Instruction::ArrayMove& instr)
782
{
112✔
783
    struct ArrayMoveResolver : public PathResolver {
112✔
784
        ArrayMoveResolver(InstructionApplier* applier, const Instruction::ArrayMove& instr)
112✔
785
            : PathResolver(applier, instr, "ArrayMove")
112✔
786
            , m_instr(instr)
112✔
787
        {
112✔
788
        }
112✔
789
        Status on_list_index(LstBase& list, uint32_t index) override
112✔
790
        {
112✔
791
            if (index >= list.size()) {
112✔
792
                m_applier->bad_transaction_log("ArrayMove from out of bounds (%1 >= %2)", m_instr.index(),
×
793
                                               list.size());
×
794
            }
×
795
            if (m_instr.ndx_2 >= list.size()) {
112✔
796
                m_applier->bad_transaction_log("ArrayMove to out of bounds (%1 >= %2)", m_instr.ndx_2, list.size());
×
797
            }
×
798
            if (index == m_instr.ndx_2) {
112✔
799
                // FIXME: Does this really need to be an error?
800
                m_applier->bad_transaction_log("ArrayMove to same location (%1)", m_instr.index());
×
801
            }
×
802
            if (m_instr.prior_size != list.size()) {
112✔
803
                m_applier->bad_transaction_log("ArrayMove: Invalid prior_size (list size = %1, prior_size = %2)",
×
804
                                               list.size(), m_instr.prior_size);
×
805
            }
×
806
            list.move(index, m_instr.ndx_2);
112✔
807
            return Status::Pending;
112✔
808
        }
112✔
809

56✔
810
    private:
112✔
811
        const Instruction::ArrayMove& m_instr;
112✔
812
    };
112✔
813
    ArrayMoveResolver(this, instr).resolve();
112✔
814
}
112✔
815

816
void InstructionApplier::operator()(const Instruction::ArrayErase& instr)
817
{
4,648✔
818
    struct ArrayEraseResolver : public PathResolver {
4,648✔
819
        ArrayEraseResolver(InstructionApplier* applier, const Instruction::ArrayErase& instr)
4,648✔
820
            : PathResolver(applier, instr, "ArrayErase")
4,648✔
821
            , m_instr(instr)
4,648✔
822
        {
4,648✔
823
        }
4,648✔
824
        Status on_list_index(LstBase& list, uint32_t index) override
4,648✔
825
        {
4,648✔
826
            if (index >= m_instr.prior_size) {
4,648✔
827
                m_applier->bad_transaction_log("ArrayErase: Invalid index (index = %1, prior_size = %2)", index,
×
828
                                               m_instr.prior_size);
×
829
            }
×
830
            if (index >= list.size()) {
4,648✔
831
                m_applier->bad_transaction_log("ArrayErase: Index out of bounds (%1 >= %2)", index, list.size());
×
832
            }
×
833
            if (m_instr.prior_size != list.size()) {
4,648✔
834
                m_applier->bad_transaction_log("ArrayErase: Invalid prior_size (list size = %1, prior_size = %2)",
×
835
                                               list.size(), m_instr.prior_size);
×
836
            }
×
837
            list.remove(index, index + 1);
4,648✔
838
            return Status::Pending;
4,648✔
839
        }
4,648✔
840

2,324✔
841
    private:
4,648✔
842
        const Instruction::ArrayErase& m_instr;
4,648✔
843
    };
4,648✔
844
    ArrayEraseResolver(this, instr).resolve();
4,648✔
845
}
4,648✔
846

847
void InstructionApplier::operator()(const Instruction::Clear& instr)
848
{
220✔
849
    struct ClearResolver : public PathResolver {
220✔
850
        ClearResolver(InstructionApplier* applier, const Instruction::Clear& instr)
220✔
851
            : PathResolver(applier, instr, "Clear")
220✔
852
        {
220✔
853
        }
220✔
854
        void on_list(LstBase& list) override
220✔
855
        {
188✔
856
            list.clear();
156✔
857
        }
156✔
858
        void on_dictionary(Dictionary& dict) override
220✔
859
        {
122✔
860
            dict.clear();
×
861
        }
×
862
        void on_set(SetBase& set) override
220✔
863
        {
154✔
864
            set.clear();
64✔
865
        }
64✔
866
    };
220✔
867
    ClearResolver(this, instr).resolve();
220✔
868
}
220✔
869

870
bool InstructionApplier::allows_null_links(const Instruction::PathInstruction& instr,
871
                                           const std::string_view& instr_name)
872
{
40✔
873
    struct AllowsNullsResolver : public PathResolver {
40✔
874
        AllowsNullsResolver(InstructionApplier* applier, const Instruction::PathInstruction& instr,
40✔
875
                            const std::string_view& instr_name)
40✔
876
            : PathResolver(applier, instr, instr_name)
40✔
877
            , m_allows_nulls(false)
40✔
878
        {
40✔
879
        }
40✔
880
        Status on_list_index(LstBase&, uint32_t) override
40✔
881
        {
20✔
882
            return Status::Pending;
×
883
        }
×
884
        void on_list(LstBase&) override {}
20✔
885
        void on_set(SetBase&) override {}
20✔
886
        void on_dictionary(Dictionary&) override
40✔
887
        {
20✔
888
            m_allows_nulls = true;
×
889
        }
×
890
        Status on_dictionary_key(Dictionary&, Mixed) override
40✔
891
        {
40✔
892
            m_allows_nulls = true;
40✔
893
            return Status::Pending;
40✔
894
        }
40✔
895
        void on_property(Obj&, ColKey) override
40✔
896
        {
20✔
897
            m_allows_nulls = true;
×
898
        }
×
899
        bool allows_nulls()
40✔
900
        {
40✔
901
            resolve();
40✔
902
            return m_allows_nulls;
40✔
903
        }
40✔
904

20✔
905
    private:
40✔
906
        bool m_allows_nulls;
40✔
907
    };
40✔
908
    return AllowsNullsResolver(this, instr, instr_name).allows_nulls();
40✔
909
}
40✔
910

911
std::string InstructionApplier::to_string(const Instruction::PathInstruction& instr) const
912
{
×
913
    REALM_ASSERT(m_log);
×
914
    std::stringstream ss;
×
915
    m_log->print_path(ss, instr.table, instr.object, instr.field, &instr.path);
×
916
    return ss.str();
×
917
}
×
918

919
bool InstructionApplier::check_links_exist(const Instruction::Payload& payload)
920
{
23,332✔
921
    bool valid_payload = true;
23,332✔
922
    using Type = Instruction::Payload::Type;
23,332✔
923
    if (payload.type == Type::Link) {
23,332✔
924
        StringData class_name = get_string(payload.data.link.target_table);
800✔
925
        Group::TableNameBuffer buffer;
800✔
926
        StringData target_table_name = Group::class_name_to_table_name(class_name, buffer);
800✔
927
        TableRef target_table = m_transaction.get_table(target_table_name);
800✔
928
        if (!target_table) {
800✔
929
            bad_transaction_log("Link with invalid target table '%1'", target_table_name);
×
930
        }
×
931
        if (target_table->is_embedded()) {
800✔
932
            bad_transaction_log("Link to embedded table '%1'", target_table_name);
×
933
        }
×
934
        Mixed linked_pk =
800✔
935
            mpark::visit(util::overload{[&](mpark::monostate) {
412✔
936
                                            return Mixed{}; // the link exists and the pk is null
24✔
937
                                        },
24✔
938
                                        [&](int64_t pk) {
780✔
939
                                            return Mixed{pk};
760✔
940
                                        },
760✔
941
                                        [&](InternString interned_pk) {
400✔
942
                                            return Mixed{get_string(interned_pk)};
×
943
                                        },
×
944
                                        [&](GlobalKey) {
400✔
945
                                            bad_transaction_log(
×
946
                                                "Unexpected link to embedded object while validating a primary key");
×
947
                                            return Mixed{}; // appease the compiler; visitors must have a single
×
948
                                                            // return type
949
                                        },
×
950
                                        [&](ObjectId pk) {
408✔
951
                                            return Mixed{pk};
16✔
952
                                        },
16✔
953
                                        [&](UUID pk) {
400✔
954
                                            return Mixed{pk};
×
955
                                        }},
×
956
                         payload.data.link.target);
800✔
957

400✔
958
        if (!target_table->find_primary_key(linked_pk)) {
800✔
959
            valid_payload = false;
168✔
960
        }
168✔
961
    }
800✔
962
    return valid_payload;
23,332✔
963
}
23,332✔
964

965
void InstructionApplier::operator()(const Instruction::SetInsert& instr)
966
{
1,744✔
967
    struct SetInsertResolver : public PathResolver {
1,744✔
968
        SetInsertResolver(InstructionApplier* applier, const Instruction::SetInsert& instr)
1,744✔
969
            : PathResolver(applier, instr, "SetInsert")
1,744✔
970
            , m_instr(instr)
1,744✔
971
        {
1,744✔
972
        }
1,744✔
973
        void on_set(SetBase& set) override
1,744✔
974
        {
1,744✔
975
            auto col = set.get_col_key();
1,744✔
976
            auto data_type = DataType(col.get_type());
1,744✔
977
            auto table = set.get_table();
1,744✔
978
            auto table_name = table->get_name();
1,744✔
979
            auto field_name = table->get_column_name(col);
1,744✔
980

872✔
981
            auto inserter = util::overload{
1,744✔
982
                [&](const ObjLink& link) {
968✔
983
                    if (data_type == type_TypedLink) {
192✔
984
                        REALM_ASSERT(dynamic_cast<Set<ObjLink>*>(&set));
×
985
                        auto& link_set = static_cast<Set<ObjLink>&>(set);
×
986
                        link_set.insert(link);
×
987
                    }
×
988
                    else if (data_type == type_Mixed) {
192✔
989
                        REALM_ASSERT(dynamic_cast<Set<Mixed>*>(&set));
88✔
990
                        auto& mixed_set = static_cast<Set<Mixed>&>(set);
88✔
991
                        mixed_set.insert(link);
88✔
992
                    }
88✔
993
                    else if (data_type == type_Link) {
104✔
994
                        REALM_ASSERT(dynamic_cast<Set<ObjKey>*>(&set));
104✔
995
                        auto& link_set = static_cast<Set<ObjKey>&>(set);
104✔
996
                        // Validate the target.
52✔
997
                        auto target_table = table->get_link_target(col);
104✔
998
                        if (target_table->get_key() != link.get_table_key()) {
104✔
999
                            m_applier->bad_transaction_log(
×
1000
                                "SetInsert: Target table mismatch (expected '%1', got '%2')",
×
1001
                                target_table->get_name(), table_name);
×
1002
                        }
×
1003
                        link_set.insert(link.get_obj_key());
104✔
1004
                    }
104✔
1005
                    else {
×
1006
                        m_applier->bad_transaction_log(
×
1007
                            "SetInsert: Type mismatch in set at '%2.%1' (expected link type, was %3)", field_name,
×
1008
                            table_name, data_type);
×
1009
                    }
×
1010
                },
192✔
1011
                [&](Mixed value) {
1,648✔
1012
                    if (value.is_null() && !col.is_nullable()) {
1,552✔
1013
                        m_applier->bad_transaction_log("SetInsert: NULL in non-nullable set '%2.%1'", field_name,
×
1014
                                                       table_name);
×
1015
                    }
×
1016

776✔
1017
                    if (data_type == type_Mixed || value.is_null() || value.get_type() == data_type) {
1,552✔
1018
                        set.insert_any(value);
1,552✔
1019
                    }
1,552✔
1020
                    else {
×
1021
                        m_applier->bad_transaction_log(
×
1022
                            "SetInsert: Type mismatch in set at '%2.%1' (expected %3, got %4)", field_name,
×
1023
                            table_name, data_type, value.get_type());
×
1024
                    }
×
1025
                },
1,552✔
1026
                [&](const Instruction::Payload::ObjectValue&) {
872✔
1027
                    m_applier->bad_transaction_log("SetInsert: Sets of embedded objects are not supported.");
×
1028
                },
×
1029
                [&](const Instruction::Payload::Dictionary&) {
1,744✔
1030
                    m_applier->bad_transaction_log("SetInsert: Sets of dictionaries are not supported.");
1,744✔
1031
                },
1,744✔
1032
                [&](const Instruction::Payload::Erased&) {
872✔
1033
                    m_applier->bad_transaction_log("SetInsert: Dictionary erase payload in SetInsert");
×
1034
                },
×
1035
            };
1,744✔
1036

872✔
1037
            m_applier->visit_payload(m_instr.value, inserter);
1,744✔
1038
        }
1,744✔
1039

872✔
1040
    private:
1,744✔
1041
        const Instruction::SetInsert& m_instr;
1,744✔
1042
    };
1,744✔
1043
    SetInsertResolver(this, instr).resolve();
1,744✔
1044
}
1,744✔
1045

1046
void InstructionApplier::operator()(const Instruction::SetErase& instr)
1047
{
480✔
1048
    struct SetEraseResolver : public PathResolver {
480✔
1049
        SetEraseResolver(InstructionApplier* applier, const Instruction::SetErase& instr)
480✔
1050
            : PathResolver(applier, instr, "SetErase")
480✔
1051
            , m_instr(instr)
480✔
1052
        {
480✔
1053
        }
480✔
1054
        void on_set(SetBase& set) override
480✔
1055
        {
480✔
1056
            auto col = set.get_col_key();
480✔
1057
            auto data_type = DataType(col.get_type());
480✔
1058
            auto table = set.get_table();
480✔
1059
            auto table_name = table->get_name();
480✔
1060
            auto field_name = table->get_column_name(col);
480✔
1061

240✔
1062
            auto inserter = util::overload{
480✔
1063
                [&](const ObjLink& link) {
324✔
1064
                    if (data_type == type_TypedLink) {
168✔
1065
                        REALM_ASSERT(dynamic_cast<Set<ObjLink>*>(&set));
×
1066
                        auto& link_set = static_cast<Set<ObjLink>&>(set);
×
1067
                        link_set.erase(link);
×
1068
                    }
×
1069
                    else if (data_type == type_Mixed) {
168✔
1070
                        REALM_ASSERT(dynamic_cast<Set<Mixed>*>(&set));
80✔
1071
                        auto& mixed_set = static_cast<Set<Mixed>&>(set);
80✔
1072
                        mixed_set.erase(link);
80✔
1073
                    }
80✔
1074
                    else if (data_type == type_Link) {
88✔
1075
                        REALM_ASSERT(dynamic_cast<Set<ObjKey>*>(&set));
88✔
1076
                        auto& link_set = static_cast<Set<ObjKey>&>(set);
88✔
1077
                        // Validate the target.
44✔
1078
                        auto target_table = table->get_link_target(col);
88✔
1079
                        if (target_table->get_key() != link.get_table_key()) {
88✔
1080
                            m_applier->bad_transaction_log(
×
1081
                                "SetErase: Target table mismatch (expected '%1', got '%2')", target_table->get_name(),
×
1082
                                table_name);
×
1083
                        }
×
1084
                        link_set.erase(link.get_obj_key());
88✔
1085
                    }
88✔
1086
                    else {
×
1087
                        m_applier->bad_transaction_log(
×
1088
                            "SetErase: Type mismatch in set at '%2.%1' (expected link type, was %3)", field_name,
×
1089
                            table_name, data_type);
×
1090
                    }
×
1091
                },
168✔
1092
                [&](Mixed value) {
396✔
1093
                    if (value.is_null() && !col.is_nullable()) {
312!
1094
                        m_applier->bad_transaction_log("SetErase: NULL in non-nullable set '%2.%1'", field_name,
×
1095
                                                       table_name);
×
1096
                    }
×
1097

156✔
1098
                    if (data_type == type_Mixed || value.get_type() == data_type) {
312✔
1099
                        set.erase_any(value);
312✔
1100
                    }
312✔
1101
                    else {
×
1102
                        m_applier->bad_transaction_log(
×
1103
                            "SetErase: Type mismatch in set at '%2.%1' (expected %3, got %4)", field_name, table_name,
×
1104
                            data_type, value.get_type());
×
1105
                    }
×
1106
                },
312✔
1107
                [&](const Instruction::Payload::ObjectValue&) {
240✔
1108
                    m_applier->bad_transaction_log("SetErase: Sets of embedded objects are not supported.");
×
1109
                },
×
1110
                [&](const Instruction::Payload::Dictionary&) {
480✔
1111
                    m_applier->bad_transaction_log("SetErase: Sets of dictionaries are not supported.");
480✔
1112
                },
480✔
1113
                [&](const Instruction::Payload::Erased&) {
240✔
1114
                    m_applier->bad_transaction_log("SetErase: Dictionary erase payload in SetErase");
×
1115
                },
×
1116
            };
480✔
1117

240✔
1118
            m_applier->visit_payload(m_instr.value, inserter);
480✔
1119
        }
480✔
1120

240✔
1121
    private:
480✔
1122
        const Instruction::SetErase& m_instr;
480✔
1123
    };
480✔
1124
    SetEraseResolver(this, instr).resolve();
480✔
1125
}
480✔
1126

1127
StringData InstructionApplier::get_table_name(const Instruction::TableInstruction& instr,
1128
                                              const std::string_view& name)
1129
{
316,610✔
1130
    if (auto class_name = m_log->try_get_string(instr.table)) {
316,618✔
1131
        return Group::class_name_to_table_name(*class_name, m_table_name_buffer);
316,616✔
1132
    }
316,616✔
1133
    else {
2,147,483,649✔
1134
        bad_transaction_log("Corrupt table name in %1 instruction", name);
2,147,483,649✔
1135
    }
2,147,483,649✔
1136
}
316,610✔
1137

1138
TableRef InstructionApplier::get_table(const Instruction::TableInstruction& instr, const std::string_view& name)
1139
{
652,818✔
1140
    if (instr.table == m_last_table_name) {
652,818✔
1141
        return m_last_table;
352,748✔
1142
    }
352,748✔
1143
    else {
300,070✔
1144
        auto table_name = get_table_name(instr, name);
300,070✔
1145
        TableRef table = m_transaction.get_table(table_name);
300,070✔
1146
        if (!table) {
300,070✔
1147
            bad_transaction_log("%1: Table '%2' does not exist", name, table_name);
×
1148
        }
×
1149
        m_last_table = table;
300,070✔
1150
        m_last_table_name = instr.table;
300,070✔
1151
        m_last_object_key.reset();
300,070✔
1152
        m_last_object.reset();
300,070✔
1153
        m_last_field_name = InternString{};
300,070✔
1154
        m_last_field = ColKey{};
300,070✔
1155
        return table;
300,070✔
1156
    }
300,070✔
1157
}
652,818✔
1158

1159
util::Optional<Obj> InstructionApplier::get_top_object(const Instruction::ObjectInstruction& instr,
1160
                                                       const std::string_view& name)
1161
{
739,828✔
1162
    if (m_last_table_name == instr.table && m_last_object_key && m_last_object &&
739,828✔
1163
        *m_last_object_key == instr.object) {
660,908✔
1164
        // We have already found the object, reuse it.
116,698✔
1165
        return *m_last_object;
247,000✔
1166
    }
247,000✔
1167
    else {
492,828✔
1168
        TableRef table = get_table(instr, name);
492,828✔
1169
        ObjKey key = get_object_key(*table, instr.object, name);
492,828✔
1170
        if (!key) {
492,828✔
1171
            return util::none;
×
1172
        }
×
1173
        if (!table->is_valid(key)) {
492,828✔
1174
            // Check if the object is deleted or is a tombstone.
7,428✔
1175
            return util::none;
14,458✔
1176
        }
14,458✔
1177

241,298✔
1178
        Obj obj = table->get_object(key);
478,370✔
1179
        m_last_object_key = instr.object;
478,370✔
1180
        m_last_object = obj;
478,370✔
1181
        return obj;
478,370✔
1182
    }
478,370✔
1183
}
739,828✔
1184

1185
std::unique_ptr<LstBase> InstructionApplier::get_list_from_path(Obj& obj, ColKey col)
1186
{
189,686✔
1187
    // For link columns, `Obj::get_listbase_ptr()` always returns an instance whose concrete type is
95,700✔
1188
    // `LnkLst`, which uses condensed indexes. However, we are interested in using non-condensed
95,700✔
1189
    // indexes, so we need to manually construct a `Lst<ObjKey>` instead for lists of non-embedded
95,700✔
1190
    // links.
95,700✔
1191
    REALM_ASSERT(col.is_list());
189,686✔
1192
    std::unique_ptr<LstBase> list;
189,686✔
1193
    if (col.get_type() == col_type_Link || col.get_type() == col_type_LinkList) {
189,686✔
1194
        auto table = obj.get_table();
13,944✔
1195
        if (!table->get_link_target(col)->is_embedded()) {
13,944✔
1196
            list = obj.get_list_ptr<ObjKey>(col);
1,760✔
1197
        }
1,760✔
1198
        else {
12,184✔
1199
            list = obj.get_listbase_ptr(col);
12,184✔
1200
        }
12,184✔
1201
    }
13,944✔
1202
    else {
175,742✔
1203
        list = obj.get_listbase_ptr(col);
175,742✔
1204
    }
175,742✔
1205
    return list;
189,686✔
1206
}
189,686✔
1207

1208
InstructionApplier::PathResolver::PathResolver(InstructionApplier* applier, const Instruction::PathInstruction& instr,
1209
                                               const std::string_view& instr_name)
1210
    : m_applier(applier)
1211
    , m_path_instr(instr)
1212
    , m_instr_name(instr_name)
1213
{
682,258✔
1214
}
682,258✔
1215

1216
InstructionApplier::PathResolver::~PathResolver()
1217
{
682,268✔
1218
    on_finish();
682,268✔
1219
}
682,268✔
1220

1221
void InstructionApplier::PathResolver::on_property(Obj&, ColKey)
1222
{
×
1223
    m_applier->bad_transaction_log(util::format("Invalid path for %1 (object, column)", m_instr_name));
×
1224
}
×
1225

1226
void InstructionApplier::PathResolver::on_list(LstBase&)
1227
{
×
1228
    m_applier->bad_transaction_log(util::format("Invalid path for %1 (list)", m_instr_name));
×
1229
}
×
1230

1231
InstructionApplier::PathResolver::Status InstructionApplier::PathResolver::on_list_index(LstBase&, uint32_t)
1232
{
×
1233
    m_applier->bad_transaction_log(util::format("Invalid path for %1 (list, index)", m_instr_name));
×
1234
    return Status::DidNotResolve;
×
1235
}
×
1236

1237
void InstructionApplier::PathResolver::on_dictionary(Dictionary&)
1238
{
×
1239
    m_applier->bad_transaction_log(util::format("Invalid path for %1 (dictionary, key)", m_instr_name));
×
1240
}
×
1241

1242
InstructionApplier::PathResolver::Status InstructionApplier::PathResolver::on_dictionary_key(Dictionary&, Mixed)
1243
{
×
1244
    m_applier->bad_transaction_log(util::format("Invalid path for %1 (dictionary, key)", m_instr_name));
×
1245
    return Status::DidNotResolve;
×
1246
}
×
1247

1248
void InstructionApplier::PathResolver::on_set(SetBase&)
1249
{
×
1250
    m_applier->bad_transaction_log(util::format("Invalid path for %1 (set)", m_instr_name));
×
1251
}
×
1252

1253
void InstructionApplier::PathResolver::on_error(const std::string& err_msg)
1254
{
×
1255
    m_applier->bad_transaction_log(err_msg);
×
1256
}
×
1257

1258
void InstructionApplier::PathResolver::on_column_advance(ColKey col)
1259
{
669,370✔
1260
    m_applier->m_last_field = col;
669,370✔
1261
}
669,370✔
1262

1263
void InstructionApplier::PathResolver::on_dict_key_advance(StringData) {}
2,092✔
1264

1265
InstructionApplier::PathResolver::Status InstructionApplier::PathResolver::on_list_index_advance(uint32_t)
1266
{
7,452✔
1267
    return Status::Pending;
7,452✔
1268
}
7,452✔
1269

1270
InstructionApplier::PathResolver::Status InstructionApplier::PathResolver::on_null_link_advance(StringData,
1271
                                                                                                StringData)
1272
{
×
1273
    return Status::Pending;
×
1274
}
×
1275

1276
InstructionApplier::PathResolver::Status InstructionApplier::PathResolver::on_begin(const util::Optional<Obj>&)
1277
{
656,926✔
1278
    m_applier->m_current_path = m_path_instr.path;
656,926✔
1279
    m_applier->m_last_field_name = m_path_instr.field;
656,926✔
1280
    return Status::Pending;
656,926✔
1281
}
656,926✔
1282

1283
void InstructionApplier::PathResolver::on_finish()
1284
{
682,268✔
1285
    m_applier->m_current_path.reset();
682,268✔
1286
    m_applier->m_last_field_name = InternString{};
682,268✔
1287
    m_applier->m_last_field = ColKey{};
682,268✔
1288
}
682,268✔
1289

1290
StringData InstructionApplier::PathResolver::get_string(InternString interned)
1291
{
706,694✔
1292
    return m_applier->get_string(interned);
706,694✔
1293
}
706,694✔
1294

1295
InstructionApplier::PathResolver::Status InstructionApplier::PathResolver::resolve()
1296
{
682,252✔
1297
    util::Optional<Obj> obj = m_applier->get_top_object(m_path_instr, m_instr_name);
682,252✔
1298
    Status begin_status = on_begin(obj);
682,252✔
1299
    if (begin_status != Status::Pending) {
682,252✔
1300
        return begin_status;
240✔
1301
    }
240✔
1302
    if (!obj) {
682,012✔
1303
        m_applier->bad_transaction_log("%1: No such object: '%2' in class '%3'", m_instr_name,
×
1304
                                       format_pk(m_applier->m_log->get_key(m_path_instr.object)),
×
1305
                                       get_string(m_path_instr.table));
×
1306
    }
×
1307

335,904✔
1308
    m_it_begin = m_path_instr.path.begin();
682,012✔
1309
    m_it_end = m_path_instr.path.end();
682,012✔
1310
    Status status = resolve_field(*obj, m_path_instr.field);
682,012✔
1311
    return status == Status::Pending ? Status::Success : status;
679,698✔
1312
}
682,012✔
1313

1314
InstructionApplier::PathResolver::Status InstructionApplier::PathResolver::resolve_field(Obj& obj, InternString field)
1315
{
697,882✔
1316
    auto field_name = get_string(field);
697,882✔
1317
    ColKey col = obj.get_table()->get_column_key(field_name);
697,882✔
1318
    if (!col) {
697,882✔
1319
        on_error(util::format("%1: No such field: '%2' in class '%3'", m_instr_name, field_name,
×
1320
                              obj.get_table()->get_name()));
×
1321
        return Status::DidNotResolve;
×
1322
    }
×
1323
    on_column_advance(col);
697,882✔
1324

343,842✔
1325
    if (m_it_begin == m_it_end) {
697,882✔
1326
        if (col.is_list()) {
496,642✔
1327
            auto list = obj.get_listbase_ptr(col);
192✔
1328
            on_list(*list);
192✔
1329
        }
192✔
1330
        else if (col.is_dictionary()) {
496,450✔
1331
            auto dict = obj.get_dictionary(col);
×
1332
            on_dictionary(dict);
×
1333
        }
×
1334
        else if (col.is_set()) {
496,450✔
1335
            SetBasePtr set;
3,628✔
1336
            if (col.get_type() == col_type_Link) {
3,628✔
1337
                // We are interested in using non-condensed indexes - as for Lists below
188✔
1338
                set = obj.get_set_ptr<ObjKey>(col);
376✔
1339
            }
376✔
1340
            else {
3,252✔
1341
                set = obj.get_setbase_ptr(col);
3,252✔
1342
            }
3,252✔
1343
            on_set(*set);
3,628✔
1344
        }
3,628✔
1345
        else {
492,822✔
1346
            on_property(obj, col);
492,822✔
1347
        }
492,822✔
1348
        return Status::Pending;
496,642✔
1349
    }
496,642✔
1350

101,480✔
1351
    if (col.is_list()) {
201,240✔
1352
        if (auto pindex = mpark::get_if<uint32_t>(&*m_it_begin)) {
187,878✔
1353
            std::unique_ptr<LstBase> list = InstructionApplier::get_list_from_path(obj, col);
187,878✔
1354
            ++m_it_begin;
187,878✔
1355
            return resolve_list_element(*list, *pindex);
187,878✔
1356
        }
187,878✔
1357
        on_error(util::format("%1: List index is not an integer on field '%2' in class '%3'", m_instr_name,
×
1358
                              field_name, obj.get_table()->get_name()));
×
1359
    }
×
1360
    else if (col.is_dictionary()) {
13,362✔
1361
        if (auto pkey = mpark::get_if<InternString>(&*m_it_begin)) {
8,808✔
1362
            auto dict = obj.get_dictionary(col);
8,808✔
1363
            ++m_it_begin;
8,808✔
1364
            return resolve_dictionary_element(dict, *pkey);
8,808✔
1365
        }
8,808✔
1366
        on_error(util::format("%1: Dictionary key is not a string on field '%2' in class '%3'", m_instr_name,
×
1367
                              field_name, obj.get_table()->get_name()));
×
1368
    }
×
1369
    else if (col.get_type() == col_type_Link) {
4,568✔
1370
        auto target = obj.get_table()->get_link_target(col);
4,568✔
1371
        if (!target->is_embedded()) {
4,568✔
1372
            on_error(util::format("%1: Reference through non-embedded link in field '%2' in class '%3'", m_instr_name,
×
1373
                                  field_name, obj.get_table()->get_name()));
×
1374
        }
×
1375
        else if (obj.is_null(col)) {
4,568✔
1376
            Status null_status =
132✔
1377
                on_null_link_advance(obj.get_table()->get_name(), obj.get_table()->get_column_name(col));
132✔
1378
            if (null_status != Status::Pending) {
132✔
1379
                return null_status;
132✔
1380
            }
132✔
1381
            on_error(util::format("%1: Reference through NULL embedded link in field '%2' in class '%3'",
×
1382
                                  m_instr_name, field_name, obj.get_table()->get_name()));
×
1383
        }
×
1384
        else if (auto pfield = mpark::get_if<InternString>(&*m_it_begin)) {
4,436✔
1385
            auto embedded_object = obj.get_linked_object(col);
4,436✔
1386
            ++m_it_begin;
4,436✔
1387
            return resolve_field(embedded_object, *pfield);
4,436✔
1388
        }
4,436✔
1389
        else {
×
1390
            on_error(util::format("%1: Embedded object field reference is not a string", m_instr_name));
×
1391
        }
×
1392
    }
4,568✔
1393
    else {
4,294,967,294✔
1394
        on_error(util::format("%1: Resolving path through unstructured field '%3.%2' of type %4", m_instr_name,
4,294,967,294✔
1395
                              field_name, obj.get_table()->get_name(), col.get_type()));
4,294,967,294✔
1396
    }
4,294,967,294✔
1397
    return Status::DidNotResolve;
4,294,967,294✔
1398
}
201,240✔
1399

1400
InstructionApplier::PathResolver::Status InstructionApplier::PathResolver::resolve_list_element(LstBase& list,
1401
                                                                                                uint32_t index)
1402
{
187,878✔
1403
    if (m_it_begin == m_it_end) {
187,878✔
1404
        return on_list_index(list, index);
177,706✔
1405
    }
177,706✔
1406

5,086✔
1407
    auto col = list.get_col_key();
10,172✔
1408
    auto field_name = list.get_table()->get_column_name(col);
10,172✔
1409

5,086✔
1410
    if (col.get_type() == col_type_LinkList) {
10,172✔
1411
        auto target = list.get_table()->get_link_target(col);
10,172✔
1412
        if (!target->is_embedded()) {
10,172✔
1413
            on_error(util::format("%1: Reference through non-embedded link at '%3.%2[%4]'", m_instr_name, field_name,
×
1414
                                  list.get_table()->get_name(), index));
×
1415
            return Status::DidNotResolve;
×
1416
        }
×
1417

5,086✔
1418
        Status list_status = on_list_index_advance(index);
10,172✔
1419
        if (list_status != Status::Pending) {
10,172✔
1420
            return list_status;
2,720✔
1421
        }
2,720✔
1422

3,726✔
1423
        REALM_ASSERT(dynamic_cast<LnkLst*>(&list));
7,452✔
1424
        auto& link_list = static_cast<LnkLst&>(list);
7,452✔
1425
        if (index >= link_list.size()) {
7,452✔
1426
            on_error(util::format("%1: Out-of-bounds index through list at '%3.%2[%4]'", m_instr_name, field_name,
×
1427
                                  list.get_table()->get_name(), index));
×
1428
        }
×
1429
        else if (auto pfield = mpark::get_if<InternString>(&*m_it_begin)) {
7,452✔
1430
            auto embedded_object = link_list.get_object(index);
7,452✔
1431
            ++m_it_begin;
7,452✔
1432
            return resolve_field(embedded_object, *pfield);
7,452✔
1433
        }
7,452✔
1434
        on_error(util::format("%1: Embedded object field reference is not a string", m_instr_name));
×
1435
    }
×
1436
    else {
×
1437
        on_error(util::format(
×
1438
            "%1: Resolving path through unstructured list element on '%3.%2', which is a list of type '%4'",
×
1439
            m_instr_name, field_name, list.get_table()->get_name(), col.get_type()));
×
1440
    }
×
1441
    return Status::DidNotResolve;
5,086✔
1442
}
10,172✔
1443

1444
InstructionApplier::PathResolver::Status
1445
InstructionApplier::PathResolver::resolve_dictionary_element(Dictionary& dict, InternString key)
1446
{
8,808✔
1447
    StringData string_key = get_string(key);
8,808✔
1448
    if (m_it_begin == m_it_end) {
8,808✔
1449
        return on_dictionary_key(dict, Mixed{string_key});
4,732✔
1450
    }
4,732✔
1451

2,038✔
1452
    on_dict_key_advance(string_key);
4,076✔
1453

2,038✔
1454
    auto col = dict.get_col_key();
4,076✔
1455
    auto table = dict.get_table();
4,076✔
1456
    auto field_name = table->get_column_name(col);
4,076✔
1457

2,038✔
1458
    if (col.get_type() == col_type_Link) {
4,076✔
1459
        auto target = dict.get_target_table();
4,076✔
1460
        if (!target->is_embedded()) {
4,076✔
1461
            on_error(util::format("%1: Reference through non-embedded link at '%3.%2[%4]'", m_instr_name, field_name,
×
1462
                                  table->get_name(), string_key));
×
1463
            return Status::DidNotResolve;
×
1464
        }
×
1465

2,038✔
1466
        auto embedded_object = dict.get_object(string_key);
4,076✔
1467
        if (!embedded_object) {
4,076✔
1468
            Status null_link_status = on_null_link_advance(table->get_name(), string_key);
80✔
1469
            if (null_link_status != Status::Pending) {
80✔
1470
                return null_link_status;
80✔
1471
            }
80✔
1472
            on_error(util::format("%1: Unmatched key through dictionary at '%3.%2[%4]'", m_instr_name, field_name,
×
1473
                                  table->get_name(), string_key));
×
1474
        }
×
1475
        else if (auto pfield = mpark::get_if<InternString>(&*m_it_begin)) {
3,996✔
1476
            ++m_it_begin;
3,996✔
1477
            return resolve_field(embedded_object, *pfield);
3,996✔
1478
        }
3,996✔
1479
        else {
×
1480
            on_error(util::format("%1: Embedded object field reference is not a string", m_instr_name));
×
1481
        }
×
1482
    }
4,076✔
1483
    else {
×
1484
        on_error(
×
1485
            util::format("%1: Resolving path through non link element on '%3.%2', which is a dictionary of type '%4'",
×
1486
                         m_instr_name, field_name, table->get_name(), col.get_type()));
×
1487
    }
×
1488
    return Status::DidNotResolve;
2,038✔
1489
}
4,076✔
1490

1491

1492
ObjKey InstructionApplier::get_object_key(Table& table, const Instruction::PrimaryKey& primary_key,
1493
                                          const std::string_view& name) const
1494
{
494,982✔
1495
    StringData table_name = table.get_name();
494,982✔
1496
    ColKey pk_col = table.get_primary_key_column();
494,982✔
1497
    StringData pk_name = "";
494,982✔
1498
    DataType pk_type;
494,982✔
1499
    if (pk_col) {
494,982✔
1500
        pk_name = table.get_column_name(pk_col);
494,972✔
1501
        pk_type = table.get_column_type(pk_col);
494,972✔
1502
    }
494,972✔
1503
    return mpark::visit(
494,982✔
1504
        util::overload{
494,982✔
1505
            [&](mpark::monostate) {
249,876✔
1506
                if (!pk_col) {
24✔
1507
                    bad_transaction_log(
×
1508
                        "%1 instruction with NULL primary key, but table '%2' does not have a primary key column",
×
1509
                        name, table_name);
×
1510
                }
×
1511
                if (!table.is_nullable(pk_col)) {
24✔
1512
                    bad_transaction_log("%1 instruction with NULL primary key, but column '%2.%3' is not nullable",
×
1513
                                        name, table_name, pk_name);
×
1514
                }
×
1515

12✔
1516
                ObjKey key = table.get_objkey_from_primary_key(realm::util::none);
24✔
1517
                return key;
24✔
1518
            },
24✔
1519
            [&](int64_t pk) {
483,176✔
1520
                if (!pk_col) {
471,232✔
1521
                    bad_transaction_log("%1 instruction with integer primary key (%2), but table '%3' does not have "
×
1522
                                        "a primary key column",
×
1523
                                        name, pk, table_name);
×
1524
                }
×
1525
                if (pk_type != type_Int) {
471,232✔
1526
                    bad_transaction_log(
×
1527
                        "%1 instruction with integer primary key (%2), but '%3.%4' has primary keys of type '%5'",
×
1528
                        name, pk, table_name, pk_name, pk_type);
×
1529
                }
×
1530
                ObjKey key = table.get_objkey_from_primary_key(pk);
471,232✔
1531
                return key;
471,232✔
1532
            },
471,232✔
1533
            [&](InternString interned_pk) {
259,038✔
1534
                auto pk = get_string(interned_pk);
18,490✔
1535
                if (!pk_col) {
18,490✔
1536
                    bad_transaction_log("%1 instruction with string primary key (\"%2\"), but table '%3' does not "
×
1537
                                        "have a primary key column",
×
1538
                                        name, pk, table_name);
×
1539
                }
×
1540
                if (pk_type != type_String) {
18,490✔
1541
                    bad_transaction_log(
×
1542
                        "%1 instruction with string primary key (\"%2\"), but '%3.%4' has primary keys of type '%5'",
×
1543
                        name, pk, table_name, pk_name, pk_type);
×
1544
                }
×
1545
                ObjKey key = table.get_objkey_from_primary_key(pk);
18,490✔
1546
                return key;
18,490✔
1547
            },
18,490✔
1548
            [&](GlobalKey id) {
249,866✔
1549
                if (pk_col) {
4✔
1550
                    bad_transaction_log(
×
1551
                        "%1 instruction without primary key, but table '%2' has a primary key column of type %3",
×
1552
                        name, table_name, pk_type);
×
1553
                }
×
1554
                ObjKey key = table.get_objkey_from_global_key(id);
4✔
1555
                return key;
4✔
1556
            },
4✔
1557
            [&](ObjectId pk) {
252,464✔
1558
                if (!pk_col) {
5,200✔
1559
                    bad_transaction_log("%1 instruction with ObjectId primary key (\"%2\"), but table '%3' does not "
×
1560
                                        "have a primary key column",
×
1561
                                        name, pk, table_name);
×
1562
                }
×
1563
                if (pk_type != type_ObjectId) {
5,200✔
1564
                    bad_transaction_log(
×
1565
                        "%1 instruction with ObjectId primary key (%2), but '%3.%4' has primary keys of type '%5'",
×
1566
                        name, pk, table_name, pk_name, pk_type);
×
1567
                }
×
1568
                ObjKey key = table.get_objkey_from_primary_key(pk);
5,200✔
1569
                return key;
5,200✔
1570
            },
5,200✔
1571
            [&](UUID pk) {
249,874✔
1572
                if (!pk_col) {
20✔
1573
                    bad_transaction_log("%1 instruction with UUID primary key (\"%2\"), but table '%3' does not "
×
1574
                                        "have a primary key column",
×
1575
                                        name, pk, table_name);
×
1576
                }
×
1577
                if (pk_type != type_UUID) {
20✔
1578
                    bad_transaction_log(
×
1579
                        "%1 instruction with UUID primary key (%2), but '%3.%4' has primary keys of type '%5'", name,
×
1580
                        pk, table_name, pk_name, pk_type);
×
1581
                }
×
1582
                ObjKey key = table.get_objkey_from_primary_key(pk);
20✔
1583
                return key;
20✔
1584
            }},
20✔
1585
        primary_key);
494,982✔
1586
}
494,982✔
1587

1588

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