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

realm / realm-core / nicola.cabiddu_1042

27 Sep 2023 06:04PM UTC coverage: 91.085% (-1.8%) from 92.915%
nicola.cabiddu_1042

Pull #6766

Evergreen

nicola-cab
Fix logic for dictionaries
Pull Request #6766: Client Reset for collections in mixed / nested collections

97276 of 178892 branches covered (0.0%)

1994 of 2029 new or added lines in 7 files covered. (98.28%)

4556 existing lines in 112 files now uncovered.

237059 of 260260 relevant lines covered (91.09%)

6321099.55 hits per line

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

85.23
/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
/********************************** SetBase *********************************/
36

37
void SetBase::insert_repl(Replication* repl, size_t index, Mixed value) const
38
{
60,720✔
39
    repl->set_insert(*this, index, value);
60,720✔
40
}
60,720✔
41

42
void SetBase::erase_repl(Replication* repl, size_t index, Mixed value) const
43
{
9,480✔
44
    repl->set_erase(*this, index, value);
9,480✔
45
}
9,480✔
46

47
void SetBase::clear_repl(Replication* repl) const
48
{
2,430✔
49
    repl->set_clear(*this);
2,430✔
50
}
2,430✔
51

52
std::vector<Mixed> SetBase::convert_to_mixed_set(const CollectionBase& rhs)
53
{
522✔
54
    std::vector<Mixed> mixed;
522✔
55
    mixed.reserve(rhs.size());
522✔
56
    for (size_t i = 0; i < rhs.size(); i++) {
2,754✔
57
        mixed.push_back(rhs.get_any(i));
2,232✔
58
    }
2,232✔
59
    std::sort(mixed.begin(), mixed.end(), SetElementLessThan<Mixed>());
522✔
60
    mixed.erase(std::unique(mixed.begin(), mixed.end()), mixed.end());
522✔
61
    return mixed;
522✔
62
}
522✔
63

64
template <>
65
void CollectionBaseImpl<SetBase>::to_json(std::ostream& out, size_t, JSONOutputMode output_mode,
66
                                          util::FunctionRef<void(const Mixed&)> fn) const
67
{
300✔
68
    int closing = 0;
300✔
69
    if (output_mode == output_mode_xjson_plus) {
300✔
70
        out << "{ \"$set\": ";
90✔
71
        closing++;
90✔
72
    }
90✔
73

150✔
74
    out << "[";
300✔
75
    auto sz = size();
300✔
76
    for (size_t i = 0; i < sz; i++) {
606✔
77
        if (i > 0)
306✔
78
            out << ",";
6✔
79
        Mixed val = get_any(i);
306✔
80
        if (val.is_type(type_TypedLink)) {
306✔
81
            fn(val);
6✔
82
        }
6✔
83
        else {
300✔
84
            val.to_json(out, output_mode);
300✔
85
        }
300✔
86
    }
306✔
87
    out << "]";
300✔
88
    while (closing--)
390✔
89
        out << "}";
90✔
90
}
300✔
91

92
/********************************* Set<Key> *********************************/
93

94
template <>
95
void Set<ObjKey>::do_insert(size_t ndx, ObjKey target_key)
96
{
16,926✔
97
    auto origin_table = get_table_unchecked();
16,926✔
98
    auto target_table_key = origin_table->get_opposite_table_key(m_col_key);
16,926✔
99
    set_backlink(m_col_key, {target_table_key, target_key});
16,926✔
100
    m_tree->insert(ndx, target_key);
16,926✔
101
    if (target_key.is_unresolved()) {
16,926✔
102
        m_tree->set_context_flag(true);
54✔
103
    }
54✔
104
}
16,926✔
105

106
template <>
107
void Set<ObjKey>::do_erase(size_t ndx)
108
{
5,592✔
109
    auto origin_table = get_table_unchecked();
5,592✔
110
    auto target_table_key = origin_table->get_opposite_table_key(m_col_key);
5,592✔
111
    ObjKey old_key = get(ndx);
5,592✔
112
    CascadeState state(old_key.is_unresolved() ? CascadeState::Mode::All : CascadeState::Mode::Strong);
5,586✔
113

2,796✔
114
    bool recurse = remove_backlink(m_col_key, {target_table_key, old_key}, state);
5,592✔
115

2,796✔
116
    m_tree->erase(ndx);
5,592✔
117

2,796✔
118
    if (recurse) {
5,592✔
UNCOV
119
        _impl::TableFriend::remove_recursive(*origin_table, state); // Throws
×
UNCOV
120
    }
×
121
    if (old_key.is_unresolved()) {
5,592✔
122
        // We might have removed the last unresolved link - check it
6✔
123

6✔
124
        // FIXME: Exploit the fact that the values are sorted and unresolved
6✔
125
        // keys have a negative value.
6✔
126
        _impl::check_for_last_unresolved(m_tree.get());
12✔
127
    }
12✔
128
}
5,592✔
129

130
template <>
131
void Set<ObjKey>::do_clear()
132
{
924✔
133
    size_t ndx = size();
924✔
134
    while (ndx--) {
4,752✔
135
        do_erase(ndx);
3,828✔
136
    }
3,828✔
137

462✔
138
    m_tree->set_context_flag(false);
924✔
139
}
924✔
140

141
template <>
142
void Set<ObjLink>::do_insert(size_t ndx, ObjLink target_link)
UNCOV
143
{
×
UNCOV
144
    set_backlink(m_col_key, target_link);
×
UNCOV
145
    m_tree->insert(ndx, target_link);
×
UNCOV
146
}
×
147

148
template <>
149
void Set<ObjLink>::do_erase(size_t ndx)
UNCOV
150
{
×
UNCOV
151
    ObjLink old_link = get(ndx);
×
UNCOV
152
    CascadeState state(old_link.get_obj_key().is_unresolved() ? CascadeState::Mode::All : CascadeState::Mode::Strong);
×
153

UNCOV
154
    bool recurse = remove_backlink(m_col_key, old_link, state);
×
155

UNCOV
156
    m_tree->erase(ndx);
×
157

158
    if (recurse) {
×
159
        auto table = get_table_unchecked();
×
UNCOV
160
        _impl::TableFriend::remove_recursive(*table, state); // Throws
×
UNCOV
161
    }
×
UNCOV
162
}
×
163

164
template <>
165
void Set<Mixed>::do_insert(size_t ndx, Mixed value)
166
{
8,712✔
167
    REALM_ASSERT(!value.is_type(type_Link));
8,712✔
168
    if (value.is_type(type_TypedLink)) {
8,712✔
169
        auto target_link = value.get<ObjLink>();
4,398✔
170
        get_table_unchecked()->get_parent_group()->validate(target_link);
4,398✔
171
        set_backlink(m_col_key, target_link);
4,398✔
172
    }
4,398✔
173
    m_tree->insert(ndx, value);
8,712✔
174
}
8,712✔
175

176
template <>
177
void Set<Mixed>::do_erase(size_t ndx)
178
{
3,672✔
179
    if (Mixed old_value = get(ndx); old_value.is_type(type_TypedLink)) {
3,672✔
180
        auto old_link = old_value.get<ObjLink>();
1,032✔
181

516✔
182
        CascadeState state(old_link.get_obj_key().is_unresolved() ? CascadeState::Mode::All
516✔
183
                                                                  : CascadeState::Mode::Strong);
1,032✔
184
        bool recurse = remove_backlink(m_col_key, old_link, state);
1,032✔
185

516✔
186
        m_tree->erase(ndx);
1,032✔
187

516✔
188
        if (recurse) {
1,032✔
UNCOV
189
            auto table = get_table_unchecked();
×
UNCOV
190
            _impl::TableFriend::remove_recursive(*table, state); // Throws
×
UNCOV
191
        }
×
192
    }
1,032✔
193
    else {
2,640✔
194
        m_tree->erase(ndx);
2,640✔
195
    }
2,640✔
196
}
3,672✔
197

198
template <>
199
void Set<Mixed>::do_clear()
200
{
270✔
201
    size_t ndx = size();
270✔
202
    while (ndx--) {
2,346✔
203
        do_erase(ndx);
2,076✔
204
    }
2,076✔
205
}
270✔
206

207
template <>
208
void Set<Mixed>::migrate()
209
{
6✔
210
    // We should just move all string values to be before the binary values
3✔
211
    size_t first_binary = size();
6✔
212
    for (size_t n = 0; n < size(); n++) {
24✔
213
        if (m_tree->get(n).is_type(type_Binary)) {
24✔
214
            first_binary = n;
6✔
215
            break;
6✔
216
        }
6✔
217
    }
24✔
218

3✔
219
    for (size_t n = first_binary; n < size(); n++) {
36✔
220
        if (m_tree->get(n).is_type(type_String)) {
30✔
221
            m_tree->insert(first_binary, Mixed());
12✔
222
            m_tree->swap(n + 1, first_binary);
12✔
223
            m_tree->erase(n + 1);
12✔
224
            first_binary++;
12✔
225
        }
12✔
226
    }
30✔
227
}
6✔
228

229
void LnkSet::remove_target_row(size_t link_ndx)
UNCOV
230
{
×
231
    // Deleting the object will automatically remove all links
232
    // to it. So we do not have to manually remove the deleted link
UNCOV
233
    ObjKey k = get(link_ndx);
×
UNCOV
234
    get_target_table()->remove_object(k);
×
UNCOV
235
}
×
236

237
void LnkSet::remove_all_target_rows()
238
{
510✔
239
    if (m_set.update()) {
510✔
240
        _impl::TableFriend::batch_erase_rows(*get_target_table(), *m_set.m_tree);
510✔
241
    }
510✔
242
}
510✔
243

244
bool LnkSet::is_subset_of(const CollectionBase& rhs) const
245
{
24✔
246
    return this->m_set.is_subset_of(rhs);
24✔
247
}
24✔
248

249
bool LnkSet::is_strict_subset_of(const CollectionBase& rhs) const
UNCOV
250
{
×
UNCOV
251
    return this->m_set.is_strict_subset_of(rhs);
×
UNCOV
252
}
×
253

254
bool LnkSet::is_superset_of(const CollectionBase& rhs) const
UNCOV
255
{
×
UNCOV
256
    return this->m_set.is_superset_of(rhs);
×
UNCOV
257
}
×
258

259
bool LnkSet::is_strict_superset_of(const CollectionBase& rhs) const
UNCOV
260
{
×
UNCOV
261
    return this->m_set.is_strict_superset_of(rhs);
×
UNCOV
262
}
×
263

264
bool LnkSet::intersects(const CollectionBase& rhs) const
265
{
36✔
266
    return this->m_set.intersects(rhs);
36✔
267
}
36✔
268

269
bool LnkSet::set_equals(const CollectionBase& rhs) const
270
{
×
271
    return this->m_set.set_equals(rhs);
×
272
}
×
273

274
void LnkSet::to_json(std::ostream& out, size_t link_depth, JSONOutputMode output_mode,
275
                     util::FunctionRef<void(const Mixed&)> fn) const
276
{
72✔
277
    auto [open_str, close_str] = get_open_close_strings(link_depth, output_mode);
72✔
278

36✔
279
    out << open_str;
72✔
280
    out << "[";
72✔
281

36✔
282
    auto sz = m_set.size();
72✔
283
    for (size_t i = 0; i < sz; i++) {
144✔
284
        if (i > 0)
72✔
285
            out << ",";
24✔
286
        Mixed val(m_set.get(i));
72✔
287
        fn(val);
72✔
288
    }
72✔
289

36✔
290
    out << "]";
72✔
291
    out << close_str;
72✔
292
}
72✔
293

294

295
void set_sorted_indices(size_t sz, std::vector<size_t>& indices, bool ascending)
296
{
31,938✔
297
    indices.resize(sz);
31,938✔
298
    if (ascending) {
31,938✔
299
        std::iota(indices.begin(), indices.end(), 0);
31,686✔
300
    }
31,686✔
301
    else {
252✔
302
        std::iota(indices.rbegin(), indices.rend(), 0);
252✔
303
    }
252✔
304
}
31,938✔
305

306
template <typename Iterator>
307
static bool partition_points(const Set<Mixed>& set, std::vector<size_t>& indices, Iterator& first_string,
308
                             Iterator& first_binary, Iterator& end)
309
{
1,842✔
310
    first_string = std::partition_point(indices.begin(), indices.end(), [&](size_t i) {
1,791✔
311
        return set.get(i).is_type(type_Bool, type_Int, type_Float, type_Double, type_Decimal);
1,740✔
312
    });
1,740✔
313
    if (first_string == indices.end() || !set.get(*first_string).is_type(type_String))
1,842✔
314
        return false;
1,752✔
315
    first_binary = std::partition_point(first_string + 1, indices.end(), [&](size_t i) {
174✔
316
        return set.get(i).is_type(type_String);
174✔
317
    });
174✔
318
    if (first_binary == indices.end() || !set.get(*first_binary).is_type(type_Binary))
90✔
319
        return false;
54✔
320
    end = std::partition_point(first_binary + 1, indices.end(), [&](size_t i) {
72✔
321
        return set.get(i).is_type(type_Binary);
72✔
322
    });
72✔
323
    return true;
36✔
324
}
36✔
325

326
template <>
327
void Set<Mixed>::sort(std::vector<size_t>& indices, bool ascending) const
328
{
1,842✔
329
    set_sorted_indices(size(), indices, true);
1,842✔
330

921✔
331
    // The on-disk order is bool -> numbers -> string -> binary -> others
921✔
332
    // We want to merge the string and binary sections to match the sort order
921✔
333
    // of other collections. To do this we find the three partition points
921✔
334
    // where the first string occurs, the first binary occurs, and the first
921✔
335
    // non-binary after binaries occurs. If there's no strings or binaries we
921✔
336
    // don't have to do anything. If they're both non-empty, we perform an
921✔
337
    // in-place merge on the strings and binaries.
921✔
338
    std::vector<size_t>::iterator first_string, first_binary, end;
1,842✔
339
    if (partition_points(*this, indices, first_string, first_binary, end)) {
1,842✔
340
        std::inplace_merge(first_string, first_binary, end, [&](auto a, auto b) {
72✔
341
            return get(a) < get(b);
72✔
342
        });
72✔
343
    }
36✔
344
    if (!ascending) {
1,842✔
345
        std::reverse(indices.begin(), indices.end());
36✔
346
    }
36✔
347
}
1,842✔
348

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