2015-03-04 10:47:53 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <DB/IO/WriteBuffer.h>
|
|
|
|
|
#include <DB/IO/BufferWithOwnMemory.h>
|
|
|
|
|
#include <statdaemons/AIO.h>
|
|
|
|
|
|
2015-03-13 11:02:43 +00:00
|
|
|
|
#include <string>
|
2015-03-05 14:21:55 +00:00
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
2015-03-04 10:47:53 +00:00
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2015-03-10 17:14:07 +00:00
|
|
|
|
/** Класс для асинхронной записи данных.
|
2015-03-13 11:02:43 +00:00
|
|
|
|
* Все размеры и смещения должны быть кратны DEFAULT_AIO_FILE_BLOCK_SIZE байтам.
|
2015-03-10 17:14:07 +00:00
|
|
|
|
*/
|
2015-03-13 11:02:43 +00:00
|
|
|
|
class WriteBufferAIO : public BufferWithOwnMemory<WriteBuffer>
|
2015-03-04 10:47:53 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
WriteBufferAIO(const std::string & filename_, size_t buffer_size_ = DBMS_DEFAULT_BUFFER_SIZE, int flags_ = -1, mode_t mode_ = 0666,
|
|
|
|
|
char * existing_memory_ = nullptr);
|
|
|
|
|
~WriteBufferAIO() override;
|
|
|
|
|
|
|
|
|
|
WriteBufferAIO(const WriteBufferAIO &) = delete;
|
|
|
|
|
WriteBufferAIO & operator=(const WriteBufferAIO &) = delete;
|
|
|
|
|
|
2015-03-05 14:21:55 +00:00
|
|
|
|
off_t seek(off_t off, int whence = SEEK_SET);
|
2015-03-13 11:02:43 +00:00
|
|
|
|
off_t getPositionInFile();
|
2015-03-05 14:21:55 +00:00
|
|
|
|
void truncate(off_t length = 0);
|
2015-03-10 16:34:00 +00:00
|
|
|
|
void sync();
|
2015-03-13 11:02:43 +00:00
|
|
|
|
std::string getFileName() const noexcept { return filename; }
|
|
|
|
|
int getFD() const noexcept { return fd; }
|
2015-03-04 10:47:53 +00:00
|
|
|
|
|
|
|
|
|
private:
|
2015-03-13 11:46:04 +00:00
|
|
|
|
/// Если в буфере ещё остались данные - запишем их.
|
|
|
|
|
void flush();
|
|
|
|
|
///
|
2015-03-13 11:02:43 +00:00
|
|
|
|
void nextImpl();
|
|
|
|
|
/// Ждать окончания текущей асинхронной задачи.
|
2015-03-13 11:46:04 +00:00
|
|
|
|
void waitForAIOCompletion();
|
2015-03-13 11:02:43 +00:00
|
|
|
|
/// Менять местами основной и дублирующий буферы.
|
|
|
|
|
void swapBuffers() noexcept;
|
2015-03-04 10:47:53 +00:00
|
|
|
|
|
|
|
|
|
private:
|
2015-03-10 17:14:07 +00:00
|
|
|
|
/// Буфер для асинхронных операций записи данных.
|
|
|
|
|
BufferWithOwnMemory<WriteBuffer> flush_buffer;
|
2015-03-10 11:43:30 +00:00
|
|
|
|
|
2015-03-11 13:26:24 +00:00
|
|
|
|
iocb request;
|
2015-03-04 10:47:53 +00:00
|
|
|
|
std::vector<iocb *> request_ptrs;
|
|
|
|
|
std::vector<io_event> events;
|
2015-03-10 11:43:30 +00:00
|
|
|
|
|
2015-03-11 14:32:32 +00:00
|
|
|
|
AIOContext aio_context;
|
|
|
|
|
|
|
|
|
|
const std::string filename;
|
|
|
|
|
|
2015-03-11 17:27:34 +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-03-10 17:14:07 +00:00
|
|
|
|
/// Асинхронная операция записи ещё не завершилась.
|
2015-03-04 10:47:53 +00:00
|
|
|
|
bool is_pending_write = false;
|
2015-03-10 17:14:07 +00:00
|
|
|
|
/// Было получено исключение.
|
2015-03-05 11:57:54 +00:00
|
|
|
|
bool got_exception = false;
|
2015-03-04 10:47:53 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|