Files
scylladb/utils/memory_data_sink.hh
Pavel Emelyanov e57ee84662 util: Re-use seastar::util::memory_data_sink
A data_sink that stores buffers into an in-memory collection had
appeared in seastar recently. In Scylla there's similar thing that uses
memory_data_sink_buffer as a container, so it's possible to drop the
data_sink_impl iself in favor of seastar implementation.

For that to work there should be append_buffers() overload for the
aforementioned container. For its nice implementation the container, in
turn, needs to get push_back() method and value_type trait. The method
already exists, but is called put(), so just rename it. There's one more
user of it this method in S3 client, and it can enjoy the added
append_buffers() helper.

Signed-off-by: Pavel Emelyanov <xemul@scylladb.com>

Closes scylladb/scylladb#28124
2026-01-14 08:54:00 +02:00

57 lines
1.5 KiB
C++

/*
* Copyright (C) 2018-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#include <seastar/core/iostream.hh>
#include <seastar/core/temporary_buffer.hh>
#include <seastar/util/memory-data-sink.hh>
#include "utils/small_vector.hh"
#include "seastarx.hh"
#pragma once
// Accumulates data sent to the memory_data_sink allowing it
// to be examined later.
class memory_data_sink_buffers {
using buffers_type = utils::small_vector<temporary_buffer<char>, 1>;
buffers_type _bufs;
size_t _size = 0;
public:
using value_type = temporary_buffer<char>; // for std::back_inserter to work
size_t size() const { return _size; }
buffers_type& buffers() { return _bufs; }
const buffers_type& buffers() const { return _bufs; }
// Strong exception guarantees
void push_back(temporary_buffer<char>&& buf) {
auto size = buf.size();
_bufs.emplace_back(std::move(buf));
_size += size;
}
void clear() {
_bufs.clear();
_size = 0;
}
memory_data_sink_buffers() = default;
memory_data_sink_buffers(memory_data_sink_buffers&& other)
: _bufs(std::move(other._bufs))
, _size(std::exchange(other._size, 0))
{
}
};
inline void append_buffers(memory_data_sink_buffers& c, std::span<temporary_buffer<char>> bufs) {
std::ranges::move(bufs, std::back_inserter(c));
}
using memory_data_sink = seastar::util::basic_memory_data_sink<memory_data_sink_buffers, 128*1024>;