2011-10-24 12:10:59 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <errno.h>
|
2015-12-13 04:52:13 +00:00
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
|
|
#include <experimental/optional>
|
2011-10-24 12:10:59 +00:00
|
|
|
|
|
2014-01-03 08:20:13 +00:00
|
|
|
|
#include <DB/Common/ProfileEvents.h>
|
2015-12-13 04:52:13 +00:00
|
|
|
|
#include <DB/Common/Stopwatch.h>
|
2014-01-03 08:20:13 +00:00
|
|
|
|
|
2015-10-05 01:35:28 +00:00
|
|
|
|
#include <DB/Common/Exception.h>
|
2016-01-21 01:47:28 +00:00
|
|
|
|
#include <DB/Common/CurrentMetrics.h>
|
2011-10-24 12:10:59 +00:00
|
|
|
|
|
2015-03-30 15:39:55 +00:00
|
|
|
|
#include <DB/IO/ReadBufferFromFileBase.h>
|
2011-10-24 12:10:59 +00:00
|
|
|
|
#include <DB/IO/ReadBuffer.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
|
|
|
|
|
{
|
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
|
namespace ErrorCodes
|
|
|
|
|
{
|
|
|
|
|
extern const int CANNOT_READ_FROM_FILE_DESCRIPTOR;
|
|
|
|
|
extern const int ARGUMENT_OUT_OF_BOUND;
|
|
|
|
|
extern const int CANNOT_SEEK_THROUGH_FILE;
|
|
|
|
|
extern const int CANNOT_SELECT;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-24 12:10:59 +00:00
|
|
|
|
/** Работает с готовым файловым дескриптором. Не открывает и не закрывает файл.
|
|
|
|
|
*/
|
2015-03-30 15:39:55 +00:00
|
|
|
|
class ReadBufferFromFileDescriptor : public ReadBufferFromFileBase
|
2011-10-24 12:10:59 +00:00
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
int fd;
|
2013-11-08 12:27:44 +00:00
|
|
|
|
off_t pos_in_file; /// Какому сдвигу в файле соответствует working_buffer.end().
|
2015-04-16 12:06:05 +00:00
|
|
|
|
|
2011-10-24 12:10:59 +00:00
|
|
|
|
bool nextImpl()
|
|
|
|
|
{
|
2011-12-26 07:07:30 +00:00
|
|
|
|
size_t bytes_read = 0;
|
|
|
|
|
while (!bytes_read)
|
|
|
|
|
{
|
2014-01-03 08:20:13 +00:00
|
|
|
|
ProfileEvents::increment(ProfileEvents::ReadBufferFromFileDescriptorRead);
|
|
|
|
|
|
2015-12-13 04:52:13 +00:00
|
|
|
|
std::experimental::optional<Stopwatch> watch;
|
|
|
|
|
if (profile_callback)
|
|
|
|
|
watch.emplace(clock_type);
|
|
|
|
|
|
2016-01-21 01:47:28 +00:00
|
|
|
|
ssize_t res = 0;
|
|
|
|
|
{
|
|
|
|
|
CurrentMetrics::Increment metric_increment{CurrentMetrics::Read};
|
|
|
|
|
res = ::read(fd, internal_buffer.begin(), internal_buffer.size());
|
|
|
|
|
}
|
2011-12-26 07:07:30 +00:00
|
|
|
|
if (!res)
|
|
|
|
|
break;
|
2015-04-16 12:06:05 +00:00
|
|
|
|
|
2011-12-26 07:07:30 +00:00
|
|
|
|
if (-1 == res && errno != EINTR)
|
|
|
|
|
throwFromErrno("Cannot read from file " + getFileName(), ErrorCodes::CANNOT_READ_FROM_FILE_DESCRIPTOR);
|
|
|
|
|
|
|
|
|
|
if (res > 0)
|
|
|
|
|
bytes_read += res;
|
2015-12-13 04:52:13 +00:00
|
|
|
|
|
|
|
|
|
if (profile_callback)
|
|
|
|
|
{
|
|
|
|
|
ProfileInfo info;
|
|
|
|
|
info.bytes_requested = internal_buffer.size();
|
|
|
|
|
info.bytes_read = res;
|
|
|
|
|
info.nanoseconds = watch->elapsed();
|
|
|
|
|
profile_callback(info);
|
|
|
|
|
}
|
2011-12-26 07:07:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-08 12:27:44 +00:00
|
|
|
|
pos_in_file += bytes_read;
|
|
|
|
|
|
2011-12-26 07:07:30 +00:00
|
|
|
|
if (bytes_read)
|
2015-04-16 12:06:05 +00:00
|
|
|
|
{
|
|
|
|
|
ProfileEvents::increment(ProfileEvents::ReadBufferFromFileDescriptorReadBytes, bytes_read);
|
2011-10-24 12:10:59 +00:00
|
|
|
|
working_buffer.resize(bytes_read);
|
2015-04-16 12:06:05 +00:00
|
|
|
|
}
|
2011-12-26 07:07:30 +00:00
|
|
|
|
else
|
|
|
|
|
return false;
|
2011-10-24 12:10:59 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Имя или описание файла
|
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
|
|
|
|
ReadBufferFromFileDescriptor(int fd_, size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE, char * existing_memory = nullptr, size_t alignment = 0)
|
2015-03-30 15:39:55 +00:00
|
|
|
|
: ReadBufferFromFileBase(buf_size, existing_memory, alignment), fd(fd_), pos_in_file(0) {}
|
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-04-03 13:45:44 +00:00
|
|
|
|
off_t getPositionInFile() override
|
|
|
|
|
{
|
|
|
|
|
return pos_in_file - (working_buffer.end() - pos);
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-13 04:52:13 +00:00
|
|
|
|
|
2015-04-03 13:45:44 +00:00
|
|
|
|
private:
|
2015-12-13 04:52:13 +00:00
|
|
|
|
|
2013-11-08 12:27:44 +00:00
|
|
|
|
/// Если offset такой маленький, что мы не выйдем за пределы буфера, настоящий seek по файлу не делается.
|
2015-04-03 13:45:44 +00:00
|
|
|
|
off_t doSeek(off_t offset, int whence) override
|
2011-12-28 20:01:41 +00:00
|
|
|
|
{
|
2013-11-08 12:27:44 +00:00
|
|
|
|
off_t new_pos = offset;
|
|
|
|
|
if (whence == SEEK_CUR)
|
|
|
|
|
new_pos = pos_in_file - (working_buffer.end() - pos) + offset;
|
|
|
|
|
else if (whence != SEEK_SET)
|
|
|
|
|
throw Exception("ReadBufferFromFileDescriptor::seek expects SEEK_SET or SEEK_CUR as whence", ErrorCodes::ARGUMENT_OUT_OF_BOUND);
|
|
|
|
|
|
|
|
|
|
/// Никуда не сдвинулись.
|
|
|
|
|
if (new_pos + (working_buffer.end() - pos) == pos_in_file)
|
|
|
|
|
return new_pos;
|
|
|
|
|
|
2015-02-07 23:13:04 +00:00
|
|
|
|
if (hasPendingData() && new_pos <= pos_in_file && new_pos >= pos_in_file - static_cast<off_t>(working_buffer.size()))
|
2013-11-08 12:27:44 +00:00
|
|
|
|
{
|
|
|
|
|
/// Остались в пределах буфера.
|
|
|
|
|
pos = working_buffer.begin() + (new_pos - (pos_in_file - working_buffer.size()));
|
|
|
|
|
return new_pos;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-01-03 08:20:13 +00:00
|
|
|
|
ProfileEvents::increment(ProfileEvents::Seek);
|
|
|
|
|
|
2013-11-08 12:27:44 +00:00
|
|
|
|
pos = working_buffer.end();
|
|
|
|
|
off_t res = lseek(fd, new_pos, SEEK_SET);
|
|
|
|
|
if (-1 == res)
|
|
|
|
|
throwFromErrno("Cannot seek through file " + getFileName(), ErrorCodes::CANNOT_SEEK_THROUGH_FILE);
|
|
|
|
|
pos_in_file = new_pos;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2011-12-28 20:01:41 +00:00
|
|
|
|
}
|
2015-12-13 08:51:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// При условии, что файловый дескриптор позволяет использовать select, проверяет в течение таймаута, есть ли данные для чтения.
|
|
|
|
|
bool poll(size_t timeout_microseconds)
|
|
|
|
|
{
|
|
|
|
|
fd_set fds;
|
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
|
FD_SET(fd, &fds);
|
|
|
|
|
timeval timeout = { time_t(timeout_microseconds / 1000000), time_t(timeout_microseconds % 1000000) };
|
|
|
|
|
|
|
|
|
|
int res = select(1, &fds, 0, 0, &timeout);
|
|
|
|
|
|
|
|
|
|
if (-1 == res)
|
|
|
|
|
throwFromErrno("Cannot select", ErrorCodes::CANNOT_SELECT);
|
|
|
|
|
|
|
|
|
|
return res > 0;
|
|
|
|
|
}
|
2011-10-24 12:10:59 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|