be/src/exec/common/ipv6_to_binary.h
Line | Count | Source |
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 | | // This file is copied from |
18 | | // https://github.com/ClickHouse/ClickHouse/blob/master/src/Common/IPv6ToBinary.cpp |
19 | | // and modified by Doris |
20 | | |
21 | | #pragma once |
22 | | |
23 | | #include <algorithm> |
24 | | |
25 | | #include "exec/common/format_ip.h" |
26 | | |
27 | | namespace doris { |
28 | | |
29 | | /// Result array could be indexed with all possible uint8 values without extra check. |
30 | | /// For values greater than 128 we will store same value as for 128 (all bits set). |
31 | | constexpr size_t IPV6_MASKS_COUNT = 256; |
32 | | using RawMaskArrayV6 = std::array<uint8_t, IPV6_BINARY_LENGTH>; |
33 | | |
34 | 0 | static constexpr RawMaskArrayV6 generate_bit_mask(size_t prefix) { |
35 | 0 | RawMaskArrayV6 arr {0}; |
36 | 0 | prefix = std::min(prefix, arr.size() * 8); |
37 | 0 | int8_t i = IPV6_BINARY_LENGTH - 1; |
38 | 0 | for (; prefix >= 8; --i, prefix -= 8) { |
39 | 0 | arr[i] = 0xff; |
40 | 0 | } |
41 | 0 | if (prefix > 0) { |
42 | 0 | arr[i--] = static_cast<uint8_t>(~(0xff >> prefix)); |
43 | 0 | } |
44 | 0 | while (i >= 0) { |
45 | 0 | arr[i--] = 0x00; |
46 | 0 | } |
47 | 0 | return arr; |
48 | 0 | } Unexecuted instantiation: column_ip_test.cpp:_ZN5dorisL17generate_bit_maskEm Unexecuted instantiation: ip_value_test.cpp:_ZN5dorisL17generate_bit_maskEm Unexecuted instantiation: dictionary_get_nullable_test.cpp:_ZN5dorisL17generate_bit_maskEm Unexecuted instantiation: dictionary_ip_trie_test.cpp:_ZN5dorisL17generate_bit_maskEm Unexecuted instantiation: function_ip_dict_test.cpp:_ZN5dorisL17generate_bit_maskEm Unexecuted instantiation: function_ip_test.cpp:_ZN5dorisL17generate_bit_maskEm Unexecuted instantiation: function_ip.cpp:_ZN5dorisL17generate_bit_maskEm Unexecuted instantiation: ip_address_dictionary.cpp:_ZN5dorisL17generate_bit_maskEm |
49 | | |
50 | 0 | static constexpr std::array<RawMaskArrayV6, IPV6_MASKS_COUNT> generate_bit_masks() { |
51 | 0 | std::array<RawMaskArrayV6, IPV6_MASKS_COUNT> arr {}; |
52 | 0 | for (size_t i = 0; i < IPV6_MASKS_COUNT; ++i) { |
53 | 0 | arr[i] = generate_bit_mask(i); |
54 | 0 | } |
55 | 0 | return arr; |
56 | 0 | } Unexecuted instantiation: column_ip_test.cpp:_ZN5dorisL18generate_bit_masksEv Unexecuted instantiation: ip_value_test.cpp:_ZN5dorisL18generate_bit_masksEv Unexecuted instantiation: dictionary_get_nullable_test.cpp:_ZN5dorisL18generate_bit_masksEv Unexecuted instantiation: dictionary_ip_trie_test.cpp:_ZN5dorisL18generate_bit_masksEv Unexecuted instantiation: function_ip_dict_test.cpp:_ZN5dorisL18generate_bit_masksEv Unexecuted instantiation: function_ip_test.cpp:_ZN5dorisL18generate_bit_masksEv Unexecuted instantiation: function_ip.cpp:_ZN5dorisL18generate_bit_masksEv Unexecuted instantiation: ip_address_dictionary.cpp:_ZN5dorisL18generate_bit_masksEv |
57 | | |
58 | | /// Returns a reference to 16-byte array containing mask with first `prefix_len` bits set to `1` and `128 - prefix_len` to `0`. |
59 | | /// Store in little-endian byte order |
60 | | /// The reference is valid during all program execution time. |
61 | | /// Values of prefix_len greater than 128 interpreted as 128 exactly. |
62 | 9 | inline const std::array<uint8_t, 16>& get_cidr_mask_ipv6(uint8_t prefix_len) { |
63 | 9 | static constexpr auto IPV6_RAW_MASK_ARRAY = generate_bit_masks(); |
64 | 9 | return IPV6_RAW_MASK_ARRAY[prefix_len]; |
65 | 9 | } |
66 | | |
67 | | } // namespace doris |