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

realm / realm-core / thomas.goyne_113

27 Oct 2023 10:49AM UTC coverage: 91.596% (+0.03%) from 91.571%
thomas.goyne_113

push

Evergreen

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

Release/13.23.2

91788 of 168244 branches covered (0.0%)

230165 of 251282 relevant lines covered (91.6%)

6444552.92 hits per line

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

89.57
/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,442✔
40
    bool nullable = attr.test(col_attr_Nullable);
908,442✔
41

908,442✔
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,670✔
47
                return std::make_unique<Set<Int>>(*this, col_key);
740,670✔
48
        }
706,620✔
49
        case type_Bool: {
706,620✔
50
            if (nullable)
16,932✔
51
                return std::make_unique<Set<util::Optional<Bool>>>(*this, col_key);
16,932✔
52
            else
34,050✔
53
                return std::make_unique<Set<Bool>>(*this, col_key);
201,822✔
54
        }
201,822✔
55
        case type_Float: {
165,885✔
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,489✔
64
            else
90,489✔
65
                return std::make_unique<Set<Double>>(*this, col_key);
90,489✔
66
        }
✔
67
        case type_String: {
×
68
            return std::make_unique<Set<String>>(*this, col_key);
90,489✔
69
        }
90,489✔
70
        case type_Binary: {
66,831✔
71
            return std::make_unique<Set<Binary>>(*this, col_key);
66,831✔
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,480✔
88
            else
225,480✔
89
                return std::make_unique<Set<UUID>>(*this, col_key);
65,196✔
90
        }
130,452✔
91
        case type_TypedLink: {
130,452✔
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;
23,658✔
102
    }
103
    REALM_TERMINATE("Unsupported column type.");
104
}
105

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

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

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

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

×
133
template <>
6,672✔
134
void Set<ObjKey>::do_insert(size_t ndx, ObjKey target_key)
6,672✔
135
{
3,492✔
136
    auto origin_table = m_obj.get_table();
3,180✔
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});
×
139
    m_tree->insert(ndx, target_key);
6,672✔
140
    if (target_key.is_unresolved()) {
6,672✔
141
        m_tree->set_context_flag(true);
3,492✔
142
    }
3,180✔
143
}
3,180✔
144

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

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

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

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

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

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

32,892✔
177
    m_tree->set_context_flag(false);
×
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
}
60,495✔
186

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

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

195
    m_tree->erase(ndx);
2,412✔
196

2,412✔
197
    if (recurse) {
2,412✔
198
        auto table = m_obj.get_table();
199
        _impl::TableFriend::remove_recursive(*table, state); // Throws
200
    }
426✔
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)) {
207
        auto target_link = value.get<ObjLink>();
208
        m_obj.get_table()->get_parent_group()->validate(target_link);
174✔
209
        m_obj.set_backlink(m_col_key, target_link);
174✔
210
    }
48✔
211
    m_tree->insert(ndx, value);
48✔
212
}
126✔
213

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

323
template <typename Iterator>
324
static bool partition_points(const Set<Mixed>& set, std::vector<size_t>& indices, Iterator& first_string,
42✔
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);
21✔
329
    });
90✔
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) {
42✔
333
        return set.get(i).is_type(type_String);
334
    });
335
    if (first_binary == indices.end() || !set.get(*first_binary).is_type(type_Binary))
36✔
336
        return false;
36✔
337
    end = std::partition_point(first_binary + 1, indices.end(), [&](size_t i) {
18✔
338
        return set.get(i).is_type(type_Binary);
18✔
339
    });
18✔
340
    return true;
18✔
341
}
18✔
342

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

36✔
348
    // The on-disk order is bool -> numbers -> string -> binary -> others
18✔
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
102✔
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
36✔
354
    // in-place merge on the strings and binaries.
355
    std::vector<size_t>::iterator first_string, first_binary, end;
356
    if (partition_points(*this, indices, first_string, first_binary, end)) {
30✔
357
        std::inplace_merge(first_string, first_binary, end, [&](auto a, auto b) {
30✔
358
            return get(a) < get(b);
12✔
359
        });
12✔
360
    }
18✔
361
    if (!ascending) {
18✔
362
        std::reverse(indices.begin(), indices.end());
18✔
363
    }
364
}
365

366
} // namespace realm
30✔
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