1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.filter;
19  
20  import static org.junit.Assert.*;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.HashSet;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Set;
29  
30  import org.apache.hadoop.hbase.HBaseTestingUtility;
31  import org.apache.hadoop.hbase.HColumnDescriptor;
32  import org.apache.hadoop.hbase.HRegionInfo;
33  import org.apache.hadoop.hbase.HTableDescriptor;
34  import org.apache.hadoop.hbase.KeyValue;
35  import org.apache.hadoop.hbase.KeyValueTestUtil;
36  import org.apache.hadoop.hbase.client.Put;
37  import org.apache.hadoop.hbase.client.Scan;
38  import org.apache.hadoop.hbase.regionserver.HRegion;
39  import org.apache.hadoop.hbase.regionserver.InternalScanner;
40  import org.apache.hadoop.hbase.util.Bytes;
41  import org.junit.Test;
42  
43  public class TestColumnPrefixFilter {
44  
45    private final static HBaseTestingUtility TEST_UTIL = new
46        HBaseTestingUtility();
47  
48    @Test
49    public void testColumnPrefixFilter() throws IOException {
50      String family = "Family";
51      HTableDescriptor htd = new HTableDescriptor("TestColumnPrefixFilter");
52      htd.addFamily(new HColumnDescriptor(family));
53      HRegionInfo info = new HRegionInfo(htd.getName(), null, null, false);
54      HRegion region = HRegion.createHRegion(info, TEST_UTIL.
55        getDataTestDir(), TEST_UTIL.getConfiguration(), htd);
56  
57      List<String> rows = generateRandomWords(100, "row");
58      List<String> columns = generateRandomWords(10000, "column");
59      long maxTimestamp = 2;
60  
61      List<KeyValue> kvList = new ArrayList<KeyValue>();
62  
63      Map<String, List<KeyValue>> prefixMap = new HashMap<String,
64          List<KeyValue>>();
65  
66      prefixMap.put("p", new ArrayList<KeyValue>());
67      prefixMap.put("s", new ArrayList<KeyValue>());
68  
69      String valueString = "ValueString";
70  
71      for (String row: rows) {
72        Put p = new Put(Bytes.toBytes(row));
73        p.setWriteToWAL(false);
74        for (String column: columns) {
75          for (long timestamp = 1; timestamp <= maxTimestamp; timestamp++) {
76            KeyValue kv = KeyValueTestUtil.create(row, family, column, timestamp,
77                valueString);
78            p.add(kv);
79            kvList.add(kv);
80            for (String s: prefixMap.keySet()) {
81              if (column.startsWith(s)) {
82                prefixMap.get(s).add(kv);
83              }
84            }
85          }
86        }
87        region.put(p);
88      }
89  
90      ColumnPrefixFilter filter;
91      Scan scan = new Scan();
92      scan.setMaxVersions();
93      for (String s: prefixMap.keySet()) {
94        filter = new ColumnPrefixFilter(Bytes.toBytes(s));
95  
96        scan.setFilter(filter);
97  
98        InternalScanner scanner = region.getScanner(scan);
99        List<KeyValue> results = new ArrayList<KeyValue>();
100       while(scanner.next(results));
101       assertEquals(prefixMap.get(s).size(), results.size());
102     }
103   }
104 
105   @Test
106   public void testColumnPrefixFilterWithFilterList() throws IOException {
107     String family = "Family";
108     HTableDescriptor htd = new HTableDescriptor("TestColumnPrefixFilter");
109     htd.addFamily(new HColumnDescriptor(family));
110     HRegionInfo info = new HRegionInfo(htd.getName(), null, null, false);
111     HRegion region = HRegion.createHRegion(info, TEST_UTIL.
112       getDataTestDir(), TEST_UTIL.getConfiguration(), htd);
113 
114     List<String> rows = generateRandomWords(100, "row");
115     List<String> columns = generateRandomWords(10000, "column");
116     long maxTimestamp = 2;
117 
118     List<KeyValue> kvList = new ArrayList<KeyValue>();
119 
120     Map<String, List<KeyValue>> prefixMap = new HashMap<String,
121         List<KeyValue>>();
122 
123     prefixMap.put("p", new ArrayList<KeyValue>());
124     prefixMap.put("s", new ArrayList<KeyValue>());
125 
126     String valueString = "ValueString";
127 
128     for (String row: rows) {
129       Put p = new Put(Bytes.toBytes(row));
130       p.setWriteToWAL(false);
131       for (String column: columns) {
132         for (long timestamp = 1; timestamp <= maxTimestamp; timestamp++) {
133           KeyValue kv = KeyValueTestUtil.create(row, family, column, timestamp,
134               valueString);
135           p.add(kv);
136           kvList.add(kv);
137           for (String s: prefixMap.keySet()) {
138             if (column.startsWith(s)) {
139               prefixMap.get(s).add(kv);
140             }
141           }
142         }
143       }
144       region.put(p);
145     }
146 
147     ColumnPrefixFilter filter;
148     Scan scan = new Scan();
149     scan.setMaxVersions();
150     for (String s: prefixMap.keySet()) {
151       filter = new ColumnPrefixFilter(Bytes.toBytes(s));
152 
153       //this is how this test differs from the one above
154       FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
155       filterList.addFilter(filter);
156       scan.setFilter(filterList);
157 
158       InternalScanner scanner = region.getScanner(scan);
159       List<KeyValue> results = new ArrayList<KeyValue>();
160       while(scanner.next(results));
161       assertEquals(prefixMap.get(s).size(), results.size());
162     }
163   }
164 
165   List<String> generateRandomWords(int numberOfWords, String suffix) {
166     Set<String> wordSet = new HashSet<String>();
167     for (int i = 0; i < numberOfWords; i++) {
168       int lengthOfWords = (int) (Math.random()*2) + 1;
169       char[] wordChar = new char[lengthOfWords];
170       for (int j = 0; j < wordChar.length; j++) {
171         wordChar[j] = (char) (Math.random() * 26 + 97);
172       }
173       String word;
174       if (suffix == null) {
175         word = new String(wordChar);
176       } else {
177         word = new String(wordChar) + suffix;
178       }
179       wordSet.add(word);
180     }
181     List<String> wordList = new ArrayList<String>(wordSet);
182     return wordList;
183   }
184 }