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.*;
31  import org.apache.hadoop.hbase.client.Put;
32  import org.apache.hadoop.hbase.client.Scan;
33  import org.apache.hadoop.hbase.regionserver.HRegion;
34  import org.apache.hadoop.hbase.regionserver.InternalScanner;
35  import org.apache.hadoop.hbase.util.Bytes;
36  import org.junit.Test;
37  import org.junit.experimental.categories.Category;
38  
39  @Category(SmallTests.class)
40  public class TestColumnPrefixFilter {
41  
42    private final static HBaseTestingUtility TEST_UTIL = new
43        HBaseTestingUtility();
44  
45    @Test
46    public void testColumnPrefixFilter() throws IOException {
47      String family = "Family";
48      HTableDescriptor htd = new HTableDescriptor("TestColumnPrefixFilter");
49      htd.addFamily(new HColumnDescriptor(family));
50      HRegionInfo info = new HRegionInfo(htd.getName(), null, null, false);
51      HRegion region = HRegion.createHRegion(info, TEST_UTIL.
52        getDataTestDir(), TEST_UTIL.getConfiguration(), htd);
53      try {
54        List<String> rows = generateRandomWords(100, "row");
55        List<String> columns = generateRandomWords(10000, "column");
56        long maxTimestamp = 2;
57  
58        List<KeyValue> kvList = new ArrayList<KeyValue>();
59  
60        Map<String, List<KeyValue>> prefixMap = new HashMap<String,
61            List<KeyValue>>();
62  
63        prefixMap.put("p", new ArrayList<KeyValue>());
64        prefixMap.put("s", new ArrayList<KeyValue>());
65  
66        String valueString = "ValueString";
67  
68        for (String row: rows) {
69          Put p = new Put(Bytes.toBytes(row));
70          p.setWriteToWAL(false);
71          for (String column: columns) {
72            for (long timestamp = 1; timestamp <= maxTimestamp; timestamp++) {
73              KeyValue kv = KeyValueTestUtil.create(row, family, column, timestamp,
74                  valueString);
75              p.add(kv);
76              kvList.add(kv);
77              for (String s: prefixMap.keySet()) {
78                if (column.startsWith(s)) {
79                  prefixMap.get(s).add(kv);
80                }
81              }
82            }
83          }
84          region.put(p);
85        }
86  
87        ColumnPrefixFilter filter;
88        Scan scan = new Scan();
89        scan.setMaxVersions();
90        for (String s: prefixMap.keySet()) {
91          filter = new ColumnPrefixFilter(Bytes.toBytes(s));
92  
93          scan.setFilter(filter);
94  
95          InternalScanner scanner = region.getScanner(scan);
96          List<KeyValue> results = new ArrayList<KeyValue>();
97          while(scanner.next(results));
98          assertEquals(prefixMap.get(s).size(), results.size());
99        }
100     } finally {
101       region.close();
102       region.getLog().closeAndDelete();
103     }
104 
105     region.close();
106     region.getLog().closeAndDelete();
107   }
108 
109   @Test
110   public void testColumnPrefixFilterWithFilterList() throws IOException {
111     String family = "Family";
112     HTableDescriptor htd = new HTableDescriptor("TestColumnPrefixFilter");
113     htd.addFamily(new HColumnDescriptor(family));
114     HRegionInfo info = new HRegionInfo(htd.getName(), null, null, false);
115     HRegion region = HRegion.createHRegion(info, TEST_UTIL.
116       getDataTestDir(), TEST_UTIL.getConfiguration(), htd);
117     try {
118       List<String> rows = generateRandomWords(100, "row");
119       List<String> columns = generateRandomWords(10000, "column");
120       long maxTimestamp = 2;
121 
122       List<KeyValue> kvList = new ArrayList<KeyValue>();
123 
124       Map<String, List<KeyValue>> prefixMap = new HashMap<String,
125           List<KeyValue>>();
126 
127       prefixMap.put("p", new ArrayList<KeyValue>());
128       prefixMap.put("s", new ArrayList<KeyValue>());
129 
130       String valueString = "ValueString";
131 
132       for (String row: rows) {
133         Put p = new Put(Bytes.toBytes(row));
134         p.setWriteToWAL(false);
135         for (String column: columns) {
136           for (long timestamp = 1; timestamp <= maxTimestamp; timestamp++) {
137             KeyValue kv = KeyValueTestUtil.create(row, family, column, timestamp,
138                 valueString);
139             p.add(kv);
140             kvList.add(kv);
141             for (String s: prefixMap.keySet()) {
142               if (column.startsWith(s)) {
143                 prefixMap.get(s).add(kv);
144               }
145             }
146           }
147         }
148         region.put(p);
149       }
150 
151       ColumnPrefixFilter filter;
152       Scan scan = new Scan();
153       scan.setMaxVersions();
154       for (String s: prefixMap.keySet()) {
155         filter = new ColumnPrefixFilter(Bytes.toBytes(s));
156 
157         //this is how this test differs from the one above
158         FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
159         filterList.addFilter(filter);
160         scan.setFilter(filterList);
161 
162         InternalScanner scanner = region.getScanner(scan);
163         List<KeyValue> results = new ArrayList<KeyValue>();
164         while(scanner.next(results));
165         assertEquals(prefixMap.get(s).size(), results.size());
166       }
167     } finally {
168       region.close();
169       region.getLog().closeAndDelete();
170     }
171 
172     region.close();
173     region.getLog().closeAndDelete();
174   }
175 
176   List<String> generateRandomWords(int numberOfWords, String suffix) {
177     Set<String> wordSet = new HashSet<String>();
178     for (int i = 0; i < numberOfWords; i++) {
179       int lengthOfWords = (int) (Math.random()*2) + 1;
180       char[] wordChar = new char[lengthOfWords];
181       for (int j = 0; j < wordChar.length; j++) {
182         wordChar[j] = (char) (Math.random() * 26 + 97);
183       }
184       String word;
185       if (suffix == null) {
186         word = new String(wordChar);
187       } else {
188         word = new String(wordChar) + suffix;
189       }
190       wordSet.add(word);
191     }
192     List<String> wordList = new ArrayList<String>(wordSet);
193     return wordList;
194   }
195 
196   @org.junit.Rule
197   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
198     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
199 }
200