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

realm / realm-core / 2642

03 Jan 2025 08:42AM UTC coverage: 91.124% (-0.006%) from 91.13%
2642

push

Evergreen

jedelbo
New changelog section to prepare for vNext

102738 of 181490 branches covered (56.61%)

217310 of 238476 relevant lines covered (91.12%)

5920676.51 hits per line

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

75.85
/src/realm/cluster.cpp
1
/*************************************************************************
2
 *
3
 * Copyright 2016 Realm Inc.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 **************************************************************************/
18

19
#include "realm/cluster.hpp"
20
#include "realm/array_integer.hpp"
21
#include "realm/array_basic.hpp"
22
#include "realm/array_bool.hpp"
23
#include "realm/array_string.hpp"
24
#include "realm/array_binary.hpp"
25
#include "realm/array_mixed.hpp"
26
#include "realm/array_timestamp.hpp"
27
#include "realm/array_decimal128.hpp"
28
#include "realm/array_fixed_bytes.hpp"
29
#include "realm/array_key.hpp"
30
#include "realm/array_ref.hpp"
31
#include "realm/array_typed_link.hpp"
32
#include "realm/array_backlink.hpp"
33
#include "realm/column_type_traits.hpp"
34
#include "realm/replication.hpp"
35
#include "realm/dictionary.hpp"
36
#include "realm/list.hpp"
37
#include <iostream>
38
#include <cmath>
39

40
namespace realm {
41

42
/******************************* FieldValues *********************************/
43

44
FieldValues::FieldValues(std::initializer_list<FieldValue> init)
45
    : m_values(init)
536,937✔
46
{
1,096,110✔
47
    if (m_values.size() > 1) {
1,096,110✔
48
        // Sort according to ColKey index
49
        std::sort(m_values.begin(), m_values.end(), [](const auto& a, const auto& b) {
106,626✔
50
            return a.col_key.get_index().val < b.col_key.get_index().val;
106,626✔
51
        });
106,626✔
52
    }
17,928✔
53
}
1,096,110✔
54

55
void FieldValues::insert(ColKey k, Mixed val, bool is_default)
56
{
882,582✔
57
    if (m_values.empty()) {
882,582✔
58
        m_values.emplace_back(k, val, is_default);
881,844✔
59
        return;
881,844✔
60
    }
881,844✔
61
    unsigned int idx = k.get_index().val;
738✔
62
    auto it = std::lower_bound(m_values.begin(), m_values.end(), idx, [](const auto& a, unsigned int i) {
942✔
63
        return a.col_key.get_index().val < i;
942✔
64
    });
942✔
65
    m_values.insert(it, {k, val, is_default});
738✔
66
}
738✔
67

68
/******************************* ClusterNode *********************************/
69

70
void ClusterNode::IteratorState::clear()
71
{
2,162,652✔
72
    m_current_leaf.detach();
2,162,652✔
73
    m_key_offset = 0;
2,162,652✔
74
    m_current_index = size_t(-1);
2,162,652✔
75
}
2,162,652✔
76

77
void ClusterNode::IteratorState::init(State& s, ObjKey key)
78
{
55,878✔
79
    m_current_leaf.init(s.mem);
55,878✔
80
    m_current_index = s.index;
55,878✔
81
    m_key_offset = key.value - m_current_leaf.get_key_value(m_current_index);
55,878✔
82
    m_current_leaf.set_offset(m_key_offset);
55,878✔
83
}
55,878✔
84

85
const Table* ClusterNode::get_owning_table() const noexcept
86
{
130,341✔
87
    return m_tree_top.get_owning_table();
130,341✔
88
}
130,341✔
89

90
void ClusterNode::get(ObjKey k, ClusterNode::State& state) const
91
{
105,678,453✔
92
    if (!k || !try_get(RowKey(k), state)) {
105,678,453✔
93
        throw KeyNotFound(util::format("No object with key '%1' in '%2'", k.value, get_owning_table()->get_name()));
210✔
94
    }
210✔
95
}
105,678,453✔
96

97

98
/********************************* Cluster ***********************************/
99

100
MemRef Cluster::create_empty_cluster(Allocator& alloc)
101
{
277,452✔
102
    Array arr(alloc);
277,452✔
103
    arr.create(Array::type_HasRefs); // Throws
277,452✔
104

105
    arr.add(RefOrTagged::make_tagged(0)); // Compact form
277,452✔
106
    return arr.get_mem();
277,452✔
107
}
277,452✔
108

109

110
template <class T>
111
inline void Cluster::do_create(ColKey col)
112
{
257,271✔
113
    T arr(m_alloc);
257,271✔
114
    arr.create();
257,271✔
115
    auto col_ndx = col.get_index();
257,271✔
116
    arr.set_parent(this, col_ndx.val + s_first_col_index);
257,271✔
117
    arr.update_parent();
257,271✔
118
}
257,271✔
119

120
void Cluster::create()
121
{
109,653✔
122
    Array::create(type_HasRefs, false, s_first_col_index);
109,653✔
123
    Array::set(0, RefOrTagged::make_tagged(0)); // Size = 0
109,653✔
124

125
    auto column_initialize = [this](ColKey col_key) {
264,039✔
126
        auto col_ndx = col_key.get_index();
264,039✔
127
        while (size() <= col_ndx.val + 1)
528,078✔
128
            add(0);
264,039✔
129
        auto type = col_key.get_type();
264,039✔
130
        auto attr = col_key.get_attrs();
264,039✔
131
        if (attr.test(col_attr_Collection)) {
264,039✔
132
            ArrayRef arr(m_alloc);
6,768✔
133
            arr.create();
6,768✔
134
            arr.set_parent(this, col_ndx.val + s_first_col_index);
6,768✔
135
            arr.update_parent();
6,768✔
136
            return IteratorControl::AdvanceToNext;
6,768✔
137
        }
6,768✔
138
        switch (type) {
257,271✔
139
            case col_type_Int:
147,807✔
140
                if (attr.test(col_attr_Nullable)) {
147,807✔
141
                    do_create<ArrayIntNull>(col_key);
9,777✔
142
                }
9,777✔
143
                else {
138,030✔
144
                    do_create<ArrayInteger>(col_key);
138,030✔
145
                }
138,030✔
146
                break;
147,807✔
147
            case col_type_Bool:
693✔
148
                do_create<ArrayBoolNull>(col_key);
693✔
149
                break;
693✔
150
            case col_type_Float:
1,137✔
151
                do_create<ArrayFloatNull>(col_key);
1,137✔
152
                break;
1,137✔
153
            case col_type_Double:
5,790✔
154
                do_create<ArrayDoubleNull>(col_key);
5,790✔
155
                break;
5,790✔
156
            case col_type_String: {
72,438✔
157
                if (m_tree_top.is_string_enum_type(col_ndx)) {
72,438✔
158
                    do_create<ArrayInteger>(col_key);
5,340✔
159
                }
5,340✔
160
                else {
67,098✔
161
                    do_create<ArrayString>(col_key);
67,098✔
162
                }
67,098✔
163
                break;
72,438✔
164
            }
×
165
            case col_type_Binary:
5,280✔
166
                do_create<ArrayBinary>(col_key);
5,280✔
167
                break;
5,280✔
168
            case col_type_Mixed:
636✔
169
                do_create<ArrayMixed>(col_key);
636✔
170
                break;
636✔
171
            case col_type_Timestamp:
14,283✔
172
                do_create<ArrayTimestamp>(col_key);
14,283✔
173
                break;
14,283✔
174
            case col_type_Decimal:
276✔
175
                do_create<ArrayDecimal128>(col_key);
276✔
176
                break;
276✔
177
            case col_type_ObjectId:
2,475✔
178
                do_create<ArrayObjectIdNull>(col_key);
2,475✔
179
                break;
2,475✔
180
            case col_type_UUID:
408✔
181
                do_create<ArrayUUIDNull>(col_key);
408✔
182
                break;
408✔
183
            case col_type_Link:
2,325✔
184
                do_create<ArrayKey>(col_key);
2,325✔
185
                break;
2,325✔
186
            case col_type_TypedLink:
✔
187
                do_create<ArrayTypedLink>(col_key);
×
188
                break;
×
189
            case col_type_BackLink:
3,723✔
190
                do_create<ArrayBacklink>(col_key);
3,723✔
191
                break;
3,723✔
192
            default:
✔
193
                REALM_UNREACHABLE();
194
        }
257,271✔
195
        return IteratorControl::AdvanceToNext;
257,271✔
196
    };
257,271✔
197
    m_tree_top.m_owner->for_each_and_every_column(column_initialize);
109,653✔
198

199
    // By specifying the minimum size, we ensure that the array has a capacity
200
    // to hold m_size 64 bit refs.
201
    ensure_size(m_size * 8);
109,653✔
202
    // "ensure_size" may COW, but as array is just created, it has no parents, so
203
    // failing to update parent is not an error.
204
    clear_missing_parent_update();
109,653✔
205
}
109,653✔
206

207
void Cluster::init(MemRef mem)
208
{
209,510,169✔
209
    Array::init_from_mem(mem);
209,510,169✔
210
    auto rot = Array::get_as_ref_or_tagged(0);
209,510,169✔
211
    if (rot.is_tagged()) {
209,510,169✔
212
        m_keys.detach();
146,604,369✔
213
    }
146,604,369✔
214
    else {
62,905,800✔
215
        m_keys.init_from_ref(rot.get_as_ref());
62,905,800✔
216
    }
62,905,800✔
217
}
209,510,169✔
218

219
void Cluster::update_from_parent() noexcept
220
{
640,089✔
221
    Array::init_from_parent();
640,089✔
222
    auto rot = Array::get_as_ref_or_tagged(0);
640,089✔
223
    if (!rot.is_tagged()) {
640,089✔
224
        m_keys.update_from_parent();
29,346✔
225
    }
29,346✔
226
}
640,089✔
227

228
MemRef Cluster::ensure_writeable(RowKey)
229
{
×
230
    // By specifying the minimum size, we ensure that the array has a capacity
231
    // to hold m_size 64 bit refs.
232
    copy_on_write(8 * m_size);
×
233

234
    return get_mem();
×
235
}
×
236

237
void Cluster::update_ref_in_parent(RowKey, ref_type)
238
{
×
239
    REALM_UNREACHABLE();
240
}
×
241

242
size_t Cluster::node_size_from_header(Allocator& alloc, const char* header)
243
{
72,832,728✔
244
    auto rot = Array::get_as_ref_or_tagged(header, s_key_ref_or_size_index);
72,832,728✔
245
    if (rot.is_tagged()) {
72,832,728✔
246
        return size_t(rot.get_as_int());
71,455,905✔
247
    }
71,455,905✔
248
    else {
1,376,823✔
249
        return Array::get_size_from_header(alloc.translate(rot.get_as_ref()));
1,376,823✔
250
    }
1,376,823✔
251
}
72,832,728✔
252

253
template <class T>
254
inline void Cluster::set_spec(T&, ColKey::Idx) const
255
{
37,459,149✔
256
}
37,459,149✔
257

258
template <>
259
inline void Cluster::set_spec(ArrayString& arr, ColKey::Idx col_ndx) const
260
{
3,761,970✔
261
    m_tree_top.set_spec(arr, col_ndx);
3,761,970✔
262
}
3,761,970✔
263

264
template <class T>
265
inline void Cluster::do_insert_row(size_t ndx, ColKey col, Mixed init_val, bool nullable)
266
{
33,530,778✔
267
    using U = typename util::RemoveOptional<typename T::value_type>::type;
33,530,778✔
268

269
    T arr(m_alloc);
33,530,778✔
270
    auto col_ndx = col.get_index();
33,530,778✔
271
    arr.set_parent(this, col_ndx.val + s_first_col_index);
33,530,778✔
272
    set_spec<T>(arr, col_ndx);
33,530,778✔
273
    arr.init_from_parent();
33,530,778✔
274
    if (init_val.is_null()) {
33,530,778✔
275
        arr.insert(ndx, T::default_value(nullable));
32,464,920✔
276
    }
32,464,920✔
277
    else {
1,065,858✔
278
        arr.insert(ndx, init_val.get<U>());
1,065,858✔
279
    }
1,065,858✔
280
}
33,530,778✔
281

282
inline void Cluster::do_insert_key(size_t ndx, ColKey col_key, Mixed init_val, ObjKey origin_key)
283
{
289,863✔
284
    ObjKey target_key = init_val.is_null() ? ObjKey{} : init_val.get<ObjKey>();
289,863✔
285
    ArrayKey arr(m_alloc);
289,863✔
286
    auto col_ndx = col_key.get_index();
289,863✔
287
    arr.set_parent(this, col_ndx.val + s_first_col_index);
289,863✔
288
    arr.init_from_parent();
289,863✔
289
    arr.insert(ndx, target_key);
289,863✔
290

291
    // Insert backlink if link is not null
292
    if (target_key) {
289,863✔
293
        const Table* origin_table = m_tree_top.get_owning_table();
12✔
294
        ColKey opp_col = origin_table->get_opposite_column(col_key);
12✔
295
        TableRef opp_table = origin_table->get_opposite_table(col_key);
12✔
296
        Obj target_obj = opp_table->get_object(target_key);
12✔
297
        target_obj.add_backlink(opp_col, origin_key);
12✔
298
    }
12✔
299
}
289,863✔
300

301
inline void Cluster::do_insert_mixed(size_t ndx, ColKey col_key, Mixed init_value, ObjKey origin_key)
302
{
166,350✔
303
    ArrayMixed arr(m_alloc);
166,350✔
304
    arr.set_parent(this, col_key.get_index().val + s_first_col_index);
166,350✔
305
    arr.init_from_parent();
166,350✔
306
    arr.insert(ndx, init_value);
166,350✔
307

308
    // Insert backlink if needed
309
    if (init_value.is_type(type_TypedLink)) {
166,350✔
310
        // In case we are inserting in a Dictionary cluster, the backlink will
311
        // be handled in Dictionary::insert function
312
        if (Table* origin_table = const_cast<Table*>(m_tree_top.get_owning_table())) {
24✔
313
            if (origin_table->is_asymmetric()) {
24✔
314
                throw IllegalOperation("Object value not supported in asymmetric table");
6✔
315
            }
6✔
316
            ObjLink link = init_value.get<ObjLink>();
18✔
317
            auto target_table = origin_table->get_parent_group()->get_table(link.get_table_key());
18✔
318
            if (target_table->is_asymmetric()) {
18✔
319
                throw IllegalOperation("Ephemeral object value not supported");
6✔
320
            }
6✔
321
            ColKey backlink_col_key = target_table->find_or_add_backlink_column(col_key, origin_table->get_key());
12✔
322
            target_table->get_object(link.get_obj_key()).add_backlink(backlink_col_key, origin_key);
12✔
323
        }
12✔
324
    }
24✔
325
}
166,350✔
326

327
inline void Cluster::do_insert_link(size_t ndx, ColKey col_key, Mixed init_val, ObjKey origin_key)
328
{
×
329
    ObjLink target_link = init_val.is_null() ? ObjLink{} : init_val.get<ObjLink>();
×
330
    ArrayTypedLink arr(m_alloc);
×
331
    auto col_ndx = col_key.get_index();
×
332
    arr.set_parent(this, col_ndx.val + s_first_col_index);
×
333
    arr.init_from_parent();
×
334
    arr.insert(ndx, target_link);
×
335

336
    // Insert backlink if link is not null
337
    if (target_link) {
×
338
        Table* origin_table = const_cast<Table*>(m_tree_top.get_owning_table());
×
339
        auto target_table = origin_table->get_parent_group()->get_table(target_link.get_table_key());
×
340

341
        ColKey backlink_col_key = target_table->find_or_add_backlink_column(col_key, origin_table->get_key());
×
342
        target_table->get_object(target_link.get_obj_key()).add_backlink(backlink_col_key, origin_key);
×
343
    }
×
344
}
×
345

346
void Cluster::insert_row(size_t ndx, RowKey row_key, const FieldValues& init_values)
347
{
23,881,887✔
348
    // Ensure the cluster array is big enough to hold 64 bit values.
349
    copy_on_write(m_size * 8);
23,881,887✔
350

351
    if (m_keys.is_attached()) {
23,881,887✔
352
        m_keys.insert(ndx, row_key.value);
1,624,047✔
353
    }
1,624,047✔
354
    else {
22,257,840✔
355
        Array::set(s_key_ref_or_size_index, Array::get(s_key_ref_or_size_index) + 2); // Increments size by 1
22,257,840✔
356
    }
22,257,840✔
357

358
    auto val = init_values.begin();
23,881,887✔
359
    auto insert_in_column = [&](ColKey col_key) {
35,829,477✔
360
        auto col_ndx = col_key.get_index();
35,829,477✔
361
        auto attr = col_key.get_attrs();
35,829,477✔
362
        Mixed init_value;
35,829,477✔
363
        // init_values must be sorted in col_ndx order - this is ensured by ClustTree::insert()
364
        if (val != init_values.end() && val->col_key.get_index().val == col_ndx.val) {
35,829,477✔
365
            init_value = val->value;
1,013,346✔
366
            ++val;
1,013,346✔
367
        }
1,013,346✔
368

369
        auto type = col_key.get_type();
35,829,477✔
370
        if (attr.test(col_attr_Collection)) {
35,829,477✔
371
            REALM_ASSERT(init_value.is_null());
753,327✔
372
            ArrayRef arr(m_alloc);
753,327✔
373
            arr.set_parent(this, col_ndx.val + s_first_col_index);
753,327✔
374
            arr.init_from_parent();
753,327✔
375
            arr.insert(ndx, 0);
753,327✔
376
            return IteratorControl::AdvanceToNext;
753,327✔
377
        }
753,327✔
378

379
        bool nullable = attr.test(col_attr_Nullable);
35,076,150✔
380
        ObjKey obj_key(int64_t(row_key.value + get_offset()));
35,076,150✔
381
        switch (type) {
35,076,150✔
382
            case col_type_Int:
26,326,521✔
383
                if (attr.test(col_attr_Nullable)) {
26,326,521✔
384
                    do_insert_row<ArrayIntNull>(ndx, col_key, init_value, nullable);
1,985,010✔
385
                }
1,985,010✔
386
                else {
24,341,511✔
387
                    do_insert_row<ArrayInteger>(ndx, col_key, init_value, nullable);
24,341,511✔
388
                }
24,341,511✔
389
                break;
26,326,521✔
390
            case col_type_Bool:
188,598✔
391
                do_insert_row<ArrayBoolNull>(ndx, col_key, init_value, nullable);
188,598✔
392
                break;
188,598✔
393
            case col_type_Float:
331,539✔
394
                do_insert_row<ArrayFloatNull>(ndx, col_key, init_value, nullable);
331,539✔
395
                break;
331,539✔
396
            case col_type_Double:
1,493,298✔
397
                do_insert_row<ArrayDoubleNull>(ndx, col_key, init_value, nullable);
1,493,298✔
398
                break;
1,493,298✔
399
            case col_type_String:
3,169,500✔
400
                do_insert_row<ArrayString>(ndx, col_key, init_value, nullable);
3,169,500✔
401
                break;
3,169,500✔
402
            case col_type_Binary:
1,369,611✔
403
                do_insert_row<ArrayBinary>(ndx, col_key, init_value, nullable);
1,369,611✔
404
                break;
1,369,611✔
405
            case col_type_Mixed: {
166,350✔
406
                do_insert_mixed(ndx, col_key, init_value, obj_key);
166,350✔
407
                break;
166,350✔
408
            }
×
409
            case col_type_Timestamp:
226,515✔
410
                do_insert_row<ArrayTimestamp>(ndx, col_key, init_value, nullable);
226,515✔
411
                break;
226,515✔
412
            case col_type_Decimal:
78,258✔
413
                do_insert_row<ArrayDecimal128>(ndx, col_key, init_value, nullable);
78,258✔
414
                break;
78,258✔
415
            case col_type_ObjectId:
228,528✔
416
                do_insert_row<ArrayObjectIdNull>(ndx, col_key, init_value, nullable);
228,528✔
417
                break;
228,528✔
418
            case col_type_UUID:
119,694✔
419
                do_insert_row<ArrayUUIDNull>(ndx, col_key, init_value, nullable);
119,694✔
420
                break;
119,694✔
421
            case col_type_Link:
289,863✔
422
                do_insert_key(ndx, col_key, init_value, obj_key);
289,863✔
423
                break;
289,863✔
424
            case col_type_TypedLink:
✔
425
                do_insert_link(ndx, col_key, init_value, obj_key);
×
426
                break;
×
427
            case col_type_BackLink: {
1,158,423✔
428
                ArrayBacklink arr(m_alloc);
1,158,423✔
429
                arr.set_parent(this, col_ndx.val + s_first_col_index);
1,158,423✔
430
                arr.init_from_parent();
1,158,423✔
431
                arr.insert(ndx, 0);
1,158,423✔
432
                break;
1,158,423✔
433
            }
×
434
            default:
✔
435
                REALM_ASSERT(false);
×
436
                break;
×
437
        }
35,076,150✔
438
        return IteratorControl::AdvanceToNext;
34,981,893✔
439
    };
35,076,150✔
440
    m_tree_top.m_owner->for_each_and_every_column(insert_in_column);
23,881,887✔
441
}
23,881,887✔
442

443
template <class T>
444
inline void Cluster::do_move(size_t ndx, ColKey col_key, Cluster* to)
445
{
23,715✔
446
    auto col_ndx = col_key.get_index().val + s_first_col_index;
23,715✔
447
    T src(m_alloc);
23,715✔
448
    src.set_parent(this, col_ndx);
23,715✔
449
    src.init_from_parent();
23,715✔
450

451
    T dst(m_alloc);
23,715✔
452
    dst.set_parent(to, col_ndx);
23,715✔
453
    dst.init_from_parent();
23,715✔
454

455
    src.move(dst, ndx);
23,715✔
456
}
23,715✔
457

458
void Cluster::move(size_t ndx, ClusterNode* new_node, int64_t offset)
459
{
12,435✔
460
    auto new_leaf = static_cast<Cluster*>(new_node);
12,435✔
461

462
    auto move_from_column = [&](ColKey col_key) {
23,715✔
463
        auto attr = col_key.get_attrs();
23,715✔
464
        auto type = col_key.get_type();
23,715✔
465

466
        if (attr.test(col_attr_Collection)) {
23,715✔
467
            do_move<ArrayRef>(ndx, col_key, new_leaf);
294✔
468
            return IteratorControl::AdvanceToNext;
294✔
469
        }
294✔
470

471
        switch (type) {
23,421✔
472
            case col_type_Int:
20,799✔
473
                if (attr.test(col_attr_Nullable)) {
20,799✔
474
                    do_move<ArrayIntNull>(ndx, col_key, new_leaf);
9,387✔
475
                }
9,387✔
476
                else {
11,412✔
477
                    do_move<ArrayInteger>(ndx, col_key, new_leaf);
11,412✔
478
                }
11,412✔
479
                break;
20,799✔
480
            case col_type_Bool:
36✔
481
                do_move<ArrayBoolNull>(ndx, col_key, new_leaf);
36✔
482
                break;
36✔
483
            case col_type_Float:
36✔
484
                do_move<ArrayFloat>(ndx, col_key, new_leaf);
36✔
485
                break;
36✔
486
            case col_type_Double:
36✔
487
                do_move<ArrayDouble>(ndx, col_key, new_leaf);
36✔
488
                break;
36✔
489
            case col_type_String: {
1,044✔
490
                if (m_tree_top.is_string_enum_type(col_key.get_index()))
1,044✔
491
                    do_move<ArrayInteger>(ndx, col_key, new_leaf);
×
492
                else
1,044✔
493
                    do_move<ArrayString>(ndx, col_key, new_leaf);
1,044✔
494
                break;
1,044✔
495
            }
×
496
            case col_type_Binary:
36✔
497
                do_move<ArrayBinary>(ndx, col_key, new_leaf);
36✔
498
                break;
36✔
499
            case col_type_Mixed:
528✔
500
                do_move<ArrayMixed>(ndx, col_key, new_leaf);
528✔
501
                break;
528✔
502
            case col_type_Timestamp:
36✔
503
                do_move<ArrayTimestamp>(ndx, col_key, new_leaf);
36✔
504
                break;
36✔
505
            case col_type_Decimal:
36✔
506
                do_move<ArrayDecimal128>(ndx, col_key, new_leaf);
36✔
507
                break;
36✔
508
            case col_type_ObjectId:
36✔
509
                do_move<ArrayObjectIdNull>(ndx, col_key, new_leaf);
36✔
510
                break;
36✔
511
            case col_type_UUID:
120✔
512
                do_move<ArrayUUIDNull>(ndx, col_key, new_leaf);
120✔
513
                break;
120✔
514
            case col_type_Link:
96✔
515
                do_move<ArrayKey>(ndx, col_key, new_leaf);
96✔
516
                break;
96✔
517
            case col_type_TypedLink:
✔
518
                do_move<ArrayTypedLink>(ndx, col_key, new_leaf);
×
519
                break;
×
520
            case col_type_BackLink:
582✔
521
                do_move<ArrayBacklink>(ndx, col_key, new_leaf);
582✔
522
                break;
582✔
523
            default:
✔
524
                REALM_ASSERT(false);
×
525
                break;
×
526
        }
23,421✔
527
        return IteratorControl::AdvanceToNext;
23,421✔
528
    };
23,421✔
529
    m_tree_top.m_owner->for_each_and_every_column(move_from_column);
12,435✔
530
    for (size_t i = ndx; i < m_keys.size(); i++) {
1,323,588✔
531
        new_leaf->m_keys.add(m_keys.get(i) - offset);
1,311,153✔
532
    }
1,311,153✔
533
    m_keys.truncate(ndx);
12,435✔
534
}
12,435✔
535

536
Cluster::~Cluster() {}
214,576,050✔
537

538
ColKey Cluster::get_col_key(size_t ndx_in_parent) const
539
{
19,035✔
540
    ColKey::Idx col_ndx{unsigned(ndx_in_parent - 1)}; // <- leaf_index here. Opaque.
19,035✔
541
    auto col_key = get_owning_table()->leaf_ndx2colkey(col_ndx);
19,035✔
542
    REALM_ASSERT(col_key.get_index().val == col_ndx.val);
19,035✔
543
    return col_key;
19,035✔
544
}
19,035✔
545

546
void Cluster::ensure_general_form()
547
{
66,354✔
548
    if (!m_keys.is_attached()) {
66,354✔
549
        size_t current_size = get_size_in_compact_form();
51,093✔
550
        m_keys.create(current_size, 255);
51,093✔
551
        m_keys.update_parent();
51,093✔
552
        for (size_t i = 0; i < current_size; i++) {
5,816,601✔
553
            m_keys.set(i, i);
5,765,508✔
554
        }
5,765,508✔
555
    }
51,093✔
556
}
66,354✔
557

558
template <class T>
559
inline void Cluster::do_insert_column(ColKey col_key, bool nullable)
560
{
603,108✔
561
    size_t sz = node_size();
603,108✔
562

563
    T arr(m_alloc);
603,108✔
564
    arr.create();
603,108✔
565
    auto val = T::default_value(nullable);
603,108✔
566
    for (size_t i = 0; i < sz; i++) {
6,847,923✔
567
        arr.add(val);
6,244,815✔
568
    }
6,244,815✔
569
    auto col_ndx = col_key.get_index();
603,108✔
570
    unsigned ndx = col_ndx.val + s_first_col_index;
603,108✔
571

572
    // Fill up if indexes are not consecutive
573
    while (size() < ndx)
603,108✔
574
        Array::add(0);
×
575

576
    if (ndx == size())
603,108✔
577
        Array::insert(ndx, from_ref(arr.get_ref()));
603,009✔
578
    else
99✔
579
        Array::set(ndx, from_ref(arr.get_ref()));
99✔
580
}
603,108✔
581

582
void Cluster::insert_column(ColKey col_key)
583
{
799,695✔
584
    auto attr = col_key.get_attrs();
799,695✔
585
    auto type = col_key.get_type();
799,695✔
586
    if (attr.test(col_attr_Collection)) {
799,695✔
587
        size_t sz = node_size();
196,593✔
588

589
        ArrayRef arr(m_alloc);
196,593✔
590
        arr.create(sz);
196,593✔
591
        auto col_ndx = col_key.get_index();
196,593✔
592
        unsigned idx = col_ndx.val + s_first_col_index;
196,593✔
593
        if (idx == size())
196,593✔
594
            Array::insert(idx, from_ref(arr.get_ref()));
196,593✔
595
        else
×
596
            Array::set(idx, from_ref(arr.get_ref()));
×
597
        return;
196,593✔
598
    }
196,593✔
599
    bool nullable = attr.test(col_attr_Nullable);
603,102✔
600
    switch (type) {
603,102✔
601
        case col_type_Int:
270,333✔
602
            if (nullable) {
270,333✔
603
                do_insert_column<ArrayIntNull>(col_key, nullable);
21,414✔
604
            }
21,414✔
605
            else {
248,919✔
606
                do_insert_column<ArrayInteger>(col_key, nullable);
248,919✔
607
            }
248,919✔
608
            break;
270,333✔
609
        case col_type_Bool:
5,523✔
610
            do_insert_column<ArrayBoolNull>(col_key, nullable);
5,523✔
611
            break;
5,523✔
612
        case col_type_Float:
6,474✔
613
            do_insert_column<ArrayFloatNull>(col_key, nullable);
6,474✔
614
            break;
6,474✔
615
        case col_type_Double:
7,119✔
616
            do_insert_column<ArrayDoubleNull>(col_key, nullable);
7,119✔
617
            break;
7,119✔
618
        case col_type_String:
105,570✔
619
            do_insert_column<ArrayString>(col_key, nullable);
105,570✔
620
            break;
105,570✔
621
        case col_type_Binary:
7,302✔
622
            do_insert_column<ArrayBinary>(col_key, nullable);
7,302✔
623
            break;
7,302✔
624
        case col_type_Mixed:
9,138✔
625
            do_insert_column<ArrayMixed>(col_key, nullable);
9,138✔
626
            break;
9,138✔
627
        case col_type_Timestamp:
24,486✔
628
            do_insert_column<ArrayTimestamp>(col_key, nullable);
24,486✔
629
            break;
24,486✔
630
        case col_type_Decimal:
5,058✔
631
            do_insert_column<ArrayDecimal128>(col_key, nullable);
5,058✔
632
            break;
5,058✔
633
        case col_type_ObjectId:
39,867✔
634
            do_insert_column<ArrayObjectIdNull>(col_key, nullable);
39,867✔
635
            break;
39,867✔
636
        case col_type_UUID:
5,670✔
637
            do_insert_column<ArrayUUIDNull>(col_key, nullable);
5,670✔
638
            break;
5,670✔
639
        case col_type_Link:
31,509✔
640
            do_insert_column<ArrayKey>(col_key, nullable);
31,509✔
641
            break;
31,509✔
642
        case col_type_TypedLink:
✔
643
            do_insert_column<ArrayTypedLink>(col_key, nullable);
×
644
            break;
×
645
        case col_type_BackLink:
85,056✔
646
            do_insert_column<ArrayBacklink>(col_key, nullable);
85,056✔
647
            break;
85,056✔
648
        default:
✔
649
            REALM_UNREACHABLE();
650
            break;
×
651
    }
603,102✔
652
}
603,102✔
653

654
void Cluster::remove_column(ColKey col_key)
655
{
6,714✔
656
    auto col_ndx = col_key.get_index();
6,714✔
657
    unsigned idx = col_ndx.val + s_first_col_index;
6,714✔
658
    ref_type ref = to_ref(Array::get(idx));
6,714✔
659
    if (ref != 0) {
6,714✔
660
        Array::destroy_deep(ref, m_alloc);
6,714✔
661
    }
6,714✔
662
    if (idx == size() - 1)
6,714✔
663
        Array::erase(idx);
5,175✔
664
    else
1,539✔
665
        Array::set(idx, 0);
1,539✔
666
}
6,714✔
667

668
ref_type Cluster::insert(RowKey row_key, const FieldValues& init_values, ClusterNode::State& state)
669
{
23,876,805✔
670
    int64_t current_key_value = -1;
23,876,805✔
671
    size_t sz;
23,876,805✔
672
    size_t ndx;
23,876,805✔
673
    ref_type ret = 0;
23,876,805✔
674

675
    auto on_error = [&] {
23,876,805✔
676
        throw KeyAlreadyUsed(
12✔
677
            util::format("When inserting key '%1' in '%2'", row_key.value, get_owning_table()->get_name()));
12✔
678
    };
12✔
679

680
    if (m_keys.is_attached()) {
23,876,805✔
681
        sz = m_keys.size();
1,611,549✔
682
        ndx = m_keys.lower_bound(row_key.value);
1,611,549✔
683
        if (ndx < sz) {
1,611,549✔
684
            current_key_value = m_keys.get(ndx);
616,566✔
685
            if (row_key.value == uint64_t(current_key_value)) {
616,566✔
686
                on_error();
12✔
687
            }
12✔
688
        }
616,566✔
689
    }
1,611,549✔
690
    else {
22,265,256✔
691
        sz = size_t(Array::get(s_key_ref_or_size_index)) >> 1; // Size is stored as tagged integer
22,265,256✔
692
        if (row_key.value < sz) {
22,265,256✔
693
            on_error();
×
694
        }
×
695
        // Key value is bigger than all other values, should be put last
696
        ndx = sz;
22,265,256✔
697
        if (row_key.value > sz && sz < cluster_node_size) {
22,265,256✔
698
            ensure_general_form();
18,186✔
699
        }
18,186✔
700
    }
22,265,256✔
701

702
    REALM_ASSERT_DEBUG(sz <= cluster_node_size);
23,876,805✔
703
    if (REALM_LIKELY(sz < cluster_node_size)) {
23,876,805✔
704
        insert_row(ndx, row_key, init_values); // Throws
23,787,174✔
705
        state.mem = get_mem();
23,787,174✔
706
        state.index = ndx;
23,787,174✔
707
    }
23,787,174✔
708
    else {
89,631✔
709
        // Split leaf node
710
        Cluster new_leaf(0, m_alloc, m_tree_top);
89,631✔
711
        new_leaf.create();
89,631✔
712
        if (ndx == sz) {
99,306✔
713
            new_leaf.insert_row(0, RowKey(0), init_values); // Throws
86,757✔
714
            state.split_key = int64_t(row_key.value);
86,757✔
715
            state.mem = new_leaf.get_mem();
86,757✔
716
            state.index = 0;
86,757✔
717
        }
86,757✔
718
        else {
2,147,496,196✔
719
            // Current cluster must be in general form to get here
720
            REALM_ASSERT_DEBUG(m_keys.is_attached());
2,147,496,196✔
721
            new_leaf.ensure_general_form();
2,147,496,196✔
722
            move(ndx, &new_leaf, current_key_value);
2,147,496,196✔
723
            insert_row(ndx, row_key, init_values); // Throws
2,147,496,196✔
724
            state.mem = get_mem();
2,147,496,196✔
725
            state.split_key = current_key_value;
2,147,496,196✔
726
            state.index = ndx;
2,147,496,196✔
727
        }
2,147,496,196✔
728
        ret = new_leaf.get_ref();
89,631✔
729
    }
89,631✔
730

731
    return ret;
23,876,805✔
732
}
23,876,805✔
733

734
bool Cluster::try_get(RowKey k, ClusterNode::State& state) const noexcept
735
{
162,192,045✔
736
    state.mem = get_mem();
162,192,045✔
737
    if (m_keys.is_attached()) {
162,192,045✔
738
        state.index = m_keys.lower_bound(k.value);
51,278,679✔
739
        return state.index != m_keys.size() && m_keys.get(state.index) == k.value;
51,278,679✔
740
    }
51,278,679✔
741
    else {
110,913,366✔
742
        if (k.value < uint64_t(Array::get(s_key_ref_or_size_index) >> 1)) {
110,913,366✔
743
            state.index = size_t(k.value);
92,230,404✔
744
            return true;
92,230,404✔
745
        }
92,230,404✔
746
    }
110,913,366✔
747
    return false;
18,682,962✔
748
}
162,192,045✔
749

750
ObjKey Cluster::get(size_t ndx, ClusterNode::State& state) const
751
{
11,854,695✔
752
    state.index = ndx;
11,854,695✔
753
    state.mem = get_mem();
11,854,695✔
754
    return get_real_key(ndx);
11,854,695✔
755
}
11,854,695✔
756

757
template <class T>
758
inline void Cluster::do_erase(size_t ndx, ColKey col_key)
759
{
7,102,410✔
760
    auto col_ndx = col_key.get_index();
7,102,410✔
761
    T values(m_alloc);
7,102,410✔
762
    values.set_parent(this, col_ndx.val + s_first_col_index);
7,102,410✔
763
    set_spec<T>(values, col_ndx);
7,102,410✔
764
    values.init_from_parent();
7,102,410✔
765
    if constexpr (std::is_same_v<T, ArrayTypedLink>) {
7,102,410✔
766
        if (ObjLink link = values.get(ndx)) {
×
767
            if (const Table* origin_table = m_tree_top.get_owning_table()) {
×
768
                auto target_obj = origin_table->get_parent_group()->get_object(link);
×
769

770
                ColKey backlink_col_key =
×
771
                    target_obj.get_table()->find_backlink_column(col_key, origin_table->get_key());
×
772
                REALM_ASSERT(backlink_col_key);
×
773
                target_obj.remove_one_backlink(backlink_col_key, get_real_key(ndx)); // Throws
×
774
            }
×
775
        }
×
776
    }
×
777
    values.erase(ndx);
7,102,410✔
778
}
7,102,410✔
779

780
inline void Cluster::do_erase_mixed(size_t ndx, ColKey col_key, CascadeState& state)
781
{
145,440✔
782
    const Table* origin_table = m_tree_top.get_owning_table();
145,440✔
783
    auto col_ndx = col_key.get_index();
145,440✔
784

785
    ArrayMixed values(m_alloc);
145,440✔
786
    values.set_parent(this, col_ndx.val + s_first_col_index);
145,440✔
787
    values.init_from_parent();
145,440✔
788

789
    Mixed value = values.get(ndx);
145,440✔
790
    if (value.is_type(type_TypedLink)) {
145,440✔
791
        ObjLink link = value.get<ObjLink>();
30✔
792
        Obj obj(origin_table->m_own_ref, get_mem(), get_real_key(ndx), ndx);
30✔
793
        obj.remove_backlink(col_key, link, state);
30✔
794
    }
30✔
795
    if (value.is_type(type_List)) {
145,440✔
796
        Obj obj(origin_table->m_own_ref, get_mem(), get_real_key(ndx), ndx);
72,042✔
797
        Lst<Mixed> list(obj, col_key);
72,042✔
798
        list.remove_backlinks(state);
72,042✔
799
    }
72,042✔
800
    if (value.is_type(type_Dictionary)) {
145,440✔
801
        Obj obj(origin_table->m_own_ref, get_mem(), get_real_key(ndx), ndx);
72,015✔
802
        Dictionary dict(obj, col_key);
72,015✔
803
        dict.remove_backlinks(state);
72,015✔
804
    }
72,015✔
805
    values.erase(ndx);
145,440✔
806
}
145,440✔
807

808
inline void Cluster::do_erase_key(size_t ndx, ColKey col_key, CascadeState& state)
809
{
92,847✔
810
    ArrayKey values(m_alloc);
92,847✔
811
    auto col_ndx = col_key.get_index();
92,847✔
812
    values.set_parent(this, col_ndx.val + s_first_col_index);
92,847✔
813
    values.init_from_parent();
92,847✔
814

815
    ObjKey key = values.get(ndx);
92,847✔
816
    if (key != null_key) {
92,847✔
817
        do_remove_backlinks(get_real_key(ndx), col_key, std::vector<ObjKey>{key}, state);
90,078✔
818
    }
90,078✔
819
    values.erase(ndx);
92,847✔
820
}
92,847✔
821

822
size_t Cluster::get_ndx(RowKey k, size_t ndx) const noexcept
823
{
10,551,447✔
824
    size_t index;
10,551,447✔
825
    if (m_keys.is_attached()) {
10,551,447✔
826
        index = m_keys.lower_bound(k.value);
10,432,551✔
827
        if (index == m_keys.size() || m_keys.get(index) != k.value) {
10,433,061✔
828
            return realm::npos;
18✔
829
        }
18✔
830
    }
10,432,551✔
831
    else {
118,896✔
832
        index = size_t(k.value);
118,896✔
833
        if (index >= get_as_ref_or_tagged(s_key_ref_or_size_index).get_as_int()) {
118,896✔
834
            return realm::npos;
×
835
        }
×
836
    }
118,896✔
837
    return index + ndx;
10,551,429✔
838
}
10,551,447✔
839

840
size_t Cluster::erase(RowKey row_key, CascadeState& state)
841
{
5,323,005✔
842
    size_t ndx = get_ndx(row_key, 0);
5,323,005✔
843
    if (ndx == realm::npos)
5,323,005✔
844
        throw KeyNotFound(util::format("When erasing key '%1' (offset '%2') in '%3'", row_key.value, m_offset,
18✔
845
                                       get_owning_table()->get_name()));
18✔
846

847
    ObjKey real_key = get_real_key(ndx);
5,322,987✔
848
    std::vector<ColKey> backlink_column_keys;
5,322,987✔
849

850
    auto erase_in_column = [&](ColKey col_key) {
7,445,082✔
851
        auto col_type = col_key.get_type();
7,445,082✔
852
        auto attr = col_key.get_attrs();
7,445,082✔
853
        if (attr.test(col_attr_Collection)) {
7,445,082✔
854
            auto col_ndx = col_key.get_index();
104,727✔
855
            ArrayRef values(m_alloc);
104,727✔
856
            values.set_parent(this, col_ndx.val + s_first_col_index);
104,727✔
857
            values.init_from_parent();
104,727✔
858
            ref_type ref = values.get(ndx);
104,727✔
859

860
            if (ref) {
104,727✔
861
                const Table* origin_table = m_tree_top.get_owning_table();
84,051✔
862
                if (attr.test(col_attr_Dictionary)) {
84,051✔
863
                    if (col_type == col_type_Mixed || col_type == col_type_Link) {
38,655✔
864
                        Obj obj(origin_table->m_own_ref, get_mem(), real_key, ndx);
36,354✔
865
                        Dictionary dict(obj, col_key);
36,354✔
866
                        dict.remove_backlinks(state);
36,354✔
867
                    }
36,354✔
868
                }
38,655✔
869
                else if (col_type == col_type_Link) {
45,396✔
870
                    BPlusTree<ObjKey> links(m_alloc);
4,677✔
871
                    links.init_from_ref(ref);
4,677✔
872
                    if (links.size() > 0) {
4,677✔
873
                        do_remove_backlinks(real_key, col_key, links.get_all(), state);
1,041✔
874
                    }
1,041✔
875
                }
4,677✔
876
                else if (col_type == col_type_TypedLink) {
40,719✔
877
                    BPlusTree<ObjLink> links(m_alloc);
×
878
                    links.init_from_ref(ref);
×
879
                    for (size_t i = 0; i < links.size(); i++) {
×
880
                        ObjLink link = links.get(i);
×
881
                        auto target_obj = origin_table->get_parent_group()->get_object(link);
×
882
                        ColKey backlink_col_key =
×
883
                            target_obj.get_table()->find_backlink_column(col_key, origin_table->get_key());
×
884
                        target_obj.remove_one_backlink(backlink_col_key, real_key);
×
885
                    }
×
886
                }
×
887
                else if (col_type == col_type_Mixed) {
40,719✔
888
                    Obj obj(origin_table->m_own_ref, get_mem(), real_key, ndx);
36,246✔
889
                    Lst<Mixed> list(obj, col_key);
36,246✔
890
                    list.remove_backlinks(state);
36,246✔
891
                }
36,246✔
892
                Array::destroy_deep(ref, m_alloc);
84,051✔
893
            }
84,051✔
894

895
            values.erase(ndx);
104,727✔
896

897
            return IteratorControl::AdvanceToNext;
104,727✔
898
        }
104,727✔
899

900
        switch (col_type) {
7,340,355✔
901
            case col_type_Int:
6,044,958✔
902
                if (attr.test(col_attr_Nullable)) {
6,044,958✔
903
                    do_erase<ArrayIntNull>(ndx, col_key);
1,276,029✔
904
                }
1,276,029✔
905
                else {
4,768,929✔
906
                    do_erase<ArrayInteger>(ndx, col_key);
4,768,929✔
907
                }
4,768,929✔
908
                break;
6,044,958✔
909
            case col_type_Bool:
42,390✔
910
                do_erase<ArrayBoolNull>(ndx, col_key);
42,390✔
911
                break;
42,390✔
912
            case col_type_Float:
36,492✔
913
                do_erase<ArrayFloatNull>(ndx, col_key);
36,492✔
914
                break;
36,492✔
915
            case col_type_Double:
36,501✔
916
                do_erase<ArrayDoubleNull>(ndx, col_key);
36,501✔
917
                break;
36,501✔
918
            case col_type_String:
428,058✔
919
                do_erase<ArrayString>(ndx, col_key);
428,058✔
920
                break;
428,058✔
921
            case col_type_Binary:
43,587✔
922
                do_erase<ArrayBinary>(ndx, col_key);
43,587✔
923
                break;
43,587✔
924
            case col_type_Mixed:
145,440✔
925
                do_erase_mixed(ndx, col_key, state);
145,440✔
926
                break;
145,440✔
927
            case col_type_Timestamp:
45,444✔
928
                do_erase<ArrayTimestamp>(ndx, col_key);
45,444✔
929
                break;
45,444✔
930
            case col_type_Decimal:
36,279✔
931
                do_erase<ArrayDecimal128>(ndx, col_key);
36,279✔
932
                break;
36,279✔
933
            case col_type_ObjectId:
73,977✔
934
                do_erase<ArrayObjectIdNull>(ndx, col_key);
73,977✔
935
                break;
73,977✔
936
            case col_type_UUID:
60,285✔
937
                do_erase<ArrayUUIDNull>(ndx, col_key);
60,285✔
938
                break;
60,285✔
939
            case col_type_Link:
92,847✔
940
                do_erase_key(ndx, col_key, state);
92,847✔
941
                break;
92,847✔
942
            case col_type_TypedLink:
✔
943
                do_erase<ArrayTypedLink>(ndx, col_key);
×
944
                break;
×
945
            case col_type_BackLink:
254,421✔
946
                if (state.m_mode == CascadeState::Mode::None) {
254,421✔
947
                    do_erase<ArrayBacklink>(ndx, col_key);
188,628✔
948
                }
188,628✔
949
                else {
65,793✔
950
                    // Postpone the deletion of backlink entries or else the
951
                    // checks for if there's any remaining backlinks will
952
                    // check the wrong row for columns which have already
953
                    // had values erased from them.
954
                    backlink_column_keys.push_back(col_key);
65,793✔
955
                }
65,793✔
956
                break;
254,421✔
957
            default:
✔
958
                REALM_ASSERT(false);
×
959
                break;
×
960
        }
7,340,355✔
961
        return IteratorControl::AdvanceToNext;
7,340,367✔
962
    };
7,340,355✔
963
    m_tree_top.m_owner->for_each_and_every_column(erase_in_column);
5,322,987✔
964

965
    // Any remaining backlink columns to erase from?
966
    for (auto k : backlink_column_keys)
5,322,987✔
967
        do_erase<ArrayBacklink>(ndx, k);
65,793✔
968

969
    if (m_keys.is_attached()) {
5,322,987✔
970
        m_keys.erase(ndx);
5,280,057✔
971
    }
5,280,057✔
972
    else {
42,930✔
973
        size_t current_size = get_size_in_compact_form();
42,930✔
974
        if (ndx == current_size - 1) {
42,930✔
975
            // When deleting last, we can still maintain compact form
976
            set(0, RefOrTagged::make_tagged(current_size - 1));
27,156✔
977
        }
27,156✔
978
        else {
15,774✔
979
            ensure_general_form();
15,774✔
980
            m_keys.erase(ndx);
15,774✔
981
        }
15,774✔
982
    }
42,930✔
983

984
    return node_size();
5,322,987✔
985
}
5,323,005✔
986

987
void Cluster::nullify_incoming_links(RowKey key, CascadeState& state)
988
{
5,192,310✔
989
    size_t ndx = get_ndx(key, 0);
5,192,310✔
990
    if (ndx == realm::npos)
5,192,310✔
991
        throw KeyNotFound(util::format("Key '%1' not found in '%2' when nullifying incoming links", key.value,
×
992
                                       get_owning_table()->get_class_name()));
×
993

994
    // We must start with backlink columns in case the corresponding link
995
    // columns are in the same table so that we can nullify links before
996
    // erasing rows in the link columns.
997
    //
998
    // This phase also generates replication instructions documenting the side-
999
    // effects of deleting the object (i.e. link nullifications). These instructions
1000
    // must come before the actual deletion of the object, but at the same time
1001
    // the Replication object may need a consistent view of the row (not including
1002
    // link columns). Therefore we first nullify links to this object, then
1003
    // generate the instruction, and then delete the row in the remaining columns.
1004

1005
    auto nullify_fwd_links = [&](ColKey col_key) {
5,192,310✔
1006
        ColKey::Idx leaf_ndx = col_key.get_index();
233,139✔
1007
        auto type = col_key.get_type();
233,139✔
1008
        REALM_ASSERT(type == col_type_BackLink);
233,139✔
1009
        ArrayBacklink values(m_alloc);
233,139✔
1010
        values.set_parent(this, leaf_ndx.val + s_first_col_index);
233,139✔
1011
        values.init_from_parent();
233,139✔
1012
        // Ensure that Cluster is writable and able to hold references to nodes in
1013
        // the slab area before nullifying or deleting links. These operation may
1014
        // both have the effect that other objects may be constructed and manipulated.
1015
        // If those other object are in the same cluster that the object to be deleted
1016
        // is in, then that will cause another accessor to this cluster to be created.
1017
        // It would lead to an error if the cluster node was relocated without it being
1018
        // reflected in the context here.
1019
        values.copy_on_write();
233,139✔
1020
        values.nullify_fwd_links(ndx, state);
233,139✔
1021

1022
        return IteratorControl::AdvanceToNext;
233,139✔
1023
    };
233,139✔
1024

1025
    m_tree_top.get_owning_table()->for_each_backlink_column(nullify_fwd_links);
5,192,310✔
1026
}
5,192,310✔
1027

1028
void Cluster::upgrade_string_to_enum(ColKey col_key, ArrayString& keys)
1029
{
1,089✔
1030
    auto col_ndx = col_key.get_index();
1,089✔
1031
    Array indexes(m_alloc);
1,089✔
1032
    indexes.create(Array::type_Normal, false);
1,089✔
1033
    ArrayString values(m_alloc);
1,089✔
1034
    ref_type ref = Array::get_as_ref(col_ndx.val + s_first_col_index);
1,089✔
1035
    values.init_from_ref(ref);
1,089✔
1036
    size_t sz = values.size();
1,089✔
1037
    for (size_t i = 0; i < sz; i++) {
119,073✔
1038
        auto v = values.get(i);
117,984✔
1039
        size_t pos = keys.lower_bound(v);
117,984✔
1040
        REALM_ASSERT_3(pos, !=, keys.size());
117,984✔
1041
        indexes.add(pos);
117,984✔
1042
    }
117,984✔
1043
    Array::set(col_ndx.val + s_first_col_index, indexes.get_ref());
1,089✔
1044
    Array::destroy_deep(ref, m_alloc);
1,089✔
1045
}
1,089✔
1046

1047
void Cluster::init_leaf(ColKey col_key, ArrayPayload* leaf) const
1048
{
8,473,569✔
1049
    auto col_ndx = col_key.get_index();
8,473,569✔
1050
    // FIXME: Move this validation into callers.
1051
    // Currently, the query subsystem may call with an unvalidated key.
1052
    // once fixed, reintroduce the noexcept declaration :-D
1053
    if (auto t = m_tree_top.get_owning_table())
8,473,569✔
1054
        t->check_column(col_key);
8,464,407✔
1055
    ref_type ref = to_ref(Array::get(col_ndx.val + 1));
8,473,569✔
1056
    if (leaf->need_spec()) {
8,473,569✔
1057
        m_tree_top.set_spec(*leaf, col_ndx);
132,966✔
1058
    }
132,966✔
1059
    leaf->init_from_ref(ref);
8,473,569✔
1060
    leaf->set_parent(const_cast<Cluster*>(this), col_ndx.val + 1);
8,473,569✔
1061
}
8,473,569✔
1062

1063
void Cluster::add_leaf(ColKey col_key, ref_type ref)
1064
{
×
1065
    auto col_ndx = col_key.get_index();
×
1066
    REALM_ASSERT((col_ndx.val + 1) == size());
×
1067
    Array::insert(col_ndx.val + 1, from_ref(ref));
×
1068
}
×
1069

1070
template <typename ArrayType>
1071
void Cluster::verify(ref_type ref, size_t index, util::Optional<size_t>& sz) const
1072
{
580,017✔
1073
    ArrayType arr(get_alloc());
580,017✔
1074
    set_spec(arr, ColKey::Idx{unsigned(index) - 1});
580,017✔
1075
    arr.set_parent(const_cast<Cluster*>(this), index);
580,017✔
1076
    arr.init_from_ref(ref);
580,017✔
1077
    arr.verify();
580,017✔
1078
    if (sz) {
580,017!
1079
        REALM_ASSERT(arr.size() == *sz);
224,406!
1080
    }
224,406✔
1081
    else {
355,611✔
1082
        sz = arr.size();
355,611✔
1083
    }
355,611✔
1084
}
580,017✔
1085
namespace {
1086

1087
template <typename ArrayType>
1088
void verify_list(ArrayRef& arr, size_t sz)
1089
{
81,786✔
1090
    for (size_t n = 0; n < sz; n++) {
260,628!
1091
        if (ref_type bp_tree_ref = arr.get(n)) {
178,842!
1092
            BPlusTree<ArrayType> links(arr.get_alloc());
85,902✔
1093
            links.init_from_ref(bp_tree_ref);
85,902✔
1094
            links.set_parent(&arr, n);
85,902✔
1095
            links.verify();
85,902✔
1096
        }
85,902✔
1097
    }
178,842✔
1098
}
81,786✔
1099

1100
template <typename SetType>
1101
void verify_set(ArrayRef& arr, size_t sz)
1102
{
174✔
1103
    for (size_t n = 0; n < sz; ++n) {
402!
1104
        if (ref_type bp_tree_ref = arr.get(n)) {
228!
1105
            BPlusTree<SetType> elements(arr.get_alloc());
216✔
1106
            elements.init_from_ref(bp_tree_ref);
216✔
1107
            elements.set_parent(&arr, n);
216✔
1108
            elements.verify();
216✔
1109

1110
            // FIXME: Check uniqueness of elements.
1111
        }
216✔
1112
    }
228✔
1113
}
174✔
1114

1115
} // namespace
1116

1117
void Cluster::verify() const
1118
{
392,118✔
1119
#ifdef REALM_DEBUG
392,118✔
1120
    util::Optional<size_t> sz;
392,118✔
1121

1122
    auto verify_column = [this, &sz](ColKey col_key) {
674,262✔
1123
        size_t col = col_key.get_index().val + s_first_col_index;
674,262✔
1124
        ref_type ref = Array::get_as_ref(col);
674,262✔
1125
        auto attr = col_key.get_attrs();
674,262✔
1126
        auto col_type = col_key.get_type();
674,262✔
1127
        bool nullable = attr.test(col_attr_Nullable);
674,262✔
1128

1129
        if (attr.test(col_attr_List)) {
674,262✔
1130
            ArrayRef arr(get_alloc());
81,807✔
1131
            arr.set_parent(const_cast<Cluster*>(this), col);
81,807✔
1132
            arr.init_from_ref(ref);
81,807✔
1133
            arr.verify();
81,807✔
1134
            if (sz) {
81,807✔
1135
                REALM_ASSERT(arr.size() == *sz);
57,558✔
1136
            }
57,558✔
1137
            else {
24,249✔
1138
                sz = arr.size();
24,249✔
1139
            }
24,249✔
1140

1141
            switch (col_type) {
81,807✔
1142
                case col_type_Int:
32,991✔
1143
                    if (nullable) {
32,991✔
1144
                        verify_list<util::Optional<int64_t>>(arr, *sz);
×
1145
                    }
×
1146
                    else {
32,991✔
1147
                        verify_list<int64_t>(arr, *sz);
32,991✔
1148
                    }
32,991✔
1149
                    break;
32,991✔
1150
                case col_type_Bool:
✔
1151
                    verify_list<Bool>(arr, *sz);
×
1152
                    break;
×
1153
                case col_type_Float:
6✔
1154
                    verify_list<Float>(arr, *sz);
6✔
1155
                    break;
6✔
1156
                case col_type_Double:
✔
1157
                    verify_list<Double>(arr, *sz);
×
1158
                    break;
×
1159
                case col_type_String:
23,709✔
1160
                    verify_list<String>(arr, *sz);
23,709✔
1161
                    break;
23,709✔
1162
                case col_type_Binary:
23,997✔
1163
                    verify_list<Binary>(arr, *sz);
23,997✔
1164
                    break;
23,997✔
1165
                case col_type_Timestamp:
✔
1166
                    verify_list<Timestamp>(arr, *sz);
×
1167
                    break;
×
1168
                case col_type_Decimal:
✔
1169
                    verify_list<Decimal128>(arr, *sz);
×
1170
                    break;
×
1171
                case col_type_ObjectId:
✔
1172
                    verify_list<ObjectId>(arr, *sz);
×
1173
                    break;
×
1174
                case col_type_UUID:
✔
1175
                    verify_list<UUID>(arr, *sz);
×
1176
                    break;
×
1177
                case col_type_Link:
1,086✔
1178
                    verify_list<ObjKey>(arr, *sz);
1,086✔
1179
                    break;
1,086✔
1180
                default:
18✔
1181
                    // FIXME: Nullable primitives
1182
                    break;
18✔
1183
            }
81,807✔
1184
            return IteratorControl::AdvanceToNext;
81,810✔
1185
        }
81,807✔
1186
        else if (attr.test(col_attr_Dictionary)) {
592,455✔
1187
            ArrayRef arr(get_alloc());
12,168✔
1188
            arr.set_parent(const_cast<Cluster*>(this), col);
12,168✔
1189
            arr.init_from_ref(ref);
12,168✔
1190
            arr.verify();
12,168✔
1191
            if (sz) {
12,168✔
1192
                REALM_ASSERT(arr.size() == *sz);
138✔
1193
            }
138✔
1194
            else {
12,030✔
1195
                sz = arr.size();
12,030✔
1196
            }
12,030✔
1197
            for (size_t n = 0; n < sz; n++) {
24,504✔
1198
                if (auto ref = arr.get(n)) {
12,336✔
1199
                    Dictionary dict(get_alloc(), col_key, to_ref(ref));
12,219✔
1200
                    dict.verify();
12,219✔
1201
                }
12,219✔
1202
            }
12,336✔
1203
            return IteratorControl::AdvanceToNext;
12,168✔
1204
        }
12,168✔
1205
        else if (attr.test(col_attr_Set)) {
580,287✔
1206
            ArrayRef arr(get_alloc());
270✔
1207
            arr.set_parent(const_cast<Cluster*>(this), col);
270✔
1208
            arr.init_from_ref(ref);
270✔
1209
            arr.verify();
270✔
1210
            if (sz) {
270✔
1211
                REALM_ASSERT(arr.size() == *sz);
252✔
1212
            }
252✔
1213
            else {
18✔
1214
                sz = arr.size();
18✔
1215
            }
18✔
1216
            switch (col_type) {
270✔
1217
                case col_type_Int:
138✔
1218
                    if (nullable) {
138✔
1219
                        verify_set<util::Optional<int64_t>>(arr, *sz);
×
1220
                    }
×
1221
                    else {
138✔
1222
                        verify_set<int64_t>(arr, *sz);
138✔
1223
                    }
138✔
1224
                    break;
138✔
1225
                case col_type_Bool:
✔
1226
                    verify_set<Bool>(arr, *sz);
×
1227
                    break;
×
1228
                case col_type_Float:
✔
1229
                    verify_set<Float>(arr, *sz);
×
1230
                    break;
×
1231
                case col_type_Double:
✔
1232
                    verify_set<Double>(arr, *sz);
×
1233
                    break;
×
1234
                case col_type_String:
6✔
1235
                    verify_set<String>(arr, *sz);
6✔
1236
                    break;
6✔
1237
                case col_type_Binary:
18✔
1238
                    verify_set<Binary>(arr, *sz);
18✔
1239
                    break;
18✔
1240
                case col_type_Timestamp:
✔
1241
                    verify_set<Timestamp>(arr, *sz);
×
1242
                    break;
×
1243
                case col_type_Decimal:
✔
1244
                    verify_set<Decimal128>(arr, *sz);
×
1245
                    break;
×
1246
                case col_type_ObjectId:
✔
1247
                    verify_set<ObjectId>(arr, *sz);
×
1248
                    break;
×
1249
                case col_type_UUID:
✔
1250
                    verify_set<UUID>(arr, *sz);
×
1251
                    break;
×
1252
                case col_type_Link:
12✔
1253
                    verify_set<ObjKey>(arr, *sz);
12✔
1254
                    break;
12✔
1255
                default:
96✔
1256
                    // FIXME: Nullable primitives
1257
                    break;
96✔
1258
            }
270✔
1259
            return IteratorControl::AdvanceToNext;
270✔
1260
        }
270✔
1261

1262
        switch (col_type) {
580,017✔
1263
            case col_type_Int:
343,899✔
1264
                if (nullable) {
343,899✔
1265
                    verify<ArrayIntNull>(ref, col, sz);
31,395✔
1266
                }
31,395✔
1267
                else {
312,504✔
1268
                    verify<ArrayInteger>(ref, col, sz);
312,504✔
1269
                }
312,504✔
1270
                break;
343,899✔
1271
            case col_type_Bool:
7,164✔
1272
                verify<ArrayBoolNull>(ref, col, sz);
7,164✔
1273
                break;
7,164✔
1274
            case col_type_Float:
252✔
1275
                verify<ArrayFloatNull>(ref, col, sz);
252✔
1276
                break;
252✔
1277
            case col_type_Double:
174✔
1278
                verify<ArrayDoubleNull>(ref, col, sz);
174✔
1279
                break;
174✔
1280
            case col_type_String:
164,094✔
1281
                verify<ArrayString>(ref, col, sz);
164,094✔
1282
                break;
164,094✔
1283
            case col_type_Binary:
49,941✔
1284
                verify<ArrayBinary>(ref, col, sz);
49,941✔
1285
                break;
49,941✔
1286
            case col_type_Mixed:
2,688✔
1287
                verify<ArrayMixed>(ref, col, sz);
2,688✔
1288
                break;
2,688✔
1289
            case col_type_Timestamp:
6,948✔
1290
                verify<ArrayTimestamp>(ref, col, sz);
6,948✔
1291
                break;
6,948✔
1292
            case col_type_Decimal:
42✔
1293
                verify<ArrayDecimal128>(ref, col, sz);
42✔
1294
                break;
42✔
1295
            case col_type_ObjectId:
42✔
1296
                verify<ArrayObjectIdNull>(ref, col, sz);
42✔
1297
                break;
42✔
1298
            case col_type_UUID:
✔
1299
                verify<ArrayUUIDNull>(ref, col, sz);
×
1300
                break;
×
1301
            case col_type_Link:
1,941✔
1302
                verify<ArrayKey>(ref, col, sz);
1,941✔
1303
                break;
1,941✔
1304
            case col_type_BackLink:
2,835✔
1305
                verify<ArrayBacklink>(ref, col, sz);
2,835✔
1306
                break;
2,835✔
1307
            default:
✔
1308
                break;
×
1309
        }
580,017✔
1310
        return IteratorControl::AdvanceToNext;
580,023✔
1311
    };
580,017✔
1312

1313
    m_tree_top.m_owner->for_each_and_every_column(verify_column);
392,118✔
1314
#endif
392,118✔
1315
}
392,118✔
1316

1317
// LCOV_EXCL_START
1318
void Cluster::dump_objects(int64_t key_offset, std::string lead) const
1319
{
×
1320
    std::cout << lead << "leaf - size: " << node_size() << std::endl;
×
1321
    if (!m_keys.is_attached()) {
×
1322
        std::cout << lead << "compact form" << std::endl;
×
1323
    }
×
1324

1325
    for (unsigned i = 0; i < node_size(); i++) {
×
1326
        int64_t key_value;
×
1327
        if (m_keys.is_attached()) {
×
1328
            key_value = m_keys.get(i);
×
1329
        }
×
1330
        else {
×
1331
            key_value = int64_t(i);
×
1332
        }
×
1333
        std::cout << lead << "key: " << std::hex << key_value + key_offset << std::dec;
×
1334
        m_tree_top.m_owner->for_each_and_every_column([&](ColKey col) {
×
1335
            size_t j = col.get_index().val + 1;
×
1336
            if (col.get_attrs().test(col_attr_List)) {
×
1337
                ref_type ref = Array::get_as_ref(j);
×
1338
                ArrayRef refs(m_alloc);
×
1339
                refs.init_from_ref(ref);
×
1340
                std::cout << ", {";
×
1341
                ref = refs.get(i);
×
1342
                if (ref) {
×
1343
                    if (col.get_type() == col_type_Int) {
×
1344
                        // This is easy to handle
1345
                        Array ints(m_alloc);
×
1346
                        ints.init_from_ref(ref);
×
1347
                        for (size_t n = 0; n < ints.size(); n++) {
×
1348
                            std::cout << ints.get(n) << ", ";
×
1349
                        }
×
1350
                    }
×
1351
                    else {
×
1352
                        std::cout << col.get_type();
×
1353
                    }
×
1354
                }
×
1355
                std::cout << "}";
×
1356
                return IteratorControl::AdvanceToNext;
×
1357
            }
×
1358

1359
            switch (col.get_type()) {
×
1360
                case col_type_Int: {
×
1361
                    bool nullable = col.get_attrs().test(col_attr_Nullable);
×
1362
                    ref_type ref = Array::get_as_ref(j);
×
1363
                    if (nullable) {
×
1364
                        ArrayIntNull arr_int_null(m_alloc);
×
1365
                        arr_int_null.init_from_ref(ref);
×
1366
                        if (arr_int_null.is_null(i)) {
×
1367
                            std::cout << ", null";
×
1368
                        }
×
1369
                        else {
×
1370
                            std::cout << ", " << *arr_int_null.get(i);
×
1371
                        }
×
1372
                    }
×
1373
                    else {
×
1374
                        Array arr(m_alloc);
×
1375
                        arr.init_from_ref(ref);
×
1376
                        std::cout << ", " << arr.get(i);
×
1377
                    }
×
1378
                    break;
×
1379
                }
×
1380
                case col_type_Bool: {
×
1381
                    ArrayBoolNull arr(m_alloc);
×
1382
                    ref_type ref = Array::get_as_ref(j);
×
1383
                    arr.init_from_ref(ref);
×
1384
                    auto val = arr.get(i);
×
1385
                    std::cout << ", " << (val ? (*val ? "true" : "false") : "null");
×
1386
                    break;
×
1387
                }
×
1388
                case col_type_Float: {
×
1389
                    ArrayFloatNull arr(m_alloc);
×
1390
                    ref_type ref = Array::get_as_ref(j);
×
1391
                    arr.init_from_ref(ref);
×
1392
                    auto val = arr.get(i);
×
1393
                    if (val)
×
1394
                        std::cout << ", " << *val;
×
1395
                    else
×
1396
                        std::cout << ", null";
×
1397
                    break;
×
1398
                }
×
1399
                case col_type_Double: {
×
1400
                    ArrayDoubleNull arr(m_alloc);
×
1401
                    ref_type ref = Array::get_as_ref(j);
×
1402
                    arr.init_from_ref(ref);
×
1403
                    auto val = arr.get(i);
×
1404
                    if (val)
×
1405
                        std::cout << ", " << *val;
×
1406
                    else
×
1407
                        std::cout << ", null";
×
1408
                    break;
×
1409
                }
×
1410
                case col_type_String: {
×
1411
                    ArrayString arr(m_alloc);
×
1412
                    set_spec(arr, col.get_index());
×
1413
                    ref_type ref = Array::get_as_ref(j);
×
1414
                    arr.init_from_ref(ref);
×
1415
                    std::cout << ", " << arr.get(i);
×
1416
                    break;
×
1417
                }
×
1418
                case col_type_Binary: {
×
1419
                    ArrayBinary arr(m_alloc);
×
1420
                    ref_type ref = Array::get_as_ref(j);
×
1421
                    arr.init_from_ref(ref);
×
1422
                    std::cout << ", " << arr.get(i);
×
1423
                    break;
×
1424
                }
×
1425
                case col_type_Mixed: {
×
1426
                    ArrayMixed arr(m_alloc);
×
1427
                    ref_type ref = Array::get_as_ref(j);
×
1428
                    arr.init_from_ref(ref);
×
1429
                    std::cout << ", " << arr.get(i);
×
1430
                    break;
×
1431
                }
×
1432
                case col_type_Timestamp: {
×
1433
                    ArrayTimestamp arr(m_alloc);
×
1434
                    ref_type ref = Array::get_as_ref(j);
×
1435
                    arr.init_from_ref(ref);
×
1436
                    if (arr.is_null(i)) {
×
1437
                        std::cout << ", null";
×
1438
                    }
×
1439
                    else {
×
1440
                        std::cout << ", " << arr.get(i);
×
1441
                    }
×
1442
                    break;
×
1443
                }
×
1444
                case col_type_Decimal: {
×
1445
                    ArrayDecimal128 arr(m_alloc);
×
1446
                    ref_type ref = Array::get_as_ref(j);
×
1447
                    arr.init_from_ref(ref);
×
1448
                    if (arr.is_null(i)) {
×
1449
                        std::cout << ", null";
×
1450
                    }
×
1451
                    else {
×
1452
                        std::cout << ", " << arr.get(i);
×
1453
                    }
×
1454
                    break;
×
1455
                }
×
1456
                case col_type_ObjectId: {
×
1457
                    ArrayObjectIdNull arr(m_alloc);
×
1458
                    ref_type ref = Array::get_as_ref(j);
×
1459
                    arr.init_from_ref(ref);
×
1460
                    if (arr.is_null(i)) {
×
1461
                        std::cout << ", null";
×
1462
                    }
×
1463
                    else {
×
1464
                        std::cout << ", " << *arr.get(i);
×
1465
                    }
×
1466
                    break;
×
1467
                }
×
1468
                case col_type_UUID: {
×
1469
                    ArrayUUIDNull arr(m_alloc);
×
1470
                    ref_type ref = Array::get_as_ref(j);
×
1471
                    arr.init_from_ref(ref);
×
1472
                    if (arr.is_null(i)) {
×
1473
                        std::cout << ", null";
×
1474
                    }
×
1475
                    else {
×
1476
                        std::cout << ", " << arr.get(i);
×
1477
                    }
×
1478
                    break;
×
1479
                }
×
1480
                case col_type_Link: {
×
1481
                    ArrayKey arr(m_alloc);
×
1482
                    ref_type ref = Array::get_as_ref(j);
×
1483
                    arr.init_from_ref(ref);
×
1484
                    std::cout << ", " << arr.get(i);
×
1485
                    break;
×
1486
                }
×
1487
                case col_type_BackLink: {
×
1488
                    break;
×
1489
                }
×
1490
                default:
×
1491
                    std::cout << ", Error";
×
1492
                    break;
×
1493
            }
×
1494
            return IteratorControl::AdvanceToNext;
×
1495
        });
×
1496
        std::cout << std::endl;
×
1497
    }
×
1498
}
×
1499
// LCOV_EXCL_STOP
1500

1501
void Cluster::remove_backlinks(const Table* origin_table, ObjKey origin_key, ColKey origin_col_key,
1502
                               const std::vector<ObjKey>& keys, CascadeState& state)
1503
{
91,887✔
1504
    TableRef target_table = origin_table->get_opposite_table(origin_col_key);
91,887✔
1505
    ColKey backlink_col_key = origin_table->get_opposite_column(origin_col_key);
91,887✔
1506
    bool strong_links = target_table->is_embedded();
91,887✔
1507

1508
    for (auto key : keys) {
93,594✔
1509
        if (key != null_key) {
93,594✔
1510
            bool is_unres = key.is_unresolved();
93,594✔
1511
            Obj target_obj = is_unres ? target_table->m_tombstones->get(key) : target_table->m_clusters.get(key);
93,594✔
1512
            bool last_removed = target_obj.remove_one_backlink(backlink_col_key, origin_key); // Throws
93,594✔
1513
            if (is_unres) {
93,594✔
1514
                if (last_removed) {
30✔
1515
                    // Check is there are more backlinks
1516
                    if (!target_obj.has_backlinks(false)) {
24✔
1517
                        // Tombstones can be erased right away - there is no cascading effect
1518
                        target_table->m_tombstones->erase(key, state);
18✔
1519
                    }
18✔
1520
                }
24✔
1521
            }
30✔
1522
            else {
93,564✔
1523
                state.enqueue_for_cascade(target_obj, strong_links, last_removed);
93,564✔
1524
            }
93,564✔
1525
        }
93,594✔
1526
    }
93,594✔
1527
}
91,887✔
1528

1529
void Cluster::remove_backlinks(const Table* origin_table, ObjKey origin_key, ColKey origin_col_key,
1530
                               const std::vector<ObjLink>& links, CascadeState& state)
1531
{
144✔
1532
    Group* group = origin_table->get_parent_group();
144✔
1533
    TableKey origin_table_key = origin_table->get_key();
144✔
1534

1535
    for (auto link : links) {
240✔
1536
        if (link) {
240✔
1537
            bool is_unres = link.get_obj_key().is_unresolved();
240✔
1538
            Obj target_obj = group->get_object(link);
240✔
1539
            TableRef target_table = target_obj.get_table();
240✔
1540
            ColKey backlink_col_key = target_table->find_or_add_backlink_column(origin_col_key, origin_table_key);
240✔
1541

1542
            bool last_removed = target_obj.remove_one_backlink(backlink_col_key, origin_key); // Throws
240✔
1543
            if (is_unres) {
240✔
1544
                if (last_removed) {
6✔
1545
                    // Check if there are more backlinks
1546
                    if (!target_obj.has_backlinks(false)) {
6✔
1547
                        // Tombstones can be erased right away - there is no cascading effect
1548
                        target_table->m_tombstones->erase(link.get_obj_key(), state);
6✔
1549
                    }
6✔
1550
                }
6✔
1551
            }
6✔
1552
            else {
234✔
1553
                state.enqueue_for_cascade(target_obj, false, last_removed);
234✔
1554
            }
234✔
1555
        }
240✔
1556
    }
240✔
1557
}
144✔
1558

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

© 2025 Coveralls, Inc