diff options
author | Maxim Pimenov <m@maps.me> | 2018-06-20 17:46:48 +0300 |
---|---|---|
committer | Arsentiy Milchakov <milcars@mapswithme.com> | 2018-06-21 15:05:58 +0300 |
commit | 73df2beda7996e7b443cba41d451daccb2e29a3e (patch) | |
tree | 7f0c7b9e0ee8f33c7dd8c9cd387d9198f4b51e67 /coding | |
parent | 222a75ff67e65a31058ba63136d4b2b3c9ce8591 (diff) |
[coding] Default cache params in FileReader's constructor.
Now that we know all the places that depended on the
implicit params, fall back to the simple constructor.
The main problem here was that there used to be a component
in generator/intermediate_data.hpp where the same field could be
used both as a reader and as a writer via template metaprogramming.
This effectively forced the constructors of FileReader and
FileWriter to have the same parameter list.
Three default parameters in FileReader's constructor that could
be implicitly cast to one another only complicated the matters,
so once we have removed the |withException| param an extra
check was needed that all the constructors are now used as
intended.
Diffstat (limited to 'coding')
-rw-r--r-- | coding/coding_tests/csv_reader_test.cpp | 9 | ||||
-rw-r--r-- | coding/coding_tests/reader_test.cpp | 16 | ||||
-rw-r--r-- | coding/coding_tests/reader_writer_ops_test.cpp | 4 | ||||
-rw-r--r-- | coding/coding_tests/writer_test.cpp | 18 | ||||
-rw-r--r-- | coding/coding_tests/zip_creator_test.cpp | 5 | ||||
-rw-r--r-- | coding/coding_tests/zip_reader_test.cpp | 6 | ||||
-rw-r--r-- | coding/file_container.cpp | 14 | ||||
-rw-r--r-- | coding/file_reader.cpp | 19 | ||||
-rw-r--r-- | coding/file_reader.hpp | 6 | ||||
-rw-r--r-- | coding/file_sort.hpp | 3 |
10 files changed, 42 insertions, 58 deletions
diff --git a/coding/coding_tests/csv_reader_test.cpp b/coding/coding_tests/csv_reader_test.cpp index 48518c1139..60d3f77085 100644 --- a/coding/coding_tests/csv_reader_test.cpp +++ b/coding/coding_tests/csv_reader_test.cpp @@ -25,8 +25,7 @@ UNIT_TEST(CSVReaderSmoke) { auto const fileName = "test.csv"; ScopedFile sf(fileName, kCSV1); - FileReader fileReader(sf.GetFullPath(), FileReader::kDefaultLogPageSize, - FileReader::kDefaultLogPageCount); + FileReader fileReader(sf.GetFullPath()); CSVReader reader; reader.Read(fileReader, [](File const & file) { TEST_EQUAL(file.size(), 1, ()); @@ -50,8 +49,7 @@ UNIT_TEST(CSVReaderCustomDelimiter) { auto const fileName = "test.csv"; ScopedFile sf(fileName, kCSV2); - FileReader fileReader(sf.GetFullPath(), FileReader::kDefaultLogPageSize, - FileReader::kDefaultLogPageCount); + FileReader fileReader(sf.GetFullPath()); CSVReader reader; CSVReader::Params p; p.m_readHeader = true; @@ -69,8 +67,7 @@ UNIT_TEST(CSVReaderEmptyFile) { auto const fileName = "test.csv"; ScopedFile sf(fileName, kCSV2); - FileReader fileReader(sf.GetFullPath(), FileReader::kDefaultLogPageSize, - FileReader::kDefaultLogPageCount); + FileReader fileReader(sf.GetFullPath()); CSVReader reader; reader.Read(fileReader, [](File const & file) { TEST_EQUAL(file.size(), 0, ()); }); diff --git a/coding/coding_tests/reader_test.cpp b/coding/coding_tests/reader_test.cpp index d751fea5e2..0da69bb07a 100644 --- a/coding/coding_tests/reader_test.cpp +++ b/coding/coding_tests/reader_test.cpp @@ -30,8 +30,7 @@ UNIT_TEST(FileReaderSmokeTest) } { - FileReader fileReader("reader_test_tmp.dat", FileReader::kDefaultLogPageSize, - FileReader::kDefaultLogPageCount); + FileReader fileReader("reader_test_tmp.dat"); TestReader(fileReader); } FileWriter::DeleteFileX("reader_test_tmp.dat"); @@ -48,9 +47,7 @@ UNIT_TEST(BufferReaderSmokeTest) writer.Write(&data[0], data.size()); } - BufferReader r2(FileReader("reader_test_tmp.dat", FileReader::kDefaultLogPageSize, - FileReader::kDefaultLogPageCount), - 7); + BufferReader r2(FileReader("reader_test_tmp.dat"), 7); TestReader(r2); FileWriter::DeleteFileX("reader_test_tmp.dat"); } @@ -66,8 +63,7 @@ UNIT_TEST(FileReaderNonExistentFileTest) { try { - FileReader reader("skjhfaxniauiuq2bmnszmn093sklsd", FileReader::kDefaultLogPageSize, - FileReader::kDefaultLogPageCount); + FileReader reader("skjhfaxniauiuq2bmnszmn093sklsd"); TEST(false, ("Exception should be thrown!")); } catch (FileReader::OpenException &) @@ -85,8 +81,7 @@ UNIT_TEST(FileReaderReadAsText) { string text; - FileReader(fName, FileReader::kDefaultLogPageSize, FileReader::kDefaultLogPageCount) - .ReadAsString(text); + FileReader(fName).ReadAsString(text); TEST_EQUAL(text, fName, ()); } @@ -104,8 +99,7 @@ UNIT_TEST(ReaderStreamBuf) } { - ReaderStreamBuf buffer(make_unique<FileReader>(name, FileReader::kDefaultLogPageSize, - FileReader::kDefaultLogPageCount)); + ReaderStreamBuf buffer(make_unique<FileReader>(name)); istream s(&buffer); std::string str; diff --git a/coding/coding_tests/reader_writer_ops_test.cpp b/coding/coding_tests/reader_writer_ops_test.cpp index dc4c4526b9..ecf6ff2081 100644 --- a/coding/coding_tests/reader_writer_ops_test.cpp +++ b/coding/coding_tests/reader_writer_ops_test.cpp @@ -21,7 +21,7 @@ namespace } { - FileReader reader(tmpFile, FileReader::kDefaultLogPageSize, FileReader::kDefaultLogPageCount); + FileReader reader(tmpFile); buffer.clear(); MemWriter<vector<char> > writer(buffer); rw_ops::Reverse(reader, writer); @@ -64,7 +64,7 @@ UNIT_TEST(Reverse_Smoke) { { FillRandFile(tmpFile, 10 * 1024 + 527); - FileReader reader(tmpFile, FileReader::kDefaultLogPageSize, FileReader::kDefaultLogPageCount); + FileReader reader(tmpFile); vector<char> buffer; GetReverseForReaderAndTmpFile(reader, buffer); diff --git a/coding/coding_tests/writer_test.cpp b/coding/coding_tests/writer_test.cpp index c08afb79f1..7d5277f6c2 100644 --- a/coding/coding_tests/writer_test.cpp +++ b/coding/coding_tests/writer_test.cpp @@ -50,7 +50,7 @@ UNIT_TEST(FileWriter_Smoke) } vector<char> s; { - FileReader reader(fileName, FileReader::kDefaultLogPageSize, FileReader::kDefaultLogPageCount); + FileReader reader(fileName); s.resize(reader.Size()); reader.Read(0, &s[0], reader.Size()); } @@ -85,7 +85,7 @@ UNIT_TEST(SubWriter_FileWriter_Smoke) } vector<char> s; { - FileReader reader(fileName, FileReader::kDefaultLogPageSize, FileReader::kDefaultLogPageCount); + FileReader reader(fileName); s.resize(reader.Size()); reader.Read(0, &s[0], reader.Size()); } @@ -101,13 +101,13 @@ UNIT_TEST(FileWriter_DeleteFile) writer.Write("123", 3); } { - FileReader reader(fileName, FileReader::kDefaultLogPageSize, FileReader::kDefaultLogPageCount); + FileReader reader(fileName); TEST_EQUAL(reader.Size(), 3, ()); } FileWriter::DeleteFileX(fileName); try { - FileReader reader(fileName, FileReader::kDefaultLogPageSize, FileReader::kDefaultLogPageCount); + FileReader reader(fileName); TEST(false, ("Exception should be thrown!")); } catch (FileReader::OpenException & ) @@ -127,7 +127,7 @@ UNIT_TEST(FileWriter_AppendAndOpenExisting) writer.Write("abcd", 4); } { - FileReader reader(fileName, FileReader::kDefaultLogPageSize, FileReader::kDefaultLogPageCount); + FileReader reader(fileName); TEST_EQUAL(reader.Size(), 4, ()); string s(static_cast<uint32_t>(reader.Size()), 0); reader.Read(0, &s[0], s.size()); @@ -138,7 +138,7 @@ UNIT_TEST(FileWriter_AppendAndOpenExisting) writer.Write("123", 3); } { - FileReader reader(fileName, FileReader::kDefaultLogPageSize, FileReader::kDefaultLogPageCount); + FileReader reader(fileName); TEST_EQUAL(reader.Size(), 3, ()); } { @@ -146,7 +146,7 @@ UNIT_TEST(FileWriter_AppendAndOpenExisting) writer.Write("4", 1); } { - FileReader reader(fileName, FileReader::kDefaultLogPageSize, FileReader::kDefaultLogPageCount); + FileReader reader(fileName); TEST_EQUAL(reader.Size(), 4, ()); string s(static_cast<uint32_t>(reader.Size()), 0); reader.Read(0, &s[0], s.size()); @@ -158,7 +158,7 @@ UNIT_TEST(FileWriter_AppendAndOpenExisting) writer.Write("56", 2); } { - FileReader reader(fileName, FileReader::kDefaultLogPageSize, FileReader::kDefaultLogPageCount); + FileReader reader(fileName); TEST_EQUAL(reader.Size(), 4, ()); string s(static_cast<uint32_t>(reader.Size()), 0); reader.Read(0, &s[0], 4); @@ -219,7 +219,7 @@ UNIT_TEST(FileWriter_Chunks) WriteTestData2(fileWriter); } { - FileReader r(TEST_FILE, FileReader::kDefaultLogPageSize, FileReader::kDefaultLogPageCount); + FileReader r(TEST_FILE); ReadTestData(r); } FileWriter::DeleteFileX(TEST_FILE); diff --git a/coding/coding_tests/zip_creator_test.cpp b/coding/coding_tests/zip_creator_test.cpp index 2c3592731c..781f6ff9f4 100644 --- a/coding/coding_tests/zip_creator_test.cpp +++ b/coding/coding_tests/zip_creator_test.cpp @@ -19,10 +19,7 @@ void CreateAndTestZip(string const & filePath, string const & zipPath) ZipFileReader::FileListT files; ZipFileReader::FilesList(zipPath, files); - TEST_EQUAL(files[0].second, - FileReader(filePath, FileReader::kDefaultLogPageSize, FileReader::kDefaultLogPageCount) - .Size(), - ()); + TEST_EQUAL(files[0].second, FileReader(filePath).Size(), ()); string const unzippedFile = "unzipped.tmp"; ZipFileReader::UnzipFile(zipPath, files[0].first, unzippedFile); diff --git a/coding/coding_tests/zip_reader_test.cpp b/coding/coding_tests/zip_reader_test.cpp index e7ac65d061..bb8781dfc7 100644 --- a/coding/coding_tests/zip_reader_test.cpp +++ b/coding/coding_tests/zip_reader_test.cpp @@ -175,15 +175,13 @@ UNIT_TEST(ZipExtract) string s; ZipFileReader::UnzipFile(ZIPFILE, files[0].first, OUTFILE); { - FileReader(OUTFILE, FileReader::kDefaultLogPageSize, FileReader::kDefaultLogPageCount) - .ReadAsString(s); + FileReader(OUTFILE).ReadAsString(s); } TEST_EQUAL(s, "aaaaaaaaaa\x0A", ()); // OUTFILE should be rewritten correctly in the next lines ZipFileReader::UnzipFile(ZIPFILE, files[1].first, OUTFILE); { - FileReader(OUTFILE, FileReader::kDefaultLogPageSize, FileReader::kDefaultLogPageCount) - .ReadAsString(s); + FileReader(OUTFILE).ReadAsString(s); } TEST_EQUAL(s, "Holalala\x0A", ()); FileWriter::DeleteFileX(OUTFILE); diff --git a/coding/file_container.cpp b/coding/file_container.cpp index 55845d6834..69d4a698f7 100644 --- a/coding/file_container.cpp +++ b/coding/file_container.cpp @@ -211,7 +211,7 @@ FilesMappingContainer::~FilesMappingContainer() void FilesMappingContainer::Open(string const & fName) { { - FileReader reader(fName, FileReader::kDefaultLogPageSize, FileReader::kDefaultLogPageCount); + FileReader reader(fName); ReadInfo(reader); } @@ -242,8 +242,7 @@ FileReader FilesMappingContainer::GetReader(Tag const & tag) const Info const * p = GetInfo(tag); if (!p) MYTHROW(Reader::OpenException, ("Can't find section:", m_name, tag)); - return FileReader(m_name, FileReader::kDefaultLogPageSize, FileReader::kDefaultLogPageCount) - .SubReader(p->m_offset, p->m_size); + return FileReader(m_name).SubReader(p->m_offset, p->m_size); } ///////////////////////////////////////////////////////////////////////////// @@ -308,7 +307,7 @@ void FilesContainerW::Open(FileWriter::Op op) case FileWriter::OP_WRITE_EXISTING: { // read an existing service info - FileReader reader(m_name, FileReader::kDefaultLogPageSize, FileReader::kDefaultLogPageCount); + FileReader reader(m_name); ReadInfo(reader); } @@ -351,8 +350,7 @@ FilesContainerW::~FilesContainerW() uint64_t FilesContainerW::SaveCurrentSize() { ASSERT(!m_bFinished, ()); - uint64_t const curr = - FileReader(m_name, FileReader::kDefaultLogPageSize, FileReader::kDefaultLogPageCount).Size(); + uint64_t const curr = FileReader(m_name).Size(); if (!m_info.empty()) m_info.back().m_size = curr - m_info.back().m_offset; return curr; @@ -428,9 +426,7 @@ FileWriter FilesContainerW::GetWriter(Tag const & tag) void FilesContainerW::Write(string const & fPath, Tag const & tag) { - Write(ModelReaderPtr(make_unique<FileReader>(fPath, FileReader::kDefaultLogPageSize, - FileReader::kDefaultLogPageCount)), - tag); + Write(ModelReaderPtr(make_unique<FileReader>(fPath)), tag); } void FilesContainerW::Write(ModelReaderPtr reader, Tag const & tag) diff --git a/coding/file_reader.cpp b/coding/file_reader.cpp index 3980d39703..3b2fd0ff4d 100644 --- a/coding/file_reader.cpp +++ b/coding/file_reader.cpp @@ -15,6 +15,9 @@ using namespace std; namespace { +uint32_t const kDefaultLogPageSize = 10; +uint32_t const kDefaultLogPageCount = 4; + class FileDataWithCachedSize : public my::FileData { using base_t = my::FileData; @@ -32,11 +35,6 @@ private: }; } // namespace -// static -const uint32_t FileReader::kDefaultLogPageSize = 10; -// static -const uint32_t FileReader::kDefaultLogPageCount = 4; - class FileReader::FileReaderData { public: @@ -78,6 +76,11 @@ private: #endif }; +FileReader::FileReader(std::string const & fileName) + : FileReader(fileName, kDefaultLogPageSize, kDefaultLogPageCount) +{ +} + FileReader::FileReader(string const & fileName, uint32_t logPageSize, uint32_t logPageCount) : BaseType(fileName) , m_logPageSize(logPageSize) @@ -85,7 +88,8 @@ FileReader::FileReader(string const & fileName, uint32_t logPageSize, uint32_t l , m_fileData(new FileReaderData(fileName, logPageSize, logPageCount)) , m_offset(0) , m_size(m_fileData->Size()) -{} +{ +} FileReader::FileReader(FileReader const & reader, uint64_t offset, uint64_t size, uint32_t logPageSize, uint32_t logPageCount) @@ -95,7 +99,8 @@ FileReader::FileReader(FileReader const & reader, uint64_t offset, uint64_t size , m_fileData(reader.m_fileData) , m_offset(offset) , m_size(size) -{} +{ +} void FileReader::Read(uint64_t pos, void * p, size_t size) const { diff --git a/coding/file_reader.hpp b/coding/file_reader.hpp index 9e1cf4781c..0a7711b1d2 100644 --- a/coding/file_reader.hpp +++ b/coding/file_reader.hpp @@ -15,10 +15,8 @@ class FileReader : public ModelReader { public: - static const uint32_t kDefaultLogPageSize; - static const uint32_t kDefaultLogPageCount; - - explicit FileReader(std::string const & fileName, uint32_t logPageSize, uint32_t logPageCount); + explicit FileReader(std::string const & fileName); + FileReader(std::string const & fileName, uint32_t logPageSize, uint32_t logPageCount); class FileReaderData; diff --git a/coding/file_sort.hpp b/coding/file_sort.hpp index 237c692f9f..fd7b8a17e8 100644 --- a/coding/file_sort.hpp +++ b/coding/file_sort.hpp @@ -63,8 +63,7 @@ public: // Write output. { m_pTmpWriter.reset(); - FileReader reader(m_TmpFileName, FileReader::kDefaultLogPageSize, - FileReader::kDefaultLogPageCount); + FileReader reader(m_TmpFileName); ItemIndexPairGreater fGreater(m_Less); PriorityQueueType q(fGreater); for (uint32_t i = 0; i < m_ItemCount; i += m_BufferCapacity) |