Skip to content

Commit

Permalink
Merge pull request #702 from wildmeshing/dzint/import_export_cache_co…
Browse files Browse the repository at this point in the history
…mponents

Dzint/import export cache components
  • Loading branch information
mtao authored Jan 21, 2024
2 parents 94cd568 + 1acdcbf commit 564492e
Show file tree
Hide file tree
Showing 14 changed files with 247 additions and 0 deletions.
2 changes: 2 additions & 0 deletions components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ set(json_components "")
# list of all components
add_component(${WMTK_COMPONENT_PREFIX} "input")
add_component(${WMTK_COMPONENT_PREFIX} "output")
add_component(${WMTK_COMPONENT_PREFIX} "export_cache")
add_component(${WMTK_COMPONENT_PREFIX} "import_cache")
add_component(${WMTK_COMPONENT_PREFIX} "delaunay")
add_component(${WMTK_COMPONENT_PREFIX} "isotropic_remeshing")
add_component(${WMTK_COMPONENT_PREFIX} "marching")
Expand Down
37 changes: 37 additions & 0 deletions components/tests/test_component_export_cache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <catch2/catch_test_macros.hpp>
#include <nlohmann/json.hpp>
#include <wmtk/components/base/Paths.hpp>
#include <wmtk/components/export_cache/export_cache.hpp>
#include <wmtk/components/input/input.hpp>

using namespace wmtk::components::base;
using json = nlohmann::json;

const std::filesystem::path data_dir = WMTK_DATA_DIR;

TEST_CASE("component_export_cache", "[components][export_cache]")
{
wmtk::io::Cache cache("wmtk_cache", ".");

// input
{
const std::filesystem::path input_file = data_dir / "small.msh";
json component_json = {
{"name", "input_mesh"},
{"file", input_file.string()},
{"ignore_z", false}};


CHECK_NOTHROW(wmtk::components::input(Paths(), component_json, cache));
}

wmtk::io::Cache cache_dump("wmtk_dump", ".");

// export cache
{
json o;
o["folder"] = cache_dump.get_cache_path() / "exported_cache";

CHECK_NOTHROW(wmtk::components::export_cache(Paths(), o, cache));
}
}
49 changes: 49 additions & 0 deletions components/tests/test_component_import_cache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <catch2/catch_test_macros.hpp>
#include <nlohmann/json.hpp>
#include <wmtk/components/base/Paths.hpp>
#include <wmtk/components/export_cache/export_cache.hpp>
#include <wmtk/components/import_cache/import_cache.hpp>
#include <wmtk/components/input/input.hpp>

using namespace wmtk::components::base;
using json = nlohmann::json;

const std::filesystem::path data_dir = WMTK_DATA_DIR;

TEST_CASE("component_import_cache", "[components][import_cache]")
{
wmtk::io::Cache cache("wmtk_cache", ".");

// input
{
const std::filesystem::path input_file = data_dir / "small.msh";
json component_json = {
{"name", "input_mesh"},
{"file", input_file.string()},
{"ignore_z", false}};


CHECK_NOTHROW(wmtk::components::input(Paths(), component_json, cache));
}

wmtk::io::Cache cache_dump("wmtk_dump", ".");

// export cache
{
json o;
o["folder"] = cache_dump.get_cache_path() / "exported_cache";

CHECK_NOTHROW(wmtk::components::export_cache(Paths(), o, cache));
}

wmtk::io::Cache cache2("wmtk_cache_from_import", ".");
// import cache
{
json o;
o["folder"] = cache_dump.get_cache_path() / "exported_cache";

CHECK_NOTHROW(wmtk::components::import_cache(Paths(), o, cache2));
}

CHECK(cache.equals(cache2));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

set(SRC_FILES
ExportCacheOptions.hpp
export_cache.cpp
export_cache.hpp)

#CURRENT_COMPONENT_LIB_NAME is set from the main cmake
target_sources(${CURRENT_COMPONENT_LIB_NAME} PRIVATE ${SRC_FILES})

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <wmtk/components/base/json_utils.hpp>

#include <nlohmann/json.hpp>

namespace wmtk {
namespace components {
namespace internal {

struct ExportCacheOptions
{
std::filesystem::path folder;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ExportCacheOptions, folder);

} // namespace internal
} // namespace components
} // namespace wmtk
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "export_cache.hpp"

#include <wmtk/components/base/resolve_path.hpp>
#include <wmtk/utils/Logger.hpp>

#include "ExportCacheOptions.hpp"

namespace wmtk::components {

void export_cache(const base::Paths& paths, const nlohmann::json& j, io::Cache& cache)
{
using namespace internal;

ExportCacheOptions options = j.get<ExportCacheOptions>();

std::string export_location =
wmtk::components::base::resolve_path(options.folder.string(), paths.root_path);

if (std::filesystem::exists(export_location)) {
log_and_throw_error("Cannot export cache, folder {} already exists", export_location);
}

if (!cache.export_cache(export_location)) {
log_and_throw_error("Could not export cache from {}", export_location);
}
}
} // namespace wmtk::components
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <nlohmann/json.hpp>
#include <wmtk/io/Cache.hpp>

#include <wmtk/components/base/Paths.hpp>

namespace wmtk::components {

void export_cache(const base::Paths& paths, const nlohmann::json& j, io::Cache& cache);

} // namespace wmtk::components
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"pointer": "/",
"type": "object",
"required": ["folder"]
},
{
"pointer": "/folder",
"type": "string"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

set(SRC_FILES
ImportCacheOptions.hpp
import_cache.cpp
import_cache.hpp)

#CURRENT_COMPONENT_LIB_NAME is set from the main cmake
target_sources(${CURRENT_COMPONENT_LIB_NAME} PRIVATE ${SRC_FILES})

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <wmtk/components/base/json_utils.hpp>

#include <nlohmann/json.hpp>

namespace wmtk {
namespace components {
namespace internal {

struct ImportCacheOptions
{
std::filesystem::path folder;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ImportCacheOptions, folder);

} // namespace internal
} // namespace components
} // namespace wmtk
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "import_cache.hpp"

#include <wmtk/components/base/resolve_path.hpp>
#include <wmtk/utils/Logger.hpp>

#include "ImportCacheOptions.hpp"

namespace wmtk::components {

void import_cache(const base::Paths& paths, const nlohmann::json& j, io::Cache& cache)
{
using namespace internal;

ImportCacheOptions options = j.get<ImportCacheOptions>();

std::string import_location =
wmtk::components::base::resolve_path(options.folder.string(), paths.root_path);

if (!std::filesystem::exists(import_location)) {
log_and_throw_error("Cannot import cache, folder {} does not exist", import_location);
}

if (!cache.import_cache(import_location)) {
log_and_throw_error("Could not import cache from {}", import_location);
}
}
} // namespace wmtk::components
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <nlohmann/json.hpp>
#include <wmtk/io/Cache.hpp>

#include <wmtk/components/base/Paths.hpp>

namespace wmtk::components {

void import_cache(const base::Paths& paths, const nlohmann::json& j, io::Cache& cache);

} // namespace wmtk::components
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"pointer": "/",
"type": "object",
"required": ["folder"]
},
{
"pointer": "/folder",
"type": "string"
}
]
1 change: 1 addition & 0 deletions src/wmtk/io/Cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ bool Cache::export_cache(const std::filesystem::path& export_location)

// delete json
fs::remove(cache_content_path);
m_file_paths.erase(m_cache_content_name);

return true;
}
Expand Down

0 comments on commit 564492e

Please sign in to comment.