1#pragma once
2
3#include <string>
4#include <ctime>
5#include <functional>
6#include <fcntl.h>
7#include <IO/ReadBuffer.h>
8#include <IO/BufferWithOwnMemory.h>
9#include <port/clock.h>
10
11namespace DB
12{
13
14class ReadBufferFromFileBase : public BufferWithOwnMemory<ReadBuffer>
15{
16public:
17 ReadBufferFromFileBase(size_t buf_size, char * existing_memory, size_t alignment);
18 ReadBufferFromFileBase(ReadBufferFromFileBase &&) = default;
19 ~ReadBufferFromFileBase() override;
20 off_t seek(off_t off, int whence = SEEK_SET);
21 virtual off_t getPositionInFile() = 0;
22 virtual std::string getFileName() const = 0;
23 virtual int getFD() const = 0;
24
25 /// It is possible to get information about the time of each reading.
26 struct ProfileInfo
27 {
28 size_t bytes_requested;
29 size_t bytes_read;
30 size_t nanoseconds;
31 };
32
33 using ProfileCallback = std::function<void(ProfileInfo)>;
34
35 /// CLOCK_MONOTONIC_COARSE is more than enough to track long reads - for example, hanging for a second.
36 void setProfileCallback(const ProfileCallback & profile_callback_, clockid_t clock_type_ = CLOCK_MONOTONIC_COARSE)
37 {
38 profile_callback = profile_callback_;
39 clock_type = clock_type_;
40 }
41
42protected:
43 ProfileCallback profile_callback;
44 clockid_t clock_type{};
45
46 /// Children implementation should be able to seek backwards
47 virtual off_t doSeek(off_t off, int whence) = 0;
48};
49
50}
51