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

realm / realm-core / 1863

24 Nov 2023 10:28AM UTC coverage: 91.688% (-0.02%) from 91.708%
1863

push

Evergreen

web-flow
Add missing install headers (#7165)

* add missing install headers

* validate installed headers on PR triggers

92392 of 169288 branches covered (0.0%)

231720 of 252727 relevant lines covered (91.69%)

6390825.2 hits per line

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

88.0
/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,302,170✔
28
    m_arr = new (&m_storage.m_string_short) ArrayStringShort(a, true);
20,302,170✔
29
}
20,302,170✔
30

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

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

9,099,408✔
40
    ArrayParent* parent = m_arr->get_parent();
18,191,451✔
41
    size_t ndx_in_parent = m_arr->get_ndx_in_parent();
18,191,451✔
42

9,099,408✔
43
    bool long_strings = Array::get_hasrefs_from_header(header);
18,191,451✔
44
    if (!long_strings) {
18,191,451✔
45
        // Small strings
6,796,014✔
46
        bool is_small = Array::get_wtype_from_header(header) == Array::wtype_Multiply;
13,546,188✔
47
        if (is_small) {
13,546,188✔
48
            auto arr = new (&m_storage.m_string_short) ArrayStringShort(m_alloc, m_nullable);
7,897,428✔
49
            arr->init_from_mem(mem);
7,897,428✔
50
            m_type = Type::small_strings;
7,897,428✔
51
        }
7,897,428✔
52
        else {
5,648,760✔
53
            auto arr = new (&m_storage.m_enum) Array(m_alloc);
5,648,760✔
54
            arr->init_from_mem(mem);
5,648,760✔
55
            m_string_enum_values = std::make_unique<ArrayString>(m_alloc);
5,648,760✔
56
            ArrayParent* p;
5,648,760✔
57
            REALM_ASSERT(m_spec != nullptr);
5,648,760✔
58
            REALM_ASSERT(m_col_ndx != realm::npos);
5,648,760✔
59
            ref_type r = m_spec->get_enumkeys_ref(m_col_ndx, p);
5,648,760✔
60
            m_string_enum_values->init_from_ref(r);
5,648,760✔
61
            m_string_enum_values->set_parent(p, m_col_ndx);
5,648,760✔
62
            m_type = Type::enum_strings;
5,648,760✔
63
        }
5,648,760✔
64
    }
13,546,188✔
65
    else {
4,645,263✔
66
        bool is_big = Array::get_context_flag_from_header(header);
4,645,263✔
67
        if (!is_big) {
4,645,263✔
68
            auto arr = new (&m_storage.m_string_long) ArraySmallBlobs(m_alloc);
1,205,724✔
69
            arr->init_from_mem(mem);
1,205,724✔
70
            m_type = Type::medium_strings;
1,205,724✔
71
        }
1,205,724✔
72
        else {
3,439,539✔
73
            auto arr = new (&m_storage.m_big_blobs) ArrayBigBlobs(m_alloc, m_nullable);
3,439,539✔
74
            arr->init_from_mem(mem);
3,439,539✔
75
            m_type = Type::big_strings;
3,439,539✔
76
        }
3,439,539✔
77
    }
4,645,263✔
78
    m_arr->set_parent(parent, ndx_in_parent);
18,191,451✔
79
}
18,191,451✔
80

81
void ArrayString::init_from_parent()
82
{
5,376,063✔
83
    ref_type ref = m_arr->get_ref_from_parent();
5,376,063✔
84
    init_from_ref(ref);
5,376,063✔
85
}
5,376,063✔
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
{
290,415✔
97
    m_arr->detach();
290,415✔
98
    // Make sure the object is in a state like right after construction
145,005✔
99
    // Next call must be to create()
145,005✔
100
    m_arr = new (&m_storage.m_string_short) ArrayStringShort(m_alloc, true);
290,415✔
101
    m_type = Type::small_strings;
290,415✔
102
}
290,415✔
103

104
size_t ArrayString::size() const
105
{
15,631,890✔
106
    switch (m_type) {
15,631,890✔
107
        case Type::small_strings:
6,866,661✔
108
            return static_cast<ArrayStringShort*>(m_arr)->size();
6,866,661✔
109
        case Type::medium_strings:
462,585✔
110
            return static_cast<ArraySmallBlobs*>(m_arr)->size();
462,585✔
111
        case Type::big_strings:
8,311,197✔
112
            return static_cast<ArrayBigBlobs*>(m_arr)->size();
8,311,197✔
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
{
159,885✔
121
    switch (upgrade_leaf(value.size())) {
159,885✔
122
        case Type::small_strings:
104,853✔
123
            static_cast<ArrayStringShort*>(m_arr)->add(value);
104,853✔
124
            break;
104,853✔
125
        case Type::medium_strings:
28,407✔
126
            static_cast<ArraySmallBlobs*>(m_arr)->add_string(value);
28,407✔
127
            break;
28,407✔
128
        case Type::big_strings:
26,625✔
129
            static_cast<ArrayBigBlobs*>(m_arr)->add_string(value);
26,625✔
130
            break;
26,625✔
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
    }
159,885✔
139
}
159,885✔
140

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

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

185
StringData ArrayString::get(size_t ndx) const
186
{
22,567,944✔
187
    switch (m_type) {
22,567,944✔
188
        case Type::small_strings:
9,214,089✔
189
            return static_cast<ArrayStringShort*>(m_arr)->get(ndx);
9,214,089✔
190
        case Type::medium_strings:
1,404,099✔
191
            return static_cast<ArraySmallBlobs*>(m_arr)->get_string(ndx);
1,404,099✔
192
        case Type::big_strings:
8,965,662✔
193
            return static_cast<ArrayBigBlobs*>(m_arr)->get_string(ndx);
8,965,662✔
194
        case Type::enum_strings: {
3,081,669✔
195
            size_t index = size_t(static_cast<Array*>(m_arr)->get(ndx));
3,081,669✔
196
            return m_string_enum_values->get(index);
3,081,669✔
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
{
3,934,455✔
226
    switch (m_type) {
3,934,455✔
227
        case Type::small_strings:
708,804✔
228
            return static_cast<ArrayStringShort*>(m_arr)->is_null(ndx);
708,804✔
229
        case Type::medium_strings:
28,368✔
230
            return static_cast<ArraySmallBlobs*>(m_arr)->is_null(ndx);
28,368✔
231
        case Type::big_strings:
3,201,615✔
232
            return static_cast<ArrayBigBlobs*>(m_arr)->is_null(ndx);
3,201,615✔
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
{
185,559✔
243
    switch (m_type) {
185,559✔
244
        case Type::small_strings:
91,581✔
245
            static_cast<ArrayStringShort*>(m_arr)->erase(ndx);
91,581✔
246
            break;
91,581✔
247
        case Type::medium_strings:
59,616✔
248
            static_cast<ArraySmallBlobs*>(m_arr)->erase(ndx);
59,616✔
249
            break;
59,616✔
250
        case Type::big_strings:
26,010✔
251
            static_cast<ArrayBigBlobs*>(m_arr)->erase(ndx);
26,010✔
252
            break;
26,010✔
253
        case Type::enum_strings:
8,364✔
254
            static_cast<Array*>(m_arr)->erase(ndx);
8,364✔
255
            break;
8,364✔
256
    }
185,559✔
257
}
185,559✔
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
{
429✔
285
    switch (m_type) {
429✔
286
        case Type::small_strings:
375✔
287
            static_cast<ArrayStringShort*>(m_arr)->clear();
375✔
288
            break;
375✔
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
    }
429✔
299
}
429✔
300

301
size_t ArrayString::find_first(StringData value, size_t begin, size_t end) const noexcept
302
{
4,077,915✔
303
    switch (m_type) {
4,077,915✔
304
        case Type::small_strings:
3,123,333✔
305
            return static_cast<ArrayStringShort*>(m_arr)->find_first(value, begin, end);
3,123,333✔
306
        case Type::medium_strings: {
104,928✔
307
            BinaryData as_binary(value.data(), value.size());
104,928✔
308
            return static_cast<ArraySmallBlobs*>(m_arr)->find_first(as_binary, true, begin, end);
104,928✔
309
            break;
×
310
        }
×
311
        case Type::big_strings: {
256,296✔
312
            BinaryData as_binary(value.data(), value.size());
256,296✔
313
            return static_cast<ArrayBigBlobs*>(m_arr)->find_first(as_binary, true, begin, end);
256,296✔
314
            break;
×
315
        }
×
316
        case Type::enum_strings: {
594,243✔
317
            size_t sz = m_string_enum_values->size();
594,243✔
318
            size_t res = m_string_enum_values->find_first(value, 0, sz);
594,243✔
319
            if (res != realm::not_found) {
594,243✔
320
                return static_cast<Array*>(m_arr)->find_first(res, begin, end);
594,243✔
321
            }
594,243✔
322
            break;
×
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,328✔
333
    return arr->get_string(ndx);
437,328✔
334
}
437,328✔
335

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

342
template <class T, class U>
343
size_t lower_bound_string(const T* arr, U value)
344
{
235,392✔
345
    size_t i = 0;
235,392✔
346
    size_t sz = arr->size();
235,392✔
347
    while (0 < sz) {
763,899✔
348
        size_t half = sz / 2;
528,507✔
349
        size_t mid = i + half;
528,507✔
350
        auto probe = get_string(arr, mid);
528,507✔
351
        if (probe < value) {
528,507✔
352
            i = mid + 1;
190,545✔
353
            sz -= half + 1;
190,545✔
354
        }
190,545✔
355
        else {
337,962✔
356
            sz = half;
337,962✔
357
        }
337,962✔
358
    }
528,507✔
359
    return i;
235,392✔
360
}
235,392✔
361
}
362

363
size_t ArrayString::lower_bound(StringData value)
364
{
235,392✔
365
    switch (m_type) {
235,392✔
366
        case Type::small_strings:
58,914✔
367
            return lower_bound_string(static_cast<ArrayStringShort*>(m_arr), value);
58,914✔
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,366,347✔
380
    if (m_type == Type::big_strings)
7,366,347✔
381
        return Type::big_strings;
1,149,603✔
382

3,054,450✔
383
    if (m_type == Type::enum_strings)
6,216,744✔
384
        return Type::enum_strings;
3,937,233✔
385

1,086,324✔
386
    if (m_type == Type::medium_strings) {
2,279,511✔
387
        if (value_size <= medium_string_max_size)
533,577✔
388
            return Type::medium_strings;
533,028✔
389

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

249✔
395
        size_t n = string_medium->size();
549✔
396
        for (size_t i = 0; i < n; i++) {
11,775✔
397
            big_blobs.add_string(string_medium->get_string(i)); // Throws
11,226✔
398
        }
11,226✔
399
        auto parent = string_medium->get_parent();
549✔
400
        auto ndx_in_parent = string_medium->get_ndx_in_parent();
549✔
401
        string_medium->destroy();
549✔
402

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

249✔
408
        m_type = Type::big_strings;
549✔
409
        return Type::big_strings;
549✔
410
    }
549✔
411

819,552✔
412
    // m_type == Type::small
819,552✔
413
    if (value_size <= small_string_max_size)
1,745,934✔
414
        return Type::small_strings;
1,683,786✔
415

29,073✔
416
    if (value_size <= medium_string_max_size) {
62,148✔
417
        // Upgrade root leaf from small to medium strings
14,994✔
418
        auto string_short = static_cast<ArrayStringShort*>(m_arr);
30,600✔
419
        ArraySmallBlobs string_long(m_alloc);
30,600✔
420
        string_long.create(); // Throws
30,600✔
421

14,994✔
422
        size_t n = string_short->size();
30,600✔
423
        for (size_t i = 0; i < n; i++) {
65,976✔
424
            string_long.add_string(string_short->get(i)); // Throws
35,376✔
425
        }
35,376✔
426
        auto parent = string_short->get_parent();
30,600✔
427
        auto ndx_in_parent = string_short->get_ndx_in_parent();
30,600✔
428
        string_short->destroy();
30,600✔
429

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

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

14,079✔
443
        size_t n = string_short->size();
31,548✔
444
        for (size_t i = 0; i < n; i++) {
73,539✔
445
            big_blobs.add_string(string_short->get(i)); // Throws
41,991✔
446
        }
41,991✔
447
        auto parent = string_short->get_parent();
31,548✔
448
        auto ndx_in_parent = string_short->get_ndx_in_parent();
31,548✔
449
        string_short->destroy();
31,548✔
450

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

14,079✔
456
        m_type = Type::big_strings;
31,548✔
457
    }
31,548✔
458

29,073✔
459
    return m_type;
62,148✔
460
}
62,148✔
461

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