@@ -193,24 +193,50 @@ void MMLru::Container<T, HookPtr>::updateLruInsertionPoint() noexcept {
193
193
template <typename T, MMLru::Hook<T> T::*HookPtr>
194
194
bool MMLru::Container<T, HookPtr>::add(T& node) noexcept {
195
195
const auto currTime = static_cast <Time>(util::getCurrentTimeSec ());
196
-
197
196
return lruMutex_->lock_combine ([this , &node, currTime]() {
198
197
if (node.isInMMContainer ()) {
199
198
return false ;
200
199
}
201
- if (config_.lruInsertionPointSpec == 0 || insertionPoint_ == nullptr ) {
202
- lru_.linkAtHead (node);
203
- } else {
204
- lru_.insertBefore (*insertionPoint_, node);
205
- }
206
- node.markInMMContainer ();
207
- setUpdateTime (node, currTime);
208
- unmarkAccessed (node);
209
- updateLruInsertionPoint ();
200
+ addNodeLocked (node,currTime);
210
201
return true ;
211
202
});
212
203
}
213
204
205
+ template <typename T, MMLru::Hook<T> T::*HookPtr>
206
+ void MMLru::Container<T, HookPtr>::addNodeLocked(T& node, const Time& currTime) {
207
+ XDCHECK (!node.isInMMContainer ());
208
+ if (config_.lruInsertionPointSpec == 0 || insertionPoint_ == nullptr ) {
209
+ lru_.linkAtHead (node);
210
+ } else {
211
+ lru_.insertBefore (*insertionPoint_, node);
212
+ }
213
+ node.markInMMContainer ();
214
+ setUpdateTime (node, currTime);
215
+ unmarkAccessed (node);
216
+ updateLruInsertionPoint ();
217
+ }
218
+
219
+ template <typename T, MMLru::Hook<T> T::*HookPtr>
220
+ template <typename It>
221
+ uint32_t MMLru::Container<T, HookPtr>::addBatch(It begin, It end) noexcept {
222
+ const auto currTime = static_cast <Time>(util::getCurrentTimeSec ());
223
+ return lruMutex_->lock_combine ([this , begin, end, currTime]() {
224
+ uint32_t i = 0 ;
225
+ for (auto itr = begin; itr != end; ++itr) {
226
+ T* node = *itr;
227
+ XDCHECK (!node->isInMMContainer ());
228
+ if (node->isInMMContainer ()) {
229
+ throw std::runtime_error (
230
+ folly::sformat (" Was not able to add all new items, failed item {}" ,
231
+ node->toString ()));
232
+ }
233
+ addNodeLocked (*node,currTime);
234
+ i++;
235
+ }
236
+ return i;
237
+ });
238
+ }
239
+
214
240
template <typename T, MMLru::Hook<T> T::*HookPtr>
215
241
typename MMLru::Container<T, HookPtr>::LockedIterator
216
242
MMLru::Container<T, HookPtr>::getEvictionIterator() const noexcept {
@@ -231,8 +257,7 @@ void MMLru::Container<T, HookPtr>::withEvictionIterator(F&& fun) {
231
257
232
258
template <typename T, MMLru::Hook<T> T::*HookPtr>
233
259
template <typename F>
234
- void
235
- MMLru::Container<T, HookPtr>::withPromotionIterator(F&& fun) {
260
+ void MMLru::Container<T, HookPtr>::withPromotionIterator(F&& fun) {
236
261
if (config_.useCombinedLockForIterators ) {
237
262
lruMutex_->lock_combine ([this , &fun]() { fun (Iterator{lru_.begin ()}); });
238
263
} else {
0 commit comments