std::pair<std::string, int> cht::find_predecessor(const std::string& key) { std::vector<std::string> hlist; get_hashlist_(key, hlist); std::string hash = make_hash(key); std::string path; build_actor_path(path, type_, name_); path += "/cht"; std::vector<std::string>::iterator node0 = std::lower_bound(hlist.begin(), hlist.end(), hash); size_t idx = (static_cast<int>(node0 - hlist.begin()) + hlist.size() - 1) % hlist.size(); std::string ip; int port; std::string loc; if (lock_service_->read(path + "/" + hlist[idx], loc)) { revert(loc, ip, port); return make_pair(ip, port); } else { throw JUBATUS_EXCEPTION(core::common::not_found(path)); // TODO(kuenishi): output log and throw exception } }
// return at most n nodes, if theres nodes less than n, return size is also less // than n. // find(hash) :: lock_service -> key -> [node] // where hash(node0) <= hash(key) < hash(node1) bool cht::find( const std::string& key, std::vector<std::pair<std::string, int> >& out, size_t n) { out.clear(); std::vector<std::string> hlist; if (!get_hashlist_(key, hlist)) { throw JUBATUS_EXCEPTION(core::common::not_found(key)); } std::string hash = make_hash(key); std::string path; build_actor_path(path, type_, name_); path += "/cht"; std::vector<std::string>::iterator node0 = std::lower_bound(hlist.begin(), hlist.end(), hash); size_t idx = static_cast<int>(node0 - hlist.begin()) % hlist.size(); std::string loc; for (size_t i = 0; i < n; ++i) { std::string ip; int port; if (lock_service_->read(path + "/" + hlist[idx], loc)) { revert(loc, ip, port); out.push_back(make_pair(ip, port)); } else { // TODO(kuenishi): output log throw JUBATUS_EXCEPTION(core::common::not_found(path)); } idx++; idx %= hlist.size(); } return !hlist.size(); }
// return at most n nodes, if theres nodes less than n, return size is also less than n. // find(hash) :: lock_service -> key -> [node] where hash(node0) <= hash(key) < hash(node1) bool cht::find(const std::string& key, std::vector<std::pair<std::string,int> >& out, size_t n){ out.clear(); std::vector<std::string> hlist; if(! get_hashlist_(key, hlist)){ throw JUBATUS_EXCEPTION(not_found(key)); } std::string hash = make_hash(key); std::string path = ACTOR_BASE_PATH + "/" + name_ + "/cht"; std::vector<std::string>::iterator node0 = std::lower_bound(hlist.begin(), hlist.end(), hash); size_t idx = int(node0 - hlist.begin()) % hlist.size(); std::string loc; for(size_t i=0; i<n; ++i){ std::string ip; int port; if(lock_service_->read(path + "/" + hlist[idx], loc)){ revert(loc, ip, port); out.push_back(make_pair(ip,port)); }else{ // TODO: output log throw JUBATUS_EXCEPTION(not_found(path)); } idx++; idx %= hlist.size(); } return !hlist.size(); }
std::pair<std::string,int> cht::find_predecessor(const std::string& key){ std::vector<std::string> hlist; get_hashlist_(key, hlist); std::string hash = make_hash(key); std::string path = ACTOR_BASE_PATH + "/" + name_ + "/cht"; std::vector<std::string>::iterator node0 = std::lower_bound(hlist.begin(), hlist.end(), hash); size_t idx = (int(node0 - hlist.begin())+ hlist.size() -1) % hlist.size(); std::string ip; int port; std::string loc; if(lock_service_->read(path + "/" + hlist[idx], loc)){ revert(loc, ip, port); return make_pair(ip, port); }else{ throw JUBATUS_EXCEPTION(not_found(path)); // TODO: output log and throw exception } }