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

realm / realm-core / 1662

12 Sep 2023 09:48AM UTC coverage: 91.217% (-0.05%) from 91.265%
1662

push

Evergreen

GitHub
Resending pending subscriptions can result in inconsistent sync progress (#6965)

95838 of 175790 branches covered (0.0%)

87 of 94 new or added lines in 2 files covered. (92.55%)

165 existing lines in 16 files now uncovered.

233563 of 256053 relevant lines covered (91.22%)

6854188.7 hits per line

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

87.78
/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)
27
{
20,314,044✔
28
    m_arr = new (&m_storage.m_string_short) ArrayStringShort(a, true);
20,314,044✔
29
}
20,314,044✔
30

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

36
void ArrayString::init_from_mem(MemRef mem) noexcept
37
{
18,303,855✔
38
    char* header = mem.get_addr();
18,303,855✔
39

9,137,829✔
40
    ArrayParent* parent = m_arr->get_parent();
18,303,855✔
41
    size_t ndx_in_parent = m_arr->get_ndx_in_parent();
18,303,855✔
42

9,137,829✔
43
    bool long_strings = Array::get_hasrefs_from_header(header);
18,303,855✔
44
    if (!long_strings) {
18,303,855✔
45
        // Small strings
6,780,153✔
46
        bool is_small = Array::get_wtype_from_header(header) == Array::wtype_Multiply;
13,586,934✔
47
        if (is_small) {
13,586,934✔
48
            auto arr = new (&m_storage.m_string_short) ArrayStringShort(m_alloc, m_nullable);
7,902,627✔
49
            arr->init_from_mem(mem);
7,902,627✔
50
            m_type = Type::small_strings;
7,902,627✔
51
        }
7,902,627✔
52
        else {
5,684,307✔
53
            auto arr = new (&m_storage.m_enum) Array(m_alloc);
5,684,307✔
54
            arr->init_from_mem(mem);
5,684,307✔
55
            m_string_enum_values = std::make_unique<ArrayString>(m_alloc);
5,684,307✔
56
            ArrayParent* p;
5,684,307✔
57
            REALM_ASSERT(m_spec != nullptr);
5,684,307✔
58
            REALM_ASSERT(m_col_ndx != realm::npos);
5,684,307✔
59
            ref_type r = m_spec->get_enumkeys_ref(m_col_ndx, p);
5,684,307✔
60
            m_string_enum_values->init_from_ref(r);
5,684,307✔
61
            m_string_enum_values->set_parent(p, m_col_ndx);
5,684,307✔
62
            m_type = Type::enum_strings;
5,684,307✔
63
        }
5,684,307✔
64
    }
13,586,934✔
65
    else {
4,716,921✔
66
        bool is_big = Array::get_context_flag_from_header(header);
4,716,921✔
67
        if (!is_big) {
4,716,921✔
68
            auto arr = new (&m_storage.m_string_long) ArraySmallBlobs(m_alloc);
1,241,769✔
69
            arr->init_from_mem(mem);
1,241,769✔
70
            m_type = Type::medium_strings;
1,241,769✔
71
        }
1,241,769✔
72
        else {
3,475,152✔
73
            auto arr = new (&m_storage.m_big_blobs) ArrayBigBlobs(m_alloc, m_nullable);
3,475,152✔
74
            arr->init_from_mem(mem);
3,475,152✔
75
            m_type = Type::big_strings;
3,475,152✔
76
        }
3,475,152✔
77
    }
4,716,921✔
78
    m_arr->set_parent(parent, ndx_in_parent);
18,303,855✔
79
}
18,303,855✔
80

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

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

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

104
size_t ArrayString::size() const
105
{
15,936,336✔
106
    switch (m_type) {
15,936,336✔
107
        case Type::small_strings:
6,918,609✔
108
            return static_cast<ArrayStringShort*>(m_arr)->size();
6,918,609✔
109
        case Type::medium_strings:
435,723✔
110
            return static_cast<ArraySmallBlobs*>(m_arr)->size();
435,723✔
111
        case Type::big_strings:
8,582,622✔
112
            return static_cast<ArrayBigBlobs*>(m_arr)->size();
8,582,622✔
113
        case Type::enum_strings:
282✔
114
            return static_cast<Array*>(m_arr)->size();
282✔
115
    }
×
116
    return {};
×
117
}
×
118

119
void ArrayString::add(StringData value)
120
{
162,474✔
121
    switch (upgrade_leaf(value.size())) {
162,474✔
122
        case Type::small_strings:
107,184✔
123
            static_cast<ArrayStringShort*>(m_arr)->add(value);
107,184✔
124
            break;
107,184✔
125
        case Type::medium_strings:
27,927✔
126
            static_cast<ArraySmallBlobs*>(m_arr)->add_string(value);
27,927✔
127
            break;
27,927✔
128
        case Type::big_strings:
27,363✔
129
            static_cast<ArrayBigBlobs*>(m_arr)->add_string(value);
27,363✔
130
            break;
27,363✔
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
    }
162,474✔
139
}
162,474✔
140

141
void ArrayString::set(size_t ndx, StringData value)
142
{
3,829,128✔
143
    switch (upgrade_leaf(value.size())) {
3,829,128✔
144
        case Type::small_strings:
528,501✔
145
            static_cast<ArrayStringShort*>(m_arr)->set(ndx, value);
528,501✔
146
            break;
528,501✔
147
        case Type::medium_strings:
269,586✔
148
            static_cast<ArraySmallBlobs*>(m_arr)->set_string(ndx, value);
269,586✔
149
            break;
269,586✔
150
        case Type::big_strings:
408,600✔
151
            static_cast<ArrayBigBlobs*>(m_arr)->set_string(ndx, value);
408,600✔
152
            break;
408,600✔
153
        case Type::enum_strings: {
2,625,603✔
154
            size_t sz = m_string_enum_values->size();
2,625,603✔
155
            size_t res = m_string_enum_values->find_first(value, 0, sz);
2,625,603✔
156
            if (res == realm::not_found) {
2,625,603✔
157
                m_string_enum_values->add(value);
9,651✔
158
                res = sz;
9,651✔
159
            }
9,651✔
160
            static_cast<Array*>(m_arr)->set(ndx, res);
2,625,603✔
161
            break;
2,625,603✔
162
        }
×
163
    }
3,829,128✔
164
}
3,829,128✔
165

166
void ArrayString::insert(size_t ndx, StringData value)
167
{
3,373,698✔
168
    switch (upgrade_leaf(value.size())) {
3,373,698✔
169
        case Type::small_strings:
1,036,278✔
170
            static_cast<ArrayStringShort*>(m_arr)->insert(ndx, value);
1,036,278✔
171
            break;
1,036,278✔
172
        case Type::medium_strings:
287,187✔
173
            static_cast<ArraySmallBlobs*>(m_arr)->insert_string(ndx, value);
287,187✔
174
            break;
287,187✔
175
        case Type::big_strings:
738,204✔
176
            static_cast<ArrayBigBlobs*>(m_arr)->insert_string(ndx, value);
738,204✔
177
            break;
738,204✔
178
        case Type::enum_strings: {
1,312,803✔
179
            static_cast<Array*>(m_arr)->insert(ndx, 0);
1,312,803✔
180
            set(ndx, value);
1,312,803✔
181
        }
1,312,803✔
182
    }
3,373,698✔
183
}
3,373,698✔
184

185
StringData ArrayString::get(size_t ndx) const
186
{
22,834,824✔
187
    switch (m_type) {
22,834,824✔
188
        case Type::small_strings:
9,207,171✔
189
            return static_cast<ArrayStringShort*>(m_arr)->get(ndx);
9,207,171✔
190
        case Type::medium_strings:
1,362,315✔
191
            return static_cast<ArraySmallBlobs*>(m_arr)->get_string(ndx);
1,362,315✔
192
        case Type::big_strings:
9,217,164✔
193
            return static_cast<ArrayBigBlobs*>(m_arr)->get_string(ndx);
9,217,164✔
194
        case Type::enum_strings: {
3,084,519✔
195
            size_t index = size_t(static_cast<Array*>(m_arr)->get(ndx));
3,084,519✔
196
            return m_string_enum_values->get(index);
3,084,519✔
197
        }
×
198
    }
×
199
    return {};
×
200
}
×
201

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

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

224
bool ArrayString::is_null(size_t ndx) const
225
{
4,012,671✔
226
    switch (m_type) {
4,012,671✔
227
        case Type::small_strings:
584,157✔
228
            return static_cast<ArrayStringShort*>(m_arr)->is_null(ndx);
584,157✔
229
        case Type::medium_strings:
14,778✔
230
            return static_cast<ArraySmallBlobs*>(m_arr)->is_null(ndx);
14,778✔
231
        case Type::big_strings:
3,413,745✔
232
            return static_cast<ArrayBigBlobs*>(m_arr)->is_null(ndx);
3,413,745✔
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
    }
×
238
    return {};
×
239
}
×
240

241
void ArrayString::erase(size_t ndx)
242
{
183,858✔
243
    switch (m_type) {
183,858✔
244
        case Type::small_strings:
92,832✔
245
            static_cast<ArrayStringShort*>(m_arr)->erase(ndx);
92,832✔
246
            break;
92,832✔
247
        case Type::medium_strings:
59,142✔
248
            static_cast<ArraySmallBlobs*>(m_arr)->erase(ndx);
59,142✔
249
            break;
59,142✔
250
        case Type::big_strings:
23,511✔
251
            static_cast<ArrayBigBlobs*>(m_arr)->erase(ndx);
23,511✔
252
            break;
23,511✔
253
        case Type::enum_strings:
8,376✔
254
            static_cast<Array*>(m_arr)->erase(ndx);
8,376✔
255
            break;
8,376✔
256
    }
183,858✔
257
}
183,858✔
258

259
void ArrayString::move(ArrayString& dst, size_t ndx)
260
{
66✔
261
    size_t sz = size();
66✔
262
    for (size_t i = ndx; i < sz; i++) {
20,730✔
263
        dst.add(get(i));
20,664✔
264
    }
20,664✔
265

33✔
266
    switch (m_type) {
66✔
267
        case Type::small_strings:
✔
268
            static_cast<ArrayStringShort*>(m_arr)->truncate(ndx);
×
269
            break;
×
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
    }
66✔
281
}
66✔
282

283
void ArrayString::clear()
284
{
441✔
285
    switch (m_type) {
441✔
286
        case Type::small_strings:
387✔
287
            static_cast<ArrayStringShort*>(m_arr)->clear();
387✔
288
            break;
387✔
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
    }
441✔
299
}
441✔
300

301
size_t ArrayString::find_first(StringData value, size_t begin, size_t end) const noexcept
302
{
4,173,537✔
303
    switch (m_type) {
4,173,537✔
304
        case Type::small_strings:
3,168,183✔
305
            return static_cast<ArrayStringShort*>(m_arr)->find_first(value, begin, end);
3,168,183✔
306
        case Type::medium_strings: {
153,825✔
307
            BinaryData as_binary(value.data(), value.size());
153,825✔
308
            return static_cast<ArraySmallBlobs*>(m_arr)->find_first(as_binary, true, begin, end);
153,825✔
309
            break;
×
310
        }
×
311
        case Type::big_strings: {
255,930✔
312
            BinaryData as_binary(value.data(), value.size());
255,930✔
313
            return static_cast<ArrayBigBlobs*>(m_arr)->find_first(as_binary, true, begin, end);
255,930✔
314
            break;
×
315
        }
×
316
        case Type::enum_strings: {
595,806✔
317
            size_t sz = m_string_enum_values->size();
595,806✔
318
            size_t res = m_string_enum_values->find_first(value, 0, sz);
595,806✔
319
            if (res != realm::not_found) {
595,806✔
320
                return static_cast<Array*>(m_arr)->find_first(res, begin, end);
595,806✔
321
            }
595,806✔
UNCOV
322
            break;
×
UNCOV
323
        }
×
324
    }
×
325
    return not_found;
×
326
}
×
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,669✔
339
    return arr->get(ndx);
93,669✔
340
}
93,669✔
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,918✔
348
        size_t half = sz / 2;
531,006✔
349
        size_t mid = i + half;
531,006✔
350
        auto probe = get_string(arr, mid);
531,006✔
351
        if (probe < value) {
531,006✔
352
            i = mid + 1;
190,878✔
353
            sz -= half + 1;
190,878✔
354
        }
190,878✔
355
        else {
340,128✔
356
            sz = half;
340,128✔
357
        }
340,128✔
358
    }
531,006✔
359
    return i;
234,912✔
360
}
234,912✔
361
}
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
    }
×
375
    return realm::npos;
×
376
}
×
377

378
ArrayString::Type ArrayString::upgrade_leaf(size_t value_size)
379
{
7,364,910✔
380
    if (m_type == Type::big_strings)
7,364,910✔
381
        return Type::big_strings;
1,136,565✔
382

3,050,211✔
383
    if (m_type == Type::enum_strings)
6,228,345✔
384
        return Type::enum_strings;
3,938,274✔
385

1,081,107✔
386
    if (m_type == Type::medium_strings) {
2,290,071✔
387
        if (value_size <= medium_string_max_size)
533,844✔
388
            return Type::medium_strings;
533,328✔
389

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

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

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

246✔
408
        m_type = Type::big_strings;
516✔
409
        return Type::big_strings;
516✔
410
    }
516✔
411

814,032✔
412
    // m_type == Type::small
814,032✔
413
    if (value_size <= small_string_max_size)
1,756,227✔
414
        return Type::small_strings;
1,671,825✔
415

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

25,206✔
422
        size_t n = string_short->size();
51,366✔
423
        for (size_t i = 0; i < n; i++) {
107,670✔
424
            string_long.add_string(string_short->get(i)); // Throws
56,304✔
425
        }
56,304✔
426
        auto parent = string_short->get_parent();
51,366✔
427
        auto ndx_in_parent = string_short->get_ndx_in_parent();
51,366✔
428
        string_short->destroy();
51,366✔
429

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

25,206✔
435
        m_type = Type::medium_strings;
51,366✔
436
    }
51,366✔
437
    else {
33,036✔
438
        // Upgrade root leaf from small to big strings
15,174✔
439
        auto string_short = static_cast<ArrayStringShort*>(m_arr);
33,036✔
440
        ArrayBigBlobs big_blobs(m_alloc, true);
33,036✔
441
        big_blobs.create(); // Throws
33,036✔
442

15,174✔
443
        size_t n = string_short->size();
33,036✔
444
        for (size_t i = 0; i < n; i++) {
70,518✔
445
            big_blobs.add_string(string_short->get(i)); // Throws
37,482✔
446
        }
37,482✔
447
        auto parent = string_short->get_parent();
33,036✔
448
        auto ndx_in_parent = string_short->get_ndx_in_parent();
33,036✔
449
        string_short->destroy();
33,036✔
450

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

15,174✔
456
        m_type = Type::big_strings;
33,036✔
457
    }
33,036✔
458

40,380✔
459
    return m_type;
84,402✔
460
}
84,402✔
461

462
void ArrayString::verify() const
463
{
3,125,496✔
464
#ifdef REALM_DEBUG
3,125,496✔
465
    switch (m_type) {
3,125,496✔
466
        case Type::small_strings:
3,118,959✔
467
            static_cast<ArrayStringShort*>(m_arr)->verify();
3,118,959✔
468
            break;
3,118,959✔
469
        case Type::medium_strings:
831✔
470
            static_cast<ArraySmallBlobs*>(m_arr)->verify();
831✔
471
            break;
831✔
472
        case Type::big_strings:
5,460✔
473
            static_cast<ArrayBigBlobs*>(m_arr)->verify();
5,460✔
474
            break;
5,460✔
475
        case Type::enum_strings:
246✔
476
            static_cast<Array*>(m_arr)->verify();
246✔
477
            break;
246✔
478
    }
3,125,496✔
479
#endif
3,125,496✔
480
}
3,125,496✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc