MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
RegionCacheManager.cpp
Go to the documentation of this file.
2
4
5namespace MayaFlux::Kakshya {
6
7std::size_t RegionHash::operator()(const Region& region) const
8{
9 std::size_t h1 = 0;
10 std::size_t h2 = 0;
11
12 for (const auto& coord : region.start_coordinates) {
13 h1 ^= std::hash<uint64_t> {}(coord) + 0x9e3779b9 + (h1 << 6) + (h1 >> 2);
14 }
15
16 for (const auto& coord : region.end_coordinates) {
17 h2 ^= std::hash<uint64_t> {}(coord) + 0x9e3779b9 + (h2 << 6) + (h2 >> 2);
18 }
19
20 return h1 ^ (h2 << 1);
21}
22
24 : m_max_cache_size(max_size)
25{
26}
27
29{
30 std::lock_guard<std::recursive_mutex> lock(m_mutex);
31 const Region& region = cache.source_region;
32
33 auto it = m_cache.find(region);
34 if (it != m_cache.end()) {
35 it->second = cache;
36 update_lru(region);
37 } else {
39 m_cache[region] = cache;
40 m_lru_list.push_front(region);
41 }
42}
43
45{
46 if (segment.is_cached) {
47 cache_region(segment.cache);
48 }
49}
50
51std::optional<RegionCache> RegionCacheManager::get_cached_region(const Region& region)
52{
53 if (!m_initialized) {
54 return std::nullopt;
55 }
56
57 try {
58 std::lock_guard<std::recursive_mutex> lock(m_mutex, std::adopt_lock);
59
60 auto it = m_cache.find(region);
61 if (it != m_cache.end()) {
62 update_lru(region);
63 it->second.mark_accessed();
64 return it->second;
65 }
66 return std::nullopt;
67 } catch (const std::exception& e) {
68 MF_ERROR(Journal::Component::Kakshya, Journal::Context::Runtime, "Exception in get_cached_region: {}", e.what());
69 return std::nullopt;
70 } catch (...) {
71 MF_ERROR(Journal::Component::Kakshya, Journal::Context::Runtime, "Unknown exception in get_cached_region");
72 return std::nullopt;
73 }
74}
75
76std::optional<RegionCache> RegionCacheManager::get_cached_segment(const RegionSegment& segment)
77{
78 if (!m_initialized) {
79 return std::nullopt;
80 }
81 try {
82 std::unique_lock<std::recursive_mutex> lock(m_mutex, std::try_to_lock);
83
84 if (!lock.owns_lock()) {
85 MF_WARN(Journal::Component::Kakshya, Journal::Context::Runtime, "Could not acquire mutex lock in get_cached_segment, potential deadlock avoided");
86 return std::nullopt;
87 }
88
90 } catch (const std::exception& e) {
91 MF_ERROR(Journal::Component::Kakshya, Journal::Context::Runtime, "Exception in get_cached_segment: {}", e.what());
92 return std::nullopt;
93 } catch (...) {
94 MF_ERROR(Journal::Component::Kakshya, Journal::Context::Runtime, "Unknown exception in get_cached_segment");
95 return std::nullopt;
96 }
97}
98
99std::optional<RegionSegment> RegionCacheManager::get_segment_with_cache(const RegionSegment& segment)
100{
101 auto cache_opt = get_cached_region(segment.source_region);
102 if (cache_opt) {
103 RegionSegment seg = segment;
104 seg.cache = *cache_opt;
105 seg.is_cached = true;
106 return seg;
107 }
108 return std::nullopt;
109}
110
111std::optional<RegionCache> RegionCacheManager::get_cached_region_internal(const Region& region)
112{
113 auto it = m_cache.find(region);
114 if (it != m_cache.end()) {
115 update_lru(region);
116 it->second.mark_accessed();
117 return it->second;
118 }
119 return std::nullopt;
120}
121
123{
124 m_cache.clear();
125 m_lru_list.clear();
126 m_current_size = 0;
127}
128
129size_t RegionCacheManager::size() const { return m_cache.size(); }
131
133{
134 while (m_cache.size() >= m_max_cache_size && !m_lru_list.empty()) {
135 auto last = m_lru_list.back();
136 m_cache.erase(last);
137 m_lru_list.pop_back();
138 }
139}
140
142{
143 auto it = std::ranges::find(m_lru_list, region);
144 if (it != m_lru_list.end()) {
145 m_lru_list.erase(it);
146 }
147 m_lru_list.push_front(region);
148}
149
150}
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
std::optional< RegionCache > get_cached_segment(const RegionSegment &segment)
std::optional< RegionSegment > get_segment_with_cache(const RegionSegment &segment)
void cache_segment(const RegionSegment &segment)
std::optional< RegionCache > get_cached_region(const Region &region)
void cache_region(const RegionCache &cache)
std::unordered_map< Region, RegionCache, RegionHash > m_cache
std::optional< RegionCache > get_cached_region_internal(const Region &region)
@ Runtime
General runtime operations (default fallback)
@ Kakshya
Containers[Signalsource, Stream, File], Regions, DataProcessors.
Region source_region
Region this cache corresponds to.
Stores cached data for a region, with metadata for cache management.
std::size_t operator()(const Region &region) const
bool is_cached
Flag indicating if data is cached.
Region source_region
Associated region.
RegionCache cache
Multi-channel cached audio data.
Represents a discrete segment of audio data with caching capabilities.
std::vector< uint64_t > end_coordinates
Ending frame index (inclusive)
Definition Region.hpp:78
std::vector< uint64_t > start_coordinates
Starting frame index (inclusive)
Definition Region.hpp:75
Represents a point or span in N-dimensional space.
Definition Region.hpp:73