From 1ff7d0bf385e8338b21ae869a90fe261d9f297a9 Mon Sep 17 00:00:00 2001 From: Maxim Pimenov Date: Tue, 25 Jul 2017 19:11:18 +0300 Subject: [PATCH] [generator] Stubs for incremental updates. --- generator/CMakeLists.txt | 2 + generator/mwm_diff/CMakeLists.txt | 12 ++++ generator/mwm_diff/diff.cpp | 56 +++++++++++++++++++ generator/mwm_diff/diff.hpp | 24 ++++++++ .../mwm_diff/mwm_diff_tests/CMakeLists.txt | 51 +++++++++++++++++ .../mwm_diff/mwm_diff_tests/diff_test.cpp | 45 +++++++++++++++ generator/mwm_diff/pymwm_diff/CMakeLists.txt | 45 +++++++++++++++ generator/mwm_diff/pymwm_diff/bindings.cpp | 13 +++++ 8 files changed, 248 insertions(+) create mode 100644 generator/mwm_diff/CMakeLists.txt create mode 100644 generator/mwm_diff/diff.cpp create mode 100644 generator/mwm_diff/diff.hpp create mode 100644 generator/mwm_diff/mwm_diff_tests/CMakeLists.txt create mode 100644 generator/mwm_diff/mwm_diff_tests/diff_test.cpp create mode 100644 generator/mwm_diff/pymwm_diff/CMakeLists.txt create mode 100644 generator/mwm_diff/pymwm_diff/bindings.cpp diff --git a/generator/CMakeLists.txt b/generator/CMakeLists.txt index 25e40ab262..2f63649c6a 100644 --- a/generator/CMakeLists.txt +++ b/generator/CMakeLists.txt @@ -108,6 +108,8 @@ add_library(${PROJECT_NAME} ${SRC}) omim_add_test_subdirectory(generator_tests_support) omim_add_test_subdirectory(generator_tests) +add_subdirectory(mwm_diff) + if (NOT SKIP_GTOOL) add_subdirectory(generator_tool) endif() diff --git a/generator/mwm_diff/CMakeLists.txt b/generator/mwm_diff/CMakeLists.txt new file mode 100644 index 0000000000..9754266ff7 --- /dev/null +++ b/generator/mwm_diff/CMakeLists.txt @@ -0,0 +1,12 @@ +project(mwm_diff) + +set( + SRC + diff.cpp + diff.hpp +) + +add_library(${PROJECT_NAME} ${SRC}) + +omim_add_pybindings_subdirectory(pymwm_diff) +omim_add_test_subdirectory(mwm_diff_tests) diff --git a/generator/mwm_diff/diff.cpp b/generator/mwm_diff/diff.cpp new file mode 100644 index 0000000000..975bc27bad --- /dev/null +++ b/generator/mwm_diff/diff.cpp @@ -0,0 +1,56 @@ +#include "generator/mwm_diff/diff.hpp" + +#include "coding/file_reader.hpp" +#include "coding/file_writer.hpp" +#include "coding/read_write_utils.hpp" +#include "coding/reader.hpp" + +#include "base/logging.hpp" + +using namespace std; + +namespace +{ +// We could use my::CopyFileX but it fails if the file at |srcFile| is empty. +bool CopyFile(string const & srcFile, string const & dstFile) +{ + size_t constexpr kBufferSize = 1024 * 1024; + + try + { + FileReader reader(srcFile, true /* withExceptions */); + ReaderSource src(reader); + FileWriter writer(dstFile); + + rw::ReadAndWrite(src, writer, kBufferSize); + } + catch (Reader::Exception const & e) + { + LOG(LERROR, ("Could not read from", srcFile, ":", e.Msg())); + return false; + } + catch (Writer::Exception const & e) + { + LOG(LERROR, ("Could not write to", dstFile, ":", e.Msg())); + return false; + } + + return true; +} +} // namespace + +namespace generator +{ +namespace mwm_diff +{ +bool MakeDiff(string const & /* oldMwmPath */, string const & newMwmPath, string const & diffPath) +{ + return CopyFile(newMwmPath, diffPath); +} + +bool ApplyDiff(string const & /* oldMwmPath */, string const & newMwmPath, string const & diffPath) +{ + return CopyFile(diffPath, newMwmPath); +} +} // namespace mwm_diff +} // namespace generator diff --git a/generator/mwm_diff/diff.hpp b/generator/mwm_diff/diff.hpp new file mode 100644 index 0000000000..28ceebf544 --- /dev/null +++ b/generator/mwm_diff/diff.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include + +namespace generator +{ +namespace mwm_diff +{ +// Makes a diff that, when applied to the mwm at |oldMwmPath|, will +// result in the mwm at |newMwmPath|. The diff is stored at |diffPath|. +// It is assumed that the files at |oldMwmPath| and |newMwmPath| are valid mwms. +// Returns true on success and false on failure. +bool MakeDiff(std::string const & /* oldMwmPath */, std::string const & newMwmPath, + std::string const & diffPath); + +// Applies the diff at |diffPath| to the mwm at |oldMwmPath|. The resulting +// mwm is stored at |newMwmPath|. +// It is assumed that the file at |oldMwmPath| is a valid mwm and the file +// at |diffPath| is a valid mwmdiff. +// Returns true on success and false on failure. +bool ApplyDiff(std::string const & /* oldMwmPath */, std::string const & newMwmPath, + std::string const & diffPath); +} // namespace mwm_diff +} // namespace generator diff --git a/generator/mwm_diff/mwm_diff_tests/CMakeLists.txt b/generator/mwm_diff/mwm_diff_tests/CMakeLists.txt new file mode 100644 index 0000000000..e030344251 --- /dev/null +++ b/generator/mwm_diff/mwm_diff_tests/CMakeLists.txt @@ -0,0 +1,51 @@ +project(mwm_diff_tests) + +set( + SRC + diff_test.cpp +) + +omim_add_test(${PROJECT_NAME} ${SRC}) + +omim_link_libraries( + ${PROJECT_NAME} + generator_tests_support + platform_tests_support + mwm_diff + generator + drape_frontend + map + routing + search + storage + indexer + drape + traffic + routing_common + editor + platform + geometry + coding + base + freetype + expat + icu + jansson + protobuf + osrm + stats_client + minizip + succinct + pugixml + tess2 + gflags + oauthcpp + opening_hours + stb_image + sdf_image + # TODO(syershov): Use FindPackage. + sqlite3 + ${LIBZ} +) + +link_qt5_core(${PROJECT_NAME}) diff --git a/generator/mwm_diff/mwm_diff_tests/diff_test.cpp b/generator/mwm_diff/mwm_diff_tests/diff_test.cpp new file mode 100644 index 0000000000..034b46e26f --- /dev/null +++ b/generator/mwm_diff/mwm_diff_tests/diff_test.cpp @@ -0,0 +1,45 @@ +#include "testing/testing.hpp" + +#include "generator/mwm_diff/diff.hpp" + +#include "platform/platform.hpp" + +#include "coding/file_name_utils.hpp" +#include "coding/file_writer.hpp" +#include "coding/internal/file_data.hpp" + +#include "base/scope_guard.hpp" + +using namespace std; + +namespace generator +{ +namespace mwm_diff +{ +UNIT_TEST(IncrementalUpdates_Smoke) +{ + string const oldMwmPath = my::JoinFoldersToPath(GetPlatform().WritableDir(), "minsk-pass.mwm"); + string const newMwmPath1 = + my::JoinFoldersToPath(GetPlatform().WritableDir(), "minsk-pass-new1.mwm"); + string const newMwmPath2 = + my::JoinFoldersToPath(GetPlatform().WritableDir(), "minsk-pass-new2.mwm"); + string const diffPath = my::JoinFoldersToPath(GetPlatform().WritableDir(), "minsk-pass.mwmdiff"); + + MY_SCOPE_GUARD(cleanup, [&] { + FileWriter::DeleteFileX(newMwmPath1); + FileWriter::DeleteFileX(newMwmPath2); + FileWriter::DeleteFileX(diffPath); + }); + + { + // Create an empty file. + FileWriter writer(newMwmPath1); + } + + TEST(MakeDiff(oldMwmPath, newMwmPath1, diffPath), ()); + TEST(ApplyDiff(oldMwmPath, newMwmPath2, diffPath), ()); + + TEST(my::IsEqualFiles(newMwmPath1, newMwmPath2), ()); +} +} // namespace mwm_diff +} // namespace generator diff --git a/generator/mwm_diff/pymwm_diff/CMakeLists.txt b/generator/mwm_diff/pymwm_diff/CMakeLists.txt new file mode 100644 index 0000000000..65bc82f8c3 --- /dev/null +++ b/generator/mwm_diff/pymwm_diff/CMakeLists.txt @@ -0,0 +1,45 @@ +project(pymwm_diff) + +set( + SRC + bindings.cpp +) + +add_library(${PROJECT_NAME} MODULE ${SRC}) + +if (PLATFORM_MAC) + omim_link_libraries( + ${PROJECT_NAME} + ${Qt5Widgets_LIBRARIES} + ) +endif() + +if (PLATFORM_WIN OR PLATFORM_LINUX) + omim_link_libraries( + ${PROJECT_NAME} + ${Qt5Widgets_LIBRARIES} + ) +endif() + +omim_link_libraries( + ${PROJECT_NAME} + mwm_diff + indexer + editor + platform + geometry + coding + base + stats_client + jansson + oauthcpp + protobuf + pugixml + opening_hours + icu + ${PYTHON_LIBRARIES} + ${Boost_LIBRARIES} + ${LIBZ} +) + +set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "") diff --git a/generator/mwm_diff/pymwm_diff/bindings.cpp b/generator/mwm_diff/pymwm_diff/bindings.cpp new file mode 100644 index 0000000000..4302e2a0e9 --- /dev/null +++ b/generator/mwm_diff/pymwm_diff/bindings.cpp @@ -0,0 +1,13 @@ +#include "generator/mwm_diff/diff.hpp" + +#include + +using namespace std; + +BOOST_PYTHON_MODULE(pymwm_diff) +{ + using namespace boost::python; + + def("make_diff", generator::mwm_diff::MakeDiff); + def("apply_diff", generator::mwm_diff::ApplyDiff); +}