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 java.io.IOException;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.fs.FileSystem;
023import org.apache.hadoop.fs.Path;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HBaseConfiguration;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.io.HFileLink;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.apache.hadoop.hbase.util.CommonFSUtils;
030import org.apache.hadoop.hbase.util.HFileArchiveUtil;
031import org.junit.Assert;
032import org.junit.ClassRule;
033import org.junit.Rule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036import org.junit.rules.TestName;
037
038@Category(SmallTests.class)
039public class TestMobFileLink {
040
041  @ClassRule
042  public static final HBaseClassTestRule CLASS_RULE =
043    HBaseClassTestRule.forClass(TestMobFileLink.class);
044
045  @Rule
046  public TestName name = new TestName();
047
048  @Test
049  public void testMobFilePath() throws IOException {
050    final TableName tableName = TableName.valueOf(name.getMethodName());
051    Configuration conf = HBaseConfiguration.create();
052    FileSystem fs = FileSystem.get(conf);
053    Path rootDir = CommonFSUtils.getRootDir(conf);
054    Path tableDir = CommonFSUtils.getTableDir(rootDir, tableName);
055    Path archiveDir = CommonFSUtils.getTableDir(HFileArchiveUtil.getArchivePath(conf), tableName);
056    String fileName = "mobFile";
057    String encodedRegionName = MobUtils.getMobRegionInfo(tableName).getEncodedName();
058    String columnFamily = "columnFamily";
059    Path regionDir = new Path(tableDir, encodedRegionName);
060    Path archivedRegionDir = new Path(archiveDir, encodedRegionName);
061    Path expectedMobFilePath =
062      new Path(MobUtils.getMobFamilyPath(conf, tableName, columnFamily), fileName)
063        .makeQualified(fs.getUri(), fs.getWorkingDirectory());
064    Path expectedOriginPath = new Path(new Path(regionDir, columnFamily), fileName)
065      .makeQualified(fs.getUri(), fs.getWorkingDirectory());
066    Path expectedArchivePath = new Path(new Path(archivedRegionDir, columnFamily), fileName)
067      .makeQualified(fs.getUri(), fs.getWorkingDirectory());
068
069    String hfileLinkName = tableName.getNameAsString() + "=" + encodedRegionName + "-" + fileName;
070    Path hfileLinkPath = new Path(columnFamily, hfileLinkName);
071    HFileLink hfileLink = HFileLink.buildFromHFileLinkPattern(conf, hfileLinkPath);
072    Assert.assertEquals(expectedMobFilePath, hfileLink.getMobPath());
073    Assert.assertEquals(expectedOriginPath, hfileLink.getOriginPath());
074    Assert.assertEquals(expectedArchivePath, hfileLink.getArchivePath());
075  }
076}