2015-03-04 10:47:53 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadBufferFromFileBase.h>
|
|
|
|
#include <IO/ReadBuffer.h>
|
|
|
|
#include <IO/BufferWithOwnMemory.h>
|
|
|
|
#include <Core/Defines.h>
|
|
|
|
#include <Common/AIO.h>
|
|
|
|
#include <Common/CurrentMetrics.h>
|
2015-03-04 10:47:53 +00:00
|
|
|
|
2015-03-13 11:02:43 +00:00
|
|
|
#include <string>
|
2015-03-05 16:20:15 +00:00
|
|
|
#include <limits>
|
2015-03-05 14:21:55 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
|
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const Metric OpenFileForRead;
|
2016-10-24 04:06:27 +00:00
|
|
|
}
|
|
|
|
|
2015-03-04 10:47:53 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/** Class for asynchronous data reading.
|
2015-03-10 17:14:07 +00:00
|
|
|
*/
|
2015-03-30 15:39:55 +00:00
|
|
|
class ReadBufferAIO : public ReadBufferFromFileBase
|
2015-03-04 10:47:53 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
ReadBufferAIO(const std::string & filename_, size_t buffer_size_ = DBMS_DEFAULT_BUFFER_SIZE, int flags_ = -1,
|
|
|
|
char * existing_memory_ = nullptr);
|
|
|
|
~ReadBufferAIO() override;
|
2015-03-04 10:47:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ReadBufferAIO(const ReadBufferAIO &) = delete;
|
|
|
|
ReadBufferAIO & operator=(const ReadBufferAIO &) = delete;
|
2015-03-04 10:47:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void setMaxBytes(size_t max_bytes_read_);
|
|
|
|
off_t getPositionInFile() override { return first_unread_pos_in_file - (working_buffer.end() - pos); }
|
|
|
|
std::string getFileName() const override { return filename; }
|
|
|
|
int getFD() const override { return fd; }
|
2015-03-04 10:47:53 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
///
|
|
|
|
bool nextImpl() override;
|
|
|
|
///
|
|
|
|
off_t doSeek(off_t off, int whence) override;
|
2017-05-28 14:29:40 +00:00
|
|
|
/// Synchronously read the data.
|
2017-04-01 07:20:54 +00:00
|
|
|
void synchronousRead();
|
2017-05-28 14:29:40 +00:00
|
|
|
/// Get data from an asynchronous request.
|
2017-04-01 07:20:54 +00:00
|
|
|
void receive();
|
2017-05-28 14:29:40 +00:00
|
|
|
/// Ignore data from an asynchronous request.
|
2017-04-01 07:20:54 +00:00
|
|
|
void skip();
|
2017-05-28 14:29:40 +00:00
|
|
|
/// Wait for the end of the current asynchronous task.
|
2017-04-01 07:20:54 +00:00
|
|
|
bool waitForAIOCompletion();
|
2017-05-28 14:29:40 +00:00
|
|
|
/// Prepare the request.
|
2017-04-01 07:20:54 +00:00
|
|
|
void prepare();
|
2017-05-28 14:29:40 +00:00
|
|
|
/// Prepare for reading a duplicate buffer containing data from
|
|
|
|
/// of the last request.
|
2017-04-01 07:20:54 +00:00
|
|
|
void finalize();
|
2015-03-04 10:47:53 +00:00
|
|
|
|
|
|
|
private:
|
2017-05-28 14:29:40 +00:00
|
|
|
/// Buffer for asynchronous data read operations.
|
2017-04-01 07:20:54 +00:00
|
|
|
BufferWithOwnMemory<ReadBuffer> fill_buffer;
|
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/// Description of the asynchronous read request.
|
2017-04-01 07:20:54 +00:00
|
|
|
iocb request{};
|
|
|
|
std::future<ssize_t> future_bytes_read;
|
|
|
|
|
|
|
|
const std::string filename;
|
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/// The maximum number of bytes that can be read.
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t max_bytes_read = std::numeric_limits<size_t>::max();
|
2017-05-28 14:29:40 +00:00
|
|
|
/// Number of bytes requested.
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t requested_byte_count = 0;
|
2017-05-28 14:29:40 +00:00
|
|
|
/// The number of bytes read at the last request.
|
2017-04-01 07:20:54 +00:00
|
|
|
ssize_t bytes_read = 0;
|
2017-05-28 14:29:40 +00:00
|
|
|
/// The total number of bytes read.
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t total_bytes_read = 0;
|
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/// The position of the first unread byte in the file.
|
2017-04-01 07:20:54 +00:00
|
|
|
off_t first_unread_pos_in_file = 0;
|
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/// The starting position of the aligned region of the disk from which the data is read.
|
2017-04-01 07:20:54 +00:00
|
|
|
off_t region_aligned_begin = 0;
|
2017-05-28 14:29:40 +00:00
|
|
|
/// Left offset to align the region of the disk.
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t region_left_padding = 0;
|
2017-05-28 14:29:40 +00:00
|
|
|
/// The size of the aligned region of the disk.
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t region_aligned_size = 0;
|
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/// The file descriptor for read.
|
2017-04-01 07:20:54 +00:00
|
|
|
int fd = -1;
|
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/// The buffer to which the received data is written.
|
2017-04-01 07:20:54 +00:00
|
|
|
Position buffer_begin = nullptr;
|
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/// The asynchronous read operation is not yet completed.
|
2017-04-01 07:20:54 +00:00
|
|
|
bool is_pending_read = false;
|
2017-05-28 14:29:40 +00:00
|
|
|
/// The end of the file is reached.
|
2017-04-01 07:20:54 +00:00
|
|
|
bool is_eof = false;
|
2017-05-28 14:29:40 +00:00
|
|
|
/// At least one read request was sent.
|
2017-04-01 07:20:54 +00:00
|
|
|
bool is_started = false;
|
2017-05-28 14:29:40 +00:00
|
|
|
/// Is the operation asynchronous?
|
2017-04-01 07:20:54 +00:00
|
|
|
bool is_aio = false;
|
2017-05-28 14:29:40 +00:00
|
|
|
/// Did the asynchronous operation fail?
|
2017-04-01 07:20:54 +00:00
|
|
|
bool aio_failed = false;
|
|
|
|
|
|
|
|
CurrentMetrics::Increment metric_increment{CurrentMetrics::OpenFileForRead};
|
2015-03-04 10:47:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|