| 2279 | } |
| 2280 | |
| 2281 | int SetTablePartition( |
| 2282 | const ::fedb::client::TableInfo& table_info, |
| 2283 | ::fedb::nameserver::TableInfo& ns_table_info) { // NOLINT |
| 2284 | if (table_info.table_partition_size() > 0) { |
| 2285 | std::map<uint32_t, std::string> leader_map; |
| 2286 | std::map<uint32_t, std::set<std::string>> follower_map; |
| 2287 | for (int idx = 0; idx < table_info.table_partition_size(); idx++) { |
| 2288 | std::string pid_group = table_info.table_partition(idx).pid_group(); |
| 2289 | uint32_t start_index = 0; |
| 2290 | uint32_t end_index = 0; |
| 2291 | if (::fedb::base::IsNumber(pid_group)) { |
| 2292 | start_index = boost::lexical_cast<uint32_t>(pid_group); |
| 2293 | end_index = start_index; |
| 2294 | } else { |
| 2295 | std::vector<std::string> vec; |
| 2296 | boost::split(vec, pid_group, boost::is_any_of("-")); |
| 2297 | if (vec.size() != 2 || !::fedb::base::IsNumber(vec[0]) || |
| 2298 | !::fedb::base::IsNumber(vec[1])) { |
| 2299 | printf( |
| 2300 | "Fail to create table. pid_group[%s] format error.\n", |
| 2301 | pid_group.c_str()); |
| 2302 | return -1; |
| 2303 | } |
| 2304 | start_index = boost::lexical_cast<uint32_t>(vec[0]); |
| 2305 | end_index = boost::lexical_cast<uint32_t>(vec[1]); |
| 2306 | } |
| 2307 | for (uint32_t pid = start_index; pid <= end_index; pid++) { |
| 2308 | if (table_info.table_partition(idx).is_leader()) { |
| 2309 | if (leader_map.find(pid) != leader_map.end()) { |
| 2310 | printf("Fail to create table. pid %u has two leader\n", |
| 2311 | pid); |
| 2312 | return -1; |
| 2313 | } |
| 2314 | leader_map.insert(std::make_pair( |
| 2315 | pid, table_info.table_partition(idx).endpoint())); |
| 2316 | } else { |
| 2317 | if (follower_map.find(pid) == follower_map.end()) { |
| 2318 | follower_map.insert( |
| 2319 | std::make_pair(pid, std::set<std::string>())); |
| 2320 | } |
| 2321 | if (follower_map[pid].find( |
| 2322 | table_info.table_partition(idx).endpoint()) != |
| 2323 | follower_map[pid].end()) { |
| 2324 | printf( |
| 2325 | "Fail to create table. pid %u has same follower on " |
| 2326 | "%s\n", |
| 2327 | pid, |
| 2328 | table_info.table_partition(idx).endpoint().c_str()); |
| 2329 | return -1; |
| 2330 | } |
| 2331 | follower_map[pid].insert( |
| 2332 | table_info.table_partition(idx).endpoint()); |
| 2333 | } |
| 2334 | } |
| 2335 | } |
| 2336 | if (leader_map.empty()) { |
| 2337 | printf("Fail to create table. has not leader pid\n"); |
| 2338 | return -1; |