|
22 | 22 |
|
23 | 23 | #include "paimon/common/io/cache/cache.h" |
24 | 24 | #include "paimon/common/io/cache/cache_key.h" |
| 25 | +#include "paimon/common/io/cache/lru_cache.h" |
25 | 26 | #include "paimon/common/memory/memory_segment.h" |
26 | 27 | #include "paimon/result.h" |
27 | 28 |
|
28 | 29 | namespace paimon { |
29 | 30 | class CacheManager { |
30 | 31 | public: |
31 | | - CacheManager() { |
32 | | - // todo implements cache |
33 | | - data_cache_ = std::make_shared<NoCache>(); |
34 | | - index_cache_ = std::make_shared<NoCache>(); |
| 32 | + /// Refreshing the cache comes with some costs, so not every time we visit the CacheManager, but |
| 33 | + /// every 10 visits, refresh the LRU strategy. |
| 34 | + static constexpr int32_t REFRESH_COUNT = 10; |
| 35 | + |
| 36 | + /// Container that wraps a MemorySegment with an access counter for refresh. |
| 37 | + class SegmentContainer { |
| 38 | + public: |
| 39 | + explicit SegmentContainer(const MemorySegment& segment) : segment_(segment) {} |
| 40 | + |
| 41 | + const MemorySegment& Access() { |
| 42 | + access_count_++; |
| 43 | + return segment_; |
| 44 | + } |
| 45 | + |
| 46 | + int32_t GetAccessCount() const { |
| 47 | + return access_count_; |
| 48 | + } |
| 49 | + |
| 50 | + private: |
| 51 | + MemorySegment segment_; |
| 52 | + int32_t access_count_ = 0; |
| 53 | + }; |
| 54 | + |
| 55 | + /// Constructs a CacheManager with LRU caching. |
| 56 | + /// @param max_memory_bytes Total cache capacity in bytes. |
| 57 | + /// @param high_priority_pool_ratio Ratio of capacity reserved for index cache [0.0, 1.0). |
| 58 | + /// If 0, index and data share the same cache. |
| 59 | + CacheManager(int64_t max_memory_bytes, double high_priority_pool_ratio) { |
| 60 | + auto index_cache_bytes = static_cast<int64_t>(max_memory_bytes * high_priority_pool_ratio); |
| 61 | + auto data_cache_bytes = |
| 62 | + static_cast<int64_t>(max_memory_bytes * (1.0 - high_priority_pool_ratio)); |
| 63 | + data_cache_ = std::make_shared<LruCache>(data_cache_bytes); |
| 64 | + if (high_priority_pool_ratio == 0.0) { |
| 65 | + index_cache_ = data_cache_; |
| 66 | + } else { |
| 67 | + index_cache_ = std::make_shared<LruCache>(index_cache_bytes); |
| 68 | + } |
35 | 69 | } |
36 | 70 |
|
37 | 71 | Result<MemorySegment> GetPage( |
38 | 72 | std::shared_ptr<CacheKey>& key, |
39 | | - std::function<Result<MemorySegment>(const std::shared_ptr<CacheKey>&)> reader); |
| 73 | + std::function<Result<MemorySegment>(const std::shared_ptr<CacheKey>&)> reader, |
| 74 | + CacheCallback eviction_callback); |
40 | 75 |
|
41 | 76 | void InvalidPage(const std::shared_ptr<CacheKey>& key); |
42 | 77 |
|
| 78 | + const std::shared_ptr<Cache>& DataCache() const { |
| 79 | + return data_cache_; |
| 80 | + } |
| 81 | + |
| 82 | + const std::shared_ptr<Cache>& IndexCache() const { |
| 83 | + return index_cache_; |
| 84 | + } |
| 85 | + |
43 | 86 | private: |
44 | 87 | std::shared_ptr<Cache> data_cache_; |
45 | 88 | std::shared_ptr<Cache> index_cache_; |
|
0 commit comments