diff --git a/src/bucket/Bucket.cpp b/src/bucket/Bucket.cpp index 540b61c3b8..371768e7ba 100644 --- a/src/bucket/Bucket.cpp +++ b/src/bucket/Bucket.cpp @@ -53,6 +53,12 @@ Bucket::isIndexed() const return static_cast(mIndex); } +std::pair +Bucket::getOfferRange() const +{ + return getIndex().getOfferRange(); +} + void Bucket::setIndex(std::unique_ptr&& index) { diff --git a/src/bucket/Bucket.h b/src/bucket/Bucket.h index 77c8d0839f..02730b5a59 100644 --- a/src/bucket/Bucket.h +++ b/src/bucket/Bucket.h @@ -95,6 +95,10 @@ class Bucket : public std::enable_shared_from_this, // Returns true if bucket is indexed, false otherwise bool isIndexed() const; + // Returns [lowerBound, upperBound) of file offsets for all offers in the + // bucket + std::pair getOfferRange() const; + // Sets index, throws if index is already set void setIndex(std::unique_ptr&& index); diff --git a/src/bucket/BucketIndex.h b/src/bucket/BucketIndex.h index a7af62ca9f..37d4e91097 100644 --- a/src/bucket/BucketIndex.h +++ b/src/bucket/BucketIndex.h @@ -113,6 +113,10 @@ class BucketIndex : public NonMovableOrCopyable virtual std::pair getPoolshareTrustlineRange(AccountID const& accountID) const = 0; + // Returns lower bound and upper bound for offer entry positions in the + // given bucket + virtual std::pair getOfferRange() const = 0; + // Returns page size for index. InidividualIndex returns 0 for page size virtual std::streamoff getPageSize() const = 0; diff --git a/src/bucket/BucketIndexImpl.cpp b/src/bucket/BucketIndexImpl.cpp index c894df6e71..7394a19ed4 100644 --- a/src/bucket/BucketIndexImpl.cpp +++ b/src/bucket/BucketIndexImpl.cpp @@ -414,16 +414,9 @@ BucketIndexImpl::scan(Iterator start, LedgerKey const& k) const template std::pair -BucketIndexImpl::getPoolshareTrustlineRange( - AccountID const& accountID) const +BucketIndexImpl::getOffsetBounds(LedgerKey const& lowerBound, + LedgerKey const& upperBound) const { - // Get the smallest and largest possible trustline keys for the given - // accountID - auto upperBound = getDummyPoolShareTrustlineKey( - accountID, std::numeric_limits::max()); - auto lowerBound = getDummyPoolShareTrustlineKey( - accountID, std::numeric_limits::min()); - // Get the index iterators for the bounds auto startIter = std::lower_bound( mData.keysToOffset.begin(), mData.keysToOffset.end(), lowerBound, @@ -433,9 +426,9 @@ BucketIndexImpl::getPoolshareTrustlineRange( return {}; } - auto endIter = - std::upper_bound(startIter, mData.keysToOffset.end(), upperBound, - upper_bound_pred); + auto endIter = std::upper_bound( + std::next(startIter), mData.keysToOffset.end(), upperBound, + upper_bound_pred); // Get file offsets based on lower and upper bound iterators std::streamoff startOff = startIter->second; @@ -450,6 +443,39 @@ BucketIndexImpl::getPoolshareTrustlineRange( return std::make_pair(startOff, endOff); } +template +std::pair +BucketIndexImpl::getPoolshareTrustlineRange( + AccountID const& accountID) const +{ + // Get the smallest and largest possible trustline keys for the given + // accountID + auto upperBound = getDummyPoolShareTrustlineKey( + accountID, std::numeric_limits::max()); + auto lowerBound = getDummyPoolShareTrustlineKey( + accountID, std::numeric_limits::min()); + + return getOffsetBounds(lowerBound, upperBound); +} + +template +std::pair +BucketIndexImpl::getOfferRange() const +{ + // Get the smallest and largest possible offer keys + LedgerKey upperBound(OFFER); + upperBound.offer().sellerID.ed25519().fill( + std::numeric_limits::max()); + upperBound.offer().offerID = std::numeric_limits::max(); + + LedgerKey lowerBound(OFFER); + lowerBound.offer().sellerID.ed25519().fill( + std::numeric_limits::min()); + lowerBound.offer().offerID = std::numeric_limits::min(); + + return getOffsetBounds(lowerBound, upperBound); +} + #ifdef BUILD_TESTS template bool diff --git a/src/bucket/BucketIndexImpl.h b/src/bucket/BucketIndexImpl.h index 6ca3764a71..a75cca06e7 100644 --- a/src/bucket/BucketIndexImpl.h +++ b/src/bucket/BucketIndexImpl.h @@ -62,6 +62,12 @@ template class BucketIndexImpl : public BucketIndex // Saves index to disk, overwriting any preexisting file for this index void saveToDisk(BucketManager& bm, Hash const& hash) const; + // Returns [lowFileOffset, highFileOffset) that contain the key ranges + // [lowerBound, upperBound]. If no file offsets exist, returns [0, 0] + std::pair + getOffsetBounds(LedgerKey const& lowerBound, + LedgerKey const& upperBound) const; + friend BucketIndex; public: @@ -74,6 +80,9 @@ template class BucketIndexImpl : public BucketIndex virtual std::pair getPoolshareTrustlineRange(AccountID const& accountID) const override; + virtual std::pair + getOfferRange() const override; + virtual std::streamoff getPageSize() const override {