mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
Refactor and improve the SHAMap code:
This commit combines a number of cleanups, targeting both the code structure and the code logic. Large changes include: - Using more strongly-typed classes for SHAMap nodes, instead of relying on runtime-time detection of class types. This change saves 16 bytes of memory per node. - Improving the interface of SHAMap::addGiveItem and SHAMap::addItem to avoid the need for passing two bool arguments. - Documenting the "copy-on-write" semantics that SHAMap uses to efficiently track changes in individual nodes. - Removing unused code and simplifying several APIs. - Improving function naming.
This commit is contained in:
94
src/ripple/shamap/impl/SHAMapLeafNode.cpp
Normal file
94
src/ripple/shamap/impl/SHAMapLeafNode.cpp
Normal file
@@ -0,0 +1,94 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <ripple/basics/contract.h>
|
||||
#include <ripple/beast/core/LexicalCast.h>
|
||||
#include <ripple/shamap/SHAMapLeafNode.h>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
SHAMapLeafNode::SHAMapLeafNode(
|
||||
std::shared_ptr<SHAMapItem const> item,
|
||||
std::uint32_t cowid)
|
||||
: SHAMapTreeNode(cowid), item_(std::move(item))
|
||||
{
|
||||
assert(item_->peekData().size() >= 12);
|
||||
}
|
||||
|
||||
SHAMapLeafNode::SHAMapLeafNode(
|
||||
std::shared_ptr<SHAMapItem const> item,
|
||||
std::uint32_t cowid,
|
||||
SHAMapHash const& hash)
|
||||
: SHAMapTreeNode(cowid, hash), item_(std::move(item))
|
||||
{
|
||||
assert(item_->peekData().size() >= 12);
|
||||
}
|
||||
|
||||
std::shared_ptr<SHAMapItem const> const&
|
||||
SHAMapLeafNode::peekItem() const
|
||||
{
|
||||
return item_;
|
||||
}
|
||||
|
||||
bool
|
||||
SHAMapLeafNode::setItem(std::shared_ptr<SHAMapItem const> i)
|
||||
{
|
||||
assert(cowid_ != 0);
|
||||
item_ = std::move(i);
|
||||
|
||||
auto const oldHash = hash_;
|
||||
|
||||
updateHash();
|
||||
|
||||
return (oldHash != hash_);
|
||||
}
|
||||
|
||||
std::string
|
||||
SHAMapLeafNode::getString(const SHAMapNodeID& id) const
|
||||
{
|
||||
std::string ret = SHAMapTreeNode::getString(id);
|
||||
|
||||
auto const type = getType();
|
||||
|
||||
if (type == SHAMapNodeType::tnTRANSACTION_NM)
|
||||
ret += ",txn\n";
|
||||
else if (type == SHAMapNodeType::tnTRANSACTION_MD)
|
||||
ret += ",txn+md\n";
|
||||
else if (type == SHAMapNodeType::tnACCOUNT_STATE)
|
||||
ret += ",as\n";
|
||||
else
|
||||
ret += ",leaf\n";
|
||||
|
||||
ret += " Tag=";
|
||||
ret += to_string(item_->key());
|
||||
ret += "\n Hash=";
|
||||
ret += to_string(hash_);
|
||||
ret += "/";
|
||||
ret += std::to_string(item_->size());
|
||||
return ret;
|
||||
}
|
||||
|
||||
void
|
||||
SHAMapLeafNode::invariants(bool) const
|
||||
{
|
||||
assert(hash_.isNonZero());
|
||||
assert(item_ != nullptr);
|
||||
}
|
||||
|
||||
} // namespace ripple
|
||||
Reference in New Issue
Block a user