码迷,mamicode.com
首页 > 编程语言 > 详细

一致性 hash 算法( consistent hashing )

时间:2014-11-04 22:48:42      阅读:388      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   ar   os   for   sp   on   2014   

1. 代码组织

bubuko.com,布布扣

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 cache
	public:
		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. 测试

bubuko.com,布布扣

一致性 hash 算法( consistent hashing )

标签:blog   http   io   ar   os   for   sp   on   2014   

原文地址:http://blog.csdn.net/wangzhicheng1983/article/details/40793821

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!