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 static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024import java.util.List;
025import java.util.concurrent.atomic.AtomicInteger;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.fs.FileSystem;
028import org.apache.hadoop.fs.Path;
029import org.apache.hadoop.hbase.ArrayBackedTag;
030import org.apache.hadoop.hbase.Cell;
031import org.apache.hadoop.hbase.HBaseClassTestRule;
032import org.apache.hadoop.hbase.HBaseTestingUtility;
033import org.apache.hadoop.hbase.KeyValue;
034import org.apache.hadoop.hbase.KeyValueUtil;
035import org.apache.hadoop.hbase.PrivateCellUtil;
036import org.apache.hadoop.hbase.Tag;
037import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
038import org.apache.hadoop.hbase.io.hfile.CacheConfig;
039import org.apache.hadoop.hbase.io.hfile.HFileContext;
040import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder;
041import org.apache.hadoop.hbase.testclassification.RegionServerTests;
042import org.apache.hadoop.hbase.testclassification.SmallTests;
043import org.apache.hadoop.hbase.util.Bytes;
044import org.junit.BeforeClass;
045import org.junit.ClassRule;
046import org.junit.Test;
047import org.junit.experimental.categories.Category;
048
049@Category({RegionServerTests.class, SmallTests.class})
050public class TestStoreFileScannerWithTagCompression {
051
052  @ClassRule
053  public static final HBaseClassTestRule CLASS_RULE =
054      HBaseClassTestRule.forClass(TestStoreFileScannerWithTagCompression.class);
055
056  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
057  private static Configuration conf = TEST_UTIL.getConfiguration();
058  private static CacheConfig cacheConf = new CacheConfig(TEST_UTIL.getConfiguration());
059  private static String ROOT_DIR = TEST_UTIL.getDataTestDir(
060      "TestStoreFileScannerWithTagCompression").toString();
061  private static FileSystem fs = null;
062
063  @BeforeClass
064  public static void setUp() throws IOException {
065    conf.setInt("hfile.format.version", 3);
066    fs = FileSystem.get(conf);
067  }
068
069  @Test
070  public void testReseek() throws Exception {
071    // write the file
072    Path f = new Path(ROOT_DIR, "testReseek");
073    HFileContext meta = new HFileContextBuilder().withBlockSize(8 * 1024).withIncludesTags(true)
074        .withCompressTags(true).withDataBlockEncoding(DataBlockEncoding.PREFIX).build();
075    // Make a store file and write data to it.
076    StoreFileWriter writer = new StoreFileWriter.Builder(conf, cacheConf, fs).withFilePath(f)
077        .withFileContext(meta).build();
078
079    writeStoreFile(writer);
080    writer.close();
081
082    StoreFileReader reader =
083        new StoreFileReader(fs, f, cacheConf, true, new AtomicInteger(0), true, conf);
084    StoreFileScanner s = reader.getStoreFileScanner(false, false, false, 0, 0, false);
085    try {
086      // Now do reseek with empty KV to position to the beginning of the file
087      KeyValue k = KeyValueUtil.createFirstOnRow(Bytes.toBytes("k2"));
088      s.reseek(k);
089      Cell kv = s.next();
090      kv = s.next();
091      kv = s.next();
092      byte[] key5 = Bytes.toBytes("k5");
093      assertTrue(Bytes.equals(key5, 0, key5.length, kv.getRowArray(), kv.getRowOffset(),
094          kv.getRowLength()));
095      List<Tag> tags = PrivateCellUtil.getTags(kv);
096      assertEquals(1, tags.size());
097      assertEquals("tag3", Bytes.toString(Tag.cloneValue(tags.get(0))));
098    } finally {
099      s.close();
100    }
101  }
102
103  private void writeStoreFile(final StoreFileWriter writer) throws IOException {
104    byte[] fam = Bytes.toBytes("f");
105    byte[] qualifier = Bytes.toBytes("q");
106    long now = System.currentTimeMillis();
107    byte[] b = Bytes.toBytes("k1");
108    Tag t1 = new ArrayBackedTag((byte) 1, "tag1");
109    Tag t2 = new ArrayBackedTag((byte) 2, "tag2");
110    Tag t3 = new ArrayBackedTag((byte) 3, "tag3");
111    try {
112      writer.append(new KeyValue(b, fam, qualifier, now, b, new Tag[] { t1 }));
113      b = Bytes.toBytes("k3");
114      writer.append(new KeyValue(b, fam, qualifier, now, b, new Tag[] { t2, t1 }));
115      b = Bytes.toBytes("k4");
116      writer.append(new KeyValue(b, fam, qualifier, now, b, new Tag[] { t3 }));
117      b = Bytes.toBytes("k5");
118      writer.append(new KeyValue(b, fam, qualifier, now, b, new Tag[] { t3 }));
119    } finally {
120      writer.close();
121    }
122  }
123}