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.regionserver;
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.ArrayBackedTag;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseConfiguration;
027import org.apache.hadoop.hbase.HBaseTestingUtil;
028import org.apache.hadoop.hbase.KeyValue;
029import org.apache.hadoop.hbase.Tag;
030import org.apache.hadoop.hbase.io.compress.Compression;
031import org.apache.hadoop.hbase.io.hfile.HFileContext;
032import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder;
033import org.apache.hadoop.hbase.testclassification.MiscTests;
034import org.apache.hadoop.hbase.testclassification.SmallTests;
035import org.apache.hadoop.hbase.util.Bytes;
036import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
037import org.junit.Before;
038import org.junit.ClassRule;
039import org.junit.Test;
040import org.junit.experimental.categories.Category;
041
042/**
043 * Test DataBlockEncodingTool.
044 */
045@Category({ MiscTests.class, SmallTests.class })
046public class TestDataBlockEncodingTool {
047
048  @ClassRule
049  public static final HBaseClassTestRule CLASS_RULE =
050    HBaseClassTestRule.forClass(TestDataBlockEncodingTool.class);
051
052  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
053  private static final String ROOT_DIR =
054    TEST_UTIL.getDataTestDir("TestDataBlockEncodingTool").toString();
055  private static final Configuration conf = TEST_UTIL.getConfiguration();
056  private static FileSystem fs;
057  private static StoreFileWriter sfw;
058
059  @Before
060  public void setUp() throws IOException {
061    fs = TEST_UTIL.getTestFileSystem();
062  }
063
064  private void testHFile(String fileName, boolean useTags, boolean allTags) throws IOException {
065    Path path = new Path(ROOT_DIR, fileName);
066    try {
067      createHFileWithTags(path, useTags, allTags);
068      testDataBlockingTool(path);
069    } finally {
070      if (fs.exists(path)) {
071        fs.delete(path, false);
072      }
073    }
074  }
075
076  private void createHFileWithTags(Path path, boolean useTags, boolean allTags) throws IOException {
077    HFileContext meta =
078      new HFileContextBuilder().withBlockSize(64 * 1024).withIncludesTags(useTags).build();
079    sfw = new StoreFileWriter.Builder(conf, fs).withFilePath(path).withFileContext(meta).build();
080    long now = EnvironmentEdgeManager.currentTime();
081    byte[] FAMILY = Bytes.toBytes("cf");
082    byte[] QUALIFIER = Bytes.toBytes("q");
083    try {
084      for (char d = 'a'; d <= 'z'; d++) {
085        for (char e = 'a'; e <= 'z'; e++) {
086          byte[] b = new byte[] { (byte) d, (byte) e };
087          KeyValue kv;
088          if (useTags) {
089            if (allTags) {
090              // Write cells with tags to HFile.
091              Tag[] tags = new Tag[] { new ArrayBackedTag((byte) 0, Bytes.toString(b)),
092                new ArrayBackedTag((byte) 0, Bytes.toString(b)) };
093              kv = new KeyValue(b, FAMILY, QUALIFIER, now, b, tags);
094            } else {
095              // Write half cells with tags and half without tags to HFile.
096              if ((e - 'a') % 2 == 0) {
097                kv = new KeyValue(b, FAMILY, QUALIFIER, now, b);
098              } else {
099                Tag[] tags = new Tag[] { new ArrayBackedTag((byte) 0, Bytes.toString(b)),
100                  new ArrayBackedTag((byte) 0, Bytes.toString(b)) };
101                kv = new KeyValue(b, FAMILY, QUALIFIER, now, b, tags);
102              }
103            }
104          } else {
105            // Write cells without tags to HFile.
106            kv = new KeyValue(b, FAMILY, QUALIFIER, now, b);
107          }
108          sfw.append(kv);
109        }
110      }
111      sfw.appendMetadata(0, false);
112    } finally {
113      sfw.close();
114    }
115  }
116
117  private static void testDataBlockingTool(Path path) throws IOException {
118    Configuration conf = HBaseConfiguration.create();
119    int maxKV = Integer.MAX_VALUE;
120    boolean doVerify = true;
121    boolean doBenchmark = true;
122    String testHFilePath = path.toString();
123    DataBlockEncodingTool.testCodecs(conf, maxKV, testHFilePath, Compression.Algorithm.GZ.getName(),
124      doBenchmark, doVerify);
125  }
126
127  @Test
128  public void testHFileAllCellsWithTags() throws IOException {
129    testHFile("1234567890", true, true);
130  }
131
132  @Test
133  public void testHFileAllCellsWithoutTags() throws IOException {
134    testHFile("1234567089", false, false);
135  }
136
137  @Test
138  public void testHFileHalfCellsWithTags() throws IOException {
139    testHFile("1234560789", true, false);
140  }
141}