@@ -337,6 +337,18 @@ class MMLru {
337337 // is unchanged.
338338 bool add (T& node) noexcept ;
339339
340+ // helper function to add the node under the container lock
341+ void addNodeLocked (T& node, const Time& currTime);
342+
343+ // adds the given nodes into the container and marks each as being present in
344+ // the container. The nodes are added to the head of the lru.
345+ //
346+ // @param vector of nodes The nodes to be added to the container.
347+ // @return number of nodes added - it is up to user to verify all
348+ // expected nodes have been added.
349+ template <typename It>
350+ uint32_t addBatch (It begin, It end) noexcept ;
351+
340352 // removes the node from the lru and sets it previous and next to nullptr.
341353 //
342354 // @param node The node to be removed from the container.
@@ -690,19 +702,46 @@ bool MMLru::Container<T, HookPtr>::add(T& node) noexcept {
690702 if (node.isInMMContainer ()) {
691703 return false ;
692704 }
693- if (config_.lruInsertionPointSpec == 0 || insertionPoint_ == nullptr ) {
694- lru_.linkAtHead (node);
695- } else {
696- lru_.insertBefore (*insertionPoint_, node);
697- }
698- node.markInMMContainer ();
699- setUpdateTime (node, currTime);
700- unmarkAccessed (node);
701- updateLruInsertionPoint ();
705+ addNodeLocked (node,currTime);
702706 return true ;
703707 });
704708}
705709
710+ template <typename T, MMLru::Hook<T> T::*HookPtr>
711+ void MMLru::Container<T, HookPtr>::addNodeLocked(T& node, const Time& currTime) {
712+ XDCHECK (!node.isInMMContainer ());
713+ if (config_.lruInsertionPointSpec == 0 || insertionPoint_ == nullptr ) {
714+ lru_.linkAtHead (node);
715+ } else {
716+ lru_.insertBefore (*insertionPoint_, node);
717+ }
718+ node.markInMMContainer ();
719+ setUpdateTime (node, currTime);
720+ unmarkAccessed (node);
721+ updateLruInsertionPoint ();
722+ }
723+
724+ template <typename T, MMLru::Hook<T> T::*HookPtr>
725+ template <typename It>
726+ uint32_t MMLru::Container<T, HookPtr>::addBatch(It begin, It end) noexcept {
727+ const auto currTime = static_cast <Time>(util::getCurrentTimeSec ());
728+ return lruMutex_->lock_combine ([this , begin, end, currTime]() {
729+ uint32_t i = 0 ;
730+ for (auto itr = begin; itr != end; ++itr) {
731+ T* node = *itr;
732+ XDCHECK (!node->isInMMContainer ());
733+ if (node->isInMMContainer ()) {
734+ throw std::runtime_error (
735+ folly::sformat (" Was not able to add all new items, failed item {}" ,
736+ node->toString ()));
737+ }
738+ addNodeLocked (*node,currTime);
739+ i++;
740+ }
741+ return i;
742+ });
743+ }
744+
706745template <typename T, MMLru::Hook<T> T::*HookPtr>
707746typename MMLru::Container<T, HookPtr>::LockedIterator
708747MMLru::Container<T, HookPtr>::getEvictionIterator() const noexcept {
0 commit comments