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

realm / realm-core / 1779

24 Oct 2023 11:23AM UTC coverage: 91.584% (-0.03%) from 91.61%
1779

push

Evergreen

web-flow
Implement BIO_CTRL_GET_KTLS_SEND and BIO_CTRL_GET_KTLS_RECV (#7078)

94354 of 173622 branches covered (0.0%)

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

153 existing lines in 19 files now uncovered.

230640 of 251835 relevant lines covered (91.58%)

6660586.44 hits per line

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

84.32
/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,292,900✔
58
    if (a == b)
11,292,900✔
59
        return 0;
9,916,395✔
60
    return utf8_compare(a, b) ? -1 : 1;
1,376,505✔
61
}
1,376,505✔
62

63
int compare_binary(BinaryData a, BinaryData b)
64
{
377,151✔
65
    size_t asz = a.size();
377,151✔
66
    size_t bsz = b.size();
377,151✔
67
    size_t min_sz = std::min(asz, bsz);
377,151✔
68
    int ret = memcmp(a.data(), b.data(), min_sz);
377,151✔
69
    if (ret == 0) {
377,151✔
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,151✔
76
}
377,151✔
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,894,368✔
100
    bool a_nan = std::isnan(a_raw);
1,894,368✔
101
    bool b_nan = std::isnan(b_raw);
1,894,368✔
102
    if (!a_nan && !b_nan) {
1,894,368✔
103
        // Just compare as IEEE floats
841,020✔
104
        return a_raw == b_raw ? 0 : a_raw < b_raw ? -1 : 1;
1,431,000✔
105
    }
1,667,352✔
106
    if (a_nan && b_nan) {
227,016✔
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
94,872✔
115
    // nans are treated as being less than all non-nan values
94,872✔
116
    return a_nan ? -1 : 1;
188,736✔
117
}
188,736✔
118

119
template <typename T>
120
int compare_generic(T lhs, T rhs)
121
{
36,565,116✔
122
    return lhs == rhs ? 0 : lhs < rhs ? -1 : 1;
35,374,464✔
123
}
36,565,116✔
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
{
332,931✔
133
    // All Longs are > NaN
159,330✔
134
    if (std::isnan(rhs))
332,931✔
135
        return 1;
6✔
136

159,327✔
137
    // Ints with magnitude <= 2**53 can be precisely represented as doubles.
159,327✔
138
    // Additionally, doubles outside of this range can't have a fractional component.
159,327✔
139
    static const int64_t kEndOfPreciseDoubles = 1ll << 53;
332,925✔
140
    if (lhs <= kEndOfPreciseDoubles && lhs >= -kEndOfPreciseDoubles) {
332,925✔
141
        return compare_float(double(lhs), rhs);
332,889✔
142
    }
332,889✔
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,226,044✔
165
    if (lhs.m_type == rhs.m_type)
3,226,044✔
166
        return lhs.m_type != 0;
2,144,313✔
167

533,916✔
168
    if (lhs.is_null() || rhs.is_null())
1,081,731✔
169
        return false;
345,471✔
170

361,185✔
171
    DataType l_type = lhs.get_type();
736,260✔
172
    DataType r_type = rhs.get_type();
736,260✔
173
    return data_types_are_comparable(l_type, r_type);
736,260✔
174
}
736,260✔
175

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

374,019✔
181
    if (is_numeric(l_type, r_type)) {
762,333✔
182
        return true;
336,558✔
183
    }
336,558✔
184
    if ((l_type == type_String && r_type == type_Binary) || (r_type == type_String && l_type == type_Binary)) {
425,775✔
185
        return true;
363,108✔
186
    }
363,108✔
187
    if (l_type == type_Mixed || r_type == type_Mixed) {
62,748✔
188
        return true; // Mixed is comparable with any type
648✔
189
    }
648✔
190
    return false;
62,019✔
191
}
62,019✔
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,721,275✔
227
    // Observe! Changing this function breaks the file format for Set<Mixed>
25,852,821✔
228

25,852,821✔
229
    if (is_null()) {
51,721,275✔
230
        return b.is_null() ? 0 : -1;
751,803✔
231
    }
901,416✔
232
    if (b.is_null())
50,819,859✔
233
        return 1;
376,134✔
234

25,214,952✔
235
    // None is null
25,214,952✔
236
    auto type = get_type();
50,443,725✔
237
    switch (type) {
50,443,725✔
238
        case type_Bool: {
1,262,187✔
239
            if (b.get_type() == type_Bool) {
1,262,187✔
240
                return compare_generic(bool_val, b.bool_val);
1,261,620✔
241
            }
1,261,620✔
242
            break;
567✔
243
        }
567✔
244
        case type_Int:
34,484,616✔
245
            switch (b.get_type()) {
34,484,616✔
246
                case type_Int:
34,256,142✔
247
                    return compare_generic(int_val, b.int_val);
34,256,142✔
248
                case type_Float:
67,407✔
249
                    return compare_long_to_double(int_val, b.float_val);
67,407✔
250
                case type_Double:
156,414✔
251
                    return compare_long_to_double(int_val, b.double_val);
156,414✔
252
                case type_Decimal:
5,022✔
253
                    return Decimal128(int_val).compare(b.decimal_val);
5,022✔
254
                default:
5,676✔
255
                    break;
5,676✔
256
            }
5,676✔
257
            break;
5,676✔
258
        case type_String:
11,291,355✔
259
            if (b.get_type() == type_String)
11,291,355✔
260
                return compare_string(get<StringData>(), b.get<StringData>());
11,285,493✔
261
            [[fallthrough]];
5,862✔
262
        case type_Binary:
386,076✔
263
            if (b.get_type() == type_String || b.get_type() == type_Binary)
386,076✔
264
                return compare_binary(get<BinaryData>(), b.get<BinaryData>());
377,151✔
265
            break;
8,925✔
266
        case type_Float:
742,593✔
267
            switch (b.get_type()) {
742,593✔
268
                case type_Int:
9,633✔
269
                    return -compare_long_to_double(b.int_val, float_val);
9,633✔
270
                case type_Float:
719,088✔
271
                    return compare_float(float_val, b.float_val);
719,088✔
272
                case type_Double:
5,406✔
273
                    return compare_float(double(float_val), b.double_val);
5,406✔
274
                case type_Decimal:
4,095✔
275
                    return Decimal128(float_val).compare(b.decimal_val);
4,095✔
276
                default:
4,371✔
277
                    break;
4,371✔
278
            }
4,371✔
279
            break;
4,371✔
280
        case type_Double:
947,556✔
281
            switch (b.get_type()) {
947,556✔
282
                case type_Int:
99,495✔
283
                    return -compare_long_to_double(b.int_val, double_val);
99,495✔
284
                case type_Float:
6,237✔
285
                    return compare_float(double_val, double(b.float_val));
6,237✔
286
                case type_Double:
830,853✔
287
                    return compare_float(double_val, b.double_val);
830,853✔
288
                case type_Decimal:
5,700✔
289
                    return Decimal128(double_val).compare(b.decimal_val);
5,700✔
290
                default:
5,289✔
291
                    break;
5,289✔
292
            }
5,289✔
293
            break;
5,289✔
294
        case type_Timestamp:
589,863✔
295
            if (b.get_type() == type_Timestamp) {
589,863✔
296
                return compare_generic(date_val, b.date_val);
589,509✔
297
            }
589,509✔
298
            break;
354✔
299
        case type_ObjectId:
88,086✔
300
            if (b.get_type() == type_ObjectId) {
88,086✔
301
                return compare_generic(id_val, b.id_val);
81,711✔
302
            }
81,711✔
303
            break;
6,375✔
304
        case type_Decimal:
454,668✔
305
            switch (b.get_type()) {
454,668✔
306
                case type_Int:
13,686✔
307
                    return decimal_val.compare(Decimal128(b.int_val));
13,686✔
308
                case type_Float:
4,581✔
309
                    return decimal_val.compare(Decimal128(b.float_val));
4,581✔
310
                case type_Double:
5,658✔
311
                    return decimal_val.compare(Decimal128(b.double_val));
5,658✔
312
                case type_Decimal:
426,741✔
313
                    return decimal_val.compare(b.decimal_val);
426,741✔
314
                default:
4,008✔
315
                    break;
4,008✔
316
            }
4,008✔
317
            break;
4,008✔
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,996✔
324
            if (b.is_type(type_TypedLink)) {
15,996✔
325
                return compare_generic(link_val, b.link_val);
15,876✔
326
            }
15,876✔
327
            break;
120✔
328
        case type_UUID:
320,061✔
329
            if (b.get_type() == type_UUID) {
320,061✔
330
                return compare_generic(uuid_val, b.uuid_val);
317,589✔
331
            }
317,589✔
332
            break;
2,472✔
333
        default:
61,860✔
334
            if (type == type_TypeOfValue && b.get_type() == type_TypeOfValue) {
61,860✔
335
                return TypeOfValue(int_val).matches(TypeOfValue(b.int_val)) ? 0 : compare_generic(int_val, b.int_val);
51,885✔
336
            }
61,827✔
337
            REALM_ASSERT_RELEASE(false && "Compare not supported for this column type");
33✔
338
            break;
33✔
339
    }
41,079✔
340

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

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

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

359
template <>
360
int64_t Mixed::export_to_type() const noexcept
361
{
14,884,941✔
362
    // If the common type is Int, then both values must be Int
6,457,506✔
363
    REALM_ASSERT(get_type() == type_Int);
14,884,941✔
364
    return int_val;
14,884,941✔
365
}
14,884,941✔
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,449,324✔
479
    // It might be by accident that this works, but it finds the most advanced type
3,229,836✔
480
    DataType common = std::max(t1, t2);
7,449,324✔
481
    return common;
7,449,324✔
482
}
7,449,324✔
483

484
Mixed Mixed::operator+(const Mixed& rhs) const noexcept
485
{
3,964,719✔
486
    if (!is_null() && !rhs.is_null()) {
3,965,931✔
487
        auto common_type = get_common_type(get_type(), rhs.get_type());
3,965,889✔
488
        switch (common_type) {
3,965,889✔
489
            case type_Int:
3,963,489✔
490
                return export_to_type<Int>() + rhs.export_to_type<Int>();
3,963,489✔
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
        }
2,147,483,713✔
500
    }
2,147,483,713✔
501
    return {};
2,147,483,713✔
502
}
2,147,483,713✔
503

504
Mixed Mixed::operator-(const Mixed& rhs) const noexcept
505
{
1,938,576✔
506
    if (!is_null() && !rhs.is_null()) {
1,938,576✔
507
        auto common_type = get_common_type(get_type(), rhs.get_type());
1,938,576✔
508
        switch (common_type) {
1,938,576✔
509
            case type_Int:
1,938,480✔
510
                return export_to_type<Int>() - rhs.export_to_type<Int>();
1,938,480✔
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
{
778,326✔
526
    if (!is_null() && !rhs.is_null()) {
778,326✔
527
        auto common_type = get_common_type(get_type(), rhs.get_type());
778,326✔
528
        switch (common_type) {
778,326✔
529
            case type_Int:
777,402✔
530
                return export_to_type<Int>() * rhs.export_to_type<Int>();
777,402✔
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
{
777,090✔
546
    if (!is_null() && !rhs.is_null()) {
777,090✔
547
        auto common_type = get_common_type(get_type(), rhs.get_type());
777,090✔
548
        switch (common_type) {
777,090✔
549
            case type_Int: {
776,478✔
550
                auto dividend = export_to_type<Int>();
776,478✔
551
                auto divisor = rhs.export_to_type<Int>();
776,478✔
552
                // We don't want to throw here. This is usually used as part of a query
332,157✔
553
                // and in this case we would just expect a no match
332,157✔
554
                if (divisor == 0)
776,478✔
555
                    return dividend < 0 ? std::numeric_limits<int64_t>::min() : std::numeric_limits<int64_t>::max();
624✔
556
                return dividend / divisor;
775,854✔
557
            }
775,854✔
558
            case type_Float:
331,974✔
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:
331,974✔
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:
331,893✔
565
                return export_to_type<Decimal128>() / rhs.export_to_type<Decimal128>();
96✔
566
            default:
331,845✔
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,405,666✔
638
    if (is_null()) {
6,405,666✔
639
        return {};
458,592✔
640
    }
458,592✔
641
    switch (get_type()) {
5,947,074✔
642
        case type_Int: {
3,690,945✔
643
            int64_t i = get_int();
3,690,945✔
644
            const char* c = reinterpret_cast<const char*>(&i);
3,690,945✔
645
            realm::safe_copy_n(c, sizeof(int64_t), buffer.data());
3,690,945✔
646
            return StringData{buffer.data(), sizeof(int64_t)};
3,690,945✔
647
        }
×
648
        case type_Bool: {
36,354✔
649
            int64_t i = get_bool() ? 1 : 0;
28,185✔
650
            return Mixed(i).get_index_data(buffer);
36,354✔
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,297,263✔
673
            return get_string();
1,297,263✔
674
        case type_Binary: {
18✔
675
            auto bin = get_binary();
12✔
676
            return {bin.data(), bin.size()};
12✔
677
        }
24✔
678
        case type_Timestamp: {
46,251✔
679
            auto dt = get<Timestamp>();
46,251✔
680
            int64_t s = dt.get_seconds();
46,251✔
681
            int32_t ns = dt.get_nanoseconds();
46,251✔
682
            constexpr size_t index_size = sizeof(s) + sizeof(ns);
46,251✔
683
            const char* s_buf = reinterpret_cast<const char*>(&s);
46,251✔
684
            const char* ns_buf = reinterpret_cast<const char*>(&ns);
46,251✔
685
            realm::safe_copy_n(s_buf, sizeof(s), buffer.data());
46,251✔
686
            realm::safe_copy_n(ns_buf, sizeof(ns), buffer.data() + sizeof(s));
46,251✔
687
            return StringData{buffer.data(), index_size};
46,251✔
688
        }
24✔
689
        case type_ObjectId: {
853,602✔
690
            auto id = get<ObjectId>();
853,602✔
691
            memcpy(&buffer, &id, sizeof(ObjectId));
853,602✔
692
            return StringData{buffer.data(), sizeof(ObjectId)};
853,602✔
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,048✔
731
    if (is_null()) {
3,192,048✔
732
        return;
3,152,088✔
733
    }
3,152,088✔
734
    switch (get_type()) {
39,960✔
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,960✔
746
}
39,960✔
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

© 2026 Coveralls, Inc