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

realm / realm-core / jorgen.edelbo_338

03 Jul 2024 03:00PM UTC coverage: 90.856% (-0.008%) from 90.864%
jorgen.edelbo_338

Pull #7803

Evergreen

nicola-cab
Merge branch 'next-major' into feature/string-compression
Pull Request #7803: Feature/string compression

103028 of 180606 branches covered (57.05%)

1144 of 1267 new or added lines in 33 files covered. (90.29%)

155 existing lines in 24 files now uncovered.

218583 of 240583 relevant lines covered (90.86%)

7959624.7 hits per line

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

58.62
/src/realm/array_string.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_ARRAY_STRING_HPP
20
#define REALM_ARRAY_STRING_HPP
21

22
#include <realm/array_string_short.hpp>
23
#include <realm/array_blobs_small.hpp>
24
#include <realm/array_blobs_big.hpp>
25

26
namespace realm {
27

28
class Spec;
29

30
class ArrayString : public ArrayPayload {
31
public:
32
    using value_type = StringData;
33

34
    explicit ArrayString(Allocator&);
35

36
    static StringData default_value(bool nullable)
37
    {
3,752,862✔
38
        return nullable ? StringData{} : StringData{""};
3,752,862✔
39
    }
3,752,862✔
40

41
    // This is only used in the upgrade process
42
    void set_nullability(bool n)
43
    {
×
44
        m_nullable = n;
×
45
    }
×
46
    void create();
47

48
    bool is_attached() const
49
    {
900,264✔
50
        return m_arr->is_attached();
900,264✔
51
    }
900,264✔
52

53
    ref_type get_ref() const
54
    {
355,890✔
55
        return m_arr->get_ref();
355,890✔
56
    }
355,890✔
57
    ArrayParent* get_parent() const
58
    {
×
59
        return m_arr->get_parent();
×
60
    }
×
61
    size_t get_ndx_in_parent() const
62
    {
×
63
        return m_arr->get_ndx_in_parent();
×
64
    }
×
65
    void set_parent(ArrayParent* p, size_t n) noexcept override
66
    {
13,091,577✔
67
        m_arr->set_parent(p, n);
13,091,577✔
68
    }
13,091,577✔
69
    bool need_string_interner() const override
70
    {
142,110✔
71
        return true;
142,110✔
72
    }
142,110✔
73
    void set_string_interner(StringInterner* string_interner) const override
74
    {
77,325,327✔
75
        m_string_interner = string_interner;
77,325,327✔
76
    }
77,325,327✔
77
    bool need_spec() const override
78
    {
142,110✔
79
        return true;
142,110✔
80
    }
142,110✔
81
    void set_spec(Spec* spec, size_t col_ndx) const override
82
    {
8,947,125✔
83
        m_spec = spec;
8,947,125✔
84
        m_col_ndx = col_ndx;
8,947,125✔
85
    }
8,947,125✔
86

87
    void update_parent()
88
    {
196,140✔
89
        m_arr->update_parent();
196,140✔
90
    }
196,140✔
91

92
    void init_from_mem(MemRef mem) noexcept;
93
    void init_from_ref(ref_type ref) noexcept override
94
    {
79,180,473✔
95
        init_from_mem(MemRef(m_alloc.translate(ref), ref, m_alloc));
79,180,473✔
96
    }
79,180,473✔
97
    void init_from_parent();
98
    void destroy() noexcept;
99
    void detach() noexcept;
100

101
    size_t size() const;
102

103
    void add(StringData value);
104
    void set(size_t ndx, StringData value);
105
    void set_null(size_t ndx)
106
    {
2,589✔
107
        set(ndx, StringData{});
2,589✔
108
    }
2,589✔
109
    void insert(size_t ndx, StringData value);
110
    StringData get(size_t ndx) const;
111
    StringData get_legacy(size_t ndx) const;
112
    Mixed get_any(size_t ndx) const override;
113
    bool is_null(size_t ndx) const;
114
    void erase(size_t ndx);
115
    void move(ArrayString& dst, size_t ndx);
116
    void clear();
117

118
    size_t find_first(StringData value, size_t begin, size_t end) const noexcept;
119

120
    size_t lower_bound(StringData value);
121

122
    /// Get the specified element without the cost of constructing an
123
    /// array instance. If an array instance is already available, or
124
    /// you need to get multiple values, then this method will be
125
    /// slower.
126
    static StringData get(const char* header, size_t ndx, Allocator& alloc) noexcept;
127

128
    void verify() const;
129
    template <class T>
130
    static ref_type typed_write(ref_type ref, T& out, Allocator& alloc);
131

132
private:
133
    static constexpr size_t small_string_max_size = 15;  // ArrayStringShort
134
    static constexpr size_t medium_string_max_size = 63; // ArrayStringLong
135
    static constexpr size_t storage_alignment =
136
        std::max({alignof(ArrayStringShort), alignof(ArraySmallBlobs), alignof(ArrayBigBlobs), alignof(Array)});
137
    static constexpr size_t storage_size =
138
        std::max({sizeof(ArrayStringShort), sizeof(ArraySmallBlobs), sizeof(ArrayBigBlobs), sizeof(Array)});
139

140
    enum class Type { small_strings, medium_strings, big_strings, enum_strings, interned_strings };
141

142
    Type m_type = Type::small_strings;
143

144
    Allocator& m_alloc;
145
    alignas(storage_alignment) std::byte m_storage[storage_size];
146
    Array* m_arr;
147
    bool m_nullable = true;
148
    mutable Spec* m_spec = nullptr;
149
    mutable size_t m_col_ndx = realm::npos;
150
    std::unique_ptr<ArrayString> m_string_enum_values;
151
    mutable StringInterner* m_string_interner = nullptr;
152

153
    Type upgrade_leaf(size_t value_size);
154
};
155

156
inline StringData ArrayString::get(const char* header, size_t ndx, Allocator& alloc) noexcept
UNCOV
157
{
×
UNCOV
158
    bool long_strings = Array::get_hasrefs_from_header(header);
×
UNCOV
159
    if (!long_strings) {
×
UNCOV
160
        return ArrayStringShort::get(header, ndx, true);
×
UNCOV
161
    }
×
UNCOV
162
    else {
×
UNCOV
163
        bool is_big = Array::get_context_flag_from_header(header);
×
UNCOV
164
        if (!is_big) {
×
UNCOV
165
            return ArraySmallBlobs::get_string(header, ndx, alloc);
×
UNCOV
166
        }
×
UNCOV
167
        else {
×
UNCOV
168
            return ArrayBigBlobs::get_string(header, ndx, alloc);
×
UNCOV
169
        }
×
UNCOV
170
    }
×
UNCOV
171
}
×
172

173
} // namespace realm
174

175
#endif /* REALM_ARRAY_STRING_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