View Javadoc

1   /**
2    * Copyright 2007 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase;
21  
22  import org.apache.hadoop.hbase.util.Addressing;
23  
24  /**
25   * Data structure to hold HRegionInfo and the address for the hosting
26   * HRegionServer.  Immutable.  Comparable, but we compare the 'location' only:
27   * i.e. the hostname and port, and *not* the regioninfo.  This means two
28   * instances are the same if they refer to the same 'location' (the same
29   * hostname and port), though they may be carrying different regions.
30   */
31  public class HRegionLocation implements Comparable<HRegionLocation> {
32    private final HRegionInfo regionInfo;
33    private final String hostname;
34    private final int port;
35    // Cache of the 'toString' result.
36    private String cachedString = null;
37    // Cache of the hostname + port
38    private String cachedHostnamePort;
39  
40    /**
41     * Constructor
42     * @param regionInfo the HRegionInfo for the region
43     * @param hostname Hostname
44     * @param port port
45     */
46    public HRegionLocation(HRegionInfo regionInfo, final String hostname,
47        final int port) {
48      this.regionInfo = regionInfo;
49      this.hostname = hostname;
50      this.port = port;
51    }
52  
53    /**
54     * @see java.lang.Object#toString()
55     */
56    @Override
57    public synchronized String toString() {
58      if (this.cachedString == null) {
59        this.cachedString = "region=" + this.regionInfo.getRegionNameAsString() +
60        ", hostname=" + this.hostname + ", port=" + this.port;
61      }
62      return this.cachedString;
63    }
64  
65    /**
66     * @see java.lang.Object#equals(java.lang.Object)
67     */
68    @Override
69    public boolean equals(Object o) {
70      if (this == o) {
71        return true;
72      }
73      if (o == null) {
74        return false;
75      }
76      if (!(o instanceof HRegionLocation)) {
77        return false;
78      }
79      return this.compareTo((HRegionLocation)o) == 0;
80    }
81  
82    /**
83     * @see java.lang.Object#hashCode()
84     */
85    @Override
86    public int hashCode() {
87      int result = this.hostname.hashCode();
88      result ^= this.port;
89      return result;
90    }
91  
92    /** @return HRegionInfo */
93    public HRegionInfo getRegionInfo(){
94      return regionInfo;
95    }
96  
97    /**
98     * Do not use!!! Creates a HServerAddress instance which will do a resolve.
99     * @return HServerAddress
100    * @deprecated Use {@link #getHostnamePort}
101    */
102   public HServerAddress getServerAddress() {
103     return new HServerAddress(this.hostname, this.port);
104   }
105 
106   public String getHostname() {
107     return this.hostname;
108   }
109 
110   public int getPort() {
111     return this.port;
112   }
113 
114   /**
115    * @return String made of hostname and port formatted as per {@link Addressing#createHostAndPortStr(String, int)}
116    */
117   public synchronized String getHostnamePort() {
118     if (this.cachedHostnamePort == null) {
119       this.cachedHostnamePort =
120         Addressing.createHostAndPortStr(this.hostname, this.port);
121     }
122     return this.cachedHostnamePort;
123   }
124 
125   //
126   // Comparable
127   //
128 
129   public int compareTo(HRegionLocation o) {
130     int result = this.hostname.compareTo(o.getHostname());
131     if (result != 0) return result;
132     return this.port - o.getPort();
133   }
134 }