2015-03-04 10:47:53 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
2015-03-06 16:16:10 +00:00
|
|
|
|
#include <DB/IO/IBufferAIO.h>
|
2015-03-04 10:47:53 +00:00
|
|
|
|
#include <DB/IO/WriteBuffer.h>
|
|
|
|
|
#include <DB/IO/BufferWithOwnMemory.h>
|
|
|
|
|
#include <statdaemons/AIO.h>
|
|
|
|
|
|
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
|
|
|
|
/** Класс для асинхронной записи данных.
|
|
|
|
|
* Все размеры и смещения должны быть кратны 512 байтам.
|
|
|
|
|
*/
|
|
|
|
|
class WriteBufferAIO : public IBufferAIO, 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);
|
|
|
|
|
void truncate(off_t length = 0);
|
2015-03-10 16:34:00 +00:00
|
|
|
|
void sync();
|
2015-03-06 16:19:01 +00:00
|
|
|
|
std::string getFileName() const noexcept override { return filename; }
|
|
|
|
|
int getFD() const noexcept override { return fd; }
|
2015-03-04 10:47:53 +00:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void nextImpl() override;
|
2015-03-06 16:19:01 +00:00
|
|
|
|
void waitForCompletion() override;
|
|
|
|
|
void swapBuffers() noexcept override;
|
2015-03-04 10:47:53 +00:00
|
|
|
|
|
|
|
|
|
private:
|
2015-03-10 17:14:07 +00:00
|
|
|
|
/// Буфер для асинхронных операций записи данных.
|
|
|
|
|
BufferWithOwnMemory<WriteBuffer> flush_buffer;
|
|
|
|
|
const std::string filename;
|
2015-03-10 11:43:30 +00:00
|
|
|
|
|
2015-03-04 10:47:53 +00:00
|
|
|
|
AIOContext aio_context;
|
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-10 17:14:07 +00:00
|
|
|
|
int fd = -1;
|
2015-03-10 11:43:30 +00:00
|
|
|
|
size_t total_bytes_written = 0;
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|