Skip to content

Commit 97f1d85

Browse files
committed
- Reorganizing typed buffer code (adding missing files)
1 parent 003fa62 commit 97f1d85

3 files changed

Lines changed: 179 additions & 132 deletions

File tree

src/processing/typed_buffer.h

Lines changed: 0 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434
#include <tcb/span.hpp>
3535

36-
3736
namespace dbps::processing {
3837

3938
// -----------------------------------------------------------------------------
@@ -151,123 +150,6 @@ inline constexpr size_t kUnsetVariableElementOffset = std::numeric_limits<size_t
151150
// Constant for the size of the [u32 size] prefix for variable-size elements.
152151
inline constexpr size_t kSizePrefixBytes = sizeof(uint32_t);
153152

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-
271153
// -----------------------------------------------------------------------------
272154
// Helper inline functions
273155
// -----------------------------------------------------------------------------
@@ -774,18 +656,4 @@ void ByteBuffer<Codec>::RebindSpanToWriteBuffer() {
774656
elements_span_ = tcb::span<const uint8_t>(write_buffer_.data(), write_buffer_.size());
775657
}
776658

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-
791659
} // namespace dbps::processing
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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 <cstddef>
21+
#include <cstdint>
22+
#include <string_view>
23+
#include <tcb/span.hpp>
24+
#include "exceptions.h"
25+
26+
template <class T, const char* TypeName>
27+
struct PlainValueCodec {
28+
using value_type = T;
29+
static constexpr bool is_fixed_sized = true;
30+
31+
// Compile-time check that the type is trivially copyable (can be copied simply by memcpy)
32+
static_assert(std::is_trivially_copyable_v<T>,
33+
"PlainValueCodec requires trivially copyable T");
34+
35+
static constexpr std::string_view type_name() noexcept {
36+
return std::string_view(TypeName);
37+
}
38+
39+
constexpr size_t element_size() const noexcept {
40+
return sizeof(T);
41+
}
42+
43+
value_type Decode(tcb::span<const uint8_t> read_span) const {
44+
if (read_span.size() != sizeof(T)) {
45+
throw InvalidInputException("Decode: read_span size does not match sizeof(T)");
46+
}
47+
T value;
48+
std::memcpy(&value, read_span.data(), sizeof(T));
49+
return value;
50+
}
51+
52+
void Encode(const value_type& value, tcb::span<uint8_t> write_span) const {
53+
if (write_span.size() != sizeof(T)) {
54+
throw InvalidInputException("Encode: write_span size does not match sizeof(T)");
55+
}
56+
std::memcpy(write_span.data(), &value, sizeof(T));
57+
}
58+
};
59+
60+
struct StringFixedSizedCodec {
61+
using value_type = std::string_view;
62+
static constexpr bool is_fixed_sized = true;
63+
64+
explicit StringFixedSizedCodec(size_t element_size_bytes = 0) : element_size_bytes_(element_size_bytes) {
65+
if (element_size_bytes_ <= 0) {
66+
throw InvalidInputException("StringFixedSizedCodec requires element_size_bytes > 0");
67+
}
68+
}
69+
70+
static constexpr std::string_view type_name() noexcept {
71+
return "string (fixed-length)";
72+
}
73+
74+
constexpr size_t element_size() const noexcept {
75+
return element_size_bytes_;
76+
}
77+
78+
value_type Decode(tcb::span<const uint8_t> read_span) const {
79+
if (read_span.size() != element_size_bytes_) {
80+
throw InvalidInputException("Decode: read_span size does not match element_size_bytes");
81+
}
82+
return std::string_view(
83+
reinterpret_cast<const char*>(read_span.data()),
84+
read_span.size());
85+
}
86+
87+
void Encode(const value_type& value, tcb::span<uint8_t> write_span) const {
88+
if (write_span.size() != element_size_bytes_) {
89+
throw InvalidInputException("Encode: write_span size does not match element_size_bytes");
90+
}
91+
if (value.size() != write_span.size()) {
92+
throw InvalidInputException("Encode: value size does not match write_span size");
93+
}
94+
std::memcpy(write_span.data(), value.data(), write_span.size());
95+
}
96+
97+
private:
98+
size_t element_size_bytes_;
99+
};
100+
101+
struct StringVariableSizedCodec {
102+
using value_type = std::string_view;
103+
static constexpr bool is_fixed_sized = false;
104+
105+
static constexpr std::string_view type_name() noexcept {
106+
return "string (variable-length)";
107+
}
108+
109+
size_t element_size() const {
110+
throw InvalidInputException("StringVariableSizedCodec does not have a fixed element size");
111+
}
112+
113+
value_type Decode(tcb::span<const uint8_t> read_span) const noexcept {
114+
return std::string_view(
115+
reinterpret_cast<const char*>(read_span.data()),
116+
read_span.size());
117+
}
118+
119+
void Encode(const value_type& value, tcb::span<uint8_t> write_span) const {
120+
if (value.size() != write_span.size()) {
121+
throw InvalidInputException("Encode: value size does not match write_span size");
122+
}
123+
std::memcpy(write_span.data(), value.data(), write_span.size());
124+
}
125+
};
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 "typed_buffer_codecs.h"
21+
#include "typed_buffer.h"
22+
23+
namespace dbps::processing {
24+
25+
struct Int96 {
26+
int32_t lo;
27+
int32_t mid;
28+
int32_t hi;
29+
};
30+
31+
inline constexpr char kI32TypeName[] = "INT32";
32+
inline constexpr char kI64TypeName[] = "INT64";
33+
inline constexpr char kF32TypeName[] = "FLOAT";
34+
inline constexpr char kF64TypeName[] = "DOUBLE";
35+
inline constexpr char kInt96TypeName[] = "INT96";
36+
37+
using TypedBufferI32 = ByteBuffer<PlainValueCodec<int32_t, kI32TypeName>>;
38+
using TypedBufferI64 = ByteBuffer<PlainValueCodec<int64_t, kI64TypeName>>;
39+
using TypedBufferFloat = ByteBuffer<PlainValueCodec<float, kF32TypeName>>;
40+
using TypedBufferDouble = ByteBuffer<PlainValueCodec<double, kF64TypeName>>;
41+
using TypedBufferInt96 = ByteBuffer<PlainValueCodec<Int96, kInt96TypeName>>;
42+
using TypedBufferStringFixedSized = ByteBuffer<StringFixedSizedCodec>;
43+
using TypedBufferStringVariableSized = ByteBuffer<StringVariableSizedCodec>;
44+
45+
using TypedValuesBuffer = std::variant<
46+
TypedBufferI32,
47+
TypedBufferI64,
48+
TypedBufferFloat,
49+
TypedBufferDouble,
50+
TypedBufferInt96,
51+
TypedBufferStringFixedSized,
52+
TypedBufferStringVariableSized
53+
>;
54+
} // namespace dbps::processing

0 commit comments

Comments
 (0)