Wildmeshing Toolkit
wmtk::io::Cache Class Reference

#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 &&)
 
Cacheoperator= (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< Meshread_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, CachedMultiMeshm_multimeshes
 
bool m_delete_cache = true
 

Static Private Attributes

static const std::string m_cache_content_name
 

Detailed Description

Definition at line 12 of file Cache.hpp.

Constructor & Destructor Documentation

◆ Cache() [1/2]

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.

Here is the call graph for this function:

◆ Cache() [2/2]

wmtk::io::Cache::Cache ( Cache &&  o)

Definition at line 31 of file Cache.cpp.

◆ ~Cache()

wmtk::io::Cache::~Cache ( )

Definition at line 79 of file Cache.cpp.

References wmtk::logger(), m_cache_dir, and m_delete_cache.

Here is the call graph for this function:

Member Function Documentation

◆ absolute_multi_mesh_id()

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.

◆ create_unique_directory()

std::filesystem::path wmtk::io::Cache::create_unique_directory ( const std::string &  prefix,
const std::filesystem::path &  location = "",
size_t  max_tries = 10000 
)
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.

Parameters
prefixA prefix for the directory name.
locationThe location where the new directory will be created.
max_triesThe 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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ create_unique_file()

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

Parameters
nameThe 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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ equals()

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ export_cache()

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ flush_multimeshes()

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().

Here is the caller graph for this function:

◆ get_cache_path()

std::filesystem::path wmtk::io::Cache::get_cache_path ( ) const

Get the path of the cache folder.

Returns
cache path

Definition at line 146 of file Cache.cpp.

References m_cache_dir.

Referenced by TEST_CASE().

Here is the caller graph for this function:

◆ get_file_path() [1/2]

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.

Returns
file path

Definition at line 122 of file Cache.cpp.

References create_unique_file(), and m_file_paths.

Referenced by load_multimesh().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_file_path() [2/2]

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.

Returns
file path

Definition at line 134 of file Cache.cpp.

References m_file_paths.

◆ import_cache()

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().

Here is the caller graph for this function:

◆ load_multimesh()

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mesh_names()

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.

◆ operator=()

Cache & wmtk::io::Cache::operator= ( Cache &&  o)

Definition at line 40 of file Cache.cpp.

References m_cache_dir, m_delete_cache, m_file_paths, and m_multimeshes.

◆ read_mesh()

std::shared_ptr< Mesh > wmtk::io::Cache::read_mesh ( const std::string &  name) const

Load a mesh from cache.

Parameters
nameThe name associated with the mesh
Returns
shared pointer to 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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_mesh()

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.

Parameters
meshThe mesh that is written
nameThe name associated with the mesh
nameThe name associated with the mesh
multimesh_namesnames 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().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_cache_content_name

const std::string wmtk::io::Cache::m_cache_content_name
inlinestaticprivate
Initial value:
=
"cache_contents"

Definition at line 161 of file Cache.hpp.

Referenced by export_cache(), and import_cache().

◆ m_cache_dir

std::filesystem::path wmtk::io::Cache::m_cache_dir
private

◆ m_delete_cache

bool wmtk::io::Cache::m_delete_cache = true
private

Definition at line 159 of file Cache.hpp.

Referenced by operator=(), and ~Cache().

◆ m_file_paths

std::map<std::string, std::filesystem::path> wmtk::io::Cache::m_file_paths
private

◆ m_multimeshes

std::map<std::string, CachedMultiMesh> wmtk::io::Cache::m_multimeshes
mutableprivate

The documentation for this class was generated from the following files: