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

realm / realm-core / github_pull_request_285284

21 Nov 2023 01:56PM UTC coverage: 91.664% (-0.03%) from 91.689%
github_pull_request_285284

Pull #7123

Evergreen

jedelbo
Merge branch 'master' into jf/mql
Pull Request #7123: PoC: Add MQL translation skeleton

92364 of 169228 branches covered (0.0%)

264 of 308 new or added lines in 5 files covered. (85.71%)

102 existing lines in 21 files now uncovered.

231504 of 252558 relevant lines covered (91.66%)

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

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

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

8,982,852✔
40
    ArrayParent* parent = m_arr->get_parent();
18,286,671✔
41
    size_t ndx_in_parent = m_arr->get_ndx_in_parent();
18,286,671✔
42

8,982,852✔
43
    bool long_strings = Array::get_hasrefs_from_header(header);
18,286,671✔
44
    if (!long_strings) {
18,286,671✔
45
        // Small strings
6,669,000✔
46
        bool is_small = Array::get_wtype_from_header(header) == Array::wtype_Multiply;
13,621,113✔
47
        if (is_small) {
13,621,113✔
48
            auto arr = new (&m_storage.m_string_short) ArrayStringShort(m_alloc, m_nullable);
7,944,966✔
49
            arr->init_from_mem(mem);
7,944,966✔
50
            m_type = Type::small_strings;
7,944,966✔
51
        }
7,944,966✔
52
        else {
5,676,147✔
53
            auto arr = new (&m_storage.m_enum) Array(m_alloc);
5,676,147✔
54
            arr->init_from_mem(mem);
5,676,147✔
55
            m_string_enum_values = std::make_unique<ArrayString>(m_alloc);
5,676,147✔
56
            ArrayParent* p;
5,676,147✔
57
            REALM_ASSERT(m_spec != nullptr);
5,676,147✔
58
            REALM_ASSERT(m_col_ndx != realm::npos);
5,676,147✔
59
            ref_type r = m_spec->get_enumkeys_ref(m_col_ndx, p);
5,676,147✔
60
            m_string_enum_values->init_from_ref(r);
5,676,147✔
61
            m_string_enum_values->set_parent(p, m_col_ndx);
5,676,147✔
62
            m_type = Type::enum_strings;
5,676,147✔
63
        }
5,676,147✔
64
    }
13,621,113✔
65
    else {
4,665,558✔
66
        bool is_big = Array::get_context_flag_from_header(header);
4,665,558✔
67
        if (!is_big) {
4,665,558✔
68
            auto arr = new (&m_storage.m_string_long) ArraySmallBlobs(m_alloc);
1,207,668✔
69
            arr->init_from_mem(mem);
1,207,668✔
70
            m_type = Type::medium_strings;
1,207,668✔
71
        }
1,207,668✔
72
        else {
3,457,890✔
73
            auto arr = new (&m_storage.m_big_blobs) ArrayBigBlobs(m_alloc, m_nullable);
3,457,890✔
74
            arr->init_from_mem(mem);
3,457,890✔
75
            m_type = Type::big_strings;
3,457,890✔
76
        }
3,457,890✔
77
    }
4,665,558✔
78
    m_arr->set_parent(parent, ndx_in_parent);
18,286,671✔
79
}
18,286,671✔
80

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

104
size_t ArrayString::size() const
105
{
15,704,562✔
106
    switch (m_type) {
15,704,562✔
107
        case Type::small_strings:
6,946,254✔
108
            return static_cast<ArrayStringShort*>(m_arr)->size();
6,946,254✔
109
        case Type::medium_strings:
442,632✔
110
            return static_cast<ArraySmallBlobs*>(m_arr)->size();
442,632✔
111
        case Type::big_strings:
8,339,871✔
112
            return static_cast<ArrayBigBlobs*>(m_arr)->size();
8,339,871✔
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
{
161,382✔
121
    switch (upgrade_leaf(value.size())) {
161,382✔
122
        case Type::small_strings:
106,188✔
123
            static_cast<ArrayStringShort*>(m_arr)->add(value);
106,188✔
124
            break;
106,188✔
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:
27,369✔
129
            static_cast<ArrayBigBlobs*>(m_arr)->add_string(value);
27,369✔
130
            break;
27,369✔
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
    }
161,382✔
139
}
161,382✔
140

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

166
void ArrayString::insert(size_t ndx, StringData value)
167
{
3,347,793✔
168
    switch (upgrade_leaf(value.size())) {
3,347,793✔
169
        case Type::small_strings:
1,011,555✔
170
            static_cast<ArrayStringShort*>(m_arr)->insert(ndx, value);
1,011,555✔
171
            break;
1,011,555✔
172
        case Type::medium_strings:
286,248✔
173
            static_cast<ArraySmallBlobs*>(m_arr)->insert_string(ndx, value);
286,248✔
174
            break;
286,248✔
175
        case Type::big_strings:
738,405✔
176
            static_cast<ArrayBigBlobs*>(m_arr)->insert_string(ndx, value);
738,405✔
177
            break;
738,405✔
178
        case Type::enum_strings: {
1,312,737✔
179
            static_cast<Array*>(m_arr)->insert(ndx, 0);
1,312,737✔
180
            set(ndx, value);
1,312,737✔
181
        }
1,312,737✔
182
    }
3,347,793✔
183
}
3,347,793✔
184

185
StringData ArrayString::get(size_t ndx) const
186
{
22,629,834✔
187
    switch (m_type) {
22,629,834✔
188
        case Type::small_strings:
9,204,207✔
189
            return static_cast<ArrayStringShort*>(m_arr)->get(ndx);
9,204,207✔
190
        case Type::medium_strings:
1,367,631✔
191
            return static_cast<ArraySmallBlobs*>(m_arr)->get_string(ndx);
1,367,631✔
192
        case Type::big_strings:
8,993,124✔
193
            return static_cast<ArrayBigBlobs*>(m_arr)->get_string(ndx);
8,993,124✔
194
        case Type::enum_strings: {
3,088,863✔
195
            size_t index = size_t(static_cast<Array*>(m_arr)->get(ndx));
3,088,863✔
196
            return m_string_enum_values->get(index);
3,088,863✔
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,962,526✔
226
    switch (m_type) {
3,962,526✔
227
        case Type::small_strings:
708,624✔
228
            return static_cast<ArrayStringShort*>(m_arr)->is_null(ndx);
708,624✔
229
        case Type::medium_strings:
19,518✔
230
            return static_cast<ArraySmallBlobs*>(m_arr)->is_null(ndx);
19,518✔
231
        case Type::big_strings:
3,234,393✔
232
            return static_cast<ArrayBigBlobs*>(m_arr)->is_null(ndx);
3,234,393✔
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
{
182,931✔
243
    switch (m_type) {
182,931✔
244
        case Type::small_strings:
92,202✔
245
            static_cast<ArrayStringShort*>(m_arr)->erase(ndx);
92,202✔
246
            break;
92,202✔
247
        case Type::medium_strings:
58,887✔
248
            static_cast<ArraySmallBlobs*>(m_arr)->erase(ndx);
58,887✔
249
            break;
58,887✔
250
        case Type::big_strings:
23,520✔
251
            static_cast<ArrayBigBlobs*>(m_arr)->erase(ndx);
23,520✔
252
            break;
23,520✔
253
        case Type::enum_strings:
8,322✔
254
            static_cast<Array*>(m_arr)->erase(ndx);
8,322✔
255
            break;
8,322✔
256
    }
182,931✔
257
}
182,931✔
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
{
426✔
285
    switch (m_type) {
426✔
286
        case Type::small_strings:
372✔
287
            static_cast<ArrayStringShort*>(m_arr)->clear();
372✔
288
            break;
372✔
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
    }
426✔
299
}
426✔
300

301
size_t ArrayString::find_first(StringData value, size_t begin, size_t end) const noexcept
302
{
4,071,723✔
303
    switch (m_type) {
4,071,723✔
304
        case Type::small_strings:
3,113,571✔
305
            return static_cast<ArrayStringShort*>(m_arr)->find_first(value, begin, end);
3,113,571✔
306
        case Type::medium_strings: {
105,258✔
307
            BinaryData as_binary(value.data(), value.size());
105,258✔
308
            return static_cast<ArraySmallBlobs*>(m_arr)->find_first(as_binary, true, begin, end);
105,258✔
309
            break;
×
310
        }
×
311
        case Type::big_strings: {
255,402✔
312
            BinaryData as_binary(value.data(), value.size());
255,402✔
313
            return static_cast<ArrayBigBlobs*>(m_arr)->find_first(as_binary, true, begin, end);
255,402✔
314
            break;
×
315
        }
×
316
        case Type::enum_strings: {
598,068✔
317
            size_t sz = m_string_enum_values->size();
598,068✔
318
            size_t res = m_string_enum_values->find_first(value, 0, sz);
598,068✔
319
            if (res != realm::not_found) {
598,068✔
320
                return static_cast<Array*>(m_arr)->find_first(res, begin, end);
598,068✔
321
            }
598,068✔
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,705✔
339
    return arr->get(ndx);
93,705✔
340
}
93,705✔
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,954✔
348
        size_t half = sz / 2;
531,042✔
349
        size_t mid = i + half;
531,042✔
350
        auto probe = get_string(arr, mid);
531,042✔
351
        if (probe < value) {
531,042✔
352
            i = mid + 1;
191,145✔
353
            sz -= half + 1;
191,145✔
354
        }
191,145✔
355
        else {
339,897✔
356
            sz = half;
339,897✔
357
        }
339,897✔
358
    }
531,042✔
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,307,304✔
380
    if (m_type == Type::big_strings)
7,307,304✔
381
        return Type::big_strings;
1,137,051✔
382

3,023,832✔
383
    if (m_type == Type::enum_strings)
6,170,253✔
384
        return Type::enum_strings;
3,937,470✔
385

1,055,640✔
386
    if (m_type == Type::medium_strings) {
2,232,783✔
387
        if (value_size <= medium_string_max_size)
531,897✔
388
            return Type::medium_strings;
531,369✔
389

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

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

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

261✔
408
        m_type = Type::big_strings;
528✔
409
        return Type::big_strings;
528✔
410
    }
528✔
411

789,588✔
412
    // m_type == Type::small
789,588✔
413
    if (value_size <= small_string_max_size)
1,700,886✔
414
        return Type::small_strings;
1,640,610✔
415

29,367✔
416
    if (value_size <= medium_string_max_size) {
60,276✔
417
        // Upgrade root leaf from small to medium strings
14,577✔
418
        auto string_short = static_cast<ArrayStringShort*>(m_arr);
29,721✔
419
        ArraySmallBlobs string_long(m_alloc);
29,721✔
420
        string_long.create(); // Throws
29,721✔
421

14,577✔
422
        size_t n = string_short->size();
29,721✔
423
        for (size_t i = 0; i < n; i++) {
64,209✔
424
            string_long.add_string(string_short->get(i)); // Throws
34,488✔
425
        }
34,488✔
426
        auto parent = string_short->get_parent();
29,721✔
427
        auto ndx_in_parent = string_short->get_ndx_in_parent();
29,721✔
428
        string_short->destroy();
29,721✔
429

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

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

14,790✔
443
        size_t n = string_short->size();
30,555✔
444
        for (size_t i = 0; i < n; i++) {
68,100✔
445
            big_blobs.add_string(string_short->get(i)); // Throws
37,545✔
446
        }
37,545✔
447
        auto parent = string_short->get_parent();
30,555✔
448
        auto ndx_in_parent = string_short->get_ndx_in_parent();
30,555✔
449
        string_short->destroy();
30,555✔
450

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

14,790✔
456
        m_type = Type::big_strings;
30,555✔
457
    }
30,555✔
458

29,367✔
459
    return m_type;
60,276✔
460
}
60,276✔
461

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