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

realm / realm-core / 2073

27 Feb 2024 02:41PM UTC coverage: 90.908% (-0.04%) from 90.948%
2073

push

Evergreen

jedelbo
Prevent opening files with file format 23 in read-only mode

93924 of 173104 branches covered (54.26%)

1 of 1 new or added line in 1 file covered. (100.0%)

169 existing lines in 18 files now uncovered.

238327 of 262162 relevant lines covered (90.91%)

5695963.88 hits per line

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

85.25
/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
{
12,634,896✔
28
    m_arr = new (&m_storage) ArrayStringShort(a, true);
12,634,896✔
29
}
12,634,896✔
30

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

36
void ArrayString::init_from_mem(MemRef mem) noexcept
37
{
11,251,869✔
38
    char* header = mem.get_addr();
11,251,869✔
39

5,534,436✔
40
    ArrayParent* parent = m_arr->get_parent();
11,251,869✔
41
    size_t ndx_in_parent = m_arr->get_ndx_in_parent();
11,251,869✔
42

5,534,436✔
43
    bool long_strings = Array::get_hasrefs_from_header(header);
11,251,869✔
44
    if (!long_strings) {
11,251,869✔
45
        // Small strings
4,165,812✔
46
        bool is_small = Array::get_wtype_from_header(header) == Array::wtype_Multiply;
8,510,856✔
47
        if (is_small) {
8,510,856✔
48
            auto arr = new (&m_storage) ArrayStringShort(m_alloc, m_nullable);
4,812,702✔
49
            arr->init_from_mem(mem);
4,812,702✔
50
            m_type = Type::small_strings;
4,812,702✔
51
        }
4,812,702✔
52
        else {
3,698,154✔
53
            auto arr = new (&m_storage) Array(m_alloc);
3,698,154✔
54
            arr->init_from_mem(mem);
3,698,154✔
55
            m_string_enum_values = std::make_unique<ArrayString>(m_alloc);
3,698,154✔
56
            ArrayParent* p;
3,698,154✔
57
            REALM_ASSERT(m_spec != nullptr);
3,698,154✔
58
            REALM_ASSERT(m_col_ndx != realm::npos);
3,698,154✔
59
            ref_type r = m_spec->get_enumkeys_ref(m_col_ndx, p);
3,698,154✔
60
            m_string_enum_values->init_from_ref(r);
3,698,154✔
61
            m_string_enum_values->set_parent(p, m_col_ndx);
3,698,154✔
62
            m_type = Type::enum_strings;
3,698,154✔
63
        }
3,698,154✔
64
    }
8,510,856✔
65
    else {
2,741,013✔
66
        bool is_big = Array::get_context_flag_from_header(header);
2,741,013✔
67
        if (!is_big) {
2,741,013✔
68
            auto arr = new (&m_storage) ArraySmallBlobs(m_alloc);
671,532✔
69
            arr->init_from_mem(mem);
671,532✔
70
            m_type = Type::medium_strings;
671,532✔
71
        }
671,532✔
72
        else {
2,069,481✔
73
            auto arr = new (&m_storage) ArrayBigBlobs(m_alloc, m_nullable);
2,069,481✔
74
            arr->init_from_mem(mem);
2,069,481✔
75
            m_type = Type::big_strings;
2,069,481✔
76
        }
2,069,481✔
77
    }
2,741,013✔
78
    m_arr->set_parent(parent, ndx_in_parent);
11,251,869✔
79
}
11,251,869✔
80

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

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

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

104
size_t ArrayString::size() const
105
{
13,555,257✔
106
    switch (m_type) {
13,555,257✔
107
        case Type::small_strings:
4,692,048✔
108
            return static_cast<ArrayStringShort*>(m_arr)->size();
4,692,048✔
109
        case Type::medium_strings:
442,101✔
110
            return static_cast<ArraySmallBlobs*>(m_arr)->size();
442,101✔
111
        case Type::big_strings:
8,460,075✔
112
            return static_cast<ArrayBigBlobs*>(m_arr)->size();
8,460,075✔
113
        case Type::enum_strings:
246✔
114
            return static_cast<Array*>(m_arr)->size();
246✔
115
    }
×
116
    return {};
×
117
}
×
118

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

141
void ArrayString::set(size_t ndx, StringData value)
142
{
3,739,278✔
143
    switch (upgrade_leaf(value.size())) {
3,739,278✔
144
        case Type::small_strings:
503,409✔
145
            static_cast<ArrayStringShort*>(m_arr)->set(ndx, value);
503,409✔
146
            break;
503,409✔
147
        case Type::medium_strings:
234,006✔
148
            static_cast<ArraySmallBlobs*>(m_arr)->set_string(ndx, value);
234,006✔
149
            break;
234,006✔
150
        case Type::big_strings:
377,475✔
151
            static_cast<ArrayBigBlobs*>(m_arr)->set_string(ndx, value);
377,475✔
152
            break;
377,475✔
153
        case Type::enum_strings: {
2,625,471✔
154
            size_t sz = m_string_enum_values->size();
2,625,471✔
155
            size_t res = m_string_enum_values->find_first(value, 0, sz);
2,625,471✔
156
            if (res == realm::not_found) {
2,625,471✔
157
                m_string_enum_values->add(value);
9,666✔
158
                res = sz;
9,666✔
159
            }
9,666✔
160
            static_cast<Array*>(m_arr)->set(ndx, res);
2,625,471✔
161
            break;
2,625,471✔
162
        }
×
163
    }
3,739,278✔
164
}
3,739,278✔
165

166
void ArrayString::insert(size_t ndx, StringData value)
167
{
3,434,649✔
168
    switch (upgrade_leaf(value.size())) {
3,434,649✔
169
        case Type::small_strings:
1,034,781✔
170
            static_cast<ArrayStringShort*>(m_arr)->insert(ndx, value);
1,034,781✔
171
            break;
1,034,781✔
172
        case Type::medium_strings:
282,672✔
173
            static_cast<ArraySmallBlobs*>(m_arr)->insert_string(ndx, value);
282,672✔
174
            break;
282,672✔
175
        case Type::big_strings:
805,824✔
176
            static_cast<ArrayBigBlobs*>(m_arr)->insert_string(ndx, value);
805,824✔
177
            break;
805,824✔
178
        case Type::enum_strings: {
1,312,701✔
179
            static_cast<Array*>(m_arr)->insert(ndx, 0);
1,312,701✔
180
            set(ndx, value);
1,312,701✔
181
        }
1,312,701✔
182
    }
3,434,649✔
183
}
3,434,649✔
184

185
StringData ArrayString::get(size_t ndx) const
186
{
19,060,377✔
187
    switch (m_type) {
19,060,377✔
188
        case Type::small_strings:
9,557,580✔
189
            return static_cast<ArrayStringShort*>(m_arr)->get(ndx);
9,557,580✔
190
        case Type::medium_strings:
912,738✔
191
            return static_cast<ArraySmallBlobs*>(m_arr)->get_string(ndx);
912,738✔
192
        case Type::big_strings:
7,468,398✔
193
            return static_cast<ArrayBigBlobs*>(m_arr)->get_string(ndx);
7,468,398✔
194
        case Type::enum_strings: {
1,138,218✔
195
            size_t index = size_t(static_cast<Array*>(m_arr)->get(ndx));
1,138,218✔
196
            return m_string_enum_values->get(index);
1,138,218✔
197
        }
×
198
    }
×
199
    return {};
×
200
}
×
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
{
133,008✔
221
    return Mixed(get(ndx));
133,008✔
222
}
133,008✔
223

224
bool ArrayString::is_null(size_t ndx) const
225
{
3,915,435✔
226
    switch (m_type) {
3,915,435✔
227
        case Type::small_strings:
655,791✔
228
            return static_cast<ArrayStringShort*>(m_arr)->is_null(ndx);
655,791✔
229
        case Type::medium_strings:
19,377✔
230
            return static_cast<ArraySmallBlobs*>(m_arr)->is_null(ndx);
19,377✔
231
        case Type::big_strings:
3,242,064✔
232
            return static_cast<ArrayBigBlobs*>(m_arr)->is_null(ndx);
3,242,064✔
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
{
129,180✔
243
    switch (m_type) {
129,180✔
244
        case Type::small_strings:
36,648✔
245
            static_cast<ArrayStringShort*>(m_arr)->erase(ndx);
36,648✔
246
            break;
36,648✔
247
        case Type::medium_strings:
57,759✔
248
            static_cast<ArraySmallBlobs*>(m_arr)->erase(ndx);
57,759✔
249
            break;
57,759✔
250
        case Type::big_strings:
26,439✔
251
            static_cast<ArrayBigBlobs*>(m_arr)->erase(ndx);
26,439✔
252
            break;
26,439✔
253
        case Type::enum_strings:
8,334✔
254
            static_cast<Array*>(m_arr)->erase(ndx);
8,334✔
255
            break;
8,334✔
256
    }
129,180✔
257
}
129,180✔
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
{
354✔
285
    switch (m_type) {
354✔
286
        case Type::small_strings:
300✔
287
            static_cast<ArrayStringShort*>(m_arr)->clear();
300✔
288
            break;
300✔
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
    }
354✔
299
}
354✔
300

301
size_t ArrayString::find_first(StringData value, size_t begin, size_t end) const noexcept
302
{
4,070,133✔
303
    switch (m_type) {
4,070,133✔
304
        case Type::small_strings:
3,114,954✔
305
            return static_cast<ArrayStringShort*>(m_arr)->find_first(value, begin, end);
3,114,954✔
306
        case Type::medium_strings: {
105,018✔
307
            BinaryData as_binary(value.data(), value.size());
105,018✔
308
            return static_cast<ArraySmallBlobs*>(m_arr)->find_first(as_binary, true, begin, end);
105,018✔
309
            break;
×
310
        }
×
311
        case Type::big_strings: {
255,024✔
312
            BinaryData as_binary(value.data(), value.size());
255,024✔
313
            return static_cast<ArrayBigBlobs*>(m_arr)->find_first(as_binary, true, begin, end);
255,024✔
314
            break;
×
315
        }
×
316
        case Type::enum_strings: {
595,542✔
317
            size_t sz = m_string_enum_values->size();
595,542✔
318
            size_t res = m_string_enum_values->find_first(value, 0, sz);
595,542✔
319
            if (res != realm::not_found) {
595,542✔
320
                return static_cast<Array*>(m_arr)->find_first(res, begin, end);
595,542✔
321
            }
595,542✔
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,642✔
339
    return arr->get(ndx);
93,642✔
340
}
93,642✔
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,891✔
348
        size_t half = sz / 2;
530,979✔
349
        size_t mid = i + half;
530,979✔
350
        auto probe = get_string(arr, mid);
530,979✔
351
        if (probe < value) {
530,979✔
352
            i = mid + 1;
191,067✔
353
            sz -= half + 1;
191,067✔
354
        }
191,067✔
355
        else {
339,912✔
356
            sz = half;
339,912✔
357
        }
339,912✔
358
    }
530,979✔
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,306,146✔
380
    if (m_type == Type::big_strings)
7,306,146✔
381
        return Type::big_strings;
1,191,168✔
382

2,998,146✔
383
    if (m_type == Type::enum_strings)
6,114,978✔
384
        return Type::enum_strings;
3,938,043✔
385

1,029,003✔
386
    if (m_type == Type::medium_strings) {
2,176,935✔
387
        if (value_size <= medium_string_max_size)
527,964✔
388
            return Type::medium_strings;
527,457✔
389

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

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

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

252✔
408
        m_type = Type::big_strings;
507✔
409
        return Type::big_strings;
507✔
410
    }
507✔
411

765,243✔
412
    // m_type == Type::small
765,243✔
413
    if (value_size <= small_string_max_size)
1,648,971✔
414
        return Type::small_strings;
1,617,288✔
415

14,694✔
416
    if (value_size <= medium_string_max_size) {
31,683✔
417
        // Upgrade root leaf from small to medium strings
8,370✔
418
        auto string_short = static_cast<ArrayStringShort*>(m_arr);
17,046✔
419
        ArraySmallBlobs string_long(m_alloc);
17,046✔
420
        string_long.create(); // Throws
17,046✔
421

8,370✔
422
        size_t n = string_short->size();
17,046✔
423
        for (size_t i = 0; i < n; i++) {
38,553✔
424
            string_long.add_string(string_short->get(i)); // Throws
21,507✔
425
        }
21,507✔
426
        auto parent = string_short->get_parent();
17,046✔
427
        auto ndx_in_parent = string_short->get_ndx_in_parent();
17,046✔
428
        string_short->destroy();
17,046✔
429

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

8,370✔
435
        m_type = Type::medium_strings;
17,046✔
436
    }
17,046✔
437
    else {
14,637✔
438
        // Upgrade root leaf from small to big strings
6,324✔
439
        auto string_short = static_cast<ArrayStringShort*>(m_arr);
14,637✔
440
        ArrayBigBlobs big_blobs(m_alloc, true);
14,637✔
441
        big_blobs.create(); // Throws
14,637✔
442

6,324✔
443
        size_t n = string_short->size();
14,637✔
444
        for (size_t i = 0; i < n; i++) {
34,626✔
445
            big_blobs.add_string(string_short->get(i)); // Throws
19,989✔
446
        }
19,989✔
447
        auto parent = string_short->get_parent();
14,637✔
448
        auto ndx_in_parent = string_short->get_ndx_in_parent();
14,637✔
449
        string_short->destroy();
14,637✔
450

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

6,324✔
456
        m_type = Type::big_strings;
14,637✔
457
    }
14,637✔
458

14,694✔
459
    return m_type;
31,683✔
460
}
31,683✔
461

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