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

realm / realm-core / jorgen.edelbo_337

03 Jul 2024 01:04PM UTC coverage: 90.864% (-0.1%) from 90.984%
jorgen.edelbo_337

Pull #7826

Evergreen

nicola-cab
Merge branch 'master' of github.com:realm/realm-core into next-major
Pull Request #7826: Merge Next major

102968 of 181176 branches covered (56.83%)

3131 of 3738 new or added lines in 54 files covered. (83.76%)

106 existing lines in 23 files now uncovered.

217725 of 239616 relevant lines covered (90.86%)

6844960.2 hits per line

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

75.95
/src/realm/spec.hpp
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
#ifndef REALM_SPEC_HPP
20
#define REALM_SPEC_HPP
21

22
#include <realm/util/features.h>
23
#include <realm/array.hpp>
24
#include <realm/array_string_short.hpp>
25
#include <realm/data_type.hpp>
26
#include <realm/column_type.hpp>
27
#include <realm/keys.hpp>
28

29
namespace realm {
30

31
class Table;
32
class Group;
33

34
class Spec {
35
public:
36
    ~Spec() noexcept;
37

38
    Allocator& get_alloc() const noexcept;
39

40
    // insert column at index
41
    void insert_column(size_t column_ndx, ColKey column_key, ColumnType type, StringData name,
42
                       int attr = col_attr_None);
43
    ColKey get_key(size_t column_ndx) const;
44
    void rename_column(size_t column_ndx, StringData new_name);
45

46
    /// Erase the column at the specified index.
47
    ///
48
    /// This function is guaranteed to *never* throw if the spec is
49
    /// used in a non-transactional context, or if the spec has
50
    /// already been successfully modified within the current write
51
    /// transaction.
52
    void erase_column(size_t column_ndx);
53

54
    // Column info
55
    size_t get_column_count() const noexcept;
56
    size_t get_public_column_count() const noexcept;
57
    ColumnType get_column_type(size_t column_ndx) const noexcept;
58
    StringData get_column_name(size_t column_ndx) const noexcept;
59

60
    /// Returns size_t(-1) if the specified column is not found.
61
    size_t get_column_index(StringData name) const noexcept;
62

63
    // Column Attributes
64
    ColumnAttrMask get_column_attr(size_t column_ndx) const noexcept;
65
    void set_dictionary_key_type(size_t column_ndx, DataType key_type);
66
    DataType get_dictionary_key_type(size_t column_ndx) const;
67

68
    // Auto Enumerated string columns
69
    void upgrade_string_to_enum(size_t column_ndx, ref_type keys_ref);
70
    size_t _get_enumkeys_ndx(size_t column_ndx) const noexcept;
71
    bool is_string_enum_type(size_t column_ndx) const noexcept;
72
    ref_type get_enumkeys_ref(size_t column_ndx, ArrayParent*& keys_parent) noexcept;
73

74
    //@{
75
    /// Compare two table specs for equality.
76
    bool operator==(const Spec&) const noexcept;
77
    bool operator!=(const Spec&) const noexcept;
78
    //@}
79

80
    void detach() noexcept;
81
    void destroy() noexcept;
82

83
    size_t get_ndx_in_parent() const noexcept;
84
    void set_ndx_in_parent(size_t) noexcept;
85

86
    void verify() const;
87
    void typed_print(std::string prefix) const
NEW
88
    {
×
NEW
89
        std::cout << prefix << "Spec as ";
×
NEW
90
        m_top.typed_print(prefix);
×
NEW
91
    }
×
92

93
private:
94
    // Underlying array structure.
95
    //
96
    static constexpr size_t s_types_ndx = 0;
97
    static constexpr size_t s_names_ndx = 1;
98
    static constexpr size_t s_attributes_ndx = 2;
99
    static constexpr size_t s_vacant_1 = 3;
100
    static constexpr size_t s_enum_keys_ndx = 4;
101
    static constexpr size_t s_col_keys_ndx = 5;
102
    static constexpr size_t s_spec_max_size = 6;
103

104
    Array m_top;
105
    Array m_types;            // 1st slot in m_top
106
    ArrayStringShort m_names; // 2nd slot in m_top
107
    Array m_attr;             // 3rd slot in m_top
108
                              // 4th slot in m_top not cached
109
    Array m_enumkeys;         // 5th slot in m_top
110
    Array m_keys;             // 6th slot in m_top
111
    size_t m_num_public_columns = 0;
112

113
    Spec(Allocator&) noexcept; // Unattached
114

115
    bool init(ref_type) noexcept;
116
    void init(MemRef) noexcept;
117
    void update_internals() noexcept;
118

119
    // Returns true in case the ref has changed.
120
    bool init_from_parent() noexcept;
121

122
    ref_type get_ref() const noexcept;
123

124
    /// Called in the context of Group::commit() to ensure that
125
    /// attached table accessors stay valid across a commit. Please
126
    /// note that this works only for non-transactional commits. Table
127
    /// accessors obtained during a transaction are always detached
128
    /// when the transaction ends.
129
    void update_from_parent() noexcept;
130

131
    void set_parent(ArrayParent*, size_t ndx_in_parent) noexcept;
132

133
    void set_column_attr(size_t column_ndx, ColumnAttrMask attr);
134

135
    // Migration
136
    bool migrate_column_keys();
137

138
    /// Construct an empty spec and return just the reference to the
139
    /// underlying memory.
140
    static MemRef create_empty_spec(Allocator&);
141

142
    friend class Group;
143
    friend class Table;
144
};
145

146
// Implementation:
147

148
inline Allocator& Spec::get_alloc() const noexcept
149
{
2,382,840✔
150
    return m_top.get_alloc();
2,382,840✔
151
}
2,382,840✔
152

153
// Uninitialized Spec (call init() to init)
154
inline Spec::Spec(Allocator& alloc) noexcept
155
    : m_top(alloc)
12,939✔
156
    , m_types(alloc)
12,939✔
157
    , m_names(alloc)
12,939✔
158
    , m_attr(alloc)
12,939✔
159
    , m_enumkeys(alloc)
12,939✔
160
    , m_keys(alloc)
12,939✔
161
{
33,726✔
162
    m_types.set_parent(&m_top, s_types_ndx);
33,726✔
163
    m_names.set_parent(&m_top, s_names_ndx);
33,726✔
164
    m_attr.set_parent(&m_top, s_attributes_ndx);
33,726✔
165
    m_enumkeys.set_parent(&m_top, s_enum_keys_ndx);
33,726✔
166
    m_keys.set_parent(&m_top, s_col_keys_ndx);
33,726✔
167
}
33,726✔
168

169
inline bool Spec::init_from_parent() noexcept
170
{
2,381,454✔
171
    ref_type ref = m_top.get_ref_from_parent();
2,381,454✔
172
    return init(ref);
2,381,454✔
173
}
2,381,454✔
174

175
inline void Spec::destroy() noexcept
176
{
×
177
    m_top.destroy_deep();
×
178
}
×
179

180
inline size_t Spec::get_ndx_in_parent() const noexcept
181
{
×
182
    return m_top.get_ndx_in_parent();
×
183
}
×
184

185
inline void Spec::set_ndx_in_parent(size_t ndx) noexcept
186
{
×
187
    m_top.set_ndx_in_parent(ndx);
×
188
}
×
189

190
inline ref_type Spec::get_ref() const noexcept
191
{
×
192
    return m_top.get_ref();
×
193
}
×
194

195
inline void Spec::set_parent(ArrayParent* parent, size_t ndx_in_parent) noexcept
196
{
33,726✔
197
    m_top.set_parent(parent, ndx_in_parent);
33,726✔
198
}
33,726✔
199

200
inline void Spec::rename_column(size_t column_ndx, StringData new_name)
201
{
360✔
202
    REALM_ASSERT(column_ndx < m_types.size());
360✔
203
    m_names.set(column_ndx, new_name);
360✔
204
}
360✔
205

206
inline size_t Spec::get_column_count() const noexcept
207
{
6,140,292✔
208
    // This is the total count of columns, including backlinks (not public)
209
    return m_types.size();
6,140,292✔
210
}
6,140,292✔
211

212
inline size_t Spec::get_public_column_count() const noexcept
213
{
31,049,682✔
214
    return m_num_public_columns;
31,049,682✔
215
}
31,049,682✔
216

217
inline ColumnType Spec::get_column_type(size_t ndx) const noexcept
218
{
693✔
219
    REALM_ASSERT(ndx < get_column_count());
693✔
220
    return ColumnType(int(m_types.get(ndx) & 0xFFFF));
693✔
221
}
693✔
222

223
inline ColumnAttrMask Spec::get_column_attr(size_t ndx) const noexcept
224
{
2,105,457✔
225
    REALM_ASSERT(ndx < get_column_count());
2,105,457✔
226
    return ColumnAttrMask(m_attr.get(ndx));
2,105,457✔
227
}
2,105,457✔
228

229
inline void Spec::set_dictionary_key_type(size_t ndx, DataType key_type)
230
{
56,508✔
231
    auto type = m_types.get(ndx);
56,508✔
232
    m_types.set(ndx, (type & 0xFFFF) + (int64_t(key_type) << 16));
56,508✔
233
}
56,508✔
234

235
inline DataType Spec::get_dictionary_key_type(size_t ndx) const
236
{
302,559✔
237
    REALM_ASSERT(ndx < get_column_count());
302,559✔
238
    return DataType(int(m_types.get(ndx) >> 16));
302,559✔
239
}
302,559✔
240

241
inline void Spec::set_column_attr(size_t column_ndx, ColumnAttrMask attr)
242
{
5,034✔
243
    REALM_ASSERT(column_ndx < get_column_count());
5,034✔
244

245
    // At this point we only allow one attr at a time
246
    // so setting it will overwrite existing. In the future
247
    // we will allow combinations.
248
    m_attr.set(column_ndx, attr.m_value);
5,034✔
249

250
    update_internals();
5,034✔
251
}
5,034✔
252

253
inline StringData Spec::get_column_name(size_t ndx) const noexcept
254
{
5,542,239✔
255
    return m_names.get(ndx);
5,542,239✔
256
}
5,542,239✔
257

258
inline size_t Spec::get_column_index(StringData name) const noexcept
259
{
8,848,719✔
260
    return m_names.find_first(name);
8,848,719✔
261
}
8,848,719✔
262

263
inline bool Spec::operator!=(const Spec& s) const noexcept
264
{
×
265
    return !(*this == s);
×
266
}
×
267

268
} // namespace realm
269

270
#endif // REALM_SPEC_HPP
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