View Javadoc

1   /**
2    * Copyright 2011 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.io.hfile;
21  
22  import java.io.DataInput;
23  import java.io.DataOutput;
24  import java.io.IOException;
25  
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  public class BlockCacheColumnFamilySummary implements Writable, Comparable<BlockCacheColumnFamilySummary> {
38  
39    private String table = "";
40    private String columnFamily = "";
41    private int blocks;
42    private long heapSize;
43  
44    /**
45     * Default constructor for Writable
46     */
47    public BlockCacheColumnFamilySummary() {
48      
49    }
50    
51    /**
52     * 
53     * @param table table
54     * @param columnFamily columnFamily
55     */
56    public BlockCacheColumnFamilySummary(String table, String columnFamily) {
57      this.table = table;
58      this.columnFamily = columnFamily;
59    }
60    
61    /**
62     * 
63     * @return table
64     */
65    public String getTable() {
66      return table;
67    }
68    /**
69     * 
70     * @param table (table that owns the cached block)
71     */
72    public void setTable(String table) {
73      this.table = table;
74    }
75    /**
76     * 
77     * @return columnFamily
78     */
79    public String getColumnFamily() {
80      return columnFamily;
81    }
82    /**
83     * 
84     * @param columnFamily (columnFamily that owns the cached block)
85     */
86    public void setColumnFamily(String columnFamily) {
87      this.columnFamily = columnFamily;
88    }
89    
90    /**
91     * 
92     * @return blocks in the cache
93     */
94    public int getBlocks() {
95      return blocks;
96    }
97    /**
98     * 
99     * @param blocks in the cache
100    */
101   public void setBlocks(int blocks) {
102     this.blocks = blocks;
103   }
104   
105   /**
106    * 
107    * @return heapSize in the cache
108    */
109   public long getHeapSize() {
110     return heapSize;
111   }
112   
113   /**
114    * Increments the number of blocks in the cache for this entry
115    */
116   public void incrementBlocks() {
117     this.blocks++;
118   }
119 
120   /**
121    * 
122    * @param heapSize to increment
123    */
124   public void incrementHeapSize(long heapSize) {
125     this.heapSize = this.heapSize + heapSize;
126   }
127 
128   /**
129    * 
130    * @param heapSize (total heapSize for the table/CF)
131    */
132   public void setHeapSize(long heapSize) {
133     this.heapSize = heapSize;
134   }
135   
136   @Override
137   public void readFields(DataInput in) throws IOException {
138     table = in.readUTF();
139     columnFamily = in.readUTF();
140     blocks = in.readInt();
141     heapSize = in.readLong();
142   }
143   
144   @Override
145   public void write(DataOutput out) throws IOException {
146     out.writeUTF(table);
147     out.writeUTF(columnFamily);
148     out.writeInt(blocks);
149     out.writeLong(heapSize);
150   }
151   
152   @Override
153   public int hashCode() {
154     final int prime = 31;
155     int result = 1;
156     result = prime * result
157         + ((columnFamily == null) ? 0 : columnFamily.hashCode());
158     result = prime * result + ((table == null) ? 0 : table.hashCode());
159     return result;
160   }
161   @Override
162   public boolean equals(Object obj) {
163     if (this == obj)
164       return true;
165     if (obj == null)
166       return false;
167     if (getClass() != obj.getClass())
168       return false;
169     BlockCacheColumnFamilySummary other = (BlockCacheColumnFamilySummary) obj;
170     if (columnFamily == null) {
171       if (other.columnFamily != null)
172         return false;
173     } else if (!columnFamily.equals(other.columnFamily))
174       return false;
175     if (table == null) {
176       if (other.table != null)
177         return false;
178     } else if (!table.equals(other.table))
179       return false;
180     return true;
181   }
182   
183   
184   
185   @Override
186   public String toString() {
187     return "BlockCacheSummaryEntry [table=" + table + ", columnFamily="
188         + columnFamily + ", blocks=" + blocks + ", heapSize=" + heapSize + "]";
189   }
190   
191   /**
192    * Construct a BlockCacheSummaryEntry from a full StoreFile Path
193    * <br><br>
194    * The path is expected to be in the format of...
195    * <pre>
196    * hdfs://localhost:51169/user/userid/-ROOT-/70236052/info/3944417774205889744
197    * </pre>
198    * ... where: <br>
199    *  '-ROOT-' = Table <br>
200    * '70236052' = Region <br>
201    * 'info' = ColumnFamily <br>
202    * '3944417774205889744' = StoreFile
203    * 
204    * @param path (full StoreFile Path)
205    * @return BlockCacheSummaryEntry
206    */
207   public static BlockCacheColumnFamilySummary createFromStoreFilePath(Path path) {
208        
209     // The full path will look something like this...
210     // hdfs://localhost:51169/user/doug.meil/-ROOT-/70236052/info/3944417774205889744
211     //                                        tbl    region   cf   sf
212     String sp = path.toString();
213     String s[] = sp.split("\\/");
214 
215     BlockCacheColumnFamilySummary bcse = null;
216     if (s.length >= 4) {
217       // why 4?   StoreFile, CF, Region, Table
218       String table = s[s.length - 4];  // 4th from the end
219       String cf = s[s.length - 2];     // 2nd from the end
220       bcse = new BlockCacheColumnFamilySummary(table, cf);
221     } 
222     return bcse;
223   }
224 
225   @Override
226   public int compareTo(BlockCacheColumnFamilySummary o) {
227     int i = table.compareTo(o.getTable());
228     if (i != 0) {
229       return i;
230     } 
231     return columnFamily.compareTo(o.getColumnFamily());
232   }
233 
234   /**
235    * Creates a new BlockCacheSummaryEntry
236    * 
237    * @param e BlockCacheSummaryEntry
238    * @return new BlockCacheSummaryEntry
239    */
240   public static BlockCacheColumnFamilySummary create(BlockCacheColumnFamilySummary e) {
241     BlockCacheColumnFamilySummary e2 = new BlockCacheColumnFamilySummary();
242     e2.setTable(e.getTable());
243     e2.setColumnFamily(e.getColumnFamily());
244     return e2;
245   }
246 }