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

realm / realm-core / github_pull_request_312964

19 Feb 2025 07:31PM UTC coverage: 90.814% (-0.3%) from 91.119%
github_pull_request_312964

Pull #8071

Evergreen

web-flow
Bump serialize-javascript and mocha

Bumps [serialize-javascript](https://github.com/yahoo/serialize-javascript) to 6.0.2 and updates ancestor dependency [mocha](https://github.com/mochajs/mocha). These dependencies need to be updated together.


Updates `serialize-javascript` from 6.0.0 to 6.0.2
- [Release notes](https://github.com/yahoo/serialize-javascript/releases)
- [Commits](https://github.com/yahoo/serialize-javascript/compare/v6.0.0...v6.0.2)

Updates `mocha` from 10.2.0 to 10.8.2
- [Release notes](https://github.com/mochajs/mocha/releases)
- [Changelog](https://github.com/mochajs/mocha/blob/main/CHANGELOG.md)
- [Commits](https://github.com/mochajs/mocha/compare/v10.2.0...v10.8.2)

---
updated-dependencies:
- dependency-name: serialize-javascript
  dependency-type: indirect
- dependency-name: mocha
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #8071: Bump serialize-javascript and mocha

96552 of 179126 branches covered (53.9%)

212672 of 234185 relevant lines covered (90.81%)

3115802.0 hits per line

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

80.0
/src/realm/object-store/dictionary.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
#include <realm/object-store/dictionary.hpp>
20

21
#include <realm/object-store/results.hpp>
22
#include <realm/table.hpp>
23

24
namespace realm {
25
namespace {
26
class DictionaryKeyAdapter : public CollectionBase {
27
public:
28
    DictionaryKeyAdapter(std::shared_ptr<Dictionary> dictionary)
29
        : m_dictionary(std::move(dictionary))
1,180✔
30
    {
1,180✔
31
    }
1,180✔
32

33
    // -------------------------------------------------------------------------
34
    // Things which this adapter does something different from Dictionary for
35

36
    Mixed get_any(size_t ndx) const override
37
    {
1,189✔
38
        return m_dictionary->get_key(ndx);
1,189✔
39
    }
1,189✔
40

41
    size_t find_any(Mixed value) const override
42
    {
174✔
43
        return m_dictionary->find_any_key(value);
174✔
44
    }
174✔
45

46
    ColKey get_col_key() const noexcept override
47
    {
2,980✔
48
        auto col_key = m_dictionary->get_col_key();
2,980✔
49
        auto type = ColumnType(m_dictionary->get_key_data_type());
2,980✔
50
        return ColKey(col_key.get_index(), type, col_key.get_attrs(), col_key.get_tag());
2,980✔
51
    }
2,980✔
52

53
    CollectionBasePtr clone_collection() const override
54
    {
×
55
        return std::make_unique<DictionaryKeyAdapter>(*this);
×
56
    }
×
57

58
    // -------------------------------------------------------------------------
59
    // Things which this just forwards on to Dictionary
60

61
    size_t size() const override
62
    {
1,629✔
63
        return m_dictionary->size();
1,629✔
64
    }
1,629✔
65
    bool is_null(size_t ndx) const override
66
    {
×
67
        return m_dictionary->is_null(ndx);
×
68
    }
×
69
    void clear() override
70
    {
42✔
71
        m_dictionary->clear();
42✔
72
    }
42✔
73
    void sort(std::vector<size_t>& indices, bool ascending = true) const override
74
    {
84✔
75
        m_dictionary->sort_keys(indices, ascending);
84✔
76
    }
84✔
77
    void distinct(std::vector<size_t>& indices, util::Optional<bool> sort_order = util::none) const override
78
    {
21✔
79
        m_dictionary->distinct_keys(indices, sort_order);
21✔
80
    }
21✔
81
    const Obj& get_obj() const noexcept override
82
    {
4,141✔
83
        return m_dictionary->get_obj();
4,141✔
84
    }
4,141✔
85
    bool has_changed() const noexcept override
86
    {
1,425✔
87
        return m_dictionary->has_changed();
1,425✔
88
    }
1,425✔
89

90
    FullPath get_path() const noexcept final
91
    {
×
92
        return m_dictionary->get_path();
×
93
    }
×
94

95
    Path get_short_path() const noexcept final
96
    {
×
97
        return m_dictionary->get_short_path();
×
98
    }
×
99

100
    StablePath get_stable_path() const noexcept final
101
    {
×
102
        return m_dictionary->get_stable_path();
×
103
    }
×
104

105
    // -------------------------------------------------------------------------
106
    // Things not applicable to the adapter
107

108
    // We currently only support string keys which means these aren't reachable
109
    // as Results will handle the type-checks
110
    util::Optional<Mixed> min(size_t* = nullptr) const override
111
    {
×
112
        REALM_TERMINATE("not implemented");
113
    }
×
114
    util::Optional<Mixed> max(size_t* = nullptr) const override
115
    {
×
116
        REALM_TERMINATE("not implemented");
117
    }
×
118
    util::Optional<Mixed> sum(size_t* = nullptr) const override
119
    {
×
120
        REALM_TERMINATE("not implemented");
121
    }
×
122
    util::Optional<Mixed> avg(size_t* = nullptr) const override
123
    {
×
124
        REALM_TERMINATE("not implemented");
125
    }
×
126
    void set_owner(const Obj& obj, ColKey ck) override
127
    {
×
128
        m_dictionary->set_owner(obj, ck);
×
129
    }
×
130
    void set_owner(std::shared_ptr<CollectionParent> parent, CollectionParent::Index index) override
131
    {
×
132
        m_dictionary->set_owner(std::move(parent), index);
×
133
    }
×
134
    CollectionType get_collection_type() const noexcept override
135
    {
×
136
        return CollectionType::List;
×
137
    }
×
138

139
private:
140
    std::shared_ptr<Dictionary> m_dictionary;
141
};
142
} // anonymous namespace
143

144
void DictionaryChangeSet::add(std::vector<Mixed>& arr, const Mixed& key)
145
{
471✔
146
    arr.push_back(key);
471✔
147
    if (key.is_type(type_String)) {
471✔
148
        REALM_ASSERT(m_string_store.size() < m_string_store.capacity());
471✔
149
        m_string_store.emplace_back();
471✔
150
        arr.back().use_buffer(m_string_store.back());
471✔
151
    }
471✔
152
}
471✔
153

154
DictionaryChangeSet::DictionaryChangeSet(const DictionaryChangeSet& other)
155
{
9✔
156
    m_string_store.reserve(other.m_string_store.size());
9✔
157
    for (auto k : other.deletions) {
9✔
158
        add(deletions, k);
8✔
159
    }
8✔
160
    for (auto k : other.insertions) {
9✔
161
        add(insertions, k);
5✔
162
    }
5✔
163
    for (auto k : other.modifications) {
9✔
164
        add(modifications, k);
×
165
    }
×
166

167
    collection_root_was_deleted = other.collection_root_was_deleted;
9✔
168
    collection_was_cleared = other.collection_was_cleared;
9✔
169
}
9✔
170

171
DictionaryChangeSet& DictionaryChangeSet::operator=(const DictionaryChangeSet& other)
172
{
126✔
173
    m_string_store.reserve(other.m_string_store.size());
126✔
174

175
    m_string_store.clear();
126✔
176
    deletions.clear();
126✔
177
    insertions.clear();
126✔
178
    modifications.clear();
126✔
179

180
    for (auto k : other.deletions) {
183✔
181
        add(deletions, k);
183✔
182
    }
183✔
183
    for (auto k : other.insertions) {
126✔
184
        add(insertions, k);
21✔
185
    }
21✔
186
    for (auto k : other.modifications) {
126✔
187
        add(modifications, k);
21✔
188
    }
21✔
189

190
    collection_root_was_deleted = other.collection_root_was_deleted;
126✔
191
    collection_was_cleared = other.collection_was_cleared;
126✔
192
    return *this;
126✔
193
}
126✔
194

195

196
namespace object_store {
197

198
bool Dictionary::operator==(const Dictionary& rgt) const noexcept
199
{
274✔
200
    return dict() == rgt.dict();
274✔
201
}
274✔
202

203
bool Dictionary::operator!=(const Dictionary& rgt) const noexcept
204
{
127✔
205
    return !(*this == rgt);
127✔
206
}
127✔
207

208
Obj Dictionary::insert_embedded(StringData key)
209
{
40✔
210
    return dict().create_and_insert_linked_object(key);
40✔
211
}
40✔
212

213
std::pair<size_t, bool> Dictionary::insert_any(StringData key, Mixed value)
214
{
66✔
215
    auto [it, inserted] = dict().insert(key, value);
66✔
216
    return std::make_pair(it.index(), inserted);
66✔
217
}
66✔
218

219
void Dictionary::erase(StringData key)
220
{
296✔
221
    verify_in_transaction();
296✔
222
    dict().erase(key);
296✔
223
}
296✔
224

225
bool Dictionary::try_erase(StringData key)
226
{
185✔
227
    verify_in_transaction();
185✔
228
    return dict().try_erase(key);
185✔
229
}
185✔
230

231
void Dictionary::remove_all()
232
{
280✔
233
    verify_in_transaction();
280✔
234
    dict().clear();
280✔
235
}
280✔
236

237
Obj Dictionary::get_object(StringData key)
238
{
5✔
239
    auto& dictionary = dict();
5✔
240
    auto obj = dictionary.get_object(key);
5✔
241
    record_audit_read(obj);
5✔
242
    return obj;
5✔
243
}
5✔
244

245
Mixed Dictionary::get_any(StringData key)
246
{
187✔
247
    auto value = dict().get(key);
187✔
248
    record_audit_read(value);
187✔
249
    return value;
187✔
250
}
187✔
251

252
Mixed Dictionary::get_any(size_t ndx) const
253
{
×
254
    verify_attached();
×
255
    auto value = dict().get_any(ndx);
×
256
    record_audit_read(value);
×
257
    return value;
×
258
}
×
259

260
util::Optional<Mixed> Dictionary::try_get_any(StringData key) const
261
{
115✔
262
    auto value = dict().try_get(key);
115✔
263
    if (value)
115✔
264
        record_audit_read(*value);
114✔
265
    return value;
115✔
266
}
115✔
267

268
std::pair<StringData, Mixed> Dictionary::get_pair(size_t ndx) const
269
{
103✔
270
    verify_attached();
103✔
271
    auto pair = dict().get_pair(ndx);
103✔
272
    record_audit_read(pair.second);
103✔
273
    return {pair.first.get_string(), pair.second};
103✔
274
}
103✔
275

276
size_t Dictionary::find_any(Mixed value) const
277
{
246✔
278
    return dict().find_any(value);
246✔
279
}
246✔
280

281
bool Dictionary::contains(StringData key) const
282
{
489✔
283
    return dict().contains(key);
489✔
284
}
489✔
285

286
Obj Dictionary::get_object(StringData key) const
287
{
3✔
288
    auto k = dict().get(key).get<ObjKey>();
3✔
289
    return dict().get_target_table()->get_object(k);
3✔
290
}
3✔
291

292
Results Dictionary::snapshot() const
293
{
×
294
    return as_results().snapshot();
×
295
}
×
296

297
Results Dictionary::get_keys() const
298
{
1,180✔
299
    verify_attached();
1,180✔
300
    return Results(m_realm,
1,180✔
301
                   std::make_shared<DictionaryKeyAdapter>(std::dynamic_pointer_cast<realm::Dictionary>(m_coll_base)));
1,180✔
302
}
1,180✔
303

304
Results Dictionary::get_values() const
305
{
1,221✔
306
    return as_results();
1,221✔
307
}
1,221✔
308

309
Dictionary::Iterator Dictionary::begin() const
310
{
81✔
311
    return dict().begin();
81✔
312
}
81✔
313

314
Dictionary::Iterator Dictionary::end() const
315
{
×
316
    return dict().end();
×
317
}
×
318

319
namespace {
320
class NotificationHandler {
321
public:
322
    NotificationHandler(realm::Dictionary& dict, Dictionary::CBFunc cb)
323
        : m_dict(dict)
45✔
324
        , m_prev_rt(static_cast<Transaction*>(dict.get_table()->get_parent_group())->duplicate())
45✔
325
        , m_prev_dict(std::dynamic_pointer_cast<realm::Dictionary>(m_prev_rt->import_copy_of(dict)))
45✔
326
        , m_cb(std::move(cb))
45✔
327
    {
45✔
328
    }
45✔
329

330
    void operator()(CollectionChangeSet const& c)
331
    {
132✔
332
        size_t max_keys = c.deletions.count() + c.insertions.count() + c.modifications.count();
132✔
333
        DictionaryChangeSet changes(max_keys);
132✔
334

335
        if (max_keys) {
132✔
336
            for (auto ndx : c.deletions.as_indexes()) {
188✔
337
                changes.add_deletion(m_prev_dict->get_key(ndx));
188✔
338
            }
188✔
339
            for (auto ndx : c.insertions.as_indexes()) {
88✔
340
                changes.add_insertion(m_dict.get_key(ndx));
24✔
341
            }
24✔
342
            for (auto ndx : c.modifications_new.as_indexes()) {
88✔
343
                changes.add_modification(m_dict.get_key(ndx));
21✔
344
            }
21✔
345
        }
88✔
346

347
        if (c.collection_root_was_deleted) {
132✔
348
            changes.collection_root_was_deleted = true;
22✔
349
            m_prev_rt = nullptr;
22✔
350
        }
22✔
351
        else {
110✔
352
            REALM_ASSERT(m_dict.is_attached());
110✔
353
            auto current_tr = static_cast<Transaction*>(m_dict.get_table()->get_parent_group());
110✔
354
            m_prev_rt->advance_read(current_tr->get_version_of_current_transaction());
110✔
355
        }
110✔
356
        changes.collection_was_cleared = c.collection_was_cleared;
132✔
357

358
        m_cb(std::move(changes));
132✔
359
    }
132✔
360

361
private:
362
    realm::Dictionary& m_dict;
363
    TransactionRef m_prev_rt;
364
    DictionaryPtr m_prev_dict;
365
    Dictionary::CBFunc m_cb;
366
};
367
} // namespace
368

369
NotificationToken Dictionary::add_key_based_notification_callback(CBFunc cb,
370
                                                                  std::optional<KeyPathArray> key_path_array) &
371
{
45✔
372
    return add_notification_callback(NotificationHandler(dict(), std::move(cb)), std::move(key_path_array));
45✔
373
}
45✔
374

375
Dictionary Dictionary::freeze(const std::shared_ptr<Realm>& frozen_realm) const
376
{
11✔
377
    auto frozen_dictionary(frozen_realm->import_copy_of(*m_coll_base));
11✔
378
    if (frozen_dictionary) {
11✔
379
        return Dictionary(frozen_realm, std::move(frozen_dictionary));
10✔
380
    }
10✔
381
    else {
1✔
382
        return Dictionary{};
1✔
383
    }
1✔
384
}
11✔
385

386
} // namespace object_store
387
} // 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