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

realm / realm-core / thomas.goyne_111

27 Oct 2023 10:49AM UTC coverage: 91.582% (+0.01%) from 91.571%
thomas.goyne_111

push

Evergreen

web-flow
Merge pull request #7085 from realm/release/13.23.2

Release/13.23.2

91742 of 168234 branches covered (0.0%)

230135 of 251288 relevant lines covered (91.58%)

6778766.91 hits per line

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

89.59
/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
{
38
    auto attr = get_table()->get_column_attr(col_key);
39
    REALM_ASSERT(attr.test(col_attr_Set));
908,250✔
40
    bool nullable = attr.test(col_attr_Nullable);
908,250✔
41

908,250✔
42
    switch (get_table()->get_column_type(col_key)) {
✔
43
        case type_Int: {
×
44
            if (nullable)
×
45
                return std::make_unique<Set<util::Optional<Int>>>(*this, col_key);
×
46
            else
740,622✔
47
                return std::make_unique<Set<Int>>(*this, col_key);
740,622✔
48
        }
706,572✔
49
        case type_Bool: {
706,572✔
50
            if (nullable)
16,944✔
51
                return std::make_unique<Set<util::Optional<Bool>>>(*this, col_key);
16,944✔
52
            else
34,050✔
53
                return std::make_unique<Set<Bool>>(*this, col_key);
201,678✔
54
        }
201,678✔
55
        case type_Float: {
165,741✔
56
            if (nullable)
×
57
                return std::make_unique<Set<util::Optional<Float>>>(*this, col_key);
×
58
            else
×
59
                return std::make_unique<Set<Float>>(*this, col_key);
×
60
        }
61
        case type_Double: {
62
            if (nullable)
63
                return std::make_unique<Set<util::Optional<Double>>>(*this, col_key);
90,492✔
64
            else
90,492✔
65
                return std::make_unique<Set<Double>>(*this, col_key);
90,492✔
66
        }
✔
67
        case type_String: {
×
68
            return std::make_unique<Set<String>>(*this, col_key);
90,492✔
69
        }
90,492✔
70
        case type_Binary: {
66,834✔
71
            return std::make_unique<Set<Binary>>(*this, col_key);
66,834✔
72
        }
11,793✔
73
        case type_Timestamp: {
11,793✔
74
            return std::make_unique<Set<Timestamp>>(*this, col_key);
23,658✔
75
        }
23,658✔
76
        case type_Decimal: {
23,658✔
77
            return std::make_unique<Set<Decimal128>>(*this, col_key);
23,658✔
78
        }
23,658✔
79
        case type_ObjectId: {
23,658✔
80
            if (nullable)
×
81
                return std::make_unique<Set<util::Optional<ObjectId>>>(*this, col_key);
×
82
            else
83
                return std::make_unique<Set<ObjectId>>(*this, col_key);
×
84
        }
×
85
        case type_UUID: {
86
            if (nullable)
87
                return std::make_unique<Set<util::Optional<UUID>>>(*this, col_key);
225,336✔
88
            else
225,336✔
89
                return std::make_unique<Set<UUID>>(*this, col_key);
65,088✔
90
        }
130,308✔
91
        case type_TypedLink: {
130,308✔
92
            return std::make_unique<Set<ObjLink>>(*this, col_key);
47,226✔
93
        }
95,028✔
94
        case type_Mixed: {
71,370✔
95
            return std::make_unique<Set<Mixed>>(*this, col_key);
71,370✔
96
        }
11,793✔
97
        case type_Link: {
11,793✔
98
            return std::make_unique<LnkSet>(*this, col_key);
23,658✔
99
        }
23,658✔
100
        case type_LinkList:
23,658✔
101
            break;
11,793✔
102
    }
23,658✔
103
    REALM_TERMINATE("Unsupported column type.");
104
}
105

106
void SetBase::insert_repl(Replication* repl, size_t index, Mixed value) const
225,336✔
107
{
225,336✔
108
    repl->set_insert(*this, index, value);
136,155✔
109
}
136,155✔
110

136,155✔
111
void SetBase::erase_repl(Replication* repl, size_t index, Mixed value) const
136,155✔
112
{
225,336✔
113
    repl->set_erase(*this, index, value);
225,336✔
114
}
115

116
void SetBase::clear_repl(Replication* repl) const
116,907✔
117
{
116,907✔
118
    repl->set_clear(*this);
116,907✔
119
}
116,907✔
120

58,278✔
121
std::vector<Mixed> SetBase::convert_to_mixed_set(const CollectionBase& rhs)
116,907✔
122
{
7,947✔
123
    std::vector<Mixed> mixed;
7,947✔
124
    mixed.reserve(rhs.size());
3,663✔
125
    for (size_t i = 0; i < rhs.size(); i++) {
4,284✔
126
        mixed.push_back(rhs.get_any(i));
4,284✔
127
    }
×
128
    std::sort(mixed.begin(), mixed.end(), SetElementLessThan<Mixed>());
5,904✔
129
    mixed.erase(std::unique(mixed.begin(), mixed.end()), mixed.end());
5,904✔
130
    return mixed;
3,108✔
131
}
2,796✔
132

2,796✔
133
template <>
×
134
void Set<ObjKey>::do_insert(size_t ndx, ObjKey target_key)
6,672✔
135
{
6,672✔
136
    auto origin_table = m_obj.get_table();
3,492✔
137
    auto target_table_key = origin_table->get_opposite_table_key(m_col_key);
3,180✔
138
    m_obj.set_backlink(m_col_key, {target_table_key, target_key});
3,180✔
139
    m_tree->insert(ndx, target_key);
×
140
    if (target_key.is_unresolved()) {
6,672✔
141
        m_tree->set_context_flag(true);
6,672✔
142
    }
3,492✔
143
}
3,180✔
144

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

6,600✔
153
    bool recurse = m_obj.remove_backlink(m_col_key, {target_table_key, old_key}, state);
6,600✔
154

×
155
    m_tree->erase(ndx);
6,198✔
156

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

2,796✔
163
        // FIXME: Exploit the fact that the values are sorted and unresolved
×
164
        // keys have a negative value.
5,904✔
165
        _impl::check_for_last_unresolved(m_tree.get());
5,904✔
166
    }
3,108✔
167
}
2,796✔
168

2,796✔
169
template <>
×
170
void Set<ObjKey>::do_clear()
✔
171
{
×
172
    size_t ndx = size();
×
173
    while (ndx--) {
10,146✔
174
        do_erase(ndx);
10,146✔
175
    }
×
176

32,892✔
177
    m_tree->set_context_flag(false);
32,892✔
178
}
×
179

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

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

9,456✔
193
    bool recurse = m_obj.remove_backlink(m_col_key, old_link, state);
9,456✔
194

195
    m_tree->erase(ndx);
196

2,412✔
197
    if (recurse) {
2,412✔
198
        auto table = m_obj.get_table();
2,412✔
199
        _impl::TableFriend::remove_recursive(*table, state); // Throws
200
    }
201
}
426✔
202

426✔
203
template <>
426✔
204
void Set<Mixed>::do_insert(size_t ndx, Mixed value)
426✔
205
{
426✔
206
    if (value.is_type(type_TypedLink)) {
426✔
207
        auto target_link = value.get<ObjLink>();
208
        m_obj.get_table()->get_parent_group()->validate(target_link);
209
        m_obj.set_backlink(m_col_key, target_link);
174✔
210
    }
174✔
211
    m_tree->insert(ndx, value);
48✔
212
}
48✔
213

126✔
214
template <>
126✔
215
void Set<Mixed>::do_erase(size_t ndx)
126✔
216
{
217
    if (Mixed old_value = get(ndx); old_value.is_type(type_TypedLink)) {
218
        auto old_link = old_value.get<ObjLink>();
219

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

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

30✔
226
        if (recurse) {
6!
227
            auto table = m_obj.get_table();
6✔
228
            _impl::TableFriend::remove_recursive(*table, state); // Throws
24✔
229
        }
24✔
230
    }
24✔
231
    else {
232
        m_tree->erase(ndx);
233
    }
36✔
234
}
36✔
235

24✔
236
template <>
24✔
237
void Set<Mixed>::do_clear()
12✔
238
{
12✔
239
    size_t ndx = size();
12✔
240
    while (ndx--) {
241
        do_erase(ndx);
242
    }
243
}
54✔
244

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

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

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

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

42✔
282
bool LnkSet::is_subset_of(const CollectionBase& rhs) const
72✔
283
{
284
    return this->m_set.is_subset_of(rhs);
285
}
162✔
286

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

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

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

302
bool LnkSet::intersects(const CollectionBase& rhs) const
303
{
304
    return this->m_set.intersects(rhs);
60✔
305
}
60✔
306

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

120✔
312
void set_sorted_indices(size_t sz, std::vector<size_t>& indices, bool ascending)
60✔
313
{
314
    indices.resize(sz);
315
    if (ascending) {
42✔
316
        std::iota(indices.begin(), indices.end(), 0);
42✔
317
    }
30✔
318
    else {
30✔
319
        std::iota(indices.rbegin(), indices.rend(), 0);
12✔
320
    }
12✔
321
}
12✔
322

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

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

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