1 #include "ArchiveExtractor.h"
2 #include "DesignCache.h"
9 using namespace std::filesystem;
11 namespace Odb::Lib::App
13 DesignCache::DesignCache(std::string directory) :
14 m_directory(std::move(directory))
18 DesignCache::~DesignCache()
23 std::shared_ptr<ProductModel::Design> DesignCache::GetDesign(
const std::string& designName)
25 auto findIt = m_designsByName.find(designName);
26 if (findIt == m_designsByName.end())
28 auto pDesign = LoadDesign(designName);
32 return m_designsByName[designName];
35 std::shared_ptr<FileModel::Design::FileArchive> DesignCache::GetFileArchive(
const std::string& designName)
38 ss <<
"Retrieving design \"" << designName <<
"\" from cache... ";
41 auto findIt = m_fileArchivesByName.find(designName);
42 if (findIt == m_fileArchivesByName.end())
44 loginfo(
"Not found in cache, attempting to load from file...");
46 auto pFileArchive = LoadFileArchive(designName);
47 if (pFileArchive ==
nullptr)
49 logwarn(
"Failed loading from file");
53 loginfo(
"Loaded from file");
59 loginfo(
"Found. Returning from cache.");
62 return m_fileArchivesByName[designName];
65 std::vector<std::string> DesignCache::getLoadedDesignNames(
const std::string& filter)
const
67 std::vector<std::string> loadedDesigns;
68 for (
const auto& kv : m_designsByName)
70 loadedDesigns.push_back(kv.second->GetFileModel()->GetProductName());
75 std::vector<std::string> DesignCache::getLoadedFileArchiveNames(
const std::string& filter)
const
77 std::vector<std::string> loadedFileArchives;
78 for (
const auto& kv : m_fileArchivesByName)
80 loadedFileArchives.push_back(kv.second->GetProductName());
82 return loadedFileArchives;
85 std::vector<std::string> DesignCache::getUnloadedDesignNames(
const std::string& filter)
const
87 std::vector<std::string> unloadedNames;
91 path dir(m_directory);
92 for (
const auto& entry : directory_iterator(dir))
94 if (entry.is_regular_file())
96 unloadedNames.push_back(entry.path().stem().string());
107 return unloadedNames;
110 int DesignCache::loadAllFileArchives(
bool stopOnError)
114 for (
const auto& entry : directory_iterator(m_directory))
116 if (entry.is_regular_file())
118 if (ArchiveExtractor::IsArchiveTypeSupported(entry.path().filename()))
122 auto pFileArchive = LoadFileArchive(entry.path().stem().string());
123 if (pFileArchive !=
nullptr)
128 catch (std::exception& e)
132 if (stopOnError)
throw e;
141 int DesignCache::loadAllDesigns(
bool stopOnError)
145 for (
const auto& entry : directory_iterator(m_directory))
147 if (entry.is_regular_file())
149 if (ArchiveExtractor::IsArchiveTypeSupported(entry.path().filename()))
153 auto pDesign = LoadDesign(entry.path().stem().string());
154 if (pDesign !=
nullptr)
159 catch (std::exception& e)
174 int DesignCache::loadFileArchives(
const StringVector& names)
178 for (
const auto& name : names)
182 auto pFileArchive = LoadFileArchive(name);
183 if (pFileArchive !=
nullptr)
188 catch (std::exception& e)
198 int DesignCache::loadDesigns(
const StringVector& names)
202 for (
const auto& name : names)
206 auto pDesign = LoadDesign(name);
207 if (pDesign !=
nullptr)
212 catch (std::exception& e)
222 void DesignCache::setDirectory(
const std::string& directory)
224 m_directory = directory;
227 const std::string& DesignCache::getDirectory()
const
237 void DesignCache::Clear()
239 m_fileArchivesByName.clear();
240 m_designsByName.clear();
243 std::shared_ptr<ProductModel::Design> DesignCache::LoadDesign(
const std::string& designName)
245 for (
const auto& entry : directory_iterator(m_directory))
247 if (entry.is_regular_file())
249 if (entry.path().stem() == designName)
251 auto pFileModel = GetFileArchive(designName);
252 if (pFileModel !=
nullptr)
254 auto pDesign = std::make_shared<ProductModel::Design>();
255 if (pDesign->Build(pFileModel))
258 m_designsByName[pFileModel->GetProductName()] = pDesign;
277 std::shared_ptr<FileModel::Design::FileArchive> DesignCache::LoadFileArchive(
const std::string& designName)
279 auto fileFound =
false;
282 const auto options = directory_options::skip_permission_denied;
283 for (
const auto& entry : directory_iterator(m_directory, options))
285 if (entry.is_regular_file())
287 if (entry.path().stem() == designName)
291 loginfo(
"file found: [" + entry.path().string() +
"], attempting to parse...");
293 auto pFileArchive = std::make_shared<FileModel::Design::FileArchive>(entry.path().string());
294 if (pFileArchive->ParseFileModel())
297 m_fileArchivesByName[pFileArchive->GetProductName()] = pFileArchive;
310 logwarn(
"Failed to find file for design \"" + designName +
"\"");