OdbDesign
OdbDesign ODB++ Parser - C++ library, gRPC/REST server, CLI, tests
 
Loading...
Searching...
No Matches
DesignCache.cpp
Go to the documentation of this file.
1#include "DesignCache.h"
2#include "ArchiveExtractor.h"
3#include "Logger.h"
4#include <exception>
5#include <filesystem>
6#include <stdexcept>
7#include <thread>
8#include <vector>
9#include "../FileModel/Design/FileArchive.h"
10#include <memory>
11#include "../ProductModel/Design.h"
12#include <iosfwd>
13#include <string>
14#include <type_traits>
15#include <StringVector.h>
16#include <shared_mutex>
17
18using namespace Utils;
19using namespace std::filesystem;
20
21namespace Odb::Lib::App
22{
23 DesignCache::DesignCache(std::string directory) :
24 m_directory(std::move(directory))
25 {
27 }
28
30 {
31 Clear();
32
33 // Detached background loads hold `this`; drain them before member
34 // teardown. A queued load first acquires its semaphore slot (running
35 // loads release theirs), so the wait is bounded by the outstanding
36 // parse time.
37 std::unique_lock<std::mutex> lock(m_backgroundMutex);
38 m_backgroundDoneCv.wait(lock, [this]()
39 {
40 return m_outstandingBackgroundLoads == 0;
41 });
42 }
43
44 std::shared_ptr<ProductModel::Design> DesignCache::GetDesign(const std::string& designName)
45 {
46 return GetOrLoadSingleFlight<ProductModel::Design>(
47 designName,
48 m_designsByName,
49 m_designsInFlight,
50 [this, &designName]() { return LoadDesign(designName); },
51 true);
52 }
53
54 std::shared_ptr<FileModel::Design::FileArchive> DesignCache::GetFileArchive(const std::string& designName)
55 {
56 return GetFileArchiveInternal(designName, true);
57 }
58
59 std::shared_ptr<FileModel::Design::FileArchive> DesignCache::GetFileArchiveInternal(const std::string& designName, bool updateState)
60 {
61 std::stringstream ss;
62 ss << "Retrieving design \"" << designName << "\" from cache... ";
63 logdebug(ss.str());
64
65 return GetOrLoadSingleFlight<FileModel::Design::FileArchive>(
66 designName,
67 m_fileArchivesByName,
68 m_fileArchivesInFlight,
69 [this, &designName]() { return LoadFileArchive(designName); },
70 updateState);
71 }
72
73 void DesignCache::AddFileArchive(const std::string& designName, std::shared_ptr<FileModel::Design::FileArchive> fileArchive, bool save, std::uint64_t injectedBytes)
74 {
75 {
76 std::unique_lock<std::shared_mutex> writeLock(m_cacheMutex);
77 m_fileArchivesByName[designName] = fileArchive;
78 // Re-upload invalidation (M1.4): any previously cached Design was
79 // built from the OLD archive contents, so it — and every serialized
80 // response payload derived from it — must go. Dropping the Design
81 // (not just the payloads) is what makes invalidation airtight: a
82 // later GetDesign rebuilds from the new archive, and re-populated
83 // payloads describe the new contents.
84 m_designsByName.erase(designName);
85 }
86
87 // Drop serialized response payloads for the design (generation bump)
88 // before the state/LRU bookkeeping.
89 InvalidateResponsePayloads(designName);
90
91 // Charge the injected archive to the byte budget (never its own eviction
92 // victim) and reflect the externally-completed load in the state machine.
93 // injectedBytes stands in for the archive size while nothing on disk
94 // backs the name (save=false); without it the charge is only the flat
95 // DESIGN_BYTES_OVERHEAD.
96 InsertLruAndEvict(designName, injectedBytes);
97 TransitionLoadState(designName, LoadState::Loaded);
98
99 if (save)
100 {
101 // SaveFileArchive calls GetFileArchive internally, which takes the locks.
102 // We must release before calling to avoid deadlock. (No locks held here.)
103 SaveFileArchive(designName);
104 // The estimate above ran before the file landed, so a fresh name was
105 // still charged injectedBytes-only; now that the archive exists on
106 // disk, re-estimate (real file size) and adjust the charge.
107 ResyncLruEstimate(designName);
108 }
109 }
110
111 void DesignCache::InvalidateDesign(const std::string& designName)
112 {
113 {
114 std::unique_lock<std::shared_mutex> writeLock(m_cacheMutex);
115 // Same contract as AddFileArchive's re-upload invalidation: a cached
116 // Design (and the payloads derived from it) was built from the old
117 // archive contents and must not survive the replacement.
118 m_fileArchivesByName.erase(designName);
119 m_designsByName.erase(designName);
120 }
121
122 // Drop serialized response payloads (generation bump), uncharging their
123 // LRU bytes when an entry exists.
124 InvalidateResponsePayloads(designName);
125
126 // Mirror eviction's state handling: observers see Loaded -> Unloaded and
127 // the next load announces a fresh Loading -> Loaded cycle.
128 TransitionLoadState(designName, LoadState::Unloaded);
129
130 loginfo("Invalidated cached design \"" + designName + "\" (archive replaced)");
131 }
132
133 bool DesignCache::SaveFileArchive(const std::string& designName)
134 {
135 auto fileArchive = GetFileArchive(designName);
136 if (fileArchive != nullptr)
137 {
138 std::string directory;
139 {
140 std::shared_lock<std::shared_mutex> readLock(m_cacheMutex);
141 directory = m_directory;
142 }
143 return fileArchive->SaveFileModel(directory);
144 }
145 return false;
146 }
147
148 std::vector<std::string> DesignCache::getLoadedDesignNames(const std::string& filter) const
149 {
150 std::shared_lock<std::shared_mutex> readLock(m_cacheMutex);
151 std::vector<std::string> loadedDesigns;
152 for (const auto& kv : m_designsByName)
153 {
154 loadedDesigns.push_back(kv.first);
155 }
156 return loadedDesigns;
157 }
158
159 std::vector<std::string> DesignCache::getLoadedFileArchiveNames(const std::string& filter) const
160 {
161 std::shared_lock<std::shared_mutex> readLock(m_cacheMutex);
162 std::vector<std::string> loadedFileArchives;
163 for (const auto& kv : m_fileArchivesByName)
164 {
165 loadedFileArchives.push_back(kv.first);
166 }
167 return loadedFileArchives;
168 }
169
170 std::vector<std::string> DesignCache::getUnloadedDesignNames(const std::string& filter) const
171 {
172 std::string directory;
173 {
174 std::shared_lock<std::shared_mutex> readLock(m_cacheMutex);
175 directory = m_directory;
176 }
177
178 std::vector<std::string> unloadedNames;
179
180 //try
181 {
182 path dir(directory);
183 for (const auto& entry : directory_iterator(dir))
184 {
185 if (entry.is_regular_file())
186 {
187 unloadedNames.push_back(entry.path().stem().string());
188 }
189 }
190 }
191 //catch (std::filesystem::filesystem_error& fe)
192 //{
193 // logexception(fe);
194 // // re-throw it so we get a HTTP 500 response to the client
195 // throw fe;
196 //}
197
198 return unloadedNames;
199 }
200
202 {
203 int loaded = 0;
204
205 std::string directory;
206 {
207 std::shared_lock<std::shared_mutex> readLock(m_cacheMutex);
208 directory = m_directory;
209 }
210
211 for (const auto& entry : directory_iterator(directory))
212 {
213 if (entry.is_regular_file())
214 {
215 if (ArchiveExtractor::IsArchiveTypeSupported(entry.path().filename()))
216 {
217 try
218 {
219 auto pFileArchive = GetFileArchive(entry.path().stem().string());
220 if (pFileArchive != nullptr)
221 {
222 loaded++;
223 }
224 }
225 catch (std::exception& e)
226 {
227 // continue if we encounter an error loading one
228 logexception(e);
229 if (stopOnError) throw e;
230 }
231 }
232 }
233 }
234
235 return loaded;
236 }
237
238 int DesignCache::loadAllDesigns(bool stopOnError)
239 {
240 int loaded = 0;
241
242 std::string directory;
243 {
244 std::shared_lock<std::shared_mutex> readLock(m_cacheMutex);
245 directory = m_directory;
246 }
247
248 for (const auto& entry : directory_iterator(directory))
249 {
250 if (entry.is_regular_file())
251 {
252 if (ArchiveExtractor::IsArchiveTypeSupported(entry.path().filename()))
253 {
254 try
255 {
256 auto pDesign = GetDesign(entry.path().stem().string());
257 if (pDesign != nullptr)
258 {
259 loaded++;
260 }
261 }
262 catch (std::exception& e)
263 {
264 logexception(e);
265 if (stopOnError)
266 {
267 throw;
268 }
269 }
270 }
271 }
272 }
273
274 return loaded;
275 }
276
278 {
279 int loaded = 0;
280
281 for (const auto& name : names)
282 {
283 try
284 {
285 auto pFileArchive = GetFileArchive(name);
286 if (pFileArchive != nullptr)
287 {
288 loaded++;
289 }
290 }
291 catch (std::exception& e)
292 {
293 // continue on error
294 logexception(e);
295 }
296 }
297
298 return loaded;
299 }
300
302 {
303 int loaded = 0;
304
305 for (const auto& name : names)
306 {
307 try
308 {
309 auto pDesign = GetDesign(name);
310 if (pDesign != nullptr)
311 {
312 loaded++;
313 }
314 }
315 catch (std::exception& e)
316 {
317 // continue on error
318 logexception(e);
319 }
320 }
321
322 return loaded;
323 }
324
325 void DesignCache::setDirectory(const std::string& directory)
326 {
327 std::unique_lock<std::shared_mutex> writeLock(m_cacheMutex);
328 m_directory = directory;
329 }
330
331 std::string DesignCache::getDirectory() const
332 {
333 std::shared_lock<std::shared_mutex> readLock(m_cacheMutex);
334 return m_directory;
335 }
336
338 {
339 std::vector<std::string> knownDesigns;
340 {
341 std::unique_lock<std::shared_mutex> writeLock(m_cacheMutex);
342 // Collect before wiping: every design this cache held gets its
343 // generation tombstone bumped below, so a post-Clear reload can
344 // never land on a generation a GetDesignStream spool was published
345 // at before the Clear (the disk-load path itself does not bump).
346 knownDesigns.reserve(m_fileArchivesByName.size() + m_designsByName.size());
347 for (const auto& kv : m_fileArchivesByName)
348 {
349 knownDesigns.push_back(kv.first);
350 }
351 for (const auto& kv : m_designsByName)
352 {
353 knownDesigns.push_back(kv.first);
354 }
355 m_fileArchivesByName.clear();
356 m_designsByName.clear();
357 }
358 {
359 std::lock_guard<std::mutex> lruLock(m_lruMutex);
360 m_lruEntries.clear();
361 m_cachedBytes = 0;
362 }
363 {
364 // Drop all serialized response payloads. A pre-serialization commit
365 // in flight during this Clear() is discarded by its epoch check.
366 m_responseStore.Clear();
367 // ...and advance every held design's generation so pre-Clear
368 // GetDesignStream spools (keyed by generation) go stale.
369 m_responseStore.BumpGenerations(knownDesigns);
370 }
371 {
372 // Wipe the state map and bump the cache generation under the state
373 // lock. Loads that registered before this Clear() see the epoch change
374 // on completion and abandon their bookkeeping — no cache insert, no
375 // LRU charge, no stale state transition — instead of re-populating
376 // the just-wiped cache with a phantom entry. Their in-flight futures
377 // are left to their owning threads (which erase their own entries);
378 // parsed values are still delivered to the direct callers holding
379 // those futures.
380 std::lock_guard<std::mutex> stateLock(m_loadStateMutex);
381 m_loadStatesByName.clear();
382 ++m_epoch;
383 }
384 }
385
386 DesignCache::LoadState DesignCache::GetLoadState(const std::string& designName) const
387 {
388 std::lock_guard<std::mutex> stateLock(m_loadStateMutex);
389 auto stateIt = m_loadStatesByName.find(designName);
390 if (stateIt != m_loadStatesByName.end())
391 {
392 return stateIt->second;
393 }
394 return LoadState::Unloaded;
395 }
396
398 {
399 std::lock_guard<std::mutex> stateLock(m_loadStateMutex);
400 m_loadObservers.push_back(std::move(callback));
401 }
402
403 // Epoch snapshot comparison for Clear()-during-load detection; see m_epoch.
404 bool DesignCache::WasClearedSince(std::uint64_t epoch) const
405 {
406 std::lock_guard<std::mutex> stateLock(m_loadStateMutex);
407 return m_epoch != epoch;
408 }
409
410 void DesignCache::setCacheMaxBytes(std::uint64_t maxBytes)
411 {
412 std::vector<std::string> evicted;
413 {
414 std::lock_guard<std::mutex> lruLock(m_lruMutex);
415 m_maxBytes = maxBytes;
416 // No protected name: shrinking the budget evicts immediately
417 evicted = EvictOverBudgetLocked("");
418 }
419 EvictCacheEntries(evicted);
420 }
421
422 std::uint64_t DesignCache::getCacheMaxBytes() const
423 {
424 std::lock_guard<std::mutex> lruLock(m_lruMutex);
425 return m_maxBytes;
426 }
427
428 void DesignCache::setMaxBackgroundLoads(std::size_t maxLoads)
429 {
430 {
431 std::lock_guard<std::mutex> lock(m_backgroundMutex);
432 m_maxBackgroundLoads = maxLoads;
433 }
434 // A raised cap frees waiting loads immediately
435 m_backgroundSlotCv.notify_all();
436 }
437
439 {
440 std::lock_guard<std::mutex> lock(m_backgroundMutex);
441 return m_maxBackgroundLoads;
442 }
443
444 bool DesignCache::ContainsDesign(const std::string& designName) const
445 {
446 return !FindArchivePath(designName).empty();
447 }
448
449 bool DesignCache::LoadDesignAsync(const std::string& designName)
450 {
451 {
452 std::lock_guard<std::mutex> lock(m_loadStateMutex);
453 // A kick we issued earlier (queued or parsing) or a synchronous
454 // single-flight load in progress means already-loading; registering
455 // the pending marker here closes the window before the background
456 // thread announces Loading.
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())
460 {
461 return false;
462 }
463 m_pendingAsyncLoads.insert(designName);
464 }
465
466 {
467 std::lock_guard<std::mutex> lock(m_backgroundMutex);
468 ++m_outstandingBackgroundLoads;
469 }
470
471 try
472 {
473 // Detached by design (fire-and-forget): ~DesignCache drains
474 // m_outstandingBackgroundLoads so the thread never outlives members.
475 std::thread(&DesignCache::BackgroundLoad, this, designName).detach();
476 }
477 catch (...)
478 {
479 {
480 std::lock_guard<std::mutex> lock(m_backgroundMutex);
481 --m_outstandingBackgroundLoads;
482 }
483 // ~DesignCache's drain waits on the CV until the count reaches
484 // zero; a decrement without this notify is a missed wakeup that
485 // hangs shutdown forever when this was the last outstanding load
486 // (mirrors the normal BackgroundLoad exit).
487 m_backgroundDoneCv.notify_all();
488 {
489 std::lock_guard<std::mutex> lock(m_loadStateMutex);
490 m_pendingAsyncLoads.erase(designName);
491 }
492 throw;
493 }
494 return true;
495 }
496
497 void DesignCache::BackgroundLoad(const std::string& designName)
498 {
499 AcquireBackgroundSlot();
500 try
501 {
502 // The single-flight path registers the Loading/Loaded/Failed
503 // transitions and observers exactly as a synchronous load; a failed
504 // parse records Failed and drops the in-flight entry (retryable).
505 auto pDesign = GetDesign(designName);
506 if (pDesign == nullptr)
507 {
508 logwarn("Background load: no archive found for design \"" + designName + "\"");
509 }
510 else
511 {
512 loginfo("Background load of design \"" + designName + "\" completed");
513 }
514 }
515 catch (const std::exception& e)
516 {
517 logexception_msg(e, "Background load of design \"" + designName + "\" failed (retryable)");
518 }
519 catch (...)
520 {
521 logerror("Background load of design \"" + designName + "\" failed with an unknown exception (retryable)");
522 }
523 ReleaseBackgroundSlot();
524
525 {
526 std::lock_guard<std::mutex> lock(m_loadStateMutex);
527 m_pendingAsyncLoads.erase(designName);
528 }
529 {
530 std::lock_guard<std::mutex> lock(m_backgroundMutex);
531 --m_outstandingBackgroundLoads;
532 }
533 m_backgroundDoneCv.notify_all();
534 }
535
536 void DesignCache::AcquireBackgroundSlot()
537 {
538 std::unique_lock<std::mutex> lock(m_backgroundMutex);
539 m_backgroundSlotCv.wait(lock, [this]()
540 {
541 return m_maxBackgroundLoads == 0 || m_activeBackgroundLoads < m_maxBackgroundLoads;
542 });
543 ++m_activeBackgroundLoads;
544 }
545
546 void DesignCache::ReleaseBackgroundSlot()
547 {
548 {
549 std::lock_guard<std::mutex> lock(m_backgroundMutex);
550 --m_activeBackgroundLoads;
551 }
552 m_backgroundSlotCv.notify_one();
553 }
554
556 {
557 if (!std::filesystem::exists(m_directory))
558 {
559 // create directory
560 try
561 {
562 std::filesystem::create_directories(m_directory);
563 }
564 catch (const std::exception& e)
565 {
566 std::string msg = "Failed to create design cache directory: " + m_directory;
567 logexception_msg(e, msg);
568 throw e;
569 }
570 }
571 }
572
573 std::filesystem::path DesignCache::FindArchivePath(const std::string& designName) const
574 {
575 std::string directory;
576 {
577 std::shared_lock<std::shared_mutex> readLock(m_cacheMutex);
578 directory = m_directory;
579 }
580
581 std::error_code ec;
582 const auto options = directory_options::skip_permission_denied;
583 directory_iterator dirIt(directory, options, ec);
584 if (ec)
585 {
586 return {};
587 }
588
589 for (const auto& entry : dirIt)
590 {
591 if (entry.is_regular_file() && entry.path().stem() == designName)
592 {
593 return entry.path();
594 }
595 }
596
597 return {};
598 }
599
600 std::shared_ptr<ProductModel::Design> DesignCache::LoadDesign(const std::string& designName)
601 {
602 // NOTE: This is a lock-free I/O helper. It does NOT modify the cache maps.
603 // Cache insertion is handled by GetOrLoadSingleFlight().
604 if (FindArchivePath(designName).empty())
605 {
606 return nullptr;
607 }
608
609 // GetFileArchive is thread-safe, manages its own locking.
610 // updateState=false: the archive is a sub-step of this design load
611 // and must not flip the design's load state mid-load.
612 auto pFileModel = GetFileArchiveInternal(designName, false);
613 if (pFileModel == nullptr)
614 {
615 return nullptr;
616 }
617
618 auto pDesign = std::make_shared<ProductModel::Design>();
619 if (pDesign->Build(pFileModel))
620 {
621 return pDesign;
622 }
623
624 return nullptr;
625 }
626
627 std::shared_ptr<FileModel::Design::FileArchive> DesignCache::LoadFileArchive(const std::string& designName)
628 {
629 // NOTE: This is a lock-free I/O helper. It does NOT modify the cache maps.
630 // Cache insertion is handled by GetOrLoadSingleFlight().
631 const auto archivePath = FindArchivePath(designName);
632 if (archivePath.empty())
633 {
634 logwarn("Failed to find file for design \"" + designName + "\"");
635
636 logdebug("Listing all files in directory: " + getDirectory());
637 const auto options = directory_options::skip_permission_denied;
638 std::error_code ec;
639 for (const auto& entry : directory_iterator(getDirectory(), options, ec))
640 {
641 if (entry.is_regular_file())
642 {
643 logdebug("Found file: " + entry.path().filename().string() + " (stem: " + entry.path().stem().string() + ")");
644 }
645 }
646
647 return nullptr;
648 }
649
650 loginfo("file found: [" + archivePath.string() + "], attempting to parse...");
651
652 auto pFileArchive = std::make_shared<FileModel::Design::FileArchive>(archivePath.string());
653 if (pFileArchive->ParseFileModel())
654 {
655 return pFileArchive;
656 }
657
658 // A matching file was found but failed to extract or parse. Treat this
659 // as a corrupt/unloadable design (error) rather than a missing one, so
660 // callers surface INTERNAL/500 instead of a misleading NOT_FOUND/404.
661 // (ParseFileModel already throws on parse failure; this covers the
662 // extraction / missing-root-dir paths that return false.)
663 // The file path is intentionally omitted here to avoid leaking server
664 // filesystem details to API clients (it is logged above for diagnostics).
665 throw std::runtime_error(
666 "Failed to load design \"" + designName + "\": could not extract or parse archive");
667 }
668
669 void DesignCache::TouchLru(const std::string& designName)
670 {
671 std::lock_guard<std::mutex> lruLock(m_lruMutex);
672 auto findIt = m_lruEntries.find(designName);
673 if (findIt != m_lruEntries.end())
674 {
675 findIt->second.lastServed = std::chrono::steady_clock::now();
676 }
677 // No entry: the name raced with eviction, was never charged, or belonged
678 // to a load abandoned across Clear(). Touch is a no-op for absent names —
679 // it never inserts — so a joiner of an abandoned load cannot phantom-charge
680 // the byte budget for an uncached name.
681 }
682
683 void DesignCache::InsertLruAndEvict(const std::string& designName, std::uint64_t injectedBytes)
684 {
685 const auto now = std::chrono::steady_clock::now();
686 const auto bytes = EstimateDesignBytes(designName, injectedBytes);
687
688 std::vector<std::string> evicted;
689 {
690 std::lock_guard<std::mutex> lruLock(m_lruMutex);
691 auto findIt = m_lruEntries.find(designName);
692 if (findIt != m_lruEntries.end())
693 {
694 findIt->second.lastServed = now;
695 // Re-injection over an existing entry (e.g. POST /filemodels
696 // overwriting a save=false design): refresh the charge to the
697 // new estimate instead of silently keeping the previous
698 // injection's bytes. The payload term is excluded —
699 // serialized payload bytes are charged separately via
700 // LruEntry::serializedBytes, and counting them here too would
701 // over-subtract at eviction. (m_lruMutex -> store mutex is a
702 // permitted nesting; see the lock-order note above.)
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);
707 }
708 else
709 {
710 m_lruEntries.emplace(designName, LruEntry{bytes, 0, now});
711 m_cachedBytes += bytes;
712 }
713 evicted = EvictOverBudgetLocked(designName);
714 }
715 EvictCacheEntries(evicted);
716 }
717
718 // Requires m_lruMutex held. May acquire m_loadStateMutex (the one permitted
719 // nesting) to skip in-flight (Loading) designs. Returns evicted names; cache-map
720 // and state cleanup happens in EvictCacheEntries after m_lruMutex is released.
721 std::vector<std::string> DesignCache::EvictOverBudgetLocked(const std::string& protectedName)
722 {
723 std::vector<std::string> evicted;
724
725 if (m_maxBytes == 0)
726 {
727 // Budget of 0 disables eviction
728 return evicted;
729 }
730
731 while (m_cachedBytes > m_maxBytes)
732 {
733 auto victimIt = m_lruEntries.end();
734 auto oldest = std::chrono::steady_clock::time_point::max();
735
736 for (auto it = m_lruEntries.begin(); it != m_lruEntries.end(); ++it)
737 {
738 if (it->first == protectedName) continue; // never evict the entry being served
739 if (it->second.lastServed >= oldest) continue; // least-recently-served first
740 {
741 std::lock_guard<std::mutex> stateLock(m_loadStateMutex);
742 auto stateIt = m_loadStatesByName.find(it->first);
743 if (stateIt != m_loadStatesByName.end() && stateIt->second == LoadState::Loading)
744 {
745 continue; // never evict an in-flight design
746 }
747 }
748 oldest = it->second.lastServed;
749 victimIt = it;
750 }
751
752 if (victimIt == m_lruEntries.end())
753 {
754 // Nothing evictable (only the protected/in-flight entries remain);
755 // over-budget is tolerated rather than dropping protected work.
756 break;
757 }
758
759 // Uncharge the victim's FULL contribution: the archive/Design
760 // estimate plus the serialized response payloads committed on top
761 // of it (CommitResponsePayloads charges both to m_cachedBytes).
762 // Stranding the payload half would permanently inflate the byte
763 // count every time a warm design is evicted.
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);
768 }
769
770 return evicted;
771 }
772
773 void DesignCache::EvictCacheEntries(const std::vector<std::string>& names)
774 {
775 for (const auto& name : names)
776 {
777 // Drop the serialized response payloads (generation bump keeps a
778 // commit that raced the eviction from re-publishing them). Their
779 // bytes were uncharged together with the estimate in
780 // EvictOverBudgetLocked — the entry (estimate + serialized charge)
781 // was removed as a whole there.
782 m_responseStore.Invalidate(name);
783
784 {
785 std::unique_lock<std::shared_mutex> writeLock(m_cacheMutex);
786 m_designsByName.erase(name);
787 m_fileArchivesByName.erase(name);
788 }
789
790 loginfo("Evicted design \"" + name + "\" from cache (over byte budget)");
791
792 TransitionLoadState(name, LoadState::Unloaded);
793 }
794 }
795
796 std::uint64_t DesignCache::EstimateDesignBytes(const std::string& designName, std::uint64_t injectedBytes) const
797 {
798 // Estimate: archive file size on disk + a small constant + the bytes
799 // currently held for the design's cached serialized response payloads
800 // (M1.4). The payload term is zero for a fresh entry — payload bytes
801 // are charged incrementally at commit time (CommitResponsePayloads) —
802 // but a re-insert while payloads exist stays honest. injectedBytes
803 // covers in-memory-only archives (AddFileArchive with save=false):
804 // nothing on disk backs the name, so the request-body size stands in
805 // for the archive footprint.
806 const auto archivePath = FindArchivePath(designName);
807 if (archivePath.empty())
808 {
809 return DESIGN_BYTES_OVERHEAD + m_responseStore.StoredBytes(designName) + injectedBytes;
810 }
811
812 std::error_code ec;
813 const auto size = std::filesystem::file_size(archivePath, ec);
814 if (ec)
815 {
816 return DESIGN_BYTES_OVERHEAD + m_responseStore.StoredBytes(designName) + injectedBytes;
817 }
818 return static_cast<std::uint64_t>(size) + DESIGN_BYTES_OVERHEAD + m_responseStore.StoredBytes(designName);
819 }
820
821 void DesignCache::ResyncLruEstimate(const std::string& designName)
822 {
823 // Estimate outside m_lruMutex (directory scan + store lookup), the
824 // same pattern InsertLruAndEvict uses, then apply the delta under it.
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())
829 {
830 return;
831 }
832 // Exclude the payload term: serialized payload bytes are charged via
833 // LruEntry::serializedBytes, and including them in estimatedBytes too
834 // would double-count at eviction. (Safe today either way — the only
835 // caller runs right after an invalidation emptied the store — but the
836 // exclusion keeps the invariant local instead of call-site-dependent.)
837 const auto base = bytes - m_responseStore.StoredBytes(designName);
838 // Signed delta: the file may be smaller than the injected estimate
839 // the entry was charged with at insert time.
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);
843 }
844
845 std::uint64_t DesignCache::GetResponseGeneration(const std::string& designName) const
846 {
847 return m_responseStore.CurrentGeneration(designName);
848 }
849
850 std::uint64_t DesignCache::cachedBytes() const
851 {
852 std::lock_guard<std::mutex> lruLock(m_lruMutex);
853 return m_cachedBytes;
854 }
855
856 void DesignCache::TransitionLoadState(const std::string& designName, LoadState to)
857 {
858 LoadObserverList observers;
859 auto from = LoadState::Unloaded;
860 {
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;
864 if (from == to)
865 {
866 // Not a transition (e.g. an archive load under an already-Loading
867 // design, or a duplicate completion) — no observer event.
868 return;
869 }
870 m_loadStatesByName[designName] = to;
871 observers = m_loadObservers;
872 }
873 NotifyLoadObservers(designName, from, to, observers);
874 }
875
876 // Removes the design's state entry and reports the revert to Unloaded. If no
877 // entry exists (nothing was recorded, or Clear() already wiped it) this is a
878 // no-op: the name is already at the implicit Unloaded, so no event is due.
879 void DesignCache::EraseLoadState(const std::string& designName)
880 {
881 LoadObserverList observers;
882 auto from = LoadState::Unloaded;
883 {
884 std::lock_guard<std::mutex> stateLock(m_loadStateMutex);
885 auto stateIt = m_loadStatesByName.find(designName);
886 if (stateIt == m_loadStatesByName.end())
887 {
888 return;
889 }
890 from = stateIt->second;
891 m_loadStatesByName.erase(stateIt);
892 observers = m_loadObservers;
893 }
894 if (from != LoadState::Unloaded)
895 {
896 NotifyLoadObservers(designName, from, LoadState::Unloaded, observers);
897 }
898 }
899
900 // Observers run with no locks held (they may re-enter the cache) and must be
901 // cheap/non-blocking; a throwing observer is logged and skipped.
902 void DesignCache::NotifyLoadObservers(const std::string& designName, LoadState from, LoadState to, const LoadObserverList& observers)
903 {
904 for (const auto& observer : observers)
905 {
906 try
907 {
908 observer(designName, from, to);
909 }
910 catch (const std::exception& e)
911 {
912 logwarn(std::string("Design load observer threw: ") + e.what());
913 }
914 catch (...)
915 {
916 logwarn("Design load observer threw an unknown exception");
917 }
918 }
919 }
920
921 // ---- serialized-response cache (M1.4) ----
922
923 bool DesignCache::ResponseStore::TryGetDesignBytes(const std::string& designName, std::string& outBytes) const
924 {
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())
928 {
929 return false;
930 }
931 outBytes = findIt->second.payload.designPbBytes;
932 return true;
933 }
934
935 bool DesignCache::ResponseStore::TryGetJsonPayload(const std::string& designName, JsonPayloadKind kind, std::string& outJson) const
936 {
937 std::lock_guard<std::mutex> lock(m_mutex);
938 auto findIt = m_entriesByName.find(designName);
939 if (findIt == m_entriesByName.end())
940 {
941 return false;
942 }
943 const auto& json = (kind == JsonPayloadKind::Design)
944 ? findIt->second.payload.designJson
945 : findIt->second.payload.fileModelJson;
946 if (json.empty())
947 {
948 return false;
949 }
950 outJson = json;
951 return true;
952 }
953
954 bool DesignCache::ResponseStore::HasEntry(const std::string& designName) const
955 {
956 std::lock_guard<std::mutex> lock(m_mutex);
957 return m_entriesByName.find(designName) != m_entriesByName.end();
958 }
959
960 std::uint64_t DesignCache::ResponseStore::StoredBytes(const std::string& designName) const
961 {
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;
965 }
966
967 std::uint64_t DesignCache::ResponseStore::TotalStoredBytes() const
968 {
969 std::lock_guard<std::mutex> lock(m_mutex);
970 std::uint64_t total = 0;
971 for (const auto& kv : m_entriesByName)
972 {
973 total += kv.second.payload.totalBytes();
974 }
975 return total;
976 }
977
978 std::uint64_t DesignCache::ResponseStore::CurrentGeneration(const std::string& designName) const
979 {
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;
983 }
984
985 DesignCache::ResponseStore::StoreResult DesignCache::ResponseStore::TryStore(
986 const std::string& designName, std::uint64_t generation, ResponsePayload&& payload)
987 {
988 std::lock_guard<std::mutex> lock(m_mutex);
989
990 // Validate against the generation tombstone: any invalidation since the
991 // committer captured its generation bumps past it and loses the race.
992 auto genIt = m_generationsByName.find(designName);
993 const auto currentGeneration = genIt != m_generationsByName.end() ? genIt->second : 0;
994 if (generation != currentGeneration)
995 {
996 StoreResult lost;
997 lost.stored = false;
998 return lost;
999 }
1000
1001 auto findIt = m_entriesByName.find(designName);
1002 if (findIt != m_entriesByName.end())
1003 {
1004 // Already stored for this generation (a racing background/lazy
1005 // committer won): report success without re-storing so the
1006 // serialization counter stays exact — one per design per generation.
1007 StoreResult result;
1008 result.stored = true;
1009 result.storedBytes = findIt->second.payload.totalBytes();
1010 return result;
1011 }
1012
1013 auto& entry = m_entriesByName[designName];
1014 entry.payload = std::move(payload);
1015 ++m_designSerializations;
1016
1017 StoreResult result;
1018 result.stored = true;
1019 result.storedBytes = entry.payload.totalBytes();
1020 return result;
1021 }
1022
1023 std::uint64_t DesignCache::ResponseStore::Invalidate(const std::string& designName)
1024 {
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())
1029 {
1030 dropped = findIt->second.payload.totalBytes();
1031 m_entriesByName.erase(findIt);
1032 }
1033 // Bump the tombstone even when no entry existed: a commit that captured
1034 // the pre-invalidation generation must lose its TryStore.
1035 ++m_generationsByName[designName];
1036 return dropped;
1037 }
1038
1039 void DesignCache::ResponseStore::BumpGenerations(const std::vector<std::string>& designNames)
1040 {
1041 std::lock_guard<std::mutex> lock(m_mutex);
1042 for (const auto& name : designNames)
1043 {
1044 // Creates the tombstone when absent (a loaded-but-never-invalidated
1045 // design sits at generation 0 with no entry here).
1046 ++m_generationsByName[name];
1047 }
1048 }
1049
1050 void DesignCache::ResponseStore::Clear()
1051 {
1052 std::lock_guard<std::mutex> lock(m_mutex);
1053 m_entriesByName.clear();
1054 // Generation tombstones are intentionally PRESERVED (and the cache's
1055 // Clear() additionally bumps one for every design it held, via
1056 // BumpGenerations). The disk-load path does NOT bump generations —
1057 // only re-upload (AddFileArchive), InvalidateDesign and eviction do —
1058 // so erasing the tombstones (or leaving them un-bumped) would let a
1059 // post-Clear reload land on the same generation a GetDesignStream
1060 // spool was published at before the Clear, warm-serving content the
1061 // system had invalidated.
1062 // Counter intentionally NOT reset: it counts work performed, not work
1063 // currently cached (tests read it as "how many serializations happened").
1064 }
1065
1066 std::uint64_t DesignCache::ResponseStore::designSerializations() const
1067 {
1068 std::lock_guard<std::mutex> lock(m_mutex);
1069 return m_designSerializations;
1070 }
1071
1072 // ---- DesignCache-level response-cache API ----
1073
1074 bool DesignCache::TryGetDesignBytes(const std::string& designName, std::string& outBytes) const
1075 {
1076 return m_responseStore.TryGetDesignBytes(designName, outBytes);
1077 }
1078
1079 bool DesignCache::TryGetJsonPayload(const std::string& designName, JsonPayloadKind kind, std::string& outJson) const
1080 {
1081 return m_responseStore.TryGetJsonPayload(designName, kind, outJson);
1082 }
1083
1084 bool DesignCache::HasCachedResponse(const std::string& designName) const
1085 {
1086 return m_responseStore.HasEntry(designName);
1087 }
1088
1090 {
1091 return m_responseStore.designSerializations();
1092 }
1093
1095 {
1096 return m_responseStore.TotalStoredBytes();
1097 }
1098
1099 void DesignCache::setMonolithicWireBudget(std::uint64_t maxBytes)
1100 {
1101 std::lock_guard<std::mutex> lruLock(m_lruMutex);
1102 m_monolithicWireBudget = maxBytes;
1103 }
1104
1106 {
1107 std::lock_guard<std::mutex> lruLock(m_lruMutex);
1108 return m_monolithicWireBudget;
1109 }
1110
1111 std::uint64_t DesignCache::CurrentEpoch() const
1112 {
1113 std::lock_guard<std::mutex> stateLock(m_loadStateMutex);
1114 return m_epoch;
1115 }
1116
1117 DesignCache::ResponsePayload DesignCache::BuildResponsePayload(const ProductModel::Design& design)
1118 {
1119 ResponsePayload payload;
1120
1121 // M6 monolithic serve budget: a design whose estimated serialized
1122 // size exceeds the budget must not have its protobuf tree built here
1123 // (the full-tree build + serialized copy is the 10 GiB-pod OOM, and
1124 // this worker runs unprompted at load completion). It stays
1125 // un-cached; the chunked GetDesignStream RPC serves it, spooling to
1126 // disk instead of RAM. Budget 0 = disabled (pre-M6 behavior).
1127 {
1128 std::lock_guard<std::mutex> lruLock(m_lruMutex);
1129 if (m_monolithicWireBudget != 0 &&
1130 design.EstimateWireSizeBytes(/*includeNormalizedLists=*/false) > m_monolithicWireBudget)
1131 {
1132 loginfo("Skipping monolithic response pre-serialization (over serve budget); design is served via GetDesignStream");
1133 return payload;
1134 }
1135 }
1136
1137 // One protobuf tree build serves both the wire bytes and the JSON.
1138 // These bytes back the gRPC GetDesign fast path, which serves the
1139 // DEFAULT (pruned) flavor — serialize that flavor here explicitly.
1140 // (include_normalized_lists requests are served cold from the live
1141 // design; REST/JSON consumers call the full-flavor no-arg
1142 // to_protobuf() on the cached Design object themselves.)
1143 auto pMessage = design.to_protobuf(/*includeNormalizedLists=*/false);
1144 if (pMessage != nullptr)
1145 {
1146 if (!pMessage->SerializeToString(&payload.designPbBytes))
1147 {
1148 payload.designPbBytes.clear();
1149 }
1150
1151 }
1152
1153 // JSON payloads (designJson/fileModelJson) are deliberately NOT built
1154 // here: their REST consumers arrive with the controller-wiring
1155 // follow-up, and eagerly building two full-JSON strings per design
1156 // alongside the protobuf tree was a major memory-thrash contributor
1157 // on large designs. TryGetJsonPayload returns false until that
1158 // follow-up adds (lazy) JSON population.
1159 return payload;
1160 }
1161
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)
1167 {
1168 if (pDesign == nullptr || payload.totalBytes() == 0)
1169 {
1170 return;
1171 }
1172
1173 // Design-object identity: the payloads are valid only for the Design
1174 // object they were built from. Eviction, Clear(), and AddFileArchive
1175 // remove or replace the cached Design; a mismatch (shared_ptr identity,
1176 // so no address-reuse ABA) aborts the commit.
1177 {
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)
1181 {
1182 return;
1183 }
1184 }
1185
1186 std::vector<std::string> evicted;
1187 {
1188 std::lock_guard<std::mutex> lruLock(m_lruMutex);
1189 auto entryIt = m_lruEntries.find(designName);
1190 if (entryIt == m_lruEntries.end())
1191 {
1192 return; // evicted while serializing
1193 }
1194 if (WasClearedSince(epochAtStart))
1195 {
1196 return; // Clear() ran since the load/serialization started
1197 }
1198
1199 // Budget re-check at COMMIT time: BuildResponsePayload checked at
1200 // serialization START; a worker that began under a higher (or
1201 // disabled) budget must not publish over-budget bytes after the
1202 // budget was lowered.
1203 //
1204 // Note the two gates compare different sizes: the start gate uses
1205 // the EstimateWireSizeBytes estimate, this gate the actual payload.
1206 // For a design in the estimate < budget < actual band, the tree is
1207 // built and serialized but never committed, so every fetch repeats
1208 // that work (transiently re-creating the memory spike the budget
1209 // exists to prevent). Accepted tradeoff: this gate exists to stop
1210 // budgets lowered mid-flight from being violated; estimate-miss
1211 // designs simply never get a monolithic in-RAM response and are
1212 // served by GetDesignStream + spool instead.
1213 if (m_monolithicWireBudget != 0 && payload.totalBytes() > m_monolithicWireBudget)
1214 {
1215 return; // over the (now lower) monolithic serve budget
1216 }
1217
1218 const auto storeResult = m_responseStore.TryStore(designName, generation, std::move(payload));
1219 if (storeResult.stored)
1220 {
1221 // Resync the entry's serialized charge with the store. An
1222 // invalidation racing between TryStore and StoredBytes shows up
1223 // here as a smaller/zero reading, which is exactly the right
1224 // charge for the store's current contents.
1225 const auto delta = storeResult.storedBytes - entryIt->second.serializedBytes;
1226 entryIt->second.serializedBytes = storeResult.storedBytes;
1227 m_cachedBytes += delta;
1228 }
1229
1230 evicted = EvictOverBudgetLocked(designName);
1231 }
1232 EvictCacheEntries(evicted);
1233 }
1234
1235 void DesignCache::ScheduleResponsePreSerialization(const std::string& designName,
1236 std::shared_ptr<ProductModel::Design> pDesign,
1237 std::uint64_t epochAtStart)
1238 {
1239 const auto generation = m_responseStore.CurrentGeneration(designName);
1240
1241 // Counted + drained exactly like background loads so detached threads
1242 // never outlive the cache members they touch (~DesignCache waits on
1243 // m_outstandingBackgroundLoads).
1244 {
1245 std::lock_guard<std::mutex> lock(m_backgroundMutex);
1246 ++m_outstandingBackgroundLoads;
1247 }
1248
1249 try
1250 {
1251 std::thread(&DesignCache::PreSerializeWorker, this, designName, std::move(pDesign),
1252 epochAtStart, generation).detach();
1253 }
1254 catch (const std::exception& e)
1255 {
1256 {
1257 std::lock_guard<std::mutex> lock(m_backgroundMutex);
1258 --m_outstandingBackgroundLoads;
1259 }
1260 // Missed-wakeup guard, identical to LoadDesignAsync's catch: the
1261 // destructor drains on the CV.
1262 m_backgroundDoneCv.notify_all();
1263 // A failed pre-serialization schedule must not fail the load that
1264 // just succeeded; the first fetch repopulates lazily.
1265 logwarn("Failed to schedule response pre-serialization for design \"" + designName + "\": " + e.what());
1266 }
1267 }
1268
1269 void DesignCache::PreSerializeWorker(std::string designName,
1270 std::shared_ptr<ProductModel::Design> pDesign,
1271 std::uint64_t epochAtStart,
1272 std::uint64_t generation)
1273 {
1274 // Shares the background-load semaphore: serialization competes with
1275 // parses for --max-background-loads slots, bounding memory spikes.
1276 AcquireBackgroundSlot();
1277 try
1278 {
1279 auto payload = BuildResponsePayload(*pDesign);
1280 CommitResponsePayloads(designName, epochAtStart, pDesign, std::move(payload), generation);
1281 logdebug("Response pre-serialization for design \"" + designName + "\" committed");
1282 }
1283 catch (const std::exception& e)
1284 {
1285 logwarn("Response pre-serialization for design \"" + designName + "\" failed: " + e.what());
1286 }
1287 catch (...)
1288 {
1289 logwarn("Response pre-serialization for design \"" + designName + "\" failed with an unknown exception");
1290 }
1291 ReleaseBackgroundSlot();
1292
1293 {
1294 std::lock_guard<std::mutex> lock(m_backgroundMutex);
1295 --m_outstandingBackgroundLoads;
1296 }
1297 m_backgroundDoneCv.notify_all();
1298 }
1299
1300 void DesignCache::InvalidateResponsePayloads(const std::string& designName)
1301 {
1302 m_responseStore.Invalidate(designName);
1303 AdjustLruSerializedBytes(designName);
1304 }
1305
1306 void DesignCache::AdjustLruSerializedBytes(const std::string& designName)
1307 {
1308 std::lock_guard<std::mutex> lruLock(m_lruMutex);
1309 auto entryIt = m_lruEntries.find(designName);
1310 if (entryIt == m_lruEntries.end())
1311 {
1312 return;
1313 }
1314 const auto stored = m_responseStore.StoredBytes(designName);
1315 if (stored != entryIt->second.serializedBytes)
1316 {
1317 m_cachedBytes -= entryIt->second.serializedBytes - stored;
1318 entryIt->second.serializedBytes = stored;
1319 }
1320 }
1321
1322 void DesignCache::LogFailedStateRecordError(const std::string& designName)
1323 {
1324 // Lives out-of-line because the calling template is in the header,
1325 // which does not include Logger.h.
1326 logerror("Failed to record Failed load-state for design \"" + designName + "\" during load-failure cleanup");
1327 }
1328
1329 void DesignCache::SerializeResponsePayloads(const std::string& designName)
1330 {
1331 std::shared_ptr<ProductModel::Design> pDesign;
1332 {
1333 std::shared_lock<std::shared_mutex> readLock(m_cacheMutex);
1334 auto findIt = m_designsByName.find(designName);
1335 if (findIt == m_designsByName.end())
1336 {
1337 return;
1338 }
1339 pDesign = findIt->second;
1340 }
1341
1342 const auto generation = m_responseStore.CurrentGeneration(designName);
1343 const auto epoch = CurrentEpoch();
1344
1345 try
1346 {
1347 auto payload = BuildResponsePayload(*pDesign);
1348 CommitResponsePayloads(designName, epoch, pDesign, std::move(payload), generation);
1349 }
1350 catch (const std::exception& e)
1351 {
1352 logwarn("Lazy response serialization for design \"" + designName + "\" failed: " + e.what());
1353 }
1354 catch (...)
1355 {
1356 logwarn("Lazy response serialization for design \"" + designName + "\" failed with an unknown exception");
1357 }
1358 }
1359}
#define logexception_msg(e, m)
Definition Logger.h:29
#define logerror(s)
Definition Logger.h:17
#define logexception(e)
Definition Logger.h:26
#define logwarn(s)
Definition Logger.h:20
#define loginfo(s)
Definition Logger.h:14
#define logdebug(s)
Definition Logger.h:23
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
Definition DesignCache.h:47
DesignCache(std::string directory)
void SerializeResponsePayloads(const std::string &designName)
void setCacheMaxBytes(std::uint64_t maxBytes)
std::uint64_t getCacheMaxBytes() const
std::vector< std::string > getLoadedDesignNames(const std::string &filter="") const
std::uint64_t EstimateWireSizeBytes(bool includeNormalizedLists=false) const
Definition Design.cpp:470
std::unique_ptr< Protobuf::ProductModel::Design > to_protobuf() const override
Definition Design.cpp:160
static bool IsArchiveTypeSupported(const std::filesystem::path &file)
std::vector< std::string > StringVector
Definition StringVector.h:8