-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsistent_hash.cpp
More file actions
39 lines (32 loc) · 1.14 KB
/
Copy pathconsistent_hash.cpp
File metadata and controls
39 lines (32 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "consistent_hash.hpp"
#include <functional>
ConsistentHash::ConsistentHash(int num_replicas) : num_replicas_(num_replicas) {}
size_t ConsistentHash::hash(const std::string& key) {
return std::hash<std::string>{}(key);
}
void ConsistentHash::add_node(const std::string& node_id) {
std::lock_guard<std::mutex> lock(mutex_);
for (int i = 0; i < num_replicas_; ++i) {
std::string vnode_key = node_id + "#" + std::to_string(i);
size_t hash_val = hash(vnode_key);
ring_[hash_val] = node_id;
}
}
void ConsistentHash::remove_node(const std::string& node_id) {
std::lock_guard<std::mutex> lock(mutex_);
for (int i = 0; i < num_replicas_; ++i) {
std::string vnode_key = node_id + "#" + std::to_string(i);
size_t hash_val = hash(vnode_key);
ring_.erase(hash_val);
}
}
std::string ConsistentHash::get_node(const std::string& key) {
std::lock_guard<std::mutex> lock(mutex_);
if (ring_.empty()) return "";
size_t hash_val = hash(key);
auto it = ring_.lower_bound(hash_val);
if (it == ring_.end()) {
return ring_.begin()->second;
}
return it->second;
}