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

realm / realm-core / jorgen.edelbo_334

01 Jul 2024 07:22AM UTC coverage: 90.829% (-0.04%) from 90.865%
jorgen.edelbo_334

Pull #7803

Evergreen

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

102912 of 180568 branches covered (56.99%)

1141 of 1267 new or added lines in 33 files covered. (90.06%)

172 existing lines in 24 files now uncovered.

218291 of 240332 relevant lines covered (90.83%)

7818396.4 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,766,431✔
38
        return nullable ? StringData{} : StringData{""};
3,766,431✔
39
    }
3,766,431✔
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
    {
252,366✔
50
        return m_arr->is_attached();
252,366✔
51
    }
252,366✔
52

53
    ref_type get_ref() const
54
    {
199,614✔
55
        return m_arr->get_ref();
199,614✔
56
    }
199,614✔
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
    {
11,475,489✔
67
        m_arr->set_parent(p, n);
11,475,489✔
68
    }
11,475,489✔
69
    bool need_string_interner() const override
70
    {
148,608✔
71
        return true;
148,608✔
72
    }
148,608✔
73
    void set_string_interner(StringInterner* string_interner) const override
74
    {
72,782,145✔
75
        m_string_interner = string_interner;
72,782,145✔
76
    }
72,782,145✔
77
    bool need_spec() const override
78
    {
148,608✔
79
        return true;
148,608✔
80
    }
148,608✔
81
    void set_spec(Spec* spec, size_t col_ndx) const override
82
    {
8,203,212✔
83
        m_spec = spec;
8,203,212✔
84
        m_col_ndx = col_ndx;
8,203,212✔
85
    }
8,203,212✔
86

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

92
    void init_from_mem(MemRef mem) noexcept;
93
    void init_from_ref(ref_type ref) noexcept override
94
    {
77,104,203✔
95
        init_from_mem(MemRef(m_alloc.translate(ref), ref, m_alloc));
77,104,203✔
96
    }
77,104,203✔
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,511✔
107
        set(ndx, StringData{});
2,511✔
108
    }
2,511✔
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