ClickHouse/dbms/include/DB/IO/WriteBufferFromFileDescriptor.h

105 lines
2.4 KiB
C
Raw Normal View History

2011-10-24 12:10:59 +00:00
#pragma once
#include <unistd.h>
#include <errno.h>
#include <DB/Core/Exception.h>
#include <DB/Core/ErrorCodes.h>
#include <DB/IO/WriteBuffer.h>
2013-06-21 20:34:19 +00:00
#include <DB/IO/WriteHelpers.h>
2011-10-24 12:10:59 +00:00
#include <DB/IO/BufferWithOwnMemory.h>
namespace DB
{
/** Работает с готовым файловым дескриптором. Не открывает и не закрывает файл.
*/
class WriteBufferFromFileDescriptor : public BufferWithOwnMemory<WriteBuffer>
{
protected:
int fd;
void nextImpl()
{
if (!offset())
return;
2011-12-26 07:07:30 +00:00
size_t bytes_written = 0;
while (bytes_written != offset())
{
ssize_t res = ::write(fd, working_buffer.begin() + bytes_written, offset() - bytes_written);
if ((-1 == res || 0 == res) && errno != EINTR)
throwFromErrno("Cannot write to file " + getFileName(), ErrorCodes::CANNOT_WRITE_TO_FILE_DESCRIPTOR);
if (res > 0)
bytes_written += res;
}
2011-10-24 12:10:59 +00:00
}
/// Имя или описание файла
virtual std::string getFileName()
{
2013-06-21 20:34:19 +00:00
return "(fd = " + toString(fd) + ")";
2011-10-24 12:10:59 +00:00
}
public:
2014-04-08 07:31:51 +00:00
WriteBufferFromFileDescriptor(int fd_ = -1, size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE, char * existing_memory = nullptr, size_t alignment = 0)
: BufferWithOwnMemory<WriteBuffer>(buf_size, existing_memory, alignment), fd(fd_) {}
2011-10-24 12:10:59 +00:00
/** Можно вызывать для инициализации, если нужный fd не был передан в конструктор.
* Менять fd во время работы нельзя.
*/
void setFD(int fd_)
{
fd = fd_;
}
~WriteBufferFromFileDescriptor()
2011-10-24 12:10:59 +00:00
{
2013-11-18 17:17:45 +00:00
try
{
next();
2013-11-18 17:17:45 +00:00
}
catch (...)
{
2013-11-18 19:18:03 +00:00
tryLogCurrentException(__PRETTY_FUNCTION__);
2013-11-18 17:17:45 +00:00
}
2011-10-24 12:10:59 +00:00
}
int getFD()
{
return fd;
}
off_t seek(off_t offset, int whence = SEEK_SET)
{
off_t res = lseek(fd, offset, whence);
if (-1 == res)
throwFromErrno("Cannot seek through file " + getFileName(), ErrorCodes::CANNOT_SEEK_THROUGH_FILE);
return res;
}
void truncate(off_t length = 0)
{
int res = ftruncate(fd, length);
if (-1 == res)
throwFromErrno("Cannot truncate file " + getFileName(), ErrorCodes::CANNOT_TRUNCATE_FILE);
}
2013-09-15 01:10:16 +00:00
void sync()
{
/// Если в буфере ещё остались данные - запишем их.
next();
/// Попросим ОС сбросить данные на диск.
int res = fsync(fd);
if (-1 == res)
throwFromErrno("Cannot fsync " + getFileName(), ErrorCodes::CANNOT_FSYNC);
}
2011-10-24 12:10:59 +00:00
};
}