|
| 1 | +#ifndef ZEROBUS_DETAIL_PROTO_BATCH_HPP |
| 2 | +#define ZEROBUS_DETAIL_PROTO_BATCH_HPP |
| 3 | + |
| 4 | +// Builds the parallel pointer/length arrays zerobus_stream_ingest_proto_records |
| 5 | +// expects, without copying payloads. |
| 6 | +// |
| 7 | +// Kept out of stream.cpp so tests can reach it: a Stream needs a live server, |
| 8 | +// but the invariants here — aliasing the caller's bytes, never handing the FFI |
| 9 | +// a null payload — are testable alone (tests/proto_batch_test.cpp). Free of |
| 10 | +// zerobus.h; the JSON equivalent stays in stream.cpp, where checked_c_str is. |
| 11 | + |
| 12 | +#include <cstddef> |
| 13 | +#include <cstdint> |
| 14 | +#include <string> |
| 15 | +#include <vector> |
| 16 | + |
| 17 | +#include "zerobus/error.hpp" |
| 18 | +#include "zerobus/record.hpp" |
| 19 | + |
| 20 | +namespace zerobus { |
| 21 | +namespace detail { |
| 22 | + |
| 23 | +// An empty payload still crosses the FFI as a non-null pointer with length 0, |
| 24 | +// rather than nullptr or a dangling data() result. |
| 25 | +inline constexpr std::uint8_t kEmptyPayloadSentinel = 0; |
| 26 | + |
| 27 | +inline const std::uint8_t* ptr_or_sentinel( |
| 28 | + const std::vector<std::uint8_t>& bytes) { |
| 29 | + return bytes.empty() ? &kEmptyPayloadSentinel : bytes.data(); |
| 30 | +} |
| 31 | + |
| 32 | +// Same sentinel for the raw form, so {nullptr, 0} is a valid empty record. |
| 33 | +inline const std::uint8_t* ptr_or_sentinel(const std::uint8_t* data, |
| 34 | + std::size_t len) { |
| 35 | + return len == 0 ? &kEmptyPayloadSentinel : data; |
| 36 | +} |
| 37 | + |
| 38 | +// The pointers alias the caller's record bytes, so a ProtoBatchView must not |
| 39 | +// outlive the records it was built from. |
| 40 | +struct ProtoBatchView { |
| 41 | + std::vector<const std::uint8_t*> ptrs; |
| 42 | + std::vector<std::uintptr_t> lens; |
| 43 | +}; |
| 44 | + |
| 45 | +inline ProtoBatchView make_proto_batch( |
| 46 | + const std::vector<std::vector<std::uint8_t>>& records) { |
| 47 | + ProtoBatchView v; |
| 48 | + v.ptrs.reserve(records.size()); |
| 49 | + v.lens.reserve(records.size()); |
| 50 | + for (const auto& r : records) { |
| 51 | + v.ptrs.push_back(ptr_or_sentinel(r)); |
| 52 | + v.lens.push_back(r.size()); |
| 53 | + } |
| 54 | + return v; |
| 55 | +} |
| 56 | + |
| 57 | +// Borrowing form. Its own loop rather than materialising the views into a |
| 58 | +// vector<vector<uint8_t>> and delegating above: those copies are the cost it |
| 59 | +// exists to avoid. |
| 60 | +inline ProtoBatchView make_proto_batch(const ProtoRecordView* records, |
| 61 | + std::size_t num_records) { |
| 62 | + if (records == nullptr && num_records != 0) { |
| 63 | + throw ZerobusException( |
| 64 | + "ingest_proto_records called with a null record array and a non-zero " |
| 65 | + "record count", |
| 66 | + false); |
| 67 | + } |
| 68 | + ProtoBatchView v; |
| 69 | + v.ptrs.reserve(num_records); |
| 70 | + v.lens.reserve(num_records); |
| 71 | + for (std::size_t i = 0; i < num_records; ++i) { |
| 72 | + // The core would dereference a sized null payload. Name the index so the |
| 73 | + // offending record is identifiable in a large batch. |
| 74 | + if (records[i].data == nullptr && records[i].size != 0) { |
| 75 | + throw ZerobusException( |
| 76 | + "proto record at index " + std::to_string(i) + |
| 77 | + " has a null data pointer with a non-zero size", |
| 78 | + false); |
| 79 | + } |
| 80 | + v.ptrs.push_back(ptr_or_sentinel(records[i].data, records[i].size)); |
| 81 | + v.lens.push_back(records[i].size); |
| 82 | + } |
| 83 | + return v; |
| 84 | +} |
| 85 | + |
| 86 | +} // namespace detail |
| 87 | +} // namespace zerobus |
| 88 | + |
| 89 | +#endif // ZEROBUS_DETAIL_PROTO_BATCH_HPP |
0 commit comments