• 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

95.98
/src/realm/object-store/collection.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/collection.hpp>
20

21
#include <realm/object-store/audit.hpp>
22
#include <realm/object-store/impl/list_notifier.hpp>
23
#include <realm/object-store/impl/realm_coordinator.hpp>
24
#include <realm/object-store/object_schema.hpp>
25
#include <realm/object-store/object_store.hpp>
26
#include <realm/object-store/results.hpp>
27
#include <realm/object-store/shared_realm.hpp>
28
#include <realm/object-store/list.hpp>
29
#include <realm/object-store/dictionary.hpp>
30
#include <realm/object-store/set.hpp>
31

32
namespace realm::object_store {
33

34
Collection::Collection(PropertyType type) noexcept
35
    : m_type(type)
200✔
36
{
200✔
37
}
200✔
38

39
Collection::Collection(const Object& parent_obj, const Property* prop)
40
    : Collection(std::shared_ptr(parent_obj.get_realm()), parent_obj.get_obj().get_collection_ptr(prop->column_key),
63✔
41
                 prop->type)
63✔
42
{
63✔
43
}
63✔
44

45
Collection::Collection(std::shared_ptr<Realm> r, const Obj& parent_obj, ColKey col)
46
    : Collection(std::move(r), parent_obj.get_collection_ptr(col),
17,574✔
47
                 ObjectSchema::from_core_type(col) & ~PropertyType::Collection)
17,574✔
48
{
17,574✔
49
}
17,574✔
50

51
Collection::Collection(std::shared_ptr<Realm> r, const CollectionBase& coll)
52
    : Collection(std::move(r), coll.clone_collection(),
92✔
53
                 ObjectSchema::from_core_type(coll.get_col_key()) & ~PropertyType::Collection)
92✔
54
{
92✔
55
}
92✔
56

57
Collection::Collection(std::shared_ptr<Realm> r, CollectionBasePtr coll)
58
    : Collection(std::move(r), std::move(coll),
552✔
59
                 ObjectSchema::from_core_type(coll->get_col_key()) & ~PropertyType::Collection)
552✔
60
{
552✔
61
}
552✔
62

63
Collection::Collection(std::shared_ptr<Realm>&& r, CollectionBasePtr&& coll, PropertyType type)
64
    : m_realm(std::move(r))
18,281✔
65
    , m_type(type)
18,281✔
66
    , m_coll_base(std::move(coll))
18,281✔
67
    , m_is_embedded(m_type == PropertyType::Object && m_coll_base->get_target_table()->is_embedded())
18,281✔
68
{
18,281✔
69
}
18,281✔
70

71
Collection::~Collection() = default;
19,496✔
72
Collection::Collection(const Collection&) = default;
571✔
73
Collection& Collection::operator=(const Collection&) = default;
37✔
74
Collection::Collection(Collection&&) = default;
444✔
75
Collection& Collection::operator=(Collection&&) = default;
52✔
76

77
bool Collection::is_valid() const
78
{
170,908✔
79
    if (!m_realm || !m_coll_base)
170,908✔
80
        return false;
260✔
81
    m_realm->verify_thread();
170,648✔
82
    if (!m_realm->is_in_read_transaction())
170,648✔
83
        return false;
273✔
84
    return m_coll_base->is_attached();
170,375✔
85
}
170,648✔
86

87
ObjKey Collection::get_parent_object_key() const
88
{
136✔
89
    verify_attached();
136✔
90
    return m_coll_base->get_owner_key();
136✔
91
}
136✔
92

93
ColKey Collection::get_parent_column_key() const
94
{
364✔
95
    verify_attached();
364✔
96
    return m_coll_base->get_col_key();
364✔
97
}
364✔
98

99
TableKey Collection::get_parent_table_key() const
100
{
366✔
101
    verify_attached();
366✔
102
    return m_coll_base->get_table()->get_key();
366✔
103
}
366✔
104

105
void Collection::validate(const Obj& obj) const
106
{
353✔
107
    if (!obj.is_valid())
353✔
108
        throw StaleAccessor("Object has been deleted or invalidated");
2✔
109
    // FIXME: This does not work for TypedLink.
110
    auto target = m_coll_base->get_target_table();
351✔
111
    if (obj.get_table() != target)
351✔
112
        throw InvalidArgument(ErrorCodes::ObjectTypeMismatch,
9✔
113
                              util::format("Object of type (%1) does not match %2 type (%3)",
9✔
114
                                           obj.get_table()->get_class_name(), type_name(), target->get_class_name()));
9✔
115
}
351✔
116

117
void Collection::verify_attached() const
118
{
168,496✔
119
    if (REALM_LIKELY(is_valid())) {
168,496✔
120
        return;
168,178✔
121
    }
168,178✔
122
    if (!m_coll_base) {
318✔
123
        throw LogicError(ErrorCodes::InvalidatedObject,
147✔
124
                         util::format("%1 was never initialized and is invalid.", type_name()));
147✔
125
    }
147✔
126

127
    throw LogicError(ErrorCodes::InvalidatedObject,
171✔
128
                     util::format("%1 is no longer valid. Either the parent object was deleted or the containing "
171✔
129
                                  "Realm has been invalidated or closed.",
171✔
130
                                  type_name()));
171✔
131
}
318✔
132

133
void Collection::verify_in_transaction() const
134
{
64,929✔
135
    verify_attached();
64,929✔
136
    if (REALM_UNLIKELY(!m_realm->is_in_transaction())) {
64,929✔
137
        throw WrongTransactionState(
42✔
138
            util::format("Cannot modify managed %1 outside of a write transaction.", type_name()));
42✔
139
    }
42✔
140
}
64,929✔
141

142
size_t Collection::size() const
143
{
46,101✔
144
    verify_attached();
46,101✔
145
    return m_coll_base->size();
46,101✔
146
}
46,101✔
147

148
const ObjectSchema& Collection::get_object_schema() const
149
{
1,121✔
150
    verify_attached();
1,121✔
151

152
    REALM_ASSERT(get_type() == PropertyType::Object);
1,121✔
153
    auto object_schema = m_object_schema.load();
1,121✔
154
    if (!object_schema) {
1,121✔
155
        auto object_type = m_coll_base->get_target_table()->get_class_name();
1,120✔
156
        auto it = m_realm->schema().find(object_type);
1,120✔
157
        REALM_ASSERT(it != m_realm->schema().end());
1,120✔
158
        m_object_schema = object_schema = &*it;
1,120✔
159
    }
1,120✔
160
    return *object_schema;
1,121✔
161
}
1,121✔
162

163
bool Collection::is_frozen() const noexcept
164
{
26✔
165
    return m_realm->is_frozen();
26✔
166
}
26✔
167

168
Results Collection::as_results() const
169
{
5,629✔
170
    verify_attached();
5,629✔
171
    return Results(m_realm, m_coll_base);
5,629✔
172
}
5,629✔
173

174
Results Collection::sort(SortDescriptor order) const
175
{
52✔
176
    verify_attached();
52✔
177
    return Results(m_realm, m_coll_base, util::none, std::move(order));
52✔
178
}
52✔
179

180
Results Collection::sort(std::vector<std::pair<std::string, bool>> const& keypaths) const
181
{
161✔
182
    return as_results().sort(keypaths);
161✔
183
}
161✔
184

185
Results Collection::snapshot() const
186
{
5✔
187
    return as_results().snapshot();
5✔
188
}
5✔
189

190
std::optional<Mixed> Collection::max(ColKey col) const
191
{
204✔
192
    return as_results().max(col);
204✔
193
}
204✔
194

195
util::Optional<Mixed> Collection::min(ColKey col) const
196
{
204✔
197
    return as_results().min(col);
204✔
198
}
204✔
199

200
Mixed Collection::sum(ColKey col) const
201
{
194✔
202
    return *as_results().sum(col);
194✔
203
}
194✔
204

205
util::Optional<Mixed> Collection::average(ColKey col) const
206
{
194✔
207
    return as_results().average(col);
194✔
208
}
194✔
209

210
NotificationToken Collection::add_notification_callback(CollectionChangeCallback callback,
211
                                                        std::optional<KeyPathArray> key_path_array) &
212
{
655✔
213
    verify_attached();
655✔
214
    m_realm->verify_notifications_available();
655✔
215
    // Adding a new callback to a notifier which had all of its callbacks
216
    // removed does not properly reinitialize the notifier. Work around this by
217
    // recreating it instead.
218
    // FIXME: The notifier lifecycle here is dumb (when all callbacks are removed
219
    // from a notifier a zombie is left sitting around uselessly) and should be
220
    // cleaned up.
221
    if (m_notifier && !m_notifier->have_callbacks())
655✔
222
        m_notifier.reset();
3✔
223
    if (!m_notifier) {
655✔
224
        m_notifier = std::make_shared<_impl::ListNotifier>(m_realm, *m_coll_base, m_type);
602✔
225
        _impl::RealmCoordinator::register_notifier(m_notifier);
602✔
226
    }
602✔
227
    return {m_notifier, m_notifier->add_callback(std::move(callback), std::move(key_path_array))};
655✔
228
}
655✔
229

230
void Collection::record_audit_read(const Obj& obj) const
231
{
247✔
232
    if (auto audit = m_realm->audit_context()) {
247✔
233
        audit->record_read(m_realm->read_transaction_version(), obj, m_coll_base->get_obj(),
×
234
                           m_coll_base->get_col_key());
×
235
    }
×
236
}
247✔
237

238
void Collection::record_audit_read(const Mixed& value) const
239
{
1,399✔
240
    if (auto audit = m_realm->audit_context(); audit && value.is_type(type_TypedLink)) {
1,399!
241
        audit->record_read(m_realm->read_transaction_version(),
×
242
                           m_realm->read_group().get_object(value.get<ObjLink>()), m_coll_base->get_obj(),
×
243
                           m_coll_base->get_col_key());
×
244
    }
×
245
}
1,399✔
246

247
namespace {
248
size_t hash_combine()
249
{
84✔
250
    return 0;
84✔
251
}
84✔
252
template <typename T, typename... Rest>
253
size_t hash_combine(const T& v, Rest... rest)
254
{
252✔
255
    size_t h = hash_combine(rest...);
252✔
256
    h ^= std::hash<T>()(v) + 0x9e3779b9 + (h << 6) + (h >> 2);
252✔
257
    return h;
252✔
258
}
252✔
259
} // namespace
260

261
size_t Collection::hash() const noexcept
262
{
84✔
263
    auto& impl = *m_coll_base;
84✔
264
    return hash_combine(impl.get_owner_key().value, impl.get_table()->get_key().value, impl.get_col_key().value);
84✔
265
}
84✔
266

267
void Collection::insert_collection(const PathElement& path, CollectionType type)
268
{
303✔
269
    verify_in_transaction();
303✔
270
    m_coll_base->insert_collection(path, type);
303✔
271
}
303✔
272

273
void Collection::set_collection(const PathElement& path, CollectionType type)
274
{
7✔
275
    verify_in_transaction();
7✔
276
    m_coll_base->set_collection(path, type);
7✔
277
}
7✔
278

279
List Collection::get_list(const PathElement& path) const
280
{
293✔
281
    return List{m_realm, m_coll_base->get_list(path)};
293✔
282
}
293✔
283

284
Dictionary Collection::get_dictionary(const PathElement& path) const
285
{
204✔
286
    return Dictionary{m_realm, m_coll_base->get_dictionary(path)};
204✔
287
}
204✔
288

289
} // namespace realm::object_store
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