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

realm / realm-core / 2214

10 Apr 2024 11:21PM UTC coverage: 91.813% (-0.8%) from 92.623%
2214

push

Evergreen

web-flow
Add missing availability checks for SecCopyErrorMessageString (#7577)

This requires iOS 11.3 and we currently target iOS 11.

94848 of 175770 branches covered (53.96%)

7 of 22 new or added lines in 2 files covered. (31.82%)

1815 existing lines in 77 files now uncovered.

242945 of 264608 relevant lines covered (91.81%)

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

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

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

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

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

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

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

97

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

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

133,791✔
105
    arr.add(RefOrTagged::make_tagged(0)); // Compact form
269,298✔
106
    return arr.get_mem();
269,298✔
107
}
269,298✔
108

109

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

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

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

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

207
void Cluster::init(MemRef mem)
208
{
193,099,275✔
209
    Array::init_from_mem(mem);
193,099,275✔
210
    auto rot = Array::get_as_ref_or_tagged(0);
193,099,275✔
211
    if (rot.is_tagged()) {
193,099,275✔
212
        m_keys.detach();
132,557,847✔
213
    }
132,557,847✔
214
    else {
60,541,428✔
215
        m_keys.init_from_ref(rot.get_as_ref());
60,541,428✔
216
    }
60,541,428✔
217
}
193,099,275✔
218

219
void Cluster::update_from_parent() noexcept
220
{
617,850✔
221
    Array::update_from_parent();
617,850✔
222
    auto rot = Array::get_as_ref_or_tagged(0);
617,850✔
223
    if (!rot.is_tagged()) {
617,850✔
224
        m_keys.update_from_parent();
24,024✔
225
    }
24,024✔
226
}
617,850✔
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,385,946✔
244
    auto rot = Array::get_as_ref_or_tagged(header, s_key_ref_or_size_index);
70,385,946✔
245
    if (rot.is_tagged()) {
70,385,946✔
246
        return size_t(rot.get_as_int());
70,061,100✔
247
    }
70,061,100✔
248
    else {
324,846✔
249
        return Array::get_size_from_header(alloc.translate(rot.get_as_ref()));
324,846✔
250
    }
324,846✔
251
}
70,385,946✔
252

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

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

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

15,886,110✔
269
    T arr(m_alloc);
32,034,105✔
270
    auto col_ndx = col.get_index();
32,034,105✔
271
    arr.set_parent(this, col_ndx.val + s_first_col_index);
32,034,105✔
272
    set_spec<T>(arr, col_ndx);
32,034,105✔
273
    arr.init_from_parent();
32,034,105✔
274
    if (init_val.is_null()) {
32,034,105✔
275
        arr.insert(ndx, T::default_value(nullable));
31,492,971✔
276
    }
31,492,971✔
277
    else {
541,134✔
278
        arr.insert(ndx, init_val.get<U>());
541,134✔
279
    }
541,134✔
280
}
32,034,105✔
281

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

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

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

8,004✔
308
    // Insert backlink if needed
8,004✔
309
    if (init_value.is_type(type_TypedLink)) {
16,308✔
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
}
16,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,078,778✔
348
    // Ensure the cluster array is big enough to hold 64 bit values.
11,529,495✔
349
    copy_on_write(m_size * 8);
23,078,778✔
350

11,529,495✔
351
    if (m_keys.is_attached()) {
23,078,778✔
352
        m_keys.insert(ndx, k.value);
1,584,261✔
353
    }
1,584,261✔
354
    else {
21,494,517✔
355
        Array::set(s_key_ref_or_size_index, Array::get(s_key_ref_or_size_index) + 2); // Increments size by 1
21,494,517✔
356
    }
21,494,517✔
357

11,529,495✔
358
    auto val = init_values.begin();
23,078,778✔
359
    auto insert_in_column = [&](ColKey col_key) {
33,832,596✔
360
        auto col_ndx = col_key.get_index();
33,832,596✔
361
        auto attr = col_key.get_attrs();
33,832,596✔
362
        Mixed init_value;
33,832,596✔
363
        // init_values must be sorted in col_ndx order - this is ensured by ClustTree::insert()
16,772,409✔
364
        if (val != init_values.end() && val->col_key.get_index().val == col_ndx.val) {
33,832,596✔
365
            init_value = val->value;
491,613✔
366
            ++val;
491,613✔
367
        }
491,613✔
368

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

16,436,559✔
379
        bool nullable = attr.test(col_attr_Nullable);
33,158,268✔
380
        switch (type) {
33,158,268✔
381
            case col_type_Int:
25,504,995✔
382
                if (attr.test(col_attr_Nullable)) {
25,504,995✔
383
                    do_insert_row<ArrayIntNull>(ndx, col_key, init_value, nullable);
1,371,501✔
384
                }
1,371,501✔
385
                else {
24,133,494✔
386
                    do_insert_row<ArrayInteger>(ndx, col_key, init_value, nullable);
24,133,494✔
387
                }
24,133,494✔
388
                break;
25,504,995✔
389
            case col_type_Bool:
187,569✔
390
                do_insert_row<ArrayBoolNull>(ndx, col_key, init_value, nullable);
187,569✔
391
                break;
187,569✔
392
            case col_type_Float:
328,947✔
393
                do_insert_row<ArrayFloatNull>(ndx, col_key, init_value, nullable);
328,947✔
394
                break;
328,947✔
395
            case col_type_Double:
1,491,774✔
396
                do_insert_row<ArrayDoubleNull>(ndx, col_key, init_value, nullable);
1,491,774✔
397
                break;
1,491,774✔
398
            case col_type_String:
2,581,797✔
399
                do_insert_row<ArrayString>(ndx, col_key, init_value, nullable);
2,581,797✔
400
                break;
2,581,797✔
401
            case col_type_Binary:
1,361,691✔
402
                do_insert_row<ArrayBinary>(ndx, col_key, init_value, nullable);
1,361,691✔
403
                break;
1,361,691✔
404
            case col_type_Mixed: {
16,308✔
405
                do_insert_mixed(ndx, col_key, init_value, ObjKey(k.value + get_offset()));
16,308✔
406
                break;
16,308✔
407
            }
×
408
            case col_type_Timestamp:
220,680✔
409
                do_insert_row<ArrayTimestamp>(ndx, col_key, init_value, nullable);
220,680✔
410
                break;
220,680✔
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:
168,522✔
415
                do_insert_row<ArrayObjectIdNull>(ndx, col_key, init_value, nullable);
168,522✔
416
                break;
168,522✔
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:
260,061✔
421
                do_insert_key(ndx, col_key, init_value, ObjKey(k.value + get_offset()));
260,061✔
422
                break;
260,061✔
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,661✔
427
                ArrayBacklink arr(m_alloc);
962,661✔
428
                arr.set_parent(this, col_ndx.val + s_first_col_index);
962,661✔
429
                arr.init_from_parent();
962,661✔
430
                arr.insert(ndx, 0);
962,661✔
431
                break;
962,661✔
432
            }
×
433
            default:
✔
434
                REALM_ASSERT(false);
×
435
                break;
×
436
        }
33,091,038✔
437
        return IteratorControl::AdvanceToNext;
33,091,038✔
438
    };
33,091,038✔
439
    m_tree_top.m_owner->for_each_and_every_column(insert_in_column);
23,078,778✔
440
}
23,078,778✔
441

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

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

10,911✔
454
    src.move(dst, ndx);
21,918✔
455
}
21,918✔
456

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

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

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

10,896✔
470
        switch (type) {
21,888✔
471
            case col_type_Int:
21,018✔
472
                if (attr.test(col_attr_Nullable)) {
21,018✔
473
                    do_move<ArrayIntNull>(ndx, col_key, new_leaf);
9,510✔
474
                }
9,510✔
475
                else {
11,508✔
476
                    do_move<ArrayInteger>(ndx, col_key, new_leaf);
11,508✔
477
                }
11,508✔
478
                break;
21,018✔
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,888✔
526
        return IteratorControl::AdvanceToNext;
21,888✔
527
    };
21,888✔
528
    m_tree_top.m_owner->for_each_and_every_column(move_from_column);
11,526✔
529
    for (size_t i = ndx; i < m_keys.size(); i++) {
1,232,208✔
530
        new_leaf->m_keys.add(m_keys.get(i) - offset);
1,220,682✔
531
    }
1,220,682✔
532
    m_keys.truncate(ndx);
11,526✔
533
}
11,526✔
534

535
Cluster::~Cluster() {}
198,398,340✔
536

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

545
void Cluster::ensure_general_form()
546
{
61,404✔
547
    if (!m_keys.is_attached()) {
61,404✔
548
        size_t current_size = get_size_in_compact_form();
48,078✔
549
        m_keys.create(current_size, 255);
48,078✔
550
        m_keys.update_parent();
48,078✔
551
        for (size_t i = 0; i < current_size; i++) {
5,514,153✔
552
            m_keys.set(i, i);
5,466,075✔
553
        }
5,466,075✔
554
    }
48,078✔
555
}
61,404✔
556

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

271,767✔
562
    T arr(m_alloc);
551,697✔
563
    arr.create();
551,697✔
564
    auto val = T::default_value(nullable);
551,697✔
565
    for (size_t i = 0; i < sz; i++) {
6,787,512!
566
        arr.add(val);
6,235,815✔
567
    }
6,235,815✔
568
    auto col_ndx = col_key.get_index();
551,697✔
569
    unsigned ndx = col_ndx.val + s_first_col_index;
551,697✔
570

271,767✔
571
    // Fill up if indexes are not consecutive
271,767✔
572
    while (size() < ndx)
551,697!
573
        Array::add(0);
×
574

271,767✔
575
    if (ndx == size())
551,697!
576
        Array::insert(ndx, from_ref(arr.get_ref()));
551,613✔
577
    else
84✔
578
        Array::set(ndx, from_ref(arr.get_ref()));
84✔
579
}
551,697✔
580

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

95,292✔
588
        ArrayRef arr(m_alloc);
191,586✔
589
        arr.create(sz);
191,586✔
590
        auto col_ndx = col_key.get_index();
191,586✔
591
        unsigned idx = col_ndx.val + s_first_col_index;
191,586✔
592
        if (idx == size())
191,586✔
593
            Array::insert(idx, from_ref(arr.get_ref()));
191,586✔
UNCOV
594
        else
×
UNCOV
595
            Array::set(idx, from_ref(arr.get_ref()));
×
596
        return;
191,586✔
597
    }
191,586✔
598
    bool nullable = attr.test(col_attr_Nullable);
551,700✔
599
    switch (type) {
551,700✔
600
        case col_type_Int:
247,818✔
601
            if (nullable) {
247,818✔
602
                do_insert_column<ArrayIntNull>(col_key, nullable);
21,075✔
603
            }
21,075✔
604
            else {
226,743✔
605
                do_insert_column<ArrayInteger>(col_key, nullable);
226,743✔
606
            }
226,743✔
607
            break;
247,818✔
608
        case col_type_Bool:
5,493✔
609
            do_insert_column<ArrayBoolNull>(col_key, nullable);
5,493✔
610
            break;
5,493✔
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:
76,002✔
618
            do_insert_column<ArrayString>(col_key, nullable);
76,002✔
619
            break;
76,002✔
620
        case col_type_Binary:
6,822✔
621
            do_insert_column<ArrayBinary>(col_key, nullable);
6,822✔
622
            break;
6,822✔
623
        case col_type_Mixed:
6,408✔
624
            do_insert_column<ArrayMixed>(col_key, nullable);
6,408✔
625
            break;
6,408✔
626
        case col_type_Timestamp:
23,085✔
627
            do_insert_column<ArrayTimestamp>(col_key, nullable);
23,085✔
628
            break;
23,085✔
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:
48,711✔
633
            do_insert_column<ArrayObjectIdNull>(col_key, nullable);
48,711✔
634
            break;
48,711✔
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,510✔
639
            do_insert_column<ArrayKey>(col_key, nullable);
30,510✔
640
            break;
30,510✔
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
    }
551,700✔
651
}
551,700✔
652

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

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

11,533,431✔
674
    auto on_error = [&] {
11,533,437✔
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,533,431✔
679
    if (m_keys.is_attached()) {
23,063,802✔
680
        sz = m_keys.size();
1,573,113✔
681
        ndx = m_keys.lower_bound(uint64_t(k.value));
1,573,113✔
682
        if (ndx < sz) {
1,573,113✔
683
            current_key_value = m_keys.get(ndx);
617,040✔
684
            if (k.value == current_key_value) {
617,040✔
685
                on_error();
12✔
686
            }
12✔
687
        }
617,040✔
688
    }
1,573,113✔
689
    else {
21,490,689✔
690
        sz = size_t(Array::get(s_key_ref_or_size_index)) >> 1; // Size is stored as tagged integer
21,490,689✔
691
        if (uint64_t(k.value) < sz) {
21,490,689✔
692
            on_error();
×
693
        }
×
694
        // Key value is bigger than all other values, should be put last
10,748,421✔
695
        ndx = sz;
21,490,689✔
696
        if (uint64_t(k.value) > sz && sz < cluster_node_size) {
21,490,689✔
697
            ensure_general_form();
16,761✔
698
        }
16,761✔
699
    }
21,490,689✔
700

11,533,431✔
701
    REALM_ASSERT_DEBUG(sz <= cluster_node_size);
23,063,802✔
702
    if (REALM_LIKELY(sz < cluster_node_size)) {
23,063,802✔
703
        insert_row(ndx, k, init_values); // Throws
22,962,231✔
704
        state.mem = get_mem();
22,962,231✔
705
        state.index = ndx;
22,962,231✔
706
    }
22,962,231✔
707
    else {
101,571✔
708
        // Split leaf node
80,055✔
709
        Cluster new_leaf(0, m_alloc, m_tree_top);
101,571✔
710
        new_leaf.create();
101,571✔
711
        if (ndx == sz) {
122,118✔
712
            new_leaf.insert_row(0, ObjKey(0), init_values); // Throws
84,183✔
713
            state.split_key = k.value;
84,183✔
714
            state.mem = new_leaf.get_mem();
84,183✔
715
            state.index = 0;
84,183✔
716
        }
84,183✔
717
        else {
2,147,521,582✔
718
            // Current cluster must be in general form to get here
37,935✔
719
            REALM_ASSERT_DEBUG(m_keys.is_attached());
2,147,521,582✔
720
            new_leaf.ensure_general_form();
2,147,521,582✔
721
            move(ndx, &new_leaf, current_key_value);
2,147,521,582✔
722
            insert_row(ndx, k, init_values); // Throws
2,147,521,582✔
723
            state.mem = get_mem();
2,147,521,582✔
724
            state.split_key = current_key_value;
2,147,521,582✔
725
            state.index = ndx;
2,147,521,582✔
726
        }
2,147,521,582✔
727
        ret = new_leaf.get_ref();
101,571✔
728
    }
101,571✔
729

11,533,431✔
730
    return ret;
23,063,802✔
731
}
23,063,802✔
732

733
bool Cluster::try_get(ObjKey k, ClusterNode::State& state) const noexcept
734
{
147,981,273✔
735
    state.mem = get_mem();
147,981,273✔
736
    if (m_keys.is_attached()) {
147,981,273✔
737
        state.index = m_keys.lower_bound(uint64_t(k.value));
50,205,336✔
738
        return state.index != m_keys.size() && m_keys.get(state.index) == uint64_t(k.value);
50,205,336✔
739
    }
50,205,336✔
740
    else {
97,775,937✔
741
        if (uint64_t(k.value) < uint64_t(Array::get(s_key_ref_or_size_index) >> 1)) {
97,775,937✔
742
            state.index = size_t(k.value);
80,058,747✔
743
            return true;
80,058,747✔
744
        }
80,058,747✔
745
    }
17,717,190✔
746
    return false;
17,717,190✔
747
}
17,717,190✔
748

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

756
template <class T>
757
inline void Cluster::do_erase(size_t ndx, ColKey col_key)
758
{
6,602,949✔
759
    auto col_ndx = col_key.get_index();
6,602,949✔
760
    T values(m_alloc);
6,602,949✔
761
    values.set_parent(this, col_ndx.val + s_first_col_index);
6,602,949✔
762
    set_spec<T>(values, col_ndx);
6,602,949✔
763
    values.init_from_parent();
6,602,949✔
764
    ObjLink link;
6,602,949✔
765
    if constexpr (std::is_same_v<T, ArrayTypedLink>) {
6,602,949✔
766
        link = values.get(ndx);
×
767
    }
×
768
    if constexpr (std::is_same_v<T, ArrayMixed>) {
6,602,949✔
769
        Mixed value = values.get(ndx);
753✔
770
        if (value.is_type(type_TypedLink)) {
753✔
771
            link = value.get<ObjLink>();
24✔
772
        }
24✔
773
    }
753✔
774
    if (link) {
6,602,949!
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,602,949✔
784
}
6,602,949✔
785

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

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

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

818
size_t Cluster::erase(ObjKey key, CascadeState& state)
819
{
5,000,475✔
820
    size_t ndx = get_ndx(key, 0);
5,000,475✔
821
    if (ndx == realm::npos)
5,000,475✔
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;
5,000,457✔
824

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

15,420✔
835
            if (ref) {
30,930✔
836
                const Table* origin_table = m_tree_top.get_owning_table();
11,346✔
837
                if (attr.test(col_attr_Dictionary)) {
11,346✔
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,691✔
845
                    BPlusTree<ObjKey> links(m_alloc);
3,879✔
846
                    links.init_from_ref(ref);
3,879✔
847
                    if (links.size() > 0) {
3,879✔
848
                        do_remove_backlinks(ObjKey(key.value + m_offset), col_key, links.get_all(), state);
1,032✔
849
                    }
1,032✔
850
                }
3,879✔
851
                else if (col_type == col_type_TypedLink) {
4,812✔
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,812✔
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,346✔
868
            }
11,346✔
869

15,420✔
870
            values.erase(ndx);
30,930✔
871

15,420✔
872
            return IteratorControl::AdvanceToNext;
30,930✔
873
        }
30,930✔
874

3,335,022✔
875
        switch (col_type) {
6,670,764✔
876
            case col_type_Int:
5,983,641✔
877
                if (attr.test(col_attr_Nullable)) {
5,983,641✔
878
                    do_erase<ArrayIntNull>(ndx, col_key);
1,275,927✔
879
                }
1,275,927✔
880
                else {
4,707,714✔
881
                    do_erase<ArrayInteger>(ndx, col_key);
4,707,714✔
882
                }
4,707,714✔
883
                break;
5,983,641✔
884
            case col_type_Bool:
42,429✔
885
                do_erase<ArrayBoolNull>(ndx, col_key);
42,429✔
886
                break;
42,429✔
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,504✔
891
                do_erase<ArrayDoubleNull>(ndx, col_key);
36,504✔
892
                break;
36,504✔
893
            case col_type_String:
75,318✔
894
                do_erase<ArrayString>(ndx, col_key);
75,318✔
895
                break;
75,318✔
896
            case col_type_Binary:
39,600✔
897
                do_erase<ArrayBinary>(ndx, col_key);
39,600✔
898
                break;
39,600✔
899
            case col_type_Mixed:
753✔
900
                do_erase<ArrayMixed>(ndx, col_key);
753✔
901
                break;
753✔
902
            case col_type_Timestamp:
45,240✔
903
                do_erase<ArrayTimestamp>(ndx, col_key);
45,240✔
904
                break;
45,240✔
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,814✔
909
                do_erase<ArrayObjectIdNull>(ndx, col_key);
38,814✔
910
                break;
38,814✔
911
            case col_type_UUID:
60,285✔
912
                do_erase<ArrayUUIDNull>(ndx, col_key);
60,285✔
913
                break;
60,285✔
914
            case col_type_Link:
67,986✔
915
                do_erase_key(ndx, col_key, state);
67,986✔
916
                break;
67,986✔
917
            case col_type_TypedLink:
✔
918
                do_erase<ArrayTypedLink>(ndx, col_key);
×
919
                break;
×
920
            case col_type_BackLink:
207,585✔
921
                if (state.m_mode == CascadeState::Mode::None) {
207,585✔
922
                    do_erase<ArrayBacklink>(ndx, col_key);
188,454✔
923
                }
188,454✔
924
                else {
19,131✔
925
                    // Postpone the deletion of backlink entries or else the
9,546✔
926
                    // checks for if there's any remaining backlinks will
9,546✔
927
                    // check the wrong row for columns which have already
9,546✔
928
                    // had values erased from them.
9,546✔
929
                    backlink_column_keys.push_back(col_key);
19,131✔
930
                }
19,131✔
931
                break;
207,585✔
932
            default:
✔
933
                REALM_ASSERT(false);
×
934
                break;
×
935
        }
6,670,782✔
936
        return IteratorControl::AdvanceToNext;
6,670,782✔
937
    };
6,670,782✔
938
    m_tree_top.m_owner->for_each_and_every_column(erase_in_column);
5,000,457✔
939

2,500,140✔
940
    // Any remaining backlink columns to erase from?
2,500,140✔
941
    for (auto k : backlink_column_keys)
5,000,457✔
942
        do_erase<ArrayBacklink>(ndx, k);
19,131✔
943

2,500,140✔
944
    if (m_keys.is_attached()) {
5,000,457✔
945
        m_keys.erase(ndx);
4,960,113✔
946
    }
4,960,113✔
947
    else {
40,344✔
948
        size_t current_size = get_size_in_compact_form();
40,344✔
949
        if (ndx == current_size - 1) {
40,344✔
950
            // When deleting last, we can still maintain compact form
12,966✔
951
            set(0, RefOrTagged::make_tagged(current_size - 1));
25,980✔
952
        }
25,980✔
953
        else {
14,364✔
954
            ensure_general_form();
14,364✔
955
            m_keys.erase(ndx);
14,364✔
956
        }
14,364✔
957
    }
40,344✔
958

2,500,140✔
959
    return node_size();
5,000,457✔
960
}
5,000,457✔
961

962
void Cluster::nullify_incoming_links(ObjKey key, CascadeState& state)
963
{
4,902,039✔
964
    size_t ndx = get_ndx(key, 0);
4,902,039✔
965
    if (ndx == realm::npos)
4,902,039✔
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,940✔
969
    // We must start with backlink columns in case the corresponding link
2,450,940✔
970
    // columns are in the same table so that we can nullify links before
2,450,940✔
971
    // erasing rows in the link columns.
2,450,940✔
972
    //
2,450,940✔
973
    // This phase also generates replication instructions documenting the side-
2,450,940✔
974
    // effects of deleting the object (i.e. link nullifications). These instructions
2,450,940✔
975
    // must come before the actual deletion of the object, but at the same time
2,450,940✔
976
    // the Replication object may need a consistent view of the row (not including
2,450,940✔
977
    // link columns). Therefore we first nullify links to this object, then
2,450,940✔
978
    // generate the instruction, and then delete the row in the remaining columns.
2,450,940✔
979

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

95,091✔
997
        return IteratorControl::AdvanceToNext;
190,254✔
998
    };
190,254✔
999

2,450,940✔
1000
    m_tree_top.get_owning_table()->for_each_backlink_column(nullify_fwd_links);
4,902,039✔
1001
}
4,902,039✔
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
{
7,728,609✔
1024
    auto col_ndx = col_key.get_index();
7,728,609✔
1025
    // FIXME: Move this validation into callers.
3,284,256✔
1026
    // Currently, the query subsystem may call with an unvalidated key.
3,284,256✔
1027
    // once fixed, reintroduce the noexcept declaration :-D
3,284,256✔
1028
    if (auto t = m_tree_top.get_owning_table())
7,728,609✔
1029
        t->check_column(col_key);
8,256,333✔
1030
    ref_type ref = to_ref(Array::get(col_ndx.val + 1));
7,728,609✔
1031
    if (leaf->need_spec()) {
7,728,609✔
1032
        m_tree_top.set_spec(*leaf, col_ndx);
134,487✔
1033
    }
134,487✔
1034
    leaf->init_from_ref(ref);
7,728,609✔
1035
    leaf->set_parent(const_cast<Cluster*>(this), col_ndx.val + 1);
7,728,609✔
1036
}
7,728,609✔
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
{
539,301✔
1048
    ArrayType arr(get_alloc());
539,301✔
1049
    set_spec(arr, ColKey::Idx{unsigned(index) - 1});
539,301✔
1050
    arr.set_parent(const_cast<Cluster*>(this), index);
539,301✔
1051
    arr.init_from_ref(ref);
539,301✔
1052
    arr.verify();
539,301✔
1053
    if (sz) {
539,301!
1054
        REALM_ASSERT(arr.size() == *sz);
184,290!
1055
    }
184,290✔
1056
    else {
355,011✔
1057
        sz = arr.size();
355,011✔
1058
    }
355,011✔
1059
}
539,301✔
1060
namespace {
1061

1062
template <typename ArrayType>
1063
void verify_list(ArrayRef& arr, size_t sz)
1064
{
83,778✔
1065
    for (size_t n = 0; n < sz; n++) {
294,591!
1066
        if (ref_type bp_tree_ref = arr.get(n)) {
210,813!
1067
            BPlusTree<ArrayType> links(arr.get_alloc());
107,397✔
1068
            links.init_from_ref(bp_tree_ref);
107,397✔
1069
            links.set_parent(&arr, n);
107,397✔
1070
            links.verify();
107,397✔
1071
        }
107,397✔
1072
    }
210,813✔
1073
}
83,778✔
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
{
391,524✔
1094
#ifdef REALM_DEBUG
391,524✔
1095
    util::Optional<size_t> sz;
391,524✔
1096

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

311,001✔
1104
        if (attr.test(col_attr_List)) {
635,439✔
1105
            ArrayRef arr(get_alloc());
83,790✔
1106
            arr.set_parent(const_cast<Cluster*>(this), col);
83,790✔
1107
            arr.init_from_ref(ref);
83,790✔
1108
            arr.verify();
83,790✔
1109
            if (sz) {
83,790✔
1110
                REALM_ASSERT(arr.size() == *sz);
59,538✔
1111
            }
59,538✔
1112
            else {
24,252✔
1113
                sz = arr.size();
24,252✔
1114
            }
24,252✔
1115

43,251✔
1116
            switch (col_type) {
83,790✔
1117
                case col_type_Int:
11,265✔
1118
                    if (nullable) {
11,265✔
1119
                        verify_list<util::Optional<int64_t>>(arr, *sz);
×
1120
                    }
×
1121
                    else {
11,265✔
1122
                        verify_list<int64_t>(arr, *sz);
11,265✔
1123
                    }
11,265✔
1124
                    break;
11,265✔
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:
47,421✔
1135
                    verify_list<String>(arr, *sz);
47,421✔
1136
                    break;
47,421✔
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,086✔
1153
                    verify_list<ObjKey>(arr, *sz);
1,086✔
1154
                    break;
1,086✔
1155
                default:
12✔
1156
                    // FIXME: Nullable primitives
6✔
1157
                    break;
12✔
1158
            }
83,790✔
1159
            return IteratorControl::AdvanceToNext;
83,790✔
1160
        }
83,790✔
1161
        else if (attr.test(col_attr_Dictionary)) {
551,649✔
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,180✔
1175
                    dict.verify();
12,180✔
1176
                }
12,180✔
1177
            }
12,270✔
1178
            return IteratorControl::AdvanceToNext;
12,156✔
1179
        }
12,156✔
1180
        else if (attr.test(col_attr_Set)) {
539,493✔
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

261,558✔
1237
        switch (col_type) {
539,265✔
1238
            case col_type_Int:
328,404✔
1239
                if (nullable) {
328,404✔
1240
                    verify<ArrayIntNull>(ref, col, sz);
34,197✔
1241
                }
34,197✔
1242
                else {
294,207✔
1243
                    verify<ArrayInteger>(ref, col, sz);
294,207✔
1244
                }
294,207✔
1245
                break;
328,404✔
1246
            case col_type_Bool:
7,200✔
1247
                verify<ArrayBoolNull>(ref, col, sz);
7,200✔
1248
                break;
7,200✔
1249
            case col_type_Float:
159✔
1250
                verify<ArrayFloatNull>(ref, col, sz);
159✔
1251
                break;
159✔
1252
            case col_type_Double:
156✔
1253
                verify<ArrayDoubleNull>(ref, col, sz);
156✔
1254
                break;
156✔
1255
            case col_type_String:
140,694✔
1256
                verify<ArrayString>(ref, col, sz);
140,694✔
1257
                break;
140,694✔
1258
            case col_type_Binary:
49,872✔
1259
                verify<ArrayBinary>(ref, col, sz);
49,872✔
1260
                break;
49,872✔
1261
            case col_type_Mixed:
1,734✔
1262
                verify<ArrayMixed>(ref, col, sz);
1,734✔
1263
                break;
1,734✔
1264
            case col_type_Timestamp:
6,948✔
1265
                verify<ArrayTimestamp>(ref, col, sz);
6,948✔
1266
                break;
6,948✔
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,590✔
1277
                verify<ArrayKey>(ref, col, sz);
1,590✔
1278
                break;
1,590✔
1279
            case col_type_BackLink:
2,460✔
1280
                verify<ArrayBacklink>(ref, col, sz);
2,460✔
1281
                break;
2,460✔
1282
            default:
✔
1283
                break;
×
1284
        }
539,274✔
1285
        return IteratorControl::AdvanceToNext;
539,274✔
1286
    };
539,274✔
1287

194,859✔
1288
    m_tree_top.m_owner->for_each_and_every_column(verify_column);
391,524✔
1289
#endif
391,524✔
1290
}
391,524✔
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
{
67,071✔
1479
    TableRef target_table = origin_table->get_opposite_table(origin_col_key);
67,071✔
1480
    ColKey backlink_col_key = origin_table->get_opposite_column(origin_col_key);
67,071✔
1481
    bool strong_links = target_table->is_embedded();
67,071✔
1482

33,534✔
1483
    for (auto key : keys) {
68,703✔
1484
        if (key != null_key) {
68,703✔
1485
            bool is_unres = key.is_unresolved();
68,703✔
1486
            Obj target_obj = is_unres ? target_table->m_tombstones->get(key) : target_table->m_clusters.get(key);
68,688✔
1487
            bool last_removed = target_obj.remove_one_backlink(backlink_col_key, origin_key); // Throws
68,703✔
1488
            if (is_unres) {
68,703✔
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,673✔
1498
                state.enqueue_for_cascade(target_obj, strong_links, last_removed);
68,673✔
1499
            }
68,673✔
1500
        }
68,703✔
1501
    }
68,703✔
1502
}
67,071✔
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

© 2026 Coveralls, Inc