1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.io.hfile;
20
21 import static org.junit.Assert.assertEquals;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.hadoop.fs.FSDataOutputStream;
27 import org.apache.hadoop.fs.Path;
28 import org.apache.hadoop.hbase.HBaseTestingUtility;
29 import org.apache.hadoop.hbase.KeyValue;
30 import org.apache.hadoop.hbase.SmallTests;
31 import org.apache.hadoop.hbase.util.Bytes;
32 import org.junit.Test;
33 import org.junit.experimental.categories.Category;
34
35
36
37
38 @Category(SmallTests.class)
39 public class TestReseekTo {
40
41 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
42
43 @Test
44 public void testReseekTo() throws Exception {
45
46 Path ncTFile = new Path(TEST_UTIL.getDataTestDir(), "basic.hfile");
47 FSDataOutputStream fout = TEST_UTIL.getTestFileSystem().create(ncTFile);
48 CacheConfig cacheConf = new CacheConfig(TEST_UTIL.getConfiguration());
49 HFile.Writer writer = HFile.getWriterFactory(
50 TEST_UTIL.getConfiguration(), cacheConf)
51 .withOutputStream(fout)
52 .withBlockSize(4000)
53
54 .withComparator(new KeyValue.RawBytesComparator())
55 .create();
56 int numberOfKeys = 1000;
57
58 String valueString = "Value";
59
60 List<Integer> keyList = new ArrayList<Integer>();
61 List<String> valueList = new ArrayList<String>();
62
63 for (int key = 0; key < numberOfKeys; key++) {
64 String value = valueString + key;
65 keyList.add(key);
66 valueList.add(value);
67 writer.append(Bytes.toBytes(key), Bytes.toBytes(value));
68 }
69 writer.close();
70 fout.close();
71
72 HFile.Reader reader = HFile.createReader(TEST_UTIL.getTestFileSystem(),
73 ncTFile, cacheConf);
74 reader.loadFileInfo();
75 HFileScanner scanner = reader.getScanner(false, true);
76
77 scanner.seekTo();
78 for (int i = 0; i < keyList.size(); i++) {
79 Integer key = keyList.get(i);
80 String value = valueList.get(i);
81 long start = System.nanoTime();
82 scanner.seekTo(Bytes.toBytes(key));
83 assertEquals(value, scanner.getValueString());
84 }
85
86 scanner.seekTo();
87 for (int i = 0; i < keyList.size(); i += 10) {
88 Integer key = keyList.get(i);
89 String value = valueList.get(i);
90 long start = System.nanoTime();
91 scanner.reseekTo(Bytes.toBytes(key));
92 assertEquals("i is " + i, value, scanner.getValueString());
93 }
94
95 reader.close();
96 }
97
98
99 }
100