Wildmeshing Toolkit
Cache.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <filesystem>
4 #include <map>
5 #include <string_view>
6 #include <wmtk/Mesh.hpp>
7 #include "CachedMultiMesh.hpp"
8 
9 namespace wmtk::io {
10 
11 
12 class Cache
13 {
14 public:
27  Cache(
28  const std::string& prefix,
29  const std::filesystem::path directory = "",
30  bool delete_cache = true);
31 
32  Cache(Cache&&);
33  Cache& operator=(Cache&&);
34 
35  ~Cache();
36 
45  const std::filesystem::path& create_unique_file(
46  const std::string& filename,
47  const std::string& extension,
48  size_t max_tries = 10000);
49 
57  const std::filesystem::path& get_file_path(const std::string& filename);
58 
66  std::filesystem::path get_file_path(const std::string& filename) const;
67 
73  std::filesystem::path get_cache_path() const;
74 
82  std::shared_ptr<Mesh> read_mesh(const std::string& name) const;
83 
84  void load_multimesh(const std::string& name) const;
85 
94  void write_mesh(
95  const Mesh& m,
96  const std::string& name,
97  const std::map<std::string, std::vector<int64_t>>& multimesh_names = {});
98 
108  bool export_cache(const std::filesystem::path& export_location);
109 
118  bool import_cache(const std::filesystem::path& import_location);
119 
120  std::vector<int64_t> absolute_multi_mesh_id(const std::string& name) const;
121 
127  bool equals(const Cache& o);
128 
141  static std::filesystem::path create_unique_directory(
142  const std::string& prefix,
143  const std::filesystem::path& location = "",
144  size_t max_tries = 10000);
145 
146 
148  void flush_multimeshes();
149 
153  std::vector<std::string> mesh_names();
154 
155 private:
156  std::filesystem::path m_cache_dir;
157  std::map<std::string, std::filesystem::path> m_file_paths; // name --> file location
158  mutable std::map<std::string, CachedMultiMesh> m_multimeshes;
159  bool m_delete_cache = true;
160 
161  inline static const std::string m_cache_content_name =
162  "cache_contents"; // name of the json file used for import/export
163 };
164 } // namespace wmtk::io
void flush_multimeshes()
Unsets the mesh held by each cached mm - useful for debugging whether cache loading works.
Definition: Cache.cpp:177
std::vector< std::string > mesh_names()
Get all names of the meshes stored in cache.
Definition: Cache.cpp:184
const std::filesystem::path & get_file_path(const std::string &filename)
Get the path where the file with the given name is stored.
Definition: Cache.cpp:122
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.
Definition: Cache.cpp:194
std::filesystem::path get_cache_path() const
Get the path of the cache folder.
Definition: Cache.cpp:146
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.
Definition: Cache.cpp:94
bool equals(const Cache &o)
Compare two caches for equality.
Definition: Cache.cpp:311
std::map< std::string, std::filesystem::path > m_file_paths
Definition: Cache.hpp:157
std::map< std::string, CachedMultiMesh > m_multimeshes
Definition: Cache.hpp:158
bool m_delete_cache
Definition: Cache.hpp:159
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.
Definition: Cache.cpp:51
Cache & operator=(Cache &&)
Definition: Cache.cpp:40
void load_multimesh(const std::string &name) const
Definition: Cache.cpp:156
bool import_cache(const std::filesystem::path &import_location)
Import a cache from the given location.
Definition: Cache.cpp:263
static const std::string m_cache_content_name
Definition: Cache.hpp:161
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.
Definition: Cache.cpp:72
std::vector< int64_t > absolute_multi_mesh_id(const std::string &name) const
Definition: Cache.cpp:150
bool export_cache(const std::filesystem::path &export_location)
Export the cache to the given location.
Definition: Cache.cpp:232
std::shared_ptr< Mesh > read_mesh(const std::string &name) const
Load a mesh from cache.
Definition: Cache.cpp:171
std::filesystem::path m_cache_dir
Definition: Cache.hpp:156