1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.io;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.apache.hadoop.conf.Configuration;
31 import org.apache.hadoop.fs.FileSystem;
32 import org.apache.hadoop.fs.Path;
33 import org.apache.hadoop.hbase.HBaseTestingUtility;
34 import org.apache.hadoop.hbase.KeyValue;
35 import org.apache.hadoop.hbase.io.hfile.CacheConfig;
36 import org.apache.hadoop.hbase.io.hfile.HFile;
37 import org.apache.hadoop.hbase.io.hfile.HFileContext;
38 import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder;
39 import org.apache.hadoop.hbase.io.hfile.HFileScanner;
40 import org.apache.hadoop.hbase.testclassification.SmallTests;
41 import org.apache.hadoop.hbase.util.Bytes;
42 import org.junit.AfterClass;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.junit.experimental.categories.Category;
46
47 @Category(SmallTests.class)
48 public class TestHalfStoreFileReader {
49 private static HBaseTestingUtility TEST_UTIL;
50
51 @BeforeClass
52 public static void setupBeforeClass() throws Exception {
53 TEST_UTIL = new HBaseTestingUtility();
54 }
55
56 @AfterClass
57 public static void tearDownAfterClass() throws Exception {
58 TEST_UTIL.cleanupTestDir();
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 @Test
78 public void testHalfScanAndReseek() throws IOException {
79 String root_dir = TEST_UTIL.getDataTestDir().toString();
80 Path p = new Path(root_dir, "test");
81
82 Configuration conf = TEST_UTIL.getConfiguration();
83 FileSystem fs = FileSystem.get(conf);
84 CacheConfig cacheConf = new CacheConfig(conf);
85 HFileContext meta = new HFileContextBuilder().withBlockSize(1024).build();
86 HFile.Writer w = HFile.getWriterFactory(conf, cacheConf)
87 .withPath(fs, p)
88 .withFileContext(meta)
89 .create();
90
91
92 List<KeyValue> items = genSomeKeys();
93 for (KeyValue kv : items) {
94 w.append(kv);
95 }
96 w.close();
97
98 HFile.Reader r = HFile.createReader(fs, p, cacheConf, conf);
99 r.loadFileInfo();
100 byte [] midkey = r.midkey();
101 KeyValue midKV = KeyValue.createKeyValueFromKey(midkey);
102 midkey = midKV.getRow();
103
104
105
106 Reference bottom = new Reference(midkey, Reference.Range.bottom);
107 doTestOfScanAndReseek(p, fs, bottom, cacheConf);
108
109 Reference top = new Reference(midkey, Reference.Range.top);
110 doTestOfScanAndReseek(p, fs, top, cacheConf);
111
112 r.close();
113 }
114
115 private void doTestOfScanAndReseek(Path p, FileSystem fs, Reference bottom,
116 CacheConfig cacheConf)
117 throws IOException {
118 final HalfStoreFileReader halfreader = new HalfStoreFileReader(fs, p,
119 cacheConf, bottom, TEST_UTIL.getConfiguration());
120 halfreader.loadFileInfo();
121 final HFileScanner scanner = halfreader.getScanner(false, false);
122
123 scanner.seekTo();
124 KeyValue curr;
125 do {
126 curr = scanner.getKeyValue();
127 KeyValue reseekKv =
128 getLastOnCol(curr);
129 int ret = scanner.reseekTo(reseekKv.getKey());
130 assertTrue("reseek to returned: " + ret, ret > 0);
131
132 } while (scanner.next());
133
134 int ret = scanner.reseekTo(getLastOnCol(curr).getKey());
135
136 assertTrue( ret > 0 );
137
138 halfreader.close(true);
139 }
140
141
142
143 @Test
144 public void testHalfScanner() throws IOException {
145 String root_dir = TEST_UTIL.getDataTestDir().toString();
146 Path p = new Path(root_dir, "test");
147 Configuration conf = TEST_UTIL.getConfiguration();
148 FileSystem fs = FileSystem.get(conf);
149 CacheConfig cacheConf = new CacheConfig(conf);
150 HFileContext meta = new HFileContextBuilder().withBlockSize(1024).build();
151 HFile.Writer w = HFile.getWriterFactory(conf, cacheConf)
152 .withPath(fs, p)
153 .withFileContext(meta)
154 .create();
155
156
157 List<KeyValue> items = genSomeKeys();
158 for (KeyValue kv : items) {
159 w.append(kv);
160 }
161 w.close();
162
163
164 HFile.Reader r = HFile.createReader(fs, p, cacheConf, conf);
165 r.loadFileInfo();
166 byte[] midkey = r.midkey();
167 KeyValue midKV = KeyValue.createKeyValueFromKey(midkey);
168 midkey = midKV.getRow();
169
170 Reference bottom = new Reference(midkey, Reference.Range.bottom);
171 Reference top = new Reference(midkey, Reference.Range.top);
172
173
174 KeyValue beforeMidKey = null;
175 for (KeyValue item : items) {
176 if (KeyValue.COMPARATOR.compare(item, midKV) >= 0) {
177 break;
178 }
179 beforeMidKey = item;
180 }
181 System.out.println("midkey: " + midKV + " or: " + Bytes.toStringBinary(midkey));
182 System.out.println("beforeMidKey: " + beforeMidKey);
183
184
185
186 KeyValue foundKeyValue = doTestOfSeekBefore(p, fs, bottom, midKV, cacheConf);
187 assertEquals(beforeMidKey, foundKeyValue);
188
189
190 foundKeyValue = doTestOfSeekBefore(p, fs, top, items.get(items.size() - 1), cacheConf);
191 assertEquals(items.get(items.size() - 2), foundKeyValue);
192
193 foundKeyValue = doTestOfSeekBefore(p, fs, bottom, items.get(items.size() - 1), cacheConf);
194 assertEquals(beforeMidKey, foundKeyValue);
195
196
197 foundKeyValue = doTestOfSeekBefore(p, fs, top, items.get(0), cacheConf);
198 assertNull(foundKeyValue);
199
200
201 foundKeyValue = doTestOfSeekBefore(p, fs, bottom, items.get(0), cacheConf);
202 assertNull(foundKeyValue);
203
204
205 foundKeyValue = doTestOfSeekBefore(p, fs, top, items.get(1), cacheConf);
206 assertNull(foundKeyValue);
207
208 foundKeyValue = doTestOfSeekBefore(p, fs, bottom, items.get(1), cacheConf);
209 assertEquals(items.get(0), foundKeyValue);
210
211
212 foundKeyValue = doTestOfSeekBefore(p, fs, top, midKV, cacheConf);
213 assertNull(foundKeyValue);
214 }
215
216 private KeyValue doTestOfSeekBefore(Path p, FileSystem fs, Reference bottom, KeyValue seekBefore,
217 CacheConfig cacheConfig)
218 throws IOException {
219 final HalfStoreFileReader halfreader = new HalfStoreFileReader(fs, p,
220 cacheConfig, bottom, TEST_UTIL.getConfiguration());
221 halfreader.loadFileInfo();
222 final HFileScanner scanner = halfreader.getScanner(false, false);
223 scanner.seekBefore(seekBefore.getKey());
224 return scanner.getKeyValue();
225 }
226
227 private KeyValue getLastOnCol(KeyValue curr) {
228 return KeyValue.createLastOnRow(
229 curr.getBuffer(), curr.getRowOffset(), curr.getRowLength(),
230 curr.getBuffer(), curr.getFamilyOffset(), curr.getFamilyLength(),
231 curr.getBuffer(), curr.getQualifierOffset(), curr.getQualifierLength());
232 }
233
234 static final int SIZE = 1000;
235
236 static byte[] _b(String s) {
237 return Bytes.toBytes(s);
238 }
239
240 List<KeyValue> genSomeKeys() {
241 List<KeyValue> ret = new ArrayList<KeyValue>(SIZE);
242 for (int i = 0; i < SIZE; i++) {
243 KeyValue kv =
244 new KeyValue(
245 _b(String.format("row_%04d", i)),
246 _b("family"),
247 _b("qualifier"),
248 1000,
249 _b("value"));
250 ret.add(kv);
251 }
252 return ret;
253 }
254
255
256
257 }
258