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

realm / realm-core / 2447

28 Jun 2024 02:00PM UTC coverage: 90.972% (+0.03%) from 90.946%
2447

push

Evergreen

web-flow
RCORE-2168: Replicate clear instruction unconditionally for nested collections (#7821)

102194 of 180408 branches covered (56.65%)

102 of 102 new or added lines in 3 files covered. (100.0%)

46 existing lines in 10 files now uncovered.

214895 of 236222 relevant lines covered (90.97%)

6082329.28 hits per line

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

92.36
/src/realm/query_engine.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/query_engine.hpp>
20

21
#include <realm/query_expression.hpp>
22
#include <realm/index_string.hpp>
23
#include <realm/db.hpp>
24
#include <realm/utilities.hpp>
25

26
namespace realm {
27

28
ParentNode::ParentNode(const ParentNode& from)
29
    : m_child(from.m_child ? from.m_child->clone() : nullptr)
1,612,158✔
30
    , m_condition_column_key(from.m_condition_column_key)
1,612,158✔
31
    , m_dD(from.m_dD)
1,612,158✔
32
    , m_dT(from.m_dT)
1,612,158✔
33
    , m_probes(from.m_probes)
1,612,158✔
34
    , m_matches(from.m_matches)
1,612,158✔
35
    , m_table(from.m_table)
1,612,158✔
36
{
3,236,742✔
37
}
3,236,742✔
38

39

40
size_t ParentNode::find_first(size_t start, size_t end)
41
{
5,944,569✔
42
    size_t sz = m_children.size();
5,944,569✔
43
    size_t current_cond = 0;
5,944,569✔
44
    size_t nb_cond_to_test = sz;
5,944,569✔
45

46
    while (REALM_LIKELY(start < end)) {
6,347,103✔
47
        size_t m = m_children[current_cond]->find_first_local(start, end);
6,317,649✔
48

49
        if (m != start) {
6,317,649✔
50
            // Pointer advanced - we will have to check all other conditions
51
            nb_cond_to_test = sz;
2,708,160✔
52
            start = m;
2,708,160✔
53
        }
2,708,160✔
54

55
        nb_cond_to_test--;
6,317,649✔
56

57
        // Optimized for one condition where this will be true first time
58
        if (REALM_LIKELY(nb_cond_to_test == 0))
6,317,649✔
59
            return m;
5,915,115✔
60

61
        current_cond++;
402,534✔
62

63
        if (current_cond == sz)
402,534✔
64
            current_cond = 0;
137,913✔
65
    }
402,534✔
66
    return not_found;
29,454✔
67
}
5,944,569✔
68

69
template <class T>
70
inline bool Obj::evaluate(T func) const
71
{
1,266,150✔
72
    REALM_ASSERT(is_valid());
1,266,150✔
73
    Cluster cluster(0, get_alloc(), m_table->m_clusters);
1,266,150✔
74
    cluster.init(m_mem);
1,266,150✔
75
    cluster.set_offset(m_key.value - cluster.get_key_value(m_row_ndx));
1,266,150✔
76
    return func(&cluster, m_row_ndx);
1,266,150✔
77
}
1,266,150✔
78

79
bool ParentNode::match(const Obj& obj)
80
{
1,266,150✔
81
    return obj.evaluate([this](const Cluster* cluster, size_t row) {
1,266,150✔
82
        set_cluster(cluster);
1,266,117✔
83
        size_t m = find_first(row, row + 1);
1,266,117✔
84
        return m != npos;
1,266,117✔
85
    });
1,266,117✔
86
}
1,266,150✔
87

88
size_t ParentNode::aggregate_local(QueryStateBase* st, size_t start, size_t end, size_t local_limit,
89
                                   ArrayPayload* source_column)
90
{
6,625,623✔
91
    // aggregate called on non-integer column type. Speed of this function is not as critical as speed of the
92
    // integer version, because find_first_local() is relatively slower here (because it's non-integers).
93
    //
94
    // Todo: Two speedups are possible. Simple: Initially test if there are no sub criterias and run
95
    // find_first_local()
96
    // in a tight loop if so (instead of testing if there are sub criterias after each match). Harder: Specialize
97
    // data type array to make array call match() directly on each match, like for integers.
98

99
    m_state = st;
6,625,623✔
100
    m_state->set_payload_column(source_column);
6,625,623✔
101
    size_t local_matches = 0;
6,625,623✔
102

103
    if (m_children.size() == 1) {
6,765,483✔
104
        return find_all_local(start, end);
6,720,912✔
105
    }
6,720,912✔
106

107
    size_t r = start - 1;
2,147,528,218✔
108
    for (;;) {
2,148,107,722✔
109
        if (local_matches == local_limit) {
1,290,042✔
110
            m_dD = double(r - start) / (local_matches + 1.1);
22,836✔
111
            return r + 1;
22,836✔
112
        }
22,836✔
113

114
        // Find first match in this condition node
115
        auto pos = r + 1;
1,267,206✔
116
        r = find_first_local(pos, end);
1,267,206✔
117
        if (r == not_found) {
1,267,206✔
118
            m_dD = double(pos - start) / (local_matches + 1.1);
39,942✔
119
            return end;
39,942✔
120
        }
39,942✔
121

122
        local_matches++;
1,227,264✔
123

124
        // Find first match in remaining condition nodes
125
        size_t m = r;
1,227,264✔
126

127
        for (size_t c = 1; c < m_children.size(); c++) {
1,913,103✔
128
            m = m_children[c]->find_first_local(r, r + 1);
1,286,484✔
129
            if (m != r) {
1,286,484✔
130
                break;
600,645✔
131
            }
600,645✔
132
        }
1,286,484✔
133

134
        // If index of first match in this node equals index of first match in all remaining nodes, we have a final
135
        // match
136
        if (m == r) {
1,227,264✔
137
            bool cont = st->match(r);
626,649✔
138
            if (!cont) {
626,649✔
139
                return static_cast<size_t>(-1);
30✔
140
            }
30✔
141
        }
626,649✔
142
    }
1,227,264✔
143
}
2,147,528,218✔
144

145
size_t ParentNode::find_all_local(size_t start, size_t end)
146
{
334,803✔
147
    while (start < end) {
11,492,295✔
148
        start = find_first_local(start, end);
11,157,522✔
149
        if (start != not_found) {
11,157,522✔
150
            bool cont = m_state->match(start);
10,956,918✔
151
            if (!cont) {
10,956,918✔
152
                return static_cast<size_t>(-1);
30✔
153
            }
30✔
154
            start++;
10,956,888✔
155
        }
10,956,888✔
156
    }
11,157,522✔
157
    return end;
334,773✔
158
}
334,803✔
159

160
void MixedNode<Equal>::init(bool will_query_ranges)
161
{
5,586✔
162
    MixedNodeBase::init(will_query_ranges);
5,586✔
163

164
    REALM_ASSERT(bool(m_index_evaluator) ==
5,586✔
165
                 (m_table.unchecked_ptr()->search_index_type(m_condition_column_key) == IndexType::General));
5,586✔
166
    if (m_index_evaluator) {
5,586✔
167
        auto index = ParentNode::m_table->get_search_index(ParentNode::m_condition_column_key);
2,898✔
168
        m_index_evaluator->init(index, m_value);
2,898✔
169
        m_dT = 0.0;
2,898✔
170
    }
2,898✔
171
    else {
2,688✔
172
        m_dT = 10.0;
2,688✔
173
    }
2,688✔
174
}
5,586✔
175

176
size_t MixedNode<Equal>::find_first_local(size_t start, size_t end)
177
{
24,738✔
178
    REALM_ASSERT(m_table);
24,738✔
179

180
    if (m_index_evaluator) {
24,738✔
181
        return m_index_evaluator->do_search_index(m_cluster, start, end);
12,132✔
182
    }
12,132✔
183
    else {
12,606✔
184
        return m_leaf->find_first(m_value, start, end);
12,606✔
185
    }
12,606✔
186

187
    return not_found;
×
188
}
24,738✔
189

190
void MixedNode<EqualIns>::init(bool will_query_ranges)
191
{
96✔
192
    MixedNodeBase::init(will_query_ranges);
96✔
193

194
    StringData val_as_string;
96✔
195
    if (m_value.is_type(type_String)) {
96✔
196
        val_as_string = m_value.get<StringData>();
36✔
197
    }
36✔
198
    else if (m_value.is_type(type_Binary)) {
60✔
199
        BinaryData bin = m_value.get<BinaryData>();
24✔
200
        val_as_string = StringData(bin.data(), bin.size());
24✔
201
    }
24✔
202
    REALM_ASSERT(bool(m_index_evaluator) ==
96✔
203
                 (m_table.unchecked_ptr()->search_index_type(m_condition_column_key) == IndexType::General));
96✔
204
    if (m_index_evaluator) {
96✔
205
        auto index = ParentNode::m_table->get_search_index(ParentNode::m_condition_column_key);
30✔
206
        if (!val_as_string.is_null()) {
30✔
207
            m_index_matches.clear();
12✔
208
            constexpr bool case_insensitive = true;
12✔
209
            index->find_all(m_index_matches, val_as_string, case_insensitive);
12✔
210
            // It is unfortunate but necessary to check the type due to Binary and String
211
            // having the same StringIndex hash values
212
            m_index_matches.erase(std::remove_if(m_index_matches.begin(), m_index_matches.end(),
12✔
213
                                                 [this](const ObjKey& obj_key) {
12✔
214
                                                     Mixed to_check =
12✔
215
                                                         m_table->get_object(obj_key).get_any(m_condition_column_key);
12✔
216
                                                     return (!Mixed::types_are_comparable(to_check, m_value));
12✔
217
                                                 }),
12✔
218
                                  m_index_matches.end());
12✔
219
            m_index_evaluator->init(&m_index_matches);
12✔
220
        }
12✔
221
        else {
18✔
222
            // search for non string can use exact match
223
            m_index_evaluator->init(index, m_value);
18✔
224
        }
18✔
225
        m_dT = 0.0;
30✔
226
    }
30✔
227
    else {
66✔
228
        m_dT = 10.0;
66✔
229
        if (!val_as_string.is_null()) {
66✔
230
            auto upper = case_map(val_as_string, true);
48✔
231
            auto lower = case_map(val_as_string, false);
48✔
232
            if (!upper || !lower) {
48✔
233
                throw query_parser::InvalidQueryError(util::format("Malformed UTF-8: %1", val_as_string));
×
234
            }
×
235
            else {
48✔
236
                m_ucase = std::move(*upper);
48✔
237
                m_lcase = std::move(*lower);
48✔
238
            }
48✔
239
        }
48✔
240
    }
66✔
241
}
96✔
242

243
size_t MixedNode<EqualIns>::find_first_local(size_t start, size_t end)
244
{
114✔
245
    REALM_ASSERT(m_table);
114✔
246

247
    EqualIns cond;
114✔
248
    if (m_value.is_type(type_String, type_Binary)) {
114✔
249
        for (size_t i = start; i < end; i++) {
4,848✔
250
            QueryValue val(m_leaf->get(i));
4,800✔
251
            if (!Mixed::types_are_comparable(m_value, val)) {
4,800✔
252
                continue;
4,062✔
253
            }
4,062✔
254
            StringData val_as_str = val.export_to_type<StringData>();
738✔
255
            if (cond(m_value.export_to_type<StringData>(), m_ucase.c_str(), m_lcase.c_str(), val_as_str))
738✔
256
                return i;
30✔
257
        }
738✔
258
    }
78✔
259
    else {
36✔
260
        for (size_t i = start; i < end; i++) {
1,818✔
261
            QueryValue val(m_leaf->get(i));
1,800✔
262
            if (cond(val, m_value))
1,800✔
263
                return i;
18✔
264
        }
1,800✔
265
    }
36✔
266

267
    return not_found;
66✔
268
}
114✔
269

270
void StringNodeEqualBase::init(bool will_query_ranges)
271
{
64,425✔
272
    StringNodeBase::init(will_query_ranges);
64,425✔
273

274
    const bool uses_index = has_search_index();
64,425✔
275
    if (m_is_string_enum) {
64,425✔
276
        m_dT = 1.0;
2,658✔
277
    }
2,658✔
278
    else if (uses_index) {
61,767✔
279
        m_dT = 0.0;
3,687✔
280
    }
3,687✔
281
    else {
58,080✔
282
        m_dT = 10.0;
58,080✔
283
    }
58,080✔
284

285
    if (uses_index) {
64,425✔
286
        m_index_evaluator = std::make_optional(IndexEvaluator{});
4,149✔
287
        _search_index_init();
4,149✔
288
    }
4,149✔
289
}
64,425✔
290

291
size_t StringNodeEqualBase::find_first_local(size_t start, size_t end)
292
{
1,110,462✔
293
    REALM_ASSERT(m_table);
1,110,462✔
294

295
    if (m_index_evaluator) {
1,110,462✔
296
        return m_index_evaluator->do_search_index(m_cluster, start, end);
241,698✔
297
    }
241,698✔
298

299
    return _find_first_local(start, end);
868,764✔
300
}
1,110,462✔
301

302

303
void IndexEvaluator::init(SearchIndex* index, Mixed value)
304
{
19,821✔
305
    REALM_ASSERT(index);
19,821✔
306
    m_matching_keys = nullptr;
19,821✔
307
    FindRes fr;
19,821✔
308
    InternalFindResult res;
19,821✔
309

310
    m_last_start_key = ObjKey();
19,821✔
311
    m_results_start = 0;
19,821✔
312
    fr = index->find_all_no_copy(value, res);
19,821✔
313

314
    m_index_matches.reset();
19,821✔
315
    switch (fr) {
19,821✔
316
        case FindRes_single:
17,013✔
317
            m_actual_key = ObjKey(res.payload);
17,013✔
318
            m_results_end = 1;
17,013✔
319
            break;
17,013✔
320
        case FindRes_column:
2,217✔
321
            m_index_matches.reset(new IntegerColumn(index->get_alloc(), ref_type(res.payload))); // Throws
2,217✔
322
            m_results_start = res.start_ndx;
2,217✔
323
            m_results_end = res.end_ndx;
2,217✔
324
            m_actual_key = ObjKey(m_index_matches->get(m_results_start));
2,217✔
325
            break;
2,217✔
326
        case FindRes_not_found:
591✔
327
            m_results_end = 0;
591✔
328
            break;
591✔
329
    }
19,821✔
330
    m_results_ndx = m_results_start;
19,821✔
331
}
19,821✔
332

333
void IndexEvaluator::init(std::vector<ObjKey>* storage)
334
{
462✔
335
    REALM_ASSERT(storage);
462✔
336
    m_matching_keys = storage;
462✔
337
    m_actual_key = ObjKey();
462✔
338
    m_last_start_key = ObjKey();
462✔
339
    m_results_start = 0;
462✔
340
    m_results_end = m_matching_keys->size();
462✔
341
    m_results_ndx = 0;
462✔
342
    if (m_results_start != m_results_end) {
462✔
343
        m_actual_key = m_matching_keys->at(0);
378✔
344
    }
378✔
345
}
462✔
346

347
size_t IndexEvaluator::do_search_index(const Cluster* cluster, size_t start, size_t end)
348
{
260,592✔
349
    if (start >= end) {
260,592✔
350
        return not_found;
9✔
351
    }
9✔
352

353
    ObjKey first_key = cluster->get_real_key(start);
260,583✔
354
    if (first_key < m_last_start_key) {
260,583✔
355
        // We are not advancing through the clusters. We basically don't know where we are,
356
        // so just start over from the beginning.
357
        m_results_ndx = m_results_start;
96,006✔
358
        m_actual_key = (m_results_start != m_results_end) ? get_internal(m_results_start) : ObjKey();
96,006✔
359
    }
96,006✔
360
    m_last_start_key = first_key;
260,583✔
361

362
    // Check if we can expect to find more keys
363
    if (m_results_ndx < m_results_end) {
260,583✔
364
        // Check if we should advance to next key to search for
365
        while (first_key > m_actual_key) {
72,291,462✔
366
            m_results_ndx++;
72,090,189✔
367
            if (m_results_ndx == m_results_end) {
72,090,189✔
368
                return not_found;
6,297✔
369
            }
6,297✔
370
            m_actual_key = get_internal(m_results_ndx);
72,083,892✔
371
        }
72,083,892✔
372

373
        // If actual_key is bigger than last key, it is not in this leaf
374
        ObjKey last_key = cluster->get_real_key(end - 1);
201,273✔
375
        if (m_actual_key > last_key)
201,273✔
376
            return not_found;
116,442✔
377

378
        // Now actual_key must be found in leaf keys
379
        return cluster->lower_bound_key(ObjKey(m_actual_key.value - cluster->get_offset()));
84,831✔
380
    }
201,273✔
381
    return not_found;
53,013✔
382
}
260,583✔
383

384
StringNode<Equal>::StringNode(ColKey col, const Mixed* begin, const Mixed* end)
385
    : StringNodeEqualBase(StringData(), col)
156✔
386
{
300✔
387
    // Don't use the search index if present since we're in a scenario where
388
    // it'd be slower
389
    m_index_evaluator.reset();
300✔
390

391
    for (const Mixed* it = begin; it != end; ++it) {
8,052✔
392
        if (it->is_null()) {
7,752✔
393
            m_needles.emplace();
66✔
394
        }
66✔
395
        else if (const StringData* str = it->get_if<StringData>()) {
7,686✔
396
            m_needle_storage.push_back(std::make_unique<char[]>(str->size() + 1));
7,650✔
397
            std::copy(str->data(), str->data() + str->size(), m_needle_storage.back().get());
7,650✔
398
            m_needle_storage.back()[str->size()] = '\0';
7,650✔
399
            m_needles.insert(StringData(m_needle_storage.back().get(), str->size()));
7,650✔
400
        }
7,650✔
401
    }
7,752✔
402
    if (m_needles.empty()) {
300✔
403
        throw InvalidArgument("No string arguments in query");
×
404
    }
×
405
}
300✔
406

407
void StringNode<Equal>::_search_index_init()
408
{
3,645✔
409
    if (!m_needles.empty()) {
3,645✔
410
        m_index_evaluator.reset();
60✔
411
        return;
60✔
412
    }
60✔
413
    REALM_ASSERT(bool(m_index_evaluator));
3,585✔
414
    auto index = ParentNode::m_table.unchecked_ptr()->get_search_index(ParentNode::m_condition_column_key);
3,585✔
415
    m_index_evaluator->init(index, StringNodeBase::m_string_value);
3,585✔
416
}
3,585✔
417

418
bool StringNode<Equal>::do_consume_condition(ParentNode& node)
419
{
234✔
420
    // Don't use the search index if present since we're in a scenario where
421
    // it'd be slower
422
    m_index_evaluator.reset();
234✔
423

424
    auto& other = static_cast<StringNode<Equal>&>(node);
234✔
425
    REALM_ASSERT(m_condition_column_key == other.m_condition_column_key);
234✔
426

427
    if (m_needles.empty()) {
234✔
428
        m_needles.insert(m_string_value);
162✔
429
    }
162✔
430
    auto add_string = [&](const StringData& str) {
264✔
431
        if (m_needles.count(str) == 0) {
264✔
432
            if (str.size()) {
246✔
433
                m_needle_storage.push_back(std::make_unique<char[]>(str.size()));
204✔
434
                std::copy(str.data(), str.data() + str.size(), m_needle_storage.back().get());
204✔
435
                m_needles.insert(StringData(m_needle_storage.back().get(), str.size()));
204✔
436
            }
204✔
437
            else {
42✔
438
                // this code path is different because we need to
439
                // distinguish null from the empty string
440
                m_needles.insert(str);
42✔
441
            }
42✔
442
        }
246✔
443
    };
264✔
444
    if (!other.m_needles.empty()) {
234✔
445
        for (const auto& str : other.m_needles) {
78✔
446
            add_string(str);
78✔
447
        }
78✔
448
    }
48✔
449
    else {
186✔
450
        add_string(other.m_string_value);
186✔
451
    }
186✔
452
    return true;
234✔
453
}
234✔
454

455
size_t StringNode<Equal>::_find_first_local(size_t start, size_t end)
456
{
867,360✔
457
    if (m_needles.empty()) {
867,360✔
458
        return m_leaf->find_first(m_string_value, start, end);
859,113✔
459
    }
859,113✔
460
    else {
8,247✔
461
        if (end == npos)
8,247✔
462
            end = m_leaf->size();
×
463
        REALM_ASSERT_3(start, <=, end);
8,247✔
464
        return find_first_haystack<20>(*m_leaf, m_needles, start, end);
8,247✔
465
    }
8,247✔
466
}
867,360✔
467

468
std::string StringNode<Equal>::describe(util::serializer::SerialisationState& state) const
469
{
6,567✔
470
    if (m_needles.empty()) {
6,567✔
471
        return StringNodeEqualBase::describe(state);
6,477✔
472
    }
6,477✔
473

474
    std::string list_contents;
90✔
475
    bool is_first = true;
90✔
476
    for (auto it : m_needles) {
222✔
477
        StringData sd(it.data(), it.size());
222✔
478
        list_contents += util::format("%1%2", is_first ? "" : ", ", util::serializer::print_value(sd));
222✔
479
        is_first = false;
222✔
480
    }
222✔
481
    std::string col_descr = state.describe_column(ParentNode::m_table, m_condition_column_key);
90✔
482
    std::string desc = util::format("%1 IN {%2}", col_descr, list_contents);
90✔
483
    return desc;
90✔
484
}
6,567✔
485

486

487
void StringNode<EqualIns>::_search_index_init()
488
{
126✔
489
    auto index = ParentNode::m_table->get_search_index(ParentNode::m_condition_column_key);
126✔
490
    m_index_matches.clear();
126✔
491
    constexpr bool case_insensitive = true;
126✔
492
    index->find_all(m_index_matches, StringNodeBase::m_string_value, case_insensitive);
126✔
493
    m_index_evaluator->init(&m_index_matches);
126✔
494
}
126✔
495

496
size_t StringNode<EqualIns>::_find_first_local(size_t start, size_t end)
497
{
1,398✔
498
    EqualIns cond;
1,398✔
499
    for (size_t s = start; s < end; ++s) {
26,214✔
500
        StringData t = get_string(s);
24,936✔
501

502
        if (cond(m_string_value, m_ucase.c_str(), m_lcase.c_str(), t))
24,936✔
503
            return s;
120✔
504
    }
24,936✔
505

506
    return not_found;
1,278✔
507
}
1,398✔
508

509
StringNodeFulltext::StringNodeFulltext(StringData v, ColKey column, std::unique_ptr<LinkMap> lm)
510
    : StringNodeEqualBase(v, column)
171✔
511
    , m_link_map(std::move(lm))
171✔
512
{
342✔
513
    if (!m_link_map)
342✔
514
        m_link_map = std::make_unique<LinkMap>();
330✔
515
}
342✔
516

517
void StringNodeFulltext::table_changed()
518
{
354✔
519
    StringNodeEqualBase::table_changed();
354✔
520
    m_link_map->set_base_table(m_table);
354✔
521
}
354✔
522

523
StringNodeFulltext::StringNodeFulltext(const StringNodeFulltext& other)
524
    : StringNodeEqualBase(other)
234✔
525
{
468✔
526
    m_link_map = std::make_unique<LinkMap>(*other.m_link_map);
468✔
527
}
468✔
528

529
void StringNodeFulltext::_search_index_init()
530
{
378✔
531
    StringIndex* index = m_link_map->get_target_table()->get_string_index(ParentNode::m_condition_column_key);
378✔
532
    REALM_ASSERT(index && index->is_fulltext_index());
378✔
533
    m_index_matches.clear();
378✔
534
    index->find_all_fulltext(m_index_matches, StringNodeBase::m_string_value);
378✔
535

536
    // If links exists, use backlinks to find the original objects
537
    if (m_link_map->links_exist()) {
378✔
538
        std::set<ObjKey> tmp;
12✔
539
        for (auto k : m_index_matches) {
24✔
540
            auto keys = m_link_map->get_origin_objkeys(k);
24✔
541
            tmp.insert(keys.begin(), keys.end());
24✔
542
        }
24✔
543
        m_index_matches.assign(tmp.begin(), tmp.end());
12✔
544
    }
12✔
545

546
    m_index_evaluator = IndexEvaluator{};
378✔
547
    m_index_evaluator->init(&m_index_matches);
378✔
548
}
378✔
549

550
std::unique_ptr<ArrayPayload> TwoColumnsNodeBase::update_cached_leaf_pointers_for_column(Allocator& alloc,
551
                                                                                         const ColKey& col_key)
552
{
58,248✔
553
    switch (col_key.get_type()) {
58,248✔
554
        case col_type_Int:
12,840✔
555
            if (col_key.is_nullable()) {
12,840✔
556
                return std::make_unique<ArrayIntNull>(alloc);
3,444✔
557
            }
3,444✔
558
            return std::make_unique<ArrayInteger>(alloc);
9,396✔
559
        case col_type_Bool:
3,216✔
560
            return std::make_unique<ArrayBoolNull>(alloc);
3,216✔
561
        case col_type_String:
3,456✔
562
            return std::make_unique<ArrayString>(alloc);
3,456✔
563
        case col_type_Binary:
✔
564
            return std::make_unique<ArrayBinary>(alloc);
×
565
        case col_type_Mixed:
3,468✔
566
            return std::make_unique<ArrayMixed>(alloc);
3,468✔
567
        case col_type_Timestamp:
6,552✔
568
            return std::make_unique<ArrayTimestamp>(alloc);
6,552✔
569
        case col_type_Float:
12,606✔
570
            return std::make_unique<ArrayFloatNull>(alloc);
12,606✔
571
        case col_type_Double:
12,654✔
572
            return std::make_unique<ArrayDoubleNull>(alloc);
12,654✔
573
        case col_type_Decimal:
3,456✔
574
            return std::make_unique<ArrayDecimal128>(alloc);
3,456✔
575
        case col_type_Link:
✔
576
            return std::make_unique<ArrayKey>(alloc);
×
577
        case col_type_ObjectId:
✔
578
            return std::make_unique<ArrayObjectIdNull>(alloc);
×
579
        case col_type_UUID:
✔
580
            return std::make_unique<ArrayUUIDNull>(alloc);
×
581
        case col_type_TypedLink:
✔
582
        case col_type_BackLink:
✔
583
            break;
×
584
    };
58,248✔
585
    REALM_UNREACHABLE();
586
    return {};
×
587
}
58,248✔
588

589
size_t size_of_list_from_ref(ref_type ref, Allocator& alloc, ColumnType col_type, bool is_nullable)
590
{
204✔
591
    switch (col_type) {
204✔
592
        case col_type_Int: {
84✔
593
            if (is_nullable) {
84✔
594
                BPlusTree<util::Optional<Int>> list(alloc);
6✔
595
                list.init_from_ref(ref);
6✔
596
                return list.size();
6✔
597
            }
6✔
598
            else {
78✔
599
                BPlusTree<Int> list(alloc);
78✔
600
                list.init_from_ref(ref);
78✔
601
                return list.size();
78✔
602
            }
78✔
603
        }
84✔
604
        case col_type_Bool: {
12✔
605
            BPlusTree<Bool> list(alloc);
12✔
606
            list.init_from_ref(ref);
12✔
607
            return list.size();
12✔
608
        }
84✔
609
        case col_type_String: {
12✔
610
            BPlusTree<String> list(alloc);
12✔
611
            list.init_from_ref(ref);
12✔
612
            return list.size();
12✔
613
        }
84✔
614
        case col_type_Binary: {
12✔
615
            BPlusTree<Binary> list(alloc);
12✔
616
            list.init_from_ref(ref);
12✔
617
            return list.size();
12✔
618
        }
84✔
619
        case col_type_Timestamp: {
12✔
620
            BPlusTree<Timestamp> list(alloc);
12✔
621
            list.init_from_ref(ref);
12✔
622
            return list.size();
12✔
623
        }
84✔
624
        case col_type_Float: {
12✔
625
            BPlusTree<Float> list(alloc);
12✔
626
            list.init_from_ref(ref);
12✔
627
            return list.size();
12✔
628
        }
84✔
629
        case col_type_Double: {
12✔
630
            BPlusTree<Double> list(alloc);
12✔
631
            list.init_from_ref(ref);
12✔
632
            return list.size();
12✔
633
        }
84✔
634
        case col_type_Decimal: {
12✔
635
            BPlusTree<Decimal128> list(alloc);
12✔
636
            list.init_from_ref(ref);
12✔
637
            return list.size();
12✔
638
        }
84✔
639
        case col_type_ObjectId: {
12✔
640
            BPlusTree<ObjectId> list(alloc);
12✔
641
            list.init_from_ref(ref);
12✔
642
            return list.size();
12✔
643
        }
84✔
644
        case col_type_UUID: {
12✔
645
            BPlusTree<UUID> list(alloc);
12✔
646
            list.init_from_ref(ref);
12✔
647
            return list.size();
12✔
648
        }
84✔
649
        case col_type_Mixed: {
✔
650
            BPlusTree<Mixed> list(alloc);
×
651
            list.init_from_ref(ref);
×
652
            return list.size();
×
653
        }
84✔
654
        case col_type_Link: {
12✔
655
            BPlusTree<ObjKey> list(alloc);
12✔
656
            list.init_from_ref(ref);
12✔
657
            return list.size();
12✔
658
        }
84✔
659
        case col_type_TypedLink: {
✔
660
            BPlusTree<ObjLink> list(alloc);
×
661
            list.init_from_ref(ref);
×
662
            return list.size();
×
663
        }
84✔
664
        case col_type_BackLink:
✔
665
            break;
×
666
    }
204✔
667
    REALM_TERMINATE("Unsupported column type.");
668
}
×
669

670
size_t NotNode::find_first_local(size_t start, size_t end)
671
{
18,216✔
672
    if (start <= m_known_range_start && end >= m_known_range_end) {
18,216✔
673
        return find_first_covers_known(start, end);
2,484✔
674
    }
2,484✔
675
    else if (start >= m_known_range_start && end <= m_known_range_end) {
15,732✔
676
        return find_first_covered_by_known(start, end);
15,228✔
677
    }
15,228✔
678
    else if (start < m_known_range_start && end >= m_known_range_start) {
504!
679
        return find_first_overlap_lower(start, end);
×
680
    }
×
681
    else if (start <= m_known_range_end && end > m_known_range_end) {
504✔
682
        return find_first_overlap_upper(start, end);
168✔
683
    }
168✔
684
    else { // start > m_known_range_end || end < m_known_range_start
336✔
685
        return find_first_no_overlap(start, end);
336✔
686
    }
336✔
687
}
18,216✔
688

689
bool NotNode::evaluate_at(size_t rowndx)
690
{
23,904✔
691
    return m_condition->find_first(rowndx, rowndx + 1) == not_found;
23,904✔
692
}
23,904✔
693

694
void NotNode::update_known(size_t start, size_t end, size_t first)
695
{
2,892✔
696
    m_known_range_start = start;
2,892✔
697
    m_known_range_end = end;
2,892✔
698
    m_first_in_known_range = first;
2,892✔
699
}
2,892✔
700

701
size_t NotNode::find_first_loop(size_t start, size_t end)
702
{
20,700✔
703
    for (size_t i = start; i < end; ++i) {
27,288✔
704
        if (evaluate_at(i)) {
23,904✔
705
            return i;
17,316✔
706
        }
17,316✔
707
    }
23,904✔
708
    return not_found;
3,384✔
709
}
20,700✔
710

711
size_t NotNode::find_first_covers_known(size_t start, size_t end)
712
{
2,484✔
713
    // CASE: start-end covers the known range
714
    // [    ######    ]
715
    REALM_ASSERT_DEBUG(start <= m_known_range_start && end >= m_known_range_end);
2,484✔
716
    size_t result = find_first_loop(start, m_known_range_start);
2,484✔
717
    if (result != not_found) {
2,484✔
718
        update_known(start, m_known_range_end, result);
×
719
    }
×
720
    else {
2,484✔
721
        if (m_first_in_known_range != not_found) {
2,484✔
722
            update_known(start, m_known_range_end, m_first_in_known_range);
×
723
            result = m_first_in_known_range;
×
724
        }
×
725
        else {
2,484✔
726
            result = find_first_loop(m_known_range_end, end);
2,484✔
727
            update_known(start, end, result);
2,484✔
728
        }
2,484✔
729
    }
2,484✔
730
    return result;
2,484✔
731
}
2,484✔
732

733
size_t NotNode::find_first_covered_by_known(size_t start, size_t end)
734
{
15,228✔
735
    REALM_ASSERT_DEBUG(start >= m_known_range_start && end <= m_known_range_end);
15,228✔
736
    // CASE: the known range covers start-end
737
    // ###[#####]###
738
    if (m_first_in_known_range != not_found) {
15,228✔
739
        if (m_first_in_known_range > end) {
15,228✔
740
            return not_found;
×
741
        }
×
742
        else if (m_first_in_known_range >= start) {
15,228✔
743
            return m_first_in_known_range;
×
744
        }
×
745
    }
15,228✔
746
    // The first known match is before start, so we can't use the results to improve
747
    // heuristics.
748
    return find_first_loop(start, end);
15,228✔
749
}
15,228✔
750

751
size_t NotNode::find_first_overlap_lower(size_t start, size_t end)
752
{
×
753
    REALM_ASSERT_DEBUG(start < m_known_range_start && end >= m_known_range_start && end <= m_known_range_end);
×
754
    static_cast<void>(end);
×
755
    // CASE: partial overlap, lower end
756
    // [   ###]#####
757
    size_t result;
×
758
    result = find_first_loop(start, m_known_range_start);
×
759
    if (result == not_found) {
×
760
        result = m_first_in_known_range;
×
761
    }
×
762
    update_known(start, m_known_range_end, result);
×
763
    return result < end ? result : not_found;
×
764
}
×
765

766
size_t NotNode::find_first_overlap_upper(size_t start, size_t end)
767
{
168✔
768
    REALM_ASSERT_DEBUG(start <= m_known_range_end && start >= m_known_range_start && end > m_known_range_end);
168✔
769
    // CASE: partial overlap, upper end
770
    // ####[###    ]
771
    size_t result;
168✔
772
    if (m_first_in_known_range != not_found) {
168✔
773
        if (m_first_in_known_range >= start) {
144✔
774
            result = m_first_in_known_range;
×
775
            update_known(m_known_range_start, end, result);
×
776
        }
×
777
        else {
144✔
778
            result = find_first_loop(start, end);
144✔
779
            update_known(m_known_range_start, end, m_first_in_known_range);
144✔
780
        }
144✔
781
    }
144✔
782
    else {
24✔
783
        result = find_first_loop(m_known_range_end, end);
24✔
784
        update_known(m_known_range_start, end, result);
24✔
785
    }
24✔
786
    return result;
168✔
787
}
168✔
788

789
size_t NotNode::find_first_no_overlap(size_t start, size_t end)
790
{
336✔
791
    REALM_ASSERT_DEBUG((start < m_known_range_start && end < m_known_range_start) ||
336!
792
                       (start > m_known_range_end && end > m_known_range_end));
336✔
793
    // CASE: no overlap
794
    // ### [    ]   or    [    ] ####
795
    // if input is a larger range, discard and replace with results.
796
    size_t result = find_first_loop(start, end);
336✔
797
    if (end - start > m_known_range_end - m_known_range_start) {
336✔
798
        update_known(start, end, result);
240✔
799
    }
240✔
800
    return result;
336✔
801
}
336✔
802

803
ExpressionNode::ExpressionNode(std::unique_ptr<Expression> expression)
804
    : m_expression(std::move(expression))
35,580✔
805
{
71,163✔
806
    m_dT = 50.0;
71,163✔
807
}
71,163✔
808

809
void ExpressionNode::table_changed()
810
{
76,515✔
811
    m_expression->set_base_table(m_table);
76,515✔
812
}
76,515✔
813

814
void ExpressionNode::cluster_changed()
815
{
124,734✔
816
    m_expression->set_cluster(m_cluster);
124,734✔
817
}
124,734✔
818

819
void ExpressionNode::init(bool will_query_ranges)
820
{
86,637✔
821
    ParentNode::init(will_query_ranges);
86,637✔
822
    m_dT = m_expression->init();
86,637✔
823
}
86,637✔
824

825
std::string ExpressionNode::describe(util::serializer::SerialisationState& state) const
826
{
21,366✔
827
    if (m_expression) {
21,366✔
828
        return m_expression->description(state);
21,366✔
829
    }
21,366✔
UNCOV
830
    else {
×
UNCOV
831
        return "empty expression";
×
UNCOV
832
    }
×
833
}
21,366✔
834

835
void ExpressionNode::collect_dependencies(std::vector<TableKey>& tables) const
836
{
9,648✔
837
    m_expression->collect_dependencies(tables);
9,648✔
838
}
9,648✔
839

840
size_t ExpressionNode::find_first_local(size_t start, size_t end)
841
{
1,867,278✔
842
    return m_expression->find_first(start, end);
1,867,278✔
843
}
1,867,278✔
844

845
std::unique_ptr<ParentNode> ExpressionNode::clone() const
846
{
72,615✔
847
    return std::unique_ptr<ParentNode>(new ExpressionNode(*this));
72,615✔
848
}
72,615✔
849

850
ExpressionNode::ExpressionNode(const ExpressionNode& from)
851
    : ParentNode(from)
36,309✔
852
    , m_expression(from.m_expression->clone())
36,309✔
853
{
72,621✔
854
}
72,621✔
855

856
template <>
857
size_t LinksToNode<Equal>::find_first_local(size_t start, size_t end)
858
{
6,050,031✔
859
    if (m_condition_column_key.is_collection()) {
6,050,031✔
860
        Allocator& alloc = m_table.unchecked_ptr()->get_alloc();
6,013,284✔
861
        if (m_condition_column_key.is_dictionary()) {
6,013,284✔
862
            auto target_table_key = m_table->get_opposite_table(m_condition_column_key)->get_key();
120✔
863
            Array top(alloc);
120✔
864
            for (size_t i = start; i < end; i++) {
168✔
865
                if (ref_type ref = get_ref(i)) {
144✔
866
                    top.init_from_ref(ref);
144✔
867
                    BPlusTree<Mixed> values(alloc);
144✔
868
                    values.set_parent(&top, 1);
144✔
869
                    values.init_from_parent();
144✔
870
                    for (auto& key : m_target_keys) {
192✔
871
                        ObjLink link(target_table_key, key);
192✔
872
                        if (values.find_first(link) != not_found)
192✔
873
                            return i;
96✔
874
                    }
192✔
875
                }
144✔
876
            }
144✔
877
        }
120✔
878
        else {
6,013,164✔
879
            // LinkLists never contain null
880
            if (!m_target_keys[0] && m_target_keys.size() == 1 && start != end)
6,013,164✔
881
                return not_found;
6✔
882

883
            BPlusTree<ObjKey> links(alloc);
6,013,158✔
884
            for (size_t i = start; i < end; i++) {
6,021,486✔
885
                if (ref_type ref = get_ref(i)) {
6,020,952✔
886
                    links.init_from_ref(ref);
6,016,680✔
887
                    for (auto& key : m_target_keys) {
6,016,692✔
888
                        if (key) {
6,016,692✔
889
                            if (links.find_first(key) != not_found)
6,016,692✔
890
                                return i;
6,012,624✔
891
                        }
6,016,692✔
892
                    }
6,016,692✔
893
                }
6,016,680✔
894
            }
6,020,952✔
895
        }
6,013,158✔
896
    }
6,013,284✔
897
    else if (m_list) {
36,750✔
898
        for (auto& key : m_target_keys) {
36,750✔
899
            auto pos = m_list->find_first(key, start, end);
36,750✔
900
            if (pos != realm::npos) {
36,750✔
901
                return pos;
7,662✔
902
            }
7,662✔
903
        }
36,750✔
904
    }
36,750✔
905

906
    return not_found;
29,643✔
907
}
6,050,031✔
908

909
template <>
910
size_t LinksToNode<NotEqual>::find_first_local(size_t start, size_t end)
911
{
6,756✔
912
    // NotEqual only makes sense for a single value
913
    REALM_ASSERT(m_target_keys.size() == 1);
6,756✔
914
    ObjKey key = m_target_keys[0];
6,756✔
915

916
    if (m_condition_column_key.is_collection()) {
6,756✔
917
        Allocator& alloc = m_table.unchecked_ptr()->get_alloc();
2,418✔
918
        if (m_condition_column_key.is_dictionary()) {
2,418✔
919
            auto target_table_key = m_table->get_opposite_table(m_condition_column_key)->get_key();
36✔
920
            Array top(alloc);
36✔
921
            for (size_t i = start; i < end; i++) {
48✔
922
                if (ref_type ref = get_ref(i)) {
36✔
923
                    top.init_from_ref(ref);
36✔
924
                    BPlusTree<Mixed> values(alloc);
36✔
925
                    values.set_parent(&top, 1);
36✔
926
                    values.init_from_parent();
36✔
927

928
                    ObjLink link(target_table_key, key);
36✔
929
                    bool found = false;
36✔
930
                    values.for_all([&](const Mixed& val) {
48✔
931
                        if (val != link) {
48✔
932
                            found = true;
24✔
933
                        }
24✔
934
                        return !found;
48✔
935
                    });
48✔
936
                    if (found)
36✔
937
                        return i;
24✔
938
                }
36✔
939
            }
36✔
940
        }
36✔
941
        else {
2,382✔
942
            BPlusTree<ObjKey> links(alloc);
2,382✔
943
            for (size_t i = start; i < end; i++) {
4,746✔
944
                if (ref_type ref = get_ref(i)) {
4,512✔
945
                    links.init_from_ref(ref);
2,364✔
946
                    auto sz = links.size();
2,364✔
947
                    for (size_t j = 0; j < sz; j++) {
2,610✔
948
                        if (links.get(j) != key) {
2,394✔
949
                            return i;
2,148✔
950
                        }
2,148✔
951
                    }
2,394✔
952
                }
2,364✔
953
            }
4,512✔
954
        }
2,382✔
955
    }
2,418✔
956
    else if (m_list) {
4,338✔
957
        for (size_t i = start; i < end; i++) {
4,854✔
958
            if (m_list->get(i) != key) {
4,674✔
959
                return i;
4,158✔
960
            }
4,158✔
961
        }
4,674✔
962
    }
4,338✔
963

964
    return not_found;
426✔
965
}
6,756✔
966

967
} // 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