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

realm / realm-core / 1854

21 Nov 2023 01:54PM UTC coverage: 91.689% (+0.006%) from 91.683%
1854

push

Evergreen

jedelbo
Move bson files to core utils

92306 of 169120 branches covered (0.0%)

231301 of 252266 relevant lines covered (91.69%)

6288175.74 hits per line

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

80.36
/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 <iostream>
37
#include <cmath>
38

39
namespace realm {
40

41
/******************************* FieldValues *********************************/
42

43
FieldValues::FieldValues(std::initializer_list<FieldValue> init)
44
    : m_values(init)
45
{
601,518✔
46
    if (m_values.size() > 1) {
601,518✔
47
        // Sort according to ColKey index
8,169✔
48
        std::sort(m_values.begin(), m_values.end(), [](const auto& a, const auto& b) {
48,909✔
49
            return a.col_key.get_index().val < b.col_key.get_index().val;
48,909✔
50
        });
48,909✔
51
    }
16,338✔
52
}
601,518✔
53

54
void FieldValues::insert(ColKey k, Mixed val, bool is_default)
55
{
536,679✔
56
    if (m_values.empty()) {
536,679✔
57
        m_values.emplace_back(k, val, is_default);
497,949✔
58
        return;
497,949✔
59
    }
497,949✔
60
    unsigned int idx = k.get_index().val;
38,730✔
61
    auto it = std::lower_bound(m_values.begin(), m_values.end(), idx, [](const auto& a, unsigned int i) {
85,698✔
62
        return a.col_key.get_index().val < i;
85,698✔
63
    });
85,698✔
64
    m_values.insert(it, {k, val, is_default});
38,730✔
65
}
38,730✔
66

67
/******************************* ClusterNode *********************************/
68

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

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

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

89
void ClusterNode::get(ObjKey k, ClusterNode::State& state) const
90
{
136,533,084✔
91
    if (!k || !try_get(k, state)) {
136,546,290✔
92
        throw KeyNotFound(util::format("No object with key '%1' in '%2'", k.value, get_owning_table()->get_name()));
462✔
93
    }
462✔
94
}
136,533,084✔
95

96

97
/********************************* Cluster ***********************************/
98

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

173,994✔
104
    arr.add(RefOrTagged::make_tagged(0)); // Compact form
350,181✔
105
    return arr.get_mem();
350,181✔
106
}
350,181✔
107

108

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

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

45,381✔
124
    auto column_initialize = [this](ColKey col_key) {
175,446✔
125
        auto col_ndx = col_key.get_index();
175,446✔
126
        while (size() <= col_ndx.val + 1)
350,889✔
127
            add(0);
175,443✔
128
        auto type = col_key.get_type();
175,446✔
129
        auto attr = col_key.get_attrs();
175,446✔
130
        if (attr.test(col_attr_Collection)) {
175,446✔
131
            ArrayRef arr(m_alloc);
4,314✔
132
            arr.create();
4,314✔
133
            arr.set_parent(this, col_ndx.val + s_first_col_index);
4,314✔
134
            arr.update_parent();
4,314✔
135
            return IteratorControl::AdvanceToNext;
4,314✔
136
        }
4,314✔
137
        switch (type) {
171,132✔
138
            case col_type_Int:
102,681✔
139
                if (attr.test(col_attr_Nullable)) {
102,681✔
140
                    do_create<ArrayIntNull>(col_key);
7,503✔
141
                }
7,503✔
142
                else {
95,178✔
143
                    do_create<ArrayInteger>(col_key);
95,178✔
144
                }
95,178✔
145
                break;
102,681✔
146
            case col_type_Bool:
696✔
147
                do_create<ArrayBoolNull>(col_key);
696✔
148
                break;
696✔
149
            case col_type_Float:
1,125✔
150
                do_create<ArrayFloatNull>(col_key);
1,125✔
151
                break;
1,125✔
152
            case col_type_Double:
5,802✔
153
                do_create<ArrayDoubleNull>(col_key);
5,802✔
154
                break;
5,802✔
155
            case col_type_String: {
44,511✔
156
                if (m_tree_top.is_string_enum_type(col_ndx)) {
44,511✔
157
                    do_create<ArrayInteger>(col_key);
5,340✔
158
                }
5,340✔
159
                else {
39,171✔
160
                    do_create<ArrayString>(col_key);
39,171✔
161
                }
39,171✔
162
                break;
44,511✔
163
            }
×
164
            case col_type_Binary:
5,136✔
165
                do_create<ArrayBinary>(col_key);
5,136✔
166
                break;
5,136✔
167
            case col_type_Mixed:
72✔
168
                do_create<ArrayMixed>(col_key);
72✔
169
                break;
72✔
170
            case col_type_Timestamp:
2,376✔
171
                do_create<ArrayTimestamp>(col_key);
2,376✔
172
                break;
2,376✔
173
            case col_type_Decimal:
276✔
174
                do_create<ArrayDecimal128>(col_key);
276✔
175
                break;
276✔
176
            case col_type_ObjectId:
2,796✔
177
                do_create<ArrayObjectIdNull>(col_key);
2,796✔
178
                break;
2,796✔
179
            case col_type_UUID:
408✔
180
                do_create<ArrayUUIDNull>(col_key);
408✔
181
                break;
408✔
182
            case col_type_Link:
2,199✔
183
                do_create<ArrayKey>(col_key);
2,199✔
184
                break;
2,199✔
185
            case col_type_TypedLink:
6✔
186
                do_create<ArrayTypedLink>(col_key);
6✔
187
                break;
6✔
188
            case col_type_BackLink:
3,048✔
189
                do_create<ArrayBacklink>(col_key);
3,048✔
190
                break;
3,048✔
191
            default:
✔
192
                REALM_UNREACHABLE();
193
        }
171,132✔
194
        return IteratorControl::AdvanceToNext;
171,135✔
195
    };
171,132✔
196
    m_tree_top.m_owner->for_each_and_every_column(column_initialize);
92,400✔
197

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

206
void Cluster::init(MemRef mem)
207
{
218,243,493✔
208
    Array::init_from_mem(mem);
218,243,493✔
209
    auto rot = Array::get_as_ref_or_tagged(0);
218,243,493✔
210
    if (rot.is_tagged()) {
218,243,493✔
211
        m_keys.detach();
153,859,749✔
212
    }
153,859,749✔
213
    else {
64,383,744✔
214
        m_keys.init_from_ref(rot.get_as_ref());
64,383,744✔
215
    }
64,383,744✔
216
}
218,243,493✔
217

218
void Cluster::update_from_parent() noexcept
219
{
745,719✔
220
    Array::update_from_parent();
745,719✔
221
    auto rot = Array::get_as_ref_or_tagged(0);
745,719✔
222
    if (!rot.is_tagged()) {
745,719✔
223
        m_keys.update_from_parent();
21,648✔
224
    }
21,648✔
225
}
745,719✔
226

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

233
    return get_mem();
×
234
}
×
235

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

241
size_t Cluster::node_size_from_header(Allocator& alloc, const char* header)
242
{
51,191,853✔
243
    auto rot = Array::get_as_ref_or_tagged(header, s_key_ref_or_size_index);
51,191,853✔
244
    if (rot.is_tagged()) {
51,191,853✔
245
        return size_t(rot.get_as_int());
51,064,266✔
246
    }
51,064,266✔
247
    else {
127,587✔
248
        return Array::get_size_from_header(alloc.translate(rot.get_as_ref()));
127,587✔
249
    }
127,587✔
250
}
51,191,853✔
251

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

257
template <>
258
inline void Cluster::set_spec(ArrayString& arr, ColKey::Idx col_ndx) const
259
{
5,023,191✔
260
    m_tree_top.set_spec(arr, col_ndx);
5,023,191✔
261
}
5,023,191✔
262

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

15,949,737✔
268
    T arr(m_alloc);
32,106,141✔
269
    auto col_ndx = col.get_index();
32,106,141✔
270
    arr.set_parent(this, col_ndx.val + s_first_col_index);
32,106,141✔
271
    set_spec<T>(arr, col_ndx);
32,106,141✔
272
    arr.init_from_parent();
32,106,141✔
273
    if (init_val.is_null()) {
32,106,141✔
274
        arr.insert(ndx, T::default_value(nullable));
31,428,624✔
275
    }
31,428,624✔
276
    else {
677,517✔
277
        arr.insert(ndx, init_val.get<U>());
677,517✔
278
    }
677,517✔
279
}
32,106,141✔
280

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

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

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

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

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

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

6✔
340
        ColKey backlink_col_key = target_table->find_or_add_backlink_column(col_key, origin_table->get_key());
12✔
341
        target_table->get_object(target_link.get_obj_key()).add_backlink(backlink_col_key, origin_key);
12✔
342
    }
12✔
343
}
66✔
344

345
void Cluster::insert_row(size_t ndx, ObjKey k, const FieldValues& init_values)
346
{
23,035,704✔
347
    // Ensure the cluster array is big enough to hold 64 bit values.
11,467,329✔
348
    copy_on_write(m_size * 8);
23,035,704✔
349

11,467,329✔
350
    if (m_keys.is_attached()) {
23,035,704✔
351
        m_keys.insert(ndx, k.value);
1,704,564✔
352
    }
1,704,564✔
353
    else {
21,331,140✔
354
        Array::set(s_key_ref_or_size_index, Array::get(s_key_ref_or_size_index) + 2); // Increments size by 1
21,331,140✔
355
    }
21,331,140✔
356

11,467,329✔
357
    auto val = init_values.begin();
23,035,704✔
358
    auto insert_in_column = [&](ColKey col_key) {
33,916,587✔
359
        auto col_ndx = col_key.get_index();
33,916,587✔
360
        auto attr = col_key.get_attrs();
33,916,587✔
361
        Mixed init_value;
33,916,587✔
362
        // init_values must be sorted in col_ndx order - this is ensured by ClustTree::insert()
16,814,322✔
363
        if (val != init_values.end() && val->col_key.get_index().val == col_ndx.val) {
33,916,587✔
364
            init_value = val->value;
614,292✔
365
            ++val;
614,292✔
366
        }
614,292✔
367

16,814,322✔
368
        auto type = col_key.get_type();
33,916,587✔
369
        if (attr.test(col_attr_Collection)) {
33,916,587✔
370
            REALM_ASSERT(init_value.is_null());
677,241✔
371
            ArrayRef arr(m_alloc);
677,241✔
372
            arr.set_parent(this, col_ndx.val + s_first_col_index);
677,241✔
373
            arr.init_from_parent();
677,241✔
374
            arr.insert(ndx, 0);
677,241✔
375
            return IteratorControl::AdvanceToNext;
677,241✔
376
        }
677,241✔
377

16,477,431✔
378
        bool nullable = attr.test(col_attr_Nullable);
33,239,346✔
379
        switch (type) {
33,239,346✔
380
            case col_type_Int:
25,469,139✔
381
                if (attr.test(col_attr_Nullable)) {
25,469,139✔
382
                    do_insert_row<ArrayIntNull>(ndx, col_key, init_value, nullable);
1,389,123✔
383
                }
1,389,123✔
384
                else {
24,080,016✔
385
                    do_insert_row<ArrayInteger>(ndx, col_key, init_value, nullable);
24,080,016✔
386
                }
24,080,016✔
387
                break;
25,469,139✔
388
            case col_type_Bool:
194,070✔
389
                do_insert_row<ArrayBoolNull>(ndx, col_key, init_value, nullable);
194,070✔
390
                break;
194,070✔
391
            case col_type_Float:
327,207✔
392
                do_insert_row<ArrayFloatNull>(ndx, col_key, init_value, nullable);
327,207✔
393
                break;
327,207✔
394
            case col_type_Double:
1,495,167✔
395
                do_insert_row<ArrayDoubleNull>(ndx, col_key, init_value, nullable);
1,495,167✔
396
                break;
1,495,167✔
397
            case col_type_String:
2,722,041✔
398
                do_insert_row<ArrayString>(ndx, col_key, init_value, nullable);
2,722,041✔
399
                break;
2,722,041✔
400
            case col_type_Binary:
1,317,816✔
401
                do_insert_row<ArrayBinary>(ndx, col_key, init_value, nullable);
1,317,816✔
402
                break;
1,317,816✔
403
            case col_type_Mixed: {
13,068✔
404
                do_insert_mixed(ndx, col_key, init_value, ObjKey(k.value + get_offset()));
13,068✔
405
                break;
13,068✔
406
            }
×
407
            case col_type_Timestamp:
221,778✔
408
                do_insert_row<ArrayTimestamp>(ndx, col_key, init_value, nullable);
221,778✔
409
                break;
221,778✔
410
            case col_type_Decimal:
75,624✔
411
                do_insert_row<ArrayDecimal128>(ndx, col_key, init_value, nullable);
75,624✔
412
                break;
75,624✔
413
            case col_type_ObjectId:
162,870✔
414
                do_insert_row<ArrayObjectIdNull>(ndx, col_key, init_value, nullable);
162,870✔
415
                break;
162,870✔
416
            case col_type_UUID:
115,842✔
417
                do_insert_row<ArrayUUIDNull>(ndx, col_key, init_value, nullable);
115,842✔
418
                break;
115,842✔
419
            case col_type_Link:
256,134✔
420
                do_insert_key(ndx, col_key, init_value, ObjKey(k.value + get_offset()));
256,134✔
421
                break;
256,134✔
422
            case col_type_TypedLink:
66✔
423
                do_insert_link(ndx, col_key, init_value, ObjKey(k.value + get_offset()));
66✔
424
                break;
66✔
425
            case col_type_BackLink: {
958,914✔
426
                ArrayBacklink arr(m_alloc);
958,914✔
427
                arr.set_parent(this, col_ndx.val + s_first_col_index);
958,914✔
428
                arr.init_from_parent();
958,914✔
429
                arr.insert(ndx, 0);
958,914✔
430
                break;
958,914✔
431
            }
×
432
            default:
✔
433
                REALM_ASSERT(false);
×
434
                break;
×
435
        }
33,126,225✔
436
        return IteratorControl::AdvanceToNext;
33,126,225✔
437
    };
33,126,225✔
438
    m_tree_top.m_owner->for_each_and_every_column(insert_in_column);
23,035,704✔
439
}
23,035,704✔
440

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

10,992✔
449
    T dst(m_alloc);
22,002✔
450
    dst.set_parent(to, col_ndx);
22,002✔
451
    dst.init_from_parent();
22,002✔
452

10,992✔
453
    src.move(dst, ndx);
22,002✔
454
}
22,002✔
455

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

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

10,992✔
464
        if (attr.test(col_attr_Collection)) {
22,002✔
465
            do_move<ArrayRef>(ndx, col_key, new_leaf);
30✔
466
            return IteratorControl::AdvanceToNext;
30✔
467
        }
30✔
468

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

534
Cluster::~Cluster() {}
221,730,288✔
535

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

544
void Cluster::ensure_general_form()
545
{
84,351✔
546
    if (!m_keys.is_attached()) {
84,351✔
547
        size_t current_size = get_size_in_compact_form();
70,968✔
548
        m_keys.create(current_size, 255);
70,968✔
549
        m_keys.update_parent();
70,968✔
550
        for (size_t i = 0; i < current_size; i++) {
5,503,827✔
551
            m_keys.set(i, i);
5,432,859✔
552
        }
5,432,859✔
553
    }
70,968✔
554
}
84,351✔
555

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

396,495✔
561
    T arr(m_alloc);
804,198✔
562
    arr.create();
804,198✔
563
    auto val = T::default_value(nullable);
804,198✔
564
    for (size_t i = 0; i < sz; i++) {
7,090,887✔
565
        arr.add(val);
6,286,689✔
566
    }
6,286,689✔
567
    auto col_ndx = col_key.get_index();
804,198✔
568
    unsigned ndx = col_ndx.val + s_first_col_index;
804,198✔
569

396,495✔
570
    // Fill up if indexes are not consecutive
396,495✔
571
    while (size() < ndx)
804,198✔
572
        Array::add(0);
×
573

396,495✔
574
    if (ndx == size())
804,198✔
575
        Array::insert(ndx, from_ref(arr.get_ref()));
804,114✔
576
    else
84✔
577
        Array::set(ndx, from_ref(arr.get_ref()));
84✔
578
}
804,198✔
579

580
void Cluster::insert_column(ColKey col_key)
581
{
1,038,606✔
582
    auto attr = col_key.get_attrs();
1,038,606✔
583
    auto type = col_key.get_type();
1,038,606✔
584
    if (attr.test(col_attr_Collection)) {
1,038,606✔
585
        size_t sz = node_size();
234,408✔
586

116,154✔
587
        ArrayRef arr(m_alloc);
234,408✔
588
        arr.create(sz);
234,408✔
589
        auto col_ndx = col_key.get_index();
234,408✔
590
        unsigned idx = col_ndx.val + s_first_col_index;
234,408✔
591
        if (idx == size())
234,408✔
592
            Array::insert(idx, from_ref(arr.get_ref()));
234,408✔
593
        else
×
594
            Array::set(idx, from_ref(arr.get_ref()));
×
595
        return;
234,408✔
596
    }
234,408✔
597
    bool nullable = attr.test(col_attr_Nullable);
804,198✔
598
    switch (type) {
804,198✔
599
        case col_type_Int:
314,565✔
600
            if (nullable) {
314,565✔
601
                do_insert_column<ArrayIntNull>(col_key, nullable);
25,389✔
602
            }
25,389✔
603
            else {
289,176✔
604
                do_insert_column<ArrayInteger>(col_key, nullable);
289,176✔
605
            }
289,176✔
606
            break;
314,565✔
607
        case col_type_Bool:
5,637✔
608
            do_insert_column<ArrayBoolNull>(col_key, nullable);
5,637✔
609
            break;
5,637✔
610
        case col_type_Float:
6,363✔
611
            do_insert_column<ArrayFloatNull>(col_key, nullable);
6,363✔
612
            break;
6,363✔
613
        case col_type_Double:
6,978✔
614
            do_insert_column<ArrayDoubleNull>(col_key, nullable);
6,978✔
615
            break;
6,978✔
616
        case col_type_String:
265,629✔
617
            do_insert_column<ArrayString>(col_key, nullable);
265,629✔
618
            break;
265,629✔
619
        case col_type_Binary:
6,360✔
620
            do_insert_column<ArrayBinary>(col_key, nullable);
6,360✔
621
            break;
6,360✔
622
        case col_type_Mixed:
3,666✔
623
            do_insert_column<ArrayMixed>(col_key, nullable);
3,666✔
624
            break;
3,666✔
625
        case col_type_Timestamp:
21,054✔
626
            do_insert_column<ArrayTimestamp>(col_key, nullable);
21,054✔
627
            break;
21,054✔
628
        case col_type_Decimal:
4,938✔
629
            do_insert_column<ArrayDecimal128>(col_key, nullable);
4,938✔
630
            break;
4,938✔
631
        case col_type_ObjectId:
43,563✔
632
            do_insert_column<ArrayObjectIdNull>(col_key, nullable);
43,563✔
633
            break;
43,563✔
634
        case col_type_UUID:
5,526✔
635
            do_insert_column<ArrayUUIDNull>(col_key, nullable);
5,526✔
636
            break;
5,526✔
637
        case col_type_Link:
29,940✔
638
            do_insert_column<ArrayKey>(col_key, nullable);
29,940✔
639
            break;
29,940✔
640
        case col_type_TypedLink:
36✔
641
            do_insert_column<ArrayTypedLink>(col_key, nullable);
36✔
642
            break;
36✔
643
        case col_type_BackLink:
89,943✔
644
            do_insert_column<ArrayBacklink>(col_key, nullable);
89,943✔
645
            break;
89,943✔
646
        default:
✔
647
            REALM_UNREACHABLE();
648
            break;
×
649
    }
804,198✔
650
}
804,198✔
651

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

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

11,437,659✔
673
    auto on_error = [&] {
11,437,665✔
674
        throw KeyAlreadyUsed(
18✔
675
            util::format("When inserting key '%1' in '%2'", k.value, get_owning_table()->get_name()));
18✔
676
    };
18✔
677

11,437,659✔
678
    if (m_keys.is_attached()) {
22,997,907✔
679
        sz = m_keys.size();
1,676,397✔
680
        ndx = m_keys.lower_bound(uint64_t(k.value));
1,676,397✔
681
        if (ndx < sz) {
1,676,397✔
682
            current_key_value = m_keys.get(ndx);
620,778✔
683
            if (k.value == current_key_value) {
620,778✔
684
                on_error();
18✔
685
            }
18✔
686
        }
620,778✔
687
    }
1,676,397✔
688
    else {
21,321,510✔
689
        sz = size_t(Array::get(s_key_ref_or_size_index)) >> 1; // Size is stored as tagged integer
21,321,510✔
690
        if (uint64_t(k.value) < sz) {
21,321,510✔
691
            on_error();
×
692
        }
×
693
        // Key value is bigger than all other values, should be put last
10,598,136✔
694
        ndx = sz;
21,321,510✔
695
        if (uint64_t(k.value) > sz && sz < cluster_node_size) {
21,321,510✔
696
            ensure_general_form();
33,894✔
697
        }
33,894✔
698
    }
21,321,510✔
699

11,437,659✔
700
    REALM_ASSERT_DEBUG(sz <= cluster_node_size);
22,997,907✔
701
    if (REALM_LIKELY(sz < cluster_node_size)) {
22,997,907✔
702
        insert_row(ndx, k, init_values); // Throws
22,953,747✔
703
        state.mem = get_mem();
22,953,747✔
704
        state.index = ndx;
22,953,747✔
705
    }
22,953,747✔
706
    else {
44,160✔
707
        // Split leaf node
9,006✔
708
        Cluster new_leaf(0, m_alloc, m_tree_top);
44,160✔
709
        new_leaf.create();
44,160✔
710
        if (ndx == sz) {
83,265✔
711
            new_leaf.insert_row(0, ObjKey(0), init_values); // Throws
83,265✔
712
            state.split_key = k.value;
83,265✔
713
            state.mem = new_leaf.get_mem();
83,265✔
714
            state.index = 0;
83,265✔
715
        }
83,265✔
716
        else {
4,294,967,294✔
717
            // Current cluster must be in general form to get here
2,147,483,647✔
718
            REALM_ASSERT_DEBUG(m_keys.is_attached());
4,294,967,294✔
719
            new_leaf.ensure_general_form();
4,294,967,294✔
720
            move(ndx, &new_leaf, current_key_value);
4,294,967,294✔
721
            insert_row(ndx, k, init_values); // Throws
4,294,967,294✔
722
            state.mem = get_mem();
4,294,967,294✔
723
            state.split_key = current_key_value;
4,294,967,294✔
724
            state.index = ndx;
4,294,967,294✔
725
        }
4,294,967,294✔
726
        ret = new_leaf.get_ref();
44,160✔
727
    }
44,160✔
728

11,437,659✔
729
    return ret;
22,997,907✔
730
}
22,997,907✔
731

732
bool Cluster::try_get(ObjKey k, ClusterNode::State& state) const noexcept
733
{
178,299,063✔
734
    state.mem = get_mem();
178,299,063✔
735
    if (m_keys.is_attached()) {
178,299,063✔
736
        state.index = m_keys.lower_bound(uint64_t(k.value));
54,150,015✔
737
        return state.index != m_keys.size() && m_keys.get(state.index) == uint64_t(k.value);
54,150,015✔
738
    }
54,150,015✔
739
    else {
124,149,048✔
740
        if (uint64_t(k.value) < uint64_t(Array::get(s_key_ref_or_size_index) >> 1)) {
124,149,048✔
741
            state.index = size_t(k.value);
107,605,881✔
742
            return true;
107,605,881✔
743
        }
107,605,881✔
744
    }
16,543,167✔
745
    return false;
16,543,167✔
746
}
16,543,167✔
747

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

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

15✔
777
            ColKey backlink_col_key = target_obj.get_table()->find_backlink_column(col_key, origin_table->get_key());
30✔
778
            REALM_ASSERT(backlink_col_key);
30!
779
            target_obj.remove_one_backlink(backlink_col_key, get_real_key(ndx)); // Throws
30✔
780
        }
30✔
781
    }
30✔
782
    values.erase(ndx);
6,682,683✔
783
}
6,682,683✔
784

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

3,678✔
792
    ObjKey key = values.get(ndx);
7,365✔
793
    if (key != null_key) {
7,365✔
794
        remove_backlinks(get_real_key(ndx), col_key, std::vector<ObjKey>{key}, state);
4,674✔
795
    }
4,674✔
796
    values.erase(ndx);
7,365✔
797
}
7,365✔
798

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

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

2,514,660✔
824
    auto erase_in_column = [&](ColKey col_key) {
6,744,498✔
825
        auto col_type = col_key.get_type();
6,744,498✔
826
        auto attr = col_key.get_attrs();
6,744,498✔
827
        if (attr.test(col_attr_Collection)) {
6,744,498✔
828
            auto col_ndx = col_key.get_index();
54,567✔
829
            ArrayRef values(m_alloc);
54,567✔
830
            values.set_parent(this, col_ndx.val + s_first_col_index);
54,567✔
831
            values.init_from_parent();
54,567✔
832
            ref_type ref = values.get(ndx);
54,567✔
833

26,856✔
834
            if (ref) {
54,567✔
835
                const Table* origin_table = m_tree_top.get_owning_table();
19,077✔
836
                if (attr.test(col_attr_Dictionary)) {
19,077✔
837
                    if (col_type == col_type_Mixed || col_type == col_type_Link) {
2,643✔
838
                        Obj obj(origin_table->m_own_ref, get_mem(), key, ndx);
348✔
839
                        const Dictionary dict = obj.get_dictionary(col_key);
348✔
840
                        dict.remove_backlinks(state);
348✔
841
                    }
348✔
842
                }
2,643✔
843
                else if (col_type == col_type_LinkList || col_type == col_type_Link) {
16,434✔
844
                    BPlusTree<ObjKey> links(m_alloc);
3,555✔
845
                    links.init_from_ref(ref);
3,555✔
846
                    if (links.size() > 0) {
3,555✔
847
                        remove_backlinks(ObjKey(key.value + m_offset), col_key, links.get_all(), state);
1,311✔
848
                    }
1,311✔
849
                }
3,555✔
850
                else if (col_type == col_type_TypedLink) {
12,879✔
851
                    BPlusTree<ObjLink> links(m_alloc);
6✔
852
                    links.init_from_ref(ref);
6✔
853
                    for (size_t i = 0; i < links.size(); i++) {
12✔
854
                        ObjLink link = links.get(i);
6✔
855
                        auto target_obj = origin_table->get_parent_group()->get_object(link);
6✔
856
                        ColKey backlink_col_key =
6✔
857
                            target_obj.get_table()->find_backlink_column(col_key, origin_table->get_key());
6✔
858
                        target_obj.remove_one_backlink(backlink_col_key, ObjKey(key.value + m_offset));
6✔
859
                    }
6✔
860
                }
6✔
861
                else if (col_type == col_type_Mixed) {
12,873✔
862
                    BPlusTree<Mixed> list(m_alloc);
210✔
863
                    list.init_from_ref(ref);
210✔
864
                    for (size_t i = 0; i < list.size(); i++) {
978✔
865
                        Mixed val = list.get(i);
768✔
866
                        if (val.is_type(type_TypedLink)) {
768✔
867
                            ObjLink link = val.get<ObjLink>();
540✔
868
                            auto target_obj = origin_table->get_parent_group()->get_object(link);
540✔
869
                            ColKey backlink_col_key =
540✔
870
                                target_obj.get_table()->find_backlink_column(col_key, origin_table->get_key());
540✔
871
                            target_obj.remove_one_backlink(backlink_col_key, ObjKey(key.value + m_offset));
540✔
872
                        }
540✔
873
                    }
768✔
874
                }
210✔
875
                Array::destroy_deep(ref, m_alloc);
19,077✔
876
            }
19,077✔
877

26,856✔
878
            values.erase(ndx);
54,567✔
879

26,856✔
880
            return IteratorControl::AdvanceToNext;
54,567✔
881
        }
54,567✔
882

3,346,941✔
883
        switch (col_type) {
6,689,931✔
884
            case col_type_Int:
6,020,322✔
885
                if (attr.test(col_attr_Nullable)) {
6,020,322✔
886
                    do_erase<ArrayIntNull>(ndx, col_key);
1,289,265✔
887
                }
1,289,265✔
888
                else {
4,731,057✔
889
                    do_erase<ArrayInteger>(ndx, col_key);
4,731,057✔
890
                }
4,731,057✔
891
                break;
6,020,322✔
892
            case col_type_Bool:
42,492✔
893
                do_erase<ArrayBoolNull>(ndx, col_key);
42,492✔
894
                break;
42,492✔
895
            case col_type_Float:
36,489✔
896
                do_erase<ArrayFloatNull>(ndx, col_key);
36,489✔
897
                break;
36,489✔
898
            case col_type_Double:
36,504✔
899
                do_erase<ArrayDoubleNull>(ndx, col_key);
36,504✔
900
                break;
36,504✔
901
            case col_type_String:
122,484✔
902
                do_erase<ArrayString>(ndx, col_key);
122,484✔
903
                break;
122,484✔
904
            case col_type_Binary:
38,883✔
905
                do_erase<ArrayBinary>(ndx, col_key);
38,883✔
906
                break;
38,883✔
907
            case col_type_Mixed:
609✔
908
                do_erase<ArrayMixed>(ndx, col_key);
609✔
909
                break;
609✔
910
            case col_type_Timestamp:
45,714✔
911
                do_erase<ArrayTimestamp>(ndx, col_key);
45,714✔
912
                break;
45,714✔
913
            case col_type_Decimal:
36,279✔
914
                do_erase<ArrayDecimal128>(ndx, col_key);
36,279✔
915
                break;
36,279✔
916
            case col_type_ObjectId:
39,057✔
917
                do_erase<ArrayObjectIdNull>(ndx, col_key);
39,057✔
918
                break;
39,057✔
919
            case col_type_UUID:
60,285✔
920
                do_erase<ArrayUUIDNull>(ndx, col_key);
60,285✔
921
                break;
60,285✔
922
            case col_type_Link:
7,365✔
923
                do_erase_key(ndx, col_key, state);
7,365✔
924
                break;
7,365✔
925
            case col_type_TypedLink:
6✔
926
                do_erase<ArrayTypedLink>(ndx, col_key);
6✔
927
                break;
6✔
928
            case col_type_BackLink:
203,556✔
929
                if (state.m_mode == CascadeState::Mode::None) {
203,556✔
930
                    do_erase<ArrayBacklink>(ndx, col_key);
176,721✔
931
                }
176,721✔
932
                else {
26,835✔
933
                    // Postpone the deletion of backlink entries or else the
13,395✔
934
                    // checks for if there's any remaining backlinks will
13,395✔
935
                    // check the wrong row for columns which have already
13,395✔
936
                    // had values erased from them.
13,395✔
937
                    backlink_column_keys.push_back(col_key);
26,835✔
938
                }
26,835✔
939
                break;
203,556✔
940
            default:
✔
941
                REALM_ASSERT(false);
×
942
                break;
×
943
        }
6,689,934✔
944
        return IteratorControl::AdvanceToNext;
6,689,934✔
945
    };
6,689,934✔
946
    m_tree_top.m_owner->for_each_and_every_column(erase_in_column);
5,026,989✔
947

2,514,660✔
948
    // Any remaining backlink columns to erase from?
2,514,660✔
949
    for (auto k : backlink_column_keys)
5,026,989✔
950
        do_erase<ArrayBacklink>(ndx, k);
26,835✔
951

2,514,660✔
952
    if (m_keys.is_attached()) {
5,026,989✔
953
        m_keys.erase(ndx);
4,969,617✔
954
    }
4,969,617✔
955
    else {
57,372✔
956
        size_t current_size = get_size_in_compact_form();
57,372✔
957
        if (ndx == current_size - 1) {
57,372✔
958
            // When deleting last, we can still maintain compact form
18,390✔
959
            set(0, RefOrTagged::make_tagged(current_size - 1));
37,014✔
960
        }
37,014✔
961
        else {
20,358✔
962
            ensure_general_form();
20,358✔
963
            m_keys.erase(ndx);
20,358✔
964
        }
20,358✔
965
    }
57,372✔
966

2,514,660✔
967
    return node_size();
5,026,989✔
968
}
5,026,989✔
969

970
void Cluster::nullify_incoming_links(ObjKey key, CascadeState& state)
971
{
4,927,518✔
972
    size_t ndx = get_ndx(key, 0);
4,927,518✔
973
    if (ndx == realm::npos)
4,927,518✔
974
        throw KeyNotFound(util::format("Key '%1' not found in '%2' when nullifying incoming links", key.value,
×
975
                                       get_owning_table()->get_class_name()));
×
976

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

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

93,588✔
1005
        return IteratorControl::AdvanceToNext;
187,260✔
1006
    };
187,260✔
1007

2,464,770✔
1008
    m_tree_top.get_owning_table()->for_each_backlink_column(nullify_fwd_links);
4,927,518✔
1009
}
4,927,518✔
1010

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

1030
void Cluster::init_leaf(ColKey col_key, ArrayPayload* leaf) const
1031
{
7,860,708✔
1032
    auto col_ndx = col_key.get_index();
7,860,708✔
1033
    // FIXME: Move this validation into callers.
3,462,876✔
1034
    // Currently, the query subsystem may call with an unvalidated key.
3,462,876✔
1035
    // once fixed, reintroduce the noexcept declaration :-D
3,462,876✔
1036
    if (auto t = m_tree_top.get_owning_table())
7,860,708✔
1037
        t->check_column(col_key);
8,224,197✔
1038
    ref_type ref = to_ref(Array::get(col_ndx.val + 1));
7,860,708✔
1039
    if (leaf->need_spec()) {
7,860,708✔
1040
        m_tree_top.set_spec(*leaf, col_ndx);
143,697✔
1041
    }
143,697✔
1042
    leaf->init_from_ref(ref);
7,860,708✔
1043
    leaf->set_parent(const_cast<Cluster*>(this), col_ndx.val + 1);
7,860,708✔
1044
}
7,860,708✔
1045

1046
void Cluster::add_leaf(ColKey col_key, ref_type ref)
1047
{
×
1048
    auto col_ndx = col_key.get_index();
×
1049
    REALM_ASSERT((col_ndx.val + 1) == size());
×
1050
    Array::insert(col_ndx.val + 1, from_ref(ref));
×
1051
}
×
1052

1053
template <typename ArrayType>
1054
void Cluster::verify(ref_type ref, size_t index, util::Optional<size_t>& sz) const
1055
{
6,943,218✔
1056
    ArrayType arr(get_alloc());
6,943,218✔
1057
    set_spec(arr, ColKey::Idx{unsigned(index) - 1});
6,943,218✔
1058
    arr.set_parent(const_cast<Cluster*>(this), index);
6,943,218✔
1059
    arr.init_from_ref(ref);
6,943,218✔
1060
    arr.verify();
6,943,218✔
1061
    if (sz) {
6,943,218!
1062
        REALM_ASSERT(arr.size() == *sz);
3,145,755!
1063
    }
3,145,755✔
1064
    else {
3,797,463✔
1065
        sz = arr.size();
3,797,463✔
1066
    }
3,797,463✔
1067
}
6,943,218✔
1068
namespace {
1069

1070
template <typename ArrayType>
1071
void verify_list(ArrayRef& arr, size_t sz)
1072
{
1,629,555✔
1073
    for (size_t n = 0; n < sz; n++) {
6,339,204!
1074
        if (ref_type bp_tree_ref = arr.get(n)) {
4,709,649!
1075
            BPlusTree<ArrayType> links(arr.get_alloc());
1,650,243✔
1076
            links.init_from_ref(bp_tree_ref);
1,650,243✔
1077
            links.set_parent(&arr, n);
1,650,243✔
1078
            links.verify();
1,650,243✔
1079
        }
1,650,243✔
1080
    }
4,709,649✔
1081
}
1,629,555✔
1082

1083
template <typename SetType>
1084
void verify_set(ArrayRef& arr, size_t sz)
1085
{
120✔
1086
    for (size_t n = 0; n < sz; ++n) {
282!
1087
        if (ref_type bp_tree_ref = arr.get(n)) {
162!
1088
            BPlusTree<SetType> elements(arr.get_alloc());
162✔
1089
            elements.init_from_ref(bp_tree_ref);
162✔
1090
            elements.set_parent(&arr, n);
162✔
1091
            elements.verify();
162✔
1092

81✔
1093
            // FIXME: Check uniqueness of elements.
81✔
1094
        }
162✔
1095
    }
162✔
1096
}
120✔
1097

1098
} // namespace
1099

1100
void Cluster::verify() const
1101
{
3,833,961✔
1102
#ifdef REALM_DEBUG
3,833,961✔
1103
    util::Optional<size_t> sz;
3,833,961✔
1104

1,916,124✔
1105
    auto verify_column = [this, &sz](ColKey col_key) {
8,585,163✔
1106
        size_t col = col_key.get_index().val + s_first_col_index;
8,585,163✔
1107
        ref_type ref = Array::get_as_ref(col);
8,585,163✔
1108
        auto attr = col_key.get_attrs();
8,585,163✔
1109
        auto col_type = col_key.get_type();
8,585,163✔
1110
        bool nullable = attr.test(col_attr_Nullable);
8,585,163✔
1111

4,254,039✔
1112
        if (attr.test(col_attr_List)) {
8,585,163✔
1113
            ArrayRef arr(get_alloc());
1,629,573✔
1114
            arr.set_parent(const_cast<Cluster*>(this), col);
1,629,573✔
1115
            arr.init_from_ref(ref);
1,629,573✔
1116
            arr.verify();
1,629,573✔
1117
            if (sz) {
1,629,573✔
1118
                REALM_ASSERT(arr.size() == *sz);
1,605,339✔
1119
            }
1,605,339✔
1120
            else {
24,234✔
1121
                sz = arr.size();
24,234✔
1122
            }
24,234✔
1123

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

3,462,501✔
1245
        switch (col_type) {
6,943,218✔
1246
            case col_type_Int:
4,745,409✔
1247
                if (nullable) {
4,745,409✔
1248
                    verify<ArrayIntNull>(ref, col, sz);
806,676✔
1249
                }
806,676✔
1250
                else {
3,938,733✔
1251
                    verify<ArrayInteger>(ref, col, sz);
3,938,733✔
1252
                }
3,938,733✔
1253
                break;
4,745,409✔
1254
            case col_type_Bool:
7,212✔
1255
                verify<ArrayBoolNull>(ref, col, sz);
7,212✔
1256
                break;
7,212✔
1257
            case col_type_Float:
174✔
1258
                verify<ArrayFloatNull>(ref, col, sz);
174✔
1259
                break;
174✔
1260
            case col_type_Double:
240✔
1261
                verify<ArrayDoubleNull>(ref, col, sz);
240✔
1262
                break;
240✔
1263
            case col_type_String:
2,178,696✔
1264
                verify<ArrayString>(ref, col, sz);
2,178,696✔
1265
                break;
2,178,696✔
1266
            case col_type_Binary:
174✔
1267
                verify<ArrayBinary>(ref, col, sz);
174✔
1268
                break;
174✔
1269
            case col_type_Mixed:
6✔
1270
                verify<ArrayMixed>(ref, col, sz);
6✔
1271
                break;
6✔
1272
            case col_type_Timestamp:
6,984✔
1273
                verify<ArrayTimestamp>(ref, col, sz);
6,984✔
1274
                break;
6,984✔
1275
            case col_type_Decimal:
42✔
1276
                verify<ArrayDecimal128>(ref, col, sz);
42✔
1277
                break;
42✔
1278
            case col_type_ObjectId:
42✔
1279
                verify<ArrayObjectIdNull>(ref, col, sz);
42✔
1280
                break;
42✔
1281
            case col_type_UUID:
✔
1282
                verify<ArrayUUIDNull>(ref, col, sz);
×
1283
                break;
×
1284
            case col_type_Link:
1,371✔
1285
                verify<ArrayKey>(ref, col, sz);
1,371✔
1286
                break;
1,371✔
1287
            case col_type_BackLink:
2,868✔
1288
                verify<ArrayBacklink>(ref, col, sz);
2,868✔
1289
                break;
2,868✔
1290
            default:
6✔
1291
                break;
6✔
1292
        }
6,943,227✔
1293
        return IteratorControl::AdvanceToNext;
6,943,227✔
1294
    };
6,943,227✔
1295

1,916,124✔
1296
    m_tree_top.m_owner->for_each_and_every_column(verify_column);
3,833,961✔
1297
#endif
3,833,961✔
1298
}
3,833,961✔
1299

1300
// LCOV_EXCL_START
1301
void Cluster::dump_objects(int64_t key_offset, std::string lead) const
1302
{
×
1303
    std::cout << lead << "leaf - size: " << node_size() << std::endl;
×
1304
    if (!m_keys.is_attached()) {
×
1305
        std::cout << lead << "compact form" << std::endl;
×
1306
    }
×
1307

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

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

1484
void Cluster::remove_backlinks(ObjKey origin_key, ColKey origin_col_key, const std::vector<ObjKey>& keys,
1485
                               CascadeState& state) const
1486
{
6,735✔
1487
    const Table* origin_table = m_tree_top.get_owning_table();
6,735✔
1488
    TableRef target_table = origin_table->get_opposite_table(origin_col_key);
6,735✔
1489
    ColKey backlink_col_key = origin_table->get_opposite_column(origin_col_key);
6,735✔
1490
    bool strong_links = target_table->is_embedded();
6,735✔
1491

3,363✔
1492
    for (auto key : keys) {
8,337✔
1493
        if (key != null_key) {
8,337✔
1494
            bool is_unres = key.is_unresolved();
8,337✔
1495
            Obj target_obj = is_unres ? target_table->m_tombstones->get(key) : target_table->m_clusters.get(key);
8,322✔
1496
            bool last_removed = target_obj.remove_one_backlink(backlink_col_key, origin_key); // Throws
8,337✔
1497
            if (is_unres) {
8,337✔
1498
                if (last_removed) {
30✔
1499
                    // Check is there are more backlinks
12✔
1500
                    if (!target_obj.has_backlinks(false)) {
24✔
1501
                        // Tombstones can be erased right away - there is no cascading effect
9✔
1502
                        target_table->m_tombstones->erase(key, state);
18✔
1503
                    }
18✔
1504
                }
24✔
1505
            }
30✔
1506
            else {
8,307✔
1507
                state.enqueue_for_cascade(target_obj, strong_links, last_removed);
8,307✔
1508
            }
8,307✔
1509
        }
8,337✔
1510
    }
8,337✔
1511
}
6,735✔
1512

1513
void Cluster::remove_backlinks(ObjKey origin_key, ColKey origin_col_key, const std::vector<ObjLink>& links,
1514
                               CascadeState& state) const
1515
{
162✔
1516
    const Table* origin_table = m_tree_top.get_owning_table();
162✔
1517
    Group* group = origin_table->get_parent_group();
162✔
1518
    TableKey origin_table_key = origin_table->get_key();
162✔
1519

81✔
1520
    for (auto link : links) {
270✔
1521
        if (link) {
270✔
1522
            bool is_unres = link.get_obj_key().is_unresolved();
270✔
1523
            Obj target_obj = group->get_object(link);
270✔
1524
            TableRef target_table = target_obj.get_table();
270✔
1525
            ColKey backlink_col_key = target_table->find_or_add_backlink_column(origin_col_key, origin_table_key);
270✔
1526

135✔
1527
            bool last_removed = target_obj.remove_one_backlink(backlink_col_key, origin_key); // Throws
270✔
1528
            if (is_unres) {
270✔
1529
                if (last_removed) {
12✔
1530
                    // Check is there are more backlinks
6✔
1531
                    if (!target_obj.has_backlinks(false)) {
12✔
1532
                        // Tombstones can be erased right away - there is no cascading effect
3✔
1533
                        target_table->m_tombstones->erase(link.get_obj_key(), state);
6✔
1534
                    }
6✔
1535
                }
12✔
1536
            }
12✔
1537
            else {
258✔
1538
                state.enqueue_for_cascade(target_obj, false, last_removed);
258✔
1539
            }
258✔
1540
        }
270✔
1541
    }
270✔
1542
}
162✔
1543

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

© 2025 Coveralls, Inc