MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
RegionCacheManager.cpp
Go to the documentation of this file.
2
3namespace MayaFlux::Kakshya {
4
5std::size_t RegionHash::operator()(const Region& region) const
6{
7 std::size_t h1 = 0;
8 std::size_t h2 = 0;
9
10 for (const auto& coord : region.start_coordinates) {
11 h1 ^= std::hash<uint64_t> {}(coord) + 0x9e3779b9 + (h1 << 6) + (h1 >> 2);
12 }
13
14 for (const auto& coord : region.end_coordinates) {
15 h2 ^= std::hash<uint64_t> {}(coord) + 0x9e3779b9 + (h2 << 6) + (h2 >> 2);
16 }
17
18 return h1 ^ (h2 << 1);
19}
20
22 : m_max_cache_size(max_size)
23{
24}
25
27{
28 std::lock_guard<std::recursive_mutex> lock(m_mutex);
29 const Region& region = cache.source_region;
30
31 auto it = m_cache.find(region);
32 if (it != m_cache.end()) {
33 it->second = cache;
34 update_lru(region);
35 } else {
37 m_cache[region] = cache;
38 m_lru_list.push_front(region);
39 }
40}
41
43{
44 if (segment.is_cached) {
45 cache_region(segment.cache);
46 }
47}
48
49std::optional<RegionCache> RegionCacheManager::get_cached_region(const Region& region)
50{
51 if (!m_initialized) {
52 return std::nullopt;
53 }
54
55 try {
56 std::lock_guard<std::recursive_mutex> lock(m_mutex, std::adopt_lock);
57
58 auto it = m_cache.find(region);
59 if (it != m_cache.end()) {
60 update_lru(region);
61 it->second.mark_accessed();
62 return it->second;
63 }
64 return std::nullopt;
65 } catch (const std::exception& e) {
66 std::cerr << "Exception in get_cached_region: " << e.what() << '\n';
67 return std::nullopt;
68 } catch (...) {
69 std::cerr << "Unknown exception in get_cached_region" << '\n';
70 return std::nullopt;
71 }
72}
73
74std::optional<RegionCache> RegionCacheManager::get_cached_segment(const RegionSegment& segment)
75{
76 if (!m_initialized) {
77 return std::nullopt;
78 }
79 try {
80 std::unique_lock<std::recursive_mutex> lock(m_mutex, std::try_to_lock);
81
82 if (!lock.owns_lock()) {
83 std::cerr << "Warning: Could not acquire mutex lock in get_cached_segment, potential deadlock avoided" << '\n';
84 return std::nullopt;
85 }
86
88 } catch (const std::exception& e) {
89 std::cerr << "Exception in get_cached_segment: " << e.what() << '\n';
90 return std::nullopt;
91 } catch (...) {
92 std::cerr << "Unknown exception in get_cached_segment" << '\n';
93 return std::nullopt;
94 }
95}
96
97std::optional<RegionSegment> RegionCacheManager::get_segment_with_cache(const RegionSegment& segment)
98{
99 auto cache_opt = get_cached_region(segment.source_region);
100 if (cache_opt) {
101 RegionSegment seg = segment;
102 seg.cache = *cache_opt;
103 seg.is_cached = true;
104 return seg;
105 }
106 return std::nullopt;
107}
108
109std::optional<RegionCache> RegionCacheManager::get_cached_region_internal(const Region& region)
110{
111 auto it = m_cache.find(region);
112 if (it != m_cache.end()) {
113 update_lru(region);
114 it->second.mark_accessed();
115 return it->second;
116 }
117 return std::nullopt;
118}
119
121{
122 m_cache.clear();
123 m_lru_list.clear();
124 m_current_size = 0;
125}
126
127size_t RegionCacheManager::size() const { return m_cache.size(); }
129
131{
132 while (m_cache.size() >= m_max_cache_size && !m_lru_list.empty()) {
133 auto last = m_lru_list.back();
134 m_cache.erase(last);
135 m_lru_list.pop_back();
136 }
137}
138
140{
141 auto it = std::ranges::find(m_lru_list, region);
142 if (it != m_lru_list.end()) {
143 m_lru_list.erase(it);
144 }
145 m_lru_list.push_front(region);
146}
147
148}
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)
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:72
std::vector< uint64_t > start_coordinates
Starting frame index (inclusive)
Definition Region.hpp:69
Represents a point or span in N-dimensional space.
Definition Region.hpp:67