|
| 1 | +/* |
| 2 | + * Copyright 2026 Redpanda Data, Inc. |
| 3 | + * |
| 4 | + * Use of this software is governed by the Business Source License |
| 5 | + * included in the file licenses/BSL.md |
| 6 | + * |
| 7 | + * As of the Change Date specified in that file, in accordance with |
| 8 | + * the Business Source License, use of this software will be governed |
| 9 | + * by the Apache License, Version 2.0 |
| 10 | + */ |
| 11 | +#pragma once |
| 12 | + |
| 13 | +#include "kafka/protocol/types.h" |
| 14 | + |
| 15 | +#include <fmt/format.h> |
| 16 | + |
| 17 | +#include <array> |
| 18 | +#include <cstddef> |
| 19 | +#include <stdexcept> |
| 20 | + |
| 21 | +namespace kafka { |
| 22 | + |
| 23 | +// Cold helper kept out of line so fmt::format doesn't bloat at() and block it |
| 24 | +// from inlining at its hot call sites. |
| 25 | +[[noreturn]] inline void throw_api_key_out_of_range(api_key key) { |
| 26 | + throw std::out_of_range( |
| 27 | + fmt::format("api_key_indexed_array::at: {} out of range", key())); |
| 28 | +} |
| 29 | + |
| 30 | +/// \brief A dense, array-backed map from kafka::api_key to T spanning two |
| 31 | +/// disjoint key regions: the standard Kafka range [0, StandardSize) and the |
| 32 | +/// reserved Redpanda range [ReservedBase, ReservedBase + ReservedSize). |
| 33 | +/// |
| 34 | +/// Indexing routes to whichever region a key falls in, so callers treat it as |
| 35 | +/// a single array keyed by api_key without paying for the large gap between the |
| 36 | +/// two regions. Every member is constexpr, so the same type backs the |
| 37 | +/// compile-time dispatch/flex tables and the run-time probe/throughput tables. |
| 38 | +template< |
| 39 | + typename T, |
| 40 | + std::size_t StandardSize, |
| 41 | + std::size_t ReservedSize, |
| 42 | + api_key::type ReservedBase = redpanda_api_key_base()> |
| 43 | +class api_key_indexed_array { |
| 44 | + // Regions must not overlap, or standard keys in [ReservedBase, |
| 45 | + // StandardSize) would silently alias the reserved region. |
| 46 | + static_assert( |
| 47 | + static_cast<std::size_t>(ReservedBase) >= StandardSize, |
| 48 | + "reserved region overlaps the standard range"); |
| 49 | + |
| 50 | +public: |
| 51 | + constexpr api_key_indexed_array() = default; |
| 52 | + |
| 53 | + /// Fill both regions with \p init (e.g. the flex map's invalid sentinel). |
| 54 | + explicit constexpr api_key_indexed_array(const T& init) { |
| 55 | + _standard.fill(init); |
| 56 | + _reserved.fill(init); |
| 57 | + } |
| 58 | + |
| 59 | + /// Unchecked access, like std::array::operator[]. Out-of-range keys are |
| 60 | + /// undefined at run time and a compile error under constant evaluation. |
| 61 | + /// Standard keys (the common case) are routed first. |
| 62 | + constexpr T& operator[](api_key key) noexcept { |
| 63 | + if (!is_reserved(key)) [[likely]] { |
| 64 | + return _standard[static_cast<std::size_t>(key())]; |
| 65 | + } |
| 66 | + return _reserved[reserved_index(key)]; |
| 67 | + } |
| 68 | + constexpr const T& operator[](api_key key) const noexcept { |
| 69 | + if (!is_reserved(key)) [[likely]] { |
| 70 | + return _standard[static_cast<std::size_t>(key())]; |
| 71 | + } |
| 72 | + return _reserved[reserved_index(key)]; |
| 73 | + } |
| 74 | + |
| 75 | + /// Checked access with std::array::at semantics: throws std::out_of_range |
| 76 | + /// for any key that is not a valid index in either region. Standard keys |
| 77 | + /// (the common case) are checked and returned first. |
| 78 | + constexpr T& at(api_key key) { |
| 79 | + if (in_standard_range(key)) [[likely]] { |
| 80 | + return _standard[static_cast<std::size_t>(key())]; |
| 81 | + } |
| 82 | + if (is_reserved(key)) { |
| 83 | + return _reserved.at(reserved_index(key)); |
| 84 | + } |
| 85 | + throw_api_key_out_of_range(key); |
| 86 | + } |
| 87 | + constexpr const T& at(api_key key) const { |
| 88 | + if (in_standard_range(key)) [[likely]] { |
| 89 | + return _standard[static_cast<std::size_t>(key())]; |
| 90 | + } |
| 91 | + if (is_reserved(key)) { |
| 92 | + return _reserved.at(reserved_index(key)); |
| 93 | + } |
| 94 | + throw_api_key_out_of_range(key); |
| 95 | + } |
| 96 | + |
| 97 | + /// True iff \p key is a valid index in either region. |
| 98 | + constexpr bool contains(api_key key) const noexcept { |
| 99 | + if (in_standard_range(key)) [[likely]] { |
| 100 | + return true; |
| 101 | + } |
| 102 | + return in_reserved_range(key); |
| 103 | + } |
| 104 | + |
| 105 | + /// Pointer to the element for \p key, or nullptr if out of range. |
| 106 | + constexpr const T* find(api_key key) const noexcept { |
| 107 | + if (in_standard_range(key)) [[likely]] { |
| 108 | + return &_standard[static_cast<std::size_t>(key())]; |
| 109 | + } |
| 110 | + if (in_reserved_range(key)) { |
| 111 | + return &_reserved[reserved_index(key)]; |
| 112 | + } |
| 113 | + return nullptr; |
| 114 | + } |
| 115 | + constexpr T* find(api_key key) noexcept { |
| 116 | + if (in_standard_range(key)) [[likely]] { |
| 117 | + return &_standard[static_cast<std::size_t>(key())]; |
| 118 | + } |
| 119 | + if (in_reserved_range(key)) { |
| 120 | + return &_reserved[reserved_index(key)]; |
| 121 | + } |
| 122 | + return nullptr; |
| 123 | + } |
| 124 | + |
| 125 | + /// Pointer to the first element (standard region then reserved) for which |
| 126 | + /// pred(api_key, element) is true, or nullptr if none match. |
| 127 | + template<typename Pred> |
| 128 | + // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward) |
| 129 | + constexpr const T* find_if(Pred&& pred) const noexcept { |
| 130 | + for (std::size_t i = 0; i < StandardSize; ++i) { |
| 131 | + if (pred(api_key(static_cast<api_key::type>(i)), _standard[i])) { |
| 132 | + return &_standard[i]; |
| 133 | + } |
| 134 | + } |
| 135 | + for (std::size_t i = 0; i < ReservedSize; ++i) { |
| 136 | + if ( |
| 137 | + pred( |
| 138 | + api_key(static_cast<api_key::type>(ReservedBase + i)), |
| 139 | + _reserved[i])) { |
| 140 | + return &_reserved[i]; |
| 141 | + } |
| 142 | + } |
| 143 | + return nullptr; |
| 144 | + } |
| 145 | + |
| 146 | + /// Invoke f(api_key, T&) for every slot, standard region then reserved |
| 147 | + /// region, passing the real api_key (not a 0..N index). |
| 148 | + template<typename F> |
| 149 | + // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward) |
| 150 | + constexpr void for_each(F&& f) { |
| 151 | + for (std::size_t i = 0; i < StandardSize; ++i) { |
| 152 | + f(api_key(static_cast<api_key::type>(i)), _standard[i]); |
| 153 | + } |
| 154 | + for (std::size_t i = 0; i < ReservedSize; ++i) { |
| 155 | + f(api_key(static_cast<api_key::type>(ReservedBase + i)), |
| 156 | + _reserved[i]); |
| 157 | + } |
| 158 | + } |
| 159 | + template<typename F> |
| 160 | + // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward) |
| 161 | + constexpr void for_each(F&& f) const { |
| 162 | + for (std::size_t i = 0; i < StandardSize; ++i) { |
| 163 | + f(api_key(static_cast<api_key::type>(i)), _standard[i]); |
| 164 | + } |
| 165 | + for (std::size_t i = 0; i < ReservedSize; ++i) { |
| 166 | + f(api_key(static_cast<api_key::type>(ReservedBase + i)), |
| 167 | + _reserved[i]); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + constexpr bool operator==(const api_key_indexed_array&) const = default; |
| 172 | + |
| 173 | + static constexpr std::size_t standard_size() { return StandardSize; } |
| 174 | + static constexpr std::size_t reserved_size() { return ReservedSize; } |
| 175 | + static constexpr api_key reserved_base() { return api_key(ReservedBase); } |
| 176 | + |
| 177 | +private: |
| 178 | + static constexpr bool in_standard_range(api_key key) noexcept { |
| 179 | + return key() >= 0 && static_cast<std::size_t>(key()) < StandardSize; |
| 180 | + } |
| 181 | + static constexpr bool in_reserved_range(api_key key) noexcept { |
| 182 | + return is_reserved(key) && reserved_index(key) < ReservedSize; |
| 183 | + } |
| 184 | + static constexpr bool is_reserved(api_key key) noexcept { |
| 185 | + return key() >= ReservedBase; |
| 186 | + } |
| 187 | + static constexpr std::size_t reserved_index(api_key key) noexcept { |
| 188 | + return static_cast<std::size_t>(key() - ReservedBase); |
| 189 | + } |
| 190 | + |
| 191 | + std::array<T, StandardSize> _standard{}; |
| 192 | + std::array<T, ReservedSize> _reserved{}; |
| 193 | +}; |
| 194 | + |
| 195 | +} // namespace kafka |
0 commit comments