From e62a18304e4bae13f2ffb77f94d9665a06c19d39 Mon Sep 17 00:00:00 2001 From: Calle Wilund Date: Wed, 10 Sep 2025 13:43:02 +0000 Subject: [PATCH] utils::seekable_source: Add a seekable IO source type Extension of data_source, with the ability to a.) Seek in any direction, i.e. move backwards. Thus not pure stream. b.) Read a limited number of bytes. The very transparent reason for the interface is to have a base abstraction for providing a read-only file layer for networked resources. --- utils/seekable_source.hh | 59 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 utils/seekable_source.hh diff --git a/utils/seekable_source.hh b/utils/seekable_source.hh new file mode 100644 index 0000000000..acddcb2b52 --- /dev/null +++ b/utils/seekable_source.hh @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2025-present ScyllaDB + */ + +/* + * SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0 + */ + +#pragma once + +#include +#include + +class seekable_data_source_impl : public seastar::data_source_impl { +public: + virtual seastar::future> get(size_t limit) = 0; + virtual seastar::future<> seek(uint64_t pos) = 0; + virtual seastar::future size() { + return seastar::make_ready_future(0); + } + virtual seastar::future timestamp() { + return seastar::make_ready_future(); + } +}; + +class seekable_data_source : public seastar::data_source { +public: + seekable_data_source(std::unique_ptr src) + : seastar::data_source(std::move(src)) + {} + seastar::future> get(size_t limit) noexcept { + try { + return static_cast(impl())->get(limit); + } catch (...) { + return seastar::current_exception_as_future>(); + } + } + seastar::future<> seek(uint64_t pos) noexcept { + try { + return static_cast(impl())->seek(pos); + } catch (...) { + return seastar::current_exception_as_future<>(); + } + } + seastar::future size() noexcept { + try { + return static_cast(impl())->size(); + } catch (...) { + return seastar::current_exception_as_future(); + } + } + seastar::future timestamp() noexcept { + try { + return static_cast(impl())->timestamp(); + } catch (...) { + return seastar::current_exception_as_future(); + } + } +};