Skip to content

Commit

Permalink
Modify ClusterClientModule
Browse files Browse the repository at this point in the history
  • Loading branch information
flyicegood committed Jun 25, 2015
1 parent 68f8c1e commit f65ed26
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 87 deletions.
33 changes: 17 additions & 16 deletions NFComm/NFCore/NFCConsistentHash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,21 @@ class NFIVirtualNode
nVirtualIndex = 0;
}

virtual std::string GetDataStr() = 0;
virtual int GetDataID() = 0;
virtual std::string GetDataStr() const = 0;
virtual int GetDataID() const = 0;

std::string ToStr() const
{
return boost::str(boost::format("%1%-%2%-%3%") % boost::lexical_cast<std::string>(GetDataID()) % GetDataStr() % nVirtualIndex);
}

private:


int nVirtualIndex;//虚拟节点序号
};

class NFCMachineNode : public NFIVirtualNode
{
public:
NFCMachineNode(const int nVirID) : NFIVirtualNode(nVirID)
{
strIP = "";
Expand All @@ -64,12 +63,14 @@ class NFCMachineNode : public NFIVirtualNode
nMachineID = 0;
}

virtual std::string GetDataStr()
public:

virtual std::string GetDataStr() const
{
return strIP;
}

virtual int GetDataID()
virtual int GetDataID() const
{
return nMachineID;
}
Expand Down Expand Up @@ -127,7 +128,7 @@ class NFCConsistentHash
return mxNodes.empty();
}

void Insert(const NFIVirtualNode& xNode)
void Insert(const NFCMachineNode& xNode)
{
uint32_t hash = m_pHasher->GetHashValue(xNode);
auto it = mxNodes.find(hash);
Expand All @@ -137,7 +138,7 @@ class NFCConsistentHash
}
}

bool Exist(const NFIVirtualNode& xInNode)
bool Exist(const NFCMachineNode& xInNode)
{
uint32_t hash = m_pHasher->GetHashValue(xInNode);
TMAP_TYPE::iterator it = mxNodes.find(hash);
Expand All @@ -149,19 +150,19 @@ class NFCConsistentHash
return false;
}

std::size_t Erase(const NFIVirtualNode& xNode)
std::size_t Erase(const NFCMachineNode& xNode)
{
uint32_t hash = m_pHasher->GetHashValue(xNode);
return mxNodes.erase(hash);
}

bool GetSuitNode(NFIVirtualNode& node)
bool GetSuitNode(NFCMachineNode& node)
{
int nID = 0;
return GetSuitNode(nID, node);
}

bool GetSuitNode(const int nID, NFIVirtualNode& node)
bool GetSuitNode(const int nID, NFCMachineNode& node)
{
std::string strData = boost::lexical_cast<std::string>(nID);
if (GetSuitNode(strData, node))
Expand All @@ -172,7 +173,7 @@ class NFCConsistentHash
return false;
}

bool GetSuitNode(const std::string& str, NFIVirtualNode& node)
bool GetSuitNode(const std::string& str, NFCMachineNode& node)
{
boost::crc_32_type ret;
ret.process_bytes(str.c_str(), str.length());
Expand All @@ -186,7 +187,7 @@ class NFCConsistentHash
return false;
}

bool GetSuitNode(uint32_t hashValue, NFIVirtualNode& node)
bool GetSuitNode(uint32_t hashValue, NFCMachineNode& node)
{
if(mxNodes.empty())
{
Expand All @@ -205,7 +206,7 @@ class NFCConsistentHash
return true;
}

bool GetMasterNodeReverse(NFIVirtualNode& node)
bool GetMasterNodeReverse(NFCMachineNode& node)
{
if(mxNodes.empty())
{
Expand All @@ -226,7 +227,7 @@ class NFCConsistentHash
return true;
}

bool GetMasterNodeSequence(NFIVirtualNode& node)
bool GetMasterNodeSequence(NFCMachineNode& node)
{
if(mxNodes.empty())
{
Expand All @@ -248,7 +249,7 @@ class NFCConsistentHash
}

private:
typedef std::map<uint32_t, NFIVirtualNode> TMAP_TYPE;
typedef std::map<uint32_t, NFCMachineNode> TMAP_TYPE;
typedef TMAP_TYPE::iterator iterator;

NFIHasher* m_pHasher;
Expand Down
Loading

0 comments on commit f65ed26

Please sign in to comment.