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

realm / realm-core / jorgen.edelbo_402

21 Aug 2024 11:10AM UTC coverage: 91.054% (-0.03%) from 91.085%
jorgen.edelbo_402

Pull #7803

Evergreen

jedelbo
Small fix to Table::typed_write

When writing the realm to a new file from a write transaction,
the Table may be COW so that the top ref is changed. So don't
use the ref that is present in the group when the operation starts.
Pull Request #7803: Feature/string compression

103494 of 181580 branches covered (57.0%)

1929 of 1999 new or added lines in 46 files covered. (96.5%)

695 existing lines in 51 files now uncovered.

220142 of 241772 relevant lines covered (91.05%)

7344461.76 hits per line

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

55.56
/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
#include <realm/string_interner.hpp>
26

27
namespace realm {
28

29
class Spec;
30

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

35
    explicit ArrayString(Allocator&);
36

37
    static StringData default_value(bool nullable)
38
    {
2,339,043✔
39
        return nullable ? StringData{} : StringData{""};
2,339,043✔
40
    }
2,339,043✔
41

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

49
    bool is_attached() const
50
    {
906,705✔
51
        return m_arr->is_attached();
906,705✔
52
    }
906,705✔
53

54
    ref_type get_ref() const
55
    {
357,981✔
56
        return m_arr->get_ref();
357,981✔
57
    }
357,981✔
58
    ArrayParent* get_parent() const
59
    {
×
60
        return m_arr->get_parent();
×
61
    }
×
62
    size_t get_ndx_in_parent() const
63
    {
×
64
        return m_arr->get_ndx_in_parent();
×
65
    }
×
66
    void set_parent(ArrayParent* p, size_t n) noexcept override
67
    {
6,907,500✔
68
        m_arr->set_parent(p, n);
6,907,500✔
69
    }
6,907,500✔
70
    bool need_string_interner() const override
71
    {
151,341✔
72
        return true;
151,341✔
73
    }
151,341✔
74
    void set_string_interner(StringInterner* string_interner) const override
75
    {
134,482,932✔
76
        m_string_interner = string_interner;
134,482,932✔
77
    }
134,482,932✔
78
    bool is_compressed() const
79
    {
120,672✔
80
        return m_type == Type::interned_strings;
120,672✔
81
    }
120,672✔
82

83
    void update_parent()
84
    {
196,281✔
85
        m_arr->update_parent();
196,281✔
86
    }
196,281✔
87

88
    void init_from_mem(MemRef mem) noexcept;
89
    void init_from_ref(ref_type ref) noexcept override
90
    {
131,579,721✔
91
        init_from_mem(MemRef(m_alloc.translate(ref), ref, m_alloc));
131,579,721✔
92
    }
131,579,721✔
93
    void init_from_parent();
94
    void destroy() noexcept;
95
    void detach() noexcept;
96

97
    size_t size() const;
98

99
    void add(StringData value);
100
    void set(size_t ndx, StringData value);
101
    void set_null(size_t ndx)
102
    {
2,529✔
103
        set(ndx, StringData{});
2,529✔
104
    }
2,529✔
105
    void insert(size_t ndx, StringData value);
106
    StringData get(size_t ndx) const;
107
    std::optional<StringID> get_string_id(size_t ndx) const;
108
    Mixed get_any(size_t ndx) const override;
109
    bool is_null(size_t ndx) const;
110
    void erase(size_t ndx);
111
    void move(ArrayString& dst, size_t ndx);
112
    void clear();
113

114
    size_t find_first(StringData value, size_t begin, size_t end) const noexcept;
115

116
    /// Special version for searching in an array or compressed strings.
117
    size_t find_first(StringData value, size_t begin, size_t end, std::optional<StringID>) const noexcept;
118

119
    size_t lower_bound(StringData value);
120

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

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

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

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

141
    Type m_type = Type::small_strings;
142

143
    Allocator& m_alloc;
144
    alignas(storage_alignment) std::byte m_storage[storage_size];
145
    Array* m_arr;
146
    bool m_nullable = true;
147
    std::unique_ptr<ArrayString> m_string_enum_values;
148
    mutable StringInterner* m_string_interner = nullptr;
149

150
    Type upgrade_leaf(size_t value_size);
151
};
152

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

170
} // namespace realm
171

172
#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