fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. #include <functional>
  5.  
  6. using namespace std;
  7.  
  8. class ConsistentHashing {
  9. private:
  10. // hash position -> server name
  11. map<size_t, string> ring;
  12.  
  13. size_t getHash(const string& value) {
  14. return hash<string>{}(value);
  15. }
  16.  
  17. public:
  18. void addServer(const string& server) {
  19. size_t hashValue = getHash(server);
  20.  
  21. ring[hashValue] = server;
  22.  
  23. cout << "Added " << server
  24. << " at position " << hashValue << "\n";
  25. }
  26.  
  27. void removeServer(const string& server) {
  28. size_t hashValue = getHash(server);
  29.  
  30. ring.erase(hashValue);
  31.  
  32. cout << "Removed " << server << "\n";
  33. }
  34.  
  35. string getServer(const string& key) {
  36. if (ring.empty())
  37. return "No servers available";
  38.  
  39. size_t keyHash = getHash(key);
  40.  
  41. // Find the first server clockwise
  42. auto it = ring.lower_bound(keyHash);
  43.  
  44. // If we reached the end of the ring,
  45. // wrap around to the first server
  46. if (it == ring.end()) {
  47. it = ring.begin();
  48. }
  49.  
  50. return it->second;
  51. }
  52.  
  53. void printRing() {
  54. cout << "\n--- Hash Ring ---\n";
  55.  
  56. for (auto& [hashValue, server] : ring) {
  57. cout << hashValue
  58. << " -> "
  59. << server << "\n";
  60. }
  61. }
  62. };
  63.  
  64. int main() {
  65.  
  66. ConsistentHashing ch;
  67.  
  68. // Add servers
  69. ch.addServer("Server-A");
  70. ch.addServer("Server-B");
  71. ch.addServer("Server-C");
  72.  
  73. ch.printRing();
  74.  
  75. cout << "\n--- Key Mapping ---\n";
  76.  
  77. string keys[] = {
  78. "user_101",
  79. "user_102",
  80. "user_103",
  81. "user_104",
  82. "user_105"
  83. };
  84.  
  85. for (const string& key : keys) {
  86. cout << key
  87. << " -> "
  88. << ch.getServer(key)
  89. << "\n";
  90. }
  91.  
  92. // Add a new server
  93. cout << "\nAdding Server-D...\n\n";
  94.  
  95. ch.addServer("Server-D");
  96.  
  97. cout << "--- Key Mapping After Adding Server-D ---\n";
  98.  
  99. for (const string& key : keys) {
  100. cout << key
  101. << " -> "
  102. << ch.getServer(key)
  103. << "\n";
  104. }
  105.  
  106. return 0;
  107. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Added Server-A at position 1934068100283367141
Added Server-B at position 8475661567887905832
Added Server-C at position 8522928446594188189

--- Hash Ring ---
1934068100283367141 -> Server-A
8475661567887905832 -> Server-B
8522928446594188189 -> Server-C

--- Key Mapping ---
user_101 -> Server-A
user_102 -> Server-A
user_103 -> Server-A
user_104 -> Server-A
user_105 -> Server-A

Adding Server-D...

Added Server-D at position 15032801287774326687
--- Key Mapping After Adding Server-D ---
user_101 -> Server-D
user_102 -> Server-A
user_103 -> Server-A
user_104 -> Server-A
user_105 -> Server-A