View Javadoc

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