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

realm / realm-core / 1777

24 Oct 2023 11:23AM UTC coverage: 91.554% (-0.06%) from 91.61%
1777

push

Evergreen

web-flow
Implement BIO_CTRL_GET_KTLS_SEND and BIO_CTRL_GET_KTLS_RECV (#7078)

94340 of 173622 branches covered (0.0%)

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

197 existing lines in 20 files now uncovered.

230561 of 251831 relevant lines covered (91.55%)

6373903.06 hits per line

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

84.01
/src/realm/mixed.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/mixed.hpp>
20
#include <realm/decimal128.hpp>
21
#include <realm/unicode.hpp>
22
#include <realm/column_type_traits.hpp>
23
#include <realm/obj.hpp>
24
#include <realm/table.hpp>
25
#include <realm/query_value.hpp>
26
#include <realm/util/serializer.hpp>
27

28
namespace realm {
29
namespace {
30
static const int sorting_rank[19] = {
31
    // Observe! Changing these values breaks the file format for Set<Mixed>
32

33
    -1, // null
34
    1,  // type_Int = 0,
35
    0,  // type_Bool = 1,
36
    2,  // type_String = 2,
37
    -1,
38
    2,  // type_Binary = 4,
39
    -1, // type_OldTable = 5,
40
    -1, // type_Mixed = 6,
41
    -1, // type_OldDateTime = 7,
42
    3,  // type_Timestamp = 8,
43
    1,  // type_Float = 9,
44
    1,  // type_Double = 10,
45
    1,  // type_Decimal = 11,
46
    7,  // type_Link = 12,
47
    -1, // type_LinkList = 13,
48
    -1,
49
    4, // type_ObjectId = 15,
50
    6, // type_TypedLink = 16
51
    5, // type_UUID = 17
52

53
    // Observe! Changing these values breaks the file format for Set<Mixed>
54
};
55

56
int compare_string(StringData a, StringData b)
57
{
11,299,008✔
58
    if (a == b)
11,299,008✔
59
        return 0;
9,893,790✔
60
    return utf8_compare(a, b) ? -1 : 1;
1,405,218✔
61
}
1,405,218✔
62

63
int compare_binary(BinaryData a, BinaryData b)
64
{
377,154✔
65
    size_t asz = a.size();
377,154✔
66
    size_t bsz = b.size();
377,154✔
67
    size_t min_sz = std::min(asz, bsz);
377,154✔
68
    int ret = memcmp(a.data(), b.data(), min_sz);
377,154✔
69
    if (ret == 0) {
377,154✔
70
        if (asz > bsz)
11,385✔
71
            ret = 1;
1,017✔
72
        else if (asz < bsz)
10,368✔
73
            ret = -1;
906✔
74
    }
11,385✔
75
    return ret;
377,154✔
76
}
377,154✔
77

78
template <int>
79
struct IntTypeForSize;
80
template <>
81
struct IntTypeForSize<1> {
82
    using type = uint8_t;
83
};
84
template <>
85
struct IntTypeForSize<2> {
86
    using type = uint16_t;
87
};
88
template <>
89
struct IntTypeForSize<4> {
90
    using type = uint32_t;
91
};
92
template <>
93
struct IntTypeForSize<8> {
94
    using type = uint64_t;
95
};
96

97
template <typename Float>
98
int compare_float(Float a_raw, Float b_raw)
99
{
1,892,421✔
100
    bool a_nan = std::isnan(a_raw);
1,892,421✔
101
    bool b_nan = std::isnan(b_raw);
1,892,421✔
102
    if (!a_nan && !b_nan) {
1,892,421✔
103
        // Just compare as IEEE floats
847,800✔
104
        return a_raw == b_raw ? 0 : a_raw < b_raw ? -1 : 1;
1,433,019✔
105
    }
1,665,174✔
106
    if (a_nan && b_nan) {
227,247✔
107
        // Compare the nan values as unsigned
19,098✔
108
        using IntType = typename IntTypeForSize<sizeof(Float)>::type;
38,280✔
109
        IntType a = 0, b = 0;
38,280✔
110
        memcpy(&a, &a_raw, sizeof(Float));
38,280✔
111
        memcpy(&b, &b_raw, sizeof(Float));
38,280✔
112
        return a == b ? 0 : a < b ? -1 : 1;
29,112✔
113
    }
38,280✔
114
    // One is nan, the other is not
95,106✔
115
    // nans are treated as being less than all non-nan values
95,106✔
116
    return a_nan ? -1 : 1;
188,967✔
117
}
188,967✔
118

119
template <typename T>
120
int compare_generic(T lhs, T rhs)
121
{
36,662,334✔
122
    return lhs == rhs ? 0 : lhs < rhs ? -1 : 1;
35,466,357✔
123
}
36,662,334✔
124

125
// This is the tricky one. Needs to support the following cases:
126
// * Doubles with a fractional component.
127
// * Longs that can't be precisely represented as a double.
128
// * Doubles outside of the range of Longs (including +/- Inf).
129
// * NaN (defined by us as less than all Longs)
130
// * Return value is always -1, 0, or 1 to ensure it is safe to negate.
131
int compare_long_to_double(int64_t lhs, double rhs)
132
{
330,003✔
133
    // All Longs are > NaN
165,369✔
134
    if (std::isnan(rhs))
330,003✔
135
        return 1;
6✔
136

165,366✔
137
    // Ints with magnitude <= 2**53 can be precisely represented as doubles.
165,366✔
138
    // Additionally, doubles outside of this range can't have a fractional component.
165,366✔
139
    static const int64_t kEndOfPreciseDoubles = 1ll << 53;
329,997✔
140
    if (lhs <= kEndOfPreciseDoubles && lhs >= -kEndOfPreciseDoubles) {
329,997✔
141
        return compare_float(double(lhs), rhs);
329,961✔
142
    }
329,961✔
143

18✔
144
    // Large magnitude doubles (including +/- Inf) are strictly > or < all Longs.
18✔
145
    static const double kBoundOfLongRange = -static_cast<double>(LLONG_MIN); // positive 2**63
36✔
146
    if (rhs >= kBoundOfLongRange)
36✔
147
        return -1; // Can't be represented in a Long.
6✔
148
    if (rhs < -kBoundOfLongRange)
30✔
149
        return 1; // Can be represented in a Long.
6✔
150

12✔
151
    // Remaining Doubles can have their integer component precisely represented as long longs.
12✔
152
    // If they have a fractional component, they must be strictly > or < lhs even after
12✔
153
    // truncation of the fractional component since low-magnitude lhs were handled above.
12✔
154
    return compare_generic(lhs, int64_t(rhs));
24✔
155
}
24✔
156
} // anonymous namespace
157

158
Mixed::Mixed(const Obj& obj) noexcept
159
    : Mixed(ObjLink(obj.get_table()->get_key(), obj.get_key()))
160
{
90✔
161
}
90✔
162

163
bool Mixed::types_are_comparable(const Mixed& lhs, const Mixed& rhs)
164
{
3,340,320✔
165
    if (lhs.m_type == rhs.m_type)
3,340,320✔
166
        return lhs.m_type != 0;
2,261,841✔
167

539,808✔
168
    if (lhs.is_null() || rhs.is_null())
1,078,479✔
169
        return false;
345,579✔
170

367,062✔
171
    DataType l_type = lhs.get_type();
732,900✔
172
    DataType r_type = rhs.get_type();
732,900✔
173
    return data_types_are_comparable(l_type, r_type);
732,900✔
174
}
732,900✔
175

176
bool Mixed::data_types_are_comparable(DataType l_type, DataType r_type)
177
{
1,458,939✔
178
    if (l_type == r_type)
1,458,939✔
179
        return true;
700,545✔
180

380,055✔
181
    if (is_numeric(l_type, r_type)) {
758,394✔
182
        return true;
333,624✔
183
    }
333,624✔
184
    if ((l_type == type_String && r_type == type_Binary) || (r_type == type_String && l_type == type_Binary)) {
424,770✔
185
        return true;
363,108✔
186
    }
363,108✔
187
    if (l_type == type_Mixed || r_type == type_Mixed) {
62,754✔
188
        return true; // Mixed is comparable with any type
648✔
189
    }
648✔
190
    return false;
61,014✔
191
}
61,014✔
192

193
bool Mixed::accumulate_numeric_to(Decimal128& destination) const noexcept
194
{
27,804✔
195
    bool did_accumulate = false;
27,804✔
196
    if (!is_null()) {
27,804✔
197
        switch (get_type()) {
27,300✔
198
            case type_Int:
17,820✔
199
                destination += Decimal128(get_int());
17,820✔
200
                did_accumulate = true;
17,820✔
201
                break;
17,820✔
202
            case type_Double:
1,164✔
203
                destination += Decimal128(get_double());
1,164✔
204
                did_accumulate = true;
1,164✔
205
                break;
1,164✔
206
            case type_Float:
1,764✔
207
                destination += Decimal128(get_float());
1,764✔
208
                did_accumulate = true;
1,764✔
209
                break;
1,764✔
210
            case type_Decimal: {
1,752✔
211
                auto val = get_decimal();
1,752✔
212
                if (!val.is_nan()) {
1,752✔
213
                    destination += val;
1,116✔
214
                    did_accumulate = true;
1,116✔
215
                }
1,116✔
216
                break;
1,752✔
217
            }
×
218
            default:
4,800✔
219
                break;
4,800✔
220
        }
27,804✔
221
    }
27,804✔
222
    return did_accumulate;
27,804✔
223
}
27,804✔
224

225
int Mixed::compare(const Mixed& b) const noexcept
226
{
51,807,066✔
227
    // Observe! Changing this function breaks the file format for Set<Mixed>
26,007,378✔
228

26,007,378✔
229
    if (is_null()) {
51,807,066✔
230
        return b.is_null() ? 0 : -1;
744,483✔
231
    }
894,009✔
232
    if (b.is_null())
50,913,057✔
233
        return 1;
375,942✔
234

25,372,644✔
235
    // None is null
25,372,644✔
236
    auto type = get_type();
50,537,115✔
237
    switch (type) {
50,537,115✔
238
        case type_Bool: {
1,252,266✔
239
            if (b.get_type() == type_Bool) {
1,252,266✔
240
                return compare_generic(bool_val, b.bool_val);
1,251,702✔
241
            }
1,251,702✔
242
            break;
564✔
243
        }
564✔
244
        case type_Int:
34,576,341✔
245
            switch (b.get_type()) {
34,576,341✔
246
                case type_Int:
34,351,083✔
247
                    return compare_generic(int_val, b.int_val);
34,351,083✔
248
                case type_Float:
64,317✔
249
                    return compare_long_to_double(int_val, b.float_val);
64,317✔
250
                case type_Double:
156,414✔
251
                    return compare_long_to_double(int_val, b.double_val);
156,414✔
252
                case type_Decimal:
5,028✔
253
                    return Decimal128(int_val).compare(b.decimal_val);
5,028✔
254
                default:
5,688✔
255
                    break;
5,688✔
256
            }
5,688✔
257
            break;
5,688✔
258
        case type_String:
11,296,407✔
259
            if (b.get_type() == type_String)
11,296,407✔
260
                return compare_string(get<StringData>(), b.get<StringData>());
11,287,512✔
261
            [[fallthrough]];
8,895✔
262
        case type_Binary:
389,109✔
263
            if (b.get_type() == type_String || b.get_type() == type_Binary)
389,109✔
264
                return compare_binary(get<BinaryData>(), b.get<BinaryData>());
377,154✔
265
            break;
11,955✔
266
        case type_Float:
742,542✔
267
            switch (b.get_type()) {
742,542✔
268
                case type_Int:
9,627✔
269
                    return -compare_long_to_double(b.int_val, float_val);
9,627✔
270
                case type_Float:
719,043✔
271
                    return compare_float(float_val, b.float_val);
719,043✔
272
                case type_Double:
5,412✔
273
                    return compare_float(double(float_val), b.double_val);
5,412✔
274
                case type_Decimal:
4,089✔
275
                    return Decimal128(float_val).compare(b.decimal_val);
4,089✔
276
                default:
4,371✔
277
                    break;
4,371✔
278
            }
4,371✔
279
            break;
4,371✔
280
        case type_Double:
948,594✔
281
            switch (b.get_type()) {
948,594✔
282
                case type_Int:
99,645✔
283
                    return -compare_long_to_double(b.int_val, double_val);
99,645✔
284
                case type_Float:
6,225✔
285
                    return compare_float(double_val, double(b.float_val));
6,225✔
286
                case type_Double:
831,801✔
287
                    return compare_float(double_val, b.double_val);
831,801✔
288
                case type_Decimal:
5,694✔
289
                    return Decimal128(double_val).compare(b.decimal_val);
5,694✔
290
                default:
5,247✔
291
                    break;
5,247✔
292
            }
5,247✔
293
            break;
5,247✔
294
        case type_Timestamp:
603,804✔
295
            if (b.get_type() == type_Timestamp) {
603,804✔
296
                return compare_generic(date_val, b.date_val);
603,444✔
297
            }
603,444✔
298
            break;
360✔
299
        case type_ObjectId:
87,990✔
300
            if (b.get_type() == type_ObjectId) {
87,990✔
301
                return compare_generic(id_val, b.id_val);
81,624✔
302
            }
81,624✔
303
            break;
6,366✔
304
        case type_Decimal:
454,680✔
305
            switch (b.get_type()) {
454,680✔
306
                case type_Int:
13,683✔
307
                    return decimal_val.compare(Decimal128(b.int_val));
13,683✔
308
                case type_Float:
4,584✔
309
                    return decimal_val.compare(Decimal128(b.float_val));
4,584✔
310
                case type_Double:
5,661✔
311
                    return decimal_val.compare(Decimal128(b.double_val));
5,661✔
312
                case type_Decimal:
426,741✔
313
                    return decimal_val.compare(b.decimal_val);
426,741✔
314
                default:
4,011✔
315
                    break;
4,011✔
316
            }
4,011✔
317
            break;
4,011✔
318
        case type_Link:
5,541✔
319
            if (b.get_type() == type_Link) {
5,541✔
320
                return compare_generic(int_val, b.int_val);
5,541✔
321
            }
5,541✔
322
            break;
×
323
        case type_TypedLink:
15,990✔
324
            if (b.is_type(type_TypedLink)) {
15,990✔
325
                return compare_generic(link_val, b.link_val);
15,876✔
326
            }
15,876✔
327
            break;
114✔
328
        case type_UUID:
320,205✔
329
            if (b.get_type() == type_UUID) {
320,205✔
330
                return compare_generic(uuid_val, b.uuid_val);
317,724✔
331
            }
317,724✔
332
            break;
2,481✔
333
        default:
62,016✔
334
            if (type == type_TypeOfValue && b.get_type() == type_TypeOfValue) {
62,016✔
335
                return TypeOfValue(int_val).matches(TypeOfValue(b.int_val)) ? 0 : compare_generic(int_val, b.int_val);
52,074✔
336
            }
62,016✔
337
            REALM_ASSERT_RELEASE(false && "Compare not supported for this column type");
×
338
            break;
×
339
    }
41,097✔
340

19,938✔
341
    // Comparing rank of types as a fallback makes it possible to sort of a list of Mixed
19,938✔
342
    REALM_ASSERT(sorting_rank[m_type] != sorting_rank[b.m_type]);
41,097✔
343
    // Using rank table will ensure that all numeric values are kept together
19,938✔
344
    return (sorting_rank[m_type] > sorting_rank[b.m_type]) ? 1 : -1;
31,977✔
345

19,938✔
346
    // Observe! Changing this function breaks the file format for Set<Mixed>
19,938✔
347
}
41,097✔
348

349
int Mixed::compare_signed(const Mixed& b) const noexcept
350
{
21,864✔
351
    if (is_type(type_String) && b.is_type(type_String)) {
21,864✔
352
        auto a_val = get_string();
21,696✔
353
        auto b_val = b.get_string();
21,696✔
354
        return a_val == b_val ? 0 : a_val < b_val ? -1 : 1;
21,684✔
355
    }
21,696✔
356
    return compare(b);
168✔
357
}
168✔
358

359
template <>
360
int64_t Mixed::export_to_type() const noexcept
361
{
15,020,772✔
362
    // If the common type is Int, then both values must be Int
8,884,998✔
363
    REALM_ASSERT(get_type() == type_Int);
15,020,772✔
364
    return int_val;
15,020,772✔
365
}
15,020,772✔
366

367
template <>
368
float Mixed::export_to_type() const noexcept
369
{
1,452✔
370
    // If the common type is Float, then values must be either Int or Float
726✔
371
    REALM_ASSERT(m_type);
1,452✔
372
    switch (get_type()) {
1,452✔
373
        case type_Int:
678✔
374
            return float(int_val);
678✔
375
        case type_Float:
774✔
376
            return float_val;
774✔
377
        default:
✔
378
            REALM_ASSERT(false);
×
379
            break;
×
380
    }
×
381
    return 0.;
×
382
}
×
383

384
template <>
385
double Mixed::export_to_type() const noexcept
386
{
2,592✔
387
    // If the common type is Double, then values must be either Int, Float or Double
1,296✔
388
    REALM_ASSERT(m_type);
2,592✔
389
    switch (get_type()) {
2,592✔
390
        case type_Int:
1,296✔
391
            return double(int_val);
1,296✔
392
        case type_Float:
✔
393
            return double(float_val);
×
394
        case type_Double:
1,296✔
395
            return double_val;
1,296✔
396
        default:
✔
397
            REALM_ASSERT(false);
×
398
            break;
×
399
    }
×
400
    return 0.;
×
401
}
×
402

403
template <>
404
Decimal128 Mixed::export_to_type() const noexcept
405
{
192✔
406
    REALM_ASSERT(m_type);
192✔
407
    switch (get_type()) {
192✔
408
        case type_Int:
96✔
409
            return Decimal128(int_val);
96✔
410
        case type_Float:
✔
411
            return Decimal128(float_val);
×
412
        case type_Double:
✔
413
            return Decimal128(double_val);
×
414
        case type_Decimal:
96✔
415
            return decimal_val;
96✔
416
        default:
✔
417
            REALM_ASSERT(false);
×
418
            break;
×
419
    }
×
420
    return {};
×
421
}
×
422

423
template <>
424
util::Optional<int64_t> Mixed::get<util::Optional<int64_t>>() const noexcept
425
{
408✔
426
    if (is_null()) {
408✔
427
        return {};
×
428
    }
×
429
    return get<int64_t>();
408✔
430
}
408✔
431

432
template <>
433
util::Optional<bool> Mixed::get<util::Optional<bool>>() const noexcept
434
{
48✔
435
    if (is_null()) {
48✔
436
        return {};
×
437
    }
×
438
    return get<bool>();
48✔
439
}
48✔
440

441
template <>
442
util::Optional<float> Mixed::get<util::Optional<float>>() const noexcept
443
{
120✔
444
    if (is_null()) {
120✔
445
        return {};
×
446
    }
×
447
    return get<float>();
120✔
448
}
120✔
449

450
template <>
451
util::Optional<double> Mixed::get<util::Optional<double>>() const noexcept
452
{
120✔
453
    if (is_null()) {
120✔
454
        return {};
×
455
    }
×
456
    return get<double>();
120✔
457
}
120✔
458

459
template <>
460
util::Optional<ObjectId> Mixed::get<util::Optional<ObjectId>>() const noexcept
461
{
96✔
462
    if (is_null()) {
96✔
463
        return {};
×
464
    }
×
465
    return get<ObjectId>();
96✔
466
}
96✔
467

468
template <>
469
util::Optional<UUID> Mixed::get<util::Optional<UUID>>() const noexcept
470
{
96✔
471
    if (is_null()) {
96✔
472
        return {};
×
473
    }
×
474
    return get<UUID>();
96✔
475
}
96✔
476

477
static DataType get_common_type(DataType t1, DataType t2) noexcept
478
{
7,515,756✔
479
    // It might be by accident that this works, but it finds the most advanced type
4,446,786✔
480
    DataType common = std::max(t1, t2);
7,515,756✔
481
    return common;
7,515,756✔
482
}
7,515,756✔
483

484
Mixed Mixed::operator+(const Mixed& rhs) const noexcept
485
{
3,998,529✔
486
    if (!is_null() && !rhs.is_null()) {
3,998,529✔
487
        auto common_type = get_common_type(get_type(), rhs.get_type());
3,997,401✔
488
        switch (common_type) {
3,997,401✔
489
            case type_Int:
3,997,347✔
490
                return export_to_type<Int>() + rhs.export_to_type<Int>();
3,997,347✔
491
            case type_Float:
372✔
492
                return export_to_type<float>() + rhs.export_to_type<float>();
372✔
493
            case type_Double:
114✔
494
                return export_to_type<double>() + rhs.export_to_type<double>();
114✔
495
            case type_Decimal:
✔
496
                return export_to_type<Decimal128>() + rhs.export_to_type<Decimal128>();
×
497
            default:
48✔
498
                break;
48✔
499
        }
1,176✔
500
    }
1,176✔
501
    return {};
1,176✔
502
}
1,176✔
503

504
Mixed Mixed::operator-(const Mixed& rhs) const noexcept
505
{
1,952,301✔
506
    if (!is_null() && !rhs.is_null()) {
1,952,301✔
507
        auto common_type = get_common_type(get_type(), rhs.get_type());
1,952,301✔
508
        switch (common_type) {
1,952,301✔
509
            case type_Int:
1,952,205✔
510
                return export_to_type<Int>() - rhs.export_to_type<Int>();
1,952,205✔
511
            case type_Float:
96✔
512
                return export_to_type<float>() - rhs.export_to_type<float>();
96✔
513
            case type_Double:
✔
514
                return export_to_type<double>() - rhs.export_to_type<double>();
×
515
            case type_Decimal:
✔
516
                return export_to_type<Decimal128>() - rhs.export_to_type<Decimal128>();
×
517
            default:
✔
518
                break;
×
519
        }
×
520
    }
×
521
    return {};
×
522
}
×
523

524
Mixed Mixed::operator*(const Mixed& rhs) const noexcept
525
{
783,816✔
526
    if (!is_null() && !rhs.is_null()) {
783,816✔
527
        auto common_type = get_common_type(get_type(), rhs.get_type());
783,816✔
528
        switch (common_type) {
783,816✔
529
            case type_Int:
782,892✔
530
                return export_to_type<Int>() * rhs.export_to_type<Int>();
782,892✔
531
            case type_Float:
✔
532
                return export_to_type<float>() * rhs.export_to_type<float>();
×
533
            case type_Double:
924✔
534
                return export_to_type<double>() * rhs.export_to_type<double>();
924✔
535
            case type_Decimal:
✔
536
                return export_to_type<Decimal128>() * rhs.export_to_type<Decimal128>();
×
537
            default:
✔
538
                break;
×
UNCOV
539
        }
×
UNCOV
540
    }
×
UNCOV
541
    return {};
×
UNCOV
542
}
×
543

544
Mixed Mixed::operator/(const Mixed& rhs) const noexcept
545
{
782,580✔
546
    if (!is_null() && !rhs.is_null()) {
782,580✔
547
        auto common_type = get_common_type(get_type(), rhs.get_type());
782,580✔
548
        switch (common_type) {
782,580✔
549
            case type_Int: {
781,968✔
550
                auto dividend = export_to_type<Int>();
781,968✔
551
                auto divisor = rhs.export_to_type<Int>();
781,968✔
552
                // We don't want to throw here. This is usually used as part of a query
467,685✔
553
                // and in this case we would just expect a no match
467,685✔
554
                if (divisor == 0)
781,968✔
555
                    return dividend < 0 ? std::numeric_limits<int64_t>::min() : std::numeric_limits<int64_t>::max();
624✔
556
                return dividend / divisor;
781,344✔
557
            }
781,344✔
558
            case type_Float:
467,502✔
559
                static_assert(std::numeric_limits<float>::is_iec559); // Infinity is supported
258✔
560
                return export_to_type<float>() / rhs.export_to_type<float>();
258✔
561
            case type_Double:
467,502✔
562
                static_assert(std::numeric_limits<double>::is_iec559); // Infinity is supported
258✔
563
                return export_to_type<double>() / rhs.export_to_type<double>();
258✔
564
            case type_Decimal:
467,421✔
565
                return export_to_type<Decimal128>() / rhs.export_to_type<Decimal128>();
96✔
566
            default:
467,373✔
567
                break;
×
UNCOV
568
        }
×
UNCOV
569
    }
×
UNCOV
570
    return {};
×
UNCOV
571
}
×
572

573
size_t Mixed::hash() const
574
{
41,727✔
575
    if (is_null())
41,727✔
576
        return 0;
4,014✔
577

19,398✔
578
    size_t hash = 0;
37,713✔
579
    switch (get_type()) {
37,713✔
580
        case type_Int:
2,946✔
581
            hash = size_t(int_val);
2,946✔
582
            break;
2,946✔
583
        case type_Bool:
1,746✔
584
            hash = bool_val ? 0xdeadbeef : 0xcafebabe;
1,320✔
585
            break;
1,746✔
586
        case type_Float: {
✔
587
            auto unsigned_data = reinterpret_cast<const unsigned char*>(&float_val);
×
588
            hash = murmur2_or_cityhash(unsigned_data, sizeof(float));
×
589
            break;
×
590
        }
×
591
        case type_Double: {
5,862✔
592
            auto unsigned_data = reinterpret_cast<const unsigned char*>(&double_val);
5,862✔
593
            hash = murmur2_or_cityhash(unsigned_data, sizeof(double));
5,862✔
594
            break;
5,862✔
595
        }
×
596
        case type_String:
15,300✔
597
            hash = get<StringData>().hash();
15,300✔
598
            break;
15,300✔
599
        case type_Binary: {
756✔
600
            auto bin = get<BinaryData>();
756✔
601
            StringData str(bin.data(), bin.size());
756✔
602
            hash = str.hash();
756✔
603
            break;
756✔
604
        }
×
605
        case type_Timestamp:
2,850✔
606
            hash = get<Timestamp>().hash();
2,850✔
607
            break;
2,850✔
608
        case type_ObjectId:
2,751✔
609
            hash = get<ObjectId>().hash();
2,751✔
610
            break;
2,751✔
611
        case type_UUID: {
2,751✔
612
            hash = get<UUID>().hash();
2,751✔
613
            break;
2,751✔
614
        }
×
615
        case type_TypedLink: {
✔
616
            auto unsigned_data = reinterpret_cast<const unsigned char*>(&link_val);
×
617
            hash = murmur2_or_cityhash(unsigned_data, 12);
×
618
            break;
×
619
        }
×
620
        case type_Decimal: {
2,751✔
621
            auto value = get<Decimal128>();
2,751✔
622
            auto unsigned_data = reinterpret_cast<const unsigned char*>(value.raw());
2,751✔
623
            hash = murmur2_or_cityhash(unsigned_data, sizeof(Decimal128::Bid128));
2,751✔
624
            break;
2,751✔
625
        }
×
626
        case type_Mixed:
✔
627
        case type_Link:
✔
628
        case type_LinkList:
✔
629
            REALM_ASSERT_RELEASE(false && "Hash not supported for this column type");
×
630
            break;
×
631
    }
37,713✔
632

19,398✔
633
    return hash;
37,713✔
634
}
37,713✔
635

636
StringData Mixed::get_index_data(std::array<char, 16>& buffer) const noexcept
637
{
6,400,311✔
638
    if (is_null()) {
6,400,311✔
639
        return {};
457,926✔
640
    }
457,926✔
641
    switch (get_type()) {
5,942,385✔
642
        case type_Int: {
3,697,725✔
643
            int64_t i = get_int();
3,697,725✔
644
            const char* c = reinterpret_cast<const char*>(&i);
3,697,725✔
645
            realm::safe_copy_n(c, sizeof(int64_t), buffer.data());
3,697,725✔
646
            return StringData{buffer.data(), sizeof(int64_t)};
3,697,725✔
647
        }
×
648
        case type_Bool: {
34,323✔
649
            int64_t i = get_bool() ? 1 : 0;
26,892✔
650
            return Mixed(i).get_index_data(buffer);
34,323✔
651
        }
×
652
        case type_Float: {
24✔
653
            auto v2 = get_float();
24✔
654
            int i = int(v2);
24✔
655
            if (i == v2) {
24✔
656
                return Mixed(i).get_index_data(buffer);
×
657
            }
×
658
            const char* src = reinterpret_cast<const char*>(&v2);
24✔
659
            realm::safe_copy_n(src, sizeof(float), buffer.data());
24✔
660
            return StringData{buffer.data(), sizeof(float)};
24✔
661
        }
24✔
662
        case type_Double: {
48✔
663
            auto v2 = get_double();
48✔
664
            int i = int(v2);
48✔
665
            if (i == v2) {
48✔
666
                return Mixed(i).get_index_data(buffer);
24✔
667
            }
24✔
668
            const char* src = reinterpret_cast<const char*>(&v2);
24✔
669
            realm::safe_copy_n(src, sizeof(double), buffer.data());
24✔
670
            return StringData{buffer.data(), sizeof(double)};
24✔
671
        }
24✔
672
        case type_String:
1,286,055✔
673
            return get_string();
1,286,055✔
674
        case type_Binary: {
18✔
675
            auto bin = get_binary();
12✔
676
            return {bin.data(), bin.size()};
12✔
677
        }
24✔
678
        case type_Timestamp: {
47,364✔
679
            auto dt = get<Timestamp>();
47,364✔
680
            int64_t s = dt.get_seconds();
47,364✔
681
            int32_t ns = dt.get_nanoseconds();
47,364✔
682
            constexpr size_t index_size = sizeof(s) + sizeof(ns);
47,364✔
683
            const char* s_buf = reinterpret_cast<const char*>(&s);
47,364✔
684
            const char* ns_buf = reinterpret_cast<const char*>(&ns);
47,364✔
685
            realm::safe_copy_n(s_buf, sizeof(s), buffer.data());
47,364✔
686
            realm::safe_copy_n(ns_buf, sizeof(ns), buffer.data() + sizeof(s));
47,364✔
687
            return StringData{buffer.data(), index_size};
47,364✔
688
        }
24✔
689
        case type_ObjectId: {
853,533✔
690
            auto id = get<ObjectId>();
853,533✔
691
            memcpy(&buffer, &id, sizeof(ObjectId));
853,533✔
692
            return StringData{buffer.data(), sizeof(ObjectId)};
853,533✔
693
        }
24✔
694
        case type_Decimal: {
30✔
695
            auto v2 = this->get_decimal();
30✔
696
            int64_t i;
30✔
697
            if (v2.to_int(i)) {
30✔
698
                return Mixed(i).get_index_data(buffer);
18✔
699
            }
18✔
700
            const char* src = reinterpret_cast<const char*>(&v2);
12✔
701
            realm::safe_copy_n(src, sizeof(v2), buffer.data());
12✔
702
            return StringData{buffer.data(), sizeof(v2)};
12✔
703
        }
12✔
704
        case type_UUID: {
25,482✔
705
            auto id = get<UUID>();
25,482✔
706
            const auto bytes = id.to_bytes();
25,482✔
707
            std::copy_n(bytes.data(), bytes.size(), buffer.begin());
25,482✔
708
            return StringData{buffer.data(), bytes.size()};
25,482✔
709
        }
12✔
710
        case type_TypedLink: {
54✔
711
            auto link = get<ObjLink>();
54✔
712
            uint32_t k1 = link.get_table_key().value;
54✔
713
            int64_t k2 = link.get_obj_key().value;
54✔
714
            const char* src = reinterpret_cast<const char*>(&k1);
54✔
715
            realm::safe_copy_n(src, sizeof(k1), buffer.data());
54✔
716
            src = reinterpret_cast<const char*>(&k2);
54✔
717
            realm::safe_copy_n(src, sizeof(k2), buffer.data() + sizeof(k1));
54✔
718
            return StringData{buffer.data(), sizeof(k1) + sizeof(k2)};
54✔
719
        }
12✔
720
        case type_Mixed:
6✔
721
        case type_Link:
✔
722
        case type_LinkList:
✔
723
            break;
×
724
    }
×
725
    REALM_ASSERT_RELEASE(false && "Index not supported for this column type");
×
726
    return {};
×
727
}
×
728

729
void Mixed::use_buffer(std::string& buf) noexcept
730
{
3,192,183✔
731
    if (is_null()) {
3,192,183✔
732
        return;
3,152,463✔
733
    }
3,152,463✔
734
    switch (get_type()) {
39,720✔
735
        case type_String:
9,354✔
736
            buf = std::string(string_val);
9,354✔
737
            string_val = StringData(buf);
9,354✔
738
            break;
9,354✔
739
        case type_Binary:
222✔
740
            buf = std::string(binary_val);
222✔
741
            binary_val = BinaryData(buf);
222✔
742
            break;
222✔
743
        default:
30,096✔
744
            break;
30,096✔
745
    }
39,720✔
746
}
39,720✔
747

748
// LCOV_EXCL_START
749
std::ostream& operator<<(std::ostream& out, const Mixed& m)
750
{
25,956✔
751
    if (m.is_null()) {
25,956✔
752
        out << "null";
264✔
753
    }
264✔
754
    else {
25,692✔
755
        switch (m.get_type()) {
25,692✔
756
            case type_Int:
8,580✔
757
                out << m.int_val;
8,580✔
758
                break;
8,580✔
759
            case type_Bool:
882✔
760
                out << (m.bool_val ? "true" : "false");
687✔
761
                break;
882✔
762
            case type_Float:
894✔
763
                out << m.float_val;
894✔
764
                break;
894✔
765
            case type_Double:
1,488✔
766
                out << m.double_val;
1,488✔
767
                break;
1,488✔
768
            case type_String:
2,340✔
769
                out << util::serializer::print_value(m.string_val);
2,340✔
770
                break;
2,340✔
771
            case type_Binary:
876✔
772
                out << util::serializer::print_value(m.binary_val);
876✔
773
                break;
876✔
774
            case type_Timestamp:
870✔
775
                out << util::serializer::print_value(m.date_val);
870✔
776
                break;
870✔
777
            case type_Decimal:
888✔
778
                out << m.decimal_val;
888✔
779
                break;
888✔
780
            case type_ObjectId:
7,788✔
781
                out << util::serializer::print_value(m.id_val);
7,788✔
782
                break;
7,788✔
783
            case type_Link:
12✔
784
                out << util::serializer::print_value(ObjKey(m.int_val));
12✔
785
                break;
12✔
786
            case type_TypedLink:
✔
787
                out << util::serializer::print_value(m.link_val);
×
788
                break;
×
789
            case type_UUID:
1,074✔
790
                out << util::serializer::print_value(m.uuid_val);
1,074✔
791
                break;
1,074✔
792
            case type_Mixed:
✔
793
            case type_LinkList:
✔
794
                REALM_ASSERT(false);
×
795
        }
25,692✔
796
    }
25,692✔
797
    return out;
25,956✔
798
}
25,956✔
799
// LCOV_EXCL_STOP
800

801

802
} // namespace realm
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