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