diff options
author | vng <viktor.govako@gmail.com> | 2011-09-05 17:25:01 +0400 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 01:23:07 +0300 |
commit | a0cf3c14137bd3ed863b607015d882ae54ffdfcf (patch) | |
tree | 4583ce49a9bcb9556968fc51c5e591a14448cae2 /coding | |
parent | 4f31145f6509610e6d6cf287b82951b65756919e (diff) |
Add truncate to FileWriter routine.
Diffstat (limited to 'coding')
-rw-r--r-- | coding/coding_tests/file_container_test.cpp | 2 | ||||
-rw-r--r-- | coding/file_container.cpp | 15 | ||||
-rw-r--r-- | coding/file_container.hpp | 2 | ||||
-rw-r--r-- | coding/file_writer.cpp | 5 | ||||
-rw-r--r-- | coding/file_writer.hpp | 2 | ||||
-rw-r--r-- | coding/internal/file_data.cpp | 9 | ||||
-rw-r--r-- | coding/internal/file_data.hpp | 2 |
7 files changed, 29 insertions, 8 deletions
diff --git a/coding/coding_tests/file_container_test.cpp b/coding/coding_tests/file_container_test.cpp index 952e010c37..83291c8975 100644 --- a/coding/coding_tests/file_container_test.cpp +++ b/coding/coding_tests/file_container_test.cpp @@ -3,6 +3,7 @@ #include "../file_container.hpp" #include "../varint.hpp" +#include "../../base/logging.hpp" #include "../../base/string_utils.hpp" @@ -146,6 +147,7 @@ namespace char const * key[], char const * value[], size_t count) { FilesContainerR reader(fName); + LOG(LINFO, ("Size=", reader.GetSize())); for (size_t i = 0; i < count; ++i) { diff --git a/coding/file_container.cpp b/coding/file_container.cpp index 7e5cacc994..718afb2c90 100644 --- a/coding/file_container.cpp +++ b/coding/file_container.cpp @@ -179,6 +179,7 @@ FileWriter FilesContainerW::GetWriter(Tag const & tag) FileWriter writer(m_name, FileWriter::OP_WRITE_EXISTING); writer.Seek(curr); + writer.Truncate(curr); return writer; } else @@ -224,16 +225,14 @@ void FilesContainerW::Finish() { ASSERT(!m_bFinished, ()); - { - uint64_t const curr = SaveCurrentSize(); - FileWriter writer(m_name, FileWriter::OP_WRITE_EXISTING); - writer.Seek(0); - WriteToSink(writer, curr); - } + uint64_t const curr = SaveCurrentSize(); - sort(m_info.begin(), m_info.end(), LessInfo()); + FileWriter writer(m_name, FileWriter::OP_WRITE_EXISTING); + writer.Seek(0); + WriteToSink(writer, curr); + writer.Seek(curr); - FileWriter writer(m_name, FileWriter::OP_APPEND); + sort(m_info.begin(), m_info.end(), LessInfo()); uint32_t const count = m_info.size(); WriteVarUint(writer, count); diff --git a/coding/file_container.hpp b/coding/file_container.hpp index 45ccad51ae..8b8ec54465 100644 --- a/coding/file_container.hpp +++ b/coding/file_container.hpp @@ -98,6 +98,8 @@ public: f(m_info[i].m_tag); } + inline uint64_t GetSize() const { return m_source.Size(); } + private: ReaderT m_source; }; diff --git a/coding/file_writer.cpp b/coding/file_writer.cpp index 03b0b5a959..5194eb9b5c 100644 --- a/coding/file_writer.cpp +++ b/coding/file_writer.cpp @@ -51,6 +51,11 @@ void FileWriter::Flush() m_pFileData->Flush(); } +void FileWriter::Truncate(uint64_t sz) +{ + m_pFileData->Truncate(sz); +} + void FileWriter::DeleteFileX(string const & fName) { my::DeleteFileX(fName); diff --git a/coding/file_writer.hpp b/coding/file_writer.hpp index 0c1b9302b8..b5ef6a1c18 100644 --- a/coding/file_writer.hpp +++ b/coding/file_writer.hpp @@ -33,8 +33,10 @@ public: void Seek(int64_t pos); int64_t Pos() const; void Write(void const * p, size_t size); + uint64_t Size() const; void Flush(); + void Truncate(uint64_t sz); static void DeleteFileX(string const & fName); diff --git a/coding/internal/file_data.cpp b/coding/internal/file_data.cpp index a59d065521..75b081a125 100644 --- a/coding/internal/file_data.cpp +++ b/coding/internal/file_data.cpp @@ -153,6 +153,15 @@ void FileData::Flush() #endif } +void FileData::Truncate(uint64_t sz) +{ +#ifdef OMIM_OS_WINDOWS + _chsize(fileno(m_File), sz); +#else + ftruncate(fileno(m_File), sz); +#endif +} + bool GetFileSize(string const & fName, uint64_t & sz) { try diff --git a/coding/internal/file_data.hpp b/coding/internal/file_data.hpp index 15f64be698..50c5bfe14b 100644 --- a/coding/internal/file_data.hpp +++ b/coding/internal/file_data.hpp @@ -27,7 +27,9 @@ public: void Read(uint64_t pos, void * p, size_t size); void Write(void const * p, size_t size); + void Flush(); + void Truncate(uint64_t sz); string GetName() const { return m_FileName; } |