dbms: Server: feature development. [#METR-15090]

This commit is contained in:
Alexey Arno 2015-04-03 16:45:44 +03:00
parent fa89f1643e
commit 4cccd8548e
11 changed files with 82 additions and 47 deletions

View File

@ -44,7 +44,7 @@ public:
{
bytes += offset();
bool res = nextImpl();
(void) sync();
sync();
if (!res)
working_buffer.resize(0);
@ -150,7 +150,7 @@ private:
* Кинуть исключение, если что-то не так.
*/
virtual bool nextImpl() { return false; };
virtual bool sync() { return false; }
virtual void sync() {}
};

View File

@ -28,15 +28,15 @@ public:
ReadBufferAIO & operator=(const ReadBufferAIO &) = delete;
void setMaxBytes(size_t max_bytes_read_);
off_t seek(off_t off, int whence = SEEK_SET) override;
off_t getPositionInFile() override;
std::string getFileName() const noexcept override { return filename; }
int getFD() const noexcept override { return fd; }
private:
off_t getPositionInFileRelaxed() const noexcept;
off_t doSeek(off_t off, int whence) override;
bool nextImpl() override;
bool sync() override;
void sync() override;
/// Ждать окончания текущей асинхронной задачи.
void waitForAIOCompletion();
/// Менять местами основной и дублирующий буферы.

View File

@ -12,16 +12,15 @@ namespace DB
class ReadBufferFromFileBase : public BufferWithOwnMemory<ReadBuffer>
{
public:
ReadBufferFromFileBase(size_t buf_size, char * existing_memory, size_t alignment)
: BufferWithOwnMemory<ReadBuffer>(buf_size, existing_memory, alignment)
{
}
virtual ~ReadBufferFromFileBase() {}
virtual off_t seek(off_t off, int whence) = 0;
ReadBufferFromFileBase(size_t buf_size, char * existing_memory, size_t alignment);
virtual ~ReadBufferFromFileBase();
off_t seek(off_t off, int whence = SEEK_SET);
virtual off_t getPositionInFile() = 0;
virtual std::string getFileName() const noexcept = 0;
virtual int getFD() const noexcept = 0;
protected:
virtual off_t doSeek(off_t off, int whence) = 0;
};
}

View File

@ -68,8 +68,14 @@ public:
return fd;
}
off_t getPositionInFile() override
{
return pos_in_file - (working_buffer.end() - pos);
}
private:
/// Если offset такой маленький, что мы не выйдем за пределы буфера, настоящий seek по файлу не делается.
off_t seek(off_t offset, int whence = SEEK_SET) override
off_t doSeek(off_t offset, int whence) override
{
off_t new_pos = offset;
if (whence == SEEK_CUR)
@ -99,11 +105,6 @@ public:
return res;
}
}
off_t getPositionInFile() override
{
return pos_in_file - (working_buffer.end() - pos);
}
};
}

View File

@ -26,7 +26,6 @@ public:
WriteBufferAIO(const WriteBufferAIO &) = delete;
WriteBufferAIO & operator=(const WriteBufferAIO &) = delete;
off_t seek(off_t off, int whence = SEEK_SET) override;
off_t getPositionInFile() override;
void truncate(off_t length = 0) override;
void sync() override;
@ -34,6 +33,8 @@ public:
int getFD() const noexcept override { return fd; }
private:
///
off_t doSeek(off_t off, int whence) override;
/// Если в буфере ещё остались данные - запишем их.
void flush();
///

View File

@ -12,18 +12,17 @@ namespace DB
class WriteBufferFromFileBase : public BufferWithOwnMemory<WriteBuffer>
{
public:
WriteBufferFromFileBase(size_t buf_size, char * existing_memory, size_t alignment)
: BufferWithOwnMemory<WriteBuffer>(buf_size, existing_memory, alignment)
{
}
virtual ~WriteBufferFromFileBase() {}
virtual off_t seek(off_t off, int whence) = 0;
WriteBufferFromFileBase(size_t buf_size, char * existing_memory, size_t alignment);
virtual ~WriteBufferFromFileBase();
off_t seek(off_t off, int whence = SEEK_SET);
virtual off_t getPositionInFile() = 0;
virtual void truncate(off_t length) = 0;
virtual void sync() = 0;
virtual std::string getFileName() const noexcept = 0;
virtual int getFD() const noexcept = 0;
protected:
virtual off_t doSeek(off_t off, int whence) = 0;
};
}

View File

@ -75,14 +75,6 @@ public:
return fd;
}
off_t seek(off_t offset, int whence = SEEK_SET) override
{
off_t res = lseek(fd, offset, whence);
if (-1 == res)
throwFromErrno("Cannot seek through file " + getFileName(), ErrorCodes::CANNOT_SEEK_THROUGH_FILE);
return res;
}
off_t getPositionInFile() override
{
return seek(0, SEEK_CUR);
@ -105,6 +97,15 @@ public:
if (-1 == res)
throwFromErrno("Cannot fsync " + getFileName(), ErrorCodes::CANNOT_FSYNC);
}
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;
}
};
}

View File

@ -37,7 +37,7 @@ ReadBufferAIO::~ReadBufferAIO()
{
try
{
(void) sync();
sync();
}
catch (...)
{
@ -59,9 +59,9 @@ void ReadBufferAIO::setMaxBytes(size_t max_bytes_read_)
max_bytes_read = max_bytes_read_;
}
off_t ReadBufferAIO::seek(off_t off, int whence)
off_t ReadBufferAIO::doSeek(off_t off, int whence)
{
(void) sync();
sync();
off_t new_pos;
@ -132,7 +132,7 @@ bool ReadBufferAIO::nextImpl()
if (is_eof)
return false;
(void) sync();
sync();
is_started = true;
/// Если конец файла только что достигнут, больше ничего не делаем.
@ -202,19 +202,13 @@ bool ReadBufferAIO::nextImpl()
return true;
}
bool ReadBufferAIO::sync()
void ReadBufferAIO::sync()
{
if (is_eof)
return false;
if (!is_started)
return false;
if (!is_pending_read)
return false;
if (is_eof || !is_started || !is_pending_read)
return;
waitForAIOCompletion();
swapBuffers();
return true;
}
void ReadBufferAIO::waitForAIOCompletion()

View File

@ -0,0 +1,20 @@
#include <DB/IO/ReadBufferFromFileBase.h>
namespace DB
{
ReadBufferFromFileBase::ReadBufferFromFileBase(size_t buf_size, char * existing_memory, size_t alignment)
: BufferWithOwnMemory<ReadBuffer>(buf_size, existing_memory, alignment)
{
}
ReadBufferFromFileBase::~ReadBufferFromFileBase()
{
}
off_t ReadBufferFromFileBase::seek(off_t off, int whence)
{
return doSeek(off, whence);
}
}

View File

@ -61,7 +61,7 @@ WriteBufferAIO::~WriteBufferAIO()
::close(fd2);
}
off_t WriteBufferAIO::seek(off_t off, int whence)
off_t WriteBufferAIO::doSeek(off_t off, int whence)
{
flush();

View File

@ -0,0 +1,20 @@
#include <DB/IO/WriteBufferFromFileBase.h>
namespace DB
{
WriteBufferFromFileBase::WriteBufferFromFileBase(size_t buf_size, char * existing_memory, size_t alignment)
: BufferWithOwnMemory<WriteBuffer>(buf_size, existing_memory, alignment)
{
}
WriteBufferFromFileBase::~WriteBufferFromFileBase()
{
}
off_t WriteBufferFromFileBase::seek(off_t off, int whence)
{
return doSeek(off, whence);
}
}