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.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.apache.hadoop.fs.FSDataOutputStream;
28 import org.apache.hadoop.fs.Path;
29 import org.apache.hadoop.hbase.HBaseTestingUtility;
30 import org.apache.hadoop.hbase.HConstants;
31 import org.apache.hadoop.hbase.KeyValue;
32 import org.apache.hadoop.hbase.testclassification.SmallTests;
33 import org.apache.hadoop.hbase.Tag;
34 import org.apache.hadoop.hbase.util.Bytes;
35 import org.junit.Test;
36 import org.junit.experimental.categories.Category;
37
38
39
40
41 @Category(SmallTests.class)
42 public class TestReseekTo {
43
44 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
45
46 @Test
47 public void testReseekTo() throws Exception {
48 testReseekToInternals(TagUsage.NO_TAG);
49 testReseekToInternals(TagUsage.ONLY_TAG);
50 testReseekToInternals(TagUsage.PARTIAL_TAG);
51 }
52
53 private void testReseekToInternals(TagUsage tagUsage) throws IOException {
54 Path ncTFile = new Path(TEST_UTIL.getDataTestDir(), "basic.hfile");
55 FSDataOutputStream fout = TEST_UTIL.getTestFileSystem().create(ncTFile);
56 if(tagUsage != TagUsage.NO_TAG){
57 TEST_UTIL.getConfiguration().setInt("hfile.format.version", 3);
58 }
59 CacheConfig cacheConf = new CacheConfig(TEST_UTIL.getConfiguration());
60 HFileContext context = new HFileContextBuilder().withBlockSize(4000).build();
61 HFile.Writer writer = HFile.getWriterFactory(
62 TEST_UTIL.getConfiguration(), cacheConf)
63 .withOutputStream(fout)
64 .withFileContext(context)
65
66 .withComparator(KeyValue.COMPARATOR)
67 .create();
68 int numberOfKeys = 1000;
69
70 String valueString = "Value";
71
72 List<Integer> keyList = new ArrayList<Integer>();
73 List<String> valueList = new ArrayList<String>();
74
75 for (int key = 0; key < numberOfKeys; key++) {
76 String value = valueString + key;
77 KeyValue kv;
78 keyList.add(key);
79 valueList.add(value);
80 if(tagUsage == TagUsage.NO_TAG){
81 kv = new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes.toBytes("qual"),
82 Bytes.toBytes(value));
83 writer.append(kv);
84 } else if (tagUsage == TagUsage.ONLY_TAG) {
85 Tag t = new Tag((byte) 1, "myTag1");
86 Tag[] tags = new Tag[1];
87 tags[0] = t;
88 kv = new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes.toBytes("qual"),
89 HConstants.LATEST_TIMESTAMP, Bytes.toBytes(value), tags);
90 writer.append(kv);
91 } else {
92 if (key % 4 == 0) {
93 Tag t = new Tag((byte) 1, "myTag1");
94 Tag[] tags = new Tag[1];
95 tags[0] = t;
96 kv = new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes.toBytes("qual"),
97 HConstants.LATEST_TIMESTAMP, Bytes.toBytes(value), tags);
98 writer.append(kv);
99 } else {
100 kv = new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes.toBytes("qual"),
101 HConstants.LATEST_TIMESTAMP, Bytes.toBytes(value));
102 writer.append(kv);
103 }
104 }
105 }
106 writer.close();
107 fout.close();
108
109 HFile.Reader reader = HFile.createReader(TEST_UTIL.getTestFileSystem(),
110 ncTFile, cacheConf, TEST_UTIL.getConfiguration());
111 reader.loadFileInfo();
112 HFileScanner scanner = reader.getScanner(false, true);
113
114 scanner.seekTo();
115 for (int i = 0; i < keyList.size(); i++) {
116 Integer key = keyList.get(i);
117 String value = valueList.get(i);
118 long start = System.nanoTime();
119 scanner.seekTo(new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes
120 .toBytes("qual"), Bytes.toBytes(value)));
121 assertEquals(value, scanner.getValueString());
122 }
123
124 scanner.seekTo();
125 for (int i = 0; i < keyList.size(); i += 10) {
126 Integer key = keyList.get(i);
127 String value = valueList.get(i);
128 long start = System.nanoTime();
129 scanner.reseekTo(new KeyValue(Bytes.toBytes(key), Bytes.toBytes("family"), Bytes
130 .toBytes("qual"), Bytes.toBytes(value)));
131 assertEquals("i is " + i, value, scanner.getValueString());
132 }
133
134 reader.close();
135 }
136
137
138 }
139