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

realm / realm-core / kraen.hansen_76

26 Aug 2024 12:57PM UTC coverage: 91.097% (+0.002%) from 91.095%
kraen.hansen_76

Pull #7903

Evergreen

web-flow
Merge branch 'master' into kh/passing-scheduler
Pull Request #7903: Passing scheduler to `EventLoopDispatcher`, `schedulerWrapBlockingFunction` and through `RealmConfig`

102818 of 181636 branches covered (56.61%)

0 of 4 new or added lines in 1 file covered. (0.0%)

58 existing lines in 17 files now uncovered.

217376 of 238621 relevant lines covered (91.1%)

5956990.13 hits per line

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

97.77
/src/realm/array_timestamp.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 <realm/array_timestamp.hpp>
20
#include <realm/array_integer_tpl.hpp>
21

22
using namespace realm;
23

24
ArrayTimestamp::ArrayTimestamp(Allocator& a)
25
    : Array(a)
1,514,154✔
26
    , m_seconds(a)
1,514,154✔
27
    , m_nanoseconds(a)
1,514,154✔
28
{
2,853,135✔
29
    m_seconds.set_parent(this, 0);
2,853,135✔
30
    m_nanoseconds.set_parent(this, 1);
2,853,135✔
31
}
2,853,135✔
32

33
void ArrayTimestamp::create()
34
{
52,446✔
35
    Array::create(Array::type_HasRefs, false /* context_flag */, 2);
52,446✔
36

37
    MemRef seconds = ArrayIntNull::create_array(Array::type_Normal, false, 0, m_alloc);
52,446✔
38
    Array::set_as_ref(0, seconds.get_ref());
52,446✔
39
    MemRef nanoseconds = ArrayInteger::create_empty_array(Array::type_Normal, false, m_alloc);
52,446✔
40
    Array::set_as_ref(1, nanoseconds.get_ref());
52,446✔
41

42
    m_seconds.init_from_parent();
52,446✔
43
    m_nanoseconds.init_from_parent();
52,446✔
44
}
52,446✔
45

46
void ArrayTimestamp::init_from_mem(MemRef mem) noexcept
47
{
2,382,009✔
48
    Array::init_from_mem(mem);
2,382,009✔
49
    m_seconds.init_from_parent();
2,382,009✔
50
    m_nanoseconds.init_from_parent();
2,382,009✔
51
}
2,382,009✔
52

53
void ArrayTimestamp::set(size_t ndx, Timestamp value)
54
{
180,291✔
55
    if (value.is_null()) {
180,291✔
56
        return set_null(ndx);
3,399✔
57
    }
3,399✔
58

59
    util::Optional<int64_t> seconds = util::make_optional(value.get_seconds());
176,892✔
60
    int32_t nanoseconds = value.get_nanoseconds();
176,892✔
61

62
    m_seconds.set(ndx, seconds);
176,892✔
63
    m_nanoseconds.set(ndx, nanoseconds); // Throws
176,892✔
64
}
176,892✔
65

66
void ArrayTimestamp::insert(size_t ndx, Timestamp value)
67
{
395,703✔
68
    if (value.is_null()) {
395,703✔
69
        m_seconds.insert(ndx, util::none);
139,479✔
70
        m_nanoseconds.insert(ndx, 0); // Throws
139,479✔
71
    }
139,479✔
72
    else {
256,224✔
73
        util::Optional<int64_t> seconds = util::make_optional(value.get_seconds());
256,224✔
74
        int32_t nanoseconds = value.get_nanoseconds();
256,224✔
75

76
        m_seconds.insert(ndx, seconds);
256,224✔
77
        m_nanoseconds.insert(ndx, nanoseconds); // Throws
256,224✔
78
    }
256,224✔
79
}
395,703✔
80

81
namespace realm {
82

83
template <>
84
size_t ArrayTimestamp::find_first<Greater>(Timestamp value, size_t begin, size_t end) const noexcept
85
{
2,424✔
86
    if (value.is_null()) {
2,424✔
87
        return not_found;
1,020✔
88
    }
1,020✔
89
    int64_t sec = value.get_seconds();
1,404✔
90
    while (begin < end) {
1,674✔
91
        size_t ret = m_seconds.find_first<GreaterEqual>(sec, begin, end);
1,662✔
92

93
        if (ret == not_found)
1,662✔
94
            return not_found;
216✔
95

96
        util::Optional<int64_t> seconds = m_seconds.get(ret);
1,446✔
97
        if (*seconds > sec) {
1,446✔
98
            return ret;
1,158✔
99
        }
1,158✔
100
        // We now know that neither m_value nor current value is null and that seconds part equals
101
        // We are just missing to compare nanoseconds part
102
        int32_t nanos = int32_t(m_nanoseconds.get(ret));
288✔
103
        if (nanos > value.get_nanoseconds()) {
288✔
104
            return ret;
18✔
105
        }
18✔
106
        begin = ret + 1;
270✔
107
    }
270✔
108

109
    return not_found;
12✔
110
}
1,404✔
111

112
template <>
113
size_t ArrayTimestamp::find_first<Less>(Timestamp value, size_t begin, size_t end) const noexcept
114
{
4,188✔
115
    if (value.is_null()) {
4,188✔
116
        return not_found;
1,020✔
117
    }
1,020✔
118
    int64_t sec = value.get_seconds();
3,168✔
119
    while (begin < end) {
3,402✔
120
        size_t ret = m_seconds.find_first<LessEqual>(sec, begin, end);
3,402✔
121

122
        if (ret == not_found)
3,402✔
123
            return not_found;
6✔
124

125
        util::Optional<int64_t> seconds = m_seconds.get(ret);
3,396✔
126
        if (*seconds < sec) {
3,396✔
127
            return ret;
3,138✔
128
        }
3,138✔
129
        // We now know that neither m_value nor current value is null and that seconds part equals
130
        // We are just missing to compare nanoseconds part
131
        int32_t nanos = int32_t(m_nanoseconds.get(ret));
258✔
132
        if (nanos < value.get_nanoseconds()) {
258✔
133
            return ret;
24✔
134
        }
24✔
135
        begin = ret + 1;
234✔
136
    }
234✔
137

138
    return not_found;
×
139
}
3,168✔
140

141
template <>
142
size_t ArrayTimestamp::find_first<GreaterEqual>(Timestamp value, size_t begin, size_t end) const noexcept
143
{
2,322✔
144
    if (value.is_null()) {
2,322✔
145
        return m_seconds.find_first<Equal>(util::none, begin, end);
1,020✔
146
    }
1,020✔
147
    int64_t sec = value.get_seconds();
1,302✔
148
    while (begin < end) {
1,326✔
149
        size_t ret = m_seconds.find_first<GreaterEqual>(sec, begin, end);
1,326✔
150

151
        if (ret == not_found)
1,326✔
152
            return not_found;
204✔
153

154
        util::Optional<int64_t> seconds = m_seconds.get(ret);
1,122✔
155
        if (*seconds > sec) {
1,122✔
156
            return ret;
876✔
157
        }
876✔
158
        // We now know that neither m_value nor current value is null and that seconds part equals
159
        // We are just missing to compare nanoseconds part
160
        int32_t nanos = int32_t(m_nanoseconds.get(ret));
246✔
161
        if (nanos >= value.get_nanoseconds()) {
246✔
162
            return ret;
222✔
163
        }
222✔
164
        begin = ret + 1;
24✔
165
    }
24✔
166

167
    return not_found;
×
168
}
1,302✔
169

170
template <>
171
size_t ArrayTimestamp::find_first<LessEqual>(Timestamp value, size_t begin, size_t end) const noexcept
172
{
4,374✔
173
    if (value.is_null()) {
4,374✔
174
        return m_seconds.find_first<Equal>(util::none, begin, end);
1,020✔
175
    }
1,020✔
176
    int64_t sec = value.get_seconds();
3,354✔
177
    while (begin < end) {
3,366✔
178
        size_t ret = m_seconds.find_first<LessEqual>(sec, begin, end);
3,366✔
179

180
        if (ret == not_found)
3,366✔
181
            return not_found;
6✔
182

183
        util::Optional<int64_t> seconds = m_seconds.get(ret);
3,360✔
184
        if (*seconds < sec) {
3,360✔
185
            return ret;
3,120✔
186
        }
3,120✔
187
        // We now know that neither m_value nor current value is null and that seconds part equals
188
        // We are just missing to compare nanoseconds part
189
        int32_t nanos = int32_t(m_nanoseconds.get(ret));
240✔
190
        if (nanos <= value.get_nanoseconds()) {
240✔
191
            return ret;
228✔
192
        }
228✔
193
        begin = ret + 1;
12✔
194
    }
12✔
195

196
    return not_found;
×
197
}
3,354✔
198

199
template <>
200
size_t ArrayTimestamp::find_first<Equal>(Timestamp value, size_t begin, size_t end) const noexcept
201
{
16,512✔
202
    if (value.is_null()) {
16,512✔
203
        return m_seconds.find_first<Equal>(util::none, begin, end);
5,178✔
204
    }
5,178✔
205
    while (begin < end) {
11,454✔
206
        auto res = m_seconds.find_first(value.get_seconds(), begin, end);
11,454✔
207
        if (res == npos)
11,454✔
208
            return not_found;
5,472✔
209
        if (m_nanoseconds.get(res) == value.get_nanoseconds())
5,982✔
210
            return res;
5,862✔
211
        begin = res + 1;
120✔
212
    }
120✔
UNCOV
213
    return not_found;
×
214
}
11,334✔
215

216
template <>
217
size_t ArrayTimestamp::find_first<NotEqual>(Timestamp value, size_t begin, size_t end) const noexcept
218
{
24,708✔
219
    if (value.is_null()) {
24,708✔
220
        return m_seconds.find_first<NotEqual>(util::none, begin, end);
20,688✔
221
    }
20,688✔
222
    int64_t sec = value.get_seconds();
4,020✔
223
    while (begin < end) {
4,260✔
224
        util::Optional<int64_t> seconds = m_seconds.get(begin);
4,254✔
225
        if (!seconds || *seconds != sec) {
4,254✔
226
            return begin;
3,978✔
227
        }
3,978✔
228
        // We now know that neither m_value nor current value is null and that seconds part equals
229
        // We are just missing to compare nanoseconds part
230
        int32_t nanos = int32_t(m_nanoseconds.get(begin));
276✔
231
        if (nanos != value.get_nanoseconds()) {
276✔
232
            return begin;
36✔
233
        }
36✔
234
        ++begin;
240✔
235
    }
240✔
236
    return not_found;
6✔
237
}
4,020✔
238

239
size_t ArrayTimestamp::find_first_in_range(Timestamp from, Timestamp to, size_t start, size_t end) const
240
{
1,452✔
241
    while (start < end) {
1,614✔
242
        start = m_seconds.find_first_in_range(from.get_seconds(), to.get_seconds(), start, end);
1,554✔
243
        if (start != realm::not_found) {
1,554✔
244
            util::Optional<int64_t> seconds = m_seconds.get(start);
1,494✔
245
            int32_t nanos = int32_t(m_nanoseconds.get(start));
1,494✔
246
            if ((from.get_seconds() < *seconds || from.get_nanoseconds() <= nanos) &&
1,494✔
247
                (to.get_seconds() > *seconds || nanos <= to.get_nanoseconds()))
1,494✔
248
                return start;
1,392✔
249
            start++;
102✔
250
        }
102✔
251
    }
1,554✔
252
    return not_found;
60✔
253
}
1,452✔
254

255

256
void ArrayTimestamp::verify() const
257
{
6,948✔
258
#ifdef REALM_DEBUG
6,948✔
259
    m_seconds.verify();
6,948✔
260
    m_nanoseconds.verify();
6,948✔
261
    REALM_ASSERT(m_seconds.size() == m_nanoseconds.size());
6,948✔
262
#endif
6,948✔
263
}
6,948✔
264
} // namespace realm
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