一致性 hash 算法( consistent hashing )

来源:互联网 发布:怎么找回淘宝店铺 编辑:程序博客网 时间:2024/06/02 02:25

1. 代码组织

2. conhash.h

/*************************************************************************    > File Name: conhash.h    > Author: ma6174    > Mail: ma6174@163.com     > Created Time: Mon 03 Nov 2014 08:51:23 PM WST ************************************************************************/#ifndef CONHASH_H#define CONHASH_H#include <stdio.h>#include <stdlib.h>#include <iostream>#include <map>#include <string>#include <sstream>using namespace std;/* * Cache class For store object * */class Cache {private:string identity;   uint vcnt;         // the count of virtual cachepublic:Cache();Cache(string, uint);string getIdentity();uint getVcnt();};struct cmp {bool operator()(uint hash1, uint hash2) const {return hash1 - hash2 <= 0;}};class ConHash {private://map<uint, Cache*, cmp>CacheMap;map<uint, Cache*>CacheMap;public:ConHash() {}uint hashFunc(string);void addCache(Cache *cache);Cache * search(string object);};#endif

3. conhash.cpp

/*************************************************************************    > File Name: conhash.cpp    > Author: ma6174    > Mail: ma6174@163.com     > Created Time: Mon 03 Nov 2014 08:58:11 PM WST ************************************************************************/#include "conhash.h"Cache::Cache() {identity = "";vcnt = 0;}Cache::Cache(string _identity, uint _vcnt):identity(_identity), vcnt(_vcnt) {}string Cache::getIdentity() {return this->identity;}uint Cache::getVcnt() {return this->vcnt;}uint ConHash::hashFunc(string str) {    uint seed = 131; // 31 131 1313 13131 131313 etc..    uint hash = 0;string::iterator index = str.begin();    while (index < str.end())    {        hash = hash * seed + (*index++);    }     return (hash & 0x7FFFFFFF);}void ConHash::addCache(Cache *cache) {ostringstream oss;string identity = cache->getIdentity();uint vcnt = cache->getVcnt();uint key;string value;int i;for(i = 0;i < vcnt;i++) {oss << identity << i;value = oss.str();key = this->hashFunc(value);this->CacheMap.insert(pair<uint, Cache*>(key, cache));}}Cache *ConHash::search(string object) {uint hash = this->hashFunc(object);map<uint, Cache*>::iterator it;  it = CacheMap.begin();while(it != CacheMap.end()) {if(it->first >= hash) {return it->second;}it++;}return NULL;}

4.  main.cpp

/*************************************************************************    > File Name: main.cpp    > Author: ma6174    > Mail: ma6174@163.com     > Created Time: Mon 03 Nov 2014 09:12:53 PM WST ************************************************************************/#include "conhash.h"int main() {Cache *cache1 = new Cache("Machine A", 20);Cache *cache2 = new Cache("Machine B", 30);Cache *cache3 = new Cache("Machine C", 10);Cache *cache4 = new Cache("Machine D", 40);string request;ConHash conhash;conhash.addCache(cache1);conhash.addCache(cache2);conhash.addCache(cache3);conhash.addCache(cache4);cout<<cache1<<" "<<cache2<<" "<<cache3<<" "<<cache4<<endl;while(1) {cout << "Request from...";cin>>request;cout<<conhash.search(request)->getIdentity()<<endl;}return 0;}

5.  Makefile

CC=g++all:$(CC) -g -o main main.cpp conhash.cpp

6. 测试


0 0
原创粉丝点击