1 #ifndef LIBFILEZILLA_AIO_READER_HEADER
2 #define LIBFILEZILLA_AIO_READER_HEADER
11 #include "../file.hpp"
12 #include "../thread_pool.hpp"
34 virtual bool seekable()
const {
return false; }
37 bool seek(uint64_t offset, uint64_t size = nosize);
42 std::wstring
const& name()
const {
return name_; }
45 virtual uint64_t
size()
const {
return size_; }
61 std::pair<aio_result, buffer_lease> get_buffer(
event_handler & h);
77 , logger_(pool.logger())
79 , max_buffers_(max_buffers ? max_buffers : 1)
84 , logger_(pool.logger())
86 , max_buffers_(max_buffers ? max_buffers : 1)
89 virtual std::pair<aio_result, buffer_lease> do_get_buffer(scoped_lock & l) = 0;
100 aio_buffer_pool & buffer_pool_;
101 logger_interface & logger_;
103 std::wstring
const name_;
105 size_t const max_buffers_{};
106 std::list<buffer_lease> buffers_;
108 uint64_t size_{nosize};
109 uint64_t max_size_{nosize};
110 uint64_t start_offset_{nosize};
111 uint64_t remaining_{nosize};
113 bool get_buffer_called_{};
126 : name_(std::move(name))
132 virtual std::unique_ptr<reader_factory>
clone()
const = 0;
145 virtual std::unique_ptr<reader_base>
open(
aio_buffer_pool & pool, uint64_t offset = 0, uint64_t size = reader_base::nosize,
size_t max_buffers = 0) = 0;
147 virtual bool seekable()
const {
return false; }
149 std::wstring name()
const {
return name_; }
151 virtual uint64_t size()
const {
return reader_base::nosize; }
152 virtual datetime mtime()
const {
return datetime(); }
169 virtual size_t preferred_buffer_count()
const {
return 1; }
172 reader_factory() =
default;
173 reader_factory(reader_factory
const&) =
default;
175 std::wstring
const name_;
199 explicit operator bool()
const {
return impl_.operator bool(); }
201 std::wstring name()
const {
return impl_ ? impl_->name() : std::wstring(); }
203 uint64_t size()
const {
return impl_ ? impl_->size() : aio_base::nosize; }
206 std::unique_ptr<reader_factory> impl_;
215 using reader_base::reader_base;
218 virtual std::pair<aio_result, buffer_lease> do_get_buffer(
scoped_lock & l)
override;
243 virtual
bool seekable() const override;
246 virtual
void FZ_PRIVATE_SYMBOL do_close(
scoped_lock & l) override;
247 virtual
bool FZ_PRIVATE_SYMBOL do_seek(
scoped_lock & l) override;
249 virtual
void FZ_PRIVATE_SYMBOL on_buffer_availability(
aio_waitable const* w) override;
251 void FZ_PRIVATE_SYMBOL entry();
263 virtual std::unique_ptr<reader_base>
open(
aio_buffer_pool & pool, uint64_t offset = 0, uint64_t size = reader_base::nosize,
size_t max_buffers = 4)
override;
264 virtual std::unique_ptr<reader_factory>
clone()
const override;
266 virtual bool seekable()
const override {
return true; }
268 virtual uint64_t size()
const override;
269 virtual datetime mtime()
const override;
273 virtual size_t preferred_buffer_count()
const override {
return 4; }
275 thread_pool & thread_pool_;
289 virtual bool seekable()
const override {
return true; }
292 virtual std::pair<aio_result, buffer_lease> FZ_PRIVATE_SYMBOL do_get_buffer(
scoped_lock & l)
override;
293 virtual void FZ_PRIVATE_SYMBOL do_close(
scoped_lock & l)
override;
294 virtual bool FZ_PRIVATE_SYMBOL do_seek(
scoped_lock & l)
override;
296 virtual void FZ_PRIVATE_SYMBOL on_buffer_availability(
aio_waitable const* w)
override;
298 std::string_view
const view_;
318 virtual std::unique_ptr<reader_base>
open(
aio_buffer_pool & pool, uint64_t offset = 0, uint64_t size = reader_base::nosize,
size_t max_buffers = 1)
override;
319 virtual std::unique_ptr<reader_factory>
clone()
const override;
321 virtual bool seekable()
const override {
return true; }
323 virtual uint64_t size()
const override {
return view_.size(); }
326 std::string_view
const view_;
338 virtual bool seekable()
const override {
return true; }
341 virtual std::pair<aio_result, buffer_lease> FZ_PRIVATE_SYMBOL do_get_buffer(
scoped_lock & l)
override;
342 virtual void FZ_PRIVATE_SYMBOL do_close(
scoped_lock & l)
override;
343 virtual bool FZ_PRIVATE_SYMBOL do_seek(
scoped_lock & l)
override;
345 virtual void FZ_PRIVATE_SYMBOL on_buffer_availability(
aio_waitable const* w)
override;
347 std::string
const data_;
360 , data_(std::move(data))
363 virtual std::unique_ptr<reader_base>
open(
aio_buffer_pool & pool, uint64_t offset = 0, uint64_t size = reader_base::nosize,
size_t max_buffers = 1)
override;
364 virtual std::unique_ptr<reader_factory>
clone()
const override;
366 virtual bool seekable()
const override {
return true; }
368 virtual uint64_t size()
const override {
return data_.size(); }
371 std::string
const data_;
Buffer management and wait machinery for asynchronous I/O.
A buffer pool for use with async readers/writers.
Definition: aio.hpp:132
Handle for asynchronous tasks.
Definition: thread_pool.hpp:24
Waitable condition variable.
Definition: mutex.hpp:196
Represents a point of time in wallclock, tracking the timestamps accuracy/precision.
Definition: time.hpp:41
Simple handler for asynchronous event processing.
Definition: event_handler.hpp:55
Factory for.
Definition: reader.hpp:259
virtual bool multiple_buffer_usage() const override
Whether the reader can benefit from multiple buffers.
Definition: reader.hpp:271
virtual std::unique_ptr< reader_base > open(aio_buffer_pool &pool, uint64_t offset=0, uint64_t size=reader_base::nosize, size_t max_buffers=4) override
Creates a reader.
virtual std::unique_ptr< reader_factory > clone() const override
Clones the factory.
File reader.
Definition: reader.hpp:232
file_reader(std::wstring &&name, aio_buffer_pool &pool, file &&f, thread_pool &tpool, uint64_t offset=0, uint64_t size=nosize, size_t max_buffers=4) noexcept
Constructs file reader.
Lean class for file access.
Definition: file.hpp:29
Base class for all readers.
Definition: reader.hpp:27
reader_base(std::wstring &&name, aio_buffer_pool &pool, size_t max_buffers) noexcept
Constructs a reader.
Definition: reader.hpp:75
bool rewind()
Only seekable readers can be rewound.
virtual bool do_seek(scoped_lock &)
Definition: reader.hpp:93
virtual uint64_t size() const
Size of the reader. If the size is indetermined, nosize is returned.
Definition: reader.hpp:45
virtual datetime mtime() const
Last modification time, might be indetermined.
Definition: reader.hpp:48
bool seek(uint64_t offset, uint64_t size=nosize)
If seek fails, the reader is in an undefined state and must be closed.
std::pair< aio_result, buffer_lease > get_buffer(aio_waiter &h)
Gets the next buffer with data from the reader.
Holder for reader factories.
Definition: reader.hpp:180
A reader factory.
Definition: reader.hpp:120
virtual std::unique_ptr< reader_factory > clone() const =0
Clones the factory.
virtual size_t min_buffer_usage() const
The reader requires at least this many buffers.
Definition: reader.hpp:160
virtual std::unique_ptr< reader_base > open(aio_buffer_pool &pool, uint64_t offset=0, uint64_t size=reader_base::nosize, size_t max_buffers=0)=0
Creates a reader.
virtual bool multiple_buffer_usage() const
Whether the reader can benefit from multiple buffers.
Definition: reader.hpp:167
A simple scoped lock.
Definition: mutex.hpp:93
Factory for.
Definition: reader.hpp:352
virtual std::unique_ptr< reader_base > open(aio_buffer_pool &pool, uint64_t offset=0, uint64_t size=reader_base::nosize, size_t max_buffers=1) override
Creates a reader.
virtual std::unique_ptr< reader_factory > clone() const override
Clones the factory.
String reader, keeps a copy of the string.
Definition: reader.hpp:331
A dumb thread-pool for asynchronous tasks.
Definition: thread_pool.hpp:64
Base class for threaded readers.
Definition: reader.hpp:213
Definition: reader.hpp:307
virtual std::unique_ptr< reader_base > open(aio_buffer_pool &pool, uint64_t offset=0, uint64_t size=reader_base::nosize, size_t max_buffers=1) override
Creates a reader.
virtual std::unique_ptr< reader_factory > clone() const override
Clones the factory.
Definition: reader.hpp:283
The namespace used by libfilezilla.
Definition: apply.hpp:17