From 8c55d7bfe8f22082e7665047e726ccae97e9b562 Mon Sep 17 00:00:00 2001 From: Tim Teulings Date: Sat, 28 Sep 2024 18:15:24 +0200 Subject: [PATCH 1/3] feat: #1606 Better support for .FileFormatVersion Added new methods to TypeConfig and Database to check for file format version of Database and Library. --- libosmscout/include/osmscout/TypeConfig.h | 3 +++ libosmscout/include/osmscout/db/Database.h | 3 +++ libosmscout/src/osmscout/TypeConfig.cpp | 29 ++++++++++++++++++++++ libosmscout/src/osmscout/db/Database.cpp | 9 +++++++ 4 files changed, 44 insertions(+) diff --git a/libosmscout/include/osmscout/TypeConfig.h b/libosmscout/include/osmscout/TypeConfig.h index fa7080328..6d5311ba5 100644 --- a/libosmscout/include/osmscout/TypeConfig.h +++ b/libosmscout/include/osmscout/TypeConfig.h @@ -1384,9 +1384,12 @@ namespace osmscout { * Methods for loading/storing of type information from/to files. */ //@{ + static uint32_t GetDatabaseFileFormatVersion(const std::string& directory); + bool LoadFromOSTFile(const std::string& filename); bool LoadFromDataFile(const std::string& directory); bool StoreToDataFile(const std::string& directory) const; + //@} }; diff --git a/libosmscout/include/osmscout/db/Database.h b/libosmscout/include/osmscout/db/Database.h index 35eaedf84..fdd52bcaf 100644 --- a/libosmscout/include/osmscout/db/Database.h +++ b/libosmscout/include/osmscout/db/Database.h @@ -371,6 +371,9 @@ namespace osmscout { explicit Database(const DatabaseParameter& parameter); ~Database(); + static uint32_t GetDatabaseFileFormatVersion(const std::string& path); + static uint32_t GetLibraryFileFormatVersion(); + bool Open(const std::string& path); bool IsOpen() const; void Close(); diff --git a/libosmscout/src/osmscout/TypeConfig.cpp b/libosmscout/src/osmscout/TypeConfig.cpp index be0cc42e6..972da3a6b 100644 --- a/libosmscout/src/osmscout/TypeConfig.cpp +++ b/libosmscout/src/osmscout/TypeConfig.cpp @@ -1196,6 +1196,35 @@ return success; } + /** + * Returns the file format version of the given database (scanning the + * "types.dat" file in the given directory) or an IOException. + * + * @param directory + * @return + */ + uint32_t TypeConfig::GetDatabaseFileFormatVersion(const std::string& directory) + { + FileScanner scanner; + + try { + scanner.Open(AppendFileToDir(directory, + "types.dat"), + FileScanner::Sequential, + true); + + uint32_t fileFormatVersion=scanner.ReadUInt32(); + + scanner.Close(); + + return fileFormatVersion; + } + catch (const IOException& e) { + scanner.CloseFailsafe(); + throw e; + } + } + /** * Loads the type configuration from the given binary data file. * diff --git a/libosmscout/src/osmscout/db/Database.cpp b/libosmscout/src/osmscout/db/Database.cpp index e485ffd14..0f71bdd50 100644 --- a/libosmscout/src/osmscout/db/Database.cpp +++ b/libosmscout/src/osmscout/db/Database.cpp @@ -192,6 +192,15 @@ namespace osmscout { } } + uint32_t Database::GetDatabaseFileFormatVersion(const std::string& path) + { + return TypeConfig::GetDatabaseFileFormatVersion(path); + } + uint32_t Database::GetLibraryFileFormatVersion() + { + return FILE_FORMAT_VERSION; + } + bool Database::Open(const std::string& path) { assert(!path.empty()); From 842de50a5e8e6a11f97639daec9798d69fb87ae1 Mon Sep 17 00:00:00 2001 From: Tim Teulings Date: Sun, 29 Sep 2024 13:52:19 +0200 Subject: [PATCH 2/3] feat: #1606 Better support for FileFormatVersion Added tests --- Tests/CMakeLists.txt | 4 +++ Tests/meson.build | 12 +++++++++ Tests/src/FileFormatVersion.cpp | 48 +++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 Tests/src/FileFormatVersion.cpp diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 30bbf2103..be12e9d29 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -34,6 +34,10 @@ osmscout_test_project(NAME ColorParse SOURCES src/ColorParse.cpp) #---- CoordinateEncoding osmscout_test_project(NAME CoordinateEncoding SOURCES src/CoordinateEncoding.cpp COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/data/testregion") +#---- FileFormatVersion +osmscout_test_project(NAME FileFormatVersion SOURCES src/FileFormatVersion.cpp) +set_tests_properties(FileFormatVersion PROPERTIES ENVIRONMENT "TESTS_TOP_DIR=${CMAKE_CURRENT_SOURCE_DIR}") + #---- Latch osmscout_test_project(NAME Latch SOURCES src/Latch.cpp) diff --git a/Tests/meson.build b/Tests/meson.build index 624cf42af..c5cef2598 100644 --- a/Tests/meson.build +++ b/Tests/meson.build @@ -172,6 +172,18 @@ File = executable('File', test('Check File utilities', File) +FileFormatVersion = executable('FileFormatVersion', + 'src/FileFormatVersion.cpp', + include_directories: [testIncDir, osmscoutIncDir], + dependencies: [mathDep, openmpDep], + link_with: [osmscout], + install: true, + install_dir: testInstallDir) + +test('Check FileFormatVersion APIs', + FileFormatVersion, + env: ['TESTS_TOP_DIR='+meson.current_source_dir()]) + FileScannerWriter = executable('FileScannerWriter', 'src/FileScannerWriter.cpp', include_directories: [testIncDir, osmscoutIncDir], diff --git a/Tests/src/FileFormatVersion.cpp b/Tests/src/FileFormatVersion.cpp new file mode 100644 index 000000000..e9d8ce608 --- /dev/null +++ b/Tests/src/FileFormatVersion.cpp @@ -0,0 +1,48 @@ + +#include + +#include +#include + +#include + +static std::string GetTestDatabaseDirectory() +{ + char* testsTopDirEnv=::getenv("TESTS_TOP_DIR"); + + if (testsTopDirEnv==nullptr) { + throw osmscout::UninitializedException("Expected environment variable 'TESTS_TOP_DIR' not set"); + } + + std::string testsTopDir=testsTopDirEnv; + + if (testsTopDir.empty()) { + throw osmscout::UninitializedException("Environment variable 'TESTS_TOP_DIR' is empty"); + } + + if (!osmscout::IsDirectory(testsTopDir)) { + throw osmscout::UninitializedException("Environment variable 'TESTS_TOP_DIR' does not point to directory"); + } + + return std::filesystem::path(testsTopDir).append("data").append("testregion").native(); +} + +TEST_CASE("TypeConfig::GetFileFormatVersion(dir) throws exception on missing db") { + CHECK_THROWS_AS(osmscout::TypeConfig::GetDatabaseFileFormatVersion("does_not_exist"),osmscout::IOException); +} + +TEST_CASE("Database::GetFileFormatVersion(dir) throws exception on missing db") { + CHECK_THROWS_AS(osmscout::Database::GetDatabaseFileFormatVersion("does_not_exist"),osmscout::IOException); +} + +TEST_CASE("TypeConfig::GetFileFormatVersion(dir) returns current version for test database") { + REQUIRE(osmscout::TypeConfig::GetDatabaseFileFormatVersion(GetTestDatabaseDirectory()) == osmscout::FILE_FORMAT_VERSION); +} + +TEST_CASE("Database::GetFileFormatVersion(dir) returns current version for test database") { + REQUIRE(osmscout::Database::GetDatabaseFileFormatVersion(GetTestDatabaseDirectory()) == osmscout::FILE_FORMAT_VERSION); +} + +TEST_CASE("Database::GetLibraryFileFormatVersion() returns current version") { + REQUIRE(osmscout::Database::GetLibraryFileFormatVersion() == osmscout::FILE_FORMAT_VERSION); +} From 58e132fa033a1b87cd99d1faebcf3d8171aaf93d Mon Sep 17 00:00:00 2001 From: Tim Teulings Date: Sun, 29 Sep 2024 14:39:41 +0200 Subject: [PATCH 3/3] feat: #1606 Better support for FileFormatVersion Added tests --- Tests/src/FileFormatVersion.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/src/FileFormatVersion.cpp b/Tests/src/FileFormatVersion.cpp index e9d8ce608..f7a15bd96 100644 --- a/Tests/src/FileFormatVersion.cpp +++ b/Tests/src/FileFormatVersion.cpp @@ -24,7 +24,7 @@ static std::string GetTestDatabaseDirectory() throw osmscout::UninitializedException("Environment variable 'TESTS_TOP_DIR' does not point to directory"); } - return std::filesystem::path(testsTopDir).append("data").append("testregion").native(); + return std::filesystem::path(testsTopDir).append("data").append("testregion").string(); } TEST_CASE("TypeConfig::GetFileFormatVersion(dir) throws exception on missing db") {