View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.io.hfile;
20  
21  import java.io.DataInput;
22  import java.io.DataOutput;
23  import java.io.IOException;
24  
25  import org.apache.hadoop.classification.InterfaceAudience;
26  import org.apache.hadoop.fs.Path;
27  import org.apache.hadoop.io.Writable;
28  
29  /**
30   * BlockCacheColumnFamilySummary represents a summary of the blockCache usage 
31   * at Table/ColumnFamily granularity.
32   * <br><br>
33   * As ColumnFamilies are owned by Tables, a summary by ColumnFamily implies that
34   * the owning Table is included in the summarization.
35   *
36   */
37  @InterfaceAudience.Private
38  public class BlockCacheColumnFamilySummary implements Writable, Comparable<BlockCacheColumnFamilySummary> {
39  
40    private String table = "";
41    private String columnFamily = "";
42    private int blocks;
43    private long heapSize;
44  
45    /**
46     * Default constructor for Writable
47     */
48    public BlockCacheColumnFamilySummary() {
49      
50    }
51    
52    /**
53     * 
54     * @param table table
55     * @param columnFamily columnFamily
56     */
57    public BlockCacheColumnFamilySummary(String table, String columnFamily) {
58      this.table = table;
59      this.columnFamily = columnFamily;
60    }
61    
62    /**
63     * 
64     * @return table
65     */
66    public String getTable() {
67      return table;
68    }
69    /**
70     * 
71     * @param table (table that owns the cached block)
72     */
73    public void setTable(String table) {
74      this.table = table;
75    }
76    /**
77     * 
78     * @return columnFamily
79     */
80    public String getColumnFamily() {
81      return columnFamily;
82    }
83    /**
84     * 
85     * @param columnFamily (columnFamily that owns the cached block)
86     */
87    public void setColumnFamily(String columnFamily) {
88      this.columnFamily = columnFamily;
89    }
90    
91    /**
92     * 
93     * @return blocks in the cache
94     */
95    public int getBlocks() {
96      return blocks;
97    }
98    /**
99     * 
100    * @param blocks in the cache
101    */
102   public void setBlocks(int blocks) {
103     this.blocks = blocks;
104   }
105   
106   /**
107    * 
108    * @return heapSize in the cache
109    */
110   public long getHeapSize() {
111     return heapSize;
112   }
113   
114   /**
115    * Increments the number of blocks in the cache for this entry
116    */
117   public void incrementBlocks() {
118     this.blocks++;
119   }
120 
121   /**
122    * 
123    * @param heapSize to increment
124    */
125   public void incrementHeapSize(long heapSize) {
126     this.heapSize = this.heapSize + heapSize;
127   }
128 
129   /**
130    * 
131    * @param heapSize (total heapSize for the table/CF)
132    */
133   public void setHeapSize(long heapSize) {
134     this.heapSize = heapSize;
135   }
136   
137   @Override
138   public void readFields(DataInput in) throws IOException {
139     table = in.readUTF();
140     columnFamily = in.readUTF();
141     blocks = in.readInt();
142     heapSize = in.readLong();
143   }
144   
145   @Override
146   public void write(DataOutput out) throws IOException {
147     out.writeUTF(table);
148     out.writeUTF(columnFamily);
149     out.writeInt(blocks);
150     out.writeLong(heapSize);
151   }
152   
153   @Override
154   public int hashCode() {
155     final int prime = 31;
156     int result = 1;
157     result = prime * result
158         + ((columnFamily == null) ? 0 : columnFamily.hashCode());
159     result = prime * result + ((table == null) ? 0 : table.hashCode());
160     return result;
161   }
162   @Override
163   public boolean equals(Object obj) {
164     if (this == obj)
165       return true;
166     if (obj == null)
167       return false;
168     if (getClass() != obj.getClass())
169       return false;
170     BlockCacheColumnFamilySummary other = (BlockCacheColumnFamilySummary) obj;
171     if (columnFamily == null) {
172       if (other.columnFamily != null)
173         return false;
174     } else if (!columnFamily.equals(other.columnFamily))
175       return false;
176     if (table == null) {
177       if (other.table != null)
178         return false;
179     } else if (!table.equals(other.table))
180       return false;
181     return true;
182   }
183   
184   
185   
186   @Override
187   public String toString() {
188     return "BlockCacheSummaryEntry [table=" + table + ", columnFamily="
189         + columnFamily + ", blocks=" + blocks + ", heapSize=" + heapSize + "]";
190   }
191   
192   /**
193    * Construct a BlockCacheSummaryEntry from a full StoreFile Path
194    * <br><br>
195    * The path is expected to be in the format of...
196    * <pre>
197    * hdfs://localhost:51169/user/userid/-ROOT-/70236052/info/3944417774205889744
198    * </pre>
199    * ... where: <br>
200    *  '-ROOT-' = Table <br>
201    * '70236052' = Region <br>
202    * 'info' = ColumnFamily <br>
203    * '3944417774205889744' = StoreFile
204    * 
205    * @param path (full StoreFile Path)
206    * @return BlockCacheSummaryEntry
207    */
208   public static BlockCacheColumnFamilySummary createFromStoreFilePath(Path path) {
209        
210     // The full path will look something like this...
211     // hdfs://localhost:51169/user/doug.meil/-ROOT-/70236052/info/3944417774205889744
212     //                                        tbl    region   cf   sf
213     String sp = path.toString();
214     String s[] = sp.split("\\/");
215 
216     BlockCacheColumnFamilySummary bcse = null;
217     if (s.length >= 4) {
218       // why 4?   StoreFile, CF, Region, Table
219       String table = s[s.length - 4];  // 4th from the end
220       String cf = s[s.length - 2];     // 2nd from the end
221       bcse = new BlockCacheColumnFamilySummary(table, cf);
222     } 
223     return bcse;
224   }
225 
226   @Override
227   public int compareTo(BlockCacheColumnFamilySummary o) {
228     int i = table.compareTo(o.getTable());
229     if (i != 0) {
230       return i;
231     } 
232     return columnFamily.compareTo(o.getColumnFamily());
233   }
234 
235   /**
236    * Creates a new BlockCacheSummaryEntry
237    * 
238    * @param e BlockCacheSummaryEntry
239    * @return new BlockCacheSummaryEntry
240    */
241   public static BlockCacheColumnFamilySummary create(BlockCacheColumnFamilySummary e) {
242     BlockCacheColumnFamilySummary e2 = new BlockCacheColumnFamilySummary();
243     e2.setTable(e.getTable());
244     e2.setColumnFamily(e.getColumnFamily());
245     return e2;
246   }
247 }