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

realm / realm-core / 2321

17 May 2024 10:55AM UTC coverage: 90.833% (+0.009%) from 90.824%
2321

push

Evergreen

web-flow
Merge pull request #7705 from realm/release/14.7.0

Release/14.7.0

102108 of 181070 branches covered (56.39%)

214606 of 236265 relevant lines covered (90.83%)

5549847.74 hits per line

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

97.58
/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,377,867✔
26
    , m_seconds(a)
1,377,867✔
27
    , m_nanoseconds(a)
1,377,867✔
28
{
2,928,270✔
29
    m_seconds.set_parent(this, 0);
2,928,270✔
30
    m_nanoseconds.set_parent(this, 1);
2,928,270✔
31
}
2,928,270✔
32

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

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

42
    m_seconds.init_from_parent();
39,705✔
43
    m_nanoseconds.init_from_parent();
39,705✔
44
}
39,705✔
45

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

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

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

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

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

76
        m_seconds.insert(ndx, seconds);
254,301✔
77
        m_nanoseconds.insert(ndx, nanoseconds); // Throws
254,301✔
78
    }
254,301✔
79
}
393,846✔
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✔
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
void ArrayTimestamp::verify() const
240
{
6,957✔
241
#ifdef REALM_DEBUG
6,957✔
242
    m_seconds.verify();
6,957✔
243
    m_nanoseconds.verify();
6,957✔
244
    REALM_ASSERT(m_seconds.size() == m_nanoseconds.size());
6,957✔
245
#endif
6,957✔
246
}
6,957✔
247
} // 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

© 2026 Coveralls, Inc