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

realm / realm-core / james.stone_381

25 Sep 2023 06:35PM UTC coverage: 90.919% (+0.03%) from 90.892%
james.stone_381

Pull #6670

Evergreen

ironage
optimize: only compare strings once
Pull Request #6670: Sorting stage 3

97114 of 177952 branches covered (0.0%)

879 of 887 new or added lines in 12 files covered. (99.1%)

105 existing lines in 17 files now uncovered.

236103 of 259684 relevant lines covered (90.92%)

7062315.99 hits per line

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

77.44
/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
{
599,013✔
47
    if (m_values.size() > 1) {
599,013✔
48
        // Sort according to ColKey index
8,124✔
49
        std::sort(m_values.begin(), m_values.end(), [](const auto& a, const auto& b) {
48,639✔
50
            return a.col_key.get_index().val < b.col_key.get_index().val;
48,639✔
51
        });
48,639✔
52
    }
16,248✔
53
}
599,013✔
54

55
void FieldValues::insert(ColKey k, Mixed val, bool is_default)
56
{
503,577✔
57
    if (m_values.empty()) {
503,577✔
58
        m_values.emplace_back(k, val, is_default);
492,711✔
59
        return;
492,711✔
60
    }
492,711✔
61
    unsigned int idx = k.get_index().val;
10,866✔
62
    auto it = std::lower_bound(m_values.begin(), m_values.end(), idx, [](const auto& a, unsigned int i) {
21,042✔
63
        return a.col_key.get_index().val < i;
21,042✔
64
    });
21,042✔
65
    m_values.insert(it, {k, val, is_default});
10,866✔
66
}
10,866✔
67

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

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

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

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

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

97

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

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

172,896✔
105
    arr.add(RefOrTagged::make_tagged(0)); // Compact form
348,351✔
106
    return arr.get_mem();
348,351✔
107
}
348,351✔
108

109

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

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

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

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

207
void Cluster::init(MemRef mem)
208
{
219,384,642✔
209
    Array::init_from_mem(mem);
219,384,642✔
210
    auto rot = Array::get_as_ref_or_tagged(0);
219,384,642✔
211
    if (rot.is_tagged()) {
219,384,642✔
212
        m_keys.detach();
153,951,621✔
213
    }
153,951,621✔
214
    else {
65,433,021✔
215
        m_keys.init_from_ref(rot.get_as_ref());
65,433,021✔
216
    }
65,433,021✔
217
}
219,384,642✔
218

219
void Cluster::update_from_parent() noexcept
220
{
725,253✔
221
    Array::update_from_parent();
725,253✔
222
    auto rot = Array::get_as_ref_or_tagged(0);
725,253✔
223
    if (!rot.is_tagged()) {
725,253✔
224
        m_keys.update_from_parent();
21,318✔
225
    }
21,318✔
226
}
725,253✔
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
{
50,710,128✔
244
    auto rot = Array::get_as_ref_or_tagged(header, s_key_ref_or_size_index);
50,710,128✔
245
    if (rot.is_tagged()) {
50,721,654✔
246
        return size_t(rot.get_as_int());
50,704,698✔
247
    }
50,704,698✔
248
    else {
2,147,500,603✔
249
        return Array::get_size_from_header(alloc.translate(rot.get_as_ref()));
2,147,500,603✔
250
    }
2,147,500,603✔
251
}
50,710,128✔
252

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

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

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

16,000,737✔
269
    T arr(m_alloc);
32,193,105✔
270
    auto col_ndx = col.get_index();
32,193,105✔
271
    arr.set_parent(this, col_ndx.val + s_first_col_index);
32,193,105✔
272
    set_spec<T>(arr, col_ndx);
32,193,105✔
273
    arr.init_from_parent();
32,193,105✔
274
    if (init_val.is_null()) {
32,193,105✔
275
        arr.insert(ndx, T::default_value(nullable));
31,569,459✔
276
    }
31,569,459✔
277
    else {
623,646✔
278
        arr.insert(ndx, init_val.get<U>());
623,646✔
279
    }
623,646✔
280
}
32,193,105✔
281

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

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

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

6,531✔
308
    // Insert backlink if needed
6,531✔
309
    if (init_value.is_type(type_TypedLink)) {
13,362✔
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
}
13,362✔
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,038,269✔
348
    // Ensure the cluster array is big enough to hold 64 bit values.
11,462,121✔
349
    copy_on_write(m_size * 8);
23,038,269✔
350

11,462,121✔
351
    if (m_keys.is_attached()) {
23,038,269✔
352
        m_keys.insert(ndx, k.value);
1,655,292✔
353
    }
1,655,292✔
354
    else {
21,382,977✔
355
        Array::set(s_key_ref_or_size_index, Array::get(s_key_ref_or_size_index) + 2); // Increments size by 1
21,382,977✔
356
    }
21,382,977✔
357

11,462,121✔
358
    auto val = init_values.begin();
23,038,269✔
359
    auto insert_in_column = [&](ColKey col_key) {
33,975,900✔
360
        auto col_ndx = col_key.get_index();
33,975,900✔
361
        auto attr = col_key.get_attrs();
33,975,900✔
362
        Mixed init_value;
33,975,900✔
363
        // init_values must be sorted in col_ndx order - this is ensured by ClustTree::insert()
16,878,156✔
364
        if (val != init_values.end() && val->col_key.get_index().val == col_ndx.val) {
33,975,900✔
365
            init_value = val->value;
580,281✔
366
            ++val;
580,281✔
367
        }
580,281✔
368

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

16,557,282✔
379
        bool nullable = attr.test(col_attr_Nullable);
33,329,862✔
380
        switch (type) {
33,329,862✔
381
            case col_type_Int:
25,549,611✔
382
                if (attr.test(col_attr_Nullable)) {
25,549,611✔
383
                    do_insert_row<ArrayIntNull>(ndx, col_key, init_value, nullable);
1,385,970✔
384
                }
1,385,970✔
385
                else {
24,163,641✔
386
                    do_insert_row<ArrayInteger>(ndx, col_key, init_value, nullable);
24,163,641✔
387
                }
24,163,641✔
388
                break;
25,549,611✔
389
            case col_type_Bool:
200,607✔
390
                do_insert_row<ArrayBoolNull>(ndx, col_key, init_value, nullable);
200,607✔
391
                break;
200,607✔
392
            case col_type_Float:
328,632✔
393
                do_insert_row<ArrayFloatNull>(ndx, col_key, init_value, nullable);
328,632✔
394
                break;
328,632✔
395
            case col_type_Double:
1,491,066✔
396
                do_insert_row<ArrayDoubleNull>(ndx, col_key, init_value, nullable);
1,491,066✔
397
                break;
1,491,066✔
398
            case col_type_String:
2,726,862✔
399
                do_insert_row<ArrayString>(ndx, col_key, init_value, nullable);
2,726,862✔
400
                break;
2,726,862✔
401
            case col_type_Binary:
1,317,231✔
402
                do_insert_row<ArrayBinary>(ndx, col_key, init_value, nullable);
1,317,231✔
403
                break;
1,317,231✔
404
            case col_type_Mixed: {
13,362✔
405
                do_insert_mixed(ndx, col_key, init_value, ObjKey(k.value + get_offset()));
13,362✔
406
                break;
13,362✔
407
            }
×
408
            case col_type_Timestamp:
218,202✔
409
                do_insert_row<ArrayTimestamp>(ndx, col_key, init_value, nullable);
218,202✔
410
                break;
218,202✔
411
            case col_type_Decimal:
75,612✔
412
                do_insert_row<ArrayDecimal128>(ndx, col_key, init_value, nullable);
75,612✔
413
                break;
75,612✔
414
            case col_type_ObjectId:
162,471✔
415
                do_insert_row<ArrayObjectIdNull>(ndx, col_key, init_value, nullable);
162,471✔
416
                break;
162,471✔
417
            case col_type_UUID:
115,818✔
418
                do_insert_row<ArrayUUIDNull>(ndx, col_key, init_value, nullable);
115,818✔
419
                break;
115,818✔
420
            case col_type_Link:
250,461✔
421
                do_insert_key(ndx, col_key, init_value, ObjKey(k.value + get_offset()));
250,461✔
422
                break;
250,461✔
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: {
952,704✔
427
                ArrayBacklink arr(m_alloc);
952,704✔
428
                arr.set_parent(this, col_ndx.val + s_first_col_index);
952,704✔
429
                arr.init_from_parent();
952,704✔
430
                arr.insert(ndx, 0);
952,704✔
431
                break;
952,704✔
432
            }
×
433
            default:
✔
434
                REALM_ASSERT(false);
×
435
                break;
×
436
        }
33,250,626✔
437
        return IteratorControl::AdvanceToNext;
33,250,626✔
438
    };
33,250,626✔
439
    m_tree_top.m_owner->for_each_and_every_column(insert_in_column);
23,038,269✔
440
}
23,038,269✔
441

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

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

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

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

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

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

11,178✔
470
        switch (type) {
22,152✔
471
            case col_type_Int:
21,282✔
472
                if (attr.test(col_attr_Nullable)) {
21,282✔
473
                    do_move<ArrayIntNull>(ndx, col_key, new_leaf);
9,639✔
474
                }
9,639✔
475
                else {
11,643✔
476
                    do_move<ArrayInteger>(ndx, col_key, new_leaf);
11,643✔
477
                }
11,643✔
478
                break;
21,282✔
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,152✔
526
        return IteratorControl::AdvanceToNext;
22,152✔
527
    };
22,152✔
528
    m_tree_top.m_owner->for_each_and_every_column(move_from_column);
11,661✔
529
    for (size_t i = ndx; i < m_keys.size(); i++) {
1,249,233✔
530
        new_leaf->m_keys.add(m_keys.get(i) - offset);
1,237,572✔
531
    }
1,237,572✔
532
    m_keys.truncate(ndx);
11,661✔
533
}
11,661✔
534

535
Cluster::~Cluster() {}
219,554,409✔
536

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

545
void Cluster::ensure_general_form()
546
{
83,034✔
547
    if (!m_keys.is_attached()) {
83,034✔
548
        size_t current_size = get_size_in_compact_form();
69,567✔
549
        m_keys.create(current_size, 255);
69,567✔
550
        m_keys.update_parent();
69,567✔
551
        for (size_t i = 0; i < current_size; i++) {
5,548,464✔
552
            m_keys.set(i, i);
5,478,897✔
553
        }
5,478,897✔
554
    }
69,567✔
555
}
83,034✔
556

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

410,760✔
562
    T arr(m_alloc);
833,436✔
563
    arr.create();
833,436✔
564
    auto val = T::default_value(nullable);
833,436✔
565
    for (size_t i = 0; i < sz; i++) {
7,116,435!
566
        arr.add(val);
6,282,999✔
567
    }
6,282,999✔
568
    auto col_ndx = col_key.get_index();
833,436✔
569
    unsigned ndx = col_ndx.val + s_first_col_index;
833,436✔
570

410,760✔
571
    // Fill up if indexes are not consecutive
410,760✔
572
    while (size() < ndx)
833,436!
573
        Array::add(0);
×
574

410,760✔
575
    if (ndx == size())
833,436!
576
        Array::insert(ndx, from_ref(arr.get_ref()));
833,355✔
577
    else
81✔
578
        Array::set(ndx, from_ref(arr.get_ref()));
81✔
579
}
833,436✔
580

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

109,335✔
588
        ArrayRef arr(m_alloc);
220,230✔
589
        arr.create(sz);
220,230✔
590
        auto col_ndx = col_key.get_index();
220,230✔
591
        unsigned idx = col_ndx.val + s_first_col_index;
220,230✔
592
        if (idx == size())
220,230✔
593
            Array::insert(idx, from_ref(arr.get_ref()));
220,230✔
UNCOV
594
        else
×
UNCOV
595
            Array::set(idx, from_ref(arr.get_ref()));
×
596
        return;
220,230✔
597
    }
220,230✔
598
    bool nullable = attr.test(col_attr_Nullable);
833,433✔
599
    switch (type) {
833,433✔
600
        case col_type_Int:
311,328✔
601
            if (nullable) {
311,328✔
602
                do_insert_column<ArrayIntNull>(col_key, nullable);
24,825✔
603
            }
24,825✔
604
            else {
286,503✔
605
                do_insert_column<ArrayInteger>(col_key, nullable);
286,503✔
606
            }
286,503✔
607
            break;
311,328✔
608
        case col_type_Bool:
17,820✔
609
            do_insert_column<ArrayBoolNull>(col_key, nullable);
17,820✔
610
            break;
17,820✔
611
        case col_type_Float:
6,408✔
612
            do_insert_column<ArrayFloatNull>(col_key, nullable);
6,408✔
613
            break;
6,408✔
614
        case col_type_Double:
7,011✔
615
            do_insert_column<ArrayDoubleNull>(col_key, nullable);
7,011✔
616
            break;
7,011✔
617
        case col_type_String:
287,874✔
618
            do_insert_column<ArrayString>(col_key, nullable);
287,874✔
619
            break;
287,874✔
620
        case col_type_Binary:
6,294✔
621
            do_insert_column<ArrayBinary>(col_key, nullable);
6,294✔
622
            break;
6,294✔
623
        case col_type_Mixed:
3,888✔
624
            do_insert_column<ArrayMixed>(col_key, nullable);
3,888✔
625
            break;
3,888✔
626
        case col_type_Timestamp:
20,754✔
627
            do_insert_column<ArrayTimestamp>(col_key, nullable);
20,754✔
628
            break;
20,754✔
629
        case col_type_Decimal:
4,938✔
630
            do_insert_column<ArrayDecimal128>(col_key, nullable);
4,938✔
631
            break;
4,938✔
632
        case col_type_ObjectId:
43,125✔
633
            do_insert_column<ArrayObjectIdNull>(col_key, nullable);
43,125✔
634
            break;
43,125✔
635
        case col_type_UUID:
5,526✔
636
            do_insert_column<ArrayUUIDNull>(col_key, nullable);
5,526✔
637
            break;
5,526✔
638
        case col_type_Link:
29,616✔
639
            do_insert_column<ArrayKey>(col_key, nullable);
29,616✔
640
            break;
29,616✔
641
        case col_type_TypedLink:
✔
642
            do_insert_column<ArrayTypedLink>(col_key, nullable);
×
643
            break;
×
644
        case col_type_BackLink:
88,854✔
645
            do_insert_column<ArrayBacklink>(col_key, nullable);
88,854✔
646
            break;
88,854✔
647
        default:
✔
648
            REALM_UNREACHABLE();
×
649
            break;
×
650
    }
833,433✔
651
}
833,433✔
652

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

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

11,457,816✔
674
    auto on_error = [&] {
11,457,822✔
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,457,816✔
679
    if (m_keys.is_attached()) {
23,025,012✔
680
        sz = m_keys.size();
1,628,247✔
681
        ndx = m_keys.lower_bound(uint64_t(k.value));
1,628,247✔
682
        if (ndx < sz) {
1,628,247✔
683
            current_key_value = m_keys.get(ndx);
620,871✔
684
            if (k.value == current_key_value) {
620,871✔
685
                on_error();
12✔
686
            }
12✔
687
        }
620,871✔
688
    }
1,628,247✔
689
    else {
21,396,765✔
690
        sz = size_t(Array::get(s_key_ref_or_size_index)) >> 1; // Size is stored as tagged integer
21,396,765✔
691
        if (uint64_t(k.value) < sz) {
21,396,765✔
692
            on_error();
×
693
        }
×
694
        // Key value is bigger than all other values, should be put last
10,643,901✔
695
        ndx = sz;
21,396,765✔
696
        if (uint64_t(k.value) > sz && sz < cluster_node_size) {
21,396,765✔
697
            ensure_general_form();
32,502✔
698
        }
32,502✔
699
    }
21,396,765✔
700

11,457,816✔
701
    REALM_ASSERT_DEBUG(sz <= cluster_node_size);
23,025,012✔
702
    if (REALM_LIKELY(sz < cluster_node_size)) {
23,025,012✔
703
        insert_row(ndx, k, init_values); // Throws
22,885,125✔
704
        state.mem = get_mem();
22,885,125✔
705
        state.index = ndx;
22,885,125✔
706
    }
22,885,125✔
707
    else {
139,887✔
708
        // Split leaf node
99,810✔
709
        Cluster new_leaf(0, m_alloc, m_tree_top);
139,887✔
710
        new_leaf.create();
139,887✔
711
        if (ndx == sz) {
141,660✔
712
            new_leaf.insert_row(0, ObjKey(0), init_values); // Throws
83,394✔
713
            state.split_key = k.value;
83,394✔
714
            state.mem = new_leaf.get_mem();
83,394✔
715
            state.index = 0;
83,394✔
716
        }
83,394✔
717
        else {
2,147,541,913✔
718
            // Current cluster must be in general form to get here
58,266✔
719
            REALM_ASSERT_DEBUG(m_keys.is_attached());
2,147,541,913✔
720
            new_leaf.ensure_general_form();
2,147,541,913✔
721
            move(ndx, &new_leaf, current_key_value);
2,147,541,913✔
722
            insert_row(ndx, k, init_values); // Throws
2,147,541,913✔
723
            state.mem = get_mem();
2,147,541,913✔
724
            state.split_key = current_key_value;
2,147,541,913✔
725
            state.index = ndx;
2,147,541,913✔
726
        }
2,147,541,913✔
727
        ret = new_leaf.get_ref();
139,887✔
728
    }
139,887✔
729

11,457,816✔
730
    return ret;
23,025,012✔
731
}
23,025,012✔
732

733
bool Cluster::try_get(ObjKey k, ClusterNode::State& state) const noexcept
734
{
177,987,321✔
735
    state.mem = get_mem();
177,987,321✔
736
    if (m_keys.is_attached()) {
177,987,321✔
737
        state.index = m_keys.lower_bound(uint64_t(k.value));
54,084,222✔
738
        return state.index != m_keys.size() && m_keys.get(state.index) == uint64_t(k.value);
54,084,222✔
739
    }
54,084,222✔
740
    else {
123,903,099✔
741
        if (uint64_t(k.value) < uint64_t(Array::get(s_key_ref_or_size_index) >> 1)) {
123,903,099✔
742
            state.index = size_t(k.value);
106,830,633✔
743
            return true;
106,830,633✔
744
        }
106,830,633✔
745
    }
17,072,466✔
746
    return false;
17,072,466✔
747
}
17,072,466✔
748

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

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

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

34,149✔
793
    ObjKey key = values.get(ndx);
68,313✔
794
    if (key != null_key) {
68,313✔
795
        do_remove_backlinks(get_real_key(ndx), col_key, std::vector<ObjKey>{key}, state);
64,503✔
796
    }
64,503✔
797
    values.erase(ndx);
68,313✔
798
}
68,313✔
799

800
size_t Cluster::get_ndx(ObjKey k, size_t ndx) const noexcept
801
{
10,174,836✔
802
    size_t index;
10,174,836✔
803
    if (m_keys.is_attached()) {
10,174,836✔
804
        index = m_keys.lower_bound(uint64_t(k.value));
10,009,947✔
805
        if (index == m_keys.size() || m_keys.get(index) != uint64_t(k.value)) {
10,009,947✔
806
            return realm::npos;
18✔
807
        }
18✔
808
    }
164,889✔
809
    else {
164,889✔
810
        index = size_t(k.value);
164,889✔
811
        if (index >= get_as_ref_or_tagged(s_key_ref_or_size_index).get_as_int()) {
164,889✔
812
            return realm::npos;
×
813
        }
×
814
    }
10,174,818✔
815
    return index + ndx;
10,174,818✔
816
}
10,174,818✔
817

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

2,543,622✔
825
    auto erase_in_column = [&](ColKey col_key) {
6,859,875✔
826
        auto col_type = col_key.get_type();
6,859,875✔
827
        if (col_type == col_type_LinkList)
6,859,875✔
828
            col_type = col_type_Link;
15,465✔
829
        auto attr = col_key.get_attrs();
6,859,875✔
830
        if (attr.test(col_attr_Collection)) {
6,859,875✔
831
            auto col_ndx = col_key.get_index();
52,704✔
832
            ArrayRef values(m_alloc);
52,704✔
833
            values.set_parent(this, col_ndx.val + s_first_col_index);
52,704✔
834
            values.init_from_parent();
52,704✔
835
            ref_type ref = values.get(ndx);
52,704✔
836

26,142✔
837
            if (ref) {
52,704✔
838
                const Table* origin_table = m_tree_top.get_owning_table();
18,726✔
839
                if (attr.test(col_attr_Dictionary)) {
18,726✔
840
                    if (col_type == col_type_Mixed || col_type == col_type_Link) {
2,649✔
841
                        Obj obj(origin_table->m_own_ref, get_mem(), key, ndx);
354✔
842
                        Dictionary dict(obj, col_key);
354✔
843
                        dict.remove_backlinks(state);
354✔
844
                    }
354✔
845
                }
2,649✔
846
                else if (col_type == col_type_Link) {
16,077✔
847
                    BPlusTree<ObjKey> links(m_alloc);
3,360✔
848
                    links.init_from_ref(ref);
3,360✔
849
                    if (links.size() > 0) {
3,360✔
850
                        do_remove_backlinks(ObjKey(key.value + m_offset), col_key, links.get_all(), state);
1,299✔
851
                    }
1,299✔
852
                }
3,360✔
853
                else if (col_type == col_type_TypedLink) {
12,717✔
854
                    BPlusTree<ObjLink> links(m_alloc);
×
855
                    links.init_from_ref(ref);
×
856
                    for (size_t i = 0; i < links.size(); i++) {
×
857
                        ObjLink link = links.get(i);
×
858
                        auto target_obj = origin_table->get_parent_group()->get_object(link);
×
859
                        ColKey backlink_col_key =
×
860
                            target_obj.get_table()->find_backlink_column(col_key, origin_table->get_key());
×
861
                        target_obj.remove_one_backlink(backlink_col_key, ObjKey(key.value + m_offset));
×
862
                    }
×
863
                }
×
864
                else if (col_type == col_type_Mixed) {
12,717✔
865
                    Obj obj(origin_table->m_own_ref, get_mem(), key, ndx);
216✔
866
                    Lst<Mixed> list(obj, col_key);
216✔
867
                    list.remove_backlinks(state);
216✔
868
                }
216✔
869
                Array::destroy_deep(ref, m_alloc);
18,726✔
870
            }
18,726✔
871

26,142✔
872
            values.erase(ndx);
52,704✔
873

26,142✔
874
            return IteratorControl::AdvanceToNext;
52,704✔
875
        }
52,704✔
876

3,404,001✔
877
        switch (col_type) {
6,807,171✔
878
            case col_type_Int:
6,079,221✔
879
                if (attr.test(col_attr_Nullable)) {
6,079,221✔
880
                    do_erase<ArrayIntNull>(ndx, col_key);
1,289,799✔
881
                }
1,289,799✔
882
                else {
4,789,422✔
883
                    do_erase<ArrayInteger>(ndx, col_key);
4,789,422✔
884
                }
4,789,422✔
885
                break;
6,079,221✔
886
            case col_type_Bool:
42,555✔
887
                do_erase<ArrayBoolNull>(ndx, col_key);
42,555✔
888
                break;
42,555✔
889
            case col_type_Float:
36,489✔
890
                do_erase<ArrayFloatNull>(ndx, col_key);
36,489✔
891
                break;
36,489✔
892
            case col_type_Double:
36,501✔
893
                do_erase<ArrayDoubleNull>(ndx, col_key);
36,501✔
894
                break;
36,501✔
895
            case col_type_String:
119,466✔
896
                do_erase<ArrayString>(ndx, col_key);
119,466✔
897
                break;
119,466✔
898
            case col_type_Binary:
38,694✔
899
                do_erase<ArrayBinary>(ndx, col_key);
38,694✔
900
                break;
38,694✔
901
            case col_type_Mixed:
627✔
902
                do_erase<ArrayMixed>(ndx, col_key);
627✔
903
                break;
627✔
904
            case col_type_Timestamp:
45,705✔
905
                do_erase<ArrayTimestamp>(ndx, col_key);
45,705✔
906
                break;
45,705✔
907
            case col_type_Decimal:
36,279✔
908
                do_erase<ArrayDecimal128>(ndx, col_key);
36,279✔
909
                break;
36,279✔
910
            case col_type_ObjectId:
40,200✔
911
                do_erase<ArrayObjectIdNull>(ndx, col_key);
40,200✔
912
                break;
40,200✔
913
            case col_type_UUID:
60,279✔
914
                do_erase<ArrayUUIDNull>(ndx, col_key);
60,279✔
915
                break;
60,279✔
916
            case col_type_Link:
68,313✔
917
                do_erase_key(ndx, col_key, state);
68,313✔
918
                break;
68,313✔
919
            case col_type_TypedLink:
✔
920
                do_erase<ArrayTypedLink>(ndx, col_key);
×
921
                break;
×
922
            case col_type_BackLink:
202,977✔
923
                if (state.m_mode == CascadeState::Mode::None) {
202,977✔
924
                    do_erase<ArrayBacklink>(ndx, col_key);
176,697✔
925
                }
176,697✔
926
                else {
26,280✔
927
                    // Postpone the deletion of backlink entries or else the
13,116✔
928
                    // checks for if there's any remaining backlinks will
13,116✔
929
                    // check the wrong row for columns which have already
13,116✔
930
                    // had values erased from them.
13,116✔
931
                    backlink_column_keys.push_back(col_key);
26,280✔
932
                }
26,280✔
933
                break;
202,977✔
934
            default:
✔
935
                REALM_ASSERT(false);
×
936
                break;
×
937
        }
6,807,021✔
938
        return IteratorControl::AdvanceToNext;
6,807,021✔
939
    };
6,807,021✔
940
    m_tree_top.m_owner->for_each_and_every_column(erase_in_column);
5,087,073✔
941

2,543,622✔
942
    // Any remaining backlink columns to erase from?
2,543,622✔
943
    for (auto k : backlink_column_keys)
5,087,073✔
944
        do_erase<ArrayBacklink>(ndx, k);
26,280✔
945

2,543,622✔
946
    if (m_keys.is_attached()) {
5,087,073✔
947
        m_keys.erase(ndx);
5,030,316✔
948
    }
5,030,316✔
949
    else {
56,757✔
950
        size_t current_size = get_size_in_compact_form();
56,757✔
951
        if (ndx == current_size - 1) {
56,757✔
952
            // When deleting last, we can still maintain compact form
18,255✔
953
            set(0, RefOrTagged::make_tagged(current_size - 1));
36,735✔
954
        }
36,735✔
955
        else {
20,022✔
956
            ensure_general_form();
20,022✔
957
            m_keys.erase(ndx);
20,022✔
958
        }
20,022✔
959
    }
56,757✔
960

2,543,622✔
961
    return node_size();
5,087,073✔
962
}
5,087,073✔
963

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

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

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

93,456✔
999
        return IteratorControl::AdvanceToNext;
186,993✔
1000
    };
186,993✔
1001

2,493,951✔
1002
    m_tree_top.get_owning_table()->for_each_backlink_column(nullify_fwd_links);
4,987,791✔
1003
}
4,987,791✔
1004

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

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

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

1047
template <typename ArrayType>
1048
void Cluster::verify(ref_type ref, size_t index, util::Optional<size_t>& sz) const
1049
{
6,869,304✔
1050
    ArrayType arr(get_alloc());
6,869,304✔
1051
    set_spec(arr, ColKey::Idx{unsigned(index) - 1});
6,869,304✔
1052
    arr.set_parent(const_cast<Cluster*>(this), index);
6,869,304✔
1053
    arr.init_from_ref(ref);
6,869,304✔
1054
    arr.verify();
6,869,304✔
1055
    if (sz) {
6,869,304!
1056
        REALM_ASSERT(arr.size() == *sz);
3,128,850!
1057
    }
3,128,850✔
1058
    else {
3,740,454✔
1059
        sz = arr.size();
3,740,454✔
1060
    }
3,740,454✔
1061
}
6,869,304✔
1062
namespace {
1063

1064
template <typename ArrayType>
1065
void verify_list(ArrayRef& arr, size_t sz)
1066
{
1,564,857✔
1067
    for (size_t n = 0; n < sz; n++) {
6,171,729!
1068
        if (ref_type bp_tree_ref = arr.get(n)) {
4,606,872!
1069
            BPlusTree<ArrayType> links(arr.get_alloc());
1,669,797✔
1070
            links.init_from_ref(bp_tree_ref);
1,669,797✔
1071
            links.set_parent(&arr, n);
1,669,797✔
1072
            links.verify();
1,669,797✔
1073
        }
1,669,797✔
1074
    }
4,606,872✔
1075
}
1,564,857✔
1076

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

84✔
1087
            // FIXME: Check uniqueness of elements.
84✔
1088
        }
168✔
1089
    }
168✔
1090
}
126✔
1091

1092
} // namespace
1093

1094
void Cluster::verify() const
1095
{
3,777,003✔
1096
#ifdef REALM_DEBUG
3,777,003✔
1097
    util::Optional<size_t> sz;
3,777,003✔
1098

1,907,622✔
1099
    auto verify_column = [this, &sz](ColKey col_key) {
8,446,548✔
1100
        size_t col = col_key.get_index().val + s_first_col_index;
8,446,548✔
1101
        ref_type ref = Array::get_as_ref(col);
8,446,548✔
1102
        auto attr = col_key.get_attrs();
8,446,548✔
1103
        auto col_type = col_key.get_type();
8,446,548✔
1104
        bool nullable = attr.test(col_attr_Nullable);
8,446,548✔
1105

4,248,138✔
1106
        if (attr.test(col_attr_List)) {
8,446,548✔
1107
            ArrayRef arr(get_alloc());
1,564,869✔
1108
            arr.set_parent(const_cast<Cluster*>(this), col);
1,564,869✔
1109
            arr.init_from_ref(ref);
1,564,869✔
1110
            arr.verify();
1,564,869✔
1111
            if (sz) {
1,564,869✔
1112
                REALM_ASSERT(arr.size() == *sz);
1,540,632✔
1113
            }
1,540,632✔
1114
            else {
24,237✔
1115
                sz = arr.size();
24,237✔
1116
            }
24,237✔
1117

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

3,477,396✔
1239
        switch (col_type) {
6,869,301✔
1240
            case col_type_Int:
4,730,301✔
1241
                if (nullable) {
4,730,301✔
1242
                    verify<ArrayIntNull>(ref, col, sz);
797,172✔
1243
                }
797,172✔
1244
                else {
3,933,129✔
1245
                    verify<ArrayInteger>(ref, col, sz);
3,933,129✔
1246
                }
3,933,129✔
1247
                break;
4,730,301✔
1248
            case col_type_Bool:
7,152✔
1249
                verify<ArrayBoolNull>(ref, col, sz);
7,152✔
1250
                break;
7,152✔
1251
            case col_type_Float:
144✔
1252
                verify<ArrayFloatNull>(ref, col, sz);
144✔
1253
                break;
144✔
1254
            case col_type_Double:
165✔
1255
                verify<ArrayDoubleNull>(ref, col, sz);
165✔
1256
                break;
165✔
1257
            case col_type_String:
2,120,568✔
1258
                verify<ArrayString>(ref, col, sz);
2,120,568✔
1259
                break;
2,120,568✔
1260
            case col_type_Binary:
144✔
1261
                verify<ArrayBinary>(ref, col, sz);
144✔
1262
                break;
144✔
1263
            case col_type_Mixed:
54✔
1264
                verify<ArrayMixed>(ref, col, sz);
54✔
1265
                break;
54✔
1266
            case col_type_Timestamp:
6,957✔
1267
                verify<ArrayTimestamp>(ref, col, sz);
6,957✔
1268
                break;
6,957✔
1269
            case col_type_Decimal:
42✔
1270
                verify<ArrayDecimal128>(ref, col, sz);
42✔
1271
                break;
42✔
1272
            case col_type_ObjectId:
42✔
1273
                verify<ArrayObjectIdNull>(ref, col, sz);
42✔
1274
                break;
42✔
1275
            case col_type_UUID:
✔
1276
                verify<ArrayUUIDNull>(ref, col, sz);
×
1277
                break;
×
1278
            case col_type_Link:
1,308✔
1279
                verify<ArrayKey>(ref, col, sz);
1,308✔
1280
                break;
1,308✔
1281
            case col_type_BackLink:
2,427✔
1282
                verify<ArrayBacklink>(ref, col, sz);
2,427✔
1283
                break;
2,427✔
1284
            default:
✔
1285
                break;
×
1286
        }
6,869,298✔
1287
        return IteratorControl::AdvanceToNext;
6,869,298✔
1288
    };
6,869,298✔
1289

1,907,622✔
1290
    m_tree_top.m_owner->for_each_and_every_column(verify_column);
3,777,003✔
1291
#endif
3,777,003✔
1292
}
3,777,003✔
1293

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

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

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

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

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

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

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

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

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