9#include "../FileModel/Design/FileArchive.h"
11#include "../ProductModel/Design.h"
16#include <shared_mutex>
19using namespace std::filesystem;
24 m_directory(std::move(directory))
37 std::unique_lock<std::mutex> lock(m_backgroundMutex);
38 m_backgroundDoneCv.wait(lock, [
this]()
40 return m_outstandingBackgroundLoads == 0;
46 return GetOrLoadSingleFlight<ProductModel::Design>(
50 [
this, &designName]() {
return LoadDesign(designName); },
56 return GetFileArchiveInternal(designName,
true);
59 std::shared_ptr<FileModel::Design::FileArchive> DesignCache::GetFileArchiveInternal(
const std::string& designName,
bool updateState)
62 ss <<
"Retrieving design \"" << designName <<
"\" from cache... ";
65 return GetOrLoadSingleFlight<FileModel::Design::FileArchive>(
68 m_fileArchivesInFlight,
69 [
this, &designName]() {
return LoadFileArchive(designName); },
73 void DesignCache::AddFileArchive(
const std::string& designName, std::shared_ptr<FileModel::Design::FileArchive> fileArchive,
bool save, std::uint64_t injectedBytes)
76 std::unique_lock<std::shared_mutex> writeLock(m_cacheMutex);
77 m_fileArchivesByName[designName] = fileArchive;
84 m_designsByName.erase(designName);
89 InvalidateResponsePayloads(designName);
96 InsertLruAndEvict(designName, injectedBytes);
107 ResyncLruEstimate(designName);
114 std::unique_lock<std::shared_mutex> writeLock(m_cacheMutex);
118 m_fileArchivesByName.erase(designName);
119 m_designsByName.erase(designName);
124 InvalidateResponsePayloads(designName);
130 loginfo(
"Invalidated cached design \"" + designName +
"\" (archive replaced)");
136 if (fileArchive !=
nullptr)
138 std::string directory;
140 std::shared_lock<std::shared_mutex> readLock(m_cacheMutex);
141 directory = m_directory;
143 return fileArchive->SaveFileModel(directory);
150 std::shared_lock<std::shared_mutex> readLock(m_cacheMutex);
151 std::vector<std::string> loadedDesigns;
152 for (
const auto& kv : m_designsByName)
154 loadedDesigns.push_back(kv.first);
156 return loadedDesigns;
161 std::shared_lock<std::shared_mutex> readLock(m_cacheMutex);
162 std::vector<std::string> loadedFileArchives;
163 for (
const auto& kv : m_fileArchivesByName)
165 loadedFileArchives.push_back(kv.first);
167 return loadedFileArchives;
172 std::string directory;
174 std::shared_lock<std::shared_mutex> readLock(m_cacheMutex);
175 directory = m_directory;
178 std::vector<std::string> unloadedNames;
183 for (
const auto& entry : directory_iterator(dir))
185 if (entry.is_regular_file())
187 unloadedNames.push_back(entry.path().stem().string());
198 return unloadedNames;
205 std::string directory;
207 std::shared_lock<std::shared_mutex> readLock(m_cacheMutex);
208 directory = m_directory;
211 for (
const auto& entry : directory_iterator(directory))
213 if (entry.is_regular_file())
220 if (pFileArchive !=
nullptr)
225 catch (std::exception& e)
229 if (stopOnError)
throw e;
242 std::string directory;
244 std::shared_lock<std::shared_mutex> readLock(m_cacheMutex);
245 directory = m_directory;
248 for (
const auto& entry : directory_iterator(directory))
250 if (entry.is_regular_file())
256 auto pDesign =
GetDesign(entry.path().stem().string());
257 if (pDesign !=
nullptr)
262 catch (std::exception& e)
281 for (
const auto& name : names)
286 if (pFileArchive !=
nullptr)
291 catch (std::exception& e)
305 for (
const auto& name : names)
310 if (pDesign !=
nullptr)
315 catch (std::exception& e)
327 std::unique_lock<std::shared_mutex> writeLock(m_cacheMutex);
328 m_directory = directory;
333 std::shared_lock<std::shared_mutex> readLock(m_cacheMutex);
339 std::vector<std::string> knownDesigns;
341 std::unique_lock<std::shared_mutex> writeLock(m_cacheMutex);
346 knownDesigns.reserve(m_fileArchivesByName.size() + m_designsByName.size());
347 for (
const auto& kv : m_fileArchivesByName)
349 knownDesigns.push_back(kv.first);
351 for (
const auto& kv : m_designsByName)
353 knownDesigns.push_back(kv.first);
355 m_fileArchivesByName.clear();
356 m_designsByName.clear();
359 std::lock_guard<std::mutex> lruLock(m_lruMutex);
360 m_lruEntries.clear();
366 m_responseStore.Clear();
369 m_responseStore.BumpGenerations(knownDesigns);
380 std::lock_guard<std::mutex> stateLock(m_loadStateMutex);
381 m_loadStatesByName.clear();
388 std::lock_guard<std::mutex> stateLock(m_loadStateMutex);
389 auto stateIt = m_loadStatesByName.find(designName);
390 if (stateIt != m_loadStatesByName.end())
392 return stateIt->second;
399 std::lock_guard<std::mutex> stateLock(m_loadStateMutex);
400 m_loadObservers.push_back(std::move(callback));
404 bool DesignCache::WasClearedSince(std::uint64_t epoch)
const
406 std::lock_guard<std::mutex> stateLock(m_loadStateMutex);
407 return m_epoch != epoch;
412 std::vector<std::string> evicted;
414 std::lock_guard<std::mutex> lruLock(m_lruMutex);
415 m_maxBytes = maxBytes;
417 evicted = EvictOverBudgetLocked(
"");
419 EvictCacheEntries(evicted);
424 std::lock_guard<std::mutex> lruLock(m_lruMutex);
431 std::lock_guard<std::mutex> lock(m_backgroundMutex);
432 m_maxBackgroundLoads = maxLoads;
435 m_backgroundSlotCv.notify_all();
440 std::lock_guard<std::mutex> lock(m_backgroundMutex);
441 return m_maxBackgroundLoads;
446 return !FindArchivePath(designName).empty();
452 std::lock_guard<std::mutex> lock(m_loadStateMutex);
457 if (m_pendingAsyncLoads.find(designName) != m_pendingAsyncLoads.end() ||
458 m_designsInFlight.find(designName) != m_designsInFlight.end() ||
459 m_fileArchivesInFlight.find(designName) != m_fileArchivesInFlight.end())
463 m_pendingAsyncLoads.insert(designName);
467 std::lock_guard<std::mutex> lock(m_backgroundMutex);
468 ++m_outstandingBackgroundLoads;
475 std::thread(&DesignCache::BackgroundLoad,
this, designName).detach();
480 std::lock_guard<std::mutex> lock(m_backgroundMutex);
481 --m_outstandingBackgroundLoads;
487 m_backgroundDoneCv.notify_all();
489 std::lock_guard<std::mutex> lock(m_loadStateMutex);
490 m_pendingAsyncLoads.erase(designName);
497 void DesignCache::BackgroundLoad(
const std::string& designName)
499 AcquireBackgroundSlot();
506 if (pDesign ==
nullptr)
508 logwarn(
"Background load: no archive found for design \"" + designName +
"\"");
512 loginfo(
"Background load of design \"" + designName +
"\" completed");
515 catch (
const std::exception& e)
517 logexception_msg(e,
"Background load of design \"" + designName +
"\" failed (retryable)");
521 logerror(
"Background load of design \"" + designName +
"\" failed with an unknown exception (retryable)");
523 ReleaseBackgroundSlot();
526 std::lock_guard<std::mutex> lock(m_loadStateMutex);
527 m_pendingAsyncLoads.erase(designName);
530 std::lock_guard<std::mutex> lock(m_backgroundMutex);
531 --m_outstandingBackgroundLoads;
533 m_backgroundDoneCv.notify_all();
536 void DesignCache::AcquireBackgroundSlot()
538 std::unique_lock<std::mutex> lock(m_backgroundMutex);
539 m_backgroundSlotCv.wait(lock, [
this]()
541 return m_maxBackgroundLoads == 0 || m_activeBackgroundLoads < m_maxBackgroundLoads;
543 ++m_activeBackgroundLoads;
546 void DesignCache::ReleaseBackgroundSlot()
549 std::lock_guard<std::mutex> lock(m_backgroundMutex);
550 --m_activeBackgroundLoads;
552 m_backgroundSlotCv.notify_one();
557 if (!std::filesystem::exists(m_directory))
562 std::filesystem::create_directories(m_directory);
564 catch (
const std::exception& e)
566 std::string msg =
"Failed to create design cache directory: " + m_directory;
573 std::filesystem::path DesignCache::FindArchivePath(
const std::string& designName)
const
575 std::string directory;
577 std::shared_lock<std::shared_mutex> readLock(m_cacheMutex);
578 directory = m_directory;
582 const auto options = directory_options::skip_permission_denied;
583 directory_iterator dirIt(directory, options, ec);
589 for (
const auto& entry : dirIt)
591 if (entry.is_regular_file() && entry.path().stem() == designName)
600 std::shared_ptr<ProductModel::Design> DesignCache::LoadDesign(
const std::string& designName)
604 if (FindArchivePath(designName).empty())
612 auto pFileModel = GetFileArchiveInternal(designName,
false);
613 if (pFileModel ==
nullptr)
618 auto pDesign = std::make_shared<ProductModel::Design>();
619 if (pDesign->Build(pFileModel))
627 std::shared_ptr<FileModel::Design::FileArchive> DesignCache::LoadFileArchive(
const std::string& designName)
631 const auto archivePath = FindArchivePath(designName);
632 if (archivePath.empty())
634 logwarn(
"Failed to find file for design \"" + designName +
"\"");
637 const auto options = directory_options::skip_permission_denied;
639 for (
const auto& entry : directory_iterator(
getDirectory(), options, ec))
641 if (entry.is_regular_file())
643 logdebug(
"Found file: " + entry.path().filename().string() +
" (stem: " + entry.path().stem().string() +
")");
650 loginfo(
"file found: [" + archivePath.string() +
"], attempting to parse...");
652 auto pFileArchive = std::make_shared<FileModel::Design::FileArchive>(archivePath.string());
653 if (pFileArchive->ParseFileModel())
665 throw std::runtime_error(
666 "Failed to load design \"" + designName +
"\": could not extract or parse archive");
669 void DesignCache::TouchLru(
const std::string& designName)
671 std::lock_guard<std::mutex> lruLock(m_lruMutex);
672 auto findIt = m_lruEntries.find(designName);
673 if (findIt != m_lruEntries.end())
675 findIt->second.lastServed = std::chrono::steady_clock::now();
683 void DesignCache::InsertLruAndEvict(
const std::string& designName, std::uint64_t injectedBytes)
685 const auto now = std::chrono::steady_clock::now();
686 const auto bytes = EstimateDesignBytes(designName, injectedBytes);
688 std::vector<std::string> evicted;
690 std::lock_guard<std::mutex> lruLock(m_lruMutex);
691 auto findIt = m_lruEntries.find(designName);
692 if (findIt != m_lruEntries.end())
694 findIt->second.lastServed = now;
703 const auto base = bytes - m_responseStore.StoredBytes(designName);
704 const auto delta =
static_cast<std::int64_t
>(base) -
static_cast<std::int64_t
>(findIt->second.estimatedBytes);
705 findIt->second.estimatedBytes = base;
706 m_cachedBytes =
static_cast<std::uint64_t
>(
static_cast<std::int64_t
>(m_cachedBytes) + delta);
710 m_lruEntries.emplace(designName, LruEntry{bytes, 0, now});
711 m_cachedBytes += bytes;
713 evicted = EvictOverBudgetLocked(designName);
715 EvictCacheEntries(evicted);
721 std::vector<std::string> DesignCache::EvictOverBudgetLocked(
const std::string& protectedName)
723 std::vector<std::string> evicted;
731 while (m_cachedBytes > m_maxBytes)
733 auto victimIt = m_lruEntries.end();
734 auto oldest = std::chrono::steady_clock::time_point::max();
736 for (
auto it = m_lruEntries.begin(); it != m_lruEntries.end(); ++it)
738 if (it->first == protectedName)
continue;
739 if (it->second.lastServed >= oldest)
continue;
741 std::lock_guard<std::mutex> stateLock(m_loadStateMutex);
742 auto stateIt = m_loadStatesByName.find(it->first);
748 oldest = it->second.lastServed;
752 if (victimIt == m_lruEntries.end())
764 m_cachedBytes -= victimIt->second.estimatedBytes + victimIt->second.serializedBytes;
765 const auto evictedName = victimIt->first;
766 m_lruEntries.erase(victimIt);
767 evicted.push_back(evictedName);
773 void DesignCache::EvictCacheEntries(
const std::vector<std::string>& names)
775 for (
const auto& name : names)
782 m_responseStore.Invalidate(name);
785 std::unique_lock<std::shared_mutex> writeLock(m_cacheMutex);
786 m_designsByName.erase(name);
787 m_fileArchivesByName.erase(name);
790 loginfo(
"Evicted design \"" + name +
"\" from cache (over byte budget)");
796 std::uint64_t DesignCache::EstimateDesignBytes(
const std::string& designName, std::uint64_t injectedBytes)
const
806 const auto archivePath = FindArchivePath(designName);
807 if (archivePath.empty())
809 return DESIGN_BYTES_OVERHEAD + m_responseStore.StoredBytes(designName) + injectedBytes;
813 const auto size = std::filesystem::file_size(archivePath, ec);
816 return DESIGN_BYTES_OVERHEAD + m_responseStore.StoredBytes(designName) + injectedBytes;
818 return static_cast<std::uint64_t
>(size) + DESIGN_BYTES_OVERHEAD + m_responseStore.StoredBytes(designName);
821 void DesignCache::ResyncLruEstimate(
const std::string& designName)
825 const auto bytes = EstimateDesignBytes(designName);
826 std::lock_guard<std::mutex> lruLock(m_lruMutex);
827 auto findIt = m_lruEntries.find(designName);
828 if (findIt == m_lruEntries.end())
837 const auto base = bytes - m_responseStore.StoredBytes(designName);
840 const auto delta =
static_cast<std::int64_t
>(base) -
static_cast<std::int64_t
>(findIt->second.estimatedBytes);
841 findIt->second.estimatedBytes = base;
842 m_cachedBytes =
static_cast<std::uint64_t
>(
static_cast<std::int64_t
>(m_cachedBytes) + delta);
847 return m_responseStore.CurrentGeneration(designName);
852 std::lock_guard<std::mutex> lruLock(m_lruMutex);
853 return m_cachedBytes;
856 void DesignCache::TransitionLoadState(
const std::string& designName, LoadState to)
858 LoadObserverList observers;
861 std::lock_guard<std::mutex> stateLock(m_loadStateMutex);
862 auto stateIt = m_loadStatesByName.find(designName);
863 from = (stateIt != m_loadStatesByName.end()) ? stateIt->second :
LoadState::
Unloaded;
870 m_loadStatesByName[designName] = to;
871 observers = m_loadObservers;
873 NotifyLoadObservers(designName, from, to, observers);
879 void DesignCache::EraseLoadState(
const std::string& designName)
881 LoadObserverList observers;
884 std::lock_guard<std::mutex> stateLock(m_loadStateMutex);
885 auto stateIt = m_loadStatesByName.find(designName);
886 if (stateIt == m_loadStatesByName.end())
890 from = stateIt->second;
891 m_loadStatesByName.erase(stateIt);
892 observers = m_loadObservers;
902 void DesignCache::NotifyLoadObservers(
const std::string& designName, LoadState from, LoadState to,
const LoadObserverList& observers)
904 for (
const auto& observer : observers)
908 observer(designName, from, to);
910 catch (
const std::exception& e)
912 logwarn(std::string(
"Design load observer threw: ") + e.what());
916 logwarn(
"Design load observer threw an unknown exception");
923 bool DesignCache::ResponseStore::TryGetDesignBytes(
const std::string& designName, std::string& outBytes)
const
925 std::lock_guard<std::mutex> lock(m_mutex);
926 auto findIt = m_entriesByName.find(designName);
927 if (findIt == m_entriesByName.end() || findIt->second.payload.designPbBytes.empty())
931 outBytes = findIt->second.payload.designPbBytes;
935 bool DesignCache::ResponseStore::TryGetJsonPayload(
const std::string& designName, JsonPayloadKind kind, std::string& outJson)
const
937 std::lock_guard<std::mutex> lock(m_mutex);
938 auto findIt = m_entriesByName.find(designName);
939 if (findIt == m_entriesByName.end())
943 const auto& json = (kind == JsonPayloadKind::Design)
944 ? findIt->second.payload.designJson
945 : findIt->second.payload.fileModelJson;
954 bool DesignCache::ResponseStore::HasEntry(
const std::string& designName)
const
956 std::lock_guard<std::mutex> lock(m_mutex);
957 return m_entriesByName.find(designName) != m_entriesByName.end();
960 std::uint64_t DesignCache::ResponseStore::StoredBytes(
const std::string& designName)
const
962 std::lock_guard<std::mutex> lock(m_mutex);
963 auto findIt = m_entriesByName.find(designName);
964 return findIt != m_entriesByName.end() ? findIt->second.payload.totalBytes() : 0;
967 std::uint64_t DesignCache::ResponseStore::TotalStoredBytes()
const
969 std::lock_guard<std::mutex> lock(m_mutex);
970 std::uint64_t total = 0;
971 for (
const auto& kv : m_entriesByName)
973 total += kv.second.payload.totalBytes();
978 std::uint64_t DesignCache::ResponseStore::CurrentGeneration(
const std::string& designName)
const
980 std::lock_guard<std::mutex> lock(m_mutex);
981 auto findIt = m_generationsByName.find(designName);
982 return findIt != m_generationsByName.end() ? findIt->second : 0;
985 DesignCache::ResponseStore::StoreResult DesignCache::ResponseStore::TryStore(
986 const std::string& designName, std::uint64_t generation, ResponsePayload&& payload)
988 std::lock_guard<std::mutex> lock(m_mutex);
992 auto genIt = m_generationsByName.find(designName);
993 const auto currentGeneration = genIt != m_generationsByName.end() ? genIt->second : 0;
994 if (generation != currentGeneration)
1001 auto findIt = m_entriesByName.find(designName);
1002 if (findIt != m_entriesByName.end())
1008 result.stored =
true;
1009 result.storedBytes = findIt->second.payload.totalBytes();
1013 auto& entry = m_entriesByName[designName];
1014 entry.payload = std::move(payload);
1015 ++m_designSerializations;
1018 result.stored =
true;
1019 result.storedBytes = entry.payload.totalBytes();
1023 std::uint64_t DesignCache::ResponseStore::Invalidate(
const std::string& designName)
1025 std::lock_guard<std::mutex> lock(m_mutex);
1026 std::uint64_t dropped = 0;
1027 auto findIt = m_entriesByName.find(designName);
1028 if (findIt != m_entriesByName.end())
1030 dropped = findIt->second.payload.totalBytes();
1031 m_entriesByName.erase(findIt);
1035 ++m_generationsByName[designName];
1039 void DesignCache::ResponseStore::BumpGenerations(
const std::vector<std::string>& designNames)
1041 std::lock_guard<std::mutex> lock(m_mutex);
1042 for (
const auto& name : designNames)
1046 ++m_generationsByName[name];
1050 void DesignCache::ResponseStore::Clear()
1052 std::lock_guard<std::mutex> lock(m_mutex);
1053 m_entriesByName.clear();
1066 std::uint64_t DesignCache::ResponseStore::designSerializations()
const
1068 std::lock_guard<std::mutex> lock(m_mutex);
1069 return m_designSerializations;
1076 return m_responseStore.TryGetDesignBytes(designName, outBytes);
1081 return m_responseStore.TryGetJsonPayload(designName, kind, outJson);
1086 return m_responseStore.HasEntry(designName);
1091 return m_responseStore.designSerializations();
1096 return m_responseStore.TotalStoredBytes();
1101 std::lock_guard<std::mutex> lruLock(m_lruMutex);
1102 m_monolithicWireBudget = maxBytes;
1107 std::lock_guard<std::mutex> lruLock(m_lruMutex);
1108 return m_monolithicWireBudget;
1111 std::uint64_t DesignCache::CurrentEpoch()
const
1113 std::lock_guard<std::mutex> stateLock(m_loadStateMutex);
1117 DesignCache::ResponsePayload DesignCache::BuildResponsePayload(
const ProductModel::Design& design)
1119 ResponsePayload payload;
1128 std::lock_guard<std::mutex> lruLock(m_lruMutex);
1129 if (m_monolithicWireBudget != 0 &&
1132 loginfo(
"Skipping monolithic response pre-serialization (over serve budget); design is served via GetDesignStream");
1144 if (pMessage !=
nullptr)
1146 if (!pMessage->SerializeToString(&payload.designPbBytes))
1148 payload.designPbBytes.clear();
1162 void DesignCache::CommitResponsePayloads(
const std::string& designName,
1163 std::uint64_t epochAtStart,
1164 const std::shared_ptr<ProductModel::Design>& pDesign,
1165 ResponsePayload&& payload,
1166 std::uint64_t generation)
1168 if (pDesign ==
nullptr || payload.totalBytes() == 0)
1178 std::shared_lock<std::shared_mutex> readLock(m_cacheMutex);
1179 auto findIt = m_designsByName.find(designName);
1180 if (findIt == m_designsByName.end() || findIt->second != pDesign)
1186 std::vector<std::string> evicted;
1188 std::lock_guard<std::mutex> lruLock(m_lruMutex);
1189 auto entryIt = m_lruEntries.find(designName);
1190 if (entryIt == m_lruEntries.end())
1194 if (WasClearedSince(epochAtStart))
1213 if (m_monolithicWireBudget != 0 && payload.totalBytes() > m_monolithicWireBudget)
1218 const auto storeResult = m_responseStore.TryStore(designName, generation, std::move(payload));
1219 if (storeResult.stored)
1225 const auto delta = storeResult.storedBytes - entryIt->second.serializedBytes;
1226 entryIt->second.serializedBytes = storeResult.storedBytes;
1227 m_cachedBytes += delta;
1230 evicted = EvictOverBudgetLocked(designName);
1232 EvictCacheEntries(evicted);
1235 void DesignCache::ScheduleResponsePreSerialization(
const std::string& designName,
1236 std::shared_ptr<ProductModel::Design> pDesign,
1237 std::uint64_t epochAtStart)
1239 const auto generation = m_responseStore.CurrentGeneration(designName);
1245 std::lock_guard<std::mutex> lock(m_backgroundMutex);
1246 ++m_outstandingBackgroundLoads;
1251 std::thread(&DesignCache::PreSerializeWorker,
this, designName, std::move(pDesign),
1252 epochAtStart, generation).detach();
1254 catch (
const std::exception& e)
1257 std::lock_guard<std::mutex> lock(m_backgroundMutex);
1258 --m_outstandingBackgroundLoads;
1262 m_backgroundDoneCv.notify_all();
1265 logwarn(
"Failed to schedule response pre-serialization for design \"" + designName +
"\": " + e.what());
1269 void DesignCache::PreSerializeWorker(std::string designName,
1270 std::shared_ptr<ProductModel::Design> pDesign,
1271 std::uint64_t epochAtStart,
1272 std::uint64_t generation)
1276 AcquireBackgroundSlot();
1279 auto payload = BuildResponsePayload(*pDesign);
1280 CommitResponsePayloads(designName, epochAtStart, pDesign, std::move(payload), generation);
1281 logdebug(
"Response pre-serialization for design \"" + designName +
"\" committed");
1283 catch (
const std::exception& e)
1285 logwarn(
"Response pre-serialization for design \"" + designName +
"\" failed: " + e.what());
1289 logwarn(
"Response pre-serialization for design \"" + designName +
"\" failed with an unknown exception");
1291 ReleaseBackgroundSlot();
1294 std::lock_guard<std::mutex> lock(m_backgroundMutex);
1295 --m_outstandingBackgroundLoads;
1297 m_backgroundDoneCv.notify_all();
1300 void DesignCache::InvalidateResponsePayloads(
const std::string& designName)
1302 m_responseStore.Invalidate(designName);
1303 AdjustLruSerializedBytes(designName);
1306 void DesignCache::AdjustLruSerializedBytes(
const std::string& designName)
1308 std::lock_guard<std::mutex> lruLock(m_lruMutex);
1309 auto entryIt = m_lruEntries.find(designName);
1310 if (entryIt == m_lruEntries.end())
1314 const auto stored = m_responseStore.StoredBytes(designName);
1315 if (stored != entryIt->second.serializedBytes)
1317 m_cachedBytes -= entryIt->second.serializedBytes - stored;
1318 entryIt->second.serializedBytes = stored;
1322 void DesignCache::LogFailedStateRecordError(
const std::string& designName)
1326 logerror(
"Failed to record Failed load-state for design \"" + designName +
"\" during load-failure cleanup");
1331 std::shared_ptr<ProductModel::Design> pDesign;
1333 std::shared_lock<std::shared_mutex> readLock(m_cacheMutex);
1334 auto findIt = m_designsByName.find(designName);
1335 if (findIt == m_designsByName.end())
1339 pDesign = findIt->second;
1342 const auto generation = m_responseStore.CurrentGeneration(designName);
1343 const auto epoch = CurrentEpoch();
1347 auto payload = BuildResponsePayload(*pDesign);
1348 CommitResponsePayloads(designName, epoch, pDesign, std::move(payload), generation);
1350 catch (
const std::exception& e)
1352 logwarn(
"Lazy response serialization for design \"" + designName +
"\" failed: " + e.what());
1356 logwarn(
"Lazy response serialization for design \"" + designName +
"\" failed with an unknown exception");
#define logexception_msg(e, m)
void InvalidateDesign(const std::string &designName)
void AddFileArchive(const std::string &designName, std::shared_ptr< FileModel::Design::FileArchive > fileArchive, bool save, std::uint64_t injectedBytes=0)
int loadFileArchives(const Utils::StringVector &names)
std::uint64_t getMonolithicWireBudget() const
std::vector< std::string > getLoadedFileArchiveNames(const std::string &filter="") const
bool SaveFileArchive(const std::string &designName)
std::uint64_t cachedResponseBytes() const
bool TryGetJsonPayload(const std::string &designName, JsonPayloadKind kind, std::string &outJson) const
int loadAllDesigns(bool stopOnError)
void setMaxBackgroundLoads(std::size_t maxLoads)
int loadAllFileArchives(bool stopOnError)
void AddLoadObserver(LoadEventCallback callback)
std::uint64_t GetResponseGeneration(const std::string &designName) const
std::shared_ptr< ProductModel::Design > GetDesign(const std::string &designName)
std::uint64_t cachedBytes() const
bool HasCachedResponse(const std::string &designName) const
bool LoadDesignAsync(const std::string &designName)
LoadState GetLoadState(const std::string &designName) const
void setMonolithicWireBudget(std::uint64_t maxBytes)
std::shared_ptr< FileModel::Design::FileArchive > GetFileArchive(const std::string &designName)
std::string getDirectory() const
bool ContainsDesign(const std::string &designName) const
std::vector< std::string > getUnloadedDesignNames(const std::string &filter="") const
bool TryGetDesignBytes(const std::string &designName, std::string &outBytes) const
std::size_t getMaxBackgroundLoads() const
std::uint64_t designSerializationCount() const
int loadDesigns(const Utils::StringVector &names)
void setDirectory(const std::string &directory)
std::function< void(const std::string &designName, LoadState from, LoadState to)> LoadEventCallback
DesignCache(std::string directory)
void SerializeResponsePayloads(const std::string &designName)
void setCacheMaxBytes(std::uint64_t maxBytes)
void ensureDirectoryExists() const
std::uint64_t getCacheMaxBytes() const
std::vector< std::string > getLoadedDesignNames(const std::string &filter="") const
std::uint64_t EstimateWireSizeBytes(bool includeNormalizedLists=false) const
std::unique_ptr< Protobuf::ProductModel::Design > to_protobuf() const override
std::vector< std::string > StringVector