@@ -193,24 +193,50 @@ void MMLru::Container<T, HookPtr>::updateLruInsertionPoint() noexcept {
193193template <typename T, MMLru::Hook<T> T::*HookPtr>
194194bool MMLru::Container<T, HookPtr>::add(T& node) noexcept {
195195 const auto currTime = static_cast <Time>(util::getCurrentTimeSec ());
196-
197196 return lruMutex_->lock_combine ([this , &node, currTime]() {
198197 if (node.isInMMContainer ()) {
199198 return false ;
200199 }
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);
210201 return true ;
211202 });
212203}
213204
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+
214240template <typename T, MMLru::Hook<T> T::*HookPtr>
215241typename MMLru::Container<T, HookPtr>::LockedIterator
216242MMLru::Container<T, HookPtr>::getEvictionIterator() const noexcept {
@@ -231,8 +257,7 @@ void MMLru::Container<T, HookPtr>::withEvictionIterator(F&& fun) {
231257
232258template <typename T, MMLru::Hook<T> T::*HookPtr>
233259template <typename F>
234- void
235- MMLru::Container<T, HookPtr>::withPromotionIterator(F&& fun) {
260+ void MMLru::Container<T, HookPtr>::withPromotionIterator(F&& fun) {
236261 if (config_.useCombinedLockForIterators ) {
237262 lruMutex_->lock_combine ([this , &fun]() { fun (Iterator{lru_.begin ()}); });
238263 } else {
0 commit comments