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