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.io.hfile;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNotEquals;
022import static org.junit.Assert.assertTrue;
023
024import java.io.ByteArrayOutputStream;
025import java.io.PrintStream;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.fs.FileSystem;
028import org.apache.hadoop.fs.Path;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.HBaseTestingUtil;
031import org.apache.hadoop.hbase.regionserver.TestHRegionServerBulkLoad;
032import org.apache.hadoop.hbase.testclassification.IOTests;
033import org.apache.hadoop.hbase.testclassification.SmallTests;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.apache.hadoop.hbase.util.CommonFSUtils;
036import org.junit.After;
037import org.junit.Before;
038import org.junit.ClassRule;
039import org.junit.Test;
040import org.junit.experimental.categories.Category;
041import org.slf4j.Logger;
042import org.slf4j.LoggerFactory;
043
044@Category({ IOTests.class, SmallTests.class })
045public class TestHFilePrettyPrinter {
046
047  @ClassRule
048  public static final HBaseClassTestRule CLASS_RULE =
049    HBaseClassTestRule.forClass(TestHFilePrettyPrinter.class);
050
051  private static final Logger LOG = LoggerFactory.getLogger(TestHFilePrettyPrinter.class);
052
053  private final static HBaseTestingUtil UTIL = new HBaseTestingUtil();
054  private static FileSystem fs;
055  private static Configuration conf;
056  private static byte[] cf = Bytes.toBytes("cf");
057  private static byte[] fam = Bytes.toBytes("fam");
058  private static byte[] value = Bytes.toBytes("val");
059  private static PrintStream original;
060  private static PrintStream ps;
061  private static ByteArrayOutputStream stream;
062
063  @Before
064  public void setup() throws Exception {
065    conf = UTIL.getConfiguration();
066    // Runs on local filesystem. Test does not need sync. Turn off checks.
067    conf.setBoolean(CommonFSUtils.UNSAFE_STREAM_CAPABILITY_ENFORCE, false);
068    fs = UTIL.getTestFileSystem();
069    stream = new ByteArrayOutputStream();
070    ps = new PrintStream(stream);
071  }
072
073  @After
074  public void teardown() {
075    original = System.out;
076    System.setOut(original);
077  }
078
079  @Test
080  public void testHFilePrettyPrinterNonRootDir() throws Exception {
081    Path fileNotInRootDir = UTIL.getDataTestDir("hfile");
082    TestHRegionServerBulkLoad.createHFile(fs, fileNotInRootDir, cf, fam, value, 1000);
083    assertNotEquals("directory used is not an HBase root dir", UTIL.getDefaultRootDirPath(),
084      fileNotInRootDir);
085
086    System.setOut(ps);
087    new HFilePrettyPrinter(conf).run(new String[] { "-v", String.valueOf(fileNotInRootDir) });
088    String result = new String(stream.toByteArray());
089    String expectedResult = "Scanning -> " + fileNotInRootDir + "\n" + "Scanned kv count -> 1000\n";
090    assertEquals(expectedResult, result);
091  }
092
093  @Test
094  public void testHFilePrettyPrinterRootDir() throws Exception {
095    Path rootPath = CommonFSUtils.getRootDir(conf);
096    String rootString = rootPath + rootPath.SEPARATOR;
097    Path fileInRootDir = new Path(rootString + "hfile");
098    TestHRegionServerBulkLoad.createHFile(fs, fileInRootDir, cf, fam, value, 1000);
099    assertTrue("directory used is a root dir", fileInRootDir.toString().startsWith(rootString));
100
101    System.setOut(ps);
102    HFilePrettyPrinter printer = new HFilePrettyPrinter();
103    printer.setConf(conf);
104    printer.processFile(fileInRootDir, true);
105    printer.run(new String[] { "-v", String.valueOf(fileInRootDir) });
106    String result = new String(stream.toByteArray());
107    String expectedResult = "Scanning -> " + fileInRootDir + "\n" + "Scanned kv count -> 1000\n";
108    assertEquals(expectedResult, result);
109  }
110
111  @Test
112  public void testHFilePrettyPrinterSeekFirstRow() throws Exception {
113    Path fileNotInRootDir = UTIL.getDataTestDir("hfile");
114    TestHRegionServerBulkLoad.createHFile(fs, fileNotInRootDir, cf, fam, value, 1000);
115    assertNotEquals("directory used is not an HBase root dir", UTIL.getDefaultRootDirPath(),
116      fileNotInRootDir);
117
118    HFile.Reader reader =
119      HFile.createReader(fs, fileNotInRootDir, CacheConfig.DISABLED, true, conf);
120    String firstRowKey = new String(reader.getFirstRowKey().get());
121
122    System.setOut(ps);
123    new HFilePrettyPrinter(conf)
124      .run(new String[] { "-v", "-w" + firstRowKey, String.valueOf(fileNotInRootDir) });
125    String result = new String(stream.toByteArray());
126    String expectedResult = "Scanning -> " + fileNotInRootDir + "\n" + "Scanned kv count -> 1\n";
127    assertEquals(expectedResult, result);
128  }
129}