Refactor ApplyView::dirAdd to give access to low-level operations

- Takes a page from #5362, which may turn out to be useful!
This commit is contained in:
Ed Hennis
2025-04-14 22:15:20 -04:00
parent 7d6155cc99
commit 7ab5bdbf3d
2 changed files with 168 additions and 75 deletions

View File

@@ -383,6 +383,45 @@ public:
emptyDirDelete(Keylet const& directory); emptyDirDelete(Keylet const& directory);
}; };
namespace directory {
/** Helper functions for managing low-level directory operations.
These are not part of the ApplyView interface.
Don't use them unless you really, really know what you're doing.
Instead use dirAdd, dirInsert, etc.
*/
std::uint64_t
createRoot(
ApplyView& view,
Keylet const& directory,
uint256 const& key,
std::function<void(std::shared_ptr<SLE> const&)> const& describe);
auto
findPreviousPage(ApplyView& view, Keylet const& directory, SLE::ref start);
std::uint64_t
insertKey(
ApplyView& view,
SLE::ref node,
std::uint64_t page,
bool preserveOrder,
STVector256& indexes,
uint256 const& key);
std::optional<std::uint64_t>
insertPage(
ApplyView& view,
std::uint64_t page,
SLE::pointer node,
std::uint64_t nextPage,
SLE::ref next,
uint256 const& key,
Keylet const& directory,
std::function<void(std::shared_ptr<SLE> const&)> const& describe);
} // namespace directory
} // namespace ripple } // namespace ripple
#endif #endif

View File

@@ -25,45 +25,53 @@
namespace ripple { namespace ripple {
std::optional<std::uint64_t> namespace directory {
ApplyView::dirAdd(
bool preserveOrder, std::uint64_t
createRoot(
ApplyView& view,
Keylet const& directory, Keylet const& directory,
uint256 const& key, uint256 const& key,
std::function<void(std::shared_ptr<SLE> const&)> const& describe) std::function<void(std::shared_ptr<SLE> const&)> const& describe)
{ {
auto root = peek(directory); auto newRoot = std::make_shared<SLE>(directory);
newRoot->setFieldH256(sfRootIndex, directory.key);
if (!root) describe(newRoot);
{
// No root, make it.
root = std::make_shared<SLE>(directory);
root->setFieldH256(sfRootIndex, directory.key);
describe(root);
STVector256 v; STVector256 v;
v.push_back(key); v.push_back(key);
root->setFieldV256(sfIndexes, v); newRoot->setFieldV256(sfIndexes, v);
insert(root); view.insert(newRoot);
return std::uint64_t{0}; return std::uint64_t{0};
} }
std::uint64_t page = root->getFieldU64(sfIndexPrevious); auto
findPreviousPage(ApplyView& view, Keylet const& directory, SLE::ref start)
{
std::uint64_t page = start->getFieldU64(sfIndexPrevious);
auto node = root; auto node = start;
if (page) if (page)
{ {
node = peek(keylet::page(directory, page)); node = view.peek(keylet::page(directory, page));
if (!node) if (!node)
LogicError("Directory chain: root back-pointer broken."); LogicError("Directory chain: root back-pointer broken.");
} }
auto indexes = node->getFieldV256(sfIndexes); auto indexes = node->getFieldV256(sfIndexes);
return std::make_tuple(page, node, indexes);
}
// If there's space, we use it: std::uint64_t
if (indexes.size() < dirNodeMaxEntries) insertKey(
ApplyView& view,
SLE::ref node,
std::uint64_t page,
bool preserveOrder,
STVector256& indexes,
uint256 const& key)
{ {
if (preserveOrder) if (preserveOrder)
{ {
@@ -88,24 +96,37 @@ ApplyView::dirAdd(
} }
node->setFieldV256(sfIndexes, indexes); node->setFieldV256(sfIndexes, indexes);
update(node); view.update(node);
return page; return page;
} }
std::optional<std::uint64_t>
insertPage(
ApplyView& view,
std::uint64_t page,
SLE::pointer node,
std::uint64_t nextPage,
SLE::ref next,
uint256 const& key,
Keylet const& directory,
std::function<void(std::shared_ptr<SLE> const&)> const& describe)
{
// Check whether we're out of pages. // Check whether we're out of pages.
if (++page >= dirNodeMaxPages) if (++page >= dirNodeMaxPages)
{
return std::nullopt; return std::nullopt;
}
// We are about to create a new node; we'll link it to // We are about to create a new node; we'll link it to
// the chain first: // the chain first:
node->setFieldU64(sfIndexNext, page); node->setFieldU64(sfIndexNext, page);
update(node); view.update(node);
root->setFieldU64(sfIndexPrevious, page); next->setFieldU64(sfIndexPrevious, page);
update(root); view.update(next);
// Insert the new key: // Insert the new key:
indexes.clear(); STVector256 indexes;
indexes.push_back(key); indexes.push_back(key);
node = std::make_shared<SLE>(keylet::page(directory, page)); node = std::make_shared<SLE>(keylet::page(directory, page));
@@ -116,12 +137,45 @@ ApplyView::dirAdd(
// it's the default. // it's the default.
if (page != 1) if (page != 1)
node->setFieldU64(sfIndexPrevious, page - 1); node->setFieldU64(sfIndexPrevious, page - 1);
if (nextPage)
node->setFieldU64(sfIndexNext, nextPage);
describe(node); describe(node);
insert(node); view.insert(node);
return page; return page;
} }
} // namespace directory
std::optional<std::uint64_t>
ApplyView::dirAdd(
bool preserveOrder,
Keylet const& directory,
uint256 const& key,
std::function<void(std::shared_ptr<SLE> const&)> const& describe)
{
auto root = peek(directory);
if (!root)
{
// No root, make it.
return directory::createRoot(*this, directory, key, describe);
}
auto [page, node, indexes] =
directory::findPreviousPage(*this, directory, root);
// If there's space, we use it:
if (indexes.size() < dirNodeMaxEntries)
{
return directory::insertKey(
*this, node, page, preserveOrder, indexes, key);
}
return directory::insertPage(
*this, page, node, 0, root, key, directory, describe);
}
bool bool
ApplyView::emptyDirDelete(Keylet const& directory) ApplyView::emptyDirDelete(Keylet const& directory)
{ {