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>
|
2015-04-16 12:06:05 +00:00
|
|
|
|
#include <DB/Common/ProfileEvents.h>
|
2011-10-24 12:10:59 +00:00
|
|
|
|
|
2015-03-30 15:39:55 +00:00
|
|
|
|
#include <DB/IO/WriteBufferFromFileBase.h>
|
2011-10-24 12:10:59 +00:00
|
|
|
|
#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
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** Работает с готовым файловым дескриптором. Не открывает и не закрывает файл.
|
|
|
|
|
*/
|
2015-03-30 15:39:55 +00:00
|
|
|
|
class WriteBufferFromFileDescriptor : public WriteBufferFromFileBase
|
2011-10-24 12:10:59 +00:00
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
int fd;
|
2015-04-16 12:06:05 +00:00
|
|
|
|
|
2011-10-24 12:10:59 +00:00
|
|
|
|
void nextImpl()
|
|
|
|
|
{
|
|
|
|
|
if (!offset())
|
|
|
|
|
return;
|
2011-12-26 07:07:30 +00:00
|
|
|
|
|
|
|
|
|
size_t bytes_written = 0;
|
|
|
|
|
while (bytes_written != offset())
|
|
|
|
|
{
|
2015-04-16 12:06:05 +00:00
|
|
|
|
ProfileEvents::increment(ProfileEvents::WriteBufferFromFileDescriptorWrite);
|
|
|
|
|
|
2011-12-26 07:07:30 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2015-04-16 12:06:05 +00:00
|
|
|
|
|
|
|
|
|
ProfileEvents::increment(ProfileEvents::WriteBufferFromFileDescriptorWriteBytes, bytes_written);
|
2011-10-24 12:10:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Имя или описание файла
|
2015-04-14 11:35:10 +00:00
|
|
|
|
virtual std::string getFileName() const override
|
2011-10-24 12:10:59 +00:00
|
|
|
|
{
|
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)
|
2015-03-30 15:39:55 +00:00
|
|
|
|
: WriteBufferFromFileBase(buf_size, existing_memory, alignment), fd(fd_) {}
|
2011-10-24 12:10:59 +00:00
|
|
|
|
|
2012-02-24 19:21:38 +00:00
|
|
|
|
/** Можно вызывать для инициализации, если нужный fd не был передан в конструктор.
|
|
|
|
|
* Менять fd во время работы нельзя.
|
|
|
|
|
*/
|
|
|
|
|
void setFD(int fd_)
|
|
|
|
|
{
|
|
|
|
|
fd = fd_;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-02 23:14:08 +00:00
|
|
|
|
~WriteBufferFromFileDescriptor()
|
2011-10-24 12:10:59 +00:00
|
|
|
|
{
|
2013-11-18 17:17:45 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2011-12-12 01:06:13 +00:00
|
|
|
|
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
|
|
|
|
}
|
2011-12-28 20:01:41 +00:00
|
|
|
|
|
2015-04-14 11:35:10 +00:00
|
|
|
|
int getFD() const override
|
2011-12-28 20:01:41 +00:00
|
|
|
|
{
|
|
|
|
|
return fd;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-30 15:39:55 +00:00
|
|
|
|
off_t getPositionInFile() override
|
|
|
|
|
{
|
|
|
|
|
return seek(0, SEEK_CUR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sync() override
|
2013-09-15 01:10:16 +00:00
|
|
|
|
{
|
|
|
|
|
/// Если в буфере ещё остались данные - запишем их.
|
|
|
|
|
next();
|
|
|
|
|
|
|
|
|
|
/// Попросим ОС сбросить данные на диск.
|
|
|
|
|
int res = fsync(fd);
|
|
|
|
|
if (-1 == res)
|
|
|
|
|
throwFromErrno("Cannot fsync " + getFileName(), ErrorCodes::CANNOT_FSYNC);
|
|
|
|
|
}
|
2015-04-03 13:45:44 +00:00
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
off_t doSeek(off_t offset, int whence) override
|
|
|
|
|
{
|
|
|
|
|
off_t res = lseek(fd, offset, whence);
|
|
|
|
|
if (-1 == res)
|
|
|
|
|
throwFromErrno("Cannot seek through file " + getFileName(), ErrorCodes::CANNOT_SEEK_THROUGH_FILE);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2015-04-30 15:04:42 +00:00
|
|
|
|
|
|
|
|
|
void doTruncate(off_t length) override
|
|
|
|
|
{
|
|
|
|
|
int res = ftruncate(fd, length);
|
|
|
|
|
if (-1 == res)
|
|
|
|
|
throwFromErrno("Cannot truncate file " + getFileName(), ErrorCodes::CANNOT_TRUNCATE_FILE);
|
|
|
|
|
}
|
2011-10-24 12:10:59 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|