Coverage Report

Created: 2025-04-30 17:40

/root/doris/be/src/olap/uint24.h
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  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,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <cstdint>
21
#include <cstring>
22
#include <iostream>
23
#include <string>
24
25
#include "olap/olap_common.h"
26
27
namespace doris {
28
29
// 24bit int type, used to store date type in storage
30
struct uint24_t {
31
public:
32
    uint24_t() = default;
33
34
38.0M
    uint24_t(const uint32_t& value) {
35
38.0M
        data[0] = static_cast<uint8_t>(value);
36
38.0M
        data[1] = static_cast<uint8_t>(value >> 8);
37
38.0M
        data[2] = static_cast<uint8_t>(value >> 16);
38
38.0M
    }
39
40
1.40M
    uint24_t& operator=(const uint32_t value) {
41
1.40M
        data[0] = static_cast<uint8_t>(value);
42
1.40M
        data[1] = static_cast<uint8_t>(value >> 8);
43
1.40M
        data[2] = static_cast<uint8_t>(value >> 16);
44
1.40M
        return *this;
45
1.40M
    }
46
47
0
    uint24_t& operator=(const uint128_t& value) {
48
0
        data[0] = static_cast<uint8_t>(value);
49
0
        data[1] = static_cast<uint8_t>(value >> 8);
50
0
        data[2] = static_cast<uint8_t>(value >> 16);
51
0
        return *this;
52
0
    }
53
54
3.00M
    uint24_t& operator=(const uint64_t value) {
55
3.00M
        data[0] = static_cast<uint8_t>(value);
56
3.00M
        data[1] = static_cast<uint8_t>(value >> 8);
57
3.00M
        data[2] = static_cast<uint8_t>(value >> 16);
58
3.00M
        return *this;
59
3.00M
    }
60
61
0
    uint24_t& operator+=(const uint24_t& value) {
62
0
        *this = static_cast<int>(*this) + static_cast<int>(value);
63
0
        return *this;
64
0
    }
65
66
0
    uint24_t& operator>>=(const int bits) {
67
0
        *this = static_cast<unsigned int>(*this) >> bits;
68
0
        return *this;
69
0
    }
70
71
0
    uint24_t& operator|=(const uint24_t& value) {
72
0
        *this = static_cast<int>(*this) | static_cast<int>(value);
73
0
        return *this;
74
0
    }
75
76
41.2M
    operator uint32_t() const {
77
41.2M
        uint32_t value = static_cast<uint8_t>(data[0]);
78
41.2M
        value += (static_cast<uint32_t>(static_cast<uint8_t>(data[1]))) << 8;
79
41.2M
        value += (static_cast<uint32_t>(static_cast<uint8_t>(data[2]))) << 16;
80
41.2M
        return value;
81
41.2M
    }
82
83
36.7k
    uint24_t& operator=(const int value) {
84
36.7k
        data[0] = static_cast<uint8_t>(value);
85
36.7k
        data[1] = static_cast<uint8_t>(value >> 8);
86
36.7k
        data[2] = static_cast<uint8_t>(value >> 16);
87
36.7k
        return *this;
88
36.7k
    }
89
90
0
    uint24_t& operator=(const int64_t value) {
91
0
        data[0] = static_cast<uint8_t>(value);
92
0
        data[1] = static_cast<uint8_t>(value >> 8);
93
0
        data[2] = static_cast<uint8_t>(value >> 16);
94
0
        return *this;
95
0
    }
96
97
2
    bool operator==(const uint24_t& value) const { return cmp(value) == 0; }
98
99
0
    bool operator!=(const uint24_t& value) const { return cmp(value) != 0; }
100
101
4.59M
    bool operator<(const uint24_t& value) const { return cmp(value) < 0; }
102
103
0
    bool operator<=(const uint24_t& value) const { return cmp(value) <= 0; }
104
105
890
    bool operator>(const uint24_t& value) const { return cmp(value) > 0; }
106
107
0
    bool operator>=(const uint24_t& value) const { return cmp(value) >= 0; }
108
109
4.59M
    int32_t cmp(const uint24_t& other) const {
110
4.59M
        if (data[2] > other.data[2]) {
111
1.17k
            return 1;
112
4.59M
        } else if (data[2] < other.data[2]) {
113
2.50k
            return -1;
114
2.50k
        }
115
116
4.59M
        if (data[1] > other.data[1]) {
117
696k
            return 1;
118
3.89M
        } else if (data[1] < other.data[1]) {
119
1.81M
            return -1;
120
1.81M
        }
121
122
2.08M
        if (data[0] > other.data[0]) {
123
1.59M
            return 1;
124
1.59M
        } else if (data[0] < other.data[0]) {
125
475k
            return -1;
126
475k
        }
127
128
12.6k
        return 0;
129
2.08M
    }
130
131
1.25k
    std::string to_string() const {
132
1.25k
        tm time_tm;
133
1.25k
        int value = *reinterpret_cast<const uint24_t*>(data);
134
1.25k
        memset(&time_tm, 0, sizeof(time_tm));
135
1.25k
        time_tm.tm_mday = static_cast<int>(value & 31);
136
1.25k
        time_tm.tm_mon = static_cast<int>(value >> 5 & 15) - 1;
137
1.25k
        time_tm.tm_year = static_cast<int>(value >> 9) - 1900;
138
1.25k
        char buf[20] = {'\0'};
139
1.25k
        strftime(buf, sizeof(buf), "%Y-%m-%d", &time_tm);
140
1.25k
        return std::string(buf);
141
1.25k
    }
142
143
0
    const uint8_t* get_data() const { return data; }
144
145
private:
146
    uint8_t data[3];
147
} __attribute__((packed));
148
149
static_assert(std::is_trivial<uint24_t>::value, "uint24_t should be a POD type");
150
151
inline std::ostream& operator<<(std::ostream& os, const uint24_t& val) {
152
    os << val.to_string();
153
    return os;
154
}
155
156
} // namespace doris