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

realm / realm-core / 2089

01 Mar 2024 06:54PM UTC coverage: 90.953% (+0.05%) from 90.906%
2089

push

Evergreen

web-flow
Fix several crashes in the object store benchmarks (#7403)

93968 of 173116 branches covered (54.28%)

238474 of 262194 relevant lines covered (90.95%)

5554337.54 hits per line

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

77.52
/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)
46
{
465,861✔
47
    if (m_values.size() > 1) {
465,861✔
48
        // Sort according to ColKey index
8,619✔
49
        std::sort(m_values.begin(), m_values.end(), [](const auto& a, const auto& b) {
51,609✔
50
            return a.col_key.get_index().val < b.col_key.get_index().val;
51,609✔
51
        });
51,609✔
52
    }
17,238✔
53
}
465,861✔
54

55
void FieldValues::insert(ColKey k, Mixed val, bool is_default)
56
{
422,598✔
57
    if (m_values.empty()) {
422,598✔
58
        m_values.emplace_back(k, val, is_default);
410,679✔
59
        return;
410,679✔
60
    }
410,679✔
61
    unsigned int idx = k.get_index().val;
11,919✔
62
    auto it = std::lower_bound(m_values.begin(), m_values.end(), idx, [](const auto& a, unsigned int i) {
23,082✔
63
        return a.col_key.get_index().val < i;
23,082✔
64
    });
23,082✔
65
    m_values.insert(it, {k, val, is_default});
11,919✔
66
}
11,919✔
67

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

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

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

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

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

97

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

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

132,783✔
105
    arr.add(RefOrTagged::make_tagged(0)); // Compact form
267,183✔
106
    return arr.get_mem();
267,183✔
107
}
267,183✔
108

109

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

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

46,131✔
125
    auto column_initialize = [this](ColKey col_key) {
177,249✔
126
        auto col_ndx = col_key.get_index();
177,249✔
127
        while (size() <= col_ndx.val + 1)
354,504✔
128
            add(0);
177,255✔
129
        auto type = col_key.get_type();
177,249✔
130
        auto attr = col_key.get_attrs();
177,249✔
131
        if (attr.test(col_attr_Collection)) {
177,249✔
132
            ArrayRef arr(m_alloc);
4,539✔
133
            arr.create();
4,539✔
134
            arr.set_parent(this, col_ndx.val + s_first_col_index);
4,539✔
135
            arr.update_parent();
4,539✔
136
            return IteratorControl::AdvanceToNext;
4,539✔
137
        }
4,539✔
138
        switch (type) {
172,710✔
139
            case col_type_Int:
104,028✔
140
                if (attr.test(col_attr_Nullable)) {
104,028✔
141
                    do_create<ArrayIntNull>(col_key);
7,572✔
142
                }
7,572✔
143
                else {
96,456✔
144
                    do_create<ArrayInteger>(col_key);
96,456✔
145
                }
96,456✔
146
                break;
104,028✔
147
            case col_type_Bool:
684✔
148
                do_create<ArrayBoolNull>(col_key);
684✔
149
                break;
684✔
150
            case col_type_Float:
1,155✔
151
                do_create<ArrayFloatNull>(col_key);
1,155✔
152
                break;
1,155✔
153
            case col_type_Double:
5,790✔
154
                do_create<ArrayDoubleNull>(col_key);
5,790✔
155
                break;
5,790✔
156
            case col_type_String: {
44,487✔
157
                if (m_tree_top.is_string_enum_type(col_ndx)) {
44,487✔
158
                    do_create<ArrayInteger>(col_key);
5,340✔
159
                }
5,340✔
160
                else {
39,147✔
161
                    do_create<ArrayString>(col_key);
39,147✔
162
                }
39,147✔
163
                break;
44,487✔
164
            }
×
165
            case col_type_Binary:
5,283✔
166
                do_create<ArrayBinary>(col_key);
5,283✔
167
                break;
5,283✔
168
            case col_type_Mixed:
72✔
169
                do_create<ArrayMixed>(col_key);
72✔
170
                break;
72✔
171
            case col_type_Timestamp:
2,445✔
172
                do_create<ArrayTimestamp>(col_key);
2,445✔
173
                break;
2,445✔
174
            case col_type_Decimal:
276✔
175
                do_create<ArrayDecimal128>(col_key);
276✔
176
                break;
276✔
177
            case col_type_ObjectId:
2,868✔
178
                do_create<ArrayObjectIdNull>(col_key);
2,868✔
179
                break;
2,868✔
180
            case col_type_UUID:
408✔
181
                do_create<ArrayUUIDNull>(col_key);
408✔
182
                break;
408✔
183
            case col_type_Link:
2,181✔
184
                do_create<ArrayKey>(col_key);
2,181✔
185
                break;
2,181✔
186
            case col_type_TypedLink:
✔
187
                do_create<ArrayTypedLink>(col_key);
×
188
                break;
×
189
            case col_type_BackLink:
3,033✔
190
                do_create<ArrayBacklink>(col_key);
3,033✔
191
                break;
3,033✔
192
            default:
✔
193
                REALM_UNREACHABLE();
194
        }
172,710✔
195
        return IteratorControl::AdvanceToNext;
172,710✔
196
    };
172,710✔
197
    m_tree_top.m_owner->for_each_and_every_column(column_initialize);
93,522✔
198

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

207
void Cluster::init(MemRef mem)
208
{
191,925,378✔
209
    Array::init_from_mem(mem);
191,925,378✔
210
    auto rot = Array::get_as_ref_or_tagged(0);
191,925,378✔
211
    if (rot.is_tagged()) {
191,925,378✔
212
        m_keys.detach();
132,009,627✔
213
    }
132,009,627✔
214
    else {
59,915,751✔
215
        m_keys.init_from_ref(rot.get_as_ref());
59,915,751✔
216
    }
59,915,751✔
217
}
191,925,378✔
218

219
void Cluster::update_from_parent() noexcept
220
{
627,822✔
221
    Array::update_from_parent();
627,822✔
222
    auto rot = Array::get_as_ref_or_tagged(0);
627,822✔
223
    if (!rot.is_tagged()) {
627,822✔
224
        m_keys.update_from_parent();
23,295✔
225
    }
23,295✔
226
}
627,822✔
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
{
70,710,882✔
244
    auto rot = Array::get_as_ref_or_tagged(header, s_key_ref_or_size_index);
70,710,882✔
245
    if (rot.is_tagged()) {
70,824,828✔
246
        return size_t(rot.get_as_int());
70,103,241✔
247
    }
70,103,241✔
248
    else {
2,148,205,234✔
249
        return Array::get_size_from_header(alloc.translate(rot.get_as_ref()));
2,148,205,234✔
250
    }
2,148,205,234✔
251
}
70,710,882✔
252

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

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

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

15,497,460✔
269
    T arr(m_alloc);
31,684,242✔
270
    auto col_ndx = col.get_index();
31,684,242✔
271
    arr.set_parent(this, col_ndx.val + s_first_col_index);
31,684,242✔
272
    set_spec<T>(arr, col_ndx);
31,684,242✔
273
    arr.init_from_parent();
31,684,242✔
274
    if (init_val.is_null()) {
31,684,242✔
275
        arr.insert(ndx, T::default_value(nullable));
31,185,723✔
276
    }
31,185,723✔
277
    else {
498,519✔
278
        arr.insert(ndx, init_val.get<U>());
498,519✔
279
    }
498,519✔
280
}
31,684,242✔
281

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

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

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

7,284✔
308
    // Insert backlink if needed
7,284✔
309
    if (init_value.is_type(type_TypedLink)) {
14,871✔
310
        // In case we are inserting in a Dictionary cluster, the backlink will
12✔
311
        // be handled in Dictionary::insert function
12✔
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
}
14,871✔
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
{
22,881,735✔
348
    // Ensure the cluster array is big enough to hold 64 bit values.
11,313,972✔
349
    copy_on_write(m_size * 8);
22,881,735✔
350

11,313,972✔
351
    if (m_keys.is_attached()) {
22,881,735✔
352
        m_keys.insert(ndx, k.value);
1,583,631✔
353
    }
1,583,631✔
354
    else {
21,298,104✔
355
        Array::set(s_key_ref_or_size_index, Array::get(s_key_ref_or_size_index) + 2); // Increments size by 1
21,298,104✔
356
    }
21,298,104✔
357

11,313,972✔
358
    auto val = init_values.begin();
22,881,735✔
359
    auto insert_in_column = [&](ColKey col_key) {
33,541,827✔
360
        auto col_ndx = col_key.get_index();
33,541,827✔
361
        auto attr = col_key.get_attrs();
33,541,827✔
362
        Mixed init_value;
33,541,827✔
363
        // init_values must be sorted in col_ndx order - this is ensured by ClustTree::insert()
16,429,746✔
364
        if (val != init_values.end() && val->col_key.get_index().val == col_ndx.val) {
33,541,827✔
365
            init_value = val->value;
487,179✔
366
            ++val;
487,179✔
367
        }
487,179✔
368

16,429,746✔
369
        auto type = col_key.get_type();
33,541,827✔
370
        if (attr.test(col_attr_Collection)) {
33,541,827✔
371
            REALM_ASSERT(init_value.is_null());
675,885✔
372
            ArrayRef arr(m_alloc);
675,885✔
373
            arr.set_parent(this, col_ndx.val + s_first_col_index);
675,885✔
374
            arr.init_from_parent();
675,885✔
375
            arr.insert(ndx, 0);
675,885✔
376
            return IteratorControl::AdvanceToNext;
675,885✔
377
        }
675,885✔
378

16,093,509✔
379
        bool nullable = attr.test(col_attr_Nullable);
32,865,942✔
380
        switch (type) {
32,865,942✔
381
            case col_type_Int:
25,141,206✔
382
                if (attr.test(col_attr_Nullable)) {
25,141,206✔
383
                    do_insert_row<ArrayIntNull>(ndx, col_key, init_value, nullable);
1,370,268✔
384
                }
1,370,268✔
385
                else {
23,770,938✔
386
                    do_insert_row<ArrayInteger>(ndx, col_key, init_value, nullable);
23,770,938✔
387
                }
23,770,938✔
388
                break;
25,141,206✔
389
            case col_type_Bool:
187,551✔
390
                do_insert_row<ArrayBoolNull>(ndx, col_key, init_value, nullable);
187,551✔
391
                break;
187,551✔
392
            case col_type_Float:
332,739✔
393
                do_insert_row<ArrayFloatNull>(ndx, col_key, init_value, nullable);
332,739✔
394
                break;
332,739✔
395
            case col_type_Double:
1,490,520✔
396
                do_insert_row<ArrayDoubleNull>(ndx, col_key, init_value, nullable);
1,490,520✔
397
                break;
1,490,520✔
398
            case col_type_String:
2,584,047✔
399
                do_insert_row<ArrayString>(ndx, col_key, init_value, nullable);
2,584,047✔
400
                break;
2,584,047✔
401
            case col_type_Binary:
1,361,427✔
402
                do_insert_row<ArrayBinary>(ndx, col_key, init_value, nullable);
1,361,427✔
403
                break;
1,361,427✔
404
            case col_type_Mixed: {
14,874✔
405
                do_insert_mixed(ndx, col_key, init_value, ObjKey(k.value + get_offset()));
14,874✔
406
                break;
14,874✔
407
            }
×
408
            case col_type_Timestamp:
220,167✔
409
                do_insert_row<ArrayTimestamp>(ndx, col_key, init_value, nullable);
220,167✔
410
                break;
220,167✔
411
            case col_type_Decimal:
75,816✔
412
                do_insert_row<ArrayDecimal128>(ndx, col_key, init_value, nullable);
75,816✔
413
                break;
75,816✔
414
            case col_type_ObjectId:
166,308✔
415
                do_insert_row<ArrayObjectIdNull>(ndx, col_key, init_value, nullable);
166,308✔
416
                break;
166,308✔
417
            case col_type_UUID:
116,046✔
418
                do_insert_row<ArrayUUIDNull>(ndx, col_key, init_value, nullable);
116,046✔
419
                break;
116,046✔
420
            case col_type_Link:
261,156✔
421
                do_insert_key(ndx, col_key, init_value, ObjKey(k.value + get_offset()));
261,156✔
422
                break;
261,156✔
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: {
963,330✔
427
                ArrayBacklink arr(m_alloc);
963,330✔
428
                arr.set_parent(this, col_ndx.val + s_first_col_index);
963,330✔
429
                arr.init_from_parent();
963,330✔
430
                arr.insert(ndx, 0);
963,330✔
431
                break;
963,330✔
432
            }
×
433
            default:
✔
434
                REALM_ASSERT(false);
×
435
                break;
×
436
        }
32,756,925✔
437
        return IteratorControl::AdvanceToNext;
32,756,925✔
438
    };
32,756,925✔
439
    m_tree_top.m_owner->for_each_and_every_column(insert_in_column);
22,881,735✔
440
}
22,881,735✔
441

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

11,169✔
450
    T dst(m_alloc);
22,287✔
451
    dst.set_parent(to, col_ndx);
22,287✔
452
    dst.init_from_parent();
22,287✔
453

11,169✔
454
    src.move(dst, ndx);
22,287✔
455
}
22,287✔
456

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

5,874✔
461
    auto move_from_column = [&](ColKey col_key) {
22,287✔
462
        auto attr = col_key.get_attrs();
22,287✔
463
        auto type = col_key.get_type();
22,287✔
464

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

11,154✔
470
        switch (type) {
22,257✔
471
            case col_type_Int:
21,387✔
472
                if (attr.test(col_attr_Nullable)) {
21,387✔
473
                    do_move<ArrayIntNull>(ndx, col_key, new_leaf);
9,687✔
474
                }
9,687✔
475
                else {
11,700✔
476
                    do_move<ArrayInteger>(ndx, col_key, new_leaf);
11,700✔
477
                }
11,700✔
478
                break;
21,387✔
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
        }
22,257✔
526
        return IteratorControl::AdvanceToNext;
22,257✔
527
    };
22,257✔
528
    m_tree_top.m_owner->for_each_and_every_column(move_from_column);
11,718✔
529
    for (size_t i = ndx; i < m_keys.size(); i++) {
1,250,187✔
530
        new_leaf->m_keys.add(m_keys.get(i) - offset);
1,238,469✔
531
    }
1,238,469✔
532
    m_keys.truncate(ndx);
11,718✔
533
}
11,718✔
534

535
Cluster::~Cluster() {}
198,643,845✔
536

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

545
void Cluster::ensure_general_form()
546
{
61,284✔
547
    if (!m_keys.is_attached()) {
61,284✔
548
        size_t current_size = get_size_in_compact_form();
47,763✔
549
        m_keys.create(current_size, 255);
47,763✔
550
        m_keys.update_parent();
47,763✔
551
        for (size_t i = 0; i < current_size; i++) {
5,511,276✔
552
            m_keys.set(i, i);
5,463,513✔
553
        }
5,463,513✔
554
    }
47,763✔
555
}
61,284✔
556

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

269,859✔
562
    T arr(m_alloc);
547,623✔
563
    arr.create();
547,623✔
564
    auto val = T::default_value(nullable);
547,623✔
565
    for (size_t i = 0; i < sz; i++) {
6,783,990!
566
        arr.add(val);
6,236,367✔
567
    }
6,236,367✔
568
    auto col_ndx = col_key.get_index();
547,623✔
569
    unsigned ndx = col_ndx.val + s_first_col_index;
547,623✔
570

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

269,859✔
575
    if (ndx == size())
547,623!
576
        Array::insert(ndx, from_ref(arr.get_ref()));
547,515✔
577
    else
108✔
578
        Array::set(ndx, from_ref(arr.get_ref()));
108✔
579
}
547,623✔
580

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

95,787✔
588
        ArrayRef arr(m_alloc);
192,420✔
589
        arr.create(sz);
192,420✔
590
        auto col_ndx = col_key.get_index();
192,420✔
591
        unsigned idx = col_ndx.val + s_first_col_index;
192,420✔
592
        if (idx == size())
192,420✔
593
            Array::insert(idx, from_ref(arr.get_ref()));
192,420✔
594
        else
×
595
            Array::set(idx, from_ref(arr.get_ref()));
×
596
        return;
192,420✔
597
    }
192,420✔
598
    bool nullable = attr.test(col_attr_Nullable);
547,620✔
599
    switch (type) {
547,620✔
600
        case col_type_Int:
244,566✔
601
            if (nullable) {
244,566✔
602
                do_insert_column<ArrayIntNull>(col_key, nullable);
20,916✔
603
            }
20,916✔
604
            else {
223,650✔
605
                do_insert_column<ArrayInteger>(col_key, nullable);
223,650✔
606
            }
223,650✔
607
            break;
244,566✔
608
        case col_type_Bool:
5,481✔
609
            do_insert_column<ArrayBoolNull>(col_key, nullable);
5,481✔
610
            break;
5,481✔
611
        case col_type_Float:
6,426✔
612
            do_insert_column<ArrayFloatNull>(col_key, nullable);
6,426✔
613
            break;
6,426✔
614
        case col_type_Double:
7,071✔
615
            do_insert_column<ArrayDoubleNull>(col_key, nullable);
7,071✔
616
            break;
7,071✔
617
        case col_type_String:
78,660✔
618
            do_insert_column<ArrayString>(col_key, nullable);
78,660✔
619
            break;
78,660✔
620
        case col_type_Binary:
6,741✔
621
            do_insert_column<ArrayBinary>(col_key, nullable);
6,741✔
622
            break;
6,741✔
623
        case col_type_Mixed:
5,034✔
624
            do_insert_column<ArrayMixed>(col_key, nullable);
5,034✔
625
            break;
5,034✔
626
        case col_type_Timestamp:
22,635✔
627
            do_insert_column<ArrayTimestamp>(col_key, nullable);
22,635✔
628
            break;
22,635✔
629
        case col_type_Decimal:
5,010✔
630
            do_insert_column<ArrayDecimal128>(col_key, nullable);
5,010✔
631
            break;
5,010✔
632
        case col_type_ObjectId:
47,229✔
633
            do_insert_column<ArrayObjectIdNull>(col_key, nullable);
47,229✔
634
            break;
47,229✔
635
        case col_type_UUID:
5,598✔
636
            do_insert_column<ArrayUUIDNull>(col_key, nullable);
5,598✔
637
            break;
5,598✔
638
        case col_type_Link:
30,429✔
639
            do_insert_column<ArrayKey>(col_key, nullable);
30,429✔
640
            break;
30,429✔
641
        case col_type_TypedLink:
✔
642
            do_insert_column<ArrayTypedLink>(col_key, nullable);
×
643
            break;
×
644
        case col_type_BackLink:
82,743✔
645
            do_insert_column<ArrayBacklink>(col_key, nullable);
82,743✔
646
            break;
82,743✔
647
        default:
✔
648
            REALM_UNREACHABLE();
649
            break;
×
650
    }
547,620✔
651
}
547,620✔
652

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

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

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

11,116,542✔
679
    if (m_keys.is_attached()) {
22,668,846✔
680
        sz = m_keys.size();
1,573,017✔
681
        ndx = m_keys.lower_bound(uint64_t(k.value));
1,573,017✔
682
        if (ndx < sz) {
1,573,017✔
683
            current_key_value = m_keys.get(ndx);
616,938✔
684
            if (k.value == current_key_value) {
616,938✔
685
                on_error();
12✔
686
            }
12✔
687
        }
616,938✔
688
    }
1,573,017✔
689
    else {
21,095,829✔
690
        sz = size_t(Array::get(s_key_ref_or_size_index)) >> 1; // Size is stored as tagged integer
21,095,829✔
691
        if (uint64_t(k.value) < sz) {
21,095,829✔
692
            on_error();
×
693
        }
×
694
        // Key value is bigger than all other values, should be put last
10,329,186✔
695
        ndx = sz;
21,095,829✔
696
        if (uint64_t(k.value) > sz && sz < cluster_node_size) {
21,095,829✔
697
            ensure_general_form();
16,362✔
698
        }
16,362✔
699
    }
21,095,829✔
700

11,116,542✔
701
    REALM_ASSERT_DEBUG(sz <= cluster_node_size);
22,668,846✔
702
    if (REALM_LIKELY(sz < cluster_node_size)) {
22,840,458✔
703
        insert_row(ndx, k, init_values); // Throws
22,814,811✔
704
        state.mem = get_mem();
22,814,811✔
705
        state.index = ndx;
22,814,811✔
706
    }
22,814,811✔
707
    else {
2,147,509,294✔
708
        // Split leaf node
2,147,483,647✔
709
        Cluster new_leaf(0, m_alloc, m_tree_top);
2,147,509,294✔
710
        new_leaf.create();
2,147,509,294✔
711
        if (ndx == sz) {
2,147,525,758✔
712
            new_leaf.insert_row(0, ObjKey(0), init_values); // Throws
84,276✔
713
            state.split_key = k.value;
84,276✔
714
            state.mem = new_leaf.get_mem();
84,276✔
715
            state.index = 0;
84,276✔
716
        }
84,276✔
717
        else {
4,294,967,294✔
718
            // Current cluster must be in general form to get here
2,147,483,647✔
719
            REALM_ASSERT_DEBUG(m_keys.is_attached());
4,294,967,294✔
720
            new_leaf.ensure_general_form();
4,294,967,294✔
721
            move(ndx, &new_leaf, current_key_value);
4,294,967,294✔
722
            insert_row(ndx, k, init_values); // Throws
4,294,967,294✔
723
            state.mem = get_mem();
4,294,967,294✔
724
            state.split_key = current_key_value;
4,294,967,294✔
725
            state.index = ndx;
4,294,967,294✔
726
        }
4,294,967,294✔
727
        ret = new_leaf.get_ref();
2,147,509,294✔
728
    }
2,147,509,294✔
729

11,116,542✔
730
    return ret;
22,668,846✔
731
}
22,668,846✔
732

733
bool Cluster::try_get(ObjKey k, ClusterNode::State& state) const noexcept
734
{
147,089,742✔
735
    state.mem = get_mem();
147,089,742✔
736
    if (m_keys.is_attached()) {
147,089,742✔
737
        state.index = m_keys.lower_bound(uint64_t(k.value));
50,204,535✔
738
        return state.index != m_keys.size() && m_keys.get(state.index) == uint64_t(k.value);
50,204,535✔
739
    }
50,204,535✔
740
    else {
96,885,207✔
741
        if (uint64_t(k.value) < uint64_t(Array::get(s_key_ref_or_size_index) >> 1)) {
96,885,207✔
742
            state.index = size_t(k.value);
79,824,273✔
743
            return true;
79,824,273✔
744
        }
79,824,273✔
745
    }
17,060,934✔
746
    return false;
17,060,934✔
747
}
17,060,934✔
748

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

756
template <class T>
757
inline void Cluster::do_erase(size_t ndx, ColKey col_key)
758
{
6,598,119✔
759
    auto col_ndx = col_key.get_index();
6,598,119✔
760
    T values(m_alloc);
6,598,119✔
761
    values.set_parent(this, col_ndx.val + s_first_col_index);
6,598,119✔
762
    set_spec<T>(values, col_ndx);
6,598,119✔
763
    values.init_from_parent();
6,598,119✔
764
    ObjLink link;
6,598,119✔
765
    if constexpr (std::is_same_v<T, ArrayTypedLink>) {
6,598,119✔
766
        link = values.get(ndx);
×
767
    }
×
768
    if constexpr (std::is_same_v<T, ArrayMixed>) {
6,598,119✔
769
        Mixed value = values.get(ndx);
729✔
770
        if (value.is_type(type_TypedLink)) {
729✔
771
            link = value.get<ObjLink>();
24✔
772
        }
24✔
773
    }
729✔
774
    if (link) {
6,598,119!
775
        if (const Table* origin_table = m_tree_top.get_owning_table()) {
24!
776
            auto target_obj = origin_table->get_parent_group()->get_object(link);
24✔
777

12✔
778
            ColKey backlink_col_key = target_obj.get_table()->find_backlink_column(col_key, origin_table->get_key());
24✔
779
            REALM_ASSERT(backlink_col_key);
24!
780
            target_obj.remove_one_backlink(backlink_col_key, get_real_key(ndx)); // Throws
24✔
781
        }
24✔
782
    }
24✔
783
    values.erase(ndx);
6,598,119✔
784
}
6,598,119✔
785

786
inline void Cluster::do_erase_key(size_t ndx, ColKey col_key, CascadeState& state)
787
{
67,806✔
788
    ArrayKey values(m_alloc);
67,806✔
789
    auto col_ndx = col_key.get_index();
67,806✔
790
    values.set_parent(this, col_ndx.val + s_first_col_index);
67,806✔
791
    values.init_from_parent();
67,806✔
792

33,894✔
793
    ObjKey key = values.get(ndx);
67,806✔
794
    if (key != null_key) {
67,806✔
795
        do_remove_backlinks(get_real_key(ndx), col_key, std::vector<ObjKey>{key}, state);
65,112✔
796
    }
65,112✔
797
    values.erase(ndx);
67,806✔
798
}
67,806✔
799

800
size_t Cluster::get_ndx(ObjKey k, size_t ndx) const noexcept
801
{
9,936,957✔
802
    size_t index;
9,936,957✔
803
    if (m_keys.is_attached()) {
9,936,957✔
804
        index = m_keys.lower_bound(uint64_t(k.value));
9,823,239✔
805
        if (index == m_keys.size() || m_keys.get(index) != uint64_t(k.value)) {
9,823,485✔
806
            return realm::npos;
18✔
807
        }
18✔
808
    }
113,718✔
809
    else {
113,718✔
810
        index = size_t(k.value);
113,718✔
811
        if (index >= get_as_ref_or_tagged(s_key_ref_or_size_index).get_as_int()) {
113,718✔
812
            return realm::npos;
×
813
        }
×
814
    }
9,936,939✔
815
    return index + ndx;
9,936,939✔
816
}
9,936,939✔
817

818
size_t Cluster::erase(ObjKey key, CascadeState& state)
819
{
4,999,077✔
820
    size_t ndx = get_ndx(key, 0);
4,999,077✔
821
    if (ndx == realm::npos)
4,999,077✔
822
        throw KeyNotFound(util::format("When erasing key '%1' in '%2'", key.value, get_owning_table()->get_name()));
18✔
823
    std::vector<ColKey> backlink_column_keys;
4,999,059✔
824

2,499,780✔
825
    auto erase_in_column = [&](ColKey col_key) {
6,696,516✔
826
        auto col_type = col_key.get_type();
6,696,516✔
827
        auto attr = col_key.get_attrs();
6,696,516✔
828
        if (attr.test(col_attr_Collection)) {
6,696,516✔
829
            auto col_ndx = col_key.get_index();
30,834✔
830
            ArrayRef values(m_alloc);
30,834✔
831
            values.set_parent(this, col_ndx.val + s_first_col_index);
30,834✔
832
            values.init_from_parent();
30,834✔
833
            ref_type ref = values.get(ndx);
30,834✔
834

15,537✔
835
            if (ref) {
30,834✔
836
                const Table* origin_table = m_tree_top.get_owning_table();
11,187✔
837
                if (attr.test(col_attr_Dictionary)) {
11,187✔
838
                    if (col_type == col_type_Mixed || col_type == col_type_Link) {
2,655✔
839
                        Obj obj(origin_table->m_own_ref, get_mem(), key, ndx);
354✔
840
                        Dictionary dict(obj, col_key);
354✔
841
                        dict.remove_backlinks(state);
354✔
842
                    }
354✔
843
                }
2,655✔
844
                else if (col_type == col_type_Link) {
8,532✔
845
                    BPlusTree<ObjKey> links(m_alloc);
3,711✔
846
                    links.init_from_ref(ref);
3,711✔
847
                    if (links.size() > 0) {
3,711✔
848
                        do_remove_backlinks(ObjKey(key.value + m_offset), col_key, links.get_all(), state);
1,041✔
849
                    }
1,041✔
850
                }
3,711✔
851
                else if (col_type == col_type_TypedLink) {
4,821✔
852
                    BPlusTree<ObjLink> links(m_alloc);
×
853
                    links.init_from_ref(ref);
×
854
                    for (size_t i = 0; i < links.size(); i++) {
×
855
                        ObjLink link = links.get(i);
×
856
                        auto target_obj = origin_table->get_parent_group()->get_object(link);
×
857
                        ColKey backlink_col_key =
×
858
                            target_obj.get_table()->find_backlink_column(col_key, origin_table->get_key());
×
859
                        target_obj.remove_one_backlink(backlink_col_key, ObjKey(key.value + m_offset));
×
860
                    }
×
861
                }
×
862
                else if (col_type == col_type_Mixed) {
4,821✔
863
                    Obj obj(origin_table->m_own_ref, get_mem(), key, ndx);
216✔
864
                    Lst<Mixed> list(obj, col_key);
216✔
865
                    list.remove_backlinks(state);
216✔
866
                }
216✔
867
                Array::destroy_deep(ref, m_alloc);
11,187✔
868
            }
11,187✔
869

15,537✔
870
            values.erase(ndx);
30,834✔
871

15,537✔
872
            return IteratorControl::AdvanceToNext;
30,834✔
873
        }
30,834✔
874

3,333,081✔
875
        switch (col_type) {
6,665,682✔
876
            case col_type_Int:
5,980,170✔
877
                if (attr.test(col_attr_Nullable)) {
5,980,170✔
878
                    do_erase<ArrayIntNull>(ndx, col_key);
1,275,459✔
879
                }
1,275,459✔
880
                else {
4,704,711✔
881
                    do_erase<ArrayInteger>(ndx, col_key);
4,704,711✔
882
                }
4,704,711✔
883
                break;
5,980,170✔
884
            case col_type_Bool:
42,339✔
885
                do_erase<ArrayBoolNull>(ndx, col_key);
42,339✔
886
                break;
42,339✔
887
            case col_type_Float:
36,489✔
888
                do_erase<ArrayFloatNull>(ndx, col_key);
36,489✔
889
                break;
36,489✔
890
            case col_type_Double:
36,501✔
891
                do_erase<ArrayDoubleNull>(ndx, col_key);
36,501✔
892
                break;
36,501✔
893
            case col_type_String:
74,952✔
894
                do_erase<ArrayString>(ndx, col_key);
74,952✔
895
                break;
74,952✔
896
            case col_type_Binary:
39,333✔
897
                do_erase<ArrayBinary>(ndx, col_key);
39,333✔
898
                break;
39,333✔
899
            case col_type_Mixed:
729✔
900
                do_erase<ArrayMixed>(ndx, col_key);
729✔
901
                break;
729✔
902
            case col_type_Timestamp:
45,309✔
903
                do_erase<ArrayTimestamp>(ndx, col_key);
45,309✔
904
                break;
45,309✔
905
            case col_type_Decimal:
36,279✔
906
                do_erase<ArrayDecimal128>(ndx, col_key);
36,279✔
907
                break;
36,279✔
908
            case col_type_ObjectId:
38,700✔
909
                do_erase<ArrayObjectIdNull>(ndx, col_key);
38,700✔
910
                break;
38,700✔
911
            case col_type_UUID:
60,282✔
912
                do_erase<ArrayUUIDNull>(ndx, col_key);
60,282✔
913
                break;
60,282✔
914
            case col_type_Link:
67,806✔
915
                do_erase_key(ndx, col_key, state);
67,806✔
916
                break;
67,806✔
917
            case col_type_TypedLink:
✔
918
                do_erase<ArrayTypedLink>(ndx, col_key);
×
919
                break;
×
920
            case col_type_BackLink:
207,027✔
921
                if (state.m_mode == CascadeState::Mode::None) {
207,027✔
922
                    do_erase<ArrayBacklink>(ndx, col_key);
188,451✔
923
                }
188,451✔
924
                else {
18,576✔
925
                    // Postpone the deletion of backlink entries or else the
9,261✔
926
                    // checks for if there's any remaining backlinks will
9,261✔
927
                    // check the wrong row for columns which have already
9,261✔
928
                    // had values erased from them.
9,261✔
929
                    backlink_column_keys.push_back(col_key);
18,576✔
930
                }
18,576✔
931
                break;
207,027✔
932
            default:
✔
933
                REALM_ASSERT(false);
×
934
                break;
×
935
        }
6,665,370✔
936
        return IteratorControl::AdvanceToNext;
6,665,370✔
937
    };
6,665,370✔
938
    m_tree_top.m_owner->for_each_and_every_column(erase_in_column);
4,999,059✔
939

2,499,780✔
940
    // Any remaining backlink columns to erase from?
2,499,780✔
941
    for (auto k : backlink_column_keys)
4,999,059✔
942
        do_erase<ArrayBacklink>(ndx, k);
18,576✔
943

2,499,780✔
944
    if (m_keys.is_attached()) {
4,999,059✔
945
        m_keys.erase(ndx);
4,959,072✔
946
    }
4,959,072✔
947
    else {
39,987✔
948
        size_t current_size = get_size_in_compact_form();
39,987✔
949
        if (ndx == current_size - 1) {
39,987✔
950
            // When deleting last, we can still maintain compact form
12,822✔
951
            set(0, RefOrTagged::make_tagged(current_size - 1));
25,590✔
952
        }
25,590✔
953
        else {
14,397✔
954
            ensure_general_form();
14,397✔
955
            m_keys.erase(ndx);
14,397✔
956
        }
14,397✔
957
    }
39,987✔
958

2,499,780✔
959
    return node_size();
4,999,059✔
960
}
4,999,059✔
961

962
void Cluster::nullify_incoming_links(ObjKey key, CascadeState& state)
963
{
4,901,196✔
964
    size_t ndx = get_ndx(key, 0);
4,901,196✔
965
    if (ndx == realm::npos)
4,901,196✔
966
        throw KeyNotFound(util::format("Key '%1' not found in '%2' when nullifying incoming links", key.value,
×
967
                                       get_owning_table()->get_class_name()));
×
968

2,450,898✔
969
    // We must start with backlink columns in case the corresponding link
2,450,898✔
970
    // columns are in the same table so that we can nullify links before
2,450,898✔
971
    // erasing rows in the link columns.
2,450,898✔
972
    //
2,450,898✔
973
    // This phase also generates replication instructions documenting the side-
2,450,898✔
974
    // effects of deleting the object (i.e. link nullifications). These instructions
2,450,898✔
975
    // must come before the actual deletion of the object, but at the same time
2,450,898✔
976
    // the Replication object may need a consistent view of the row (not including
2,450,898✔
977
    // link columns). Therefore we first nullify links to this object, then
2,450,898✔
978
    // generate the instruction, and then delete the row in the remaining columns.
2,450,898✔
979

2,450,898✔
980
    auto nullify_fwd_links = [&](ColKey col_key) {
4,901,196✔
981
        ColKey::Idx leaf_ndx = col_key.get_index();
190,077✔
982
        auto type = col_key.get_type();
190,077✔
983
        REALM_ASSERT(type == col_type_BackLink);
190,077✔
984
        ArrayBacklink values(m_alloc);
190,077✔
985
        values.set_parent(this, leaf_ndx.val + s_first_col_index);
190,077✔
986
        values.init_from_parent();
190,077✔
987
        // Ensure that Cluster is writable and able to hold references to nodes in
94,995✔
988
        // the slab area before nullifying or deleting links. These operation may
94,995✔
989
        // both have the effect that other objects may be constructed and manipulated.
94,995✔
990
        // If those other object are in the same cluster that the object to be deleted
94,995✔
991
        // is in, then that will cause another accessor to this cluster to be created.
94,995✔
992
        // It would lead to an error if the cluster node was relocated without it being
94,995✔
993
        // reflected in the context here.
94,995✔
994
        values.copy_on_write();
190,077✔
995
        values.nullify_fwd_links(ndx, state);
190,077✔
996

94,995✔
997
        return IteratorControl::AdvanceToNext;
190,077✔
998
    };
190,077✔
999

2,450,898✔
1000
    m_tree_top.get_owning_table()->for_each_backlink_column(nullify_fwd_links);
4,901,196✔
1001
}
4,901,196✔
1002

1003
void Cluster::upgrade_string_to_enum(ColKey col_key, ArrayString& keys)
1004
{
1,086✔
1005
    auto col_ndx = col_key.get_index();
1,086✔
1006
    Array indexes(m_alloc);
1,086✔
1007
    indexes.create(Array::type_Normal, false);
1,086✔
1008
    ArrayString values(m_alloc);
1,086✔
1009
    ref_type ref = Array::get_as_ref(col_ndx.val + s_first_col_index);
1,086✔
1010
    values.init_from_ref(ref);
1,086✔
1011
    size_t sz = values.size();
1,086✔
1012
    for (size_t i = 0; i < sz; i++) {
118,542✔
1013
        auto v = values.get(i);
117,456✔
1014
        size_t pos = keys.lower_bound(v);
117,456✔
1015
        REALM_ASSERT_3(pos, !=, keys.size());
117,456✔
1016
        indexes.add(pos);
117,456✔
1017
    }
117,456✔
1018
    Array::set(col_ndx.val + s_first_col_index, indexes.get_ref());
1,086✔
1019
    Array::destroy_deep(ref, m_alloc);
1,086✔
1020
}
1,086✔
1021

1022
void Cluster::init_leaf(ColKey col_key, ArrayPayload* leaf) const
1023
{
8,119,056✔
1024
    auto col_ndx = col_key.get_index();
8,119,056✔
1025
    // FIXME: Move this validation into callers.
3,708,972✔
1026
    // Currently, the query subsystem may call with an unvalidated key.
3,708,972✔
1027
    // once fixed, reintroduce the noexcept declaration :-D
3,708,972✔
1028
    if (auto t = m_tree_top.get_owning_table())
8,119,056✔
1029
        t->check_column(col_key);
8,231,637✔
1030
    ref_type ref = to_ref(Array::get(col_ndx.val + 1));
8,119,056✔
1031
    if (leaf->need_spec()) {
8,119,056✔
1032
        m_tree_top.set_spec(*leaf, col_ndx);
111,282✔
1033
    }
111,282✔
1034
    leaf->init_from_ref(ref);
8,119,056✔
1035
    leaf->set_parent(const_cast<Cluster*>(this), col_ndx.val + 1);
8,119,056✔
1036
}
8,119,056✔
1037

1038
void Cluster::add_leaf(ColKey col_key, ref_type ref)
1039
{
×
1040
    auto col_ndx = col_key.get_index();
×
1041
    REALM_ASSERT((col_ndx.val + 1) == size());
×
1042
    Array::insert(col_ndx.val + 1, from_ref(ref));
×
1043
}
×
1044

1045
template <typename ArrayType>
1046
void Cluster::verify(ref_type ref, size_t index, util::Optional<size_t>& sz) const
1047
{
534,603✔
1048
    ArrayType arr(get_alloc());
534,603✔
1049
    set_spec(arr, ColKey::Idx{unsigned(index) - 1});
534,603✔
1050
    arr.set_parent(const_cast<Cluster*>(this), index);
534,603✔
1051
    arr.init_from_ref(ref);
534,603✔
1052
    arr.verify();
534,603✔
1053
    if (sz) {
534,603!
1054
        REALM_ASSERT(arr.size() == *sz);
181,992!
1055
    }
181,992✔
1056
    else {
352,611✔
1057
        sz = arr.size();
352,611✔
1058
    }
352,611✔
1059
}
534,603✔
1060
namespace {
1061

1062
template <typename ArrayType>
1063
void verify_list(ArrayRef& arr, size_t sz)
1064
{
103,536✔
1065
    for (size_t n = 0; n < sz; n++) {
372,864!
1066
        if (ref_type bp_tree_ref = arr.get(n)) {
269,328!
1067
            BPlusTree<ArrayType> links(arr.get_alloc());
131,556✔
1068
            links.init_from_ref(bp_tree_ref);
131,556✔
1069
            links.set_parent(&arr, n);
131,556✔
1070
            links.verify();
131,556✔
1071
        }
131,556✔
1072
    }
269,328✔
1073
}
103,536✔
1074

1075
template <typename SetType>
1076
void verify_set(ArrayRef& arr, size_t sz)
1077
{
132✔
1078
    for (size_t n = 0; n < sz; ++n) {
306!
1079
        if (ref_type bp_tree_ref = arr.get(n)) {
174!
1080
            BPlusTree<SetType> elements(arr.get_alloc());
174✔
1081
            elements.init_from_ref(bp_tree_ref);
174✔
1082
            elements.set_parent(&arr, n);
174✔
1083
            elements.verify();
174✔
1084

87✔
1085
            // FIXME: Check uniqueness of elements.
87✔
1086
        }
174✔
1087
    }
174✔
1088
}
132✔
1089

1090
} // namespace
1091

1092
void Cluster::verify() const
1093
{
389,232✔
1094
#ifdef REALM_DEBUG
389,232✔
1095
    util::Optional<size_t> sz;
389,232✔
1096

200,541✔
1097
    auto verify_column = [this, &sz](ColKey col_key) {
650,529✔
1098
        size_t col = col_key.get_index().val + s_first_col_index;
650,529✔
1099
        ref_type ref = Array::get_as_ref(col);
650,529✔
1100
        auto attr = col_key.get_attrs();
650,529✔
1101
        auto col_type = col_key.get_type();
650,529✔
1102
        bool nullable = attr.test(col_attr_Nullable);
650,529✔
1103

338,157✔
1104
        if (attr.test(col_attr_List)) {
650,529✔
1105
            ArrayRef arr(get_alloc());
103,548✔
1106
            arr.set_parent(const_cast<Cluster*>(this), col);
103,548✔
1107
            arr.init_from_ref(ref);
103,548✔
1108
            arr.verify();
103,548✔
1109
            if (sz) {
103,548✔
1110
                REALM_ASSERT(arr.size() == *sz);
79,284✔
1111
            }
79,284✔
1112
            else {
24,264✔
1113
                sz = arr.size();
24,264✔
1114
            }
24,264✔
1115

56,364✔
1116
            switch (col_type) {
103,548✔
1117
                case col_type_Int:
43,269✔
1118
                    if (nullable) {
43,269✔
1119
                        verify_list<util::Optional<int64_t>>(arr, *sz);
×
1120
                    }
×
1121
                    else {
43,269✔
1122
                        verify_list<int64_t>(arr, *sz);
43,269✔
1123
                    }
43,269✔
1124
                    break;
43,269✔
1125
                case col_type_Bool:
✔
1126
                    verify_list<Bool>(arr, *sz);
×
1127
                    break;
×
1128
                case col_type_Float:
6✔
1129
                    verify_list<Float>(arr, *sz);
6✔
1130
                    break;
6✔
1131
                case col_type_Double:
✔
1132
                    verify_list<Double>(arr, *sz);
×
1133
                    break;
×
1134
                case col_type_String:
35,073✔
1135
                    verify_list<String>(arr, *sz);
35,073✔
1136
                    break;
35,073✔
1137
                case col_type_Binary:
24,000✔
1138
                    verify_list<Binary>(arr, *sz);
24,000✔
1139
                    break;
24,000✔
1140
                case col_type_Timestamp:
✔
1141
                    verify_list<Timestamp>(arr, *sz);
×
1142
                    break;
×
1143
                case col_type_Decimal:
✔
1144
                    verify_list<Decimal128>(arr, *sz);
×
1145
                    break;
×
1146
                case col_type_ObjectId:
✔
1147
                    verify_list<ObjectId>(arr, *sz);
×
1148
                    break;
×
1149
                case col_type_UUID:
✔
1150
                    verify_list<UUID>(arr, *sz);
×
1151
                    break;
×
1152
                case col_type_Link:
1,188✔
1153
                    verify_list<ObjKey>(arr, *sz);
1,188✔
1154
                    break;
1,188✔
1155
                default:
12✔
1156
                    // FIXME: Nullable primitives
6✔
1157
                    break;
12✔
1158
            }
103,548✔
1159
            return IteratorControl::AdvanceToNext;
103,548✔
1160
        }
103,548✔
1161
        else if (attr.test(col_attr_Dictionary)) {
546,981✔
1162
            ArrayRef arr(get_alloc());
12,156✔
1163
            arr.set_parent(const_cast<Cluster*>(this), col);
12,156✔
1164
            arr.init_from_ref(ref);
12,156✔
1165
            arr.verify();
12,156✔
1166
            if (sz) {
12,156✔
1167
                REALM_ASSERT(arr.size() == *sz);
126✔
1168
            }
126✔
1169
            else {
12,030✔
1170
                sz = arr.size();
12,030✔
1171
            }
12,030✔
1172
            for (size_t n = 0; n < sz; n++) {
24,426✔
1173
                if (auto ref = arr.get(n)) {
12,270✔
1174
                    Dictionary dict(get_alloc(), col_key, to_ref(ref));
12,171✔
1175
                    dict.verify();
12,171✔
1176
                }
12,171✔
1177
            }
12,270✔
1178
            return IteratorControl::AdvanceToNext;
12,156✔
1179
        }
12,156✔
1180
        else if (attr.test(col_attr_Set)) {
534,825✔
1181
            ArrayRef arr(get_alloc());
228✔
1182
            arr.set_parent(const_cast<Cluster*>(this), col);
228✔
1183
            arr.init_from_ref(ref);
228✔
1184
            arr.verify();
228✔
1185
            if (sz) {
228✔
1186
                REALM_ASSERT(arr.size() == *sz);
210✔
1187
            }
210✔
1188
            else {
18✔
1189
                sz = arr.size();
18✔
1190
            }
18✔
1191
            switch (col_type) {
228✔
1192
                case col_type_Int:
96✔
1193
                    if (nullable) {
96✔
1194
                        verify_set<util::Optional<int64_t>>(arr, *sz);
×
1195
                    }
×
1196
                    else {
96✔
1197
                        verify_set<int64_t>(arr, *sz);
96✔
1198
                    }
96✔
1199
                    break;
96✔
1200
                case col_type_Bool:
✔
1201
                    verify_set<Bool>(arr, *sz);
×
1202
                    break;
×
1203
                case col_type_Float:
✔
1204
                    verify_set<Float>(arr, *sz);
×
1205
                    break;
×
1206
                case col_type_Double:
✔
1207
                    verify_set<Double>(arr, *sz);
×
1208
                    break;
×
1209
                case col_type_String:
6✔
1210
                    verify_set<String>(arr, *sz);
6✔
1211
                    break;
6✔
1212
                case col_type_Binary:
18✔
1213
                    verify_set<Binary>(arr, *sz);
18✔
1214
                    break;
18✔
1215
                case col_type_Timestamp:
✔
1216
                    verify_set<Timestamp>(arr, *sz);
×
1217
                    break;
×
1218
                case col_type_Decimal:
✔
1219
                    verify_set<Decimal128>(arr, *sz);
×
1220
                    break;
×
1221
                case col_type_ObjectId:
✔
1222
                    verify_set<ObjectId>(arr, *sz);
×
1223
                    break;
×
1224
                case col_type_UUID:
✔
1225
                    verify_set<UUID>(arr, *sz);
×
1226
                    break;
×
1227
                case col_type_Link:
12✔
1228
                    verify_set<ObjKey>(arr, *sz);
12✔
1229
                    break;
12✔
1230
                default:
96✔
1231
                    // FIXME: Nullable primitives
48✔
1232
                    break;
96✔
1233
            }
228✔
1234
            return IteratorControl::AdvanceToNext;
228✔
1235
        }
228✔
1236

275,601✔
1237
        switch (col_type) {
534,597✔
1238
            case col_type_Int:
321,198✔
1239
                if (nullable) {
321,198✔
1240
                    verify<ArrayIntNull>(ref, col, sz);
25,272✔
1241
                }
25,272✔
1242
                else {
295,926✔
1243
                    verify<ArrayInteger>(ref, col, sz);
295,926✔
1244
                }
295,926✔
1245
                break;
321,198✔
1246
            case col_type_Bool:
7,191✔
1247
                verify<ArrayBoolNull>(ref, col, sz);
7,191✔
1248
                break;
7,191✔
1249
            case col_type_Float:
144✔
1250
                verify<ArrayFloatNull>(ref, col, sz);
144✔
1251
                break;
144✔
1252
            case col_type_Double:
156✔
1253
                verify<ArrayDoubleNull>(ref, col, sz);
156✔
1254
                break;
156✔
1255
            case col_type_String:
144,570✔
1256
                verify<ArrayString>(ref, col, sz);
144,570✔
1257
                break;
144,570✔
1258
            case col_type_Binary:
49,890✔
1259
                verify<ArrayBinary>(ref, col, sz);
49,890✔
1260
                break;
49,890✔
1261
            case col_type_Mixed:
60✔
1262
                verify<ArrayMixed>(ref, col, sz);
60✔
1263
                break;
60✔
1264
            case col_type_Timestamp:
6,996✔
1265
                verify<ArrayTimestamp>(ref, col, sz);
6,996✔
1266
                break;
6,996✔
1267
            case col_type_Decimal:
42✔
1268
                verify<ArrayDecimal128>(ref, col, sz);
42✔
1269
                break;
42✔
1270
            case col_type_ObjectId:
42✔
1271
                verify<ArrayObjectIdNull>(ref, col, sz);
42✔
1272
                break;
42✔
1273
            case col_type_UUID:
✔
1274
                verify<ArrayUUIDNull>(ref, col, sz);
×
1275
                break;
×
1276
            case col_type_Link:
1,671✔
1277
                verify<ArrayKey>(ref, col, sz);
1,671✔
1278
                break;
1,671✔
1279
            case col_type_BackLink:
2,643✔
1280
                verify<ArrayBacklink>(ref, col, sz);
2,643✔
1281
                break;
2,643✔
1282
            default:
✔
1283
                break;
×
1284
        }
534,594✔
1285
        return IteratorControl::AdvanceToNext;
534,594✔
1286
    };
534,594✔
1287

200,541✔
1288
    m_tree_top.m_owner->for_each_and_every_column(verify_column);
389,232✔
1289
#endif
389,232✔
1290
}
389,232✔
1291

1292
// LCOV_EXCL_START
1293
void Cluster::dump_objects(int64_t key_offset, std::string lead) const
1294
{
×
1295
    std::cout << lead << "leaf - size: " << node_size() << std::endl;
×
1296
    if (!m_keys.is_attached()) {
×
1297
        std::cout << lead << "compact form" << std::endl;
×
1298
    }
×
1299

1300
    for (unsigned i = 0; i < node_size(); i++) {
×
1301
        int64_t key_value;
×
1302
        if (m_keys.is_attached()) {
×
1303
            key_value = m_keys.get(i);
×
1304
        }
×
1305
        else {
×
1306
            key_value = int64_t(i);
×
1307
        }
×
1308
        std::cout << lead << "key: " << std::hex << key_value + key_offset << std::dec;
×
1309
        m_tree_top.m_owner->for_each_and_every_column([&](ColKey col) {
×
1310
            size_t j = col.get_index().val + 1;
×
1311
            if (col.get_attrs().test(col_attr_List)) {
×
1312
                ref_type ref = Array::get_as_ref(j);
×
1313
                ArrayRef refs(m_alloc);
×
1314
                refs.init_from_ref(ref);
×
1315
                std::cout << ", {";
×
1316
                ref = refs.get(i);
×
1317
                if (ref) {
×
1318
                    if (col.get_type() == col_type_Int) {
×
1319
                        // This is easy to handle
1320
                        Array ints(m_alloc);
×
1321
                        ints.init_from_ref(ref);
×
1322
                        for (size_t n = 0; n < ints.size(); n++) {
×
1323
                            std::cout << ints.get(n) << ", ";
×
1324
                        }
×
1325
                    }
×
1326
                    else {
×
1327
                        std::cout << col.get_type();
×
1328
                    }
×
1329
                }
×
1330
                std::cout << "}";
×
1331
                return IteratorControl::AdvanceToNext;
×
1332
            }
×
1333

1334
            switch (col.get_type()) {
×
1335
                case col_type_Int: {
×
1336
                    bool nullable = col.get_attrs().test(col_attr_Nullable);
×
1337
                    ref_type ref = Array::get_as_ref(j);
×
1338
                    if (nullable) {
×
1339
                        ArrayIntNull arr_int_null(m_alloc);
×
1340
                        arr_int_null.init_from_ref(ref);
×
1341
                        if (arr_int_null.is_null(i)) {
×
1342
                            std::cout << ", null";
×
1343
                        }
×
1344
                        else {
×
1345
                            std::cout << ", " << *arr_int_null.get(i);
×
1346
                        }
×
1347
                    }
×
1348
                    else {
×
1349
                        Array arr(m_alloc);
×
1350
                        arr.init_from_ref(ref);
×
1351
                        std::cout << ", " << arr.get(i);
×
1352
                    }
×
1353
                    break;
×
1354
                }
×
1355
                case col_type_Bool: {
×
1356
                    ArrayBoolNull arr(m_alloc);
×
1357
                    ref_type ref = Array::get_as_ref(j);
×
1358
                    arr.init_from_ref(ref);
×
1359
                    auto val = arr.get(i);
×
1360
                    std::cout << ", " << (val ? (*val ? "true" : "false") : "null");
×
1361
                    break;
×
1362
                }
×
1363
                case col_type_Float: {
×
1364
                    ArrayFloatNull arr(m_alloc);
×
1365
                    ref_type ref = Array::get_as_ref(j);
×
1366
                    arr.init_from_ref(ref);
×
1367
                    auto val = arr.get(i);
×
1368
                    if (val)
×
1369
                        std::cout << ", " << *val;
×
1370
                    else
×
1371
                        std::cout << ", null";
×
1372
                    break;
×
1373
                }
×
1374
                case col_type_Double: {
×
1375
                    ArrayDoubleNull arr(m_alloc);
×
1376
                    ref_type ref = Array::get_as_ref(j);
×
1377
                    arr.init_from_ref(ref);
×
1378
                    auto val = arr.get(i);
×
1379
                    if (val)
×
1380
                        std::cout << ", " << *val;
×
1381
                    else
×
1382
                        std::cout << ", null";
×
1383
                    break;
×
1384
                }
×
1385
                case col_type_String: {
×
1386
                    ArrayString arr(m_alloc);
×
1387
                    set_spec(arr, col.get_index());
×
1388
                    ref_type ref = Array::get_as_ref(j);
×
1389
                    arr.init_from_ref(ref);
×
1390
                    std::cout << ", " << arr.get(i);
×
1391
                    break;
×
1392
                }
×
1393
                case col_type_Binary: {
×
1394
                    ArrayBinary arr(m_alloc);
×
1395
                    ref_type ref = Array::get_as_ref(j);
×
1396
                    arr.init_from_ref(ref);
×
1397
                    std::cout << ", " << arr.get(i);
×
1398
                    break;
×
1399
                }
×
1400
                case col_type_Mixed: {
×
1401
                    ArrayMixed arr(m_alloc);
×
1402
                    ref_type ref = Array::get_as_ref(j);
×
1403
                    arr.init_from_ref(ref);
×
1404
                    std::cout << ", " << arr.get(i);
×
1405
                    break;
×
1406
                }
×
1407
                case col_type_Timestamp: {
×
1408
                    ArrayTimestamp arr(m_alloc);
×
1409
                    ref_type ref = Array::get_as_ref(j);
×
1410
                    arr.init_from_ref(ref);
×
1411
                    if (arr.is_null(i)) {
×
1412
                        std::cout << ", null";
×
1413
                    }
×
1414
                    else {
×
1415
                        std::cout << ", " << arr.get(i);
×
1416
                    }
×
1417
                    break;
×
1418
                }
×
1419
                case col_type_Decimal: {
×
1420
                    ArrayDecimal128 arr(m_alloc);
×
1421
                    ref_type ref = Array::get_as_ref(j);
×
1422
                    arr.init_from_ref(ref);
×
1423
                    if (arr.is_null(i)) {
×
1424
                        std::cout << ", null";
×
1425
                    }
×
1426
                    else {
×
1427
                        std::cout << ", " << arr.get(i);
×
1428
                    }
×
1429
                    break;
×
1430
                }
×
1431
                case col_type_ObjectId: {
×
1432
                    ArrayObjectIdNull 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_UUID: {
×
1444
                    ArrayUUIDNull 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_Link: {
×
1456
                    ArrayKey arr(m_alloc);
×
1457
                    ref_type ref = Array::get_as_ref(j);
×
1458
                    arr.init_from_ref(ref);
×
1459
                    std::cout << ", " << arr.get(i);
×
1460
                    break;
×
1461
                }
×
1462
                case col_type_BackLink: {
×
1463
                    break;
×
1464
                }
×
1465
                default:
×
1466
                    std::cout << ", Error";
×
1467
                    break;
×
1468
            }
×
1469
            return IteratorControl::AdvanceToNext;
×
1470
        });
×
1471
        std::cout << std::endl;
×
1472
    }
×
1473
}
×
1474
// LCOV_EXCL_STOP
1475

1476
void Cluster::remove_backlinks(const Table* origin_table, ObjKey origin_key, ColKey origin_col_key,
1477
                               const std::vector<ObjKey>& keys, CascadeState& state)
1478
{
66,900✔
1479
    TableRef target_table = origin_table->get_opposite_table(origin_col_key);
66,900✔
1480
    ColKey backlink_col_key = origin_table->get_opposite_column(origin_col_key);
66,900✔
1481
    bool strong_links = target_table->is_embedded();
66,900✔
1482

33,444✔
1483
    for (auto key : keys) {
68,526✔
1484
        if (key != null_key) {
68,529✔
1485
            bool is_unres = key.is_unresolved();
68,529✔
1486
            Obj target_obj = is_unres ? target_table->m_tombstones->get(key) : target_table->m_clusters.get(key);
68,514✔
1487
            bool last_removed = target_obj.remove_one_backlink(backlink_col_key, origin_key); // Throws
68,529✔
1488
            if (is_unres) {
68,529✔
1489
                if (last_removed) {
30✔
1490
                    // Check is there are more backlinks
12✔
1491
                    if (!target_obj.has_backlinks(false)) {
24✔
1492
                        // Tombstones can be erased right away - there is no cascading effect
9✔
1493
                        target_table->m_tombstones->erase(key, state);
18✔
1494
                    }
18✔
1495
                }
24✔
1496
            }
30✔
1497
            else {
68,499✔
1498
                state.enqueue_for_cascade(target_obj, strong_links, last_removed);
68,499✔
1499
            }
68,499✔
1500
        }
68,529✔
1501
    }
68,526✔
1502
}
66,900✔
1503

1504
void Cluster::remove_backlinks(const Table* origin_table, ObjKey origin_key, ColKey origin_col_key,
1505
                               const std::vector<ObjLink>& links, CascadeState& state)
1506
{
144✔
1507
    Group* group = origin_table->get_parent_group();
144✔
1508
    TableKey origin_table_key = origin_table->get_key();
144✔
1509

72✔
1510
    for (auto link : links) {
240✔
1511
        if (link) {
240✔
1512
            bool is_unres = link.get_obj_key().is_unresolved();
240✔
1513
            Obj target_obj = group->get_object(link);
240✔
1514
            TableRef target_table = target_obj.get_table();
240✔
1515
            ColKey backlink_col_key = target_table->find_or_add_backlink_column(origin_col_key, origin_table_key);
240✔
1516

120✔
1517
            bool last_removed = target_obj.remove_one_backlink(backlink_col_key, origin_key); // Throws
240✔
1518
            if (is_unres) {
240✔
1519
                if (last_removed) {
6✔
1520
                    // Check is there are more backlinks
3✔
1521
                    if (!target_obj.has_backlinks(false)) {
6✔
1522
                        // Tombstones can be erased right away - there is no cascading effect
3✔
1523
                        target_table->m_tombstones->erase(link.get_obj_key(), state);
6✔
1524
                    }
6✔
1525
                }
6✔
1526
            }
6✔
1527
            else {
234✔
1528
                state.enqueue_for_cascade(target_obj, false, last_removed);
234✔
1529
            }
234✔
1530
        }
240✔
1531
    }
240✔
1532
}
144✔
1533

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