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