001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.filter; 019 020import static org.junit.Assert.*; 021 022import java.util.ArrayList; 023import java.util.HashMap; 024import java.util.HashSet; 025import java.util.List; 026import java.util.Map; 027import java.util.Set; 028import org.apache.hadoop.hbase.*; 029import org.apache.hadoop.hbase.HBaseClassTestRule; 030import org.apache.hadoop.hbase.client.Durability; 031import org.apache.hadoop.hbase.client.Put; 032import org.apache.hadoop.hbase.client.Result; 033import org.apache.hadoop.hbase.client.ResultScanner; 034import org.apache.hadoop.hbase.client.Scan; 035import org.apache.hadoop.hbase.client.Table; 036import org.apache.hadoop.hbase.testclassification.FilterTests; 037import org.apache.hadoop.hbase.testclassification.MediumTests; 038import org.apache.hadoop.hbase.util.Bytes; 039import org.junit.After; 040import org.junit.AfterClass; 041import org.junit.Before; 042import org.junit.BeforeClass; 043import org.junit.ClassRule; 044import org.junit.Rule; 045import org.junit.Test; 046import org.junit.experimental.categories.Category; 047import org.junit.rules.TestName; 048import org.slf4j.Logger; 049import org.slf4j.LoggerFactory; 050 051class StringRange { 052 private String start = null; 053 private String end = null; 054 private boolean startInclusive = true; 055 private boolean endInclusive = false; 056 057 public StringRange(String start, boolean startInclusive, String end, 058 boolean endInclusive) { 059 this.start = start; 060 this.startInclusive = startInclusive; 061 this.end = end; 062 this.endInclusive = endInclusive; 063 } 064 065 public String getStart() { 066 return this.start; 067 } 068 069 public String getEnd() { 070 return this.end; 071 } 072 073 public boolean isStartInclusive() { 074 return this.startInclusive; 075 } 076 077 public boolean isEndInclusive() { 078 return this.endInclusive; 079 } 080 081 @Override 082 public int hashCode() { 083 int hashCode = 0; 084 if (this.start != null) { 085 hashCode ^= this.start.hashCode(); 086 } 087 088 if (this.end != null) { 089 hashCode ^= this.end.hashCode(); 090 } 091 return hashCode; 092 } 093 094 @Override 095 public String toString() { 096 String result = (this.startInclusive ? "[" : "(") 097 + (this.start == null ? null : this.start) + ", " 098 + (this.end == null ? null : this.end) 099 + (this.endInclusive ? "]" : ")"); 100 return result; 101 } 102 103 public boolean inRange(String value) { 104 boolean afterStart = true; 105 if (this.start != null) { 106 int startCmp = value.compareTo(this.start); 107 afterStart = this.startInclusive ? startCmp >= 0 : startCmp > 0; 108 } 109 110 boolean beforeEnd = true; 111 if (this.end != null) { 112 int endCmp = value.compareTo(this.end); 113 beforeEnd = this.endInclusive ? endCmp <= 0 : endCmp < 0; 114 } 115 116 return afterStart && beforeEnd; 117 } 118 119} 120 121 122@Category({FilterTests.class, MediumTests.class}) 123public class TestColumnRangeFilter { 124 125 @ClassRule 126 public static final HBaseClassTestRule CLASS_RULE = 127 HBaseClassTestRule.forClass(TestColumnRangeFilter.class); 128 129 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 130 131 private static final Logger LOG = LoggerFactory.getLogger(TestColumnRangeFilter.class); 132 133 @Rule 134 public TestName name = new TestName(); 135 136 /** 137 * @throws java.lang.Exception 138 */ 139 @BeforeClass 140 public static void setUpBeforeClass() throws Exception { 141 TEST_UTIL.startMiniCluster(); 142 } 143 144 /** 145 * @throws java.lang.Exception 146 */ 147 @AfterClass 148 public static void tearDownAfterClass() throws Exception { 149 TEST_UTIL.shutdownMiniCluster(); 150 } 151 152 /** 153 * @throws java.lang.Exception 154 */ 155 @Before 156 public void setUp() throws Exception { 157 // Nothing to do. 158 } 159 160 /** 161 * @throws java.lang.Exception 162 */ 163 @After 164 public void tearDown() throws Exception { 165 // Nothing to do. 166 } 167 168 @Test 169 public void TestColumnRangeFilterClient() throws Exception { 170 String family = "Family"; 171 Table ht = TEST_UTIL.createTable(TableName.valueOf(name.getMethodName()), 172 Bytes.toBytes(family), Integer.MAX_VALUE); 173 174 List<String> rows = generateRandomWords(10, 8); 175 long maxTimestamp = 2; 176 List<String> columns = generateRandomWords(20000, 8); 177 178 List<KeyValue> kvList = new ArrayList<>(); 179 180 Map<StringRange, List<KeyValue>> rangeMap = new HashMap<>(); 181 182 rangeMap.put(new StringRange(null, true, "b", false), 183 new ArrayList<>()); 184 rangeMap.put(new StringRange("p", true, "q", false), 185 new ArrayList<>()); 186 rangeMap.put(new StringRange("r", false, "s", true), 187 new ArrayList<>()); 188 rangeMap.put(new StringRange("z", false, null, false), 189 new ArrayList<>()); 190 String valueString = "ValueString"; 191 192 for (String row : rows) { 193 Put p = new Put(Bytes.toBytes(row)); 194 p.setDurability(Durability.SKIP_WAL); 195 for (String column : columns) { 196 for (long timestamp = 1; timestamp <= maxTimestamp; timestamp++) { 197 KeyValue kv = KeyValueTestUtil.create(row, family, column, timestamp, 198 valueString); 199 p.add(kv); 200 kvList.add(kv); 201 for (StringRange s : rangeMap.keySet()) { 202 if (s.inRange(column)) { 203 rangeMap.get(s).add(kv); 204 } 205 } 206 } 207 } 208 ht.put(p); 209 } 210 211 TEST_UTIL.flush(); 212 213 ColumnRangeFilter filter; 214 Scan scan = new Scan(); 215 scan.setMaxVersions(); 216 for (StringRange s : rangeMap.keySet()) { 217 filter = new ColumnRangeFilter(s.getStart() == null ? null 218 : Bytes.toBytes(s.getStart()), s.isStartInclusive(), 219 s.getEnd() == null ? null : Bytes.toBytes(s.getEnd()), 220 s.isEndInclusive()); 221 scan.setFilter(filter); 222 ResultScanner scanner = ht.getScanner(scan); 223 List<Cell> results = new ArrayList<>(); 224 LOG.info("scan column range: " + s.toString()); 225 long timeBeforeScan = System.currentTimeMillis(); 226 227 Result result; 228 while ((result = scanner.next()) != null) { 229 for (Cell kv : result.listCells()) { 230 results.add(kv); 231 } 232 } 233 long scanTime = System.currentTimeMillis() - timeBeforeScan; 234 scanner.close(); 235 LOG.info("scan time = " + scanTime + "ms"); 236 LOG.info("found " + results.size() + " results"); 237 LOG.info("Expecting " + rangeMap.get(s).size() + " results"); 238 239 /* 240 for (KeyValue kv : results) { 241 LOG.info("found row " + Bytes.toString(kv.getRow()) + ", column " 242 + Bytes.toString(kv.getQualifier())); 243 } 244 */ 245 246 assertEquals(rangeMap.get(s).size(), results.size()); 247 } 248 ht.close(); 249 } 250 251 List<String> generateRandomWords(int numberOfWords, int maxLengthOfWords) { 252 Set<String> wordSet = new HashSet<>(); 253 for (int i = 0; i < numberOfWords; i++) { 254 int lengthOfWords = (int) (Math.random() * maxLengthOfWords) + 1; 255 char[] wordChar = new char[lengthOfWords]; 256 for (int j = 0; j < wordChar.length; j++) { 257 wordChar[j] = (char) (Math.random() * 26 + 97); 258 } 259 String word = new String(wordChar); 260 wordSet.add(word); 261 } 262 List<String> wordList = new ArrayList<>(wordSet); 263 return wordList; 264 } 265 266} 267