@@ -708,6 +708,198 @@ SearchableLiveBucketListSnapshot::scanForEntriesOfType(
708708 loopAllBuckets (scanBucket);
709709}
710710
711+ namespace
712+ {
713+ // Iterator for `BucketEntry`s of a given type in a bucket. Expects the stream
714+ // to be positioned at the start of the type range.
715+ class BucketEntryIterator
716+ {
717+ BucketEntry mEntry ;
718+ LedgerKey mKey ;
719+ XDRInputFileStream& mStream ;
720+ LedgerEntryType const mType ;
721+
722+ public:
723+ BucketEntryIterator (XDRInputFileStream& stream, LedgerEntryType type)
724+ : mStream (stream), mType (type)
725+ {
726+ }
727+
728+ BucketEntry const &
729+ getEntry () const
730+ {
731+ return mEntry ;
732+ }
733+ LedgerKey const &
734+ getKey () const
735+ {
736+ return mKey ;
737+ }
738+ bool advance ();
739+ };
740+
741+ bool
742+ BucketEntryIterator::advance ()
743+ {
744+ while (mStream .readOne (mEntry ))
745+ {
746+ if (isBucketMetaEntry<LiveBucket>(mEntry ))
747+ {
748+ continue ;
749+ }
750+ mKey = getBucketLedgerKey (mEntry );
751+ if (mKey .type () > mType )
752+ {
753+ break ;
754+ }
755+
756+ auto type = mEntry .type ();
757+ releaseAssert (type == LIVEENTRY || type == INITENTRY ||
758+ type == DEADENTRY );
759+ return true ;
760+ }
761+ return false ;
762+ }
763+ } // namespace
764+
765+ void
766+ SearchableLiveBucketListSnapshot::scanForLiveEntriesOfType (
767+ LedgerEntryType type,
768+ std::function<void (LedgerEntry const &, LedgerKey const &)> callback) const
769+ {
770+ // We implement this as a k-way merge over all buckets. We use a loser tree
771+ // for this. The benefit over a heap is ~2x fewer comparisons. A loser tree
772+ // is like a single-elimination tournament. The leaves of the tree are the
773+ // iterators, and the internal nodes represent the loser of the comparison
774+ // between the two children. This implementation represents the binary tree
775+ // in an array, where the tournament tree is from indices [1, 2n) (leaves
776+ // are [n, 2n)). Index 0 is used for keeping track of the overall winner. To
777+ // update, we just need to advance the iterator for the winning node and
778+ // then do the log(k) comparisons upward along the path to the root to
779+ // update the losers. While loser trees often store the whole node value at
780+ // intermediate nodes, we just store an index, since copying the XDR types
781+ // is probably more expensive than the extra indirection.
782+
783+ std::vector<BucketEntryIterator> iterators;
784+ loopAllBuckets ([&iterators, type,
785+ this ](std::shared_ptr<LiveBucket const > const & bucket) {
786+ if (bucket->isEmpty ())
787+ {
788+ return Loop::INCOMPLETE ;
789+ }
790+
791+ auto range = bucket->getRangeForType (type);
792+ if (!range)
793+ {
794+ return Loop::INCOMPLETE ;
795+ }
796+
797+ auto & stream = getStream (bucket);
798+ stream.seek (range->first );
799+
800+ iterators.emplace_back (stream, type);
801+ return Loop::INCOMPLETE ;
802+ });
803+
804+ if (iterators.empty ())
805+ {
806+ return ;
807+ }
808+
809+ size_t const numIterators = iterators.size ();
810+
811+ constexpr int exhausted = -1 ;
812+ std::vector<int > tree;
813+ tree.resize (numIterators * 2 );
814+ for (size_t i = 0 ; i < numIterators; ++i)
815+ {
816+ if (iterators[i].advance ())
817+ {
818+ tree[numIterators + i] = i;
819+ }
820+ else
821+ {
822+ tree[numIterators + i] = exhausted;
823+ }
824+ }
825+
826+ auto leftWins = [&iterators](int leftIndex, int rightIndex) -> bool {
827+ if (leftIndex == exhausted)
828+ {
829+ return false ;
830+ }
831+ if (rightIndex == exhausted)
832+ {
833+ return true ;
834+ }
835+ if (auto cmp = compareLedgerKeys (iterators[leftIndex].getKey (),
836+ iterators[rightIndex].getKey ());
837+ cmp != std::partial_ordering::equivalent)
838+ {
839+ releaseAssert (cmp != std::partial_ordering::unordered);
840+ return cmp == std::partial_ordering::less;
841+ }
842+ return leftIndex < rightIndex;
843+ };
844+
845+ // Play the match at index i; store the loser, return the winner
846+ auto play = [&tree, &leftWins](auto & play, size_t index) -> int {
847+ if (2 * index >= tree.size ())
848+ {
849+ return tree[index];
850+ }
851+ int left = play (play, 2 * index);
852+ int right = play (play, 2 * index + 1 );
853+ if (leftWins (left, right))
854+ {
855+ tree[index] = right;
856+ return left;
857+ }
858+ else
859+ {
860+ tree[index] = left;
861+ return right;
862+ }
863+ };
864+ tree[0 ] = play (play, 1 );
865+
866+ bool first = true ;
867+ LedgerKey last;
868+ while (tree[0 ] != exhausted)
869+ {
870+ int index = tree[0 ];
871+ auto & iter = iterators[index];
872+ if (auto & key = iter.getKey (); first || key != last)
873+ {
874+ last = key;
875+ auto & entry = iter.getEntry ();
876+ if (entry.type () == LIVEENTRY || entry.type () == INITENTRY )
877+ {
878+ callback (entry.liveEntry (), key);
879+ }
880+ }
881+ first = false ;
882+
883+ if (!iter.advance ())
884+ {
885+ tree[index + numIterators] = exhausted;
886+ }
887+ int winner = tree[index + numIterators];
888+
889+ int i = (index + numIterators) / 2 ;
890+ while (i > 0 )
891+ {
892+ if (leftWins (tree[i], winner))
893+ {
894+ std::swap (tree[i], winner);
895+ }
896+ i /= 2 ;
897+ }
898+
899+ tree[0 ] = winner;
900+ }
901+ }
902+
711903// Helper function to handle scan logic in a single bucket.
712904Loop
713905SearchableLiveBucketListSnapshot::scanForEvictionInBucket (
0 commit comments