001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.mob; 020 021import org.apache.yetus.audience.InterfaceAudience; 022import org.apache.hadoop.hbase.util.MD5Hash; 023 024/** 025 * The mob file name. 026 * It consists of a md5 of a start key, a date and an uuid. 027 * It looks like md5(start) + date + uuid. 028 * <ol> 029 * <li>characters 0-31: md5 hex string of a start key. Since the length of the start key is not 030 * fixed, have to use the md5 instead which has a fix length.</li> 031 * <li>characters 32-39: a string of a date with format yyyymmdd. The date is the latest timestamp 032 * of cells in this file</li> 033 * <li>the remaining characters: the uuid.</li> 034 * </ol> 035 * Using md5 hex string of start key as the prefix of file name makes files with the same start 036 * key unique, they're different from the ones with other start keys 037 * The cells come from different regions might be in the same mob file by region split, 038 * this is allowed. 039 * Has the latest timestamp of cells in the file name in order to clean the expired mob files by 040 * TTL easily. If this timestamp is older than the TTL, it's regarded as expired. 041 */ 042@InterfaceAudience.Private 043public final class MobFileName { 044 private final String date; 045 private final String startKey; 046 private final String uuid; 047 private final String fileName; 048 049 private static final int STARTKEY_END_INDEX = 32; 050 private static final int DATE_END_INDEX = 40; 051 private static final int UUID_END_INDEX = 72; 052 053 /** 054 * @param startKey 055 * The start key. 056 * @param date 057 * The string of the latest timestamp of cells in this file, the format is yyyymmdd. 058 * @param uuid 059 * The uuid 060 */ 061 private MobFileName(byte[] startKey, String date, String uuid) { 062 this.startKey = MD5Hash.getMD5AsHex(startKey, 0, startKey.length); 063 this.uuid = uuid; 064 this.date = date; 065 this.fileName = this.startKey + this.date + this.uuid; 066 } 067 068 /** 069 * @param startKey 070 * The md5 hex string of the start key. 071 * @param date 072 * The string of the latest timestamp of cells in this file, the format is yyyymmdd. 073 * @param uuid 074 * The uuid 075 */ 076 private MobFileName(String startKey, String date, String uuid) { 077 this.startKey = startKey; 078 this.uuid = uuid; 079 this.date = date; 080 this.fileName = this.startKey + this.date + this.uuid; 081 } 082 083 /** 084 * Creates an instance of MobFileName 085 * 086 * @param startKey 087 * The md5 hex string of the start key. 088 * @param date 089 * The string of the latest timestamp of cells in this file, the format is yyyymmdd. 090 * @param uuid The uuid. 091 * @return An instance of a MobFileName. 092 */ 093 public static MobFileName create(byte[] startKey, String date, String uuid) { 094 return new MobFileName(startKey, date, uuid); 095 } 096 097 /** 098 * Creates an instance of MobFileName 099 * 100 * @param startKey 101 * The md5 hex string of the start key. 102 * @param date 103 * The string of the latest timestamp of cells in this file, the format is yyyymmdd. 104 * @param uuid The uuid. 105 * @return An instance of a MobFileName. 106 */ 107 public static MobFileName create(String startKey, String date, String uuid) { 108 return new MobFileName(startKey, date, uuid); 109 } 110 111 /** 112 * Creates an instance of MobFileName. 113 * @param fileName The string format of a file name. 114 * @return An instance of a MobFileName. 115 */ 116 public static MobFileName create(String fileName) { 117 // The format of a file name is md5HexString(0-31bytes) + date(32-39bytes) + UUID 118 // The date format is yyyyMMdd 119 String startKey = fileName.substring(0, STARTKEY_END_INDEX); 120 String date = fileName.substring(STARTKEY_END_INDEX, DATE_END_INDEX); 121 String uuid = fileName.substring(DATE_END_INDEX, UUID_END_INDEX); 122 return new MobFileName(startKey, date, uuid); 123 } 124 125 /** 126 * get startKey from MobFileName. 127 * @param fileName file name. 128 * @return startKey 129 */ 130 public static String getStartKeyFromName(final String fileName) { 131 return fileName.substring(0, STARTKEY_END_INDEX); 132 } 133 134 /** 135 * get date from MobFileName. 136 * @param fileName file name. 137 * @return date 138 */ 139 public static String getDateFromName(final String fileName) { 140 return fileName.substring(STARTKEY_END_INDEX, DATE_END_INDEX); 141 } 142 143 /** 144 * Gets the hex string of the md5 for a start key. 145 * @return The hex string of the md5 for a start key. 146 */ 147 public String getStartKey() { 148 return startKey; 149 } 150 151 /** 152 * Gets the date string. Its format is yyyymmdd. 153 * @return The date string. 154 */ 155 public String getDate() { 156 return this.date; 157 } 158 159 @Override 160 public int hashCode() { 161 return fileName.hashCode(); 162 } 163 164 @Override 165 public boolean equals(Object anObject) { 166 if (this == anObject) { 167 return true; 168 } 169 if (anObject instanceof MobFileName) { 170 MobFileName another = (MobFileName) anObject; 171 return this.getFileName().equals(another.getFileName()); 172 } 173 return false; 174 } 175 176 /** 177 * Gets the file name. 178 * @return The file name. 179 */ 180 public String getFileName() { 181 return this.fileName; 182 } 183}