Marine Library  1.0
C++ library for Linux Networking Development
Classes | Public Types | Public Member Functions | List of all members
CConsistentHash< Key, HashKey > Class Template Reference

A lightweight implementation of Consistent Hashng algorithm. More...

#include <consistent_hash.hh>

Public Types

typedef Key key_type
 
typedef uint32_t value_type
 
typedef HashKey< key_type > hasher
 

Public Member Functions

value_type hash (const key_type &key) const
 Get consistent hash result of a key. More...
 
void setValue (value_type value, size_t weight=1000)
 Add, remove or modify weight of a value. More...
 

Detailed Description

template<class Key, template< typename >class HashKey = CHashFn>
class CConsistentHash< Key, HashKey >

A lightweight implementation of Consistent Hashng algorithm.

Consistent Hashing is a technique to map a large number of keys to limited amount of values.
Consider a common use case below:
You have 10 servers to serve for 1 million users, you need to decide which user is served by which server. CConsistentHash can do the mapping easily:

The real power of consistent hashing algorithm is that, when you want to add or remove a server from your servers pool, only a fraction of your users will be redirected to a different server, which could make the impact as small as possible. CConsistentHash makes this operation even easier by using one API setValue only:

Member Function Documentation

template<class Key, template< typename >class HashKey = CHashFn>
value_type CConsistentHash< Key, HashKey >::hash ( const key_type &  key) const
inline

Get consistent hash result of a key.

Parameters
keyA key
Returns
An integer denoting the hash result of key
template<class Key, template< typename >class HashKey = CHashFn>
void CConsistentHash< Key, HashKey >::setValue ( value_type  value,
size_t  weight = 1000 
)
inline

Add, remove or modify weight of a value.

Parameters
valueAn integer denoting a value
weightNew weight of value. If it's 0, value will be removed

The documentation for this class was generated from the following file: