View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.io.IOException;
25  import java.util.List;
26  
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.hbase.Cell;
31  import org.apache.hadoop.hbase.HBaseTestingUtility;
32  import org.apache.hadoop.hbase.KeyValue;
33  import org.apache.hadoop.hbase.KeyValueUtil;
34  import org.apache.hadoop.hbase.testclassification.SmallTests;
35  import org.apache.hadoop.hbase.Tag;
36  import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
37  import org.apache.hadoop.hbase.io.hfile.CacheConfig;
38  import org.apache.hadoop.hbase.io.hfile.HFileContext;
39  import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder;
40  import org.apache.hadoop.hbase.util.Bytes;
41  import org.junit.BeforeClass;
42  import org.junit.Test;
43  import org.junit.experimental.categories.Category;
44  
45  @Category(SmallTests.class)
46  public class TestStoreFileScannerWithTagCompression {
47  
48    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
49    private static Configuration conf = TEST_UTIL.getConfiguration();
50    private static CacheConfig cacheConf = new CacheConfig(TEST_UTIL.getConfiguration());
51    private static String ROOT_DIR = TEST_UTIL.getDataTestDir(
52        "TestStoreFileScannerWithTagCompression").toString();
53    private static FileSystem fs = null;
54  
55    @BeforeClass
56    public static void setUp() throws IOException {
57      conf.setInt("hfile.format.version", 3);
58      fs = FileSystem.get(conf);
59    }
60  
61    @Test
62    public void testReseek() throws Exception {
63      // write the file
64      Path f = new Path(ROOT_DIR, "testReseek");
65      HFileContext meta = new HFileContextBuilder().withBlockSize(8 * 1024).withIncludesTags(true)
66          .withCompressTags(true).withDataBlockEncoding(DataBlockEncoding.PREFIX).build();
67      // Make a store file and write data to it.
68      StoreFile.Writer writer = new StoreFile.WriterBuilder(conf, cacheConf, fs).withFilePath(f)
69          .withFileContext(meta).build();
70  
71      writeStoreFile(writer);
72      writer.close();
73  
74      StoreFile.Reader reader = new StoreFile.Reader(fs, f, cacheConf, conf);
75      StoreFileScanner s = reader.getStoreFileScanner(false, false);
76      try {
77        // Now do reseek with empty KV to position to the beginning of the file
78        KeyValue k = KeyValueUtil.createFirstOnRow(Bytes.toBytes("k2"));
79        s.reseek(k);
80        Cell kv = s.next();
81        kv = s.next();
82        kv = s.next();
83        byte[] key5 = Bytes.toBytes("k5");
84        assertTrue(Bytes.equals(key5, 0, key5.length, kv.getRowArray(), kv.getRowOffset(),
85            kv.getRowLength()));
86        List<Tag> tags = KeyValueUtil.ensureKeyValue(kv).getTags();
87        assertEquals(1, tags.size());
88        assertEquals("tag3", Bytes.toString(tags.get(0).getValue()));
89      } finally {
90        s.close();
91      }
92    }
93  
94    private void writeStoreFile(final StoreFile.Writer writer) throws IOException {
95      byte[] fam = Bytes.toBytes("f");
96      byte[] qualifier = Bytes.toBytes("q");
97      long now = System.currentTimeMillis();
98      byte[] b = Bytes.toBytes("k1");
99      Tag t1 = new Tag((byte) 1, "tag1");
100     Tag t2 = new Tag((byte) 2, "tag2");
101     Tag t3 = new Tag((byte) 3, "tag3");
102     try {
103       writer.append(new KeyValue(b, fam, qualifier, now, b, new Tag[] { t1 }));
104       b = Bytes.toBytes("k3");
105       writer.append(new KeyValue(b, fam, qualifier, now, b, new Tag[] { t2, t1 }));
106       b = Bytes.toBytes("k4");
107       writer.append(new KeyValue(b, fam, qualifier, now, b, new Tag[] { t3 }));
108       b = Bytes.toBytes("k5");
109       writer.append(new KeyValue(b, fam, qualifier, now, b, new Tag[] { t3 }));
110     } finally {
111       writer.close();
112     }
113   }
114 }