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

realm / realm-core / jorgen.edelbo_322

20 Jun 2024 09:43AM UTC coverage: 84.879% (-6.1%) from 90.966%
jorgen.edelbo_322

Pull #7826

Evergreen

jedelbo
remove set_direct methods from integer compressors
Pull Request #7826: Merge Next major

66056 of 81292 branches covered (81.26%)

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

566 existing lines in 29 files now uncovered.

84791 of 99896 relevant lines covered (84.88%)

15351931.14 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,615,883✔
150
    return m_top.get_alloc();
2,615,883✔
151
}
2,615,883✔
152

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

169
inline bool Spec::init_from_parent() noexcept
170
{
2,613,801✔
171
    ref_type ref = m_top.get_ref_from_parent();
2,613,801✔
172
    return init(ref);
2,613,801✔
173
}
2,613,801✔
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
{
34,131✔
197
    m_top.set_parent(parent, ndx_in_parent);
34,131✔
198
}
34,131✔
199

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

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

212
inline size_t Spec::get_public_column_count() const noexcept
213
{
32,116,413✔
214
    return m_num_public_columns;
32,116,413✔
215
}
32,116,413✔
216

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

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

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

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

241
inline void Spec::set_column_attr(size_t column_ndx, ColumnAttrMask attr)
242
{
5,031✔
243
    REALM_ASSERT(column_ndx < get_column_count());
5,031✔
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,031✔
249

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

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

258
inline size_t Spec::get_column_index(StringData name) const noexcept
259
{
8,867,499✔
260
    return m_names.find_first(name);
8,867,499✔
261
}
8,867,499✔
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