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.HBaseTestingUtility;
31  import org.apache.hadoop.hbase.KeyValue;
32  import org.apache.hadoop.hbase.SmallTests;
33  import org.apache.hadoop.hbase.Tag;
34  import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
35  import org.apache.hadoop.hbase.io.hfile.CacheConfig;
36  import org.apache.hadoop.hbase.io.hfile.HFileContext;
37  import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder;
38  import org.apache.hadoop.hbase.util.Bytes;
39  import org.junit.BeforeClass;
40  import org.junit.Test;
41  import org.junit.experimental.categories.Category;
42  
43  @Category(SmallTests.class)
44  public class TestStoreFileScannerWithTagCompression {
45  
46    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
47    private static Configuration conf = TEST_UTIL.getConfiguration();
48    private static CacheConfig cacheConf = new CacheConfig(TEST_UTIL.getConfiguration());
49    private static String ROOT_DIR = TEST_UTIL.getDataTestDir(
50        "TestStoreFileScannerWithTagCompression").toString();
51    private static FileSystem fs = null;
52  
53    @BeforeClass
54    public static void setUp() throws IOException {
55      conf.setInt("hfile.format.version", 3);
56      fs = FileSystem.get(conf);
57    }
58  
59    @Test
60    public void testReseek() throws Exception {
61      // write the file
62      Path f = new Path(ROOT_DIR, "testReseek");
63      HFileContext meta = new HFileContextBuilder().withBlockSize(8 * 1024).withIncludesTags(true)
64          .withCompressTags(true).withDataBlockEncoding(DataBlockEncoding.PREFIX).build();
65      // Make a store file and write data to it.
66      StoreFile.Writer writer = new StoreFile.WriterBuilder(conf, cacheConf, fs).withFilePath(f)
67          .withFileContext(meta).build();
68  
69      writeStoreFile(writer);
70      writer.close();
71  
72      StoreFile.Reader reader = new StoreFile.Reader(fs, f, cacheConf, conf);
73      StoreFileScanner s = reader.getStoreFileScanner(false, false);
74      try {
75        // Now do reseek with empty KV to position to the beginning of the file
76        KeyValue k = KeyValue.createFirstOnRow(Bytes.toBytes("k2"));
77        s.reseek(k);
78        KeyValue kv = s.next();
79        kv = s.next();
80        kv = s.next();
81        byte[] key5 = Bytes.toBytes("k5");
82        assertTrue(Bytes.equals(key5, 0, key5.length, kv.getRowArray(), kv.getRowOffset(),
83            kv.getRowLength()));
84        List<Tag> tags = kv.getTags();
85        assertEquals(1, tags.size());
86        assertEquals("tag3", Bytes.toString(tags.get(0).getValue()));
87      } finally {
88        s.close();
89      }
90    }
91  
92    private void writeStoreFile(final StoreFile.Writer writer) throws IOException {
93      byte[] fam = Bytes.toBytes("f");
94      byte[] qualifier = Bytes.toBytes("q");
95      long now = System.currentTimeMillis();
96      byte[] b = Bytes.toBytes("k1");
97      Tag t1 = new Tag((byte) 1, "tag1");
98      Tag t2 = new Tag((byte) 2, "tag2");
99      Tag t3 = new Tag((byte) 3, "tag3");
100     try {
101       writer.append(new KeyValue(b, fam, qualifier, now, b, new Tag[] { t1 }));
102       b = Bytes.toBytes("k3");
103       writer.append(new KeyValue(b, fam, qualifier, now, b, new Tag[] { t2, t1 }));
104       b = Bytes.toBytes("k4");
105       writer.append(new KeyValue(b, fam, qualifier, now, b, new Tag[] { t3 }));
106       b = Bytes.toBytes("k5");
107       writer.append(new KeyValue(b, fam, qualifier, now, b, new Tag[] { t3 }));
108     } finally {
109       writer.close();
110     }
111   }
112 }