Skip to content

Commit

Permalink
Merge pull request #1607 from Framstag/#1606-better-support-file-form…
Browse files Browse the repository at this point in the history
…at-version

feat: #1606 Better support for FileFormatVersion
  • Loading branch information
Framstag authored Sep 29, 2024
2 parents 1f95d83 + 58e132f commit af2408d
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 12 additions & 0 deletions Tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
48 changes: 48 additions & 0 deletions Tests/src/FileFormatVersion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

#include <filesystem>

#include <osmscout/db/Database.h>
#include <osmscout/TypeConfig.h>

#include <TestMain.h>

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").string();
}

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);
}
3 changes: 3 additions & 0 deletions libosmscout/include/osmscout/TypeConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

//@}
};

Expand Down
3 changes: 3 additions & 0 deletions libosmscout/include/osmscout/db/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
29 changes: 29 additions & 0 deletions libosmscout/src/osmscout/TypeConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
9 changes: 9 additions & 0 deletions libosmscout/src/osmscout/db/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit af2408d

Please sign in to comment.