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

realm / realm-core / 2439

20 Jun 2024 08:47PM UTC coverage: 90.962% (+0.006%) from 90.956%
2439

push

Evergreen

web-flow
RCORE-2172 Fix race condition in "unregister connection change listener" test while listener is being unregistered (#7824)

* Fixed race in 'unregister connection change listener' test

* Updated test per review

* Updates from review

102170 of 180382 branches covered (56.64%)

30 of 31 new or added lines in 1 file covered. (96.77%)

60 existing lines in 10 files now uncovered.

214712 of 236047 relevant lines covered (90.96%)

5336875.2 hits per line

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

75.6
/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)
230,799✔
46
{
483,606✔
47
    if (m_values.size() > 1) {
483,606✔
48
        // Sort according to ColKey index
49
        std::sort(m_values.begin(), m_values.end(), [](const auto& a, const auto& b) {
53,463✔
50
            return a.col_key.get_index().val < b.col_key.get_index().val;
53,463✔
51
        });
53,463✔
52
    }
17,856✔
53
}
483,606✔
54

55
void FieldValues::insert(ColKey k, Mixed val, bool is_default)
56
{
440,373✔
57
    if (m_values.empty()) {
440,373✔
58
        m_values.emplace_back(k, val, is_default);
427,830✔
59
        return;
427,830✔
60
    }
427,830✔
61
    unsigned int idx = k.get_index().val;
12,543✔
62
    auto it = std::lower_bound(m_values.begin(), m_values.end(), idx, [](const auto& a, unsigned int i) {
24,330✔
63
        return a.col_key.get_index().val < i;
24,330✔
64
    });
24,330✔
65
    m_values.insert(it, {k, val, is_default});
12,543✔
66
}
12,543✔
67

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

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

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

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

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

97

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

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

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

109

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

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

125
    auto column_initialize = [this](ColKey col_key) {
188,076✔
126
        auto col_ndx = col_key.get_index();
188,076✔
127
        while (size() <= col_ndx.val + 1)
376,152✔
128
            add(0);
188,076✔
129
        auto type = col_key.get_type();
188,076✔
130
        auto attr = col_key.get_attrs();
188,076✔
131
        if (attr.test(col_attr_Collection)) {
188,076✔
132
            ArrayRef arr(m_alloc);
6,174✔
133
            arr.create();
6,174✔
134
            arr.set_parent(this, col_ndx.val + s_first_col_index);
6,174✔
135
            arr.update_parent();
6,174✔
136
            return IteratorControl::AdvanceToNext;
6,174✔
137
        }
6,174✔
138
        switch (type) {
181,902✔
139
            case col_type_Int:
111,165✔
140
                if (attr.test(col_attr_Nullable)) {
111,165✔
141
                    do_create<ArrayIntNull>(col_key);
10,959✔
142
                }
10,959✔
143
                else {
100,206✔
144
                    do_create<ArrayInteger>(col_key);
100,206✔
145
                }
100,206✔
146
                break;
111,165✔
147
            case col_type_Bool:
684✔
148
                do_create<ArrayBoolNull>(col_key);
684✔
149
                break;
684✔
150
            case col_type_Float:
1,110✔
151
                do_create<ArrayFloatNull>(col_key);
1,110✔
152
                break;
1,110✔
153
            case col_type_Double:
5,793✔
154
                do_create<ArrayDoubleNull>(col_key);
5,793✔
155
                break;
5,793✔
156
            case col_type_String: {
46,503✔
157
                if (m_tree_top.is_string_enum_type(col_ndx)) {
46,503✔
158
                    do_create<ArrayInteger>(col_key);
5,340✔
159
                }
5,340✔
160
                else {
41,163✔
161
                    do_create<ArrayString>(col_key);
41,163✔
162
                }
41,163✔
163
                break;
46,503✔
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:
108✔
169
                do_create<ArrayMixed>(col_key);
108✔
170
                break;
108✔
171
            case col_type_Timestamp:
2,466✔
172
                do_create<ArrayTimestamp>(col_key);
2,466✔
173
                break;
2,466✔
174
            case col_type_Decimal:
276✔
175
                do_create<ArrayDecimal128>(col_key);
276✔
176
                break;
276✔
177
            case col_type_ObjectId:
2,889✔
178
                do_create<ArrayObjectIdNull>(col_key);
2,889✔
179
                break;
2,889✔
180
            case col_type_UUID:
408✔
181
                do_create<ArrayUUIDNull>(col_key);
408✔
182
                break;
408✔
183
            case col_type_Link:
2,190✔
184
                do_create<ArrayKey>(col_key);
2,190✔
185
                break;
2,190✔
186
            case col_type_TypedLink:
✔
187
                do_create<ArrayTypedLink>(col_key);
×
188
                break;
×
189
            case col_type_BackLink:
3,030✔
190
                do_create<ArrayBacklink>(col_key);
3,030✔
191
                break;
3,030✔
192
            default:
✔
193
                REALM_UNREACHABLE();
194
        }
181,902✔
195
        return IteratorControl::AdvanceToNext;
181,890✔
196
    };
181,902✔
197
    m_tree_top.m_owner->for_each_and_every_column(column_initialize);
95,718✔
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);
95,718✔
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();
95,718✔
205
}
95,718✔
206

207
void Cluster::init(MemRef mem)
208
{
204,403,413✔
209
    Array::init_from_mem(mem);
204,403,413✔
210
    auto rot = Array::get_as_ref_or_tagged(0);
204,403,413✔
211
    if (rot.is_tagged()) {
204,403,413✔
212
        m_keys.detach();
143,181,927✔
213
    }
143,181,927✔
214
    else {
61,221,486✔
215
        m_keys.init_from_ref(rot.get_as_ref());
61,221,486✔
216
    }
61,221,486✔
217
}
204,403,413✔
218

219
void Cluster::update_from_parent() noexcept
220
{
665,472✔
221
    Array::update_from_parent();
665,472✔
222
    auto rot = Array::get_as_ref_or_tagged(0);
665,472✔
223
    if (!rot.is_tagged()) {
665,472✔
224
        m_keys.update_from_parent();
23,925✔
225
    }
23,925✔
226
}
665,472✔
227

228
MemRef Cluster::ensure_writeable(ObjKey)
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(ObjKey, ref_type)
238
{
×
239
    REALM_UNREACHABLE();
240
}
×
241

242
size_t Cluster::node_size_from_header(Allocator& alloc, const char* header)
243
{
71,617,155✔
244
    auto rot = Array::get_as_ref_or_tagged(header, s_key_ref_or_size_index);
71,617,155✔
245
    if (rot.is_tagged()) {
71,652,861✔
246
        return size_t(rot.get_as_int());
71,513,361✔
247
    }
71,513,361✔
248
    else {
2,147,623,147✔
249
        return Array::get_size_from_header(alloc.translate(rot.get_as_ref()));
2,147,623,147✔
250
    }
2,147,623,147✔
251
}
71,617,155✔
252

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

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

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

269
    T arr(m_alloc);
32,695,350✔
270
    auto col_ndx = col.get_index();
32,695,350✔
271
    arr.set_parent(this, col_ndx.val + s_first_col_index);
32,695,350✔
272
    set_spec<T>(arr, col_ndx);
32,695,350✔
273
    arr.init_from_parent();
32,695,350✔
274
    if (init_val.is_null()) {
32,695,350✔
275
        arr.insert(ndx, T::default_value(nullable));
32,090,082✔
276
    }
32,090,082✔
277
    else {
605,268✔
278
        arr.insert(ndx, init_val.get<U>());
605,268✔
279
    }
605,268✔
280
}
32,695,350✔
281

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

291
    // Insert backlink if link is not null
292
    if (target_key) {
259,923✔
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
}
259,923✔
300

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

308
    // Insert backlink if needed
309
    if (init_value.is_type(type_TypedLink)) {
22,308✔
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
}
22,308✔
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, ObjKey k, const FieldValues& init_values)
347
{
23,298,693✔
348
    // Ensure the cluster array is big enough to hold 64 bit values.
349
    copy_on_write(m_size * 8);
23,298,693✔
350

351
    if (m_keys.is_attached()) {
23,298,693✔
352
        m_keys.insert(ndx, k.value);
1,582,740✔
353
    }
1,582,740✔
354
    else {
21,715,953✔
355
        Array::set(s_key_ref_or_size_index, Array::get(s_key_ref_or_size_index) + 2); // Increments size by 1
21,715,953✔
356
    }
21,715,953✔
357

358
    auto val = init_values.begin();
23,298,693✔
359
    auto insert_in_column = [&](ColKey col_key) {
34,536,795✔
360
        auto col_ndx = col_key.get_index();
34,536,795✔
361
        auto attr = col_key.get_attrs();
34,536,795✔
362
        Mixed init_value;
34,536,795✔
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) {
34,536,795✔
365
            init_value = val->value;
506,736✔
366
            ++val;
506,736✔
367
        }
506,736✔
368

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

379
        bool nullable = attr.test(col_attr_Nullable);
33,858,027✔
380
        switch (type) {
33,858,027✔
381
            case col_type_Int:
26,119,344✔
382
                if (attr.test(col_attr_Nullable)) {
26,119,344✔
383
                    do_insert_row<ArrayIntNull>(ndx, col_key, init_value, nullable);
2,176,683✔
384
                }
2,176,683✔
385
                else {
23,942,661✔
386
                    do_insert_row<ArrayInteger>(ndx, col_key, init_value, nullable);
23,942,661✔
387
                }
23,942,661✔
388
                break;
26,119,344✔
389
            case col_type_Bool:
187,002✔
390
                do_insert_row<ArrayBoolNull>(ndx, col_key, init_value, nullable);
187,002✔
391
                break;
187,002✔
392
            case col_type_Float:
325,524✔
393
                do_insert_row<ArrayFloatNull>(ndx, col_key, init_value, nullable);
325,524✔
394
                break;
325,524✔
395
            case col_type_Double:
1,493,133✔
396
                do_insert_row<ArrayDoubleNull>(ndx, col_key, init_value, nullable);
1,493,133✔
397
                break;
1,493,133✔
398
            case col_type_String:
2,604,261✔
399
                do_insert_row<ArrayString>(ndx, col_key, init_value, nullable);
2,604,261✔
400
                break;
2,604,261✔
401
            case col_type_Binary:
1,364,217✔
402
                do_insert_row<ArrayBinary>(ndx, col_key, init_value, nullable);
1,364,217✔
403
                break;
1,364,217✔
404
            case col_type_Mixed: {
22,308✔
405
                do_insert_mixed(ndx, col_key, init_value, ObjKey(k.value + get_offset()));
22,308✔
406
                break;
22,308✔
407
            }
×
408
            case col_type_Timestamp:
225,105✔
409
                do_insert_row<ArrayTimestamp>(ndx, col_key, init_value, nullable);
225,105✔
410
                break;
225,105✔
411
            case col_type_Decimal:
78,222✔
412
                do_insert_row<ArrayDecimal128>(ndx, col_key, init_value, nullable);
78,222✔
413
                break;
78,222✔
414
            case col_type_ObjectId:
173,478✔
415
                do_insert_row<ArrayObjectIdNull>(ndx, col_key, init_value, nullable);
173,478✔
416
                break;
173,478✔
417
            case col_type_UUID:
119,652✔
418
                do_insert_row<ArrayUUIDNull>(ndx, col_key, init_value, nullable);
119,652✔
419
                break;
119,652✔
420
            case col_type_Link:
259,923✔
421
                do_insert_key(ndx, col_key, init_value, ObjKey(k.value + get_offset()));
259,923✔
422
                break;
259,923✔
423
            case col_type_TypedLink:
✔
424
                do_insert_link(ndx, col_key, init_value, ObjKey(k.value + get_offset()));
×
425
                break;
×
426
            case col_type_BackLink: {
962,730✔
427
                ArrayBacklink arr(m_alloc);
962,730✔
428
                arr.set_parent(this, col_ndx.val + s_first_col_index);
962,730✔
429
                arr.init_from_parent();
962,730✔
430
                arr.insert(ndx, 0);
962,730✔
431
                break;
962,730✔
432
            }
×
433
            default:
✔
434
                REALM_ASSERT(false);
×
435
                break;
×
436
        }
33,858,027✔
437
        return IteratorControl::AdvanceToNext;
33,752,655✔
438
    };
33,858,027✔
439
    m_tree_top.m_owner->for_each_and_every_column(insert_in_column);
23,298,693✔
440
}
23,298,693✔
441

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

450
    T dst(m_alloc);
21,990✔
451
    dst.set_parent(to, col_ndx);
21,990✔
452
    dst.init_from_parent();
21,990✔
453

454
    src.move(dst, ndx);
21,990✔
455
}
21,990✔
456

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

461
    auto move_from_column = [&](ColKey col_key) {
21,990✔
462
        auto attr = col_key.get_attrs();
21,990✔
463
        auto type = col_key.get_type();
21,990✔
464

465
        if (attr.test(col_attr_Collection)) {
21,990✔
466
            do_move<ArrayRef>(ndx, col_key, new_leaf);
30✔
467
            return IteratorControl::AdvanceToNext;
30✔
468
        }
30✔
469

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

535
Cluster::~Cluster() {}
209,565,267✔
536

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

545
void Cluster::ensure_general_form()
546
{
61,719✔
547
    if (!m_keys.is_attached()) {
61,719✔
548
        size_t current_size = get_size_in_compact_form();
48,342✔
549
        m_keys.create(current_size, 255);
48,342✔
550
        m_keys.update_parent();
48,342✔
551
        for (size_t i = 0; i < current_size; i++) {
5,516,619✔
552
            m_keys.set(i, i);
5,468,277✔
553
        }
5,468,277✔
554
    }
48,342✔
555
}
61,719✔
556

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

562
    T arr(m_alloc);
610,623✔
563
    arr.create();
610,623✔
564
    auto val = T::default_value(nullable);
610,623✔
565
    for (size_t i = 0; i < sz; i++) {
6,851,958✔
566
        arr.add(val);
6,241,335✔
567
    }
6,241,335✔
568
    auto col_ndx = col_key.get_index();
610,623✔
569
    unsigned ndx = col_ndx.val + s_first_col_index;
610,623✔
570

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

575
    if (ndx == size())
610,623✔
576
        Array::insert(ndx, from_ref(arr.get_ref()));
610,527✔
577
    else
96✔
578
        Array::set(ndx, from_ref(arr.get_ref()));
96✔
579
}
610,623✔
580

581
void Cluster::insert_column(ColKey col_key)
582
{
805,458✔
583
    auto attr = col_key.get_attrs();
805,458✔
584
    auto type = col_key.get_type();
805,458✔
585
    if (attr.test(col_attr_Collection)) {
805,458✔
586
        size_t sz = node_size();
194,838✔
587

588
        ArrayRef arr(m_alloc);
194,838✔
589
        arr.create(sz);
194,838✔
590
        auto col_ndx = col_key.get_index();
194,838✔
591
        unsigned idx = col_ndx.val + s_first_col_index;
194,838✔
592
        if (idx == size())
194,838✔
593
            Array::insert(idx, from_ref(arr.get_ref()));
194,838✔
UNCOV
594
        else
×
UNCOV
595
            Array::set(idx, from_ref(arr.get_ref()));
×
596
        return;
194,838✔
597
    }
194,838✔
598
    bool nullable = attr.test(col_attr_Nullable);
610,620✔
599
    switch (type) {
610,620✔
600
        case col_type_Int:
274,812✔
601
            if (nullable) {
274,812✔
602
                do_insert_column<ArrayIntNull>(col_key, nullable);
32,736✔
603
            }
32,736✔
604
            else {
242,076✔
605
                do_insert_column<ArrayInteger>(col_key, nullable);
242,076✔
606
            }
242,076✔
607
            break;
274,812✔
608
        case col_type_Bool:
5,478✔
609
            do_insert_column<ArrayBoolNull>(col_key, nullable);
5,478✔
610
            break;
5,478✔
611
        case col_type_Float:
6,435✔
612
            do_insert_column<ArrayFloatNull>(col_key, nullable);
6,435✔
613
            break;
6,435✔
614
        case col_type_Double:
7,080✔
615
            do_insert_column<ArrayDoubleNull>(col_key, nullable);
7,080✔
616
            break;
7,080✔
617
        case col_type_String:
101,094✔
618
            do_insert_column<ArrayString>(col_key, nullable);
101,094✔
619
            break;
101,094✔
620
        case col_type_Binary:
6,888✔
621
            do_insert_column<ArrayBinary>(col_key, nullable);
6,888✔
622
            break;
6,888✔
623
        case col_type_Mixed:
9,036✔
624
            do_insert_column<ArrayMixed>(col_key, nullable);
9,036✔
625
            break;
9,036✔
626
        case col_type_Timestamp:
23,637✔
627
            do_insert_column<ArrayTimestamp>(col_key, nullable);
23,637✔
628
            break;
23,637✔
629
        case col_type_Decimal:
5,022✔
630
            do_insert_column<ArrayDecimal128>(col_key, nullable);
5,022✔
631
            break;
5,022✔
632
        case col_type_ObjectId:
50,577✔
633
            do_insert_column<ArrayObjectIdNull>(col_key, nullable);
50,577✔
634
            break;
50,577✔
635
        case col_type_UUID:
5,616✔
636
            do_insert_column<ArrayUUIDNull>(col_key, nullable);
5,616✔
637
            break;
5,616✔
638
        case col_type_Link:
31,134✔
639
            do_insert_column<ArrayKey>(col_key, nullable);
31,134✔
640
            break;
31,134✔
641
        case col_type_TypedLink:
✔
642
            do_insert_column<ArrayTypedLink>(col_key, nullable);
×
643
            break;
×
644
        case col_type_BackLink:
83,814✔
645
            do_insert_column<ArrayBacklink>(col_key, nullable);
83,814✔
646
            break;
83,814✔
647
        default:
✔
648
            REALM_UNREACHABLE();
649
            break;
×
650
    }
610,620✔
651
}
610,620✔
652

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

667
ref_type Cluster::insert(ObjKey k, const FieldValues& init_values, ClusterNode::State& state)
668
{
23,296,803✔
669
    int64_t current_key_value = -1;
23,296,803✔
670
    size_t sz;
23,296,803✔
671
    size_t ndx;
23,296,803✔
672
    ref_type ret = 0;
23,296,803✔
673

674
    auto on_error = [&] {
23,296,803✔
675
        throw KeyAlreadyUsed(
15✔
676
            util::format("When inserting key '%1' in '%2'", k.value, get_owning_table()->get_name()));
15✔
677
    };
15✔
678

679
    if (m_keys.is_attached()) {
23,296,803✔
680
        sz = m_keys.size();
1,571,526✔
681
        ndx = m_keys.lower_bound(uint64_t(k.value));
1,571,526✔
682
        if (ndx < sz) {
1,571,526✔
683
            current_key_value = m_keys.get(ndx);
617,061✔
684
            if (k.value == current_key_value) {
617,061✔
685
                on_error();
15✔
686
            }
15✔
687
        }
617,061✔
688
    }
1,571,526✔
689
    else {
21,725,277✔
690
        sz = size_t(Array::get(s_key_ref_or_size_index)) >> 1; // Size is stored as tagged integer
21,725,277✔
691
        if (uint64_t(k.value) < sz) {
21,725,277✔
692
            on_error();
×
693
        }
×
694
        // Key value is bigger than all other values, should be put last
695
        ndx = sz;
21,725,277✔
696
        if (uint64_t(k.value) > sz && sz < cluster_node_size) {
21,725,277✔
697
            ensure_general_form();
16,911✔
698
        }
16,911✔
699
    }
21,725,277✔
700

701
    REALM_ASSERT_DEBUG(sz <= cluster_node_size);
23,296,803✔
702
    if (REALM_LIKELY(sz < cluster_node_size)) {
23,296,803✔
703
        insert_row(ndx, k, init_values); // Throws
23,199,366✔
704
        state.mem = get_mem();
23,199,366✔
705
        state.index = ndx;
23,199,366✔
706
    }
23,199,366✔
707
    else {
97,437✔
708
        // Split leaf node
709
        Cluster new_leaf(0, m_alloc, m_tree_top);
97,437✔
710
        new_leaf.create();
97,437✔
711
        if (ndx == sz) {
103,158✔
712
            new_leaf.insert_row(0, ObjKey(0), init_values); // Throws
84,876✔
713
            state.split_key = k.value;
84,876✔
714
            state.mem = new_leaf.get_mem();
84,876✔
715
            state.index = 0;
84,876✔
716
        }
84,876✔
717
        else {
2,147,501,929✔
718
            // Current cluster must be in general form to get here
719
            REALM_ASSERT_DEBUG(m_keys.is_attached());
2,147,501,929✔
720
            new_leaf.ensure_general_form();
2,147,501,929✔
721
            move(ndx, &new_leaf, current_key_value);
2,147,501,929✔
722
            insert_row(ndx, k, init_values); // Throws
2,147,501,929✔
723
            state.mem = get_mem();
2,147,501,929✔
724
            state.split_key = current_key_value;
2,147,501,929✔
725
            state.index = ndx;
2,147,501,929✔
726
        }
2,147,501,929✔
727
        ret = new_leaf.get_ref();
97,437✔
728
    }
97,437✔
729

730
    return ret;
23,296,803✔
731
}
23,296,803✔
732

733
bool Cluster::try_get(ObjKey k, ClusterNode::State& state) const noexcept
734
{
157,839,018✔
735
    state.mem = get_mem();
157,839,018✔
736
    if (m_keys.is_attached()) {
157,839,018✔
737
        state.index = m_keys.lower_bound(uint64_t(k.value));
50,120,394✔
738
        return state.index != m_keys.size() && m_keys.get(state.index) == uint64_t(k.value);
50,120,394✔
739
    }
50,120,394✔
740
    else {
107,718,624✔
741
        if (uint64_t(k.value) < uint64_t(Array::get(s_key_ref_or_size_index) >> 1)) {
107,718,624✔
742
            state.index = size_t(k.value);
89,875,077✔
743
            return true;
89,875,077✔
744
        }
89,875,077✔
745
    }
107,718,624✔
746
    return false;
17,843,547✔
747
}
157,839,018✔
748

749
ObjKey Cluster::get(size_t ndx, ClusterNode::State& state) const
750
{
12,833,355✔
751
    state.index = ndx;
12,833,355✔
752
    state.mem = get_mem();
12,833,355✔
753
    return get_real_key(ndx);
12,833,355✔
754
}
12,833,355✔
755

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

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

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

784
    ArrayMixed values(m_alloc);
1,431✔
785
    values.set_parent(this, col_ndx.val + s_first_col_index);
1,431✔
786
    values.init_from_parent();
1,431✔
787

788
    Mixed value = values.get(ndx);
1,431✔
789
    if (value.is_type(type_TypedLink)) {
1,431✔
790
        ObjLink link = value.get<ObjLink>();
24✔
791
        auto target_obj = origin_table->get_parent_group()->get_object(link);
24✔
792

793
        ColKey backlink_col_key = target_obj.get_table()->find_backlink_column(col_key, origin_table->get_key());
24✔
794
        REALM_ASSERT(backlink_col_key);
24✔
795
        target_obj.remove_one_backlink(backlink_col_key, get_real_key(ndx)); // Throws
24✔
796
    }
24✔
797
    if (value.is_type(type_List)) {
1,431✔
798
        Obj obj(origin_table->m_own_ref, get_mem(), key, ndx);
42✔
799
        Lst<Mixed> list(obj, col_key);
42✔
800
        list.remove_backlinks(state);
42✔
801
    }
42✔
802
    if (value.is_type(type_Dictionary)) {
1,431✔
803
        Obj obj(origin_table->m_own_ref, get_mem(), key, ndx);
18✔
804
        Dictionary dict(obj, col_key);
18✔
805
        dict.remove_backlinks(state);
18✔
806
    }
18✔
807
    values.erase(ndx);
1,431✔
808
}
1,431✔
809

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

817
    ObjKey key = values.get(ndx);
68,169✔
818
    if (key != null_key) {
68,169✔
819
        do_remove_backlinks(get_real_key(ndx), col_key, std::vector<ObjKey>{key}, state);
65,412✔
820
    }
65,412✔
821
    values.erase(ndx);
68,169✔
822
}
68,169✔
823

824
size_t Cluster::get_ndx(ObjKey k, size_t ndx) const noexcept
825
{
9,939,909✔
826
    size_t index;
9,939,909✔
827
    if (m_keys.is_attached()) {
9,939,909✔
828
        index = m_keys.lower_bound(uint64_t(k.value));
9,825,225✔
829
        if (index == m_keys.size() || m_keys.get(index) != uint64_t(k.value)) {
9,825,249✔
830
            return realm::npos;
18✔
831
        }
18✔
832
    }
9,825,225✔
833
    else {
114,684✔
834
        index = size_t(k.value);
114,684✔
835
        if (index >= get_as_ref_or_tagged(s_key_ref_or_size_index).get_as_int()) {
114,684✔
836
            return realm::npos;
×
837
        }
×
838
    }
114,684✔
839
    return index + ndx;
9,939,891✔
840
}
9,939,909✔
841

842
size_t Cluster::erase(ObjKey key, CascadeState& state)
843
{
5,000,814✔
844
    size_t ndx = get_ndx(key, 0);
5,000,814✔
845
    if (ndx == realm::npos)
5,000,814✔
846
        throw KeyNotFound(util::format("When erasing key '%1' in '%2'", key.value, get_owning_table()->get_name()));
18✔
847
    std::vector<ColKey> backlink_column_keys;
5,000,796✔
848

849
    auto erase_in_column = [&](ColKey col_key) {
6,705,036✔
850
        auto col_type = col_key.get_type();
6,705,036✔
851
        auto attr = col_key.get_attrs();
6,705,036✔
852
        if (attr.test(col_attr_Collection)) {
6,705,036✔
853
            auto col_ndx = col_key.get_index();
31,572✔
854
            ArrayRef values(m_alloc);
31,572✔
855
            values.set_parent(this, col_ndx.val + s_first_col_index);
31,572✔
856
            values.init_from_parent();
31,572✔
857
            ref_type ref = values.get(ndx);
31,572✔
858

859
            if (ref) {
31,572✔
860
                const Table* origin_table = m_tree_top.get_owning_table();
11,490✔
861
                if (attr.test(col_attr_Dictionary)) {
11,490✔
862
                    if (col_type == col_type_Mixed || col_type == col_type_Link) {
2,655✔
863
                        Obj obj(origin_table->m_own_ref, get_mem(), key, ndx);
354✔
864
                        Dictionary dict(obj, col_key);
354✔
865
                        dict.remove_backlinks(state);
354✔
866
                    }
354✔
867
                }
2,655✔
868
                else if (col_type == col_type_Link) {
8,835✔
869
                    BPlusTree<ObjKey> links(m_alloc);
3,999✔
870
                    links.init_from_ref(ref);
3,999✔
871
                    if (links.size() > 0) {
3,999✔
872
                        do_remove_backlinks(ObjKey(key.value + m_offset), col_key, links.get_all(), state);
1,029✔
873
                    }
1,029✔
874
                }
3,999✔
875
                else if (col_type == col_type_TypedLink) {
4,836✔
876
                    BPlusTree<ObjLink> links(m_alloc);
×
877
                    links.init_from_ref(ref);
×
878
                    for (size_t i = 0; i < links.size(); i++) {
×
879
                        ObjLink link = links.get(i);
×
880
                        auto target_obj = origin_table->get_parent_group()->get_object(link);
×
881
                        ColKey backlink_col_key =
×
882
                            target_obj.get_table()->find_backlink_column(col_key, origin_table->get_key());
×
883
                        target_obj.remove_one_backlink(backlink_col_key, ObjKey(key.value + m_offset));
×
884
                    }
×
885
                }
×
886
                else if (col_type == col_type_Mixed) {
4,836✔
887
                    Obj obj(origin_table->m_own_ref, get_mem(), key, ndx);
228✔
888
                    Lst<Mixed> list(obj, col_key);
228✔
889
                    list.remove_backlinks(state);
228✔
890
                }
228✔
891
                Array::destroy_deep(ref, m_alloc);
11,490✔
892
            }
11,490✔
893

894
            values.erase(ndx);
31,572✔
895

896
            return IteratorControl::AdvanceToNext;
31,572✔
897
        }
31,572✔
898

899
        switch (col_type) {
6,673,464✔
900
            case col_type_Int:
5,984,298✔
901
                if (attr.test(col_attr_Nullable)) {
5,984,298✔
902
                    do_erase<ArrayIntNull>(ndx, col_key);
1,275,504✔
903
                }
1,275,504✔
904
                else {
4,708,794✔
905
                    do_erase<ArrayInteger>(ndx, col_key);
4,708,794✔
906
                }
4,708,794✔
907
                break;
5,984,298✔
908
            case col_type_Bool:
42,342✔
909
                do_erase<ArrayBoolNull>(ndx, col_key);
42,342✔
910
                break;
42,342✔
911
            case col_type_Float:
36,489✔
912
                do_erase<ArrayFloatNull>(ndx, col_key);
36,489✔
913
                break;
36,489✔
914
            case col_type_Double:
36,501✔
915
                do_erase<ArrayDoubleNull>(ndx, col_key);
36,501✔
916
                break;
36,501✔
917
            case col_type_String:
76,113✔
918
                do_erase<ArrayString>(ndx, col_key);
76,113✔
919
                break;
76,113✔
920
            case col_type_Binary:
39,633✔
921
                do_erase<ArrayBinary>(ndx, col_key);
39,633✔
922
                break;
39,633✔
923
            case col_type_Mixed:
1,431✔
924
                do_erase_mixed(ndx, col_key, key, state);
1,431✔
925
                break;
1,431✔
926
            case col_type_Timestamp:
45,300✔
927
                do_erase<ArrayTimestamp>(ndx, col_key);
45,300✔
928
                break;
45,300✔
929
            case col_type_Decimal:
36,279✔
930
                do_erase<ArrayDecimal128>(ndx, col_key);
36,279✔
931
                break;
36,279✔
932
            case col_type_ObjectId:
38,775✔
933
                do_erase<ArrayObjectIdNull>(ndx, col_key);
38,775✔
934
                break;
38,775✔
935
            case col_type_UUID:
60,285✔
936
                do_erase<ArrayUUIDNull>(ndx, col_key);
60,285✔
937
                break;
60,285✔
938
            case col_type_Link:
68,169✔
939
                do_erase_key(ndx, col_key, state);
68,169✔
940
                break;
68,169✔
941
            case col_type_TypedLink:
✔
942
                do_erase<ArrayTypedLink>(ndx, col_key);
×
943
                break;
×
944
            case col_type_BackLink:
207,900✔
945
                if (state.m_mode == CascadeState::Mode::None) {
207,900✔
946
                    do_erase<ArrayBacklink>(ndx, col_key);
188,610✔
947
                }
188,610✔
948
                else {
19,290✔
949
                    // Postpone the deletion of backlink entries or else the
950
                    // checks for if there's any remaining backlinks will
951
                    // check the wrong row for columns which have already
952
                    // had values erased from them.
953
                    backlink_column_keys.push_back(col_key);
19,290✔
954
                }
19,290✔
955
                break;
207,900✔
956
            default:
✔
957
                REALM_ASSERT(false);
×
958
                break;
×
959
        }
6,673,464✔
960
        return IteratorControl::AdvanceToNext;
6,673,431✔
961
    };
6,673,464✔
962
    m_tree_top.m_owner->for_each_and_every_column(erase_in_column);
5,000,796✔
963

964
    // Any remaining backlink columns to erase from?
965
    for (auto k : backlink_column_keys)
5,000,796✔
966
        do_erase<ArrayBacklink>(ndx, k);
19,290✔
967

968
    if (m_keys.is_attached()) {
5,000,796✔
969
        m_keys.erase(ndx);
4,960,107✔
970
    }
4,960,107✔
971
    else {
40,689✔
972
        size_t current_size = get_size_in_compact_form();
40,689✔
973
        if (ndx == current_size - 1) {
40,689✔
974
            // When deleting last, we can still maintain compact form
975
            set(0, RefOrTagged::make_tagged(current_size - 1));
26,244✔
976
        }
26,244✔
977
        else {
14,445✔
978
            ensure_general_form();
14,445✔
979
            m_keys.erase(ndx);
14,445✔
980
        }
14,445✔
981
    }
40,689✔
982

983
    return node_size();
5,000,796✔
984
}
5,000,814✔
985

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

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

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

1021
        return IteratorControl::AdvanceToNext;
190,410✔
1022
    };
190,410✔
1023

1024
    m_tree_top.get_owning_table()->for_each_backlink_column(nullify_fwd_links);
4,902,489✔
1025
}
4,902,489✔
1026

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

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

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

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

1086
template <typename ArrayType>
1087
void verify_list(ArrayRef& arr, size_t sz)
1088
{
74,658✔
1089
    for (size_t n = 0; n < sz; n++) {
252,282!
1090
        if (ref_type bp_tree_ref = arr.get(n)) {
177,624!
1091
            BPlusTree<ArrayType> links(arr.get_alloc());
71,748✔
1092
            links.init_from_ref(bp_tree_ref);
71,748✔
1093
            links.set_parent(&arr, n);
71,748✔
1094
            links.verify();
71,748✔
1095
        }
71,748✔
1096
    }
177,624✔
1097
}
74,658✔
1098

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

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

1114
} // namespace
1115

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

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

1128
        if (attr.test(col_attr_List)) {
605,292✔
1129
            ArrayRef arr(get_alloc());
74,676✔
1130
            arr.set_parent(const_cast<Cluster*>(this), col);
74,676✔
1131
            arr.init_from_ref(ref);
74,676✔
1132
            arr.verify();
74,676✔
1133
            if (sz) {
74,676✔
1134
                REALM_ASSERT(arr.size() == *sz);
50,424✔
1135
            }
50,424✔
1136
            else {
24,252✔
1137
                sz = arr.size();
24,252✔
1138
            }
24,252✔
1139

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

1261
        switch (col_type) {
518,178✔
1262
            case col_type_Int:
291,843✔
1263
                if (nullable) {
291,843✔
1264
                    verify<ArrayIntNull>(ref, col, sz);
19,572✔
1265
                }
19,572✔
1266
                else {
272,271✔
1267
                    verify<ArrayInteger>(ref, col, sz);
272,271✔
1268
                }
272,271✔
1269
                break;
291,843✔
1270
            case col_type_Bool:
7,158✔
1271
                verify<ArrayBoolNull>(ref, col, sz);
7,158✔
1272
                break;
7,158✔
1273
            case col_type_Float:
150✔
1274
                verify<ArrayFloatNull>(ref, col, sz);
150✔
1275
                break;
150✔
1276
            case col_type_Double:
165✔
1277
                verify<ArrayDoubleNull>(ref, col, sz);
165✔
1278
                break;
165✔
1279
            case col_type_String:
155,157✔
1280
                verify<ArrayString>(ref, col, sz);
155,157✔
1281
                break;
155,157✔
1282
            case col_type_Binary:
49,872✔
1283
                verify<ArrayBinary>(ref, col, sz);
49,872✔
1284
                break;
49,872✔
1285
            case col_type_Mixed:
2,688✔
1286
                verify<ArrayMixed>(ref, col, sz);
2,688✔
1287
                break;
2,688✔
1288
            case col_type_Timestamp:
6,969✔
1289
                verify<ArrayTimestamp>(ref, col, sz);
6,969✔
1290
                break;
6,969✔
1291
            case col_type_Decimal:
42✔
1292
                verify<ArrayDecimal128>(ref, col, sz);
42✔
1293
                break;
42✔
1294
            case col_type_ObjectId:
42✔
1295
                verify<ArrayObjectIdNull>(ref, col, sz);
42✔
1296
                break;
42✔
1297
            case col_type_UUID:
✔
1298
                verify<ArrayUUIDNull>(ref, col, sz);
×
1299
                break;
×
1300
            case col_type_Link:
1,590✔
1301
                verify<ArrayKey>(ref, col, sz);
1,590✔
1302
                break;
1,590✔
1303
            case col_type_BackLink:
2,514✔
1304
                verify<ArrayBacklink>(ref, col, sz);
2,514✔
1305
                break;
2,514✔
1306
            default:
✔
1307
                break;
×
1308
        }
518,178✔
1309
        return IteratorControl::AdvanceToNext;
518,175✔
1310
    };
518,178✔
1311

1312
    m_tree_top.m_owner->for_each_and_every_column(verify_column);
377,106✔
1313
#endif
377,106✔
1314
}
377,106✔
1315

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

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

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

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

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

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

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

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

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