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

realm / realm-core / 1677

15 Sep 2023 08:33PM UTC coverage: 91.164% (-0.05%) from 91.209%
1677

push

Evergreen

web-flow
Allow non-embedded links in asymmetric objects (#6981)

* Allow non-embedded links in asymmetric objects

* Update CHANGELOG

* Add test of non-embedded links in asymmetric objects

* Fix lint

96026 of 175988 branches covered (0.0%)

8 of 8 new or added lines in 2 files covered. (100.0%)

166 existing lines in 22 files now uncovered.

233684 of 256333 relevant lines covered (91.16%)

6650629.98 hits per line

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

98.72
/src/realm/array_blobs_big.cpp
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
#include <algorithm>
20

21
#include <realm/array_blobs_big.hpp>
22
#include <realm/column_integer.hpp>
23

24

25
using namespace realm;
26

27
BinaryData ArrayBigBlobs::get_at(size_t ndx, size_t& pos) const noexcept
28
{
314,511✔
29
    ref_type ref = get_as_ref(ndx);
314,511✔
30
    if (ref == 0)
314,511✔
31
        return {}; // realm::null();
59,439✔
32

126,537✔
33
    ArrayBlob blob(m_alloc);
255,072✔
34
    blob.init_from_ref(ref);
255,072✔
35

126,537✔
36
    return blob.get_at(pos);
255,072✔
37
}
255,072✔
38

39

40
void ArrayBigBlobs::add(BinaryData value, bool add_zero_term)
41
{
105,603✔
42
    REALM_ASSERT_7(value.size(), ==, 0, ||, value.data(), !=, 0);
105,603✔
43

52,266✔
44
    if (value.is_null()) {
105,603✔
45
        Array::add(0); // Throws
51,489✔
46
    }
51,489✔
47
    else {
54,114✔
48
        ArrayBlob new_blob(m_alloc);
54,114✔
49
        new_blob.create();                                                      // Throws
54,114✔
50
        ref_type ref = new_blob.add(value.data(), value.size(), add_zero_term); // Throws
54,114✔
51
        Array::add(from_ref(ref));                                              // Throws
54,114✔
52
    }
54,114✔
53
}
105,603✔
54

55

56
void ArrayBigBlobs::set(size_t ndx, BinaryData value, bool add_zero_term)
57
{
5,459,871✔
58
    REALM_ASSERT_3(ndx, <, size());
5,459,871✔
59
    REALM_ASSERT_7(value.size(), ==, 0, ||, value.data(), !=, 0);
5,459,871✔
60

2,730,552✔
61
    ref_type ref = get_as_ref(ndx);
5,459,871✔
62

2,730,552✔
63
    if (ref == 0 && value.is_null()) {
5,459,871✔
64
        return;
1,710✔
65
    }
1,710✔
66
    else if (ref == 0 && value.data() != nullptr) {
5,458,161✔
67
        ArrayBlob new_blob(m_alloc);
1,400,334✔
68
        new_blob.create();                                             // Throws
1,400,334✔
69
        ref = new_blob.add(value.data(), value.size(), add_zero_term); // Throws
1,400,334✔
70
        Array::set_as_ref(ndx, ref);
1,400,334✔
71
        return;
1,400,334✔
72
    }
1,400,334✔
73
    else if (ref != 0 && value.data() != nullptr) {
4,059,093✔
74
        char* header = m_alloc.translate(ref);
4,049,733✔
75
        if (Array::get_context_flag_from_header(header)) {
4,049,733✔
76
            Array arr(m_alloc);
6✔
77
            arr.init_from_mem(MemRef(header, ref, m_alloc));
6✔
78
            arr.set_parent(this, ndx);
6✔
79
            ref_type new_ref =
6✔
80
                arr.blob_replace(0, arr.blob_size(), value.data(), value.size(), add_zero_term); // Throws
6✔
81
            if (new_ref != ref) {
6✔
UNCOV
82
                Array::set_as_ref(ndx, new_ref);
×
UNCOV
83
            }
×
84
        }
6✔
85
        else {
4,049,727✔
86
            ArrayBlob blob(m_alloc);
4,049,727✔
87
            blob.init_from_mem(MemRef(header, ref, m_alloc));
4,049,727✔
88
            blob.set_parent(this, ndx);
4,049,727✔
89
            ref_type new_ref = blob.replace(0, blob.blob_size(), value.data(), value.size(), add_zero_term); // Throws
4,049,727✔
90
            if (new_ref != ref) {
4,049,727✔
91
                Array::set_as_ref(ndx, new_ref);
3,673,575✔
92
            }
3,673,575✔
93
        }
4,049,727✔
94
        return;
4,049,733✔
95
    }
4,049,733✔
96
    else if (ref != 0 && value.is_null()) {
9,018✔
97
        Array::destroy_deep(ref, get_alloc());
7,944✔
98
        Array::set(ndx, 0);
7,944✔
99
        return;
7,944✔
100
    }
7,944✔
101
    REALM_ASSERT(false);
2,147,484,721✔
102
}
2,147,484,721✔
103

104

105
void ArrayBigBlobs::insert(size_t ndx, BinaryData value, bool add_zero_term)
106
{
2,336,301✔
107
    REALM_ASSERT_3(ndx, <=, size());
2,336,301✔
108
    REALM_ASSERT_7(value.size(), ==, 0, ||, value.data(), !=, 0);
2,336,301✔
109

1,160,580✔
110
    if (value.is_null()) {
2,336,301✔
111
        Array::insert(ndx, 0); // Throws
1,408,929✔
112
    }
1,408,929✔
113
    else {
927,372✔
114
        ArrayBlob new_blob(m_alloc);
927,372✔
115
        new_blob.create();                                                      // Throws
927,372✔
116
        ref_type ref = new_blob.add(value.data(), value.size(), add_zero_term); // Throws
927,372✔
117

456,720✔
118
        Array::insert(ndx, int64_t(ref)); // Throws
927,372✔
119
    }
927,372✔
120
}
2,336,301✔
121

122

123
size_t ArrayBigBlobs::count(BinaryData value, bool is_string, size_t begin, size_t end) const noexcept
124
{
12✔
125
    size_t num_matches = 0;
12✔
126

6✔
127
    size_t begin_2 = begin;
12✔
128
    for (;;) {
48✔
129
        size_t ndx = find_first(value, is_string, begin_2, end);
48✔
130
        if (ndx == not_found)
48✔
131
            break;
12✔
132
        ++num_matches;
36✔
133
        begin_2 = ndx + 1;
36✔
134
    }
36✔
135

6✔
136
    return num_matches;
12✔
137
}
12✔
138

139

140
size_t ArrayBigBlobs::find_first(BinaryData value, bool is_string, size_t begin, size_t end) const noexcept
141
{
256,680✔
142
    if (end == npos)
256,680✔
143
        end = m_size;
108✔
144
    REALM_ASSERT_11(begin, <=, m_size, &&, end, <=, m_size, &&, begin, <=, end);
256,680✔
145

128,826✔
146
    // When strings are stored as blobs, they are always zero-terminated
128,826✔
147
    // but the value we get as input might not be.
128,826✔
148
    size_t value_size = value.size();
256,680✔
149
    size_t full_size = is_string ? value_size + 1 : value_size;
256,653✔
150

128,826✔
151
    if (value.is_null()) {
256,680✔
152
        for (size_t i = begin; i != end; ++i) {
67,512✔
153
            ref_type ref = get_as_ref(i);
67,509✔
154
            if (ref == 0)
67,509✔
155
                return i;
67,464✔
156
        }
67,509✔
157
    }
67,467✔
158
    else {
189,213✔
159
        for (size_t i = begin; i != end; ++i) {
2,530,485✔
160
            ref_type ref = get_as_ref(i);
2,516,106✔
161
            if (ref) {
2,516,106✔
162
                const char* blob_header = get_alloc().translate(ref);
2,452,161✔
163
                size_t sz = get_size_from_header(blob_header);
2,452,161✔
164
                if (sz == full_size) {
2,452,161✔
165
                    const char* blob_value = ArrayBlob::get(blob_header, 0);
972,666✔
166
                    if (std::equal(blob_value, blob_value + value_size, value.data()))
972,666✔
167
                        return i;
174,834✔
168
                }
972,666✔
169
            }
2,452,161✔
170
        }
2,516,106✔
171
    }
189,213✔
172

128,826✔
173
    return not_found;
136,068✔
174
}
256,680✔
175

176

177
void ArrayBigBlobs::find_all(IntegerColumn& result, BinaryData value, bool is_string, size_t add_offset, size_t begin,
178
                             size_t end)
179
{
12✔
180
    size_t begin_2 = begin;
12✔
181
    for (;;) {
48✔
182
        size_t ndx = find_first(value, is_string, begin_2, end);
48✔
183
        if (ndx == not_found)
48✔
184
            break;
12✔
185
        result.add(add_offset + ndx); // Throws
36✔
186
        begin_2 = ndx + 1;
36✔
187
    }
36✔
188
}
12✔
189

190

191

192
void ArrayBigBlobs::verify() const
193
{
16,830✔
194
#ifdef REALM_DEBUG
16,830✔
195
    REALM_ASSERT(has_refs());
16,830✔
196
    for (size_t i = 0; i < size(); ++i) {
61,527✔
197
        ref_type blob_ref = Array::get_as_ref(i);
44,697✔
198
        // 0 is used to indicate realm::null()
22,092✔
199
        if (blob_ref != 0) {
44,697✔
200
            ArrayBlob blob(m_alloc);
43,143✔
201
            blob.init_from_ref(blob_ref);
43,143✔
202
            blob.verify();
43,143✔
203
        }
43,143✔
204
    }
44,697✔
205
#endif
16,830✔
206
}
16,830✔
207

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