View Javadoc

1   /*
2    * Copyright 2010 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  
21  package org.apache.hadoop.hbase.rest.model;
22  
23  import java.io.Serializable;
24  
25  import javax.xml.bind.annotation.XmlAttribute;
26  import javax.xml.bind.annotation.XmlRootElement;
27  
28  import org.apache.hadoop.hbase.util.Bytes;
29  
30  /**
31   * Representation of a region of a table and its current location on the
32   * storage cluster.
33   * 
34   * <pre>
35   * &lt;complexType name="TableRegion"&gt;
36   *   &lt;attribute name="name" type="string"&gt;&lt;/attribute&gt;
37   *   &lt;attribute name="id" type="int"&gt;&lt;/attribute&gt;
38   *   &lt;attribute name="startKey" type="base64Binary"&gt;&lt;/attribute&gt;
39   *   &lt;attribute name="endKey" type="base64Binary"&gt;&lt;/attribute&gt;
40   *   &lt;attribute name="location" type="string"&gt;&lt;/attribute&gt;
41   *  &lt;/complexType&gt;
42   * </pre>
43   */
44  @XmlRootElement(name="Region")
45  public class TableRegionModel implements Serializable {
46  
47    private static final long serialVersionUID = 1L;
48  
49    private String table;
50    private long id;
51    private byte[] startKey; 
52    private byte[] endKey;
53    private String location;
54  
55    /**
56     * Constructor
57     */
58    public TableRegionModel() {}
59  
60    /**
61     * Constructor
62     * @param table the table name
63     * @param id the encoded id of the region
64     * @param startKey the start key of the region
65     * @param endKey the end key of the region
66     */
67    public TableRegionModel(String table, long id, byte[] startKey,
68        byte[] endKey) {
69      this(table, id, startKey, endKey, null);
70    }
71  
72    /**
73     * Constructor
74     * @param table the table name
75     * @param id the encoded id of the region
76     * @param startKey the start key of the region
77     * @param endKey the end key of the region
78     * @param location the name and port of the region server hosting the region
79     */
80    public TableRegionModel(String table, long id, byte[] startKey,
81        byte[] endKey, String location) {
82      this.table = table;
83      this.id = id;
84      this.startKey = startKey;
85      this.endKey = endKey;
86      this.location = location;
87    }
88  
89    /**
90     * @return the region name
91     */
92    @XmlAttribute
93    public String getName() {
94      StringBuilder sb = new StringBuilder();
95      sb.append(table);
96      sb.append(',');
97      sb.append(Bytes.toString(startKey));
98      sb.append(',');
99      sb.append(id);
100     return sb.toString();
101   }
102 
103   /**
104    * @return the encoded region id
105    */
106   @XmlAttribute 
107   public long getId() {
108     return id;
109   }
110 
111   /**
112    * @return the start key
113    */
114   @XmlAttribute 
115   public byte[] getStartKey() {
116     return startKey;
117   }
118 
119   /**
120    * @return the end key
121    */
122   @XmlAttribute 
123   public byte[] getEndKey() {
124     return endKey;
125   }
126 
127   /**
128    * @return the name and port of the region server hosting the region
129    */
130   @XmlAttribute 
131   public String getLocation() {
132     return location;
133   }
134 
135   /**
136    * @param name region printable name
137    */
138   public void setName(String name) {
139     String split[] = name.split(",");
140     table = split[0];
141     startKey = Bytes.toBytes(split[1]);
142     id = Long.valueOf(split[2]);
143   }
144 
145   /**
146    * @param id the region's encoded id
147    */
148   public void setId(long id) {
149     this.id = id;
150   }
151 
152   /**
153    * @param startKey the start key
154    */
155   public void setStartKey(byte[] startKey) {
156     this.startKey = startKey;
157   }
158 
159   /**
160    * @param endKey the end key
161    */
162   public void setEndKey(byte[] endKey) {
163     this.endKey = endKey;
164   }
165 
166   /**
167    * @param location the name and port of the region server hosting the region
168    */
169   public void setLocation(String location) {
170     this.location = location;
171   }
172 
173   /* (non-Javadoc)
174    * @see java.lang.Object#toString()
175    */
176   @Override
177   public String toString() {
178     StringBuilder sb = new StringBuilder();
179     sb.append(getName());
180     sb.append(" [\n  id=");
181     sb.append(id);
182     sb.append("\n  startKey='");
183     sb.append(Bytes.toString(startKey));
184     sb.append("'\n  endKey='");
185     sb.append(Bytes.toString(endKey));
186     if (location != null) {
187       sb.append("'\n  location='");
188       sb.append(location);
189     }
190     sb.append("'\n]\n");
191     return sb.toString();
192   }
193 }