Fixes and cleanups.

This commit is contained in:
JoelKatz
2012-01-31 18:44:59 -08:00
parent eec4bfddff
commit bac03c31de
2 changed files with 20 additions and 14 deletions

View File

@@ -18,7 +18,7 @@ void SHAMap::dirtyUp(const uint256& id)
#ifdef DEBUG #ifdef DEBUG
std::cerr << "dirtyUp(" << id.GetHex() << ")" << std::endl; std::cerr << "dirtyUp(" << id.GetHex() << ")" << std::endl;
#endif #endif
SHAMapLeafNode::pointer leaf=mLeafByID[SHAMapNode(SHAMapNode::leafDepth, id)]; SHAMapLeafNode::pointer leaf=checkCacheLeaf(SHAMapNode(SHAMapNode::leafDepth, id));
if(!leaf) throw SHAMapException(MissingNode); if(!leaf) throw SHAMapException(MissingNode);
uint256 hVal=leaf->getNodeHash(); uint256 hVal=leaf->getNodeHash();
@@ -33,7 +33,7 @@ void SHAMap::dirtyUp(const uint256& id)
for(int depth=SHAMapNode::leafDepth-1; depth>=0; depth--) for(int depth=SHAMapNode::leafDepth-1; depth>=0; depth--)
{ // walk up the tree to the root updating nodes { // walk up the tree to the root updating nodes
SHAMapInnerNode::pointer node=mInnerNodeByID[SHAMapNode(depth, leaf->getNodeID())]; SHAMapInnerNode::pointer node=checkCacheNode(SHAMapNode(depth, leaf->getNodeID()));
if(!node) throw SHAMapException(MissingNode); if(!node) throw SHAMapException(MissingNode);
if(!node->setChildHash(node->selectBranch(id), hVal)) if(!node->setChildHash(node->selectBranch(id), hVal))
{ {
@@ -60,12 +60,17 @@ void SHAMap::dirtyUp(const uint256& id)
SHAMapLeafNode::pointer SHAMap::checkCacheLeaf(const SHAMapNode& iNode) SHAMapLeafNode::pointer SHAMap::checkCacheLeaf(const SHAMapNode& iNode)
{ {
assert(iNode.isLeaf()); assert(iNode.isLeaf());
SHAMapLeafNode::pointer leaf=mLeafByID[iNode]; std::map<SHAMapNode, SHAMapLeafNode::pointer>::iterator it=mLeafByID.find(iNode);
#ifdef ST_DEBUG if(it==mLeafByID.end()) return SHAMapLeafNode::pointer();
if(!leaf) std::cerr << "Leaf(" << iNode.getString() << ") not in cache" << std::endl; return it->second;
else std::cerr << "Leaf(" << iNode.getString() << ") found in cache" << std::endl; }
#endif
return leaf; SHAMapInnerNode::pointer SHAMap::checkCacheNode(const SHAMapNode& iNode)
{
assert(!iNode.isLeaf());
std::map<SHAMapNode, SHAMapInnerNode::pointer>::iterator it=mInnerNodeByID.find(iNode);
if(it==mInnerNodeByID.end()) return SHAMapInnerNode::pointer();
return it->second;
} }
@@ -146,20 +151,20 @@ SHAMapLeafNode::pointer SHAMap::getLeaf(const SHAMapNode& id, const uint256& has
assert(!!hash); assert(!!hash);
if(!id.isLeaf()) return SHAMapLeafNode::pointer(); if(!id.isLeaf()) return SHAMapLeafNode::pointer();
SHAMapLeafNode::pointer leaf=mLeafByID[id]; // is the leaf in memory SHAMapLeafNode::pointer leaf=checkCacheLeaf(id);
if(leaf) return returnLeaf(leaf, modify); if(leaf) return returnLeaf(leaf, modify);
std::vector<unsigned char> leafData; std::vector<unsigned char> leafData;
if(!fetchNode(hash, leafData)) throw SHAMapException(MissingNode); if(!fetchNode(hash, leafData)) throw SHAMapException(MissingNode);
leaf=SHAMapLeafNode::pointer(new SHAMapLeafNode(id, leafData, mSeq)); leaf=SHAMapLeafNode::pointer(new SHAMapLeafNode(id, leafData, mSeq));
if(leaf->getNodeHash()!=hash) throw SHAMapException(InvalidNode); if(leaf->getNodeHash()!=hash) throw SHAMapException(InvalidNode);
mLeafByID[id]=leaf; mLeafByID.insert(std::make_pair(id, leaf));
return leaf; return leaf;
} }
SHAMapInnerNode::pointer SHAMap::getInner(const SHAMapNode& id, const uint256& hash, bool modify) SHAMapInnerNode::pointer SHAMap::getInner(const SHAMapNode& id, const uint256& hash, bool modify)
{ // retrieve an inner node whose node hash is known { // retrieve an inner node whose node hash is known
SHAMapInnerNode::pointer node=mInnerNodeByID[id]; SHAMapInnerNode::pointer node=checkCacheNode(id);
if(node) return returnNode(node, modify); if(node) return returnNode(node, modify);
std::vector<unsigned char> rawNode; std::vector<unsigned char> rawNode;
@@ -313,7 +318,7 @@ SHAMapItem::pointer SHAMap::peekNextItem(const uint256& id)
for(int depth=SHAMapNode::leafDepth-1; depth>=0; depth--) for(int depth=SHAMapNode::leafDepth-1; depth>=0; depth--)
{ // walk up the tree until we find a node with a subsequent child { // walk up the tree until we find a node with a subsequent child
SHAMapInnerNode::pointer node=mInnerNodeByID[SHAMapNode(depth, id)]; SHAMapInnerNode::pointer node=checkCacheNode(SHAMapNode(depth, id));
if(!node) if(!node)
{ {
#ifdef DEBUG #ifdef DEBUG
@@ -367,7 +372,7 @@ SHAMapItem::pointer SHAMap::peekPrevItem(const uint256& id)
for(int depth=SHAMapNode::leafDepth-1; depth>=0; depth--) for(int depth=SHAMapNode::leafDepth-1; depth>=0; depth--)
{ // walk up the tree until we find a node with a previous child { // walk up the tree until we find a node with a previous child
SHAMapInnerNode::pointer node=mInnerNodeByID[SHAMapNode(depth, id)]; SHAMapInnerNode::pointer node=checkCacheNode(SHAMapNode(depth, id));
if(!node) if(!node)
{ {
#ifdef DEBUG #ifdef DEBUG

View File

@@ -229,9 +229,10 @@ protected:
SHAMapLeafNode::pointer createLeaf(const SHAMapInnerNode& lowestParent, const uint256& id); SHAMapLeafNode::pointer createLeaf(const SHAMapInnerNode& lowestParent, const uint256& id);
SHAMapLeafNode::pointer checkCacheLeaf(const SHAMapNode&); SHAMapLeafNode::pointer checkCacheLeaf(const SHAMapNode&);
SHAMapLeafNode::pointer walkToLeaf(const uint256& id, bool create, bool modify); SHAMapLeafNode::pointer walkToLeaf(const uint256& id, bool create, bool modify);
SHAMapLeafNode::pointer getLeaf(const SHAMapNode& id, const uint256& hash, bool modify); SHAMapLeafNode::pointer getLeaf(const SHAMapNode& id, const uint256& hash, bool modify);
SHAMapLeafNode::pointer returnLeaf(SHAMapLeafNode::pointer leaf, bool modify); SHAMapLeafNode::pointer returnLeaf(SHAMapLeafNode::pointer leaf, bool modify);
SHAMapInnerNode::pointer checkCacheNode(const SHAMapNode&);
SHAMapInnerNode::pointer getInner(const SHAMapNode& id, const uint256& hash, bool modify); SHAMapInnerNode::pointer getInner(const SHAMapNode& id, const uint256& hash, bool modify);
SHAMapInnerNode::pointer returnNode(SHAMapInnerNode::pointer node, bool modify); SHAMapInnerNode::pointer returnNode(SHAMapInnerNode::pointer node, bool modify);
SHAMapInnerNode::pointer walkTo(const SHAMapNode& id); SHAMapInnerNode::pointer walkTo(const SHAMapNode& id);