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.mob;
20  
21  import org.apache.hadoop.hbase.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.util.MD5Hash;
23  
24  /**
25   * The mob file name.
26   * It consists of a md5 of a start key, a date and an uuid.
27   * It looks like md5(start) + date + uuid.
28   * <ol>
29   * <li>characters 0-31: md5 hex string of a start key. Since the length of the start key is not
30   * fixed, have to use the md5 instead which has a fix length.</li>
31   * <li>characters 32-39: a string of a date with format yyyymmdd. The date is the latest timestamp
32   * of cells in this file</li>
33   * <li>the remaining characters: the uuid.</li>
34   * </ol>
35   * Using md5 hex string of start key as the prefix of file name makes files with the same start
36   * key unique, they're different from the ones with other start keys
37   * The cells come from different regions might be in the same mob file by region split,
38   * this is allowed.
39   * Has the latest timestamp of cells in the file name in order to clean the expired mob files by
40   * TTL easily. If this timestamp is older than the TTL, it's regarded as expired.
41   */
42  @InterfaceAudience.Private
43  public class MobFileName {
44  
45    private final String date;
46    private final String startKey;
47    private final String uuid;
48    private final String fileName;
49  
50    private static final int STARTKEY_END_INDEX = 32;
51    private static final int DATE_END_INDEX = 40;
52    private static final int UUID_END_INDEX = 72;
53  
54    /**
55     * @param startKey
56     *          The start key.
57     * @param date
58     *          The string of the latest timestamp of cells in this file, the format is yyyymmdd.
59     * @param uuid
60     *          The uuid
61     */
62    private MobFileName(byte[] startKey, String date, String uuid) {
63      this.startKey = MD5Hash.getMD5AsHex(startKey, 0, startKey.length);
64      this.uuid = uuid;
65      this.date = date;
66      this.fileName = this.startKey + this.date + this.uuid;
67    }
68  
69    /**
70     * @param startKey
71     *          The md5 hex string of the start key.
72     * @param date
73     *          The string of the latest timestamp of cells in this file, the format is yyyymmdd.
74     * @param uuid
75     *          The uuid
76     */
77    private MobFileName(String startKey, String date, String uuid) {
78      this.startKey = startKey;
79      this.uuid = uuid;
80      this.date = date;
81      this.fileName = this.startKey + this.date + this.uuid;
82    }
83  
84    /**
85     * Creates an instance of MobFileName
86     *
87     * @param startKey
88     *          The md5 hex string of the start key.
89     * @param date
90     *          The string of the latest timestamp of cells in this file, the format is yyyymmdd.
91     * @param uuid The uuid.
92     * @return An instance of a MobFileName.
93     */
94    public static MobFileName create(byte[] startKey, String date, String uuid) {
95      return new MobFileName(startKey, date, uuid);
96    }
97  
98    /**
99     * Creates an instance of MobFileName
100    *
101    * @param startKey
102    *          The md5 hex string of the start key.
103    * @param date
104    *          The string of the latest timestamp of cells in this file, the format is yyyymmdd.
105    * @param uuid The uuid.
106    * @return An instance of a MobFileName.
107    */
108   public static MobFileName create(String startKey, String date, String uuid) {
109     return new MobFileName(startKey, date, uuid);
110   }
111 
112   /**
113    * Creates an instance of MobFileName.
114    * @param fileName The string format of a file name.
115    * @return An instance of a MobFileName.
116    */
117   public static MobFileName create(String fileName) {
118     // The format of a file name is md5HexString(0-31bytes) + date(32-39bytes) + UUID
119     // The date format is yyyyMMdd
120     String startKey = fileName.substring(0, STARTKEY_END_INDEX);
121     String date = fileName.substring(STARTKEY_END_INDEX, DATE_END_INDEX);
122     String uuid = fileName.substring(DATE_END_INDEX, UUID_END_INDEX);
123     return new MobFileName(startKey, date, uuid);
124   }
125 
126   /**
127    * get startKey from MobFileName.
128    * @param fileName file name.
129    * @return startKey
130    */
131   public static String getStartKeyFromName(final String fileName) {
132     return fileName.substring(0, STARTKEY_END_INDEX);
133   }
134 
135   /**
136    * get date from MobFileName.
137    * @param fileName file name.
138    * @return date
139    */
140   public static String getDateFromName(final String fileName) {
141     return fileName.substring(STARTKEY_END_INDEX, DATE_END_INDEX);
142   }
143 
144   /**
145    * Gets the hex string of the md5 for a start key.
146    * @return The hex string of the md5 for a start key.
147    */
148   public String getStartKey() {
149     return startKey;
150   }
151 
152   /**
153    * Gets the date string. Its format is yyyymmdd.
154    * @return The date string.
155    */
156   public String getDate() {
157     return this.date;
158   }
159 
160   @Override
161   public int hashCode() {
162     return fileName.hashCode();
163   }
164 
165   @Override
166   public boolean equals(Object anObject) {
167     if (this == anObject) {
168       return true;
169     }
170     if (anObject instanceof MobFileName) {
171       MobFileName another = (MobFileName) anObject;
172       return this.getFileName().equals(another.getFileName());
173     }
174     return false;
175   }
176 
177   /**
178    * Gets the file name.
179    * @return The file name.
180    */
181   public String getFileName() {
182     return this.fileName;
183   }
184 }