00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef GRIDCONSISTENCEHASHIMPL_HPP__INCLUDED
00011 #define GRIDCONSISTENCEHASHIMPL_HPP__INCLUDED
00012
00013 #include <map>
00014 #include <set>
00015 #include <utility>
00016 #include <memory>
00017
00018 #include "gridgain/gridclientuuid.hpp"
00019
00023 class NodeInfo {
00024 public:
00029 NodeInfo(const GridClientUuid& id, const std::shared_ptr<GridClientHasheableObject>& hashId): mId(id), mHashId(hashId) {}
00030
00036 NodeInfo(const NodeInfo& from): mId(from.mId), mHashId(from.mHashId) {}
00037
00043 NodeInfo& operator=(const NodeInfo& right) {
00044 mId = right.id();
00045 mHashId = right.hashId();
00046
00047 return *this;
00048 }
00049
00055 bool operator==(const NodeInfo& right) const {
00056 return mId == right.id() && mHashId->hashCode() == right.hashId()->hashCode();
00057 }
00058
00064 bool operator<(const NodeInfo& right) const {
00065 if (mId == right.id())
00066 return mHashId->hashCode() < right.hashId()->hashCode();
00067
00068 return mId < right.id();
00069 }
00070
00074 GridClientUuid id() const {
00075 return mId;
00076 }
00077
00081 std::shared_ptr<GridClientHasheableObject> hashId() const {
00082 return mHashId;
00083 }
00084
00085 private:
00087 GridClientUuid mId;
00088
00090 std::shared_ptr<GridClientHasheableObject> mHashId;
00091 };
00092
00096 class GridClientConsistentHashImpl {
00097 public:
00098
00102 GridClientConsistentHashImpl();
00103
00107 virtual ~GridClientConsistentHashImpl() {
00108
00109 }
00110
00119 virtual void addNode(const NodeInfo& node, int replicas);
00120
00126 virtual void removeNode(const NodeInfo& node);
00127
00133 virtual std::set<NodeInfo> getNodes() const;
00134
00142 virtual NodeInfo node(const GridClientHasheableObject& value, const std::set<NodeInfo>& inc) const;
00143
00149 size_t nodesSize() const {
00150 return nodes.size();
00151 }
00152
00158 size_t circlesSize() const {
00159 return circle.size();
00160 }
00161
00165 void clear() {
00166 circle.clear();
00167 nodes.clear();
00168 }
00169
00176 int32_t hash(const GridClientHasheableObject& key) const;
00177
00178 private:
00186 void addNodeToCircle(const NodeInfo& node, const int32_t hashCode);
00187
00194 static int32_t hashCode(long value);
00195
00197 std::map<int32_t, std::set<NodeInfo> > circle;
00198
00200 std::set<NodeInfo> nodes;
00201
00203 long affSeed;
00204 };
00205
00206 #endif