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 final 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 /** 51 * @param startKey 52 * The start key. 53 * @param date 54 * The string of the latest timestamp of cells in this file, the format is yyyymmdd. 55 * @param uuid 56 * The uuid 57 */ 58 private MobFileName(byte[] startKey, String date, String uuid) { 59 this.startKey = MD5Hash.getMD5AsHex(startKey, 0, startKey.length); 60 this.uuid = uuid; 61 this.date = date; 62 this.fileName = this.startKey + date + uuid; 63 } 64 65 /** 66 * @param startKey 67 * The md5 hex string of the start key. 68 * @param date 69 * The string of the latest timestamp of cells in this file, the format is yyyymmdd. 70 * @param uuid 71 * The uuid 72 */ 73 private MobFileName(String startKey, String date, String uuid) { 74 this.startKey = startKey; 75 this.uuid = uuid; 76 this.date = date; 77 this.fileName = this.startKey + date + uuid; 78 } 79 80 /** 81 * Creates an instance of MobFileName 82 * 83 * @param startKey 84 * The start key. 85 * @param date 86 * The string of the latest timestamp of cells in this file, the format is yyyymmdd. 87 * @param uuid The uuid. 88 * @return An instance of a MobFileName. 89 */ 90 public static MobFileName create(byte[] startKey, String date, String uuid) { 91 return new MobFileName(startKey, date, uuid); 92 } 93 94 /** 95 * Creates an instance of MobFileName 96 * 97 * @param startKey 98 * The md5 hex string of the start key. 99 * @param date 100 * The string of the latest timestamp of cells in this file, the format is yyyymmdd. 101 * @param uuid The uuid. 102 * @return An instance of a MobFileName. 103 */ 104 public static MobFileName create(String startKey, String date, String uuid) { 105 return new MobFileName(startKey, date, uuid); 106 } 107 108 /** 109 * Creates an instance of MobFileName. 110 * @param fileName The string format of a file name. 111 * @return An instance of a MobFileName. 112 */ 113 public static MobFileName create(String fileName) { 114 // The format of a file name is md5HexString(0-31bytes) + date(32-39bytes) + UUID 115 // The date format is yyyyMMdd 116 String startKey = fileName.substring(0, 32); 117 String date = fileName.substring(32, 40); 118 String uuid = fileName.substring(40); 119 return new MobFileName(startKey, date, uuid); 120 } 121 122 /** 123 * Gets the hex string of the md5 for a start key. 124 * @return The hex string of the md5 for a start key. 125 */ 126 public String getStartKey() { 127 return startKey; 128 } 129 130 /** 131 * Gets the date string. Its format is yyyymmdd. 132 * @return The date string. 133 */ 134 public String getDate() { 135 return this.date; 136 } 137 138 @Override 139 public int hashCode() { 140 StringBuilder builder = new StringBuilder(); 141 builder.append(startKey); 142 builder.append(date); 143 builder.append(uuid); 144 return builder.toString().hashCode(); 145 } 146 147 @Override 148 public boolean equals(Object anObject) { 149 if (this == anObject) { 150 return true; 151 } 152 if (anObject instanceof MobFileName) { 153 MobFileName another = (MobFileName) anObject; 154 if (this.startKey.equals(another.startKey) && this.date.equals(another.date) 155 && this.uuid.equals(another.uuid)) { 156 return true; 157 } 158 } 159 return false; 160 } 161 162 /** 163 * Gets the file name. 164 * @return The file name. 165 */ 166 public String getFileName() { 167 return this.fileName; 168 } 169 }