MCPcopy Create free account
hub / github.com/ceph/ceph / insert

Method insert

src/mgr/MgrMapCache.cc:92–130  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

90
91template<class Value>
92typename LFUCache<Value>::InsertRes
93LFUCache<Value>::insert(std::string_view key, Value value) {
94 if (!can_write_cache(key)) {
95 return InsertRes{false};
96 }
97
98 std::unique_lock<std::shared_mutex> l(m);
99 // Re-check enabled after acquiring lock: state may have flipped while waiting.
100 if (!enabled.load(std::memory_order_relaxed)) {
101 return InsertRes{false};
102 }
103
104 auto it = cache_data.find(key);
105 if (it != cache_data.end()) {
106 InsertRes res{true};
107 res.replaced = std::move(it->second.val);
108 it->second.val = std::move(value);
109 return res;
110 }
111
112 // New insert counts as a miss (cache didn't have it)
113 mark_miss();
114
115 InsertRes res{true};
116 if (cache_data.size() >= capacity && !cache_data.empty()) {
117 auto min_it = std::min_element(cache_data.begin(),
118 cache_data.end(),
119 [](const auto& a, const auto& b) {
120 return a.second.hits.load(std::memory_order_relaxed) <
121 b.second.hits.load(std::memory_order_relaxed);
122 });
123 res.evicted = std::move(min_it->second.val);
124 cache_data.erase(min_it);
125 }
126
127 // Allocate std::string only here, when we actually need to store a new key.
128 cache_data.emplace(std::string(key), Entry(std::move(value)));
129 return res;
130}
131
132template <class Value>
133MgrMapCache<Value>::MgrMapCache(uint16_t size)

Callers

nothing calls this directly

Calls 13

insertFunction · 0.85
can_write_cacheMethod · 0.80
EntryClass · 0.70
loadMethod · 0.45
findMethod · 0.45
endMethod · 0.45
sizeMethod · 0.45
emptyMethod · 0.45
beginMethod · 0.45
eraseMethod · 0.45
emplaceMethod · 0.45
has_valueMethod · 0.45

Tested by

no test coverage detected