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