• 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

90.88
/src/realm/group.cpp
1
/*************************************************************************
2
 *
3
 * Copyright 2016 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 <new>
20
#include <algorithm>
21
#include <fstream>
22

23
#ifdef REALM_DEBUG
24
#include <iostream>
25
#include <iomanip>
26
#endif
27

28
#include <realm/util/file_mapper.hpp>
29
#include <realm/util/memory_stream.hpp>
30
#include <realm/util/miscellaneous.hpp>
31
#include <realm/util/thread.hpp>
32
#include <realm/impl/destroy_guard.hpp>
33
#include <realm/utilities.hpp>
34
#include <realm/exceptions.hpp>
35
#include <realm/group_writer.hpp>
36
#include <realm/transaction.hpp>
37
#include <realm/replication.hpp>
38

39
using namespace realm;
40
using namespace realm::util;
41

42
namespace {
43

44
class Initialization {
45
public:
46
    Initialization()
47
    {
24✔
48
        realm::cpuid_init();
24✔
49
    }
24✔
50
};
51

52
Initialization initialization;
53

54
} // anonymous namespace
55

56
Group::Group()
57
    : m_local_alloc(new SlabAlloc)
58
    , m_alloc(*m_local_alloc) // Throws
59
    , m_top(m_alloc)
60
    , m_tables(m_alloc)
61
    , m_table_names(m_alloc)
62
{
4,014✔
63
    init_array_parents();
4,014✔
64
    m_alloc.attach_empty(); // Throws
4,014✔
65
    m_file_format_version = get_target_file_format_version_for_session(0, Replication::hist_None);
4,014✔
66
    ref_type top_ref = 0; // Instantiate a new empty group
4,014✔
67
    bool create_group_when_missing = true;
4,014✔
68
    bool writable = create_group_when_missing;
4,014✔
69
    attach(top_ref, writable, create_group_when_missing); // Throws
4,014✔
70
}
4,014✔
71

72

73
Group::Group(const std::string& file_path, const char* encryption_key)
74
    : m_local_alloc(new SlabAlloc) // Throws
75
    , m_alloc(*m_local_alloc)
76
    , m_top(m_alloc)
77
    , m_tables(m_alloc)
78
    , m_table_names(m_alloc)
79
{
849✔
80
    init_array_parents();
849✔
81

327✔
82
    SlabAlloc::Config cfg;
849✔
83
    cfg.read_only = true;
849✔
84
    cfg.no_create = true;
849✔
85
    cfg.encryption_key = encryption_key;
849✔
86
    ref_type top_ref = m_alloc.attach_file(file_path, cfg); // Throws
849✔
87
    // Non-Transaction Groups always allow writing and simply don't allow
327✔
88
    // committing when opened in read-only mode
327✔
89
    m_alloc.set_read_only(false);
849✔
90

327✔
91
    open(top_ref, file_path);
849✔
92
}
849✔
93

94

95
Group::Group(BinaryData buffer, bool take_ownership)
96
    : m_local_alloc(new SlabAlloc) // Throws
97
    , m_alloc(*m_local_alloc)
98
    , m_top(m_alloc)
99
    , m_tables(m_alloc)
100
    , m_table_names(m_alloc)
101
{
48✔
102
    REALM_ASSERT(buffer.data());
48✔
103

24✔
104
    init_array_parents();
48✔
105
    ref_type top_ref = m_alloc.attach_buffer(buffer.data(), buffer.size()); // Throws
48✔
106

24✔
107
    open(top_ref, {});
48✔
108

24✔
109
    if (take_ownership)
48✔
110
        m_alloc.own_buffer();
36✔
111
}
48✔
112

113
Group::Group(SlabAlloc* alloc) noexcept
114
    : m_alloc(*alloc)
115
    , // Throws
116
    m_top(m_alloc)
117
    , m_tables(m_alloc)
118
    , m_table_names(m_alloc)
119
{
2,698,326✔
120
    init_array_parents();
2,698,326✔
121
}
2,698,326✔
122

123
namespace {
124

125
class TableRecycler : public std::vector<Table*> {
126
public:
127
    ~TableRecycler()
128
    {
×
129
        REALM_UNREACHABLE();
×
130
        // if ever enabled, remember to release Tables:
×
131
        // for (auto t : *this) {
×
132
        //    delete t;
×
133
        //}
×
134
    }
×
135
};
136

137
// We use the classic approach to construct a FIFO from two LIFO's,
138
// insertion is done into recycler_1, removal is done from recycler_2,
139
// and when recycler_2 is empty, recycler_1 is reversed into recycler_2.
140
// this i O(1) for each entry.
141
auto& g_table_recycler_1 = *new TableRecycler;
142
auto& g_table_recycler_2 = *new TableRecycler;
143
// number of tables held back before being recycled. We hold back recycling
144
// the latest to increase the probability of detecting race conditions
145
// without crashing.
146
const static int g_table_recycling_delay = 100;
147
auto& g_table_recycler_mutex = *new std::mutex;
148

149
} // namespace
150

151
TableKeyIterator& TableKeyIterator::operator++()
152
{
5,084,295✔
153
    m_pos++;
5,084,295✔
154
    m_index_in_group++;
5,084,295✔
155
    load_key();
5,084,295✔
156
    return *this;
5,084,295✔
157
}
5,084,295✔
158

159
TableKey TableKeyIterator::operator*()
160
{
5,219,004✔
161
    if (!bool(m_table_key)) {
5,219,004✔
162
        load_key();
1,297,440✔
163
    }
1,297,440✔
164
    return m_table_key;
5,219,004✔
165
}
5,219,004✔
166

167
void TableKeyIterator::load_key()
168
{
6,381,735✔
169
    const Group& g = *m_group;
6,381,735✔
170
    size_t max_index_in_group = g.m_table_names.size();
6,381,735✔
171
    while (m_index_in_group < max_index_in_group) {
6,761,517✔
172
        RefOrTagged rot = g.m_tables.get_as_ref_or_tagged(m_index_in_group);
5,598,852✔
173
        if (rot.is_ref()) {
5,598,852✔
174
            Table* t;
5,219,070✔
175
            if (m_index_in_group < g.m_table_accessors.size() &&
5,219,070✔
176
                (t = load_atomic(g.m_table_accessors[m_index_in_group], std::memory_order_acquire))) {
5,219,070✔
177
                m_table_key = t->get_key();
1,136,610✔
178
            }
1,136,610✔
179
            else {
4,082,460✔
180
                m_table_key = Table::get_key_direct(g.m_tables.get_alloc(), rot.get_as_ref());
4,082,460✔
181
            }
4,082,460✔
182
            return;
5,219,070✔
183
        }
5,219,070✔
184
        m_index_in_group++;
379,782✔
185
    }
379,782✔
186
    m_table_key = TableKey();
3,784,002✔
187
}
1,162,665✔
188

189
TableKey TableKeys::operator[](size_t p) const
190
{
615✔
191
    if (p < m_iter.m_pos) {
615✔
192
        m_iter = TableKeyIterator(m_iter.m_group, 0);
×
193
    }
×
194
    while (m_iter.m_pos < p) {
882✔
195
        ++m_iter;
267✔
196
    }
267✔
197
    return *m_iter;
615✔
198
}
615✔
199

200
size_t Group::size() const noexcept
201
{
1,752,348✔
202
    return m_num_tables;
1,752,348✔
203
}
1,752,348✔
204

205

206
void Group::set_size() const noexcept
207
{
2,979,009✔
208
    int retval = 0;
2,979,009✔
209
    if (is_attached() && m_table_names.is_attached()) {
2,979,255✔
210
        size_t max_index = m_tables.size();
2,830,254✔
211
        REALM_ASSERT_EX(max_index < (1 << 16), max_index);
2,830,254✔
212
        for (size_t j = 0; j < max_index; ++j) {
11,744,286✔
213
            RefOrTagged rot = m_tables.get_as_ref_or_tagged(j);
8,914,032✔
214
            if (rot.is_ref() && rot.get_as_ref()) {
8,914,032✔
215
                ++retval;
8,558,544✔
216
            }
8,558,544✔
217
        }
8,914,032✔
218
    }
2,830,254✔
219
    m_num_tables = retval;
2,979,009✔
220
}
2,979,009✔
221

222
std::map<TableRef, ColKey> Group::get_primary_key_columns_from_pk_table(TableRef pk_table)
UNCOV
223
{
×
UNCOV
224
    std::map<TableRef, ColKey> ret;
×
UNCOV
225
    REALM_ASSERT(pk_table);
×
UNCOV
226
    ColKey col_table = pk_table->get_column_key("pk_table");
×
UNCOV
227
    ColKey col_prop = pk_table->get_column_key("pk_property");
×
UNCOV
228
    for (auto pk_obj : *pk_table) {
×
UNCOV
229
        auto object_type = pk_obj.get<String>(col_table);
×
UNCOV
230
        auto name = std::string(g_class_name_prefix) + std::string(object_type);
×
UNCOV
231
        auto table = get_table(name);
×
UNCOV
232
        auto pk_col_name = pk_obj.get<String>(col_prop);
×
UNCOV
233
        auto pk_col = table->get_column_key(pk_col_name);
×
UNCOV
234
        ret.emplace(table, pk_col);
×
UNCOV
235
    }
×
236

UNCOV
237
    return ret;
×
UNCOV
238
}
×
239

240
TableKey Group::ndx2key(size_t ndx) const
241
{
7,566✔
242
    REALM_ASSERT(is_attached());
7,566✔
243
    Table* accessor = load_atomic(m_table_accessors[ndx], std::memory_order_acquire);
7,566✔
244
    if (accessor)
7,566✔
245
        return accessor->get_key(); // fast path
1,866✔
246

2,724✔
247
    // slow path:
2,724✔
248
    RefOrTagged rot = m_tables.get_as_ref_or_tagged(ndx);
5,700✔
249
    if (rot.is_tagged())
5,700✔
250
        throw NoSuchTable();
×
251
    ref_type ref = rot.get_as_ref();
5,700✔
252
    REALM_ASSERT(ref);
5,700✔
253
    return Table::get_key_direct(m_tables.get_alloc(), ref);
5,700✔
254
}
5,700✔
255

256
size_t Group::key2ndx_checked(TableKey key) const
257
{
41,908,776✔
258
    size_t idx = key2ndx(key);
41,908,776✔
259
    // early out
21,812,658✔
260
    // note: don't lock when accessing m_table_accessors, because if we miss a concurrently introduced table
21,812,658✔
261
    // accessor, we'll just fall through to the slow path. Table accessors can be introduced concurrently,
21,812,658✔
262
    // but never removed. The following is only safe because 'm_table_accessors' will not be relocated
21,812,658✔
263
    // concurrently. (We aim to be safe in face of concurrent access to a frozen transaction, where tables
21,812,658✔
264
    // cannot be added or removed. All other races are undefined behaviour)
21,812,658✔
265
    if (idx < m_table_accessors.size()) {
41,961,318✔
266
        Table* tbl = load_atomic(m_table_accessors[idx], std::memory_order_acquire);
41,764,626✔
267
        if (tbl && tbl->get_key() == key)
41,764,626✔
268
            return idx;
36,346,665✔
269
    }
5,562,111✔
270
    // The notion of a const group as it is now, is not really
2,982,036✔
271
    // useful. It is linked to a distinction between a read
2,982,036✔
272
    // and a write transaction. This distinction is no longer
2,982,036✔
273
    // a compile time aspect (it's not const anymore)
2,982,036✔
274
    Allocator* alloc = const_cast<SlabAlloc*>(&m_alloc);
5,562,111✔
275
    if (m_tables.is_attached() && idx < m_tables.size()) {
5,608,452✔
276
        RefOrTagged rot = m_tables.get_as_ref_or_tagged(idx);
5,487,459✔
277
        if (rot.is_ref() && rot.get_as_ref() && (Table::get_key_direct(*alloc, rot.get_as_ref()) == key)) {
5,490,177✔
278

3,027,681✔
279
            return idx;
5,488,620✔
280
        }
5,488,620✔
281
    }
2,147,602,783✔
282
    throw NoSuchTable();
2,147,602,783✔
283
}
2,147,602,783✔
284

285

286
int Group::get_file_format_version() const noexcept
287
{
1,193,034✔
288
    return m_file_format_version;
1,193,034✔
289
}
1,193,034✔
290

291

292
void Group::set_file_format_version(int file_format) noexcept
293
{
2,695,305✔
294
    m_file_format_version = file_format;
2,695,305✔
295
}
2,695,305✔
296

297

298
int Group::get_committed_file_format_version() const noexcept
299
{
×
300
    return m_alloc.get_committed_file_format_version();
×
301
}
×
302

303
std::optional<int> Group::fake_target_file_format;
304

305
void _impl::GroupFriend::fake_target_file_format(const std::optional<int> format) noexcept
306
{
72✔
307
    Group::fake_target_file_format = format;
72✔
308
}
72✔
309

310
int Group::get_target_file_format_version_for_session(int current_file_format_version,
311
                                                      int requested_history_type) noexcept
312
{
170,025✔
313
    if (Group::fake_target_file_format) {
170,025✔
314
        return *Group::fake_target_file_format;
72✔
315
    }
72✔
316
    // Note: This function is responsible for choosing the target file format
83,712✔
317
    // for a sessions. If it selects a file format that is different from
83,712✔
318
    // `current_file_format_version`, it will trigger a file format upgrade
83,712✔
319
    // process.
83,712✔
320

83,712✔
321
    // Note: `current_file_format_version` may be zero at this time, which means
83,712✔
322
    // that the file format it is not yet decided (only possible for empty
83,712✔
323
    // Realms where top-ref is zero).
83,712✔
324

83,712✔
325
    // Please see Group::get_file_format_version() for information about the
83,712✔
326
    // individual file format versions.
83,712✔
327

83,712✔
328
    if (requested_history_type == Replication::hist_None) {
169,953✔
329
        if (current_file_format_version == 23) {
34,011✔
330
            // We are able to open these file formats in RO mode
UNCOV
331
            return current_file_format_version;
×
UNCOV
332
        }
×
333
    }
169,953✔
334

83,712✔
335
    return g_current_file_format_version;
169,953✔
336
}
169,953✔
337

338
void Group::get_version_and_history_info(const Array& top, _impl::History::version_type& version, int& history_type,
339
                                         int& history_schema_version) noexcept
340
{
772,692✔
341
    using version_type = _impl::History::version_type;
772,692✔
342
    version_type version_2 = 0;
772,692✔
343
    int history_type_2 = 0;
772,692✔
344
    int history_schema_version_2 = 0;
772,692✔
345
    if (top.is_attached()) {
772,692✔
346
        if (top.size() > s_version_ndx) {
722,937✔
347
            version_2 = version_type(top.get_as_ref_or_tagged(s_version_ndx).get_as_int());
722,613✔
348
        }
722,613✔
349
        if (top.size() > s_hist_type_ndx) {
722,937✔
350
            history_type_2 = int(top.get_as_ref_or_tagged(s_hist_type_ndx).get_as_int());
720,357✔
351
        }
720,357✔
352
        if (top.size() > s_hist_version_ndx) {
722,937✔
353
            history_schema_version_2 = int(top.get_as_ref_or_tagged(s_hist_version_ndx).get_as_int());
720,357✔
354
        }
720,357✔
355
    }
722,937✔
356
    // Version 0 is not a legal initial version, so it has to be set to 1
385,374✔
357
    // instead.
385,374✔
358
    if (version_2 == 0)
772,692✔
359
        version_2 = 1;
51,987✔
360
    version = version_2;
772,692✔
361
    history_type = history_type_2;
772,692✔
362
    history_schema_version = history_schema_version_2;
772,692✔
363
}
772,692✔
364

365
int Group::get_history_schema_version() noexcept
366
{
24,384✔
367
    bool history_schema_version = (m_top.is_attached() && m_top.size() > s_hist_version_ndx);
24,384✔
368
    if (history_schema_version) {
24,384✔
369
        return int(m_top.get_as_ref_or_tagged(s_hist_version_ndx).get_as_int());
714✔
370
    }
714✔
371
    return 0;
23,670✔
372
}
23,670✔
373

374
uint64_t Group::get_sync_file_id() const noexcept
375
{
13,470,648✔
376
    if (m_top.is_attached() && m_top.size() > s_sync_file_id_ndx) {
13,470,648✔
377
        return uint64_t(m_top.get_as_ref_or_tagged(s_sync_file_id_ndx).get_as_int());
6,055,089✔
378
    }
6,055,089✔
379
    auto repl = get_replication();
7,415,559✔
380
    if (repl && repl->get_history_type() == Replication::hist_SyncServer) {
7,415,559✔
381
        return 1;
2,709✔
382
    }
2,709✔
383
    return 0;
7,412,850✔
384
}
7,412,850✔
385

386
int Group::read_only_version_check(SlabAlloc& alloc, ref_type top_ref, const std::string& path)
387
{
1,029✔
388
    // Select file format if it is still undecided.
417✔
389
    auto file_format_version = alloc.get_committed_file_format_version();
1,029✔
390

417✔
391
    bool file_format_ok = false;
1,029✔
392
    // It is not possible to open prior file format versions without an upgrade.
417✔
393
    // Since a Realm file cannot be upgraded when opened in this mode
417✔
394
    // (we may be unable to write to the file), no earlier versions can be opened.
417✔
395
    // Please see Group::get_file_format_version() for information about the
417✔
396
    // individual file format versions.
417✔
397
    switch (file_format_version) {
1,029✔
398
        case 0:
6✔
399
            file_format_ok = (top_ref == 0);
6✔
400
            break;
6✔
401
        case 23:
396✔
402
        case g_current_file_format_version:
987✔
403
            file_format_ok = true;
987✔
404
            break;
987✔
405
    }
1,029✔
406
    if (REALM_UNLIKELY(!file_format_ok))
1,029✔
407
        throw FileAccessError(ErrorCodes::FileFormatUpgradeRequired,
435✔
408
                              util::format("Realm file at path '%1' cannot be opened in read-only mode because it "
36✔
409
                                           "has a file format version (%2) which requires an upgrade",
36✔
410
                                           path, file_format_version),
36✔
411
                              path);
36✔
412
    return file_format_version;
993✔
413
}
993✔
414

415
void Group::open(ref_type top_ref, const std::string& file_path)
416
{
849✔
417
    SlabAlloc::DetachGuard dg(m_alloc);
849✔
418
    m_file_format_version = read_only_version_check(m_alloc, top_ref, file_path);
849✔
419

327✔
420
    Replication::HistoryType history_type = Replication::hist_None;
849✔
421
    int target_file_format_version = get_target_file_format_version_for_session(m_file_format_version, history_type);
849✔
422
    if (m_file_format_version == 0) {
849✔
423
        set_file_format_version(target_file_format_version);
6✔
424
    }
6✔
425
    else {
843✔
426
        // From a technical point of view, we could upgrade the Realm file
324✔
427
        // format in memory here, but since upgrading can be expensive, it is
324✔
428
        // currently disallowed.
324✔
429
        REALM_ASSERT(target_file_format_version == m_file_format_version);
843✔
430
    }
843✔
431

327✔
432
    // Make all dynamically allocated memory (space beyond the attached file) as
327✔
433
    // available free-space.
327✔
434
    reset_free_space_tracking(); // Throws
849✔
435

327✔
436
    bool create_group_when_missing = true;
849✔
437
    bool writable = create_group_when_missing;
849✔
438
    attach(top_ref, writable, create_group_when_missing); // Throws
849✔
439
    dg.release();                                         // Do not detach after all
849✔
440
}
849✔
441

442
Group::~Group() noexcept
443
{
2,703,930✔
444
    // If this group accessor is detached at this point in time, it is either
1,628,643✔
445
    // because it is DB::m_group (m_is_shared), or it is a free-stading
1,628,643✔
446
    // group accessor that was never successfully opened.
1,628,643✔
447
    if (!m_top.is_attached())
2,703,930✔
448
        return;
2,697,903✔
449

3,450✔
450
    // Free-standing group accessor
3,450✔
451
    detach();
6,027✔
452

3,450✔
453
    // if a local allocator is set in m_local_alloc, then the destruction
3,450✔
454
    // of m_local_alloc will trigger destruction of the allocator, which will
3,450✔
455
    // verify that the allocator has been detached, so....
3,450✔
456
    if (m_local_alloc)
6,027✔
457
        m_local_alloc->detach();
4,827✔
458
}
6,027✔
459

460
void Group::remap_and_update_refs(ref_type new_top_ref, size_t new_file_size, bool writable)
461
{
389,646✔
462
    m_alloc.update_reader_view(new_file_size); // Throws
389,646✔
463
    update_allocator_wrappers(writable);
389,646✔
464

193,344✔
465
    // force update of all ref->ptr translations if the mapping has changed
193,344✔
466
    auto mapping_version = m_alloc.get_mapping_version();
389,646✔
467
    if (mapping_version != m_last_seen_mapping_version) {
389,646✔
468
        m_last_seen_mapping_version = mapping_version;
198,402✔
469
    }
198,402✔
470
    update_refs(new_top_ref);
389,646✔
471
}
389,646✔
472

473
void Group::validate_top_array(const Array& arr, const SlabAlloc& alloc, std::optional<size_t> read_lock_file_size,
474
                               std::optional<uint_fast64_t> read_lock_version)
475
{
2,908,944✔
476
    size_t top_size = arr.size();
2,908,944✔
477
    ref_type top_ref = arr.get_ref();
2,908,944✔
478

1,771,830✔
479
    switch (top_size) {
2,908,944✔
480
        // These are the valid sizes
481
        case 3:
1,771,188✔
482
        case 5:
1,771,188✔
483
        case 7:
1,821,321✔
484
        case 9:
1,821,321✔
485
        case 10:
1,821,321✔
486
        case 11:
2,903,226✔
487
        case 12: {
2,907,831✔
488
            ref_type table_names_ref = arr.get_as_ref_or_tagged(s_table_name_ndx).get_as_ref();
2,907,831✔
489
            ref_type tables_ref = arr.get_as_ref_or_tagged(s_table_refs_ndx).get_as_ref();
2,907,831✔
490
            auto logical_file_size = arr.get_as_ref_or_tagged(s_file_size_ndx).get_as_int();
2,907,831✔
491

1,770,909✔
492
            // Logical file size must never exceed actual file size.
1,770,909✔
493
            auto file_size = alloc.get_baseline();
2,907,831✔
494
            if (logical_file_size > file_size) {
2,907,831✔
495
                std::string err = util::format("Invalid logical file size: %1, actual file size: %2, read lock file "
×
496
                                               "size: %3, read lock version: %4",
×
497
                                               logical_file_size, file_size, read_lock_file_size, read_lock_version);
×
498
                throw InvalidDatabase(err, "");
×
UNCOV
499
            }
×
500
            // First two entries must be valid refs pointing inside the file
1,770,909✔
501
            auto invalid_ref = [logical_file_size](ref_type ref) {
5,812,353✔
502
                return ref == 0 || (ref & 7) || ref > logical_file_size;
5,813,121✔
503
            };
5,812,353✔
504
            if (invalid_ref(table_names_ref) || invalid_ref(tables_ref)) {
2,907,933✔
505
                std::string err = util::format(
×
506
                    "Invalid top array (top_ref, [0], [1]): %1, %2, %3, read lock size: %4, read lock version: %5",
×
507
                    top_ref, table_names_ref, tables_ref, read_lock_file_size, read_lock_version);
×
508
                throw InvalidDatabase(err, "");
×
UNCOV
509
            }
×
510
            break;
2,907,831✔
511
        }
2,907,831✔
512
        default: {
1,770,909✔
513
            auto logical_file_size = arr.get_as_ref_or_tagged(s_file_size_ndx).get_as_int();
×
514
            std::string err =
×
515
                util::format("Invalid top array size (ref: %1, array size: %2) file size: %3, read "
×
516
                             "lock size: %4, read lock version: %5",
×
517
                             top_ref, top_size, logical_file_size, read_lock_file_size, read_lock_version);
×
UNCOV
518
            throw InvalidDatabase(err, "");
×
519
            break;
1,770,909✔
520
        }
2,907,831✔
521
    }
2,908,944✔
522
}
2,908,944✔
523

524
void Group::attach(ref_type top_ref, bool writable, bool create_group_when_missing, size_t file_size,
525
                   uint_fast64_t version)
526
{
2,983,461✔
527
    REALM_ASSERT(!m_top.is_attached());
2,983,461✔
528
    if (create_group_when_missing)
2,983,461✔
529
        REALM_ASSERT(writable);
2,983,461✔
530

1,808,205✔
531
    // If this function throws, it must leave the group accesor in a the
1,808,205✔
532
    // unattached state.
1,808,205✔
533

1,808,205✔
534
    m_tables.detach();
2,983,461✔
535
    m_table_names.detach();
2,983,461✔
536
    m_is_writable = writable;
2,983,461✔
537

1,808,205✔
538
    if (top_ref != 0) {
2,983,461✔
539
        m_top.init_from_ref(top_ref);
2,820,636✔
540
        validate_top_array(m_top, m_alloc, file_size, version);
2,820,636✔
541
        m_table_names.init_from_parent();
2,820,636✔
542
        m_tables.init_from_parent();
2,820,636✔
543
    }
2,820,636✔
544
    else if (create_group_when_missing) {
162,825✔
545
        create_empty_group(); // Throws
14,286✔
546
    }
14,286✔
547
    m_attached = true;
2,983,461✔
548
    set_size();
2,983,461✔
549

1,808,205✔
550
    size_t sz = m_tables.is_attached() ? m_tables.size() : 0;
2,907,483✔
551
    while (m_table_accessors.size() > sz) {
2,983,566✔
552
        if (Table* t = m_table_accessors.back()) {
105✔
553
            t->detach(Table::cookie_void);
105✔
554
            recycle_table_accessor(t);
105✔
555
        }
105✔
556
        m_table_accessors.pop_back();
105✔
557
    }
105✔
558
    while (m_table_accessors.size() < sz) {
11,378,835✔
559
        m_table_accessors.emplace_back();
8,395,374✔
560
    }
8,395,374✔
561
}
2,983,461✔
562

563

564
void Group::detach() noexcept
565
{
2,697,606✔
566
    detach_table_accessors();
2,697,606✔
567
    m_table_accessors.clear();
2,697,606✔
568

1,622,367✔
569
    m_table_names.detach();
2,697,606✔
570
    m_tables.detach();
2,697,606✔
571
    m_top.detach();
2,697,606✔
572

1,622,367✔
573
    m_attached = false;
2,697,606✔
574
}
2,697,606✔
575

576
void Group::attach_shared(ref_type new_top_ref, size_t new_file_size, bool writable, VersionID version)
577
{
2,697,534✔
578
    REALM_ASSERT_3(new_top_ref, <, new_file_size);
2,697,534✔
579
    REALM_ASSERT(!is_attached());
2,697,534✔
580

1,624,818✔
581
    // update readers view of memory
1,624,818✔
582
    m_alloc.update_reader_view(new_file_size); // Throws
2,697,534✔
583
    update_allocator_wrappers(writable);
2,697,534✔
584

1,624,818✔
585
    // When `new_top_ref` is null, ask attach() to create a new node structure
1,624,818✔
586
    // for an empty group, but only during the initiation of write
1,624,818✔
587
    // transactions. When the transaction being initiated is a read transaction,
1,624,818✔
588
    // we instead have to leave array accessors m_top, m_tables, and
1,624,818✔
589
    // m_table_names in their detached state, as there are no underlying array
1,624,818✔
590
    // nodes to attached them to. In the case of write transactions, the nodes
1,624,818✔
591
    // have to be created, as they have to be ready for being modified.
1,624,818✔
592
    bool create_group_when_missing = writable;
2,697,534✔
593
    attach(new_top_ref, writable, create_group_when_missing, new_file_size, version.version); // Throws
2,697,534✔
594
}
2,697,534✔
595

596

597
void Group::detach_table_accessors() noexcept
598
{
2,698,050✔
599
    for (auto& table_accessor : m_table_accessors) {
8,727,486✔
600
        if (Table* t = table_accessor) {
8,727,486✔
601
            t->detach(Table::cookie_transaction_ended);
5,102,928✔
602
            recycle_table_accessor(t);
5,102,928✔
603
            table_accessor = nullptr;
5,102,928✔
604
        }
5,102,928✔
605
    }
8,727,486✔
606
}
2,698,050✔
607

608

609
void Group::create_empty_group()
610
{
79,065✔
611
    m_top.create(Array::type_HasRefs); // Throws
79,065✔
612
    _impl::DeepArrayDestroyGuard dg_top(&m_top);
79,065✔
613
    {
79,065✔
614
        m_table_names.create(); // Throws
79,065✔
615
        _impl::DestroyGuard<ArrayStringShort> dg(&m_table_names);
79,065✔
616
        m_top.add(m_table_names.get_ref()); // Throws
79,065✔
617
        dg.release();
79,065✔
618
    }
79,065✔
619
    {
79,065✔
620
        m_tables.create(Array::type_HasRefs); // Throws
79,065✔
621
        _impl::DestroyGuard<Array> dg(&m_tables);
79,065✔
622
        m_top.add(m_tables.get_ref()); // Throws
79,065✔
623
        dg.release();
79,065✔
624
    }
79,065✔
625
    size_t initial_logical_file_size = sizeof(SlabAlloc::Header);
79,065✔
626
    m_top.add(RefOrTagged::make_tagged(initial_logical_file_size)); // Throws
79,065✔
627
    dg_top.release();
79,065✔
628
}
79,065✔
629

630

631
Table* Group::do_get_table(size_t table_ndx)
632
{
34,860,690✔
633
    REALM_ASSERT(m_table_accessors.size() == m_tables.size());
34,860,690✔
634
    // Get table accessor from cache if it exists, else create
18,403,992✔
635
    Table* table = load_atomic(m_table_accessors[table_ndx], std::memory_order_acquire);
34,860,690✔
636
    if (!table) {
34,860,690✔
637
        // double-checked locking idiom
2,677,239✔
638
        std::lock_guard<std::mutex> lock(m_accessor_mutex);
4,786,383✔
639
        table = m_table_accessors[table_ndx];
4,786,383✔
640
        if (!table)
4,786,383✔
641
            table = create_table_accessor(table_ndx); // Throws
4,778,880✔
642
    }
4,786,383✔
643
    return table;
34,860,690✔
644
}
34,860,690✔
645

646

647
Table* Group::do_get_table(StringData name)
648
{
9,366,417✔
649
    if (!m_table_names.is_attached())
9,366,417✔
650
        return 0;
914,967✔
651
    size_t table_ndx = m_table_names.find_first(name);
8,451,450✔
652
    if (table_ndx == not_found)
8,451,450✔
653
        return 0;
378,282✔
654

4,042,125✔
655
    Table* table = do_get_table(table_ndx); // Throws
8,073,168✔
656
    return table;
8,073,168✔
657
}
8,073,168✔
658

659
TableRef Group::add_table_with_primary_key(StringData name, DataType pk_type, StringData pk_name, bool nullable,
660
                                           Table::Type table_type)
661
{
129,951✔
662
    check_attached();
129,951✔
663
    check_table_name_uniqueness(name);
129,951✔
664

64,383✔
665
    auto table = do_add_table(name, table_type, false);
129,951✔
666

64,383✔
667
    // Add pk column - without replication
64,383✔
668
    ColumnAttrMask attr;
129,951✔
669
    if (nullable)
129,951✔
670
        attr.set(col_attr_Nullable);
12,756✔
671
    ColKey pk_col = table->generate_col_key(ColumnType(pk_type), attr);
129,951✔
672
    table->do_insert_root_column(pk_col, ColumnType(pk_type), pk_name);
129,951✔
673
    table->do_set_primary_key_column(pk_col);
129,951✔
674

64,383✔
675
    if (Replication* repl = *get_repl())
129,951✔
676
        repl->add_class_with_primary_key(table->get_key(), name, pk_type, pk_name, nullable, table_type);
129,213✔
677

64,383✔
678
    return TableRef(table, table->m_alloc.get_instance_version());
129,951✔
679
}
129,951✔
680

681
Table* Group::do_add_table(StringData name, Table::Type table_type, bool do_repl)
682
{
338,211✔
683
    if (!m_is_writable)
338,211✔
684
        throw LogicError(ErrorCodes::ReadOnlyDB, "Database not writable");
6✔
685

167,793✔
686
    // get new key and index
167,793✔
687
    // find first empty spot:
167,793✔
688
    uint32_t j;
338,205✔
689
    RefOrTagged rot = RefOrTagged::make_tagged(0);
338,205✔
690
    for (j = 0; j < m_tables.size(); ++j) {
48,064,947✔
691
        rot = m_tables.get_as_ref_or_tagged(j);
47,731,167✔
692
        if (!rot.is_ref())
47,731,167✔
693
            break;
4,425✔
694
    }
47,731,167✔
695
    bool gen_null_tag = (j == m_tables.size()); // new tags start at zero
338,205✔
696
    uint32_t tag = gen_null_tag ? 0 : uint32_t(rot.get_as_int());
335,958✔
697
    TableKey key = TableKey((tag << 16) | j);
338,205✔
698

167,793✔
699
    if (REALM_UNLIKELY(name.size() > max_table_name_length))
338,205✔
700
        throw InvalidArgument(ErrorCodes::InvalidName, util::format("Name too long: %1", name));
167,796✔
701

167,790✔
702
    using namespace _impl;
338,199✔
703
    size_t table_ndx = key2ndx(key);
338,199✔
704
    ref_type ref = Table::create_empty_table(m_alloc, key); // Throws
338,199✔
705
    REALM_ASSERT_3(m_tables.size(), ==, m_table_names.size());
338,199✔
706

167,790✔
707
    rot = RefOrTagged::make_ref(ref);
338,199✔
708
    REALM_ASSERT(m_table_accessors.size() == m_tables.size());
338,199✔
709

167,790✔
710
    if (table_ndx == m_tables.size()) {
338,199✔
711
        m_tables.add(rot);
333,777✔
712
        m_table_names.add(name);
333,777✔
713
        // Need new slot for table accessor
165,606✔
714
        m_table_accessors.push_back(nullptr);
333,777✔
715
    }
333,777✔
716
    else {
4,422✔
717
        m_tables.set(table_ndx, rot);       // Throws
4,422✔
718
        m_table_names.set(table_ndx, name); // Throws
4,422✔
719
    }
4,422✔
720

167,790✔
721
    Replication* repl = *get_repl();
338,199✔
722
    if (do_repl && repl)
338,199✔
723
        repl->add_class(key, name, table_type);
202,656✔
724

167,790✔
725
    ++m_num_tables;
338,199✔
726

167,790✔
727
    Table* table = create_table_accessor(j);
338,199✔
728
    table->do_set_table_type(table_type);
338,199✔
729

167,790✔
730
    return table;
338,199✔
731
}
338,199✔
732

733

734
Table* Group::create_table_accessor(size_t table_ndx)
735
{
5,116,743✔
736
    REALM_ASSERT(m_tables.size() == m_table_accessors.size());
5,116,743✔
737
    REALM_ASSERT(table_ndx < m_table_accessors.size());
5,116,743✔
738

2,843,409✔
739
    RefOrTagged rot = m_tables.get_as_ref_or_tagged(table_ndx);
5,116,743✔
740
    ref_type ref = rot.get_as_ref();
5,116,743✔
741
    if (ref == 0) {
5,116,743✔
742
        throw NoSuchTable();
×
UNCOV
743
    }
×
744
    Table* table = 0;
5,116,743✔
745
    {
5,116,743✔
746
        std::lock_guard<std::mutex> lg(g_table_recycler_mutex);
5,116,743✔
747
        if (g_table_recycler_2.empty()) {
5,116,743✔
748
            while (!g_table_recycler_1.empty()) {
5,144,043✔
749
                auto t = g_table_recycler_1.back();
5,106,024✔
750
                g_table_recycler_1.pop_back();
5,106,024✔
751
                g_table_recycler_2.push_back(t);
5,106,024✔
752
            }
5,106,024✔
753
        }
38,019✔
754
        if (g_table_recycler_2.size() + g_table_recycler_1.size() > g_table_recycling_delay) {
5,116,743✔
755
            table = g_table_recycler_2.back();
5,088,378✔
756
            table->fully_detach();
5,088,378✔
757
            g_table_recycler_2.pop_back();
5,088,378✔
758
        }
5,088,378✔
759
    }
5,116,743✔
760
    if (table) {
5,116,743✔
761
        table->revive(get_repl(), m_alloc, m_is_writable);
5,088,279✔
762
        table->init(ref, this, table_ndx, m_is_writable, is_frozen());
5,088,279✔
763
    }
5,088,279✔
764
    else {
28,464✔
765
        std::unique_ptr<Table> new_table(new Table(get_repl(), m_alloc));  // Throws
28,464✔
766
        new_table->init(ref, this, table_ndx, m_is_writable, is_frozen()); // Throws
28,464✔
767
        table = new_table.release();
28,464✔
768
    }
28,464✔
769
    table->refresh_index_accessors();
5,116,743✔
770
    // must be atomic to allow concurrent probing of the m_table_accessors vector.
2,843,409✔
771
    store_atomic(m_table_accessors[table_ndx], table, std::memory_order_release);
5,116,743✔
772
    return table;
5,116,743✔
773
}
5,116,743✔
774

775

776
void Group::recycle_table_accessor(Table* to_be_recycled)
777
{
5,116,956✔
778
    std::lock_guard<std::mutex> lg(g_table_recycler_mutex);
5,116,956✔
779
    g_table_recycler_1.push_back(to_be_recycled);
5,116,956✔
780
}
5,116,956✔
781

782
void Group::remove_table(StringData name)
783
{
7,548✔
784
    check_attached();
7,548✔
785
    size_t table_ndx = m_table_names.find_first(name);
7,548✔
786
    if (table_ndx == not_found)
7,548✔
787
        throw NoSuchTable();
6✔
788
    auto key = ndx2key(table_ndx);
7,542✔
789
    remove_table(table_ndx, key); // Throws
7,542✔
790
}
7,542✔
791

792

793
void Group::remove_table(TableKey key)
794
{
129✔
795
    check_attached();
129✔
796

63✔
797
    size_t table_ndx = key2ndx_checked(key);
129✔
798
    remove_table(table_ndx, key);
129✔
799
}
129✔
800

801

802
void Group::remove_table(size_t table_ndx, TableKey key)
803
{
7,671✔
804
    if (!m_is_writable)
7,671✔
UNCOV
805
        throw LogicError(ErrorCodes::ReadOnlyDB, "Database not writable");
×
806
    REALM_ASSERT_3(m_tables.size(), ==, m_table_names.size());
7,671✔
807
    REALM_ASSERT(table_ndx < m_tables.size());
7,671✔
808
    TableRef table = get_table(key);
7,671✔
809

3,684✔
810
    // In principle we could remove a table even if it is the target of link
3,684✔
811
    // columns of other tables, however, to do that, we would have to
3,684✔
812
    // automatically remove the "offending" link columns from those other
3,684✔
813
    // tables. Such a behaviour is deemed too obscure, and we shall therefore
3,684✔
814
    // require that a removed table does not contain foreign origin backlink
3,684✔
815
    // columns.
3,684✔
816
    if (table->is_cross_table_link_target())
7,671✔
817
        throw CrossTableLinkTarget(table->get_name());
27✔
818

3,672✔
819
    {
7,644✔
820
        // We don't want to replicate the individual column removals along the
3,672✔
821
        // way as they're covered by the table removal
3,672✔
822
        Table::DisableReplication dr(*table);
7,644✔
823
        for (size_t i = table->get_column_count(); i > 0; --i) {
24,621✔
824
            ColKey col_key = table->spec_ndx2colkey(i - 1);
16,977✔
825
            table->remove_column(col_key);
16,977✔
826
        }
16,977✔
827
    }
7,644✔
828

3,672✔
829
    size_t prior_num_tables = m_tables.size();
7,644✔
830
    Replication* repl = *get_repl();
7,644✔
831
    if (repl)
7,644✔
832
        repl->erase_class(key, table->get_name(), prior_num_tables); // Throws
7,566✔
833

3,672✔
834
    int64_t ref_64 = m_tables.get(table_ndx);
7,644✔
835
    REALM_ASSERT(!int_cast_has_overflow<ref_type>(ref_64));
7,644✔
836
    ref_type ref = ref_type(ref_64);
7,644✔
837

3,672✔
838
    // Replace entry in m_tables with next tag to use:
3,672✔
839
    RefOrTagged rot = RefOrTagged::make_tagged((1 + (key.value >> 16)) & 0x7FFF);
7,644✔
840
    // Remove table
3,672✔
841
    m_tables.set(table_ndx, rot);     // Throws
7,644✔
842
    m_table_names.set(table_ndx, {}); // Throws
7,644✔
843
    m_table_accessors[table_ndx] = nullptr;
7,644✔
844
    --m_num_tables;
7,644✔
845

3,672✔
846
    table->detach(Table::cookie_removed);
7,644✔
847
    // Destroy underlying node structure
3,672✔
848
    Array::destroy_deep(ref, m_alloc);
7,644✔
849
    recycle_table_accessor(table.unchecked_ptr());
7,644✔
850
}
7,644✔
851

852

853
void Group::rename_table(StringData name, StringData new_name, bool require_unique_name)
854
{
24✔
855
    check_attached();
24✔
856
    size_t table_ndx = m_table_names.find_first(name);
24✔
857
    if (table_ndx == not_found)
24✔
858
        throw NoSuchTable();
6✔
859
    rename_table(ndx2key(table_ndx), new_name, require_unique_name); // Throws
18✔
860
}
18✔
861

862

863
void Group::rename_table(TableKey key, StringData new_name, bool require_unique_name)
864
{
24✔
865
    check_attached();
24✔
866
    if (!m_is_writable)
24✔
UNCOV
867
        throw LogicError(ErrorCodes::ReadOnlyDB, "Database not writable");
×
868
    REALM_ASSERT_3(m_tables.size(), ==, m_table_names.size());
24✔
869
    if (require_unique_name && has_table(new_name))
24✔
870
        throw TableNameInUse();
6✔
871
    size_t table_ndx = key2ndx_checked(key);
18✔
872
    m_table_names.set(table_ndx, new_name);
18✔
873
    if (Replication* repl = *get_repl())
18✔
UNCOV
874
        repl->rename_class(key, new_name); // Throws
×
875
}
18✔
876

877
Obj Group::get_object(ObjLink link)
878
{
149,655✔
879
    auto target_table = get_table(link.get_table_key());
149,655✔
880
    ObjKey key = link.get_obj_key();
149,655✔
881
    ClusterTree* ct = key.is_unresolved() ? target_table->m_tombstones.get() : &target_table->m_clusters;
145,026✔
882
    return ct->get(key);
149,655✔
883
}
149,655✔
884

885
Obj Group::try_get_object(ObjLink link) noexcept
UNCOV
886
{
×
UNCOV
887
    auto target_table = get_table(link.get_table_key());
×
UNCOV
888
    ObjKey key = link.get_obj_key();
×
UNCOV
889
    ClusterTree* ct = key.is_unresolved() ? target_table->m_tombstones.get() : &target_table->m_clusters;
×
UNCOV
890
    return ct->try_get_obj(key);
×
UNCOV
891
}
×
892

893
void Group::validate(ObjLink link) const
894
{
12,588✔
895
    if (auto tk = link.get_table_key()) {
12,588✔
896
        auto target_key = link.get_obj_key();
12,588✔
897
        auto target_table = get_table(tk);
12,588✔
898
        const ClusterTree* ct =
12,588✔
899
            target_key.is_unresolved() ? target_table->m_tombstones.get() : &target_table->m_clusters;
12,510✔
900
        if (!ct->is_valid(target_key)) {
12,588✔
901
            throw InvalidArgument(ErrorCodes::KeyNotFound, "Target object not found");
12✔
902
        }
12✔
903
        if (target_table->is_embedded()) {
12,576✔
904
            throw IllegalOperation("Cannot link to embedded object");
×
UNCOV
905
        }
×
906
        if (target_table->is_asymmetric()) {
12,576✔
907
            throw IllegalOperation("Cannot link to ephemeral object");
6✔
908
        }
6✔
909
    }
12,576✔
910
}
12,588✔
911

912
ref_type Group::DefaultTableWriter::write_names(_impl::OutputStream& out)
913
{
462✔
914
    bool deep = true;                                                 // Deep
462✔
915
    bool only_if_modified = false;                                    // Always
462✔
916
    return m_group->m_table_names.write(out, deep, only_if_modified); // Throws
462✔
917
}
462✔
918
ref_type Group::DefaultTableWriter::write_tables(_impl::OutputStream& out)
919
{
462✔
920
    bool deep = true;                                            // Deep
462✔
921
    bool only_if_modified = false;                               // Always
462✔
922
    return m_group->m_tables.write(out, deep, only_if_modified); // Throws
462✔
923
}
462✔
924

925
auto Group::DefaultTableWriter::write_history(_impl::OutputStream& out) -> HistoryInfo
926
{
228✔
927
    bool deep = true;              // Deep
228✔
928
    bool only_if_modified = false; // Always
228✔
929
    ref_type history_ref = _impl::GroupFriend::get_history_ref(*m_group);
228✔
930
    HistoryInfo info;
228✔
931
    if (history_ref) {
228✔
932
        _impl::History::version_type version;
192✔
933
        int history_type, history_schema_version;
192✔
934
        _impl::GroupFriend::get_version_and_history_info(_impl::GroupFriend::get_alloc(*m_group),
192✔
935
                                                         m_group->m_top.get_ref(), version, history_type,
192✔
936
                                                         history_schema_version);
192✔
937
        REALM_ASSERT(history_type != Replication::hist_None);
192✔
938
        if (!m_should_write_history ||
192✔
939
            (history_type != Replication::hist_SyncClient && history_type != Replication::hist_SyncServer)) {
189✔
940
            return info; // Only sync history should be preserved when writing to a new file
144✔
941
        }
144✔
942
        info.type = history_type;
48✔
943
        info.version = history_schema_version;
48✔
944
        Array history{const_cast<Allocator&>(_impl::GroupFriend::get_alloc(*m_group))};
48✔
945
        history.init_from_ref(history_ref);
48✔
946
        info.ref = history.write(out, deep, only_if_modified); // Throws
48✔
947
    }
48✔
948
    info.sync_file_id = m_group->get_sync_file_id();
156✔
949
    return info;
84✔
950
}
228✔
951

952
void Group::write(std::ostream& out, bool pad) const
953
{
42✔
954
    DefaultTableWriter table_writer;
42✔
955
    write(out, pad, 0, table_writer);
42✔
956
}
42✔
957

958
void Group::write(std::ostream& out, bool pad_for_encryption, uint_fast64_t version_number, TableWriter& writer) const
959
{
474✔
960
    REALM_ASSERT(is_attached());
474✔
961
    writer.set_group(this);
474✔
962
    bool no_top_array = !m_top.is_attached();
474✔
963
    write(out, m_file_format_version, writer, no_top_array, pad_for_encryption, version_number); // Throws
474✔
964
}
474✔
965

966
void Group::write(File& file, const char* encryption_key, uint_fast64_t version_number, TableWriter& writer) const
967
{
432✔
968
    REALM_ASSERT(file.get_size() == 0);
432✔
969

216✔
970
    file.set_encryption_key(encryption_key);
432✔
971

216✔
972
    // The aim is that the buffer size should be at least 1/256 of needed size but less than 64 Mb
216✔
973
    constexpr size_t upper_bound = 64 * 1024 * 1024;
432✔
974
    size_t min_space = std::min(get_used_space() >> 8, upper_bound);
432✔
975
    size_t buffer_size = 4096;
432✔
976
    while (buffer_size < min_space) {
504✔
977
        buffer_size <<= 1;
72✔
978
    }
72✔
979
    File::Streambuf streambuf(&file, buffer_size);
432✔
980

216✔
981
    std::ostream out(&streambuf);
432✔
982
    out.exceptions(std::ios_base::failbit | std::ios_base::badbit);
432✔
983
    write(out, encryption_key != 0, version_number, writer);
432✔
984
    int sync_status = streambuf.pubsync();
432✔
985
    REALM_ASSERT(sync_status == 0);
432✔
986
}
432✔
987

988
void Group::write(const std::string& path, const char* encryption_key, uint64_t version_number,
989
                  bool write_history) const
990
{
234✔
991
    File file;
234✔
992
    int flags = 0;
234✔
993
    file.open(path, File::access_ReadWrite, File::create_Must, flags);
234✔
994
    DefaultTableWriter table_writer(write_history);
234✔
995
    write(file, encryption_key, version_number, table_writer);
234✔
996
}
234✔
997

998

999
BinaryData Group::write_to_mem() const
1000
{
42✔
1001
    REALM_ASSERT(is_attached());
42✔
1002

21✔
1003
    // Get max possible size of buffer
21✔
1004
    size_t max_size = m_alloc.get_total_size();
42✔
1005

21✔
1006
    auto buffer = std::unique_ptr<char[]>(new (std::nothrow) char[max_size]);
42✔
1007
    if (!buffer)
42✔
UNCOV
1008
        throw Exception(ErrorCodes::OutOfMemory, "Could not allocate memory while dumping to memory");
×
1009
    MemoryOutputStream out; // Throws
42✔
1010
    out.set_buffer(buffer.get(), buffer.get() + max_size);
42✔
1011
    write(out); // Throws
42✔
1012
    size_t buffer_size = out.size();
42✔
1013
    return BinaryData(buffer.release(), buffer_size);
42✔
1014
}
42✔
1015

1016

1017
void Group::write(std::ostream& out, int file_format_version, TableWriter& table_writer, bool no_top_array,
1018
                  bool pad_for_encryption, uint_fast64_t version_number)
1019
{
474✔
1020
    _impl::OutputStream out_2(out);
474✔
1021

237✔
1022
    // Write the file header
237✔
1023
    SlabAlloc::Header streaming_header;
474✔
1024
    if (no_top_array) {
474✔
1025
        file_format_version = 0;
12✔
1026
    }
12✔
1027
    else if (file_format_version == 0) {
462✔
1028
        // Use current file format version
1029
        file_format_version = get_target_file_format_version_for_session(0, Replication::hist_None);
×
UNCOV
1030
    }
×
1031
    SlabAlloc::init_streaming_header(&streaming_header, file_format_version);
474✔
1032
    out_2.write(reinterpret_cast<const char*>(&streaming_header), sizeof streaming_header);
474✔
1033

237✔
1034
    ref_type top_ref = 0;
474✔
1035
    size_t final_file_size = sizeof streaming_header;
474✔
1036
    if (no_top_array) {
474✔
1037
        // Accept version number 1 as that number is (unfortunately) also used
6✔
1038
        // to denote the empty initial state of a Realm file.
6✔
1039
        REALM_ASSERT(version_number == 0 || version_number == 1);
12✔
1040
    }
12✔
1041
    else {
462✔
1042
        // Because we need to include the total logical file size in the
231✔
1043
        // top-array, we have to start by writing everything except the
231✔
1044
        // top-array, and then finally compute and write a correct version of
231✔
1045
        // the top-array. The free-space information of the group will only be
231✔
1046
        // included if a non-zero version number is given as parameter,
231✔
1047
        // indicating that versioning info is to be saved. This is used from
231✔
1048
        // DB to compact the database by writing only the live data
231✔
1049
        // into a separate file.
231✔
1050
        ref_type names_ref = table_writer.write_names(out_2);   // Throws
462✔
1051
        ref_type tables_ref = table_writer.write_tables(out_2); // Throws
462✔
1052
        SlabAlloc new_alloc;
462✔
1053
        new_alloc.attach_empty(); // Throws
462✔
1054
        Array top(new_alloc);
462✔
1055
        top.create(Array::type_HasRefs); // Throws
462✔
1056
        _impl::ShallowArrayDestroyGuard dg_top(&top);
462✔
1057
        int_fast64_t value_1 = from_ref(names_ref);
462✔
1058
        int_fast64_t value_2 = from_ref(tables_ref);
462✔
1059
        top.add(value_1); // Throws
462✔
1060
        top.add(value_2); // Throws
462✔
1061
        top.add(0);       // Throws
462✔
1062

231✔
1063
        int top_size = 3;
462✔
1064
        if (version_number) {
462✔
1065
            TableWriter::HistoryInfo history_info = table_writer.write_history(out_2); // Throws
228✔
1066

114✔
1067
            Array free_list(new_alloc);
228✔
1068
            Array size_list(new_alloc);
228✔
1069
            Array version_list(new_alloc);
228✔
1070
            free_list.create(Array::type_Normal); // Throws
228✔
1071
            _impl::DeepArrayDestroyGuard dg_1(&free_list);
228✔
1072
            size_list.create(Array::type_Normal); // Throws
228✔
1073
            _impl::DeepArrayDestroyGuard dg_2(&size_list);
228✔
1074
            version_list.create(Array::type_Normal); // Throws
228✔
1075
            _impl::DeepArrayDestroyGuard dg_3(&version_list);
228✔
1076
            bool deep = true;              // Deep
228✔
1077
            bool only_if_modified = false; // Always
228✔
1078
            ref_type free_list_ref = free_list.write(out_2, deep, only_if_modified);
228✔
1079
            ref_type size_list_ref = size_list.write(out_2, deep, only_if_modified);
228✔
1080
            ref_type version_list_ref = version_list.write(out_2, deep, only_if_modified);
228✔
1081
            top.add(RefOrTagged::make_ref(free_list_ref));     // Throws
228✔
1082
            top.add(RefOrTagged::make_ref(size_list_ref));     // Throws
228✔
1083
            top.add(RefOrTagged::make_ref(version_list_ref));  // Throws
228✔
1084
            top.add(RefOrTagged::make_tagged(version_number)); // Throws
228✔
1085
            top_size = 7;
228✔
1086

114✔
1087
            if (history_info.type != Replication::hist_None) {
228✔
1088
                top.add(RefOrTagged::make_tagged(history_info.type));
48✔
1089
                top.add(RefOrTagged::make_ref(history_info.ref));
48✔
1090
                top.add(RefOrTagged::make_tagged(history_info.version));
48✔
1091
                top.add(RefOrTagged::make_tagged(history_info.sync_file_id));
48✔
1092
                top_size = s_group_max_size;
48✔
1093
                // ^ this is too large, since the evacuation point entry is not there:
24✔
1094
                // (but the code below is self correcting)
24✔
1095
            }
48✔
1096
        }
228✔
1097
        top_ref = out_2.get_ref_of_next_array();
462✔
1098

231✔
1099
        // Produce a preliminary version of the top array whose
231✔
1100
        // representation is guaranteed to be able to hold the final file
231✔
1101
        // size
231✔
1102
        size_t max_top_byte_size = Array::get_max_byte_size(top_size);
462✔
1103
        size_t max_final_file_size = size_t(top_ref) + max_top_byte_size;
462✔
1104
        top.ensure_minimum_width(RefOrTagged::make_tagged(max_final_file_size)); // Throws
462✔
1105

231✔
1106
        // Finalize the top array by adding the projected final file size
231✔
1107
        // to it
231✔
1108
        size_t top_byte_size = top.get_byte_size();
462✔
1109
        final_file_size = size_t(top_ref) + top_byte_size;
462✔
1110
        top.set(2, RefOrTagged::make_tagged(final_file_size)); // Throws
462✔
1111

231✔
1112
        // Write the top array
231✔
1113
        bool deep = false;                        // Shallow
462✔
1114
        bool only_if_modified = false;            // Always
462✔
1115
        top.write(out_2, deep, only_if_modified); // Throws
462✔
1116
        REALM_ASSERT_3(size_t(out_2.get_ref_of_next_array()), ==, final_file_size);
462✔
1117

231✔
1118
        dg_top.reset(nullptr); // Destroy now
462✔
1119
    }
462✔
1120

237✔
1121
    // encryption will pad the file to a multiple of the page, so ensure the
237✔
1122
    // footer is aligned to the end of a page
237✔
1123
    if (pad_for_encryption) {
474✔
1124
#if REALM_ENABLE_ENCRYPTION
18✔
1125
        size_t unrounded_size = final_file_size + sizeof(SlabAlloc::StreamingFooter);
18✔
1126
        size_t rounded_size = round_up_to_page_size(unrounded_size);
18✔
1127
        if (rounded_size != unrounded_size) {
18✔
1128
            std::unique_ptr<char[]> buffer(new char[rounded_size - unrounded_size]());
18✔
1129
            out_2.write(buffer.get(), rounded_size - unrounded_size);
18✔
1130
        }
18✔
1131
#endif
18✔
1132
    }
18✔
1133

237✔
1134
    // Write streaming footer
237✔
1135
    SlabAlloc::StreamingFooter footer;
474✔
1136
    footer.m_top_ref = top_ref;
474✔
1137
    footer.m_magic_cookie = SlabAlloc::footer_magic_cookie;
474✔
1138
    out_2.write(reinterpret_cast<const char*>(&footer), sizeof footer);
474✔
1139
}
474✔
1140

1141

1142
void Group::update_refs(ref_type top_ref) noexcept
1143
{
389,646✔
1144
    // After Group::commit() we will always have free space tracking
193,344✔
1145
    // info.
193,344✔
1146
    REALM_ASSERT_3(m_top.size(), >=, 5);
389,646✔
1147

193,344✔
1148
    m_top.init_from_ref(top_ref);
389,646✔
1149

193,344✔
1150
    // Now we can update it's child arrays
193,344✔
1151
    m_table_names.update_from_parent();
389,646✔
1152
    m_tables.update_from_parent();
389,646✔
1153

193,344✔
1154
    // Update all attached table accessors.
193,344✔
1155
    for (auto& table_accessor : m_table_accessors) {
1,325,970✔
1156
        if (table_accessor) {
1,325,970✔
1157
            table_accessor->update_from_parent();
813,846✔
1158
        }
813,846✔
1159
    }
1,325,970✔
1160
}
389,646✔
1161

1162
bool Group::operator==(const Group& g) const
1163
{
66✔
1164
    for (auto tk : get_table_keys()) {
138✔
1165
        const StringData& table_name = get_table_name(tk);
138✔
1166

69✔
1167
        ConstTableRef table_1 = get_table(tk);
138✔
1168
        ConstTableRef table_2 = g.get_table(table_name);
138✔
1169
        if (!table_2)
138✔
1170
            return false;
12✔
1171
        if (table_1->get_primary_key_column().get_type() != table_2->get_primary_key_column().get_type()) {
126✔
1172
            return false;
×
UNCOV
1173
        }
×
1174
        if (table_1->is_embedded() != table_2->is_embedded())
126✔
UNCOV
1175
            return false;
×
1176
        if (table_1->is_embedded())
126✔
1177
            continue;
60✔
1178

33✔
1179
        if (*table_1 != *table_2)
66✔
1180
            return false;
18✔
1181
    }
66✔
1182
    return true;
51✔
1183
}
66✔
1184
size_t Group::get_used_space() const noexcept
1185
{
450✔
1186
    if (!m_top.is_attached())
450✔
1187
        return 0;
12✔
1188

219✔
1189
    size_t used_space = (size_t(m_top.get(2)) >> 1);
438✔
1190

219✔
1191
    if (m_top.size() > 4) {
438✔
1192
        Array free_lengths(const_cast<SlabAlloc&>(m_alloc));
312✔
1193
        free_lengths.init_from_ref(ref_type(m_top.get(4)));
312✔
1194
        used_space -= size_t(free_lengths.get_sum());
312✔
1195
    }
312✔
1196

219✔
1197
    return used_space;
438✔
1198
}
438✔
1199

1200

1201
namespace {
1202
class TransactAdvancer : public _impl::NullInstructionObserver {
1203
public:
1204
    TransactAdvancer(Group&, bool& schema_changed)
1205
        : m_schema_changed(schema_changed)
1206
    {
44,766✔
1207
    }
44,766✔
1208

1209
    bool insert_group_level_table(TableKey) noexcept
1210
    {
11,481✔
1211
        m_schema_changed = true;
11,481✔
1212
        return true;
11,481✔
1213
    }
11,481✔
1214

1215
    bool erase_class(TableKey) noexcept
1216
    {
×
1217
        m_schema_changed = true;
×
UNCOV
1218
        return true;
×
UNCOV
1219
    }
×
1220

1221
    bool rename_class(TableKey) noexcept
UNCOV
1222
    {
×
UNCOV
1223
        m_schema_changed = true;
×
UNCOV
1224
        return true;
×
UNCOV
1225
    }
×
1226

1227
    bool insert_column(ColKey)
1228
    {
36,126✔
1229
        m_schema_changed = true;
36,126✔
1230
        return true;
36,126✔
1231
    }
36,126✔
1232

1233
    bool erase_column(ColKey)
UNCOV
1234
    {
×
UNCOV
1235
        m_schema_changed = true;
×
UNCOV
1236
        return true;
×
UNCOV
1237
    }
×
1238

1239
    bool rename_column(ColKey) noexcept
UNCOV
1240
    {
×
UNCOV
1241
        m_schema_changed = true;
×
UNCOV
1242
        return true; // No-op
×
UNCOV
1243
    }
×
1244

1245
private:
1246
    bool& m_schema_changed;
1247
};
1248
} // anonymous namespace
1249

1250

1251
void Group::update_allocator_wrappers(bool writable)
1252
{
5,626,527✔
1253
    m_is_writable = writable;
5,626,527✔
1254
    for (size_t i = 0; i < m_table_accessors.size(); ++i) {
10,862,289✔
1255
        auto table_accessor = m_table_accessors[i];
5,235,762✔
1256
        if (table_accessor) {
5,235,762✔
1257
            table_accessor->update_allocator_wrapper(writable);
3,592,206✔
1258
        }
3,592,206✔
1259
    }
5,235,762✔
1260
}
5,626,527✔
1261

1262
void Group::flush_accessors_for_commit()
1263
{
1,362,624✔
1264
    for (auto& acc : m_table_accessors)
1,362,624✔
1265
        if (acc)
5,516,136✔
1266
            acc->flush_for_commit();
3,968,967✔
1267
}
1,362,624✔
1268

1269
void Group::refresh_dirty_accessors()
1270
{
278,427✔
1271
    if (!m_tables.is_attached()) {
278,427✔
1272
        m_table_accessors.clear();
54✔
1273
        return;
54✔
1274
    }
54✔
1275

178,314✔
1276
    // The array of Tables cannot have shrunk:
178,314✔
1277
    REALM_ASSERT(m_tables.size() >= m_table_accessors.size());
278,373✔
1278

178,314✔
1279
    // but it may have grown - and if so, we must resize the accessor array to match
178,314✔
1280
    if (m_tables.size() > m_table_accessors.size()) {
278,373✔
UNCOV
1281
        m_table_accessors.resize(m_tables.size());
×
UNCOV
1282
    }
×
1283

178,314✔
1284
    // Update all attached table accessors.
178,314✔
1285
    for (size_t i = 0; i < m_table_accessors.size(); ++i) {
835,047✔
1286
        auto& table_accessor = m_table_accessors[i];
556,674✔
1287
        if (table_accessor) {
556,674✔
1288
            // If the table has changed it's key in the file, it's a
231,444✔
1289
            // new table. This will detach the old accessor and remove it.
231,444✔
1290
            RefOrTagged rot = m_tables.get_as_ref_or_tagged(i);
381,462✔
1291
            bool same_table = false;
381,462✔
1292
            if (rot.is_ref()) {
381,654✔
1293
                auto ref = rot.get_as_ref();
381,648✔
1294
                TableKey new_key = Table::get_key_direct(m_alloc, ref);
381,648✔
1295
                if (new_key == table_accessor->get_key())
381,648✔
1296
                    same_table = true;
381,474✔
1297
            }
381,648✔
1298
            if (same_table) {
381,471✔
1299
                table_accessor->refresh_accessor_tree();
381,465✔
1300
            }
381,465✔
1301
            else {
2,147,483,653✔
1302
                table_accessor->detach(Table::cookie_removed);
2,147,483,653✔
1303
                recycle_table_accessor(table_accessor);
2,147,483,653✔
1304
                m_table_accessors[i] = nullptr;
2,147,483,653✔
1305
            }
2,147,483,653✔
1306
        }
381,462✔
1307
    }
556,674✔
1308
}
278,373✔
1309

1310

1311
void Group::advance_transact(ref_type new_top_ref, util::InputStream* in, bool writable)
1312
{
279,105✔
1313
    REALM_ASSERT(is_attached());
279,105✔
1314
    // Exception safety: If this function throws, the group accessor and all of
178,971✔
1315
    // its subordinate accessors are left in a state that may not be fully
178,971✔
1316
    // consistent. Only minimal consistency is guaranteed (see
178,971✔
1317
    // AccessorConsistencyLevels). In this case, the application is required to
178,971✔
1318
    // either destroy the Group object, forcing all subordinate accessors to
178,971✔
1319
    // become detached, or take some other equivalent action that involves a
178,971✔
1320
    // call to Group::detach(), such as terminating the transaction in progress.
178,971✔
1321
    // such actions will also lead to the detachment of all subordinate
178,971✔
1322
    // accessors. Until then it is an error, and unsafe if the application
178,971✔
1323
    // attempts to access the group one of its subordinate accessors.
178,971✔
1324
    //
178,971✔
1325
    // The purpose of this function is to refresh all attached accessors after
178,971✔
1326
    // the underlying node structure has undergone arbitrary change, such as
178,971✔
1327
    // when a read transaction has been advanced to a later snapshot of the
178,971✔
1328
    // database.
178,971✔
1329
    //
178,971✔
1330
    // Initially, when this function is invoked, we cannot assume any
178,971✔
1331
    // correspondence between the accessor state and the underlying node
178,971✔
1332
    // structure. We can assume that the hierarchy is in a state of minimal
178,971✔
1333
    // consistency, and that it can be brought to a state of structural
178,971✔
1334
    // correspondence using information in the transaction logs. When structural
178,971✔
1335
    // correspondence is achieved, we can reliably refresh the accessor hierarchy
178,971✔
1336
    // (Table::refresh_accessor_tree()) to bring it back to a fully consistent
178,971✔
1337
    // state. See AccessorConsistencyLevels.
178,971✔
1338
    //
178,971✔
1339
    // Much of the information in the transaction logs is not used in this
178,971✔
1340
    // process, because the changes have already been applied to the underlying
178,971✔
1341
    // node structure. All we need to do here is to bring the accessors back
178,971✔
1342
    // into a state where they correctly reflect the underlying structure (or
178,971✔
1343
    // detach them if the underlying object has been removed.)
178,971✔
1344
    //
178,971✔
1345
    // This is no longer needed in Core, but we need to compute "schema_changed",
178,971✔
1346
    // for the benefit of ObjectStore.
178,971✔
1347
    bool schema_changed = false;
279,105✔
1348
    if (in && has_schema_change_notification_handler()) {
279,105✔
1349
        TransactAdvancer advancer(*this, schema_changed);
44,766✔
1350
        _impl::TransactLogParser parser; // Throws
44,766✔
1351
        parser.parse(*in, advancer);     // Throws
44,766✔
1352
    }
44,766✔
1353

178,971✔
1354
    m_top.detach();                                           // Soft detach
279,105✔
1355
    bool create_group_when_missing = false;                   // See Group::attach_shared().
279,105✔
1356
    attach(new_top_ref, writable, create_group_when_missing); // Throws
279,105✔
1357
    refresh_dirty_accessors();                                // Throws
279,105✔
1358

178,971✔
1359
    if (schema_changed)
279,105✔
1360
        send_schema_change_notification();
10,605✔
1361
}
279,105✔
1362

1363
void Group::prepare_top_for_history(int history_type, int history_schema_version, uint64_t file_ident)
1364
{
69,324✔
1365
    REALM_ASSERT(m_file_format_version >= 7);
69,324✔
1366
    while (m_top.size() < s_hist_type_ndx) {
345,636✔
1367
        m_top.add(0); // Throws
276,312✔
1368
    }
276,312✔
1369

34,167✔
1370
    if (m_top.size() > s_hist_version_ndx) {
69,324✔
1371
        int stored_history_type = int(m_top.get_as_ref_or_tagged(s_hist_type_ndx).get_as_int());
204✔
1372
        int stored_history_schema_version = int(m_top.get_as_ref_or_tagged(s_hist_version_ndx).get_as_int());
204✔
1373
        if (stored_history_type != Replication::hist_None) {
204✔
1374
            REALM_ASSERT(stored_history_type == history_type);
6✔
1375
            REALM_ASSERT(stored_history_schema_version == history_schema_version);
6✔
1376
        }
6✔
1377
        m_top.set(s_hist_type_ndx, RefOrTagged::make_tagged(history_type));              // Throws
204✔
1378
        m_top.set(s_hist_version_ndx, RefOrTagged::make_tagged(history_schema_version)); // Throws
204✔
1379
    }
204✔
1380
    else {
69,120✔
1381
        // No history yet
34,065✔
1382
        REALM_ASSERT(m_top.size() == s_hist_type_ndx);
69,120✔
1383
        ref_type history_ref = 0;                                    // No history yet
69,120✔
1384
        m_top.add(RefOrTagged::make_tagged(history_type));           // Throws
69,120✔
1385
        m_top.add(RefOrTagged::make_ref(history_ref));               // Throws
69,120✔
1386
        m_top.add(RefOrTagged::make_tagged(history_schema_version)); // Throws
69,120✔
1387
    }
69,120✔
1388

34,167✔
1389
    if (m_top.size() > s_sync_file_id_ndx) {
69,324✔
1390
        m_top.set(s_sync_file_id_ndx, RefOrTagged::make_tagged(file_ident));
42✔
1391
    }
42✔
1392
    else {
69,282✔
1393
        m_top.add(RefOrTagged::make_tagged(file_ident)); // Throws
69,282✔
1394
    }
69,282✔
1395
}
69,324✔
1396

1397
void Group::clear_history()
1398
{
36✔
1399
    bool has_history = (m_top.is_attached() && m_top.size() > s_hist_type_ndx);
36✔
1400
    if (has_history) {
36✔
1401
        auto hist_ref = m_top.get_as_ref(s_hist_ref_ndx);
36✔
1402
        Array::destroy_deep(hist_ref, m_top.get_alloc());
36✔
1403
        m_top.set(s_hist_type_ndx, RefOrTagged::make_tagged(Replication::hist_None)); // Throws
36✔
1404
        m_top.set(s_hist_version_ndx, RefOrTagged::make_tagged(0));                   // Throws
36✔
1405
        m_top.set(s_hist_ref_ndx, 0);                                                 // Throws
36✔
1406
    }
36✔
1407
}
36✔
1408

1409
#ifdef REALM_DEBUG // LCOV_EXCL_START ignore debug functions
1410

1411
class MemUsageVerifier : public Array::MemUsageHandler {
1412
public:
1413
    MemUsageVerifier(ref_type ref_begin, ref_type immutable_ref_end, ref_type mutable_ref_end, ref_type baseline)
1414
        : m_ref_begin(ref_begin)
1415
        , m_immutable_ref_end(immutable_ref_end)
1416
        , m_mutable_ref_end(mutable_ref_end)
1417
        , m_baseline(baseline)
1418
    {
1,279,620✔
1419
    }
1,279,620✔
1420
    void add_immutable(ref_type ref, size_t size)
1421
    {
12,674,907✔
1422
        REALM_ASSERT_3(ref % 8, ==, 0);  // 8-byte alignment
12,674,907✔
1423
        REALM_ASSERT_3(size % 8, ==, 0); // 8-byte alignment
12,674,907✔
1424
        REALM_ASSERT_3(size, >, 0);
12,674,907✔
1425
        REALM_ASSERT_3(ref, >=, m_ref_begin);
12,674,907✔
1426
        REALM_ASSERT_3(size, <=, m_immutable_ref_end - ref);
12,674,907✔
1427
        Chunk chunk;
12,674,907✔
1428
        chunk.ref = ref;
12,674,907✔
1429
        chunk.size = size;
12,674,907✔
1430
        m_chunks.push_back(chunk);
12,674,907✔
1431
    }
12,674,907✔
1432
    void add_mutable(ref_type ref, size_t size)
1433
    {
5,468,889✔
1434
        REALM_ASSERT_3(ref % 8, ==, 0);  // 8-byte alignment
5,468,889✔
1435
        REALM_ASSERT_3(size % 8, ==, 0); // 8-byte alignment
5,468,889✔
1436
        REALM_ASSERT_3(size, >, 0);
5,468,889✔
1437
        REALM_ASSERT_3(ref, >=, m_immutable_ref_end);
5,468,889✔
1438
        REALM_ASSERT_3(size, <=, m_mutable_ref_end - ref);
5,468,889✔
1439
        Chunk chunk;
5,468,889✔
1440
        chunk.ref = ref;
5,468,889✔
1441
        chunk.size = size;
5,468,889✔
1442
        m_chunks.push_back(chunk);
5,468,889✔
1443
    }
5,468,889✔
1444
    void add(ref_type ref, size_t size)
1445
    {
52,381,773✔
1446
        REALM_ASSERT_3(ref % 8, ==, 0);  // 8-byte alignment
52,381,773✔
1447
        REALM_ASSERT_3(size % 8, ==, 0); // 8-byte alignment
52,381,773✔
1448
        REALM_ASSERT_3(size, >, 0);
52,381,773✔
1449
        REALM_ASSERT_3(ref, >=, m_ref_begin);
52,381,773✔
1450
        REALM_ASSERT(size <= (ref < m_baseline ? m_immutable_ref_end : m_mutable_ref_end) - ref);
52,381,773✔
1451
        Chunk chunk;
52,381,773✔
1452
        chunk.ref = ref;
52,381,773✔
1453
        chunk.size = size;
52,381,773✔
1454
        m_chunks.push_back(chunk);
52,381,773✔
1455
    }
52,381,773✔
1456
    void add(const MemUsageVerifier& verifier)
1457
    {
1,917,102✔
1458
        m_chunks.insert(m_chunks.end(), verifier.m_chunks.begin(), verifier.m_chunks.end());
1,917,102✔
1459
    }
1,917,102✔
1460
    void handle(ref_type ref, size_t allocated, size_t) override
1461
    {
52,381,776✔
1462
        add(ref, allocated);
52,381,776✔
1463
    }
52,381,776✔
1464
    void canonicalize()
1465
    {
5,113,824✔
1466
        // Sort the chunks in order of increasing ref, then merge adjacent
2,557,050✔
1467
        // chunks while checking that there is no overlap
2,557,050✔
1468
        typedef std::vector<Chunk>::iterator iter;
5,113,824✔
1469
        iter i_1 = m_chunks.begin(), end = m_chunks.end();
5,113,824✔
1470
        iter i_2 = i_1;
5,113,824✔
1471
        sort(i_1, end);
5,113,824✔
1472
        if (i_1 != end) {
5,113,824✔
1473
            while (++i_2 != end) {
108,132,144✔
1474
                ref_type prev_ref_end = i_1->ref + i_1->size;
103,352,607✔
1475
                REALM_ASSERT_3(prev_ref_end, <=, i_2->ref);
103,352,607✔
1476
                if (i_2->ref == prev_ref_end) { // in-file
103,352,607✔
1477
                    i_1->size += i_2->size;     // Merge
69,885,753✔
1478
                }
69,885,753✔
1479
                else {
33,466,854✔
1480
                    *++i_1 = *i_2;
33,466,854✔
1481
                }
33,466,854✔
1482
            }
103,352,607✔
1483
            m_chunks.erase(i_1 + 1, end);
4,779,537✔
1484
        }
4,779,537✔
1485
    }
5,113,824✔
1486
    void clear()
1487
    {
1,917,102✔
1488
        m_chunks.clear();
1,917,102✔
1489
    }
1,917,102✔
1490
    void check_total_coverage()
1491
    {
639,810✔
1492
        REALM_ASSERT_3(m_chunks.size(), ==, 1);
639,810✔
1493
        REALM_ASSERT_3(m_chunks.front().ref, ==, m_ref_begin);
639,810✔
1494
        REALM_ASSERT_3(m_chunks.front().size, ==, m_mutable_ref_end - m_ref_begin);
639,810✔
1495
    }
639,810✔
1496

1497
private:
1498
    struct Chunk {
1499
        ref_type ref;
1500
        size_t size;
1501
        bool operator<(const Chunk& c) const
1502
        {
656,644,536✔
1503
            return ref < c.ref;
656,644,536✔
1504
        }
656,644,536✔
1505
    };
1506
    std::vector<Chunk> m_chunks;
1507
    ref_type m_ref_begin, m_immutable_ref_end, m_mutable_ref_end, m_baseline;
1508
};
1509

1510
#endif
1511

1512
void Group::verify() const
1513
{
702,588✔
1514
#ifdef REALM_DEBUG
702,588✔
1515
    REALM_ASSERT(is_attached());
702,588✔
1516

351,354✔
1517
    m_alloc.verify();
702,588✔
1518

351,354✔
1519
    if (!m_top.is_attached()) {
702,588✔
1520
        return;
84✔
1521
    }
84✔
1522

351,321✔
1523
    // Verify tables
351,321✔
1524
    {
702,504✔
1525
        auto keys = get_table_keys();
702,504✔
1526
        for (auto key : keys) {
2,943,681✔
1527
            ConstTableRef table = get_table(key);
2,943,681✔
1528
            REALM_ASSERT_3(table->get_key().value, ==, key.value);
2,943,681✔
1529
            table->verify();
2,943,681✔
1530
        }
2,943,681✔
1531
    }
702,504✔
1532

351,321✔
1533
    // Verify history if present
351,321✔
1534
    if (Replication* repl = *get_repl()) {
702,504✔
1535
        if (auto hist = repl->_create_history_read()) {
656,796✔
1536
            hist->set_group(const_cast<Group*>(this), false);
656,790✔
1537
            _impl::History::version_type version = 0;
656,790✔
1538
            int history_type = 0;
656,790✔
1539
            int history_schema_version = 0;
656,790✔
1540
            get_version_and_history_info(m_top, version, history_type, history_schema_version);
656,790✔
1541
            REALM_ASSERT(history_type != Replication::hist_None || history_schema_version == 0);
656,790✔
1542
            ref_type hist_ref = get_history_ref(m_top);
656,790✔
1543
            hist->update_from_ref_and_version(hist_ref, version);
656,790✔
1544
            hist->verify();
656,790✔
1545
        }
656,790✔
1546
    }
656,796✔
1547

351,321✔
1548
    if (auto tr = dynamic_cast<const Transaction*>(this)) {
702,504✔
1549
        // This is a transaction
351,177✔
1550
        if (tr->get_transact_stage() == DB::TransactStage::transact_Reading) {
702,216✔
1551
            // Verifying the memory cannot be done from a read transaction
31,398✔
1552
            // There might be a write transaction running that has freed some
31,398✔
1553
            // memory that is seen as being in use in this transaction
31,398✔
1554
            return;
62,694✔
1555
        }
62,694✔
1556
    }
639,810✔
1557
    size_t logical_file_size = to_size_t(m_top.get_as_ref_or_tagged(2).get_as_int());
639,810✔
1558
    size_t ref_begin = sizeof(SlabAlloc::Header);
639,810✔
1559
    ref_type real_immutable_ref_end = logical_file_size;
639,810✔
1560
    ref_type real_mutable_ref_end = m_alloc.get_total_size();
639,810✔
1561
    ref_type real_baseline = m_alloc.get_baseline();
639,810✔
1562
    // Fake that any empty area between the file and slab is part of the file (immutable):
319,923✔
1563
    ref_type immutable_ref_end = m_alloc.align_size_to_section_boundary(real_immutable_ref_end);
639,810✔
1564
    ref_type mutable_ref_end = m_alloc.align_size_to_section_boundary(real_mutable_ref_end);
639,810✔
1565
    ref_type baseline = m_alloc.align_size_to_section_boundary(real_baseline);
639,810✔
1566

319,923✔
1567
    // Check the consistency of the allocation of used memory
319,923✔
1568
    MemUsageVerifier mem_usage_1(ref_begin, immutable_ref_end, mutable_ref_end, baseline);
639,810✔
1569
    m_top.report_memory_usage(mem_usage_1);
639,810✔
1570
    mem_usage_1.canonicalize();
639,810✔
1571

319,923✔
1572
    // Check concistency of the allocation of the immutable memory that was
319,923✔
1573
    // marked as free before the file was opened.
319,923✔
1574
    MemUsageVerifier mem_usage_2(ref_begin, immutable_ref_end, mutable_ref_end, baseline);
639,810✔
1575
    {
639,810✔
1576
        REALM_ASSERT_EX(m_top.size() == 3 || m_top.size() == 5 || m_top.size() == 7 || m_top.size() >= 10,
639,810✔
1577
                        m_top.size());
639,810✔
1578
        Allocator& alloc = m_top.get_alloc();
639,810✔
1579
        Array pos(alloc), len(alloc), ver(alloc);
639,810✔
1580
        pos.set_parent(const_cast<Array*>(&m_top), s_free_pos_ndx);
639,810✔
1581
        len.set_parent(const_cast<Array*>(&m_top), s_free_size_ndx);
639,810✔
1582
        ver.set_parent(const_cast<Array*>(&m_top), s_free_version_ndx);
639,810✔
1583
        if (m_top.size() > s_free_pos_ndx) {
639,810✔
1584
            if (ref_type ref = m_top.get_as_ref(s_free_pos_ndx))
639,390✔
1585
                pos.init_from_ref(ref);
637,482✔
1586
        }
639,390✔
1587
        if (m_top.size() > s_free_size_ndx) {
639,810✔
1588
            if (ref_type ref = m_top.get_as_ref(s_free_size_ndx))
639,390✔
1589
                len.init_from_ref(ref);
637,482✔
1590
        }
639,390✔
1591
        if (m_top.size() > s_free_version_ndx) {
639,810✔
1592
            if (ref_type ref = m_top.get_as_ref(s_free_version_ndx))
639,390✔
1593
                ver.init_from_ref(ref);
637,482✔
1594
        }
639,390✔
1595
        REALM_ASSERT(pos.is_attached() == len.is_attached());
639,810✔
1596
        REALM_ASSERT(pos.is_attached() || !ver.is_attached()); // pos.is_attached() <== ver.is_attached()
639,810✔
1597
        if (pos.is_attached()) {
639,810✔
1598
            size_t n = pos.size();
637,482✔
1599
            REALM_ASSERT_3(n, ==, len.size());
637,482✔
1600
            if (ver.is_attached())
637,482✔
1601
                REALM_ASSERT_3(n, ==, ver.size());
637,482✔
1602
            for (size_t i = 0; i != n; ++i) {
10,365,663✔
1603
                ref_type ref = to_ref(pos.get(i));
9,728,181✔
1604
                size_t size_of_i = to_size_t(len.get(i));
9,728,181✔
1605
                mem_usage_2.add_immutable(ref, size_of_i);
9,728,181✔
1606
            }
9,728,181✔
1607
            mem_usage_2.canonicalize();
637,482✔
1608
            mem_usage_1.add(mem_usage_2);
637,482✔
1609
            mem_usage_1.canonicalize();
637,482✔
1610
            mem_usage_2.clear();
637,482✔
1611
        }
637,482✔
1612
    }
639,810✔
1613

319,923✔
1614
    // Check the concistency of the allocation of the immutable memory that has
319,923✔
1615
    // been marked as free after the file was opened
319,923✔
1616
    for (const auto& free_block : m_alloc.m_free_read_only) {
2,306,916✔
1617
        mem_usage_2.add_immutable(free_block.first, free_block.second);
2,306,916✔
1618
    }
2,306,916✔
1619
    mem_usage_2.canonicalize();
639,810✔
1620
    mem_usage_1.add(mem_usage_2);
639,810✔
1621
    mem_usage_1.canonicalize();
639,810✔
1622
    mem_usage_2.clear();
639,810✔
1623

319,923✔
1624
    // Check the consistency of the allocation of the mutable memory that has
319,923✔
1625
    // been marked as free
319,923✔
1626
    m_alloc.for_all_free_entries([&](ref_type ref, size_t sz) {
5,468,889✔
1627
        mem_usage_2.add_mutable(ref, sz);
5,468,889✔
1628
    });
5,468,889✔
1629
    mem_usage_2.canonicalize();
639,810✔
1630
    mem_usage_1.add(mem_usage_2);
639,810✔
1631
    mem_usage_1.canonicalize();
639,810✔
1632
    mem_usage_2.clear();
639,810✔
1633

319,923✔
1634
    // There may be a hole between the end of file and the beginning of the slab area.
319,923✔
1635
    // We need to take that into account here.
319,923✔
1636
    REALM_ASSERT_3(real_immutable_ref_end, <=, real_baseline);
639,810✔
1637
    auto slab_start = immutable_ref_end;
639,810✔
1638
    if (real_immutable_ref_end < slab_start) {
639,810✔
1639
        ref_type ref = real_immutable_ref_end;
639,810✔
1640
        size_t corrected_size = slab_start - real_immutable_ref_end;
639,810✔
1641
        mem_usage_1.add_immutable(ref, corrected_size);
639,810✔
1642
        mem_usage_1.canonicalize();
639,810✔
1643
    }
639,810✔
1644

319,923✔
1645
    // At this point we have accounted for all memory managed by the slab
319,923✔
1646
    // allocator
319,923✔
1647
    mem_usage_1.check_total_coverage();
639,810✔
1648
#endif
639,810✔
1649
}
639,810✔
1650

1651
void Group::validate_primary_columns()
1652
{
480✔
1653
    auto table_keys = this->get_table_keys();
480✔
1654
    for (auto tk : table_keys) {
1,800✔
1655
        auto table = get_table(tk);
1,800✔
1656
        table->validate_primary_column();
1,800✔
1657
    }
1,800✔
1658
}
480✔
1659

1660
#ifdef REALM_DEBUG
1661

1662
MemStats Group::get_stats()
UNCOV
1663
{
×
UNCOV
1664
    MemStats mem_stats;
×
UNCOV
1665
    m_top.stats(mem_stats);
×
1666

UNCOV
1667
    return mem_stats;
×
UNCOV
1668
}
×
1669

1670

1671
void Group::print() const
UNCOV
1672
{
×
UNCOV
1673
    m_alloc.print();
×
UNCOV
1674
}
×
1675

1676

1677
void Group::print_free() const
UNCOV
1678
{
×
UNCOV
1679
    Allocator& alloc = m_top.get_alloc();
×
UNCOV
1680
    Array pos(alloc), len(alloc), ver(alloc);
×
UNCOV
1681
    pos.set_parent(const_cast<Array*>(&m_top), s_free_pos_ndx);
×
UNCOV
1682
    len.set_parent(const_cast<Array*>(&m_top), s_free_size_ndx);
×
UNCOV
1683
    ver.set_parent(const_cast<Array*>(&m_top), s_free_version_ndx);
×
UNCOV
1684
    if (m_top.size() > s_free_pos_ndx) {
×
UNCOV
1685
        if (ref_type ref = m_top.get_as_ref(s_free_pos_ndx))
×
UNCOV
1686
            pos.init_from_ref(ref);
×
UNCOV
1687
    }
×
UNCOV
1688
    if (m_top.size() > s_free_size_ndx) {
×
UNCOV
1689
        if (ref_type ref = m_top.get_as_ref(s_free_size_ndx))
×
UNCOV
1690
            len.init_from_ref(ref);
×
UNCOV
1691
    }
×
UNCOV
1692
    if (m_top.size() > s_free_version_ndx) {
×
UNCOV
1693
        if (ref_type ref = m_top.get_as_ref(s_free_version_ndx))
×
UNCOV
1694
            ver.init_from_ref(ref);
×
UNCOV
1695
    }
×
1696

UNCOV
1697
    if (!pos.is_attached()) {
×
UNCOV
1698
        std::cout << "none\n";
×
UNCOV
1699
        return;
×
UNCOV
1700
    }
×
UNCOV
1701
    bool has_versions = ver.is_attached();
×
1702

UNCOV
1703
    size_t n = pos.size();
×
UNCOV
1704
    for (size_t i = 0; i != n; ++i) {
×
UNCOV
1705
        size_t offset = to_size_t(pos.get(i));
×
UNCOV
1706
        size_t size_of_i = to_size_t(len.get(i));
×
UNCOV
1707
        std::cout << i << ": " << offset << " " << size_of_i;
×
1708

UNCOV
1709
        if (has_versions) {
×
UNCOV
1710
            size_t version = to_size_t(ver.get(i));
×
UNCOV
1711
            std::cout << " " << version;
×
UNCOV
1712
        }
×
UNCOV
1713
        std::cout << "\n";
×
UNCOV
1714
    }
×
UNCOV
1715
    std::cout << "\n";
×
UNCOV
1716
}
×
1717
#endif
1718

1719
// LCOV_EXCL_STOP ignore debug functions
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