1 package org.apache.hadoop.hbase.filter;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.util.ArrayList;
7 import java.util.HashMap;
8 import java.util.HashSet;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Set;
12
13 import org.apache.hadoop.hbase.HBaseTestingUtility;
14 import org.apache.hadoop.hbase.HColumnDescriptor;
15 import org.apache.hadoop.hbase.HRegionInfo;
16 import org.apache.hadoop.hbase.HTableDescriptor;
17 import org.apache.hadoop.hbase.KeyValue;
18 import org.apache.hadoop.hbase.KeyValueTestUtil;
19 import org.apache.hadoop.hbase.client.Put;
20 import org.apache.hadoop.hbase.client.Scan;
21 import org.apache.hadoop.hbase.regionserver.HRegion;
22 import org.apache.hadoop.hbase.regionserver.InternalScanner;
23 import org.apache.hadoop.hbase.util.Bytes;
24 import org.junit.Test;
25
26 public class TestColumnPrefixFilter {
27
28 private final static HBaseTestingUtility TEST_UTIL = new
29 HBaseTestingUtility();
30
31 @Test
32 public void testColumnPrefixFilter() throws IOException {
33 String family = "Family";
34 HTableDescriptor htd = new HTableDescriptor("TestColumnPrefixFilter");
35 htd.addFamily(new HColumnDescriptor(family));
36 HRegionInfo info = new HRegionInfo(htd, null, null, false);
37 HRegion region = HRegion.createHRegion(info, HBaseTestingUtility.
38 getTestDir(), TEST_UTIL.getConfiguration());
39
40 List<String> rows = generateRandomWords(100, "row");
41 List<String> columns = generateRandomWords(10000, "column");
42 long maxTimestamp = 2;
43
44 List<KeyValue> kvList = new ArrayList<KeyValue>();
45
46 Map<String, List<KeyValue>> prefixMap = new HashMap<String,
47 List<KeyValue>>();
48
49 prefixMap.put("p", new ArrayList<KeyValue>());
50 prefixMap.put("s", new ArrayList<KeyValue>());
51
52 String valueString = "ValueString";
53
54 for (String row: rows) {
55 Put p = new Put(Bytes.toBytes(row));
56 for (String column: columns) {
57 for (long timestamp = 1; timestamp <= maxTimestamp; timestamp++) {
58 KeyValue kv = KeyValueTestUtil.create(row, family, column, timestamp,
59 valueString);
60 p.add(kv);
61 kvList.add(kv);
62 for (String s: prefixMap.keySet()) {
63 if (column.startsWith(s)) {
64 prefixMap.get(s).add(kv);
65 }
66 }
67 }
68 }
69 region.put(p);
70 }
71
72 ColumnPrefixFilter filter;
73 Scan scan = new Scan();
74 scan.setMaxVersions();
75 for (String s: prefixMap.keySet()) {
76 filter = new ColumnPrefixFilter(Bytes.toBytes(s));
77 scan.setFilter(filter);
78 InternalScanner scanner = region.getScanner(scan);
79 List<KeyValue> results = new ArrayList<KeyValue>();
80 while(scanner.next(results));
81 assertEquals(prefixMap.get(s).size(), results.size());
82 }
83 }
84
85 List<String> generateRandomWords(int numberOfWords, String suffix) {
86 Set<String> wordSet = new HashSet<String>();
87 for (int i = 0; i < numberOfWords; i++) {
88 int lengthOfWords = (int) (Math.random()*2) + 1;
89 char[] wordChar = new char[lengthOfWords];
90 for (int j = 0; j < wordChar.length; j++) {
91 wordChar[j] = (char) (Math.random() * 26 + 97);
92 }
93 String word;
94 if (suffix == null) {
95 word = new String(wordChar);
96 } else {
97 word = new String(wordChar) + suffix;
98 }
99 wordSet.add(word);
100 }
101 List<String> wordList = new ArrayList<String>(wordSet);
102 return wordList;
103 }
104 }