Skip to content

Commit

Permalink
Added logic for offer offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
SirTyson committed Feb 9, 2024
1 parent 6e73c0a commit 6dea1f5
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 12 deletions.
6 changes: 6 additions & 0 deletions src/bucket/Bucket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ Bucket::isIndexed() const
return static_cast<bool>(mIndex);
}

std::pair<std::streamoff, std::streamoff>
Bucket::getOfferRange() const
{
return getIndex().getOfferRange();
}

void
Bucket::setIndex(std::unique_ptr<BucketIndex const>&& index)
{
Expand Down
4 changes: 4 additions & 0 deletions src/bucket/Bucket.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ class Bucket : public std::enable_shared_from_this<Bucket>,
// 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<std::streamoff, std::streamoff> getOfferRange() const;

// Sets index, throws if index is already set
void setIndex(std::unique_ptr<BucketIndex const>&& index);

Expand Down
4 changes: 4 additions & 0 deletions src/bucket/BucketIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ class BucketIndex : public NonMovableOrCopyable
virtual std::pair<std::streamoff, std::streamoff>
getPoolshareTrustlineRange(AccountID const& accountID) const = 0;

// Returns lower bound and upper bound for offer entry positions in the
// given bucket
virtual std::pair<std::streamoff, std::streamoff> getOfferRange() const = 0;

// Returns page size for index. InidividualIndex returns 0 for page size
virtual std::streamoff getPageSize() const = 0;

Expand Down
50 changes: 38 additions & 12 deletions src/bucket/BucketIndexImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,16 +414,9 @@ BucketIndexImpl<IndexT>::scan(Iterator start, LedgerKey const& k) const

template <class IndexT>
std::pair<std::streamoff, std::streamoff>
BucketIndexImpl<IndexT>::getPoolshareTrustlineRange(
AccountID const& accountID) const
BucketIndexImpl<IndexT>::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<uint8_t>::max());
auto lowerBound = getDummyPoolShareTrustlineKey(
accountID, std::numeric_limits<uint8_t>::min());

// Get the index iterators for the bounds
auto startIter = std::lower_bound(
mData.keysToOffset.begin(), mData.keysToOffset.end(), lowerBound,
Expand All @@ -433,9 +426,9 @@ BucketIndexImpl<IndexT>::getPoolshareTrustlineRange(
return {};
}

auto endIter =
std::upper_bound(startIter, mData.keysToOffset.end(), upperBound,
upper_bound_pred<typename IndexT::value_type>);
auto endIter = std::upper_bound(
std::next(startIter), mData.keysToOffset.end(), upperBound,
upper_bound_pred<typename IndexT::value_type>);

// Get file offsets based on lower and upper bound iterators
std::streamoff startOff = startIter->second;
Expand All @@ -450,6 +443,39 @@ BucketIndexImpl<IndexT>::getPoolshareTrustlineRange(
return std::make_pair(startOff, endOff);
}

template <class IndexT>
std::pair<std::streamoff, std::streamoff>
BucketIndexImpl<IndexT>::getPoolshareTrustlineRange(
AccountID const& accountID) const
{
// Get the smallest and largest possible trustline keys for the given
// accountID
auto upperBound = getDummyPoolShareTrustlineKey(
accountID, std::numeric_limits<uint8_t>::max());
auto lowerBound = getDummyPoolShareTrustlineKey(
accountID, std::numeric_limits<uint8_t>::min());

return getOffsetBounds(lowerBound, upperBound);
}

template <class IndexT>
std::pair<std::streamoff, std::streamoff>
BucketIndexImpl<IndexT>::getOfferRange() const
{
// Get the smallest and largest possible offer keys
LedgerKey upperBound(OFFER);
upperBound.offer().sellerID.ed25519().fill(
std::numeric_limits<uint8_t>::max());
upperBound.offer().offerID = std::numeric_limits<int64_t>::max();

LedgerKey lowerBound(OFFER);
lowerBound.offer().sellerID.ed25519().fill(
std::numeric_limits<uint8_t>::min());
lowerBound.offer().offerID = std::numeric_limits<int64_t>::min();

return getOffsetBounds(lowerBound, upperBound);
}

#ifdef BUILD_TESTS
template <class IndexT>
bool
Expand Down
9 changes: 9 additions & 0 deletions src/bucket/BucketIndexImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ template <class IndexT> 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<std::streamoff, std::streamoff>
getOffsetBounds(LedgerKey const& lowerBound,
LedgerKey const& upperBound) const;

friend BucketIndex;

public:
Expand All @@ -74,6 +80,9 @@ template <class IndexT> class BucketIndexImpl : public BucketIndex
virtual std::pair<std::streamoff, std::streamoff>
getPoolshareTrustlineRange(AccountID const& accountID) const override;

virtual std::pair<std::streamoff, std::streamoff>
getOfferRange() const override;

virtual std::streamoff
getPageSize() const override
{
Expand Down

0 comments on commit 6dea1f5

Please sign in to comment.