Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize account_lines and account_offers (RIPD-587) #579

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 96 additions & 5 deletions src/ripple/app/ledger/Ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,89 @@ void Ledger::visitAccountItems (

}

bool Ledger::visitAccountItems (
Account const& accountID,
uint256 const& startAfter,
std::uint64_t const hint,
unsigned int limit,
std::function <bool (SLE::ref)> func) const
{
// Visit each item in this account's owner directory
uint256 const rootIndex (Ledger::getOwnerDirIndex (accountID));
uint256 currentIndex (rootIndex);

// If startAfter is not zero try jumping to that page using the hint
if (startAfter.isNonZero ())
{
uint256 const hintIndex (getDirNodeIndex (rootIndex, hint));
SLE::pointer hintDir (getSLEi (hintIndex));
if (hintDir != nullptr)
{
for (auto const& node : hintDir->getFieldV256 (sfIndexes))
{
if (node == startAfter)
{
// We found the hint, we can start here
currentIndex = hintIndex;
break;
}
}
}

bool found (false);
for (;;)
{
SLE::pointer ownerDir (getSLEi (currentIndex));

if (! ownerDir || ownerDir->getType () != ltDIR_NODE)
return found;

for (auto const& node : ownerDir->getFieldV256 (sfIndexes))
{
if (!found)
{
if (node == startAfter)
found = true;
}
else if (func (getSLEi (node)) && limit-- <= 1)
{
return found;
}
}

std::uint64_t const uNodeNext (ownerDir->getFieldU64 (sfIndexNext));

if (uNodeNext == 0)
return found;

currentIndex = Ledger::getDirNodeIndex (rootIndex, uNodeNext);
}
}
else
{
for (;;)
{
SLE::pointer ownerDir (getSLEi (currentIndex));

if (! ownerDir || ownerDir->getType () != ltDIR_NODE)
return true;

for (auto const& node : ownerDir->getFieldV256 (sfIndexes))
{
if (func (getSLEi (node)) && limit-- <= 1)
return true;
}

std::uint64_t const uNodeNext (ownerDir->getFieldU64 (sfIndexNext));

if (uNodeNext == 0)
return true;

currentIndex = Ledger::getDirNodeIndex (rootIndex, uNodeNext);
}
}
}

static void visitHelper (
std::function<void (SLE::ref)>& function, SHAMapItem::ref item)
{
Expand Down Expand Up @@ -1785,12 +1868,20 @@ uint256 Ledger::getRippleStateIndex (
{
Serializer s (62);

bool const bAltB = a < b;
s.add16 (spaceRipple); // 2

if (a < b)
{
s.add160 (a); // 20
s.add160 (b); // 20
}
else
{
s.add160 (b); // 20
s.add160 (a); // 20
}

s.add16 (spaceRipple); // 2
s.add160 (bAltB ? a : b); // 20
s.add160 (bAltB ? b : a); // 20
s.add160 (currency); // 20
s.add160 (currency); // 20

return s.getSHA512Half ();
}
Expand Down
9 changes: 8 additions & 1 deletion src/ripple/app/ledger/Ledger.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,15 @@ class Ledger
SLE::pointer getAccountRoot (Account const& accountID) const;
SLE::pointer getAccountRoot (const RippleAddress & naAccountID) const;
void updateSkipList ();

void visitAccountItems (
Account const& acctID, std::function<void (SLE::ref)>) const;
Account const& accountID, std::function<void (SLE::ref)>) const;
bool visitAccountItems (
Account const& accountID,
uint256 const& startAfter, // Entry to start after
std::uint64_t const hint, // Hint which page to start at
unsigned int limit,
std::function <bool (SLE::ref)>) const;
void visitStateItems (std::function<void (SLE::ref)>) const;

// database functions (low-level)
Expand Down
Loading