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

realm / realm-core / 1775

20 Oct 2023 07:34PM UTC coverage: 91.596% (+0.03%) from 91.567%
1775

push

Evergreen

web-flow
Added sync socket result enum for sync socket callback handlers in C API (#7015)

* Added sync socket result enum for sync socket callback handlers in C API
* Updated changelog
* CAPI: timer callbacks are now released by canceled/complete function
* Updated c_api tests to use all sync socket c_api functions
* Additional updates to sync socket c api test
* Added CAPI write callback manager to manage async write callbacks
* Pass error codes up to default socket provider for async_write_binary() callbacks
* Removed async write callback manager from CAPI
* Updated changelog after release
* clang format and updates from review
* Update async_write_binary() error handling to not throw exception
* Updated a few comments
* Another comment update
* Updates from review

94374 of 173622 branches covered (0.0%)

111 of 150 new or added lines in 8 files covered. (74.0%)

50 existing lines in 13 files now uncovered.

230666 of 251830 relevant lines covered (91.6%)

7038030.93 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
{
19,880,289✔
28
    m_arr = new (&m_storage.m_string_short) ArrayStringShort(a, true);
19,880,289✔
29
}
19,880,289✔
30

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

36
void ArrayString::init_from_mem(MemRef mem) noexcept
37
{
17,833,437✔
38
    char* header = mem.get_addr();
17,833,437✔
39

8,827,101✔
40
    ArrayParent* parent = m_arr->get_parent();
17,833,437✔
41
    size_t ndx_in_parent = m_arr->get_ndx_in_parent();
17,833,437✔
42

8,827,101✔
43
    bool long_strings = Array::get_hasrefs_from_header(header);
17,833,437✔
44
    if (!long_strings) {
17,833,437✔
45
        // Small strings
6,497,031✔
46
        bool is_small = Array::get_wtype_from_header(header) == Array::wtype_Multiply;
13,179,546✔
47
        if (is_small) {
13,179,546✔
48
            auto arr = new (&m_storage.m_string_short) ArrayStringShort(m_alloc, m_nullable);
7,516,812✔
49
            arr->init_from_mem(mem);
7,516,812✔
50
            m_type = Type::small_strings;
7,516,812✔
51
        }
7,516,812✔
52
        else {
5,662,734✔
53
            auto arr = new (&m_storage.m_enum) Array(m_alloc);
5,662,734✔
54
            arr->init_from_mem(mem);
5,662,734✔
55
            m_string_enum_values = std::make_unique<ArrayString>(m_alloc);
5,662,734✔
56
            ArrayParent* p;
5,662,734✔
57
            REALM_ASSERT(m_spec != nullptr);
5,662,734✔
58
            REALM_ASSERT(m_col_ndx != realm::npos);
5,662,734✔
59
            ref_type r = m_spec->get_enumkeys_ref(m_col_ndx, p);
5,662,734✔
60
            m_string_enum_values->init_from_ref(r);
5,662,734✔
61
            m_string_enum_values->set_parent(p, m_col_ndx);
5,662,734✔
62
            m_type = Type::enum_strings;
5,662,734✔
63
        }
5,662,734✔
64
    }
13,179,546✔
65
    else {
4,653,891✔
66
        bool is_big = Array::get_context_flag_from_header(header);
4,653,891✔
67
        if (!is_big) {
4,653,891✔
68
            auto arr = new (&m_storage.m_string_long) ArraySmallBlobs(m_alloc);
1,205,367✔
69
            arr->init_from_mem(mem);
1,205,367✔
70
            m_type = Type::medium_strings;
1,205,367✔
71
        }
1,205,367✔
72
        else {
3,448,524✔
73
            auto arr = new (&m_storage.m_big_blobs) ArrayBigBlobs(m_alloc, m_nullable);
3,448,524✔
74
            arr->init_from_mem(mem);
3,448,524✔
75
            m_type = Type::big_strings;
3,448,524✔
76
        }
3,448,524✔
77
    }
4,653,891✔
78
    m_arr->set_parent(parent, ndx_in_parent);
17,833,437✔
79
}
17,833,437✔
80

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

104
size_t ArrayString::size() const
105
{
15,237,111✔
106
    switch (m_type) {
15,237,111✔
107
        case Type::small_strings:
6,753,087✔
108
            return static_cast<ArrayStringShort*>(m_arr)->size();
6,753,087✔
109
        case Type::medium_strings:
451,479✔
110
            return static_cast<ArraySmallBlobs*>(m_arr)->size();
451,479✔
111
        case Type::big_strings:
8,035,548✔
112
            return static_cast<ArrayBigBlobs*>(m_arr)->size();
8,035,548✔
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
{
160,434✔
121
    switch (upgrade_leaf(value.size())) {
160,434✔
122
        case Type::small_strings:
105,348✔
123
            static_cast<ArrayStringShort*>(m_arr)->add(value);
105,348✔
124
            break;
105,348✔
125
        case Type::medium_strings:
28,275✔
126
            static_cast<ArraySmallBlobs*>(m_arr)->add_string(value);
28,275✔
127
            break;
28,275✔
128
        case Type::big_strings:
26,811✔
129
            static_cast<ArrayBigBlobs*>(m_arr)->add_string(value);
26,811✔
130
            break;
26,811✔
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
    }
160,434✔
139
}
160,434✔
140

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

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

185
StringData ArrayString::get(size_t ndx) const
186
{
22,367,481✔
187
    switch (m_type) {
22,367,481✔
188
        case Type::small_strings:
9,210,510✔
189
            return static_cast<ArrayStringShort*>(m_arr)->get(ndx);
9,210,510✔
190
        case Type::medium_strings:
1,384,578✔
191
            return static_cast<ArraySmallBlobs*>(m_arr)->get_string(ndx);
1,384,578✔
192
        case Type::big_strings:
8,691,594✔
193
            return static_cast<ArrayBigBlobs*>(m_arr)->get_string(ndx);
8,691,594✔
194
        case Type::enum_strings: {
3,087,687✔
195
            size_t index = size_t(static_cast<Array*>(m_arr)->get(ndx));
3,087,687✔
196
            return m_string_enum_values->get(index);
3,087,687✔
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,555,234✔
226
    switch (m_type) {
3,555,234✔
227
        case Type::small_strings:
448,245✔
228
            return static_cast<ArrayStringShort*>(m_arr)->is_null(ndx);
448,245✔
229
        case Type::medium_strings:
21,810✔
230
            return static_cast<ArraySmallBlobs*>(m_arr)->is_null(ndx);
21,810✔
231
        case Type::big_strings:
3,085,185✔
232
            return static_cast<ArrayBigBlobs*>(m_arr)->is_null(ndx);
3,085,185✔
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
{
179,409✔
243
    switch (m_type) {
179,409✔
244
        case Type::small_strings:
87,918✔
245
            static_cast<ArrayStringShort*>(m_arr)->erase(ndx);
87,918✔
246
            break;
87,918✔
247
        case Type::medium_strings:
59,475✔
248
            static_cast<ArraySmallBlobs*>(m_arr)->erase(ndx);
59,475✔
249
            break;
59,475✔
250
        case Type::big_strings:
23,673✔
251
            static_cast<ArrayBigBlobs*>(m_arr)->erase(ndx);
23,673✔
252
            break;
23,673✔
253
        case Type::enum_strings:
8,343✔
254
            static_cast<Array*>(m_arr)->erase(ndx);
8,343✔
255
            break;
8,343✔
256
    }
179,409✔
257
}
179,409✔
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
{
381✔
285
    switch (m_type) {
381✔
286
        case Type::small_strings:
327✔
287
            static_cast<ArrayStringShort*>(m_arr)->clear();
327✔
288
            break;
327✔
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
    }
381✔
299
}
381✔
300

301
size_t ArrayString::find_first(StringData value, size_t begin, size_t end) const noexcept
302
{
4,083,888✔
303
    switch (m_type) {
4,083,888✔
304
        case Type::small_strings:
3,127,677✔
305
            return static_cast<ArrayStringShort*>(m_arr)->find_first(value, begin, end);
3,127,677✔
306
        case Type::medium_strings: {
104,598✔
307
            BinaryData as_binary(value.data(), value.size());
104,598✔
308
            return static_cast<ArraySmallBlobs*>(m_arr)->find_first(as_binary, true, begin, end);
104,598✔
309
            break;
×
310
        }
×
311
        case Type::big_strings: {
255,828✔
312
            BinaryData as_binary(value.data(), value.size());
255,828✔
313
            return static_cast<ArrayBigBlobs*>(m_arr)->find_first(as_binary, true, begin, end);
255,828✔
314
            break;
×
315
        }
×
316
        case Type::enum_strings: {
597,132✔
317
            size_t sz = m_string_enum_values->size();
597,132✔
318
            size_t res = m_string_enum_values->find_first(value, 0, sz);
597,132✔
319
            if (res != realm::not_found) {
597,132✔
320
                return static_cast<Array*>(m_arr)->find_first(res, begin, end);
597,132✔
321
            }
597,132✔
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,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;
190,824✔
353
            sz -= half + 1;
190,824✔
354
        }
190,824✔
355
        else {
340,167✔
356
            sz = half;
340,167✔
357
        }
340,167✔
358
    }
530,991✔
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,335,816✔
380
    if (m_type == Type::big_strings)
7,335,816✔
381
        return Type::big_strings;
1,135,890✔
382

3,042,750✔
383
    if (m_type == Type::enum_strings)
6,199,926✔
384
        return Type::enum_strings;
3,937,035✔
385

1,073,961✔
386
    if (m_type == Type::medium_strings) {
2,262,891✔
387
        if (value_size <= medium_string_max_size)
532,158✔
388
            return Type::medium_strings;
531,615✔
389

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

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

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

285✔
408
        m_type = Type::big_strings;
543✔
409
        return Type::big_strings;
543✔
410
    }
543✔
411

807,657✔
412
    // m_type == Type::small
807,657✔
413
    if (value_size <= small_string_max_size)
1,730,733✔
414
        return Type::small_strings;
1,670,208✔
415

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

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

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

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

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

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

14,793✔
456
        m_type = Type::big_strings;
30,987✔
457
    }
30,987✔
458

29,295✔
459
    return m_type;
60,525✔
460
}
60,525✔
461

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