[coding] Moved FilesContainer Writers to file_writer.hpp.

This commit is contained in:
Maxim Pimenov 2019-09-20 15:44:33 +03:00 committed by Tatiana Yan
parent ab23306ef0
commit bf4f6a303e
7 changed files with 49 additions and 65 deletions

View file

@ -29,7 +29,6 @@ set(
diff.hpp
elias_coder.hpp
endianness.hpp
file_container_writers.hpp
file_reader.cpp
file_reader.hpp
file_sort.hpp

View file

@ -1,51 +0,0 @@
#pragma once
#include "coding/file_writer.hpp"
#include "coding/internal/file_data.hpp"
#include "base/assert.hpp"
#include <cstdint>
#include <string>
#include <vector>
class FileContainerWriter : public FileWriter
{
public:
FileContainerWriter(std::string const & fileName, Op operation)
: FileWriter(fileName, operation)
{
}
void WritePaddingByEnd(size_t factor) { WritePadding(Size(), factor); }
void WritePaddingByPos(size_t factor) { WritePadding(Pos(), factor); }
private:
void WritePadding(uint64_t offset, uint64_t factor)
{
ASSERT_GREATER(factor, 1, ());
uint64_t const padding = ((offset + factor - 1) / factor) * factor - offset;
if (!padding)
return;
std::vector<uint8_t> buffer(static_cast<size_t>(padding));
Write(buffer.data(), buffer.size());
}
};
class TruncatingFileWriter : public FileContainerWriter
{
public:
explicit TruncatingFileWriter(std::string const & fileName)
: FileContainerWriter(fileName, FileWriter::OP_WRITE_EXISTING)
{
}
TruncatingFileWriter(TruncatingFileWriter && rhs) = default;
// Writer overrides:
~TruncatingFileWriter() override
{
GetFileData().Flush();
GetFileData().Truncate(Pos());
}
};

View file

@ -1,18 +1,16 @@
#pragma once
#include "coding/writer.hpp"
#include "coding/internal/file_data.hpp"
#include "base/assert.hpp"
#include "base/base.hpp"
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
namespace base
{
class FileData;
}
#include <vector>
// FileWriter, not thread safe.
class FileWriter : public Writer
@ -61,3 +59,44 @@ protected:
private:
std::unique_ptr<base::FileData> m_pFileData;
};
class FilesContainerWriter : public FileWriter
{
public:
FilesContainerWriter(std::string const & fileName, Op operation)
: FileWriter(fileName, operation)
{
}
void WritePaddingByEnd(size_t factor) { WritePadding(Size(), factor); }
void WritePaddingByPos(size_t factor) { WritePadding(Pos(), factor); }
private:
void WritePadding(uint64_t offset, uint64_t factor)
{
ASSERT_GREATER(factor, 1, ());
uint64_t const padding = ((offset + factor - 1) / factor) * factor - offset;
if (padding == 0)
return;
std::vector<uint8_t> buffer(static_cast<size_t>(padding));
Write(buffer.data(), buffer.size());
}
};
class TruncatingFileWriter : public FilesContainerWriter
{
public:
explicit TruncatingFileWriter(std::string const & fileName)
: FilesContainerWriter(fileName, FileWriter::OP_WRITE_EXISTING)
{
}
TruncatingFileWriter(TruncatingFileWriter && rhs) = default;
// Writer overrides:
~TruncatingFileWriter() override
{
GetFileData().Flush();
GetFileData().Truncate(Pos());
}
};

View file

@ -385,7 +385,7 @@ void FilesContainerW::DeleteSection(Tag const & tag)
Open(FileWriter::OP_WRITE_EXISTING);
}
std::unique_ptr<FileContainerWriter> FilesContainerW::GetWriter(Tag const & tag)
std::unique_ptr<FilesContainerWriter> FilesContainerW::GetWriter(Tag const & tag)
{
ASSERT(!m_finished, ());
@ -426,7 +426,7 @@ std::unique_ptr<FileContainerWriter> FilesContainerW::GetWriter(Tag const & tag)
{
SaveCurrentSize();
auto writer = make_unique<FileContainerWriter>(m_name, FileWriter::OP_APPEND);
auto writer = make_unique<FilesContainerWriter>(m_name, FileWriter::OP_APPEND);
writer->WritePaddingByPos(kSectionAlignment);
m_info.emplace_back(tag, writer->Pos());

View file

@ -1,7 +1,7 @@
#pragma once
#include "coding/file_reader.hpp"
#include "coding/file_container_writers.hpp"
#include "coding/file_writer.hpp"
#include "base/assert.hpp"
#include "base/macros.hpp"
@ -241,7 +241,7 @@ public:
FileWriter::Op op = FileWriter::OP_WRITE_TRUNCATE);
~FilesContainerW();
std::unique_ptr<FileContainerWriter> GetWriter(Tag const & tag);
std::unique_ptr<FilesContainerWriter> GetWriter(Tag const & tag);
void Write(std::string const & fPath, Tag const & tag);
void Write(ModelReaderPtr reader, Tag const & tag);

View file

@ -1,4 +1,5 @@
#pragma once
#include "coding/internal/file64_api.hpp"
#include "base/base.hpp"

View file

@ -50,7 +50,6 @@
3D489BC31D3D21AE0052AA38 /* elias_coder_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3D489BB71D3D217E0052AA38 /* elias_coder_test.cpp */; };
3D74EF211F8F55740081202C /* csv_reader.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 3D74EF1F1F8F55740081202C /* csv_reader.hpp */; };
3D74EF221F8F55740081202C /* csv_reader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3D74EF201F8F55740081202C /* csv_reader.cpp */; };
3DAB4B6822FDC6DF00F7E5EB /* file_container_writers.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 3DAB4B6722FDC6DF00F7E5EB /* file_container_writers.hpp */; };
3DAB4B6E23018E0500F7E5EB /* buffered_file_writer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3DAB4B6C23018E0500F7E5EB /* buffered_file_writer.cpp */; };
3DAB4B6F23018E0500F7E5EB /* buffered_file_writer.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 3DAB4B6D23018E0500F7E5EB /* buffered_file_writer.hpp */; };
402E9A9321D0DBD9002D3CF4 /* map_uint32_to_val.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 402E9A9221D0DBD9002D3CF4 /* map_uint32_to_val.hpp */; };
@ -207,7 +206,6 @@
3D489BBA1D3D217E0052AA38 /* succinct_mapper_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = succinct_mapper_test.cpp; sourceTree = "<group>"; };
3D74EF1F1F8F55740081202C /* csv_reader.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = csv_reader.hpp; sourceTree = "<group>"; };
3D74EF201F8F55740081202C /* csv_reader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = csv_reader.cpp; sourceTree = "<group>"; };
3DAB4B6722FDC6DF00F7E5EB /* file_container_writers.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = file_container_writers.hpp; sourceTree = "<group>"; };
3DAB4B6C23018E0500F7E5EB /* buffered_file_writer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = buffered_file_writer.cpp; sourceTree = "<group>"; };
3DAB4B6D23018E0500F7E5EB /* buffered_file_writer.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = buffered_file_writer.hpp; sourceTree = "<group>"; };
402E9A9221D0DBD9002D3CF4 /* map_uint32_to_val.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = map_uint32_to_val.hpp; sourceTree = "<group>"; };
@ -435,7 +433,6 @@
3954E00D233500E90007FDE4 /* files_container.hpp */,
3DAB4B6C23018E0500F7E5EB /* buffered_file_writer.cpp */,
3DAB4B6D23018E0500F7E5EB /* buffered_file_writer.hpp */,
3DAB4B6722FDC6DF00F7E5EB /* file_container_writers.hpp */,
3973743021C17EFE0003807A /* string_utf8_multilang.cpp */,
3973742F21C17EFE0003807A /* string_utf8_multilang.hpp */,
39C3C0BB21A43061003B4712 /* point_coding.cpp */,
@ -570,7 +567,6 @@
675342981A3F588C00A0A8C3 /* diff.hpp in Headers */,
670D04C01B0BA92D0013A7AC /* file_data.hpp in Headers */,
39C3C0BC21A43061003B4712 /* point_coding.hpp in Headers */,
3DAB4B6822FDC6DF00F7E5EB /* file_container_writers.hpp in Headers */,
39F376C7207D327B0058E8E0 /* geometry_coding.hpp in Headers */,
4563B063205909290057556D /* sha1.hpp in Headers */,
6753428B1A3F588C00A0A8C3 /* buffer_reader.hpp in Headers */,