1#include "duckdb/common/serializer/buffered_serializer.hpp"
2
3#include <cstring>
4
5namespace duckdb {
6
7BufferedSerializer::BufferedSerializer(idx_t maximum_size)
8 : BufferedSerializer(make_unsafe_uniq_array<data_t>(n: maximum_size), maximum_size) {
9}
10
11BufferedSerializer::BufferedSerializer(unsafe_unique_array<data_t> data, idx_t size)
12 : maximum_size(size), data(data.get()) {
13 blob.size = 0;
14 blob.data = std::move(data);
15}
16
17BufferedSerializer::BufferedSerializer(data_ptr_t data, idx_t size) : maximum_size(size), data(data) {
18 blob.size = 0;
19}
20
21void BufferedSerializer::WriteData(const_data_ptr_t buffer, idx_t write_size) {
22 if (blob.size + write_size >= maximum_size) {
23 do {
24 maximum_size *= 2;
25 } while (blob.size + write_size > maximum_size);
26 auto new_data = new data_t[maximum_size];
27 memcpy(dest: new_data, src: data, n: blob.size);
28 data = new_data;
29 blob.data = unsafe_unique_array<data_t>(new_data);
30 }
31
32 memcpy(dest: data + blob.size, src: buffer, n: write_size);
33 blob.size += write_size;
34}
35
36} // namespace duckdb
37