Wildmeshing Toolkit
|
#include <Cache.hpp>
Public Member Functions | |
Cache (const std::string &prefix, const std::filesystem::path directory="", bool delete_cache=true) | |
This class creates and maintains a cache folder for storing temporary data. More... | |
Cache (Cache &&) | |
Cache & | operator= (Cache &&) |
~Cache () | |
const std::filesystem::path & | create_unique_file (const std::string &filename, const std::string &extension, size_t max_tries=10000) |
Create a file with the given name in the cache without overwriting any file with the same name. More... | |
const std::filesystem::path & | get_file_path (const std::string &filename) |
Get the path where the file with the given name is stored. More... | |
std::filesystem::path | get_file_path (const std::string &filename) const |
Get the path where the file with the given name is stored. More... | |
std::filesystem::path | get_cache_path () const |
Get the path of the cache folder. More... | |
std::shared_ptr< Mesh > | read_mesh (const std::string &name) const |
Load a mesh from cache. More... | |
void | load_multimesh (const std::string &name) const |
void | write_mesh (const Mesh &m, const std::string &name, const std::map< std::string, std::vector< int64_t >> &multimesh_names={}) |
Write a mesh to cache. More... | |
bool | export_cache (const std::filesystem::path &export_location) |
Export the cache to the given location. More... | |
bool | import_cache (const std::filesystem::path &import_location) |
Import a cache from the given location. More... | |
std::vector< int64_t > | absolute_multi_mesh_id (const std::string &name) const |
bool | equals (const Cache &o) |
Compare two caches for equality. More... | |
void | flush_multimeshes () |
Unsets the mesh held by each cached mm - useful for debugging whether cache loading works. More... | |
std::vector< std::string > | mesh_names () |
Get all names of the meshes stored in cache. More... | |
Static Public Member Functions | |
static std::filesystem::path | create_unique_directory (const std::string &prefix, const std::filesystem::path &location="", size_t max_tries=10000) |
Create a unique directory in the given location. More... | |
Private Attributes | |
std::filesystem::path | m_cache_dir |
std::map< std::string, std::filesystem::path > | m_file_paths |
std::map< std::string, CachedMultiMesh > | m_multimeshes |
bool | m_delete_cache = true |
Static Private Attributes | |
static const std::string | m_cache_content_name |
wmtk::io::Cache::Cache | ( | const std::string & | prefix, |
const std::filesystem::path | directory = "" , |
||
bool | delete_cache = true |
||
) |
This class creates and maintains a cache folder for storing temporary data.
The folder is placed under the path given in directory
and starts with the prefix
. The remainder of the cache name is a hex number representing a timestamp and another one representing the number of tries that were necessary to create the cache folder.
The cache folder is automatically removed in the destructor of the class if delete_cache is set to be true. If the contents of the cache should be copied out, one can use export_cache()
. Once a cache is exported, it can be imported again using import_cache()
.
Definition at line 72 of file Cache.cpp.
References create_unique_directory(), and m_cache_dir.
wmtk::io::Cache::~Cache | ( | ) |
Definition at line 79 of file Cache.cpp.
References wmtk::logger(), m_cache_dir, and m_delete_cache.
std::vector< int64_t > wmtk::io::Cache::absolute_multi_mesh_id | ( | const std::string & | name | ) | const |
Definition at line 150 of file Cache.cpp.
References m_multimeshes.
|
static |
Create a unique directory in the given location.
The directory will consist of the given prefix, a timestamp in nanoseconds convertex to hex, and a counter that is increased each time the directory generation fails.
If the given location is empty, use the system tmp directory.
prefix | A prefix for the directory name. |
location | The location where the new directory will be created. |
max_tries | The maximum number of tries before the function throws a runtime error. |
Definition at line 51 of file Cache.cpp.
References nanoseconds_timestamp(), and number_to_hex().
Referenced by Cache().
const std::filesystem::path & wmtk::io::Cache::create_unique_file | ( | const std::string & | filename, |
const std::string & | extension, | ||
size_t | max_tries = 10000 |
||
) |
Create a file with the given name in the cache without overwriting any file with the same name.
The file path is stored internally and can be accessed using get_file_path
name | The file name. |
Definition at line 94 of file Cache.cpp.
References m_cache_dir, m_file_paths, nanoseconds_timestamp(), and number_to_hex().
Referenced by export_cache(), get_file_path(), and write_mesh().
bool wmtk::io::Cache::equals | ( | const Cache & | o | ) |
Compare two caches for equality.
Only compares meshes registered in the cache.
Definition at line 311 of file Cache.cpp.
References wmtk::logger(), m_file_paths, and wmtk::read_mesh().
Referenced by TEST_CASE().
bool wmtk::io::Cache::export_cache | ( | const std::filesystem::path & | export_location | ) |
Export the cache to the given location.
The location must be a non-existing path. Along with all files, a json file is written that contains the dictionary of names used inside the program and the actual file names relative to the cache folder.
returns true if export was successful, false otherwise
Definition at line 232 of file Cache.cpp.
References create_unique_file(), m_cache_content_name, m_cache_dir, and m_file_paths.
Referenced by wmtk::components::export_cache().
void wmtk::io::Cache::flush_multimeshes | ( | ) |
Unsets the mesh held by each cached mm - useful for debugging whether cache loading works.
Definition at line 177 of file Cache.cpp.
References m_multimeshes.
Referenced by wmtk::components::run_components().
std::filesystem::path wmtk::io::Cache::get_cache_path | ( | ) | const |
Get the path of the cache folder.
Definition at line 146 of file Cache.cpp.
References m_cache_dir.
Referenced by TEST_CASE().
const std::filesystem::path & wmtk::io::Cache::get_file_path | ( | const std::string & | filename | ) |
Get the path where the file with the given name is stored.
If a file with this name does not exist yet, it is created using create_unique_file
.
Definition at line 122 of file Cache.cpp.
References create_unique_file(), and m_file_paths.
Referenced by load_multimesh().
std::filesystem::path wmtk::io::Cache::get_file_path | ( | const std::string & | filename | ) | const |
Get the path where the file with the given name is stored.
If a file with this name does not exist an error is thrown.
Definition at line 134 of file Cache.cpp.
References m_file_paths.
bool wmtk::io::Cache::import_cache | ( | const std::filesystem::path & | import_location | ) |
Import a cache from the given location.
The location must be cache folder that was previously generated using the export
function. The cache must be empty before importing.
returns true if import was successful, false otherwise
Definition at line 263 of file Cache.cpp.
References m_cache_content_name, m_cache_dir, and m_file_paths.
Referenced by wmtk::components::import_cache().
void wmtk::io::Cache::load_multimesh | ( | const std::string & | name | ) | const |
Definition at line 156 of file Cache.cpp.
References get_file_path(), and m_multimeshes.
Referenced by read_mesh().
std::vector< std::string > wmtk::io::Cache::mesh_names | ( | ) |
Get all names of the meshes stored in cache.
Definition at line 184 of file Cache.cpp.
References m_file_paths.
Definition at line 40 of file Cache.cpp.
References m_cache_dir, m_delete_cache, m_file_paths, and m_multimeshes.
std::shared_ptr< Mesh > wmtk::io::Cache::read_mesh | ( | const std::string & | name | ) | const |
Load a mesh from cache.
name | The name associated with the mesh |
Definition at line 171 of file Cache.cpp.
References load_multimesh(), and m_multimeshes.
Referenced by wmtk::components::get_all_meshes(), wmtk::components::mesh_info(), wmtk::components::periodic_optimization(), wmtk::components::regular_space(), wmtk::components::tag_intersection(), TEST_CASE(), and wmtk::components::wildmeshing().
void wmtk::io::Cache::write_mesh | ( | const Mesh & | m, |
const std::string & | name, | ||
const std::map< std::string, std::vector< int64_t >> & | multimesh_names = {} |
||
) |
Write a mesh to cache.
mesh | The mesh that is written |
name | The name associated with the mesh |
name | The name associated with the mesh |
multimesh_names | names to absolute_multi_mesh_id |
Definition at line 194 of file Cache.cpp.
References create_unique_file(), m_file_paths, m_multimeshes, and wmtk::Mesh::serialize().
Referenced by wmtk::components::get_all_meshes(), wmtk::components::periodic_optimization(), wmtk::components::regular_space(), wmtk::components::tag_intersection(), TEST_CASE(), and wmtk::components::wildmeshing().
|
inlinestaticprivate |
Definition at line 161 of file Cache.hpp.
Referenced by export_cache(), and import_cache().
|
private |
Definition at line 156 of file Cache.hpp.
Referenced by Cache(), create_unique_file(), export_cache(), get_cache_path(), import_cache(), operator=(), and ~Cache().
|
private |
Definition at line 159 of file Cache.hpp.
Referenced by operator=(), and ~Cache().
|
private |
Definition at line 157 of file Cache.hpp.
Referenced by create_unique_file(), equals(), export_cache(), get_file_path(), import_cache(), mesh_names(), operator=(), and write_mesh().
|
mutableprivate |
Definition at line 158 of file Cache.hpp.
Referenced by absolute_multi_mesh_id(), flush_multimeshes(), load_multimesh(), operator=(), read_mesh(), and write_mesh().