| 606 | } |
| 607 | |
| 608 | bool RedisClusterChannel::PickEndpointForKey(const std::string& key, |
| 609 | std::string* endpoint, |
| 610 | int* slot) const { |
| 611 | const int key_slot = HashSlot(key); |
| 612 | if (key_slot < 0 || key_slot >= static_cast<int>(kRedisClusterSlotCount)) { |
| 613 | return false; |
| 614 | } |
| 615 | butil::DoublyBufferedData<std::vector<std::string> >::ScopedPtr s; |
| 616 | if (_db_slot_to_endpoint.Read(&s) != 0 || |
| 617 | static_cast<size_t>(key_slot) >= s->size()) { |
| 618 | return false; |
| 619 | } |
| 620 | const std::string& mapped = (*s)[key_slot]; |
| 621 | if (mapped.empty()) { |
| 622 | return false; |
| 623 | } |
| 624 | *endpoint = mapped; |
| 625 | if (slot != NULL) { |
| 626 | *slot = key_slot; |
| 627 | } |
| 628 | return true; |
| 629 | } |
| 630 | |
| 631 | bool RedisClusterChannel::PickAnyEndpoint(std::string* endpoint) const { |
| 632 | BAIDU_SCOPED_LOCK(_mutex); |