-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconhash.cc
More file actions
51 lines (43 loc) · 1.25 KB
/
conhash.cc
File metadata and controls
51 lines (43 loc) · 1.25 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
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (c) 2017 Mirants Lu. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "conhash.h"
#include <assert.h>
#include <stdio.h>
#include "murmurhash3.h"
ConHash::ConHash() {}
void ConHash::AddNode(const Node& node) {
assert(node.replicas > 0);
for (int i = 0; i < node.replicas; ++i) {
uint32_t key = Hash(node.identify, i);
vnodes_.insert(std::make_pair(key, node));
}
}
void ConHash::RemoveNode(const Node& node) {
for (int i = 0; i < node.replicas; ++i) {
uint32_t key = Hash(node.identify, i);
vnodes_.erase(key);
}
}
bool ConHash::Lookup(const std::string& object, Node* node) {
if (vnodes_.empty()) {
return false;
}
uint32_t key;
MurmurHash3_x86_32(object.c_str(), static_cast<int>(object.size()), 0, &key);
std::map<uint32_t, Node>::iterator it = vnodes_.upper_bound(key);
if (it == vnodes_.end()) {
*node = vnodes_.begin()->second;
} else {
*node = it->second;
}
return true;
}
uint32_t ConHash::Hash(const std::string& identify, int i) {
char buf[128];
int len = snprintf(buf, sizeof(buf), "%s#%d", identify.c_str(), i);
assert(len > 0);
uint32_t out;
MurmurHash3_x86_32(buf, len, 0, &out);
return out;
}