|
33 | 33 |
|
34 | 34 | #include <tcb/span.hpp> |
35 | 35 |
|
36 | | - |
37 | 36 | namespace dbps::processing { |
38 | 37 |
|
39 | 38 | // ----------------------------------------------------------------------------- |
@@ -151,123 +150,6 @@ inline constexpr size_t kUnsetVariableElementOffset = std::numeric_limits<size_t |
151 | 150 | // Constant for the size of the [u32 size] prefix for variable-size elements. |
152 | 151 | inline constexpr size_t kSizePrefixBytes = sizeof(uint32_t); |
153 | 152 |
|
154 | | -// ----------------------------------------------------------------------------- |
155 | | -// Templates for codecs for ByteBuffer |
156 | | -// ----------------------------------------------------------------------------- |
157 | | - |
158 | | -template <class T, const char* TypeName> |
159 | | -struct PlainValueCodec { |
160 | | - using value_type = T; |
161 | | - static constexpr bool is_fixed_sized = true; |
162 | | - |
163 | | - // Compile-time check that the type is trivially copyable (can be copied simply by memcpy) |
164 | | - static_assert(std::is_trivially_copyable_v<T>, |
165 | | - "PlainValueCodec requires trivially copyable T"); |
166 | | - |
167 | | - static constexpr std::string_view type_name() noexcept { |
168 | | - return std::string_view(TypeName); |
169 | | - } |
170 | | - |
171 | | - constexpr size_t element_size() const noexcept { |
172 | | - return sizeof(T); |
173 | | - } |
174 | | - |
175 | | - value_type Decode(tcb::span<const uint8_t> read_span) const { |
176 | | - if (read_span.size() != sizeof(T)) { |
177 | | - throw InvalidInputException("Decode: read_span size does not match sizeof(T)"); |
178 | | - } |
179 | | - T value; |
180 | | - std::memcpy(&value, read_span.data(), sizeof(T)); |
181 | | - return value; |
182 | | - } |
183 | | - |
184 | | - void Encode(const value_type& value, tcb::span<uint8_t> write_span) const { |
185 | | - if (write_span.size() != sizeof(T)) { |
186 | | - throw InvalidInputException("Encode: write_span size does not match sizeof(T)"); |
187 | | - } |
188 | | - std::memcpy(write_span.data(), &value, sizeof(T)); |
189 | | - } |
190 | | -}; |
191 | | - |
192 | | -struct StringFixedSizedCodec { |
193 | | - using value_type = std::string_view; |
194 | | - static constexpr bool is_fixed_sized = true; |
195 | | - |
196 | | - explicit StringFixedSizedCodec(size_t element_size_bytes = 0) : element_size_bytes_(element_size_bytes) { |
197 | | - if (element_size_bytes_ <= 0) { |
198 | | - throw InvalidInputException("StringFixedSizedCodec requires element_size_bytes > 0"); |
199 | | - } |
200 | | - } |
201 | | - |
202 | | - static constexpr std::string_view type_name() noexcept { |
203 | | - return "string (FIXED_LEN_BYTE_ARRAY)"; |
204 | | - } |
205 | | - |
206 | | - constexpr size_t element_size() const noexcept { |
207 | | - return element_size_bytes_; |
208 | | - } |
209 | | - |
210 | | - value_type Decode(tcb::span<const uint8_t> read_span) const { |
211 | | - if (read_span.size() != element_size_bytes_) { |
212 | | - throw InvalidInputException("Decode: read_span size does not match element_size_bytes"); |
213 | | - } |
214 | | - return std::string_view( |
215 | | - reinterpret_cast<const char*>(read_span.data()), |
216 | | - read_span.size()); |
217 | | - } |
218 | | - |
219 | | - void Encode(const value_type& value, tcb::span<uint8_t> write_span) const { |
220 | | - if (write_span.size() != element_size_bytes_) { |
221 | | - throw InvalidInputException("Encode: write_span size does not match element_size_bytes"); |
222 | | - } |
223 | | - if (value.size() != write_span.size()) { |
224 | | - throw InvalidInputException("Encode: value size does not match write_span size"); |
225 | | - } |
226 | | - std::memcpy(write_span.data(), value.data(), write_span.size()); |
227 | | - } |
228 | | - |
229 | | - private: |
230 | | - size_t element_size_bytes_; |
231 | | -}; |
232 | | - |
233 | | -struct StringVariableSizedCodec { |
234 | | - using value_type = std::string_view; |
235 | | - static constexpr bool is_fixed_sized = false; |
236 | | - |
237 | | - static constexpr std::string_view type_name() noexcept { |
238 | | - return "string (BYTE_ARRAY)"; |
239 | | - } |
240 | | - |
241 | | - size_t element_size() const { |
242 | | - throw InvalidInputException("StringVariableSizedCodec does not have a fixed element size"); |
243 | | - } |
244 | | - |
245 | | - value_type Decode(tcb::span<const uint8_t> read_span) const noexcept { |
246 | | - return std::string_view( |
247 | | - reinterpret_cast<const char*>(read_span.data()), |
248 | | - read_span.size()); |
249 | | - } |
250 | | - |
251 | | - void Encode(const value_type& value, tcb::span<uint8_t> write_span) const { |
252 | | - if (value.size() != write_span.size()) { |
253 | | - throw InvalidInputException("Encode: value size does not match write_span size"); |
254 | | - } |
255 | | - std::memcpy(write_span.data(), value.data(), write_span.size()); |
256 | | - } |
257 | | -}; |
258 | | - |
259 | | - |
260 | | - |
261 | | - |
262 | | - |
263 | | - |
264 | | - |
265 | | - |
266 | | - |
267 | | - |
268 | | - |
269 | | - |
270 | | - |
271 | 153 | // ----------------------------------------------------------------------------- |
272 | 154 | // Helper inline functions |
273 | 155 | // ----------------------------------------------------------------------------- |
@@ -774,18 +656,4 @@ void ByteBuffer<Codec>::RebindSpanToWriteBuffer() { |
774 | 656 | elements_span_ = tcb::span<const uint8_t>(write_buffer_.data(), write_buffer_.size()); |
775 | 657 | } |
776 | 658 |
|
777 | | -namespace { |
778 | | -inline constexpr char kI32TypeName[] = "int32"; |
779 | | -inline constexpr char kI64TypeName[] = "int64"; |
780 | | -inline constexpr char kF32TypeName[] = "float"; |
781 | | -inline constexpr char kF64TypeName[] = "double"; |
782 | | -} // namespace |
783 | | - |
784 | | -template class ByteBuffer<PlainValueCodec<int32_t, kI32TypeName>>; |
785 | | -template class ByteBuffer<PlainValueCodec<int64_t, kI64TypeName>>; |
786 | | -template class ByteBuffer<PlainValueCodec<float, kF32TypeName>>; |
787 | | -template class ByteBuffer<PlainValueCodec<double, kF64TypeName>>; |
788 | | -template class ByteBuffer<StringFixedSizedCodec>; |
789 | | -template class ByteBuffer<StringVariableSizedCodec>; |
790 | | - |
791 | 659 | } // namespace dbps::processing |
0 commit comments