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

realm / realm-core / 1706

25 Sep 2023 06:02PM UTC coverage: 91.215% (+0.02%) from 91.198%
1706

push

Evergreen

web-flow
Fixed double delete in CAPITimer and add documentation for platform networking CAPI integration (#6994)

* Fixed double delete in CAPITimer and added individual functions for handling callbacks.
* Added realm_sync_socket_write_complete() fcn and other updates from review
* Don't call handler functions more than once, esp for timers; updated documentation
* Updated some comments
* Updated changelog after release

95864 of 175718 branches covered (0.0%)

20 of 33 new or added lines in 2 files covered. (60.61%)

82 existing lines in 11 files now uncovered.

232491 of 254882 relevant lines covered (91.22%)

6729880.87 hits per line

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

70.42
/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
{
773,446✔
66
    auto string = m_log->try_get_intern_string(str);
773,446✔
67
    if (REALM_UNLIKELY(!string))
773,446✔
68
        bad_transaction_log("string read fails");
377,254✔
69
    return m_log->get_string(*string);
773,446✔
70
}
773,446✔
71

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

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

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

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

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

6,424✔
121
    auto add_table = util::overload{
13,012✔
122
        [&](const Instruction::AddTable::TopLevelTable& spec) {
12,902✔
123
            auto table_type = (spec.is_asymmetric ? Table::Type::TopLevelAsymmetric : Table::Type::TopLevel);
12,780✔
124
            if (spec.pk_type == Instruction::Payload::Type::GlobalKey) {
12,792✔
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,784✔
129
                if (!is_valid_key_type(spec.pk_type)) {
12,784✔
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,784✔
134
                StringData pk_field = get_string(spec.pk_field);
12,784✔
135
                bool nullable = spec.pk_nullable;
12,784✔
136

6,310✔
137
                log("group.get_or_add_table_with_primary_key(group, \"%1\", %2, \"%3\", %4, %5);", table_name,
12,784✔
138
                    pk_type, pk_field, nullable, table_type);
12,784✔
139
                if (!m_transaction.get_or_add_table_with_primary_key(table_name, pk_type, pk_field, nullable,
12,784✔
140
                                                                     table_type)) {
6,310✔
141
                    bad_transaction_log("AddTable: The existing table '%1' has different properties", table_name);
×
142
                }
×
143
            }
12,784✔
144
        },
12,792✔
145
        [&](const Instruction::AddTable::EmbeddedTable&) {
6,534✔
146
            if (TableRef table = m_transaction.get_table(table_name)) {
220✔
UNCOV
147
                if (!table->is_embedded()) {
×
148
                    bad_transaction_log("AddTable: The existing table '%1' is not embedded", table_name);
×
149
                }
×
UNCOV
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
    };
13,012✔
157

6,424✔
158
    mpark::visit(std::move(add_table), instr.type);
13,012✔
159
}
13,012✔
160

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

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

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

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

67,950✔
182
    mpark::visit(
135,382✔
183
        util::overload{
135,382✔
184
            [&](mpark::monostate) {
67,966✔
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,070✔
196
                if (!pk_col) {
110,528✔
197
                    bad_transaction_log("CreateObject(Int) on table without a primary key");
×
198
                }
×
199
                if (table->get_column_type(pk_col) != type_Int) {
110,528✔
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,528✔
204
                m_last_object = table->create_object_with_primary_key(pk);
110,528✔
205
            },
110,528✔
206
            [&](InternString pk) {
76,316✔
207
                if (!pk_col) {
17,748✔
208
                    bad_transaction_log("CreateObject(String) on table without a primary key");
×
209
                }
×
210
                if (table->get_column_type(pk_col) != type_String) {
17,748✔
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);
17,748✔
215
                log("sync::create_object_with_primary_key(group, get_table(\"%1\"), \"%2\");", table->get_name(),
17,748✔
216
                    str);
17,748✔
217
                m_last_object = table->create_object_with_primary_key(str);
17,748✔
218
            },
17,748✔
219
            [&](const ObjectId& id) {
71,850✔
220
                if (!pk_col) {
7,014✔
221
                    bad_transaction_log("CreateObject(ObjectId) on table without a primary key");
×
222
                }
×
223
                if (table->get_column_type(pk_col) != type_ObjectId) {
7,014✔
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,014✔
228
                m_last_object = table->create_object_with_primary_key(id);
7,014✔
229
            },
7,014✔
230
            [&](const UUID& id) {
67,974✔
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) {
67,956✔
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
        },
135,382✔
250
        instr.object);
135,382✔
251
}
135,382✔
252

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

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

313,366✔
269
    const auto& data = payload.data;
643,416✔
270
    switch (payload.type) {
643,416✔
271
        case Type::ObjectValue:
2,604✔
272
            return visitor(Instruction::Payload::ObjectValue{});
2,604✔
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:
874✔
280
            return visitor(realm::util::none);
874✔
281
        case Type::Int:
447,800✔
282
            return visitor(data.integer);
447,800✔
283
        case Type::Bool:
184✔
284
            return visitor(data.boolean);
184✔
285
        case Type::String: {
172,152✔
286
            StringData value = get_string(data.str);
172,152✔
287
            return visitor(value);
172,152✔
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,064✔
298
            return visitor(data.dnum);
1,064✔
299
        case Type::Decimal:
168✔
300
            return visitor(data.decimal);
168✔
301
        case Type::Link: {
1,848✔
302
            StringData class_name = get_string(data.link.target_table);
1,848✔
303
            Group::TableNameBuffer buffer;
1,848✔
304
            StringData target_table_name = Group::class_name_to_table_name(class_name, buffer);
1,848✔
305
            TableRef target_table = m_transaction.get_table(target_table_name);
1,848✔
306
            if (!target_table) {
1,848✔
307
                bad_transaction_log("Link with invalid target table '%1'", target_table_name);
×
308
            }
×
309
            if (target_table->is_embedded()) {
1,848✔
310
                bad_transaction_log("Link to embedded table '%1'", target_table_name);
×
311
            }
×
312
            ObjKey target = get_object_key(*target_table, data.link.target);
1,848✔
313
            ObjLink link = ObjLink{target_table->get_key(), target};
1,848✔
314
            return visitor(link);
1,848✔
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
    }
643,416✔
321
}
643,416✔
322

323
void InstructionApplier::operator()(const Instruction::Update& instr)
324
{
477,468✔
325
    struct UpdateResolver : public PathResolver {
477,468✔
326
        UpdateResolver(InstructionApplier* applier, const Instruction::Update& instr)
477,468✔
327
            : PathResolver(applier, instr, "Update")
477,468✔
328
            , m_instr(instr)
477,468✔
329
        {
477,474✔
330
        }
477,472✔
331
        void on_property(Obj& obj, ColKey col) override
477,468✔
332
        {
473,240✔
333
            // Update of object field.
228,694✔
334

228,694✔
335
            auto table = obj.get_table();
469,816✔
336
            auto table_name = table->get_name();
469,816✔
337
            auto field_name = table->get_column_name(col);
469,816✔
338
            auto data_type = DataType(col.get_type());
469,816✔
339

228,694✔
340
            auto visitor = [&](const mpark::variant<ObjLink, Mixed, Instruction::Payload::ObjectValue,
469,816✔
341
                                                    Instruction::Payload::Erased>& arg) {
469,836✔
342
                if (const auto link_ptr = mpark::get_if<ObjLink>(&arg)) {
469,814✔
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)) {
469,714✔
362
                    if (mixed_ptr->is_null()) {
469,138✔
363
                        if (col.is_nullable()) {
734✔
364
                            obj.set_null(col, m_instr.is_default);
734✔
365
                        }
734✔
366
                        else {
×
367
                            m_applier->bad_transaction_log("Update: NULL in non-nullable field '%2.%1'", field_name,
×
368
                                                           table_name);
×
369
                        }
×
370
                    }
734✔
371
                    else if (data_type == type_Mixed || mixed_ptr->get_type() == data_type) {
468,404✔
372
                        obj.set_any(col, *mixed_ptr, m_instr.is_default);
468,388✔
373
                    }
468,388✔
374
                    else {
16✔
375
                        m_applier->bad_transaction_log("Update: Type mismatch in '%2.%1' (expected %3, got %4)",
16✔
376
                                                       field_name, table_name, col.get_type(), mixed_ptr->get_type());
16✔
377
                    }
16✔
378
                }
469,138✔
379
                else if (const auto obj_val_ptr = mpark::get_if<Instruction::Payload::ObjectValue>(&arg)) {
590✔
380
                    if (obj.is_null(col)) {
584✔
381
                        obj.create_and_set_linked_object(col);
556✔
382
                    }
556✔
383
                }
584✔
384
                else if (const auto erase_ptr = mpark::get_if<Instruction::Payload::Erased>(&arg)) {
2,147,483,653✔
385
                    m_applier->bad_transaction_log("Update: Dictionary erase at object field");
×
386
                }
×
387
            };
469,814✔
388

228,694✔
389
            m_applier->visit_payload(m_instr.value, visitor);
469,816✔
390
        }
469,816✔
391
        Status on_list_index(LstBase& list, uint32_t index) override
477,468✔
392
        {
235,034✔
393
            // Update of list element.
2,108✔
394

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

2,108✔
401
            auto visitor = util::overload{
5,024✔
402
                [&](const ObjLink& link) {
2,186✔
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) {
4,946✔
432
                    if (value.is_null()) {
4,868✔
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 {
4,868✔
442
                        if (data_type == type_Mixed || value.get_type() == data_type) {
4,868✔
443
                            list.set_any(index, value);
4,868✔
444
                        }
4,868✔
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
                    }
4,868✔
451
                },
4,868✔
452
                [&](const Instruction::Payload::ObjectValue&) {
2,108✔
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,108✔
457
                    m_applier->bad_transaction_log("Update: Dictionary erase of list element");
×
458
                },
×
459
            };
5,024✔
460

2,108✔
461
            m_applier->visit_payload(m_instr.value, visitor);
5,024✔
462
            return Status::Pending;
5,024✔
463
        }
5,024✔
464
        Status on_dictionary_key(Dictionary& dict, Mixed key) override
477,468✔
465
        {
233,448✔
466
            // Update (insert) of dictionary element.
1,330✔
467

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

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

232,118✔
494
    private:
477,468✔
495
        const Instruction::Update& m_instr;
477,468✔
496
    };
477,468✔
497
    UpdateResolver resolver(this, instr);
477,468✔
498
    resolver.resolve();
477,468✔
499
}
477,468✔
500

501
void InstructionApplier::operator()(const Instruction::AddInteger& instr)
502
{
3,552✔
503
    // FIXME: Implement increments of array elements, dictionary values.
1,718✔
504
    struct AddIntegerResolver : public PathResolver {
3,552✔
505
        AddIntegerResolver(InstructionApplier* applier, const Instruction::AddInteger& instr)
3,552✔
506
            : PathResolver(applier, instr, "AddInteger")
3,552✔
507
            , m_instr(instr)
3,552✔
508
        {
3,552✔
509
        }
3,552✔
510
        void on_property(Obj& obj, ColKey col)
3,552✔
511
        {
3,552✔
512
            // Increment of object field.
1,718✔
513
            if (!obj.is_null(col)) {
3,552✔
514
                try {
3,496✔
515
                    obj.add_int(col, m_instr.value);
3,496✔
516
                }
3,496✔
517
                catch (const LogicError&) {
1,690✔
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,496✔
523
        }
3,552✔
524

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

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

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

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

10,856✔
543
    if (ColKey existing_key = table->get_column_key(col_name)) {
22,322✔
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,704✔
583
    if (instr.collection_type == CollectionType::Dictionary && instr.key_type != Type::String) {
22,018✔
584
        bad_transaction_log("AddColumn '%1.%3' adding dictionary column with non-string keys", table->get_name(),
×
585
                            col_name);
×
586
    }
×
587

10,704✔
588
    if (instr.type != Type::Link) {
22,018✔
589
        DataType type = get_data_type(instr.type);
20,882✔
590
        switch (instr.collection_type) {
20,882✔
591
            case CollectionType::Single: {
14,804✔
592
                table->add_column(type, col_name, instr.nullable);
14,804✔
593
                break;
14,804✔
594
            }
×
595
            case CollectionType::List: {
5,846✔
596
                table->add_column_list(type, col_name, instr.nullable);
5,846✔
597
                break;
5,846✔
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,136✔
608
        }
1,136✔
609
    }
1,136✔
610
    else {
1,136✔
611
        Group::TableNameBuffer buffer;
1,136✔
612
        auto target_table_name = get_string(instr.link_target_table);
1,136✔
613
        if (target_table_name.size() != 0) {
1,136✔
614
            TableRef target = m_transaction.get_table(Group::class_name_to_table_name(target_table_name, buffer));
1,128✔
615
            if (!target) {
1,128✔
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,128✔
620
                table->add_column_list(*target, col_name);
580✔
621
            }
580✔
622
            else if (instr.collection_type == CollectionType::Set) {
548✔
623
                table->add_column_set(*target, col_name);
8✔
624
            }
8✔
625
            else if (instr.collection_type == CollectionType::Dictionary) {
540✔
626
                table->add_column_dictionary(*target, col_name);
32✔
627
            }
32✔
628
            else {
508✔
629
                REALM_ASSERT(instr.collection_type == CollectionType::Single);
508✔
630
                table->add_column(*target, col_name);
508✔
631
            }
508✔
632
        }
1,128✔
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,136✔
643
}
22,018✔
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
{
163,788✔
663
    struct ArrayInsertResolver : public PathResolver {
163,788✔
664
        ArrayInsertResolver(InstructionApplier* applier, const Instruction::ArrayInsert& instr)
163,788✔
665
            : PathResolver(applier, instr, "ArrayInsert")
163,788✔
666
            , m_instr(instr)
163,788✔
667
        {
163,788✔
668
        }
163,788✔
669
        Status on_list_index(LstBase& list, uint32_t index) override
163,788✔
670
        {
163,788✔
671
            auto col = list.get_col_key();
163,788✔
672
            auto data_type = DataType(col.get_type());
163,788✔
673
            auto table = list.get_table();
163,788✔
674
            auto table_name = table->get_name();
163,788✔
675
            auto field_name = table->get_column_name(col);
163,788✔
676

80,184✔
677
            if (index > m_instr.prior_size) {
163,788✔
678
                m_applier->bad_transaction_log("ArrayInsert: Invalid insertion index (index = %1, prior_size = %2)",
×
679
                                               index, m_instr.prior_size);
×
680
            }
×
681

80,184✔
682
            if (index > list.size()) {
163,788✔
683
                m_applier->bad_transaction_log("ArrayInsert: Index out of bounds (%1 > %2)", index, list.size());
×
684
            }
×
685

80,184✔
686
            if (m_instr.prior_size != list.size()) {
163,788✔
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

80,184✔
691
            auto inserter = util::overload{
163,788✔
692
                [&](const ObjLink& link) {
80,764✔
693
                    if (data_type == type_TypedLink) {
1,160✔
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,160✔
699
                        REALM_ASSERT(dynamic_cast<Lst<Mixed>*>(&list));
132✔
700
                        auto& mixed_list = static_cast<Lst<Mixed>&>(list);
132✔
701
                        mixed_list.insert(index, link);
132✔
702
                    }
132✔
703
                    else if (data_type == type_LinkList || data_type == type_Link) {
1,028!
704
                        REALM_ASSERT(dynamic_cast<Lst<ObjKey>*>(&list));
1,028✔
705
                        auto& link_list = static_cast<Lst<ObjKey>&>(list);
1,028✔
706
                        // Validate the target.
514✔
707
                        auto target_table = table->get_link_target(col);
1,028✔
708
                        if (target_table->get_key() != link.get_table_key()) {
1,028✔
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,028✔
715
                    }
1,028✔
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,160✔
722
                [&](Mixed value) {
162,378✔
723
                    if (value.is_null()) {
160,968✔
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 {
160,916✔
733
                        if (data_type == type_Mixed || value.get_type() == data_type) {
160,916✔
734
                            list.insert_any(index, value);
160,916✔
735
                        }
160,916✔
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
                    }
160,916✔
742
                },
160,968✔
743
                [&](const Instruction::Payload::ObjectValue&) {
81,014✔
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&) {
163,788✔
764
                    m_applier->bad_transaction_log("Dictionary payload for ArrayInsert");
163,788✔
765
                },
163,788✔
766
                [&](const Instruction::Payload::Erased&) {
80,184✔
767
                    m_applier->bad_transaction_log("Dictionary erase payload for ArrayInsert");
×
768
                },
×
769
            };
163,788✔
770

80,184✔
771
            m_applier->visit_payload(m_instr.value, inserter);
163,788✔
772
            return Status::Pending;
163,788✔
773
        }
163,788✔
774

80,184✔
775
    private:
163,788✔
776
        const Instruction::ArrayInsert& m_instr;
163,788✔
777
    };
163,788✔
778
    ArrayInsertResolver(this, instr).resolve();
163,788✔
779
}
163,788✔
780

781
void InstructionApplier::operator()(const Instruction::ArrayMove& instr)
782
{
104✔
783
    struct ArrayMoveResolver : public PathResolver {
104✔
784
        ArrayMoveResolver(InstructionApplier* applier, const Instruction::ArrayMove& instr)
104✔
785
            : PathResolver(applier, instr, "ArrayMove")
104✔
786
            , m_instr(instr)
104✔
787
        {
104✔
788
        }
104✔
789
        Status on_list_index(LstBase& list, uint32_t index) override
104✔
790
        {
104✔
791
            if (index >= list.size()) {
104✔
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()) {
104✔
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) {
104✔
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()) {
104✔
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);
104✔
807
            return Status::Pending;
104✔
808
        }
104✔
809

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

816
void InstructionApplier::operator()(const Instruction::ArrayErase& instr)
817
{
4,732✔
818
    struct ArrayEraseResolver : public PathResolver {
4,732✔
819
        ArrayEraseResolver(InstructionApplier* applier, const Instruction::ArrayErase& instr)
4,732✔
820
            : PathResolver(applier, instr, "ArrayErase")
4,732✔
821
            , m_instr(instr)
4,732✔
822
        {
4,732✔
823
        }
4,732✔
824
        Status on_list_index(LstBase& list, uint32_t index) override
4,732✔
825
        {
4,732✔
826
            if (index >= m_instr.prior_size) {
4,732✔
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,732✔
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,732✔
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,732✔
838
            return Status::Pending;
4,732✔
839
        }
4,732✔
840

1,972✔
841
    private:
4,732✔
842
        const Instruction::ArrayErase& m_instr;
4,732✔
843
    };
4,732✔
844
    ArrayEraseResolver(this, instr).resolve();
4,732✔
845
}
4,732✔
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
        {
98✔
860
            dict.clear();
×
861
        }
×
862
        void on_set(SetBase& set) override
220✔
863
        {
130✔
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
{
16✔
873
    struct AllowsNullsResolver : public PathResolver {
16✔
874
        AllowsNullsResolver(InstructionApplier* applier, const Instruction::PathInstruction& instr,
16✔
875
                            const std::string_view& instr_name)
16✔
876
            : PathResolver(applier, instr, instr_name)
16✔
877
            , m_allows_nulls(false)
16✔
878
        {
16✔
879
        }
16✔
880
        Status on_list_index(LstBase&, uint32_t) override
16✔
881
        {
8✔
882
            return Status::Pending;
×
883
        }
×
884
        void on_list(LstBase&) override {}
8✔
885
        void on_set(SetBase&) override {}
8✔
886
        void on_dictionary(Dictionary&) override
16✔
887
        {
8✔
888
            m_allows_nulls = true;
×
889
        }
×
890
        Status on_dictionary_key(Dictionary&, Mixed) override
16✔
891
        {
16✔
892
            m_allows_nulls = true;
16✔
893
            return Status::Pending;
16✔
894
        }
16✔
895
        void on_property(Obj&, ColKey) override
16✔
896
        {
8✔
897
            m_allows_nulls = true;
×
898
        }
×
899
        bool allows_nulls()
16✔
900
        {
16✔
901
            resolve();
16✔
902
            return m_allows_nulls;
16✔
903
        }
16✔
904

8✔
905
    private:
16✔
906
        bool m_allows_nulls;
16✔
907
    };
16✔
908
    return AllowsNullsResolver(this, instr, instr_name).allows_nulls();
16✔
909
}
16✔
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
{
21,896✔
921
    bool valid_payload = true;
21,896✔
922
    using Type = Instruction::Payload::Type;
21,896✔
923
    if (payload.type == Type::Link) {
21,896✔
924
        StringData class_name = get_string(payload.data.link.target_table);
500✔
925
        Group::TableNameBuffer buffer;
500✔
926
        StringData target_table_name = Group::class_name_to_table_name(class_name, buffer);
500✔
927
        TableRef target_table = m_transaction.get_table(target_table_name);
500✔
928
        if (!target_table) {
500✔
929
            bad_transaction_log("Link with invalid target table '%1'", target_table_name);
×
930
        }
×
931
        if (target_table->is_embedded()) {
500✔
932
            bad_transaction_log("Link to embedded table '%1'", target_table_name);
×
933
        }
×
934
        Mixed linked_pk =
500✔
935
            mpark::visit(util::overload{[&](mpark::monostate) {
262✔
936
                                            return Mixed{}; // the link exists and the pk is null
24✔
937
                                        },
24✔
938
                                        [&](int64_t pk) {
486✔
939
                                            return Mixed{pk};
472✔
940
                                        },
472✔
941
                                        [&](InternString interned_pk) {
250✔
942
                                            return Mixed{get_string(interned_pk)};
×
943
                                        },
×
944
                                        [&](GlobalKey) {
250✔
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) {
252✔
951
                                            return Mixed{pk};
4✔
952
                                        },
4✔
953
                                        [&](UUID pk) {
250✔
954
                                            return Mixed{pk};
×
955
                                        }},
×
956
                         payload.data.link.target);
500✔
957

250✔
958
        if (!target_table->find_primary_key(linked_pk)) {
500✔
959
            valid_payload = false;
48✔
960
        }
48✔
961
    }
500✔
962
    return valid_payload;
21,896✔
963
}
21,896✔
964

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

848✔
981
            auto inserter = util::overload{
1,694✔
982
                [&](const ObjLink& link) {
920✔
983
                    if (data_type == type_TypedLink) {
144✔
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) {
144✔
989
                        REALM_ASSERT(dynamic_cast<Set<Mixed>*>(&set));
64✔
990
                        auto& mixed_set = static_cast<Set<Mixed>&>(set);
64✔
991
                        mixed_set.insert(link);
64✔
992
                    }
64✔
993
                    else if (data_type == type_Link) {
80✔
994
                        REALM_ASSERT(dynamic_cast<Set<ObjKey>*>(&set));
80✔
995
                        auto& link_set = static_cast<Set<ObjKey>&>(set);
80✔
996
                        // Validate the target.
40✔
997
                        auto target_table = table->get_link_target(col);
80✔
998
                        if (target_table->get_key() != link.get_table_key()) {
80✔
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());
80✔
1004
                    }
80✔
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
                },
144✔
1011
                [&](Mixed value) {
1,622✔
1012
                    if (value.is_null() && !col.is_nullable()) {
1,550✔
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,550✔
1018
                        set.insert_any(value);
1,550✔
1019
                    }
1,550✔
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,550✔
1026
                [&](const Instruction::Payload::ObjectValue&) {
848✔
1027
                    m_applier->bad_transaction_log("SetInsert: Sets of embedded objects are not supported.");
×
1028
                },
×
1029
                [&](const Instruction::Payload::Dictionary&) {
1,694✔
1030
                    m_applier->bad_transaction_log("SetInsert: Sets of dictionaries are not supported.");
1,694✔
1031
                },
1,694✔
1032
                [&](const Instruction::Payload::Erased&) {
848✔
1033
                    m_applier->bad_transaction_log("SetInsert: Dictionary erase payload in SetInsert");
×
1034
                },
×
1035
            };
1,694✔
1036

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

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

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

228✔
1062
            auto inserter = util::overload{
452✔
1063
                [&](const ObjLink& link) {
300✔
1064
                    if (data_type == type_TypedLink) {
144✔
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) {
144✔
1070
                        REALM_ASSERT(dynamic_cast<Set<Mixed>*>(&set));
68✔
1071
                        auto& mixed_set = static_cast<Set<Mixed>&>(set);
68✔
1072
                        mixed_set.erase(link);
68✔
1073
                    }
68✔
1074
                    else if (data_type == type_Link) {
76✔
1075
                        REALM_ASSERT(dynamic_cast<Set<ObjKey>*>(&set));
76✔
1076
                        auto& link_set = static_cast<Set<ObjKey>&>(set);
76✔
1077
                        // Validate the target.
38✔
1078
                        auto target_table = table->get_link_target(col);
76✔
1079
                        if (target_table->get_key() != link.get_table_key()) {
76✔
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());
76✔
1085
                    }
76✔
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
                },
144✔
1092
                [&](Mixed value) {
380✔
1093
                    if (value.is_null() && !col.is_nullable()) {
308!
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) {
308✔
1099
                        set.erase_any(value);
308✔
1100
                    }
308✔
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
                },
308✔
1107
                [&](const Instruction::Payload::ObjectValue&) {
228✔
1108
                    m_applier->bad_transaction_log("SetErase: Sets of embedded objects are not supported.");
×
1109
                },
×
1110
                [&](const Instruction::Payload::Dictionary&) {
452✔
1111
                    m_applier->bad_transaction_log("SetErase: Sets of dictionaries are not supported.");
452✔
1112
                },
452✔
1113
                [&](const Instruction::Payload::Erased&) {
228✔
1114
                    m_applier->bad_transaction_log("SetErase: Dictionary erase payload in SetErase");
×
1115
                },
×
1116
            };
452✔
1117

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

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

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

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

1159
util::Optional<Obj> InstructionApplier::get_top_object(const Instruction::ObjectInstruction& instr,
1160
                                                       const std::string_view& name)
1161
{
733,044✔
1162
    if (m_last_table_name == instr.table && m_last_object_key && m_last_object &&
733,044✔
1163
        *m_last_object_key == instr.object) {
652,596✔
1164
        // We have already found the object, reuse it.
115,354✔
1165
        return *m_last_object;
244,076✔
1166
    }
244,076✔
1167
    else {
488,968✔
1168
        TableRef table = get_table(instr, name);
488,968✔
1169
        ObjKey key = get_object_key(*table, instr.object, name);
488,968✔
1170
        if (!key) {
488,968✔
1171
            return util::none;
×
1172
        }
×
1173
        if (!table->is_valid(key)) {
488,968✔
1174
            // Check if the object is deleted or is a tombstone.
8,164✔
1175
            return util::none;
14,984✔
1176
        }
14,984✔
1177

235,258✔
1178
        Obj obj = table->get_object(key);
473,984✔
1179
        m_last_object_key = instr.object;
473,984✔
1180
        m_last_object = obj;
473,984✔
1181
        return obj;
473,984✔
1182
    }
473,984✔
1183
}
733,044✔
1184

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

1216
InstructionApplier::PathResolver::~PathResolver()
1217
{
675,964✔
1218
    on_finish();
675,964✔
1219
}
675,964✔
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
{
664,318✔
1260
    m_applier->m_last_field = col;
664,318✔
1261
}
664,318✔
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
{
652,056✔
1278
    m_applier->m_current_path = m_path_instr.path;
652,056✔
1279
    m_applier->m_last_field_name = m_path_instr.field;
652,056✔
1280
    return Status::Pending;
652,056✔
1281
}
652,056✔
1282

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

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

1295
InstructionApplier::PathResolver::Status InstructionApplier::PathResolver::resolve()
1296
{
675,976✔
1297
    util::Optional<Obj> obj = m_applier->get_top_object(m_path_instr, m_instr_name);
675,976✔
1298
    Status begin_status = on_begin(obj);
675,976✔
1299
    if (begin_status != Status::Pending) {
675,976✔
1300
        return begin_status;
232✔
1301
    }
232✔
1302
    if (!obj) {
675,744✔
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

329,076✔
1308
    m_it_begin = m_path_instr.path.begin();
675,744✔
1309
    m_it_end = m_path_instr.path.end();
675,744✔
1310
    Status status = resolve_field(*obj, m_path_instr.field);
675,744✔
1311
    return status == Status::Pending ? Status::Success : status;
673,444✔
1312
}
675,744✔
1313

1314
InstructionApplier::PathResolver::Status InstructionApplier::PathResolver::resolve_field(Obj& obj, InternString field)
1315
{
691,410✔
1316
    auto field_name = get_string(field);
691,410✔
1317
    ColKey col = obj.get_table()->get_column_key(field_name);
691,410✔
1318
    if (!col) {
691,410✔
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);
691,410✔
1324

336,914✔
1325
    if (m_it_begin == m_it_end) {
691,410✔
1326
        if (col.is_list()) {
491,720✔
1327
            auto list = obj.get_listbase_ptr(col);
192✔
1328
            on_list(*list);
192✔
1329
        }
192✔
1330
        else if (col.is_dictionary()) {
491,528✔
1331
            auto dict = obj.get_dictionary(col);
×
1332
            on_dictionary(dict);
×
1333
        }
×
1334
        else if (col.is_set()) {
491,528✔
1335
            SetBasePtr set;
3,478✔
1336
            if (col.get_type() == col_type_Link) {
3,478✔
1337
                // We are interested in using non-condensed indexes - as for Lists below
152✔
1338
                set = obj.get_set_ptr<ObjKey>(col);
304✔
1339
            }
304✔
1340
            else {
3,174✔
1341
                set = obj.get_setbase_ptr(col);
3,174✔
1342
            }
3,174✔
1343
            on_set(*set);
3,478✔
1344
        }
3,478✔
1345
        else {
488,050✔
1346
            on_property(obj, col);
488,050✔
1347
        }
488,050✔
1348
        return Status::Pending;
491,720✔
1349
    }
491,720✔
1350

97,342✔
1351
    if (col.is_list()) {
199,690✔
1352
        if (auto pindex = mpark::get_if<uint32_t>(&*m_it_begin)) {
186,700✔
1353
            std::unique_ptr<LstBase> list = InstructionApplier::get_list_from_path(obj, col);
186,700✔
1354
            ++m_it_begin;
186,700✔
1355
            return resolve_list_element(*list, *pindex);
186,700✔
1356
        }
186,700✔
1357
        on_error(util::format("%1: List index is not an integer on field '%2' in class '%3'", m_instr_name,
2,147,483,647✔
1358
                              field_name, obj.get_table()->get_name()));
2,147,483,647✔
1359
    }
2,147,483,647✔
1360
    else if (col.is_dictionary()) {
12,992✔
1361
        if (auto pkey = mpark::get_if<InternString>(&*m_it_begin)) {
8,624✔
1362
            auto dict = obj.get_dictionary(col);
8,624✔
1363
            ++m_it_begin;
8,624✔
1364
            return resolve_dictionary_element(dict, *pkey);
8,624✔
1365
        }
8,624✔
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,376✔
1370
        auto target = obj.get_table()->get_link_target(col);
4,376✔
1371
        if (!target->is_embedded()) {
4,376✔
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,376✔
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,244✔
1385
            auto embedded_object = obj.get_linked_object(col);
4,244✔
1386
            ++m_it_begin;
4,244✔
1387
            return resolve_field(embedded_object, *pfield);
4,244✔
1388
        }
4,244✔
1389
        else {
×
1390
            on_error(util::format("%1: Embedded object field reference is not a string", m_instr_name));
×
1391
        }
×
1392
    }
4,376✔
1393
    else {
2,147,483,647✔
1394
        on_error(util::format("%1: Resolving path through unstructured field '%3.%2' of type %4", m_instr_name,
2,147,483,647✔
1395
                              field_name, obj.get_table()->get_name(), col.get_type()));
2,147,483,647✔
1396
    }
2,147,483,647✔
1397
    return Status::DidNotResolve;
2,147,580,989✔
1398
}
199,690✔
1399

1400
InstructionApplier::PathResolver::Status InstructionApplier::PathResolver::resolve_list_element(LstBase& list,
1401
                                                                                                uint32_t index)
1402
{
186,700✔
1403
    if (m_it_begin == m_it_end) {
186,700✔
1404
        return on_list_index(list, index);
176,528✔
1405
    }
176,528✔
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,624✔
1447
    StringData string_key = get_string(key);
8,624✔
1448
    if (m_it_begin == m_it_end) {
8,624✔
1449
        return on_dictionary_key(dict, Mixed{string_key});
4,548✔
1450
    }
4,548✔
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
{
490,908✔
1495
    StringData table_name = table.get_name();
490,908✔
1496
    ColKey pk_col = table.get_primary_key_column();
490,908✔
1497
    StringData pk_name = "";
490,908✔
1498
    DataType pk_type;
490,908✔
1499
    if (pk_col) {
490,912✔
1500
        pk_name = table.get_column_name(pk_col);
490,910✔
1501
        pk_type = table.get_column_type(pk_col);
490,910✔
1502
    }
490,910✔
1503
    return mpark::visit(
490,908✔
1504
        util::overload{
490,908✔
1505
            [&](mpark::monostate) {
244,400✔
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) {
479,264✔
1520
                if (!pk_col) {
469,186✔
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) {
469,186✔
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);
469,186✔
1531
                return key;
469,186✔
1532
            },
469,186✔
1533
            [&](InternString interned_pk) {
253,584✔
1534
                auto pk = get_string(interned_pk);
16,812✔
1535
                if (!pk_col) {
16,812✔
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) {
16,812✔
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);
16,812✔
1546
                return key;
16,812✔
1547
            },
16,812✔
1548
            [&](GlobalKey id) {
244,390✔
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) {
246,818✔
1558
                if (!pk_col) {
4,860✔
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) {
4,860✔
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);
4,860✔
1569
                return key;
4,860✔
1570
            },
4,860✔
1571
            [&](UUID pk) {
244,398✔
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);
490,908✔
1586
}
490,908✔
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

© 2025 Coveralls, Inc