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

realm / realm-core / github_pull_request_275914

25 Sep 2023 03:10PM UTC coverage: 92.915% (+1.7%) from 91.215%
github_pull_request_275914

Pull #6073

Evergreen

jedelbo
Merge tag 'v13.21.0' into next-major

"Feature/Bugfix release"
Pull Request #6073: Merge next-major

96928 of 177706 branches covered (0.0%)

8324 of 8714 new or added lines in 122 files covered. (95.52%)

181 existing lines in 28 files now uncovered.

247505 of 266379 relevant lines covered (92.91%)

7164945.17 hits per line

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

98.29
/src/realm/set.cpp
1
/*************************************************************************
2
 *
3
 * Copyright 2020 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

20
#include "realm/set.hpp"
21
#include "realm/array_basic.hpp"
22
#include "realm/array_integer.hpp"
23
#include "realm/array_bool.hpp"
24
#include "realm/array_string.hpp"
25
#include "realm/array_binary.hpp"
26
#include "realm/array_timestamp.hpp"
27
#include "realm/array_decimal128.hpp"
28
#include "realm/array_fixed_bytes.hpp"
29
#include "realm/array_typed_link.hpp"
30
#include "realm/array_mixed.hpp"
31
#include "realm/replication.hpp"
32

33
namespace realm {
34

35
// FIXME: This method belongs in obj.cpp.
36
SetBasePtr Obj::get_setbase_ptr(ColKey col_key) const
37
{
58,266✔
38
    auto attr = get_table()->get_column_attr(col_key);
88,656✔
39
    REALM_ASSERT(attr.test(col_attr_Set));
88,656✔
40
    bool nullable = attr.test(col_attr_Nullable);
88,656✔
41

58,266✔
42
    switch (get_table()->get_column_type(col_key)) {
58,266✔
43
        case type_Int: {
8,619✔
44
            if (nullable)
8,619✔
45
                return std::make_unique<Set<util::Optional<Int>>>(*this, col_key);
6,477✔
46
            else
2,142✔
47
                return std::make_unique<Set<Int>>(*this, col_key);
2,142✔
48
        }
1,206✔
49
        case type_Bool: {
4,158✔
50
            if (nullable)
4,158✔
51
                return std::make_unique<Set<util::Optional<Bool>>>(*this, col_key);
1,554✔
52
            else
1,398✔
53
                return std::make_unique<Set<Bool>>(*this, col_key);
1,659✔
54
        }
261✔
55
        case type_Float: {
3,597✔
56
            if (nullable)
4,713✔
57
                return std::make_unique<Set<util::Optional<Float>>>(*this, col_key);
2,862✔
58
            else
2,706✔
59
                return std::make_unique<Set<Float>>(*this, col_key);
1,851✔
60
        }
261✔
61
        case type_Double: {
3,597✔
62
            if (nullable)
3,597✔
63
                return std::make_unique<Set<util::Optional<Double>>>(*this, col_key);
1,746✔
64
            else
1,590✔
65
                return std::make_unique<Set<Double>>(*this, col_key);
1,590✔
66
        }
67
        case type_String: {
3,486✔
68
            return std::make_unique<Set<String>>(*this, col_key);
3,486✔
69
        }
150✔
70
        case type_Binary: {
3,309✔
71
            return std::make_unique<Set<Binary>>(*this, col_key);
3,309✔
72
        }
45✔
73
        case type_Timestamp: {
3,300✔
74
            return std::make_unique<Set<Timestamp>>(*this, col_key);
3,450✔
75
        }
150✔
76
        case type_Decimal: {
3,402✔
77
            return std::make_unique<Set<Decimal128>>(*this, col_key);
3,252✔
78
        }
3✔
79
        case type_ObjectId: {
7,539✔
80
            if (nullable)
7,539✔
81
                return std::make_unique<Set<util::Optional<ObjectId>>>(*this, col_key);
5,991✔
82
            else
1,401✔
83
                return std::make_unique<Set<ObjectId>>(*this, col_key);
1,548✔
84
        }
150✔
85
        case type_UUID: {
3,102✔
86
            if (nullable)
3,105✔
87
                return std::make_unique<Set<util::Optional<UUID>>>(*this, col_key);
1,704✔
88
            else
1,593✔
89
                return std::make_unique<Set<UUID>>(*this, col_key);
1,443✔
90
        }
150✔
91
        case type_TypedLink: {
92
            return std::make_unique<Set<ObjLink>>(*this, col_key);
93
        }
94
        case type_Mixed: {
5,067✔
95
            return std::make_unique<Set<Mixed>>(*this, col_key);
5,067✔
96
        }
8,553✔
97
        case type_Link: {
24,903✔
98
            return std::make_unique<LnkSet>(*this, col_key);
24,903✔
99
        }
8,553✔
100
        case type_LinkList:
8,553✔
101
            break;
8,553✔
102
    }
27✔
103
    REALM_TERMINATE("Unsupported column type.");
27✔
104
}
8,553✔
105

106
void SetBase::insert_repl(Replication* repl, size_t index, Mixed value) const
107
{
30,114✔
108
    repl->set_insert(*this, index, value);
32,910✔
109
}
32,910✔
110

2,796✔
111
void SetBase::erase_repl(Replication* repl, size_t index, Mixed value) const
2,796✔
112
{
7,518✔
113
    repl->set_erase(*this, index, value);
4,728✔
114
}
7,524✔
115

116
void SetBase::clear_repl(Replication* repl) const
2,796✔
117
{
1,206✔
118
    repl->set_clear(*this);
4,002✔
119
}
1,206✔
120

121
std::vector<Mixed> SetBase::convert_to_mixed_set(const CollectionBase& rhs)
2,796✔
122
{
261✔
123
    std::vector<Mixed> mixed;
261✔
124
    mixed.reserve(rhs.size());
261✔
125
    for (size_t i = 0; i < rhs.size(); i++) {
1,377✔
126
        mixed.push_back(rhs.get_any(i));
1,122✔
127
    }
1,122✔
128
    std::sort(mixed.begin(), mixed.end(), SetElementLessThan<Mixed>());
3,057✔
129
    mixed.erase(std::unique(mixed.begin(), mixed.end()), mixed.end());
261✔
130
    return mixed;
261✔
131
}
261✔
132

462✔
133
template <>
462✔
134
void Set<ObjKey>::do_insert(size_t ndx, ObjKey target_key)
2,376✔
135
{
10,287✔
136
    auto origin_table = m_obj.get_table();
10,287✔
137
    auto target_table_key = origin_table->get_opposite_table_key(m_col_key);
8,373✔
138
    m_obj.set_backlink(m_col_key, {target_table_key, target_key});
8,835✔
139
    m_tree->insert(ndx, target_key);
8,835✔
140
    if (target_key.is_unresolved()) {
8,373✔
141
        m_tree->set_context_flag(true);
27✔
142
    }
27✔
143
}
8,373✔
144

145
template <>
146
void Set<ObjKey>::do_erase(size_t ndx)
147
{
2,796✔
148
    auto origin_table = m_obj.get_table();
2,796✔
149
    auto target_table_key = origin_table->get_opposite_table_key(m_col_key);
2,796✔
150
    ObjKey old_key = get(ndx);
2,796✔
151
    CascadeState state(old_key.is_unresolved() ? CascadeState::Mode::All : CascadeState::Mode::Strong);
2,796✔
152

2,796!
153
    bool recurse = m_obj.remove_backlink(m_col_key, {target_table_key, old_key}, state);
2,796✔
154

2,796✔
155
    m_tree->erase(ndx);
2,796✔
156

2,796✔
157
    if (recurse) {
2,796✔
UNCOV
158
        _impl::TableFriend::remove_recursive(*origin_table, state); // Throws
×
NEW
159
    }
×
160
    if (old_key.is_unresolved()) {
2,796✔
161
        // We might have removed the last unresolved link - check it
6✔
162

6✔
163
        // FIXME: Exploit the fact that the values are sorted and unresolved
6✔
164
        // keys have a negative value.
6✔
165
        _impl::check_for_last_unresolved(m_tree.get());
6✔
166
    }
4,278✔
167
}
7,068✔
168

4,272✔
169
template <>
2,199✔
170
void Set<ObjKey>::do_clear()
2,199✔
171
{
2,661✔
172
    size_t ndx = size();
2,661✔
173
    while (ndx--) {
6,648✔
174
        do_erase(ndx);
6,186✔
175
    }
1,914✔
176

462✔
177
    m_tree->set_context_flag(false);
462✔
178
}
2,271✔
179

1,809✔
180
template <>
516✔
181
void Set<ObjLink>::do_insert(size_t ndx, ObjLink target_link)
182
{
15✔
183
    m_obj.set_backlink(m_col_key, target_link);
531✔
184
    m_tree->insert(ndx, target_link);
531✔
185
}
15✔
186

516✔
187
template <>
188
void Set<ObjLink>::do_erase(size_t ndx)
516✔
189
{
3✔
190
    ObjLink old_link = get(ndx);
3✔
191
    CascadeState state(old_link.get_obj_key().is_unresolved() ? CascadeState::Mode::All : CascadeState::Mode::Strong);
3✔
192

519✔
193
    bool recurse = m_obj.remove_backlink(m_col_key, old_link, state);
1,296✔
194

1,296✔
195
    m_tree->erase(ndx);
1,296✔
196

1,812✔
197
    if (recurse) {
3✔
198
        auto table = m_obj.get_table();
199
        _impl::TableFriend::remove_recursive(*table, state); // Throws
200
    }
126✔
201
}
129✔
202

1,146✔
203
template <>
1,020✔
204
void Set<Mixed>::do_insert(size_t ndx, Mixed value)
1,020✔
205
{
4,353✔
206
    if (value.is_type(type_TypedLink)) {
4,227✔
207
        auto target_link = value.get<ObjLink>();
2,196✔
208
        m_obj.get_table()->get_parent_group()->validate(target_link);
2,196✔
209
        m_obj.set_backlink(m_col_key, target_link);
2,199✔
210
    }
2,196✔
211
    m_tree->insert(ndx, value);
4,230✔
212
}
4,239✔
213

12✔
214
template <>
3✔
215
void Set<Mixed>::do_erase(size_t ndx)
3✔
216
{
1,812✔
217
    if (Mixed old_value = get(ndx); old_value.is_type(type_TypedLink)) {
1,821✔
218
        auto old_link = old_value.get<ObjLink>();
516✔
219

534✔
220
        CascadeState state(old_link.get_obj_key().is_unresolved() ? CascadeState::Mode::All
531✔
221
                                                                  : CascadeState::Mode::Strong);
522✔
222
        bool recurse = m_obj.remove_backlink(m_col_key, old_link, state);
522✔
223

522✔
224
        m_tree->erase(ndx);
522✔
225

522✔
226
        if (recurse) {
531✔
227
            auto table = m_obj.get_table();
3✔
228
            _impl::TableFriend::remove_recursive(*table, state); // Throws
229
        }
230
    }
516✔
231
    else {
1,293✔
232
        m_tree->erase(ndx);
1,293✔
233
    }
1,293✔
234
}
1,809✔
235

236
template <>
237
void Set<Mixed>::do_clear()
238
{
381✔
239
    size_t ndx = size();
381✔
240
    while (ndx--) {
1,401✔
241
        do_erase(ndx);
1,275✔
242
    }
1,275✔
243
}
126✔
244

245
template <>
12✔
246
void Set<Mixed>::migrate()
12✔
247
{
15✔
248
    // We should just move all string values to be before the binary values
3✔
249
    size_t first_binary = size();
3✔
250
    for (size_t n = 0; n < size(); n++) {
12✔
251
        if (m_tree->get(n).is_type(type_Binary)) {
12✔
252
            first_binary = n;
3✔
253
            break;
3✔
254
        }
3✔
255
    }
12✔
256

3✔
257
    for (size_t n = first_binary; n < size(); n++) {
18✔
258
        if (m_tree->get(n).is_type(type_String)) {
15✔
259
            m_tree->insert(first_binary, Mixed());
6✔
260
            m_tree->swap(n + 1, first_binary);
6✔
261
            m_tree->erase(n + 1);
6✔
262
            first_binary++;
6✔
263
        }
6✔
264
    }
15✔
265
}
21✔
266

18✔
267
void LnkSet::remove_target_row(size_t link_ndx)
18✔
268
{
269
    // Deleting the object will automatically remove all links
270
    // to it. So we do not have to manually remove the deleted link
×
271
    ObjKey k = get(link_ndx);
×
272
    get_target_table()->remove_object(k);
×
273
}
274

275
void LnkSet::remove_all_target_rows()
276
{
291✔
277
    if (m_set.update()) {
291✔
278
        _impl::TableFriend::batch_erase_rows(*get_target_table(), *m_set.m_tree);
255✔
279
    }
291✔
280
}
291✔
281

282
bool LnkSet::is_subset_of(const CollectionBase& rhs) const
36✔
283
{
84✔
284
    return this->m_set.is_subset_of(rhs);
48✔
285
}
24✔
286

36✔
287
bool LnkSet::is_strict_subset_of(const CollectionBase& rhs) const
36✔
288
{
36✔
289
    return this->m_set.is_strict_subset_of(rhs);
290
}
36✔
291

36✔
292
bool LnkSet::is_superset_of(const CollectionBase& rhs) const
36✔
293
{
294
    return this->m_set.is_superset_of(rhs);
295
}
296

15,939✔
297
bool LnkSet::is_strict_superset_of(const CollectionBase& rhs) const
15,939✔
298
{
15,939✔
299
    return this->m_set.is_strict_superset_of(rhs);
15,813✔
300
}
15,813✔
301

126✔
302
bool LnkSet::intersects(const CollectionBase& rhs) const
126✔
303
{
144✔
304
    return this->m_set.intersects(rhs);
15,957✔
305
}
18✔
306

307
bool LnkSet::set_equals(const CollectionBase& rhs) const
308
{
309
    return this->m_set.set_equals(rhs);
891✔
310
}
843✔
311

843✔
312
void set_sorted_indices(size_t sz, std::vector<size_t>& indices, bool ascending)
843✔
313
{
16,830✔
314
    indices.resize(sz);
16,797✔
315
    if (ascending) {
16,014✔
316
        std::iota(indices.begin(), indices.end(), 0);
15,888✔
317
    }
15,888✔
318
    else {
159✔
319
        std::iota(indices.rbegin(), indices.rend(), 0);
141✔
320
    }
162✔
321
}
15,975✔
322

36✔
323
template <typename Iterator>
18✔
324
static bool partition_points(const Set<Mixed>& set, std::vector<size_t>& indices, Iterator& first_string,
18✔
325
                             Iterator& first_binary, Iterator& end)
326
{
891✔
327
    first_string = std::partition_point(indices.begin(), indices.end(), [&](size_t i) {
891✔
328
        return set.get(i).is_type(type_Bool, type_Int, type_Float, type_Double, type_Decimal);
1,734✔
329
    });
1,734✔
330
    if (first_string == indices.end() || !set.get(*first_string).is_type(type_String))
891✔
331
        return false;
858✔
332
    first_binary = std::partition_point(first_string + 1, indices.end(), [&](size_t i) {
75✔
333
        return set.get(i).is_type(type_String);
75✔
334
    });
75✔
335
    if (first_binary == indices.end() || !set.get(*first_binary).is_type(type_Binary))
33✔
336
        return false;
15✔
337
    end = std::partition_point(first_binary + 1, indices.end(), [&](size_t i) {
36✔
338
        return set.get(i).is_type(type_Binary);
927✔
339
    });
927✔
340
    return true;
54✔
341
}
54✔
342

36✔
343
template <>
18✔
344
void Set<Mixed>::sort(std::vector<size_t>& indices, bool ascending) const
891✔
345
{
909✔
346
    set_sorted_indices(size(), indices, true);
909✔
347

1,782✔
348
    // The on-disk order is bool -> numbers -> string -> binary -> others
891✔
349
    // We want to merge the string and binary sections to match the sort order
891✔
350
    // of other collections. To do this we find the three partition points
891✔
351
    // where the first string occurs, the first binary occurs, and the first
891✔
352
    // non-binary after binaries occurs. If there's no strings or binaries we
891✔
353
    // don't have to do anything. If they're both non-empty, we perform an
891✔
354
    // in-place merge on the strings and binaries.
891✔
355
    std::vector<size_t>::iterator first_string, first_binary, end;
891✔
356
    if (partition_points(*this, indices, first_string, first_binary, end)) {
891✔
357
        std::inplace_merge(first_string, first_binary, end, [&](auto a, auto b) {
60✔
358
            return get(a) < get(b);
60✔
359
        });
60✔
360
    }
18✔
361
    if (!ascending) {
891✔
362
        std::reverse(indices.begin(), indices.end());
18✔
363
    }
18✔
364
}
891✔
365

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