2015-03-04 10:47:53 +00:00
|
|
|
#pragma once
|
|
|
|
|
2015-03-30 15:39:55 +00:00
|
|
|
#include <DB/IO/ReadBufferFromFileBase.h>
|
2015-03-04 10:47:53 +00:00
|
|
|
#include <DB/IO/ReadBuffer.h>
|
|
|
|
#include <DB/IO/BufferWithOwnMemory.h>
|
2015-04-03 11:51:41 +00:00
|
|
|
#include <DB/Core/Defines.h>
|
2015-03-04 10:47:53 +00:00
|
|
|
#include <statdaemons/AIO.h>
|
|
|
|
|
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>
|
2015-04-03 11:51:41 +00:00
|
|
|
#include <sys/uio.h>
|
2015-03-05 14:21:55 +00:00
|
|
|
|
2015-03-04 10:47:53 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2015-03-16 10:41:58 +00:00
|
|
|
/** Класс для асинхронного чтения данных.
|
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:
|
2015-03-30 22:10:59 +00:00
|
|
|
ReadBufferAIO(const std::string & filename_, size_t buffer_size_ = DBMS_DEFAULT_BUFFER_SIZE, int flags_ = -1,
|
2015-03-04 10:47:53 +00:00
|
|
|
char * existing_memory_ = nullptr);
|
|
|
|
~ReadBufferAIO() override;
|
|
|
|
|
|
|
|
ReadBufferAIO(const ReadBufferAIO &) = delete;
|
|
|
|
ReadBufferAIO & operator=(const ReadBufferAIO &) = delete;
|
|
|
|
|
2015-03-05 16:20:15 +00:00
|
|
|
void setMaxBytes(size_t max_bytes_read_);
|
2015-03-30 15:39:55 +00:00
|
|
|
off_t getPositionInFile() override;
|
|
|
|
std::string getFileName() const noexcept override { return filename; }
|
|
|
|
int getFD() const noexcept override { return fd; }
|
2015-03-04 10:47:53 +00:00
|
|
|
|
|
|
|
private:
|
2015-03-13 11:02:43 +00:00
|
|
|
off_t getPositionInFileRelaxed() const noexcept;
|
2015-04-03 13:45:44 +00:00
|
|
|
off_t doSeek(off_t off, int whence) override;
|
2015-03-30 15:39:55 +00:00
|
|
|
bool nextImpl() override;
|
2015-04-03 22:44:32 +00:00
|
|
|
void synchronousRead();
|
|
|
|
void initRequest();
|
|
|
|
void publishReceivedData();
|
|
|
|
void sync();
|
2015-03-13 11:02:43 +00:00
|
|
|
/// Ждать окончания текущей асинхронной задачи.
|
2015-04-05 15:54:16 +00:00
|
|
|
bool waitForAIOCompletion();
|
2015-03-13 11:02:43 +00:00
|
|
|
/// Менять местами основной и дублирующий буферы.
|
|
|
|
void swapBuffers() noexcept;
|
2015-04-05 15:54:16 +00:00
|
|
|
void skipPendingAIO();
|
2015-03-04 10:47:53 +00:00
|
|
|
|
|
|
|
private:
|
2015-03-10 17:14:07 +00:00
|
|
|
/// Буфер для асинхронных операций чтения данных.
|
|
|
|
BufferWithOwnMemory<ReadBuffer> fill_buffer;
|
2015-03-10 11:43:30 +00:00
|
|
|
|
2015-03-11 13:26:24 +00:00
|
|
|
iocb request;
|
2015-03-16 10:49:27 +00:00
|
|
|
std::vector<iocb *> request_ptrs{&request};
|
|
|
|
std::vector<io_event> events{1};
|
2015-03-10 11:43:30 +00:00
|
|
|
|
2015-03-16 10:41:58 +00:00
|
|
|
AIOContext aio_context{1};
|
2015-03-11 14:32:32 +00:00
|
|
|
|
|
|
|
const std::string filename;
|
|
|
|
|
2015-04-03 22:44:32 +00:00
|
|
|
ssize_t bytes_read = 0;
|
2015-03-05 16:20:15 +00:00
|
|
|
size_t max_bytes_read = std::numeric_limits<size_t>::max();
|
2015-03-06 11:08:25 +00:00
|
|
|
size_t total_bytes_read = 0;
|
2015-03-17 12:44:49 +00:00
|
|
|
size_t requested_byte_count = 0;
|
2015-04-03 22:44:32 +00:00
|
|
|
off_t region_aligned_begin = 0;
|
2015-03-05 14:21:55 +00:00
|
|
|
off_t pos_in_file = 0;
|
2015-03-11 14:32:32 +00:00
|
|
|
int fd = -1;
|
2015-03-10 11:43:30 +00:00
|
|
|
|
2015-04-04 22:24:41 +00:00
|
|
|
Position buffer_begin = nullptr;
|
|
|
|
off_t region_aligned_size = 0;
|
2015-04-05 20:04:55 +00:00
|
|
|
|
2015-03-10 17:14:07 +00:00
|
|
|
/// Асинхронная операция чтения ещё не завершилась.
|
2015-03-04 10:47:53 +00:00
|
|
|
bool is_pending_read = false;
|
2015-03-10 17:14:07 +00:00
|
|
|
/// Конец файла достигнут.
|
2015-03-05 14:21:55 +00:00
|
|
|
bool is_eof = false;
|
2015-03-10 17:14:07 +00:00
|
|
|
/// Был отправлен хоть один запрос на асинхронную операцию чтения.
|
2015-03-05 16:20:15 +00:00
|
|
|
bool is_started = false;
|
2015-04-05 20:04:55 +00:00
|
|
|
/// Асинхронная операция завершилась неудачно?
|
2015-04-05 17:09:23 +00:00
|
|
|
bool aio_failed = false;
|
2015-03-04 10:47:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|