[refactor] improve code readability by reducing if-statement levels
Change-Id: I846a74e3525b0de986b95a05e8a39b476c3daa91
Reviewed-on: http://gerrit.cloudera.org:8080/19239
Tested-by: Kudu Jenkins
Reviewed-by: Yuqi Du <shenxingwuying@gmail.com>
Reviewed-by: Yifan Zhang <chinazhangyifan@163.com>
Reviewed-by: Alexey Serbin <alexey@apache.org>
diff --git a/src/kudu/util/net/dns_resolver.cc b/src/kudu/util/net/dns_resolver.cc
index 5eb9f0d..d1fb9df 100644
--- a/src/kudu/util/net/dns_resolver.cc
+++ b/src/kudu/util/net/dns_resolver.cc
@@ -151,24 +151,30 @@
bool DnsResolver::GetCachedAddresses(const HostPort& hostport,
vector<Sockaddr>* addresses) {
- if (PREDICT_TRUE(cache_)) {
- auto handle = cache_->Get(hostport.host());
- if (handle) {
- if (addresses) {
- // Copy the cached records and set the result port number as necessary:
- // a cached port number is not relevant and stored in the cache as a
- // by-product: HostRecordCache stores not just IPs address, but Sockaddr
- // structures.
- vector<Sockaddr> result_addresses(handle.value());
- for (auto& addr : result_addresses) {
- addr.set_port(hostport.port());
- }
- *addresses = std::move(result_addresses);
- }
- return true;
- }
+ if (PREDICT_FALSE(!cache_)) {
+ // Cache is disabled.
+ return false;
}
- return false;
+
+ auto handle = cache_->Get(hostport.host());
+ if (!handle) {
+ // The key is not found.
+ return false;
+ }
+
+ if (addresses) {
+ // Copy the cached records and set the result port number as necessary:
+ // a cached port number is not relevant and stored in the cache as a
+ // by-product: HostRecordCache stores not just IPs address, but Sockaddr
+ // structures.
+ vector<Sockaddr> result_addresses(handle.value());
+ for (auto& addr : result_addresses) {
+ addr.set_port(hostport.port());
+ }
+ *addresses = std::move(result_addresses);
+ }
+
+ return true;
}
} // namespace kudu