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

realm / realm-core / 2451

29 Jun 2024 12:42AM UTC coverage: 90.99% (+0.03%) from 90.961%
2451

push

Evergreen

web-flow
RCORE-2175 RCORE-2176 RCORE-2182 RCORE-2083 Fix several problems with object removal and links (#7830)

* fix change of mode from Strong to All when deleting embedded objects that link to a tombstone

* fix remove recursive to follow mixed links

* fix removal of backlinks from mixed in large clusters

* add more test coverage to verify single link removal

* Replace ObjKey with RowKey in selected places

* fix a few more type casts

* touchup changelog

---------

Co-authored-by: Jørgen Edelbo <jorgen.edelbo@mongodb.com>

102368 of 180506 branches covered (56.71%)

371 of 382 new or added lines in 8 files covered. (97.12%)

50 existing lines in 11 files now uncovered.

215226 of 236537 relevant lines covered (90.99%)

5659844.03 hits per line

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

88.03
/src/realm/array_string.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/array_string.hpp>
20
#include <realm/spec.hpp>
21
#include <realm/mixed.hpp>
22

23
using namespace realm;
24

25
ArrayString::ArrayString(Allocator& a)
26
    : m_alloc(a)
9,784,260✔
27
{
19,685,151✔
28
    m_arr = new (&m_storage) ArrayStringShort(a, true);
19,685,151✔
29
}
19,685,151✔
30

31
void ArrayString::create()
32
{
548,466✔
33
    static_cast<ArrayStringShort*>(m_arr)->create();
548,466✔
34
}
548,466✔
35

36
void ArrayString::init_from_mem(MemRef mem) noexcept
37
{
12,676,599✔
38
    char* header = mem.get_addr();
12,676,599✔
39

40
    ArrayParent* parent = m_arr->get_parent();
12,676,599✔
41
    size_t ndx_in_parent = m_arr->get_ndx_in_parent();
12,676,599✔
42

43
    bool long_strings = Array::get_hasrefs_from_header(header);
12,676,599✔
44
    if (!long_strings) {
12,676,599✔
45
        // Small strings
46
        bool is_small = Array::get_wtype_from_header(header) == Array::wtype_Multiply;
9,902,229✔
47
        if (is_small) {
9,902,229✔
48
            auto arr = new (&m_storage) ArrayStringShort(m_alloc, m_nullable);
6,208,146✔
49
            arr->init_from_mem(mem);
6,208,146✔
50
            m_type = Type::small_strings;
6,208,146✔
51
        }
6,208,146✔
52
        else {
3,694,083✔
53
            auto arr = new (&m_storage) Array(m_alloc);
3,694,083✔
54
            arr->init_from_mem(mem);
3,694,083✔
55
            m_string_enum_values = std::make_unique<ArrayString>(m_alloc);
3,694,083✔
56
            ArrayParent* p;
3,694,083✔
57
            REALM_ASSERT(m_spec != nullptr);
3,694,083✔
58
            REALM_ASSERT(m_col_ndx != realm::npos);
3,694,083✔
59
            ref_type r = m_spec->get_enumkeys_ref(m_col_ndx, p);
3,694,083✔
60
            m_string_enum_values->init_from_ref(r);
3,694,083✔
61
            m_string_enum_values->set_parent(p, m_col_ndx);
3,694,083✔
62
            m_type = Type::enum_strings;
3,694,083✔
63
        }
3,694,083✔
64
    }
9,902,229✔
65
    else {
2,774,370✔
66
        bool is_big = Array::get_context_flag_from_header(header);
2,774,370✔
67
        if (!is_big) {
2,774,370✔
68
            auto arr = new (&m_storage) ArraySmallBlobs(m_alloc);
706,173✔
69
            arr->init_from_mem(mem);
706,173✔
70
            m_type = Type::medium_strings;
706,173✔
71
        }
706,173✔
72
        else {
2,068,197✔
73
            auto arr = new (&m_storage) ArrayBigBlobs(m_alloc, m_nullable);
2,068,197✔
74
            arr->init_from_mem(mem);
2,068,197✔
75
            m_type = Type::big_strings;
2,068,197✔
76
        }
2,068,197✔
77
    }
2,774,370✔
78
    m_arr->set_parent(parent, ndx_in_parent);
12,676,599✔
79
}
12,676,599✔
80

81
void ArrayString::init_from_parent()
82
{
5,821,467✔
83
    ref_type ref = m_arr->get_ref_from_parent();
5,821,467✔
84
    init_from_ref(ref);
5,821,467✔
85
}
5,821,467✔
86

87
void ArrayString::destroy() noexcept
88
{
1,044✔
89
    if (m_arr->is_attached()) {
1,044✔
90
        Array::destroy_deep(m_arr->get_ref(), m_alloc);
468✔
91
        detach();
468✔
92
    }
468✔
93
}
1,044✔
94

95
void ArrayString::detach() noexcept
96
{
4,174,197✔
97
    m_arr->detach();
4,174,197✔
98
    // Make sure the object is in a state like right after construction
99
    // Next call must be to create()
100
    m_arr = new (&m_storage) ArrayStringShort(m_alloc, true);
4,174,197✔
101
    m_type = Type::small_strings;
4,174,197✔
102
}
4,174,197✔
103

104
size_t ArrayString::size() const
105
{
16,151,640✔
106
    switch (m_type) {
16,151,640✔
107
        case Type::small_strings:
6,857,493✔
108
            return static_cast<ArrayStringShort*>(m_arr)->size();
6,857,493✔
109
        case Type::medium_strings:
465,507✔
110
            return static_cast<ArraySmallBlobs*>(m_arr)->size();
465,507✔
111
        case Type::big_strings:
8,882,370✔
112
            return static_cast<ArrayBigBlobs*>(m_arr)->size();
8,882,370✔
113
        case Type::enum_strings:
246✔
114
            return static_cast<Array*>(m_arr)->size();
246✔
115
    }
16,151,640✔
116
    return {};
×
117
}
16,151,640✔
118

119
void ArrayString::add(StringData value)
120
{
448,509✔
121
    switch (upgrade_leaf(value.size())) {
448,509✔
122
        case Type::small_strings:
392,271✔
123
            static_cast<ArrayStringShort*>(m_arr)->add(value);
392,271✔
124
            break;
392,271✔
125
        case Type::medium_strings:
28,257✔
126
            static_cast<ArraySmallBlobs*>(m_arr)->add_string(value);
28,257✔
127
            break;
28,257✔
128
        case Type::big_strings:
27,987✔
129
            static_cast<ArrayBigBlobs*>(m_arr)->add_string(value);
27,987✔
130
            break;
27,987✔
131
        case Type::enum_strings: {
✔
132
            auto a = static_cast<Array*>(m_arr);
×
133
            size_t ndx = a->size();
×
134
            a->add(0);
×
135
            set(ndx, value);
×
136
            break;
×
137
        }
×
138
    }
448,509✔
139
}
448,509✔
140

141
void ArrayString::set(size_t ndx, StringData value)
142
{
3,757,422✔
143
    switch (upgrade_leaf(value.size())) {
3,757,422✔
144
        case Type::small_strings:
500,067✔
145
            static_cast<ArrayStringShort*>(m_arr)->set(ndx, value);
500,067✔
146
            break;
500,067✔
147
        case Type::medium_strings:
249,723✔
148
            static_cast<ArraySmallBlobs*>(m_arr)->set_string(ndx, value);
249,723✔
149
            break;
249,723✔
150
        case Type::big_strings:
384,417✔
151
            static_cast<ArrayBigBlobs*>(m_arr)->set_string(ndx, value);
384,417✔
152
            break;
384,417✔
153
        case Type::enum_strings: {
2,625,516✔
154
            size_t sz = m_string_enum_values->size();
2,625,516✔
155
            size_t res = m_string_enum_values->find_first(value, 0, sz);
2,625,516✔
156
            if (res == realm::not_found) {
2,625,516✔
157
                m_string_enum_values->add(value);
9,648✔
158
                res = sz;
9,648✔
159
            }
9,648✔
160
            static_cast<Array*>(m_arr)->set(ndx, res);
2,625,516✔
161
            break;
2,625,516✔
162
        }
×
163
    }
3,757,422✔
164
}
3,757,422✔
165

166
void ArrayString::insert(size_t ndx, StringData value)
167
{
4,244,715✔
168
    switch (upgrade_leaf(value.size())) {
4,244,715✔
169
        case Type::small_strings:
1,833,285✔
170
            static_cast<ArrayStringShort*>(m_arr)->insert(ndx, value);
1,833,285✔
171
            break;
1,833,285✔
172
        case Type::medium_strings:
295,155✔
173
            static_cast<ArraySmallBlobs*>(m_arr)->insert_string(ndx, value);
295,155✔
174
            break;
295,155✔
175
        case Type::big_strings:
805,503✔
176
            static_cast<ArrayBigBlobs*>(m_arr)->insert_string(ndx, value);
805,503✔
177
            break;
805,503✔
178
        case Type::enum_strings: {
1,312,734✔
179
            static_cast<Array*>(m_arr)->insert(ndx, 0);
1,312,734✔
180
            set(ndx, value);
1,312,734✔
181
        }
1,312,734✔
182
    }
4,244,715✔
183
}
4,244,715✔
184

185
StringData ArrayString::get(size_t ndx) const
186
{
20,957,358✔
187
    switch (m_type) {
20,957,358✔
188
        case Type::small_strings:
11,022,132✔
189
            return static_cast<ArrayStringShort*>(m_arr)->get(ndx);
11,022,132✔
190
        case Type::medium_strings:
917,262✔
191
            return static_cast<ArraySmallBlobs*>(m_arr)->get_string(ndx);
917,262✔
192
        case Type::big_strings:
7,890,798✔
193
            return static_cast<ArrayBigBlobs*>(m_arr)->get_string(ndx);
7,890,798✔
194
        case Type::enum_strings: {
1,139,001✔
195
            size_t index = size_t(static_cast<Array*>(m_arr)->get(ndx));
1,139,001✔
196
            return m_string_enum_values->get(index);
1,139,001✔
197
        }
×
198
    }
20,957,358✔
199
    return {};
×
200
}
20,957,358✔
201

202
StringData ArrayString::get_legacy(size_t ndx) const
203
{
×
204
    switch (m_type) {
×
205
        case Type::small_strings:
×
206
            return static_cast<ArrayStringShort*>(m_arr)->get(ndx);
×
207
        case Type::medium_strings:
×
208
            return static_cast<ArraySmallBlobs*>(m_arr)->get_string_legacy(ndx);
×
209
        case Type::big_strings:
×
210
            return static_cast<ArrayBigBlobs*>(m_arr)->get_string(ndx);
×
211
        case Type::enum_strings: {
×
212
            size_t index = size_t(static_cast<Array*>(m_arr)->get(ndx));
×
213
            return m_string_enum_values->get(index);
×
214
        }
×
215
    }
×
216
    return {};
×
217
}
×
218

219
Mixed ArrayString::get_any(size_t ndx) const
220
{
132,768✔
221
    return Mixed(get(ndx));
132,768✔
222
}
132,768✔
223

224
bool ArrayString::is_null(size_t ndx) const
225
{
3,947,394✔
226
    switch (m_type) {
3,947,394✔
227
        case Type::small_strings:
636,144✔
228
            return static_cast<ArrayStringShort*>(m_arr)->is_null(ndx);
636,144✔
229
        case Type::medium_strings:
20,241✔
230
            return static_cast<ArraySmallBlobs*>(m_arr)->is_null(ndx);
20,241✔
231
        case Type::big_strings:
3,291,033✔
232
            return static_cast<ArrayBigBlobs*>(m_arr)->is_null(ndx);
3,291,033✔
233
        case Type::enum_strings: {
✔
234
            size_t index = size_t(static_cast<Array*>(m_arr)->get(ndx));
×
235
            return m_string_enum_values->is_null(index);
×
236
        }
×
237
    }
3,947,394✔
238
    return {};
×
239
}
3,947,394✔
240

241
void ArrayString::erase(size_t ndx)
242
{
409,692✔
243
    switch (m_type) {
409,692✔
244
        case Type::small_strings:
318,078✔
245
            static_cast<ArrayStringShort*>(m_arr)->erase(ndx);
318,078✔
246
            break;
318,078✔
247
        case Type::medium_strings:
57,669✔
248
            static_cast<ArraySmallBlobs*>(m_arr)->erase(ndx);
57,669✔
249
            break;
57,669✔
250
        case Type::big_strings:
25,569✔
251
            static_cast<ArrayBigBlobs*>(m_arr)->erase(ndx);
25,569✔
252
            break;
25,569✔
253
        case Type::enum_strings:
8,415✔
254
            static_cast<Array*>(m_arr)->erase(ndx);
8,415✔
255
            break;
8,415✔
256
    }
409,692✔
257
}
409,692✔
258

259
void ArrayString::move(ArrayString& dst, size_t ndx)
260
{
1,074✔
261
    size_t sz = size();
1,074✔
262
    for (size_t i = ndx; i < sz; i++) {
116,787✔
263
        dst.add(get(i));
115,713✔
264
    }
115,713✔
265

266
    switch (m_type) {
1,074✔
267
        case Type::small_strings:
1,008✔
268
            static_cast<ArrayStringShort*>(m_arr)->truncate(ndx);
1,008✔
269
            break;
1,008✔
270
        case Type::medium_strings:
60✔
271
            static_cast<ArraySmallBlobs*>(m_arr)->truncate(ndx);
60✔
272
            break;
60✔
273
        case Type::big_strings:
6✔
274
            static_cast<ArrayBigBlobs*>(m_arr)->truncate(ndx);
6✔
275
            break;
6✔
276
        case Type::enum_strings:
✔
277
            // this operation will never be called for enumerated columns
278
            REALM_UNREACHABLE();
279
            break;
×
280
    }
1,074✔
281
}
1,074✔
282

283
void ArrayString::clear()
284
{
357✔
285
    switch (m_type) {
357✔
286
        case Type::small_strings:
303✔
287
            static_cast<ArrayStringShort*>(m_arr)->clear();
303✔
288
            break;
303✔
289
        case Type::medium_strings:
6✔
290
            static_cast<ArraySmallBlobs*>(m_arr)->clear();
6✔
291
            break;
6✔
292
        case Type::big_strings:
48✔
293
            static_cast<ArrayBigBlobs*>(m_arr)->clear();
48✔
294
            break;
48✔
295
        case Type::enum_strings:
✔
296
            static_cast<Array*>(m_arr)->clear();
×
297
            break;
×
298
    }
357✔
299
}
357✔
300

301
size_t ArrayString::find_first(StringData value, size_t begin, size_t end) const noexcept
302
{
4,092,513✔
303
    switch (m_type) {
4,092,513✔
304
        case Type::small_strings:
3,111,816✔
305
            return static_cast<ArrayStringShort*>(m_arr)->find_first(value, begin, end);
3,111,816✔
306
        case Type::medium_strings: {
130,314✔
307
            BinaryData as_binary(value.data(), value.size());
130,314✔
308
            return static_cast<ArraySmallBlobs*>(m_arr)->find_first(as_binary, true, begin, end);
130,314✔
309
            break;
×
310
        }
×
311
        case Type::big_strings: {
254,748✔
312
            BinaryData as_binary(value.data(), value.size());
254,748✔
313
            return static_cast<ArrayBigBlobs*>(m_arr)->find_first(as_binary, true, begin, end);
254,748✔
314
            break;
×
315
        }
×
316
        case Type::enum_strings: {
596,073✔
317
            size_t sz = m_string_enum_values->size();
596,073✔
318
            size_t res = m_string_enum_values->find_first(value, 0, sz);
596,073✔
319
            if (res != realm::not_found) {
596,073✔
320
                return static_cast<Array*>(m_arr)->find_first(res, begin, end);
596,073✔
321
            }
596,073✔
UNCOV
322
            break;
×
323
        }
596,073✔
324
    }
4,092,513✔
325
    return not_found;
×
326
}
4,092,513✔
327

328
namespace {
329

330
template <class T>
331
inline StringData get_string(const T* arr, size_t ndx)
332
{
437,337✔
333
    return arr->get_string(ndx);
437,337✔
334
}
437,337✔
335

336
template <>
337
inline StringData get_string(const ArrayStringShort* arr, size_t ndx)
338
{
93,654✔
339
    return arr->get(ndx);
93,654✔
340
}
93,654✔
341

342
template <class T, class U>
343
size_t lower_bound_string(const T* arr, U value)
344
{
234,912✔
345
    size_t i = 0;
234,912✔
346
    size_t sz = arr->size();
234,912✔
347
    while (0 < sz) {
765,903✔
348
        size_t half = sz / 2;
530,991✔
349
        size_t mid = i + half;
530,991✔
350
        auto probe = get_string(arr, mid);
530,991✔
351
        if (probe < value) {
530,991✔
352
            i = mid + 1;
191,055✔
353
            sz -= half + 1;
191,055✔
354
        }
191,055✔
355
        else {
339,936✔
356
            sz = half;
339,936✔
357
        }
339,936✔
358
    }
530,991✔
359
    return i;
234,912✔
360
}
234,912✔
361
} // namespace
362

363
size_t ArrayString::lower_bound(StringData value)
364
{
234,912✔
365
    switch (m_type) {
234,912✔
366
        case Type::small_strings:
58,434✔
367
            return lower_bound_string(static_cast<ArrayStringShort*>(m_arr), value);
58,434✔
368
        case Type::medium_strings:
68,418✔
369
            return lower_bound_string(static_cast<ArraySmallBlobs*>(m_arr), value);
68,418✔
370
        case Type::big_strings:
108,060✔
371
            return lower_bound_string(static_cast<ArrayBigBlobs*>(m_arr), value);
108,060✔
372
        case Type::enum_strings:
✔
373
            break;
×
374
    }
234,912✔
375
    return realm::npos;
×
376
}
234,912✔
377

378
ArrayString::Type ArrayString::upgrade_leaf(size_t value_size)
379
{
8,443,422✔
380
    if (m_type == Type::big_strings)
8,443,422✔
381
        return Type::big_strings;
1,197,480✔
382

383
    if (m_type == Type::enum_strings)
7,245,942✔
384
        return Type::enum_strings;
3,938,169✔
385

386
    if (m_type == Type::medium_strings) {
3,307,773✔
387
        if (value_size <= medium_string_max_size)
533,259✔
388
            return Type::medium_strings;
532,704✔
389

390
        // Upgrade root leaf from medium to big strings
391
        auto string_medium = static_cast<ArraySmallBlobs*>(m_arr);
555✔
392
        ArrayBigBlobs big_blobs(m_alloc, true);
555✔
393
        big_blobs.create(); // Throws
555✔
394

395
        size_t n = string_medium->size();
555✔
396
        for (size_t i = 0; i < n; i++) {
10,260✔
397
            big_blobs.add_string(string_medium->get_string(i)); // Throws
9,705✔
398
        }
9,705✔
399
        auto parent = string_medium->get_parent();
555✔
400
        auto ndx_in_parent = string_medium->get_ndx_in_parent();
555✔
401
        string_medium->destroy();
555✔
402

403
        auto arr = new (&m_storage) ArrayBigBlobs(m_alloc, true);
555✔
404
        arr->init_from_mem(big_blobs.get_mem());
555✔
405
        arr->set_parent(parent, ndx_in_parent);
555✔
406
        arr->update_parent();
555✔
407

408
        m_type = Type::big_strings;
555✔
409
        return Type::big_strings;
555✔
410
    }
533,259✔
411

412
    // m_type == Type::small
413
    if (value_size <= small_string_max_size)
2,774,514✔
414
        return Type::small_strings;
2,724,930✔
415

416
    if (value_size <= medium_string_max_size) {
49,584✔
417
        // Upgrade root leaf from small to medium strings
418
        auto string_short = static_cast<ArrayStringShort*>(m_arr);
40,428✔
419
        ArraySmallBlobs string_long(m_alloc);
40,428✔
420
        string_long.create(); // Throws
40,428✔
421

422
        size_t n = string_short->size();
40,428✔
423
        for (size_t i = 0; i < n; i++) {
73,932✔
424
            string_long.add_string(string_short->get(i)); // Throws
33,504✔
425
        }
33,504✔
426
        auto parent = string_short->get_parent();
40,428✔
427
        auto ndx_in_parent = string_short->get_ndx_in_parent();
40,428✔
428
        string_short->destroy();
40,428✔
429

430
        auto arr = new (&m_storage) ArraySmallBlobs(m_alloc);
40,428✔
431
        arr->init_from_mem(string_long.get_mem());
40,428✔
432
        arr->set_parent(parent, ndx_in_parent);
40,428✔
433
        arr->update_parent();
40,428✔
434

435
        m_type = Type::medium_strings;
40,428✔
436
    }
40,428✔
437
    else {
9,156✔
438
        // Upgrade root leaf from small to big strings
439
        auto string_short = static_cast<ArrayStringShort*>(m_arr);
9,156✔
440
        ArrayBigBlobs big_blobs(m_alloc, true);
9,156✔
441
        big_blobs.create(); // Throws
9,156✔
442

443
        size_t n = string_short->size();
9,156✔
444
        for (size_t i = 0; i < n; i++) {
29,349✔
445
            big_blobs.add_string(string_short->get(i)); // Throws
20,193✔
446
        }
20,193✔
447
        auto parent = string_short->get_parent();
9,156✔
448
        auto ndx_in_parent = string_short->get_ndx_in_parent();
9,156✔
449
        string_short->destroy();
9,156✔
450

451
        auto arr = new (&m_storage) ArrayBigBlobs(m_alloc, true);
9,156✔
452
        arr->init_from_mem(big_blobs.get_mem());
9,156✔
453
        arr->set_parent(parent, ndx_in_parent);
9,156✔
454
        arr->update_parent();
9,156✔
455

456
        m_type = Type::big_strings;
9,156✔
457
    }
9,156✔
458

459
    return m_type;
49,584✔
460
}
2,774,514✔
461

462
void ArrayString::verify() const
463
{
163,578✔
464
#ifdef REALM_DEBUG
163,578✔
465
    switch (m_type) {
163,578✔
466
        case Type::small_strings:
157,155✔
467
            static_cast<ArrayStringShort*>(m_arr)->verify();
157,155✔
468
            break;
157,155✔
469
        case Type::medium_strings:
852✔
470
            static_cast<ArraySmallBlobs*>(m_arr)->verify();
852✔
471
            break;
852✔
472
        case Type::big_strings:
5,328✔
473
            static_cast<ArrayBigBlobs*>(m_arr)->verify();
5,328✔
474
            break;
5,328✔
475
        case Type::enum_strings:
246✔
476
            static_cast<Array*>(m_arr)->verify();
246✔
477
            break;
246✔
478
    }
163,578✔
479
#endif
163,578✔
480
}
163,578✔
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